MonadTrees
Some checks failed
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
buildbot/cargo clippy (1.65) Build done.

This commit is contained in:
AF 2023-08-31 22:12:28 +00:00
parent 8d665b0801
commit c11990d020
6 changed files with 49 additions and 54 deletions

View File

@ -8,32 +8,34 @@ use std::fmt::Display;
use crate::flow::comparator::*; use crate::flow::comparator::*;
use crate::func::{context::*, *}; use crate::func::{context::*, *};
pub type Split<'a, BT> = ( pub type Split<BT> = (
<BT as BinaryTrees<'a>>::Tree, <BT as BinaryTrees>::Tree,
<BT as BinaryTrees<'a>>::Tree, <BT as BinaryTrees>::Tree,
<BT as BinaryTrees<'a>>::Key, <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 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 Node: Send;
type Reference: Send; type Reference: Send;
type Tree: Send; type Tree: Send;
type Key: Send + Clone; type Key: Send + Clone;
type Comparator: Comparator<Self::Key>; type Comparator: Comparator<Self::Key>;
}
pub trait MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees {
type _Tm: Monad<'a>; type _Tm: Monad<'a>;
fn comparator(&self) -> &Self::Comparator; 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 resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool; fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>; 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_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> { 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 empty(&self) -> Self::Tree;
fn split_key_empty( fn split_key_empty(&self, tree: Self::Tree, key: Self::Key)
&self, -> BTWrap<'a, Self, KeySplit<Self>>;
tree: Self::Tree,
key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>>;
} }
pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
@ -91,11 +90,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
}) })
} }
fn split_key_node( fn split_key_node(self, node: Self::Node, key: Self::Key) -> BTWrap<'a, Self, KeySplit<Self>> {
self,
node: Self::Node,
key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
let (tl, tr, k) = self.split(&node); let (tl, tr, k) = self.split(&node);
match self.comparator().compare(&key, &k) { match self.comparator().compare(&key, &k) {
Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| { 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) { match self.refer(&tree) {
Some(reference) => Self::bind(self.resolve(&reference), |node| { Some(reference) => Self::bind(self.resolve(&reference), |node| {
self.split_key_node(node, key) 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(&self, tree: &Self::Tree) -> u64;
fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T>; 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( fn try_join(
&self, &self,
tl: Self::Tree, tl: Self::Tree,

View File

@ -1,12 +1,12 @@
use super::*; 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>; fn into_tree(self, trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree>;
} }
struct T<T>(T); 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> { fn into_tree(self, _trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree> {
Trees::pure(self.0) Trees::pure(self.0)
} }

View File

@ -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 Node = BT::Node;
type Reference = (BT::Reference, u64); type Reference = (BT::Reference, u64);
type Tree = BT::Tree; type Tree = BT::Tree;
type Key = BT::Key; type Key = BT::Key;
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
type _Tm = Self::T; type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
self.0.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) self.0.split(node)
} }
@ -143,7 +141,7 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<
&self, &self,
tree: Self::Tree, tree: Self::Tree,
key: Self::Key, key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<Self>> {
self.0.split_key_empty(tree, key) self.0.split_key_empty(tree, key)
} }
} }

View File

@ -43,7 +43,7 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
type T = BT::T; 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_error<T: 'a + Send>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>;
fn bounds_bind<A: 'a + Send, B: 'a + Send>( 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 Node = BoundNode<Self::Key, BT::Node>;
type Reference = Bound<Self::Key, BT::Reference>; type Reference = Bound<Self::Key, BT::Reference>;
type Tree = Bound<Self::Key, BT::Tree>; type Tree = Bound<Self::Key, BT::Tree>;
type Key = BT::Key; type Key = BT::Key;
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
}
impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
type _Tm = Self::T; type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
self.0.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); let (tl, tr, key) = self.0.split(&node.node);
( (
Bound { Bound {
@ -147,7 +145,7 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
&self, &self,
tree: Self::Tree, tree: Self::Tree,
key: Self::Key, key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<Self>> {
self.0.bounds_bind( self.0.bounds_bind(
tree.bounds.split(&key, self.comparator()), tree.bounds.split(&key, self.comparator()),
|(boundsl, boundsr)| { |(boundsl, boundsr)| {

View File

@ -86,24 +86,22 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees<A> {
type T = instances::solo::SoloInstance; 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 Node = Node<A>;
type Reference = Reference<A>; type Reference = Reference<A>;
type Tree = Tree<A>; type Tree = Tree<A>;
type Key = A; type Key = A;
type Comparator = DefaultComparator; type Comparator = DefaultComparator;
}
impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
type _Tm = Self::T; type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
&DefaultComparator &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()) (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, &self,
_tree: Self::Tree, _tree: Self::Tree,
_key: Self::Key, _key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<Self>> {
(self.empty(), self.empty()) (self.empty(), self.empty())
} }
} }

View File

@ -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< impl<
'a, 'a,
@ -75,25 +76,30 @@ impl<
A: Mentionable<'a, Ctx> + Clone, A: Mentionable<'a, Ctx> + Clone,
C: 'a + Comparator<A>, C: 'a + Comparator<A>,
E: 'a + Send, 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 Node = Node<'a, Ctx, A>;
type Reference = Point<'a, Ctx, Self::Node>; type Reference = Point<'a, Ctx, Self::Node>;
type Tree = Tree<'a, Ctx, A>; type Tree = Tree<'a, Ctx, A>;
type Key = A; type Key = A;
type Comparator = C; 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; type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
self.0 .0.as_ref() 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()) (node.l.clone(), node.r.clone(), node.key.clone())
} }
@ -136,7 +142,7 @@ impl<
&self, &self,
_tree: Self::Tree, _tree: Self::Tree,
_key: Self::Key, _key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> { ) -> BTWrap<'a, Self, KeySplit<Self>> {
Self::pure((self.empty(), self.empty())) Self::pure((self.empty(), self.empty()))
} }
} }