BinaryTreesBindable::bounds_bind
This commit is contained in:
parent
1c589acd85
commit
4f717b01e2
@ -24,6 +24,17 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
|
||||
|
||||
trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
|
||||
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> {
|
||||
@ -70,15 +81,17 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
|
||||
let bounds = reference.bounds.clone();
|
||||
Self::bind(self.0.resolve(&reference.bound), move |node| {
|
||||
let (_, _, key) = ctx.0.split(&node);
|
||||
match bounds.clone().split(&key, ctx.comparator()) {
|
||||
Ok((boundsl, boundsr)) => Self::pure(BoundNode2 {
|
||||
ctx.0.bounds_bind(
|
||||
bounds.clone().split(&key, ctx.comparator()),
|
||||
|(boundsl, boundsr)| {
|
||||
Self::pure(BoundNode2 {
|
||||
boundsl,
|
||||
boundsr,
|
||||
bounds,
|
||||
node,
|
||||
}),
|
||||
Err(e) => ctx.0.bounds_error(e),
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@ -110,8 +123,9 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
|
||||
tree: Self::Tree,
|
||||
key: Self::Key,
|
||||
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
|
||||
match tree.bounds.split(&key, self.comparator()) {
|
||||
Ok((boundsl, boundsr)) => {
|
||||
self.0.bounds_bind(
|
||||
tree.bounds.split(&key, self.comparator()),
|
||||
|(boundsl, boundsr)| {
|
||||
Self::bind(self.0.split_key_empty(tree.bound, key), |(tl, tr)| {
|
||||
Self::pure((
|
||||
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,
|
||||
) -> BTWrap<'a, Self, Self::Node> {
|
||||
let (boundsl, boundsr) = (tl.bounds, tr.bounds);
|
||||
match Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()) {
|
||||
Ok(bounds) => Self::fmap(
|
||||
self.0.bounds_bind(
|
||||
Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()),
|
||||
|bounds| {
|
||||
Self::fmap(
|
||||
self.0.join_key_unbalanced(tl.bound, key, tr.bound),
|
||||
|node| BoundNode2 {
|
||||
boundsl,
|
||||
@ -155,8 +170,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesAvl<'a>> BinaryTreesAvl<'a> fo
|
||||
bounds,
|
||||
node,
|
||||
},
|
||||
),
|
||||
Err(e) => self.0.bounds_error(e),
|
||||
}
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user