WithComparator
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.

This commit is contained in:
AF 2023-09-23 20:58:40 +00:00
parent 4dde3b5d8c
commit ea69b8414d
5 changed files with 97 additions and 52 deletions

View File

@ -23,14 +23,18 @@ pub trait BinaryTrees: Clone {
type Reference: Send;
type Tree: Send;
type Key: Send + Clone;
type Comparator: Comparator<Self::Key>;
fn comparator(&self) -> &Self::Comparator;
fn split(&self, node: &Self::Node) -> Split<Self>;
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
}
pub trait WithComparator: BinaryTrees {
type Comparator: Comparator<Self::Key>;
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<Self>>;
}
pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
pub trait BinaryTreesMutable<'a>:
BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> + WithComparator
{
fn join_key(
self,
tl: Self::Tree,

View File

@ -84,16 +84,19 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
}
}
impl<BT: TreesHeight> BinaryTrees for BalancedTrees<BT> {
type Node = BT::Node;
type Reference = (BT::Reference, u64);
type Tree = BT::Tree;
type Key = BT::Key;
impl<BT: TreesHeight + WithComparator> WithComparator for BalancedTrees<BT> {
type Comparator = BT::Comparator;
fn comparator(&self) -> &Self::Comparator {
self.0.comparator()
}
}
impl<BT: TreesHeight> BinaryTrees for BalancedTrees<BT> {
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> {
self.0.split(node)
@ -108,7 +111,10 @@ impl<BT: TreesHeight> BinaryTrees for BalancedTrees<BT> {
}
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
impl<'a, BT> MonadTrees<'a> for BalancedTrees<BT>
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<BT> {
}
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees<BT> {
impl<'a, BT> BinaryTreesTreeOf<'a> for BalancedTrees<BT>
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<BT>
impl<'a, BT> BinaryTreesEmpty<'a> for BalancedTrees<BT>
where
BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>,
{
fn empty(&self) -> Self::Tree {
self.0.empty()
@ -150,14 +160,18 @@ impl<BT: TreesHeight> TreesHeight for BalancedTrees<BT> {
}
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> TreesHeightError<'a> for BalancedTrees<BT> {
impl<'a, BT> TreesHeightError<'a> for BalancedTrees<BT>
where
BT: BinaryTreesUnbalanced<'a>,
{
fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
self.0.height_error(error)
}
}
impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a>
for BalancedTrees<BT>
impl<'a, BT> BinaryTreesTryJoin<'a> for BalancedTrees<BT>
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<BT>
impl<'a, BT> BinaryTreesBindable<'a> for BalancedTrees<BT>
where
BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>,
{
fn bounds_error<T: 'a + Send>(
&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<BT>
impl<'a, BT> BinaryTreesMutable<'a> for BalancedTrees<BT>
where
BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a> + BinaryTreesTryJoin<'a> + WithComparator,
{
fn join_key(
self,

View File

@ -58,16 +58,19 @@ pub trait BinaryTreesBindable<'a>: MonadTrees<'a> {
}
}
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;
impl<BT: WithComparator> WithComparator for BoundTrees<BT> {
type Comparator = BT::Comparator;
fn comparator(&self) -> &Self::Comparator {
self.0.comparator()
}
}
impl<BT: WithComparator> 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;
fn split(&self, node: &Self::Node) -> Split<Self> {
let (tl, tr, key) = self.0.split(&node.node);
@ -97,7 +100,10 @@ impl<BT: BinaryTrees> BinaryTrees for BoundTrees<BT> {
}
}
impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
impl<'a, BT> MonadTrees<'a> for BoundTrees<BT>
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<BT> {
}
}
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a>
for BoundTrees<BT>
impl<'a, BT> BinaryTreesTreeOf<'a> for BoundTrees<BT>
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<BT>
impl<'a, BT> BinaryTreesEmpty<'a> for BoundTrees<BT>
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<BT: TreesHeight> TreesHeight for BoundTrees<BT> {
impl<BT: TreesHeight + WithComparator> TreesHeight for BoundTrees<BT> {
fn height(&self, tree: &Self::Tree) -> u64 {
self.0.height(&tree.bound)
}
}
impl<'a, BT: BinaryTreesBindable<'a> + TreesHeightError<'a>> TreesHeightError<'a>
for BoundTrees<BT>
impl<'a, BT> TreesHeightError<'a> for BoundTrees<BT>
where
BT: BinaryTreesBindable<'a> + TreesHeightError<'a> + WithComparator,
{
fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
self.0.height_error(error)
}
}
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a>
for BoundTrees<BT>
impl<'a, BT> BinaryTreesTryJoin<'a> for BoundTrees<BT>
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,
impl<'a, BT> BinaryTreesMutable<'a> for BoundTrees<BT>
where
BT: BinaryTreesBindable<'a>
+ BinaryTreesTreeOf<'a>
+ BinaryTreesEmpty<'a>
+ TreesHeightError<'a>
+ BinaryTreesTryJoin<'a>,
> BinaryTreesMutable<'a> for BoundTrees<BT>
+ BinaryTreesTryJoin<'a>
+ WithComparator,
{
fn join_key(
self,

View File

@ -86,16 +86,19 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees<A> {
type T = instances::solo::SoloInstance;
}
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;
impl<A: Send + Sync + Ord + Clone> WithComparator for Trees<A> {
type Comparator = DefaultComparator;
fn comparator(&self) -> &Self::Comparator {
&DefaultComparator
}
}
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;
fn split(&self, node: &Self::Node) -> Split<Self> {
(node.l.clone(), node.r.clone(), node.key.clone())

View File

@ -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<A>,
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<Self> {
(node.l.clone(), node.r.clone(), node.key.clone())