BinaryTreesBindable::bounds_bind

This commit is contained in:
AF 2023-06-16 09:17:25 +00:00
parent 1c589acd85
commit 4f717b01e2

View File

@ -24,6 +24,17 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
trait BinaryTreesBindable<'a>: BinaryTrees<'a> { trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>; fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>;
fn bounds_bind<A: 'a, B: 'a>(
&self,
ra: Result<A, BoundsError<Self::Key>>,
f: impl FnOnce(A) -> BTWrap<'a, Self, B>,
) -> BTWrap<'a, Self, B> {
match ra {
Ok(a) => f(a),
Err(e) => self.bounds_error(e),
}
}
} }
impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> { impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
@ -70,15 +81,17 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
let bounds = reference.bounds.clone(); let bounds = reference.bounds.clone();
Self::bind(self.0.resolve(&reference.bound), move |node| { Self::bind(self.0.resolve(&reference.bound), move |node| {
let (_, _, key) = ctx.0.split(&node); let (_, _, key) = ctx.0.split(&node);
match bounds.clone().split(&key, ctx.comparator()) { ctx.0.bounds_bind(
Ok((boundsl, boundsr)) => Self::pure(BoundNode2 { bounds.clone().split(&key, ctx.comparator()),
|(boundsl, boundsr)| {
Self::pure(BoundNode2 {
boundsl, boundsl,
boundsr, boundsr,
bounds, bounds,
node, node,
}), })
Err(e) => ctx.0.bounds_error(e), },
} )
}) })
} }
@ -110,8 +123,9 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
tree: Self::Tree, tree: Self::Tree,
key: Self::Key, key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
match tree.bounds.split(&key, self.comparator()) { self.0.bounds_bind(
Ok((boundsl, boundsr)) => { tree.bounds.split(&key, self.comparator()),
|(boundsl, boundsr)| {
Self::bind(self.0.split_key_empty(tree.bound, key), |(tl, tr)| { Self::bind(self.0.split_key_empty(tree.bound, key), |(tl, tr)| {
Self::pure(( Self::pure((
Bound { Bound {
@ -124,9 +138,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
}, },
)) ))
}) })
} },
Err(e) => self.0.bounds_error(e), )
}
} }
} }
@ -146,8 +159,10 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesAvl<'a>> BinaryTreesAvl<'a> fo
tr: Self::Tree, tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Node> { ) -> BTWrap<'a, Self, Self::Node> {
let (boundsl, boundsr) = (tl.bounds, tr.bounds); let (boundsl, boundsr) = (tl.bounds, tr.bounds);
match Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()) { self.0.bounds_bind(
Ok(bounds) => Self::fmap( Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()),
|bounds| {
Self::fmap(
self.0.join_key_unbalanced(tl.bound, key, tr.bound), self.0.join_key_unbalanced(tl.bound, key, tr.bound),
|node| BoundNode2 { |node| BoundNode2 {
boundsl, boundsl,
@ -155,8 +170,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesAvl<'a>> BinaryTreesAvl<'a> fo
bounds, bounds,
node, node,
}, },
), )
Err(e) => self.0.bounds_error(e), },
} )
} }
} }