diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 989eff7..ffbfd49 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -23,16 +23,17 @@ pub trait BinaryTrees: Clone { type Tree: Send; type Key: Send + Clone; type Comparator: Comparator; + + fn comparator(&self) -> &Self::Comparator; + fn split(&self, node: &Self::Node) -> Split; + fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool; + fn refer(&self, tree: &Self::Tree) -> Option; } pub trait MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees { type _Tm: Monad<'a>; - fn comparator(&self) -> &Self::Comparator; - fn split(&self, node: &Self::Node) -> Split; fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>; - fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool; - fn refer(&self, tree: &Self::Tree) -> Option; } pub trait BinaryTreesTreeOf<'a>: MonadTrees<'a> { @@ -146,8 +147,11 @@ impl Display for HeightError { } } -pub trait BinaryTreesHeight<'a>: MonadTrees<'a> { +pub trait TreesHeight: BinaryTrees { fn height(&self, tree: &Self::Tree) -> u64; +} + +pub trait TreesHeightError<'a>: MonadTrees<'a> + TreesHeight { fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T>; } diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index 97b8871..5c74ea3 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -56,7 +56,7 @@ fn balancing(hl: u64, hr: u64) -> Balancing { } pub trait BinaryTreesAvl<'a>: - BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a> + TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a> { fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> { match self.refer(tree) { @@ -116,7 +116,7 @@ pub trait BinaryTreesAvl<'a>: } } -impl<'a, BT: BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>> +impl<'a, BT: TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>> BinaryTreesAvl<'a> for BT { } diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index c36fed7..967d212 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use super::{avl::*, bound::*, *}; -pub trait BinaryTreesUnbalanced<'a>: BinaryTreesHeight<'a> { +pub trait BinaryTreesUnbalanced<'a>: TreesHeightError<'a> { fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree>; fn balancing_error(&self, error: BalancingError) -> BTWrap<'a, Self, T>; @@ -84,16 +84,12 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> { } } -impl BinaryTrees for BalancedTrees { +impl BinaryTrees for BalancedTrees { type Node = BT::Node; type Reference = (BT::Reference, u64); type Tree = BT::Tree; type Key = BT::Key; type Comparator = BT::Comparator; -} - -impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { - type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { self.0.comparator() @@ -103,6 +99,18 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { self.0.split(node) } + fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool { + hl == hr && self.0.equal(rl, rr) + } + + fn refer(&self, tree: &Self::Tree) -> Option { + Some((self.0.refer(tree)?, self.0.height(tree))) + } +} + +impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { + type _Tm = Self::T; + fn resolve(&self, (reference, hp): &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let hp = *hp; let ctx = self.0.clone(); @@ -111,14 +119,6 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { ctx.balancing_bind(matches_height(hl, hr, hp), |_| Self::pure(node)) }) } - - fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool { - hl == hr && self.0.equal(rl, rr) - } - - fn refer(&self, tree: &Self::Tree) -> Option { - Some((self.0.refer(tree)?, self.0.height(tree))) - } } impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees { @@ -146,11 +146,13 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty< } } -impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesHeight<'a> for BalancedTrees { +impl TreesHeight for BalancedTrees { fn height(&self, tree: &Self::Tree) -> u64 { self.0.height(tree) } +} +impl<'a, BT: BinaryTreesUnbalanced<'a>> TreesHeightError<'a> for BalancedTrees { fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { self.0.height_error(error) } diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index feb3696..e3f86b1 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -64,10 +64,6 @@ impl BinaryTrees for BoundTrees { type Tree = Bound; type Key = BT::Key; type Comparator = BT::Comparator; -} - -impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { - type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { self.0.comparator() @@ -88,6 +84,22 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { ) } + fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool { + Bounds::equal(&rl.bounds, &rr.bounds, self.comparator()) + && self.0.equal(&rl.bound, &rr.bound) + } + + fn refer(&self, tree: &Self::Tree) -> Option { + Some(Bound { + bound: self.0.refer(&tree.bound)?, + bounds: tree.bounds.clone(), + }) + } +} + +impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { + type _Tm = Self::T; + fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let ctx = self.0.clone(); let bounds = reference.bounds.clone(); @@ -106,18 +118,6 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { ) }) } - - fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool { - Bounds::equal(&rl.bounds, &rr.bounds, self.comparator()) - && self.0.equal(&rl.bound, &rr.bound) - } - - fn refer(&self, tree: &Self::Tree) -> Option { - Some(Bound { - bound: self.0.refer(&tree.bound)?, - bounds: tree.bounds.clone(), - }) - } } impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a> @@ -166,13 +166,15 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a } } -impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight<'a> - for BoundTrees -{ +impl TreesHeight for BoundTrees { fn height(&self, tree: &Self::Tree) -> u64 { self.0.height(&tree.bound) } +} +impl<'a, BT: BinaryTreesBindable<'a> + TreesHeightError<'a>> TreesHeightError<'a> + for BoundTrees +{ fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { self.0.height_error(error) } @@ -207,7 +209,7 @@ impl< BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a> + BinaryTreesEmpty<'a> - + BinaryTreesHeight<'a> + + TreesHeightError<'a> + BinaryTreesTryJoin<'a>, > BinaryTreesMutable<'a> for BoundTrees { diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index a22d126..bce7986 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -92,10 +92,6 @@ impl BinaryTrees for Trees { type Tree = Tree; type Key = A; type Comparator = DefaultComparator; -} - -impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees { - type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { &DefaultComparator @@ -105,10 +101,6 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees { (node.l.clone(), node.r.clone(), node.key.clone()) } - fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { - reference.node.as_ref().clone() - } - fn equal(&self, _rhs: &Self::Reference, _lhs: &Self::Reference) -> bool { false } @@ -118,11 +110,21 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees { } } -impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTreesHeight<'a> for Trees { +impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees { + type _Tm = Self::T; + + fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { + reference.node.as_ref().clone() + } +} + +impl TreesHeight for Trees { fn height(&self, tree: &Self::Tree) -> u64 { tree.height } +} +impl<'a, A: 'a + Send + Sync + Ord + Clone> TreesHeightError<'a> for Trees { fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { panic!("{error}") } diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index c2a5b6b..d7653bd 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -82,6 +82,22 @@ impl< type Tree = Tree<'a, Ctx, A>; type Key = A; type Comparator = C; + + fn comparator(&self) -> &Self::Comparator { + self.0 .0.as_ref() + } + + fn split(&self, node: &Self::Node) -> crate::flow::binary::Split { + (node.l.clone(), node.r.clone(), node.key.clone()) + } + + fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool { + rl == rr + } + + fn refer(&self, tree: &Self::Tree) -> Option { + tree.node.as_ref().cloned() + } } impl< @@ -94,14 +110,6 @@ impl< { type _Tm = Self::T; - fn comparator(&self) -> &Self::Comparator { - self.0 .0.as_ref() - } - - fn split(&self, node: &Self::Node) -> crate::flow::binary::Split { - (node.l.clone(), node.r.clone(), node.key.clone()) - } - fn resolve( &self, reference: &Self::Reference, @@ -112,14 +120,6 @@ impl< .map_err(TreeContextError::Resolution) })) } - - fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool { - rl == rr - } - - fn refer(&self, tree: &Self::Tree) -> Option { - tree.node.as_ref().cloned() - } } impl< @@ -155,12 +155,21 @@ impl< A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a + Send, - > BinaryTreesHeight<'a> for TreeContext2<'a, Ctx, A, C, E> + > TreesHeight for TreeContext2<'a, Ctx, A, C, E> { fn height(&self, tree: &Self::Tree) -> u64 { tree.height } +} +impl< + 'a, + Ctx: Context<'a>, + A: Mentionable<'a, Ctx> + Clone, + C: 'a + Comparator, + E: 'a + Send, + > TreesHeightError<'a> for TreeContext2<'a, Ctx, A, C, E> +{ fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { Self::fail(error.into()) }