MonadTrees
	
		
			
	
		
	
	
				
					
				
			This commit is contained in:
		
							parent
							
								
									8d665b0801
								
							
						
					
					
						commit
						c11990d020
					
				@ -8,32 +8,34 @@ use std::fmt::Display;
 | 
			
		||||
use crate::flow::comparator::*;
 | 
			
		||||
use crate::func::{context::*, *};
 | 
			
		||||
 | 
			
		||||
pub type Split<'a, BT> = (
 | 
			
		||||
    <BT as BinaryTrees<'a>>::Tree,
 | 
			
		||||
    <BT as BinaryTrees<'a>>::Tree,
 | 
			
		||||
    <BT as BinaryTrees<'a>>::Key,
 | 
			
		||||
pub type Split<BT> = (
 | 
			
		||||
    <BT as BinaryTrees>::Tree,
 | 
			
		||||
    <BT as BinaryTrees>::Tree,
 | 
			
		||||
    <BT as BinaryTrees>::Key,
 | 
			
		||||
);
 | 
			
		||||
pub type KeySplit<'a, BT> = (<BT as BinaryTrees<'a>>::Tree, <BT as BinaryTrees<'a>>::Tree);
 | 
			
		||||
pub type KeySplit<BT> = (<BT as BinaryTrees>::Tree, <BT as BinaryTrees>::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<Self::Key>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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<Self>;
 | 
			
		||||
    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<Self::Reference>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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<Self>>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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<Self>> {
 | 
			
		||||
        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<Self>> {
 | 
			
		||||
        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<T: 'a + Send>(&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,
 | 
			
		||||
 | 
			
		||||
@ -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>(T);
 | 
			
		||||
 | 
			
		||||
impl<'a, Trees: BinaryTrees<'a>> IntoTree<'a, Trees> for T<Trees::Tree> {
 | 
			
		||||
impl<'a, Trees: MonadTrees<'a>> IntoTree<'a, Trees> for T<Trees::Tree> {
 | 
			
		||||
    fn into_tree(self, _trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree> {
 | 
			
		||||
        Trees::pure(self.0)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -84,24 +84,22 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTrees<'a> for BalancedTrees<BT> {
 | 
			
		||||
impl<BT: BinaryTrees> BinaryTrees for BalancedTrees<BT> {
 | 
			
		||||
    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<BT> {
 | 
			
		||||
    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> {
 | 
			
		||||
        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>> {
 | 
			
		||||
        self.0.split_key_empty(tree, key)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -43,7 +43,7 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
 | 
			
		||||
    type T = BT::T;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
 | 
			
		||||
pub trait BinaryTreesBindable<'a>: MonadTrees<'a> {
 | 
			
		||||
    fn bounds_error<T: 'a + Send>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>;
 | 
			
		||||
 | 
			
		||||
    fn bounds_bind<A: 'a + Send, B: 'a + Send>(
 | 
			
		||||
@ -58,24 +58,22 @@ pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
 | 
			
		||||
impl<BT: BinaryTrees> BinaryTrees for BoundTrees<BT> {
 | 
			
		||||
    type Node = BoundNode<Self::Key, BT::Node>;
 | 
			
		||||
 | 
			
		||||
    type Reference = Bound<Self::Key, BT::Reference>;
 | 
			
		||||
 | 
			
		||||
    type Tree = Bound<Self::Key, BT::Tree>;
 | 
			
		||||
 | 
			
		||||
    type Key = BT::Key;
 | 
			
		||||
 | 
			
		||||
    type Comparator = BT::Comparator;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
 | 
			
		||||
    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> {
 | 
			
		||||
        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>> {
 | 
			
		||||
        self.0.bounds_bind(
 | 
			
		||||
            tree.bounds.split(&key, self.comparator()),
 | 
			
		||||
            |(boundsl, boundsr)| {
 | 
			
		||||
 | 
			
		||||
@ -86,24 +86,22 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees<A> {
 | 
			
		||||
    type T = instances::solo::SoloInstance;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTrees<'a> for Trees<A> {
 | 
			
		||||
impl<A: Send + Sync + Ord + Clone> BinaryTrees for Trees<A> {
 | 
			
		||||
    type Node = Node<A>;
 | 
			
		||||
 | 
			
		||||
    type Reference = Reference<A>;
 | 
			
		||||
 | 
			
		||||
    type Tree = Tree<A>;
 | 
			
		||||
 | 
			
		||||
    type Key = A;
 | 
			
		||||
 | 
			
		||||
    type Comparator = DefaultComparator;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
 | 
			
		||||
    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<Self> {
 | 
			
		||||
        (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<A> {
 | 
			
		||||
        &self,
 | 
			
		||||
        _tree: Self::Tree,
 | 
			
		||||
        _key: Self::Key,
 | 
			
		||||
    ) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
 | 
			
		||||
    ) -> BTWrap<'a, Self, KeySplit<Self>> {
 | 
			
		||||
        (self.empty(), self.empty())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -56,7 +56,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<HeightError>
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub type TreeContext2<'a, Ctx, A, C, E> = TreeContext<(Arc<C>, Fctr<'a, Ctx, A>), (Ctx, A, E)>;
 | 
			
		||||
pub type TreeContext2<'a, Ctx, A, C, E> =
 | 
			
		||||
    TreeContext<(Arc<C>, Fctr<'a, Ctx, A>), (&'a (), Ctx, A, E)>;
 | 
			
		||||
 | 
			
		||||
impl<
 | 
			
		||||
        'a,
 | 
			
		||||
@ -75,25 +76,30 @@ impl<
 | 
			
		||||
        A: Mentionable<'a, Ctx> + Clone,
 | 
			
		||||
        C: 'a + Comparator<A>,
 | 
			
		||||
        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<A>,
 | 
			
		||||
        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<Self> {
 | 
			
		||||
        (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>> {
 | 
			
		||||
        Self::pure((self.empty(), self.empty()))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user