diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 394e2a1..fb704a7 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -23,14 +23,18 @@ pub trait BinaryTrees: Clone { type Reference: Send; 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 WithComparator: BinaryTrees { + type Comparator: Comparator; + + fn comparator(&self) -> &Self::Comparator; +} + pub trait MonadTrees<'a>: MonadContext<'a> + BinaryTrees { fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>; } @@ -50,7 +54,9 @@ pub trait BinaryTreesEmpty<'a>: MonadTrees<'a> { -> BTWrap<'a, Self, KeySplit>; } -pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { +pub trait BinaryTreesMutable<'a>: + BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> + WithComparator +{ fn join_key( self, tl: Self::Tree, diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index 87539c2..1830e2d 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -84,16 +84,19 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> { } } -impl BinaryTrees for BalancedTrees { - type Node = BT::Node; - type Reference = (BT::Reference, u64); - type Tree = BT::Tree; - type Key = BT::Key; +impl WithComparator for BalancedTrees { type Comparator = BT::Comparator; fn comparator(&self) -> &Self::Comparator { self.0.comparator() } +} + +impl BinaryTrees for BalancedTrees { + type Node = BT::Node; + type Reference = (BT::Reference, u64); + type Tree = BT::Tree; + type Key = BT::Key; fn split(&self, node: &Self::Node) -> Split { self.0.split(node) @@ -108,7 +111,10 @@ impl BinaryTrees for BalancedTrees { } } -impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { +impl<'a, BT> MonadTrees<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a>, +{ fn resolve(&self, (reference, hp): &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let hp = *hp; let ctx = self.0.clone(); @@ -119,7 +125,10 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees { } } -impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees { +impl<'a, BT> BinaryTreesTreeOf<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a>, +{ fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> { let (hl, hr) = self.0.node_heights(&node); self.0.balancing_bind(parent_height(hl, hr), |height| { @@ -128,8 +137,9 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees< } } -impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a> - for BalancedTrees +impl<'a, BT> BinaryTreesEmpty<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>, { fn empty(&self) -> Self::Tree { self.0.empty() @@ -150,14 +160,18 @@ impl TreesHeight for BalancedTrees { } } -impl<'a, BT: BinaryTreesUnbalanced<'a>> TreesHeightError<'a> for BalancedTrees { +impl<'a, BT> TreesHeightError<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a>, +{ fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { self.0.height_error(error) } } -impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a> - for BalancedTrees +impl<'a, BT> BinaryTreesTryJoin<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>, { fn try_join( &self, @@ -171,8 +185,9 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJ } } -impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>> BinaryTreesBindable<'a> - for BalancedTrees +impl<'a, BT> BinaryTreesBindable<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>, { fn bounds_error( &self, @@ -182,8 +197,9 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>> BinaryTreesBin } } -impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a> + BinaryTreesTryJoin<'a>> - BinaryTreesMutable<'a> for BalancedTrees +impl<'a, BT> BinaryTreesMutable<'a> for BalancedTrees +where + BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a> + BinaryTreesTryJoin<'a> + WithComparator, { fn join_key( self, diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index 2903595..1629e78 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -58,16 +58,19 @@ pub trait BinaryTreesBindable<'a>: MonadTrees<'a> { } } -impl BinaryTrees for BoundTrees { - type Node = BoundNode; - type Reference = Bound; - type Tree = Bound; - type Key = BT::Key; +impl WithComparator for BoundTrees { type Comparator = BT::Comparator; fn comparator(&self) -> &Self::Comparator { self.0.comparator() } +} + +impl BinaryTrees for BoundTrees { + type Node = BoundNode; + type Reference = Bound; + type Tree = Bound; + type Key = BT::Key; fn split(&self, node: &Self::Node) -> Split { let (tl, tr, key) = self.0.split(&node.node); @@ -97,7 +100,10 @@ impl BinaryTrees for BoundTrees { } } -impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { +impl<'a, BT> MonadTrees<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + WithComparator, +{ fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> { let ctx = self.0.clone(); let bounds = reference.bounds.clone(); @@ -118,8 +124,9 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees { } } -impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a> - for BoundTrees +impl<'a, BT> BinaryTreesTreeOf<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a> + WithComparator, { fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> { Self::fmap(self.0.tree_of(node.node), move |tree| Bound { @@ -129,8 +136,9 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf< } } -impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a> - for BoundTrees +impl<'a, BT> BinaryTreesEmpty<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a> + WithComparator, { fn empty(&self) -> Self::Tree { Bound { @@ -164,22 +172,24 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a } } -impl TreesHeight 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 +impl<'a, BT> TreesHeightError<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + TreesHeightError<'a> + WithComparator, { fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { self.0.height_error(error) } } -impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a> - for BoundTrees +impl<'a, BT> BinaryTreesTryJoin<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a> + WithComparator, { fn try_join( &self, @@ -202,14 +212,14 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoi } } -impl< - 'a, - BT: BinaryTreesBindable<'a> - + BinaryTreesTreeOf<'a> - + BinaryTreesEmpty<'a> - + TreesHeightError<'a> - + BinaryTreesTryJoin<'a>, - > BinaryTreesMutable<'a> for BoundTrees +impl<'a, BT> BinaryTreesMutable<'a> for BoundTrees +where + BT: BinaryTreesBindable<'a> + + BinaryTreesTreeOf<'a> + + BinaryTreesEmpty<'a> + + TreesHeightError<'a> + + BinaryTreesTryJoin<'a> + + WithComparator, { fn join_key( self, diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index 2fd7e9a..65b7cbf 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -86,16 +86,19 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees { type T = instances::solo::SoloInstance; } -impl BinaryTrees for Trees { - type Node = Node; - type Reference = Reference; - type Tree = Tree; - type Key = A; +impl WithComparator for Trees { type Comparator = DefaultComparator; fn comparator(&self) -> &Self::Comparator { &DefaultComparator } +} + +impl BinaryTrees for Trees { + type Node = Node; + type Reference = Reference; + type Tree = Tree; + type Key = A; fn split(&self, node: &Self::Node) -> Split { (node.l.clone(), node.r.clone(), node.key.clone()) diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index 476212a..4beabad 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -69,6 +69,21 @@ impl< type T = FallibleMonad<'a, Ctx, TreeContextError<'a, Ctx, A, E>>; } +impl< + 'a, + Ctx: Context<'a>, + A: Mentionable<'a, Ctx> + Clone, + C: 'a + Comparator, + E: 'a + Send, + > WithComparator for TreeContext2<'a, Ctx, A, C, E> +{ + type Comparator = C; + + fn comparator(&self) -> &Self::Comparator { + self.0 .0.as_ref() + } +} + impl< 'a, Ctx: Context<'a>, @@ -81,11 +96,6 @@ impl< type Reference = Point<'a, Ctx, Self::Node>; 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())