From 21055139591c200e71537dc863aa4edc3a76f1fb Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 23 Sep 2023 21:09:38 +0000 Subject: [PATCH] `flow::keyed` --- src/flow.rs | 1 + src/flow/binary.rs | 13 ++++--------- src/flow/binary/balancing.rs | 7 +++++-- src/flow/binary/bound.rs | 7 +++++-- src/flow/keyed.rs | 11 +++++++++++ src/mrds/trees/heighted.rs | 6 +++++- src/rstd/collections/tree/context.rs | 15 +++++++++++++-- 7 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 src/flow/keyed.rs diff --git a/src/flow.rs b/src/flow.rs index c832713..d0aa34b 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -4,6 +4,7 @@ pub mod binary; pub mod comparator; +pub mod keyed; pub mod query; pub mod speculative; pub mod traversible; diff --git a/src/flow/binary.rs b/src/flow/binary.rs index fb704a7..31fe03a 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -8,33 +8,28 @@ use std::fmt::Display; use crate::flow::comparator::*; use crate::func::{context::*, *}; +use super::keyed::*; + pub type Split = ( ::Tree, ::Tree, - ::Key, + ::Key, ); pub type KeySplit = (::Tree, ::Tree); pub type BTWrap<'a, BT, A> = WrapC<'a, A, BT>; -pub trait BinaryTrees: Clone { +pub trait BinaryTrees: Keyed { type Node: Send; type Reference: Send; type Tree: Send; - type Key: Send + Clone; 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>; } diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs index 1830e2d..ef832e4 100644 --- a/src/flow/binary/balancing.rs +++ b/src/flow/binary/balancing.rs @@ -84,7 +84,11 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> { } } -impl WithComparator for BalancedTrees { +impl Keyed for BalancedTrees { + type Key = BT::Key; +} + +impl WithComparator for BalancedTrees { type Comparator = BT::Comparator; fn comparator(&self) -> &Self::Comparator { @@ -96,7 +100,6 @@ 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) diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs index 1629e78..77520ba 100644 --- a/src/flow/binary/bound.rs +++ b/src/flow/binary/bound.rs @@ -58,6 +58,10 @@ pub trait BinaryTreesBindable<'a>: MonadTrees<'a> { } } +impl Keyed for BoundTrees { + type Key = BT::Key; +} + impl WithComparator for BoundTrees { type Comparator = BT::Comparator; @@ -66,11 +70,10 @@ impl WithComparator for BoundTrees { } } -impl BinaryTrees for BoundTrees { +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); diff --git a/src/flow/keyed.rs b/src/flow/keyed.rs new file mode 100644 index 0000000..97e8c53 --- /dev/null +++ b/src/flow/keyed.rs @@ -0,0 +1,11 @@ +use super::comparator::*; + +pub trait Keyed: Clone { + type Key: Send + Clone; +} + +pub trait WithComparator: Keyed { + type Comparator: Comparator; + + fn comparator(&self) -> &Self::Comparator; +} diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs index 65b7cbf..ad5d3b2 100644 --- a/src/mrds/trees/heighted.rs +++ b/src/mrds/trees/heighted.rs @@ -3,6 +3,7 @@ use std::{fmt::Display, marker::PhantomData, sync::Arc}; use crate::flow::{ binary::{balancing::*, bound::*, *}, comparator::*, + keyed::*, }; use crate::func::{context::*, *}; @@ -86,6 +87,10 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees { type T = instances::solo::SoloInstance; } +impl Keyed for Trees { + type Key = A; +} + impl WithComparator for Trees { type Comparator = DefaultComparator; @@ -98,7 +103,6 @@ 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 4beabad..87afee4 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -2,8 +2,9 @@ use std::{marker::PhantomData, sync::Arc}; use crate::{ flow::{ - binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *}, + binary::{balancing::*, bound::*, bounds::*, *}, comparator::*, + keyed::*, }, func::context::*, rcore::*, @@ -69,6 +70,17 @@ 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, + > Keyed for TreeContext2<'a, Ctx, A, C, E> +{ + type Key = A; +} + impl< 'a, Ctx: Context<'a>, @@ -95,7 +107,6 @@ impl< type Node = Node<'a, Ctx, A>; type Reference = Point<'a, Ctx, Self::Node>; type Tree = Tree<'a, Ctx, A>; - type Key = A; fn split(&self, node: &Self::Node) -> crate::flow::binary::Split { (node.l.clone(), node.r.clone(), node.key.clone())