BinaryTrees func shortcuts

This commit is contained in:
AF 2023-05-30 12:53:28 +00:00
parent 7b8221b4c3
commit 354c2363c8
2 changed files with 54 additions and 35 deletions

View File

@ -31,6 +31,31 @@ pub trait BinaryTrees<'a>: 'a + Clone {
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, rhs: &Self::Reference, lhs: &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>(
fa: BTWrap<'a, Self, A>,
f: impl 'a + FnOnce(A) -> BTWrap<'a, Self, B>,
) -> BTWrap<'a, Self, B> {
<Self::T as Monad>::bind(fa, f)
}
fn pure<A: 'a>(a: A) -> BTWrap<'a, Self, A> {
<Self::T as Pure>::pure(a)
}
fn fmap<A: 'a, B: 'a>(
fa: BTWrap<'a, Self, A>,
f: impl 'a + FnOnce(A) -> B,
) -> BTWrap<'a, Self, B> {
<Self::T as Functor>::fmap(fa, f)
}
fn fail<A: 'a, E: 'a>(e: E) -> BTWrap<'a, Self, A>
where
Self::T: Fail<'a, E>,
{
<Self::T as Fail<_>>::fail(e)
}
} }
pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
@ -49,15 +74,15 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
key: KeyRc<'a, Self>, key: KeyRc<'a, Self>,
tr: Self::Tree, tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Tree> { ) -> BTWrap<'a, Self, Self::Tree> {
<Self::T as Monad>::bind(self.clone().join_key(tl, key, tr), move |n| self.tree_of(n)) Self::bind(self.clone().join_key(tl, key, tr), move |n| self.tree_of(n))
} }
fn join(self, tl: Self::Tree, tr: Self::Tree) -> BTWrap<'a, Self, Self::Tree> { fn join(self, tl: Self::Tree, tr: Self::Tree) -> BTWrap<'a, Self, Self::Tree> {
let Some(rl) = self.refer(&tl) else { let Some(rl) = self.refer(&tl) else {
return <Self::T as Pure>::pure(tr) return Self::pure(tr)
}; };
let Some(rr) = self.refer(&tr) else { let Some(rr) = self.refer(&tr) else {
return <Self::T as Pure>::pure(tl) return Self::pure(tl)
}; };
Self::T::bind2(self.resolve(&rl), self.resolve(&rr), move |nl, nr| { Self::T::bind2(self.resolve(&rl), self.resolve(&rr), move |nl, nr| {
let (tll, tlr, kl) = self.split(&nl); let (tll, tlr, kl) = self.split(&nl);
@ -65,11 +90,11 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
let ft = self.clone().join(tlr, trl); let ft = self.clone().join(tlr, trl);
let ft = { let ft = {
let ctx = self.clone(); let ctx = self.clone();
<Self::T as Monad>::bind(ft, move |t| ctx.join_key_tree(tll, kl, t)) Self::bind(ft, move |t| ctx.join_key_tree(tll, kl, t))
}; };
let ft = { let ft = {
let ctx = self.clone(); let ctx = self.clone();
<Self::T as Monad>::bind(ft, move |t| ctx.join_key_tree(t, kr, trr)) Self::bind(ft, move |t| ctx.join_key_tree(t, kr, trr))
}; };
ft ft
}) })
@ -88,17 +113,13 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
let (tl, tr, k) = self.split(&node); let (tl, tr, k) = self.split(&node);
match self.comparator().pick_smaller(&key, &k) { match self.comparator().pick_smaller(&key, &k) {
Comparison::L => { Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| {
<Self::T as Monad>::bind(self.clone().split_key(tl, key), move |(tll, tlr)| { Self::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t))
<Self::T as Functor>::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t)) }),
}) Comparison::E => Self::pure((tl, tr)),
} Comparison::R => Self::bind(self.clone().split_key(tr, key), move |(trl, trr)| {
Comparison::E => <Self::T as Pure>::pure((tl, tr)), Self::fmap(self.join_key_tree(tl, k, trl), |t| (t, trr))
Comparison::R => { }),
<Self::T as Monad>::bind(self.clone().split_key(tr, key), move |(trl, trr)| {
<Self::T as Functor>::fmap(self.join_key_tree(tl, k, trl), |t| (t, trr))
})
}
} }
} }
@ -108,7 +129,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
key: KeyRc<'a, Self>, key: KeyRc<'a, Self>,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
match self.refer(&tree) { match self.refer(&tree) {
Some(reference) => <Self::T as Monad>::bind(self.resolve(&reference), |node| { Some(reference) => Self::bind(self.resolve(&reference), |node| {
self.split_key_node(node, key) self.split_key_node(node, key)
}), }),
None => self.split_key_empty(tree, key), None => self.split_key_empty(tree, key),
@ -116,17 +137,17 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
} }
fn add(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Node> { fn add(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Node> {
<Self::T as Monad>::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { Self::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| {
self.join_key(tl, key, tr) self.join_key(tl, key, tr)
}) })
} }
fn add_tree(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> { fn add_tree(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> {
<Self::T as Monad>::bind(self.clone().add(tree, key), move |n| self.tree_of(n)) Self::bind(self.clone().add(tree, key), move |n| self.tree_of(n))
} }
fn remove(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> { fn remove(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> {
<Self::T as Monad>::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { Self::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| {
self.join(tl, tr) self.join(tl, tr)
}) })
} }
@ -157,7 +178,7 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
key: KeyRc<'a, Self>, key: KeyRc<'a, Self>,
tr: Self::Tree, tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Tree> { ) -> BTWrap<'a, Self, Self::Tree> {
<Self::T as Monad>::bind(self.clone().join_key_balanced(tl, key, tr), move |node| { Self::bind(self.clone().join_key_balanced(tl, key, tr), move |node| {
self.tree_of(node) self.tree_of(node)
}) })
} }
@ -171,11 +192,11 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
let (lh, rh) = (self.height(&tl), self.height(&tr)); let (lh, rh) = (self.height(&tl), self.height(&tr));
match (lh.saturating_sub(rh), rh.saturating_sub(lh)) { match (lh.saturating_sub(rh), rh.saturating_sub(lh)) {
(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::T as Monad>::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);
let (rlh, rrh) = (self.height(&trl), self.height(&trr)); let (rlh, rrh) = (self.height(&trl), self.height(&trr));
if rlh > rrh { if rlh > rrh {
<Self::T as Monad>::bind(self.assume_node(&trl), move |nrl| { Self::bind(self.assume_node(&trl), move |nrl| {
let (trll, trlr, krl) = self.split(&nrl); let (trll, trlr, krl) = self.split(&nrl);
Self::T::bind2( Self::T::bind2(
self.clone().join_key_balanced_tree(tl, key, trll), self.clone().join_key_balanced_tree(tl, key, trll),
@ -184,17 +205,16 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
) )
}) })
} else { } else {
<Self::T as Monad>::bind( Self::bind(self.clone().join_key_balanced_tree(tl, key, trl), |t| {
self.clone().join_key_balanced_tree(tl, key, trl), self.join_key_balanced(t, kr, trr)
|t| self.join_key_balanced(t, kr, trr), })
)
} }
}), }),
(_, 0) => <Self::T as Monad>::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 (llh, lrh) = (self.height(&tll), self.height(&tlr));
if llh < lrh { if llh < lrh {
<Self::T as Monad>::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(
self.clone().join_key_balanced_tree(tll, kl, tlrl), self.clone().join_key_balanced_tree(tll, kl, tlrl),
@ -203,10 +223,9 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
) )
}) })
} else { } else {
<Self::T as Monad>::bind( Self::bind(self.clone().join_key_balanced_tree(tlr, key, tr), |t| {
self.clone().join_key_balanced_tree(tlr, key, tr), self.join_key_balanced(tll, kl, t)
|t| self.join_key_balanced(tll, kl, t), })
)
} }
}), }),
(_, _) => unreachable!(), (_, _) => unreachable!(),

View File

@ -41,8 +41,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> Binar
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> { fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
match node.into_tree() { match node.into_tree() {
Ok(tree) => <Self::T as Pure>::pure(tree), Ok(tree) => Self::pure(tree),
Err(e) => <Self::T as Fail<_>>::fail(ResolutionError::Parse(e)), Err(e) => Self::fail(ResolutionError::Parse(e)),
} }
} }