more uniform name s
This commit is contained in:
parent
99a00d0170
commit
c565f0b6dc
@ -29,7 +29,7 @@ pub trait BinaryTrees<'a>: 'a + Clone {
|
|||||||
fn split(&self, node: &Self::Node) -> Split<'a, Self>;
|
fn split(&self, node: &Self::Node) -> Split<'a, Self>;
|
||||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree>;
|
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree>;
|
||||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
|
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
|
||||||
fn equal(&self, rhs: &Self::Reference, lhs: &Self::Reference) -> bool;
|
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
|
||||||
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
|
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
|
||||||
|
|
||||||
fn bind<A: 'a, B: 'a>(
|
fn bind<A: 'a, B: 'a>(
|
||||||
@ -192,8 +192,8 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
|
|||||||
key: KeyRc<'a, Self>,
|
key: KeyRc<'a, Self>,
|
||||||
tr: Self::Tree,
|
tr: Self::Tree,
|
||||||
) -> BTWrap<'a, Self, Self::Node> {
|
) -> BTWrap<'a, Self, Self::Node> {
|
||||||
let (lh, rh) = (self.height(&tl), self.height(&tr));
|
let (hl, hr) = (self.height(&tl), self.height(&tr));
|
||||||
match (lh.saturating_sub(rh), rh.saturating_sub(lh)) {
|
match (hl.saturating_sub(hr), hr.saturating_sub(hl)) {
|
||||||
(0, 0) | (0, 1) | (1, 0) => self.join_key_unbalanced(tl, key, tr),
|
(0, 0) | (0, 1) | (1, 0) => self.join_key_unbalanced(tl, key, tr),
|
||||||
(0, _) => Self::bind(self.assume_node(&tr), move |nr| {
|
(0, _) => Self::bind(self.assume_node(&tr), move |nr| {
|
||||||
let (trl, trr, kr) = self.split(&nr);
|
let (trl, trr, kr) = self.split(&nr);
|
||||||
@ -215,8 +215,8 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
|
|||||||
}),
|
}),
|
||||||
(_, 0) => Self::bind(self.assume_node(&tl), move |nl| {
|
(_, 0) => Self::bind(self.assume_node(&tl), move |nl| {
|
||||||
let (tll, tlr, kl) = self.split(&nl);
|
let (tll, tlr, kl) = self.split(&nl);
|
||||||
let (llh, lrh) = (self.height(&tll), self.height(&tlr));
|
let (hll, hlr) = (self.height(&tll), self.height(&tlr));
|
||||||
if llh < lrh {
|
if hll < hlr {
|
||||||
Self::bind(self.assume_node(&tlr), move |nlr| {
|
Self::bind(self.assume_node(&tlr), move |nlr| {
|
||||||
let (tlrl, tlrr, klr) = self.split(&nlr);
|
let (tlrl, tlrr, klr) = self.split(&nlr);
|
||||||
Self::T::bind2(
|
Self::T::bind2(
|
||||||
|
@ -29,8 +29,8 @@ impl<A: Display> Display for AvlN<A> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
impl<A> AvlN<A> {
|
impl<A> AvlN<A> {
|
||||||
fn balanced(&self) -> bool {
|
fn balanced(&self) -> bool {
|
||||||
let (lh, rh) = (self.l.height, self.r.height);
|
let (hl, hr) = (self.l.height, self.r.height);
|
||||||
std::cmp::max(lh, rh) - std::cmp::min(lh, rh) < 2 && self.l.balanced() && self.r.balanced()
|
std::cmp::max(hl, hr) - std::cmp::min(hl, hr) < 2 && self.l.balanced() && self.r.balanced()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ pub trait Effect {
|
|||||||
fn e_pure() -> Self;
|
fn e_pure() -> Self;
|
||||||
|
|
||||||
/// Used in [`ApplicativeLA2::la2`] and other [`Applicative`] methods.
|
/// Used in [`ApplicativeLA2::la2`] and other [`Applicative`] methods.
|
||||||
fn e_seq(lhs: Self, rhs: Self) -> Self;
|
fn e_seq(el: Self, er: Self) -> Self;
|
||||||
|
|
||||||
/// Used in [`Monad::bind`] and other [`Monad`] methods.
|
/// Used in [`Monad::bind`] and other [`Monad`] methods.
|
||||||
fn e_after(self, effect: Self) -> Self;
|
fn e_after(self, effect: Self) -> Self;
|
||||||
|
@ -21,14 +21,14 @@ impl<'a> EvalTree<'a> {
|
|||||||
fn next(self) -> Oet<'a> {
|
fn next(self) -> Oet<'a> {
|
||||||
match self {
|
match self {
|
||||||
EvalTree::Atom(f) => f(),
|
EvalTree::Atom(f) => f(),
|
||||||
EvalTree::Composite(left, right) => match *left {
|
EvalTree::Composite(etl, etr) => match *etl {
|
||||||
EvalTree::Atom(f) => match f() {
|
EvalTree::Atom(f) => match f() {
|
||||||
Some(newleft) => Some(EvalTree::Composite(Box::new(newleft), right)),
|
Some(newleft) => Some(EvalTree::Composite(Box::new(newleft), etr)),
|
||||||
None => Some(*right),
|
None => Some(*etr),
|
||||||
},
|
},
|
||||||
EvalTree::Composite(leftleft, leftright) => Some(EvalTree::Composite(
|
EvalTree::Composite(etll, etlr) => Some(EvalTree::Composite(
|
||||||
leftleft,
|
etll,
|
||||||
Box::new(EvalTree::Composite(leftright, right)),
|
Box::new(EvalTree::Composite(etlr, etr)),
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -61,14 +61,14 @@ impl<'a, A: 'a> Stackless<'a, A> {
|
|||||||
/// the preferred way to chain [`Stackless<A>`] and `FnOnce(A) -> Stackless<B>` into [`Stackless<B>`].
|
/// the preferred way to chain [`Stackless<A>`] and `FnOnce(A) -> Stackless<B>` into [`Stackless<B>`].
|
||||||
pub fn bind<B: 'a>(self, f: impl 'a + FnOnce(A) -> Stackless<'a, B>) -> Stackless<'a, B> {
|
pub fn bind<B: 'a>(self, f: impl 'a + FnOnce(A) -> Stackless<'a, B>) -> Stackless<'a, B> {
|
||||||
Stackless(Box::new(|takesb| {
|
Stackless(Box::new(|takesb| {
|
||||||
let lcell = Rc::new(Cell::new(None));
|
let cell_l = Rc::new(Cell::new(None));
|
||||||
let rcell = lcell.clone();
|
let cell_r = cell_l.clone();
|
||||||
Some(EvalTree::Composite(
|
Some(EvalTree::Composite(
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
self.call(move |a| set_cell(lcell, a))
|
self.call(move |a| set_cell(cell_l, a))
|
||||||
}))),
|
}))),
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
let stackless = f(get_cell(rcell));
|
let stackless = f(get_cell(cell_r));
|
||||||
Some(EvalTree::Atom(Box::new(|| stackless.0(takesb))))
|
Some(EvalTree::Atom(Box::new(|| stackless.0(takesb))))
|
||||||
}))),
|
}))),
|
||||||
))
|
))
|
||||||
@ -78,14 +78,14 @@ impl<'a, A: 'a> Stackless<'a, A> {
|
|||||||
/// Method-like equivalent of [`Functor::fmap`].
|
/// Method-like equivalent of [`Functor::fmap`].
|
||||||
pub fn map<B: 'a>(self, f: impl 'a + FnOnce(A) -> B) -> Stackless<'a, B> {
|
pub fn map<B: 'a>(self, f: impl 'a + FnOnce(A) -> B) -> Stackless<'a, B> {
|
||||||
Stackless(Box::new(|takesb| {
|
Stackless(Box::new(|takesb| {
|
||||||
let lcell = Rc::new(Cell::new(None));
|
let cell_l = Rc::new(Cell::new(None));
|
||||||
let rcell = lcell.clone();
|
let cell_r = cell_l.clone();
|
||||||
Some(EvalTree::Composite(
|
Some(EvalTree::Composite(
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
self.call(move |a| set_cell(lcell, a))
|
self.call(move |a| set_cell(cell_l, a))
|
||||||
}))),
|
}))),
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
let b = f(get_cell(rcell));
|
let b = f(get_cell(cell_r));
|
||||||
Some(EvalTree::Atom(Box::new(|| {
|
Some(EvalTree::Atom(Box::new(|| {
|
||||||
takesb(b);
|
takesb(b);
|
||||||
None
|
None
|
||||||
@ -209,14 +209,14 @@ impl<'a> Monad<'a> for StacklessInstance {
|
|||||||
|
|
||||||
fn join<A: 'a>(ffa: Self::F<Self::F<A>>) -> Self::F<A> {
|
fn join<A: 'a>(ffa: Self::F<Self::F<A>>) -> Self::F<A> {
|
||||||
Stackless(Box::new(|takesa| {
|
Stackless(Box::new(|takesa| {
|
||||||
let lcell = Rc::new(Cell::new(None));
|
let cell_l = Rc::new(Cell::new(None));
|
||||||
let rcell = lcell.clone();
|
let cell_r = cell_l.clone();
|
||||||
Some(EvalTree::Composite(
|
Some(EvalTree::Composite(
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
ffa.call(move |a| set_cell(lcell, a))
|
ffa.call(move |a| set_cell(cell_l, a))
|
||||||
}))),
|
}))),
|
||||||
Box::new(EvalTree::Atom(Box::new(move || {
|
Box::new(EvalTree::Atom(Box::new(move || {
|
||||||
let stackless = get_cell(rcell);
|
let stackless = get_cell(cell_r);
|
||||||
Some(EvalTree::Atom(Box::new(|| stackless.0(takesa))))
|
Some(EvalTree::Atom(Box::new(|| stackless.0(takesa))))
|
||||||
}))),
|
}))),
|
||||||
))
|
))
|
||||||
|
@ -64,7 +64,7 @@ impl Add for R {
|
|||||||
success: self.success + rhs.success,
|
success: self.success + rhs.success,
|
||||||
total: self.total + rhs.total,
|
total: self.total + rhs.total,
|
||||||
result: match (self.result, rhs.result) {
|
result: match (self.result, rhs.result) {
|
||||||
(Err(le), Err(re)) => Err(le + "\n" + re.as_str()),
|
(Err(el), Err(er)) => Err(el + "\n" + er.as_str()),
|
||||||
(e, Ok(_)) => e,
|
(e, Ok(_)) => e,
|
||||||
(Ok(_), e) => e,
|
(Ok(_), e) => e,
|
||||||
},
|
},
|
||||||
@ -77,12 +77,12 @@ impl AddAssign<R> for R {
|
|||||||
self.success += rhs.success;
|
self.success += rhs.success;
|
||||||
self.total += rhs.total;
|
self.total += rhs.total;
|
||||||
match (&mut self.result, rhs.result) {
|
match (&mut self.result, rhs.result) {
|
||||||
(Err(ref mut le), Err(re)) => {
|
(Err(ref mut el), Err(er)) => {
|
||||||
*le += "\n";
|
*el += "\n";
|
||||||
*le += re.as_str()
|
*el += er.as_str()
|
||||||
}
|
}
|
||||||
(_, Ok(_)) => {}
|
(_, Ok(_)) => {}
|
||||||
(lr, Err(re)) => *lr = Err(re),
|
(rl, Err(er)) => *rl = Err(er),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,17 +19,17 @@ impl<'a> From<&'a [u8]> for SliceDeserializer<'a> {
|
|||||||
|
|
||||||
impl<'a> Deserializer for SliceDeserializer<'a> {
|
impl<'a> Deserializer for SliceDeserializer<'a> {
|
||||||
fn read_n(&mut self, n: usize) -> &[u8] {
|
fn read_n(&mut self, n: usize) -> &[u8] {
|
||||||
let (left, right) = self.slice.split_at(min(n, self.slice.len()));
|
let (slice_l, slice_r) = self.slice.split_at(min(n, self.slice.len()));
|
||||||
self.slice = right;
|
self.slice = slice_r;
|
||||||
self.pos += left.len();
|
self.pos += slice_l.len();
|
||||||
left
|
slice_l
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_all(&mut self) -> &[u8] {
|
fn read_all(&mut self) -> &[u8] {
|
||||||
let left = self.slice;
|
let slice_l = self.slice;
|
||||||
self.slice = &[];
|
self.slice = &[];
|
||||||
self.pos += left.len();
|
self.pos += slice_l.len();
|
||||||
left
|
slice_l
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tell(&self) -> usize {
|
fn tell(&self) -> usize {
|
||||||
|
@ -51,7 +51,7 @@ impl<E: Display> Display for AvlError<E> {
|
|||||||
Self::LeafHeight(height) => {
|
Self::LeafHeight(height) => {
|
||||||
f.write_fmt(format_args!("invalid AVL leaf height: {height}!=0."))
|
f.write_fmt(format_args!("invalid AVL leaf height: {height}!=0."))
|
||||||
}
|
}
|
||||||
Self::Balance(lh, rh) => f.write_fmt(format_args!("unbalanced AVL node: {lh} {rh}.")),
|
Self::Balance(hl, hr) => f.write_fmt(format_args!("unbalanced AVL node: {hl} {hr}.")),
|
||||||
Self::HeightOverflow => f.write_fmt(format_args!("AVL tree height overflow")),
|
Self::HeightOverflow => f.write_fmt(format_args!("AVL tree height overflow")),
|
||||||
Self::HeightMismatch { child, parent } => f.write_fmt(format_args!(
|
Self::HeightMismatch { child, parent } => f.write_fmt(format_args!(
|
||||||
"AVL child-parent height mismatch: {child:?}, {parent}"
|
"AVL child-parent height mismatch: {child:?}, {parent}"
|
||||||
@ -120,9 +120,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Avl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn balanced<E>(lh: u64, rh: u64) -> Result<(), AvlError<E>> {
|
fn balanced<E>(hl: u64, hr: u64) -> Result<(), AvlError<E>> {
|
||||||
if std::cmp::max(lh, rh) - std::cmp::min(lh, rh) > 1 {
|
if std::cmp::max(hl, hr) - std::cmp::min(hl, hr) > 1 {
|
||||||
return Err(AvlError::Balance(lh, rh));
|
return Err(AvlError::Balance(hl, hr));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -113,28 +113,31 @@ impl<A> Bounds<A> {
|
|||||||
) -> Result<Self, BoundsError<A>> {
|
) -> Result<Self, BoundsError<A>> {
|
||||||
if let Some(lr) = &l.r {
|
if let Some(lr) = &l.r {
|
||||||
Self::ordered(lr, key, comparator)?
|
Self::ordered(lr, key, comparator)?
|
||||||
|
} else if let Some(ll) = &l.l {
|
||||||
|
Self::ordered(ll, key, comparator)?
|
||||||
}
|
}
|
||||||
if let Some(rl) = &r.l {
|
if let Some(rl) = &r.l {
|
||||||
Self::ordered(key, rl, comparator)?
|
Self::ordered(key, rl, comparator)?
|
||||||
|
} else if let Some(rr) = &r.r {
|
||||||
|
Self::ordered(key, rr, comparator)?
|
||||||
}
|
}
|
||||||
Self::new(l.l, r.r, comparator)
|
Self::new(l.l, r.r, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn equal_bound(
|
pub fn equal_bound(
|
||||||
lhs: &Option<Rc<A>>,
|
l: &Option<Rc<A>>,
|
||||||
rhs: &Option<Rc<A>>,
|
r: &Option<Rc<A>>,
|
||||||
comparator: &impl Comparator<A>,
|
comparator: &impl Comparator<A>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match (lhs, rhs) {
|
match (l, r) {
|
||||||
(None, None) => true,
|
(None, None) => true,
|
||||||
(Some(kl), Some(kr)) => matches!(comparator.pick_smaller(kl, kr), Comparison::E),
|
(Some(kl), Some(kr)) => matches!(comparator.pick_smaller(kl, kr), Comparison::E),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn equal(lhs: &Self, rhs: &Self, comparator: &impl Comparator<A>) -> bool {
|
pub fn equal(bl: &Self, br: &Self, comparator: &impl Comparator<A>) -> bool {
|
||||||
Self::equal_bound(&lhs.l, &rhs.l, comparator)
|
Self::equal_bound(&bl.l, &br.l, comparator) && Self::equal_bound(&bl.r, &br.r, comparator)
|
||||||
&& Self::equal_bound(&lhs.r, &rhs.r, comparator)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,7 +276,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> BoundReference<'a, Ctx, A> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn equal(lhs: &Self, rhs: &Self, comparator: &impl Comparator<A>) -> bool {
|
pub fn equal(rl: &Self, rr: &Self, comparator: &impl Comparator<A>) -> bool {
|
||||||
lhs.reference == rhs.reference && Bounds::equal(&lhs.bounds, &rhs.bounds, comparator)
|
rl.reference == rr.reference && Bounds::equal(&rl.bounds, &rr.bounds, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,8 +50,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> Binar
|
|||||||
Ctx::stuff(reference.resolve(self.comparator.clone()))
|
Ctx::stuff(reference.resolve(self.comparator.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn equal(&self, rhs: &Self::Reference, lhs: &Self::Reference) -> bool {
|
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
|
||||||
BoundReference::equal(lhs, rhs, self.comparator.as_ref())
|
BoundReference::equal(rl, rr, self.comparator.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
|
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
|
||||||
|
@ -31,8 +31,8 @@ impl instances::effect::Effect for TraceBox {
|
|||||||
TraceBox::pure()
|
TraceBox::pure()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn e_seq(lhs: Self, rhs: Self) -> Self {
|
fn e_seq(el: Self, er: Self) -> Self {
|
||||||
TraceBox::parallel(lhs, rhs)
|
TraceBox::parallel(el, er)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn e_after(self, effect: Self) -> Self {
|
fn e_after(self, effect: Self) -> Self {
|
||||||
|
@ -29,8 +29,8 @@ impl instances::effect::Effect for usize {
|
|||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn e_seq(lhs: Self, rhs: Self) -> Self {
|
fn e_seq(el: Self, er: Self) -> Self {
|
||||||
max(lhs, rhs)
|
max(el, er)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn e_after(self, effect: Self) -> Self {
|
fn e_after(self, effect: Self) -> Self {
|
||||||
|
Loading…
Reference in New Issue
Block a user