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 equal(&self, rhs: &Self::Reference, lhs: &Self::Reference) -> bool;
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> {
@ -49,15 +74,15 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
key: KeyRc<'a, Self>,
tr: 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> {
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 {
return <Self::T as Pure>::pure(tl)
return Self::pure(tl)
};
Self::T::bind2(self.resolve(&rl), self.resolve(&rr), move |nl, nr| {
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 = {
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 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
})
@ -88,17 +113,13 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> {
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
let (tl, tr, k) = self.split(&node);
match self.comparator().pick_smaller(&key, &k) {
Comparison::L => {
<Self::T as Monad>::bind(self.clone().split_key(tl, key), move |(tll, tlr)| {
<Self::T as Functor>::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t))
})
}
Comparison::E => <Self::T as Pure>::pure((tl, tr)),
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))
})
}
Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| {
Self::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)| {
Self::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>,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
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)
}),
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> {
<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)
})
}
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> {
<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)
})
}
@ -157,7 +178,7 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
key: KeyRc<'a, Self>,
tr: 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)
})
}
@ -171,11 +192,11 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
let (lh, rh) = (self.height(&tl), self.height(&tr));
match (lh.saturating_sub(rh), rh.saturating_sub(lh)) {
(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 (rlh, rrh) = (self.height(&trl), self.height(&trr));
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);
Self::T::bind2(
self.clone().join_key_balanced_tree(tl, key, trll),
@ -184,17 +205,16 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
)
})
} else {
<Self::T as Monad>::bind(
self.clone().join_key_balanced_tree(tl, key, trl),
|t| self.join_key_balanced(t, kr, trr),
)
Self::bind(self.clone().join_key_balanced_tree(tl, key, trl), |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 (llh, lrh) = (self.height(&tll), self.height(&tlr));
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);
Self::T::bind2(
self.clone().join_key_balanced_tree(tll, kl, tlrl),
@ -203,10 +223,9 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> {
)
})
} else {
<Self::T as Monad>::bind(
self.clone().join_key_balanced_tree(tlr, key, tr),
|t| self.join_key_balanced(tll, kl, t),
)
Self::bind(self.clone().join_key_balanced_tree(tlr, key, tr), |t| {
self.join_key_balanced(tll, kl, t)
})
}
}),
(_, _) => 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> {
match node.into_tree() {
Ok(tree) => <Self::T as Pure>::pure(tree),
Err(e) => <Self::T as Fail<_>>::fail(ResolutionError::Parse(e)),
Ok(tree) => Self::pure(tree),
Err(e) => Self::fail(ResolutionError::Parse(e)),
}
}