diff --git a/src/flow/binary.rs b/src/flow/binary.rs index a7aa556..e4dd68e 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -8,32 +8,34 @@ use std::fmt::Display; use crate::flow::comparator::*; use crate::func::{context::*, *}; -pub type Split<'a, BT> = ( - >::Tree, - >::Tree, - >::Key, +pub type Split = ( + ::Tree, + ::Tree, + ::Key, ); -pub type KeySplit<'a, BT> = (>::Tree, >::Tree); +pub type KeySplit = (::Tree, ::Tree); pub type BTWrap<'a, BT, A> = WrapC<'a, A, BT>; -pub trait BinaryTrees<'a>: FunctorContext<'a, T = Self::_Tm> + Clone { +pub trait BinaryTrees: Clone { type Node: Send; type Reference: Send; type Tree: Send; type Key: Send + Clone; type Comparator: Comparator; +} +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<'a, Self>; + 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>: BinaryTrees<'a> { +pub trait BinaryTreesTreeOf<'a>: MonadTrees<'a> { fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree>; fn tree_bind(self, fnode: BTWrap<'a, Self, Self::Node>) -> BTWrap<'a, Self, Self::Tree> { @@ -41,14 +43,11 @@ pub trait BinaryTreesTreeOf<'a>: BinaryTrees<'a> { } } -pub trait BinaryTreesEmpty<'a>: BinaryTrees<'a> { +pub trait BinaryTreesEmpty<'a>: MonadTrees<'a> { fn empty(&self) -> Self::Tree; - fn split_key_empty( - &self, - tree: Self::Tree, - key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>>; + fn split_key_empty(&self, tree: Self::Tree, key: Self::Key) + -> BTWrap<'a, Self, KeySplit>; } pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { @@ -91,11 +90,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { }) } - fn split_key_node( - self, - node: Self::Node, - key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + fn split_key_node(self, node: Self::Node, key: Self::Key) -> BTWrap<'a, Self, KeySplit> { let (tl, tr, k) = self.split(&node); match self.comparator().compare(&key, &k) { Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| { @@ -108,7 +103,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { } } - fn split_key(self, tree: Self::Tree, key: Self::Key) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + fn split_key(self, tree: Self::Tree, key: Self::Key) -> BTWrap<'a, Self, KeySplit> { match self.refer(&tree) { Some(reference) => Self::bind(self.resolve(&reference), |node| { self.split_key_node(node, key) @@ -151,12 +146,12 @@ impl Display for HeightError { } } -pub trait BinaryTreesHeight<'a>: BinaryTrees<'a> { +pub trait BinaryTreesHeight<'a>: MonadTrees<'a> { fn height(&self, tree: &Self::Tree) -> u64; fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T>; } -pub trait BinaryTreesTryJoin<'a>: BinaryTrees<'a> { +pub trait BinaryTreesTryJoin<'a>: MonadTrees<'a> { fn try_join( &self, tl: Self::Tree, diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index 5f3bbd5..97b8871 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -1,12 +1,12 @@ use super::*; -trait IntoTree<'a, Trees: BinaryTrees<'a>> { +trait IntoTree<'a, Trees: MonadTrees<'a>> { fn into_tree(self, trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree>; } struct T(T); -impl<'a, Trees: BinaryTrees<'a>> IntoTree<'a, Trees> for T { +impl<'a, Trees: MonadTrees<'a>> IntoTree<'a, Trees> for T { fn into_tree(self, _trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree> { Trees::pure(self.0) } diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index c48c637..c36fed7 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -84,24 +84,22 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> { } } -impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTrees<'a> 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() } - fn split(&self, node: &Self::Node) -> Split<'a, Self> { + fn split(&self, node: &Self::Node) -> Split { self.0.split(node) } @@ -143,7 +141,7 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty< &self, tree: Self::Tree, key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + ) -> BTWrap<'a, Self, KeySplit> { self.0.split_key_empty(tree, key) } } diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index 829b691..feb3696 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -43,7 +43,7 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees { type T = BT::T; } -pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> { +pub trait BinaryTreesBindable<'a>: MonadTrees<'a> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T>; fn bounds_bind( @@ -58,24 +58,22 @@ pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> { } } -impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees { +impl BinaryTrees for BoundTrees { type Node = BoundNode; - type Reference = Bound; - 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() } - fn split(&self, node: &Self::Node) -> Split<'a, Self> { + fn split(&self, node: &Self::Node) -> Split { let (tl, tr, key) = self.0.split(&node.node); ( Bound { @@ -147,7 +145,7 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a &self, tree: Self::Tree, key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + ) -> BTWrap<'a, Self, KeySplit> { self.0.bounds_bind( tree.bounds.split(&key, self.comparator()), |(boundsl, boundsr)| { diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index 2fa4d4b..a22d126 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -86,24 +86,22 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees { type T = instances::solo::SoloInstance; } -impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTrees<'a> for Trees { +impl BinaryTrees for Trees { type Node = Node; - type Reference = Reference; - 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 } - fn split(&self, node: &Self::Node) -> Split<'a, Self> { + fn split(&self, node: &Self::Node) -> Split { (node.l.clone(), node.r.clone(), node.key.clone()) } @@ -142,7 +140,7 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTreesEmpty<'a> for Trees { &self, _tree: Self::Tree, _key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + ) -> BTWrap<'a, Self, KeySplit> { (self.empty(), self.empty()) } } diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index 1fcc448..ebd330a 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -56,7 +56,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From } } -pub type TreeContext2<'a, Ctx, A, C, E> = TreeContext<(Arc, Fctr<'a, Ctx, A>), (Ctx, A, E)>; +pub type TreeContext2<'a, Ctx, A, C, E> = + TreeContext<(Arc, Fctr<'a, Ctx, A>), (&'a (), Ctx, A, E)>; impl< 'a, @@ -75,25 +76,30 @@ impl< A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a + Send, - > BinaryTrees<'a> for TreeContext2<'a, Ctx, A, C, E> + > BinaryTrees for TreeContext2<'a, Ctx, A, C, E> { type Node = Node<'a, Ctx, A>; - type Reference = Point<'a, Ctx, Self::Node>; - type Tree = Tree<'a, Ctx, A>; - type Key = A; - type Comparator = C; +} +impl< + 'a, + Ctx: Context<'a>, + A: Mentionable<'a, Ctx> + Clone, + C: 'a + Comparator, + E: 'a + Send, + > MonadTrees<'a> for TreeContext2<'a, Ctx, A, C, E> +{ type _Tm = Self::T; fn comparator(&self) -> &Self::Comparator { self.0 .0.as_ref() } - fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<'a, Self> { + fn split(&self, node: &Self::Node) -> crate::flow::binary::Split { (node.l.clone(), node.r.clone(), node.key.clone()) } @@ -136,7 +142,7 @@ impl< &self, _tree: Self::Tree, _key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + ) -> BTWrap<'a, Self, KeySplit> { Self::pure((self.empty(), self.empty())) } }