WithComparator
This commit is contained in:
parent
4dde3b5d8c
commit
ea69b8414d
@ -23,14 +23,18 @@ pub trait BinaryTrees: Clone {
|
|||||||
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>;
|
|
||||||
|
|
||||||
fn comparator(&self) -> &Self::Comparator;
|
|
||||||
fn split(&self, node: &Self::Node) -> Split<Self>;
|
fn split(&self, node: &Self::Node) -> Split<Self>;
|
||||||
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 WithComparator: BinaryTrees {
|
||||||
|
type Comparator: Comparator<Self::Key>;
|
||||||
|
|
||||||
|
fn comparator(&self) -> &Self::Comparator;
|
||||||
|
}
|
||||||
|
|
||||||
pub trait MonadTrees<'a>: MonadContext<'a> + BinaryTrees {
|
pub trait MonadTrees<'a>: MonadContext<'a> + BinaryTrees {
|
||||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
|
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>>;
|
-> 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(
|
fn join_key(
|
||||||
self,
|
self,
|
||||||
tl: Self::Tree,
|
tl: Self::Tree,
|
||||||
|
@ -84,16 +84,19 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BT: TreesHeight> BinaryTrees for BalancedTrees<BT> {
|
impl<BT: TreesHeight + WithComparator> WithComparator for BalancedTrees<BT> {
|
||||||
type Node = BT::Node;
|
|
||||||
type Reference = (BT::Reference, u64);
|
|
||||||
type Tree = BT::Tree;
|
|
||||||
type Key = BT::Key;
|
|
||||||
type Comparator = BT::Comparator;
|
type Comparator = BT::Comparator;
|
||||||
|
|
||||||
fn comparator(&self) -> &Self::Comparator {
|
fn comparator(&self) -> &Self::Comparator {
|
||||||
self.0.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> {
|
fn split(&self, node: &Self::Node) -> Split<Self> {
|
||||||
self.0.split(node)
|
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> {
|
fn resolve(&self, (reference, hp): &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
|
||||||
let hp = *hp;
|
let hp = *hp;
|
||||||
let ctx = self.0.clone();
|
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> {
|
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||||
let (hl, hr) = self.0.node_heights(&node);
|
let (hl, hr) = self.0.node_heights(&node);
|
||||||
self.0.balancing_bind(parent_height(hl, hr), |height| {
|
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>
|
impl<'a, BT> BinaryTreesEmpty<'a> for BalancedTrees<BT>
|
||||||
for BalancedTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>,
|
||||||
{
|
{
|
||||||
fn empty(&self) -> Self::Tree {
|
fn empty(&self) -> Self::Tree {
|
||||||
self.0.empty()
|
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> {
|
fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
|
||||||
self.0.height_error(error)
|
self.0.height_error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a>
|
impl<'a, BT> BinaryTreesTryJoin<'a> for BalancedTrees<BT>
|
||||||
for BalancedTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>,
|
||||||
{
|
{
|
||||||
fn try_join(
|
fn try_join(
|
||||||
&self,
|
&self,
|
||||||
@ -171,8 +185,9 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>> BinaryTreesBindable<'a>
|
impl<'a, BT> BinaryTreesBindable<'a> for BalancedTrees<BT>
|
||||||
for BalancedTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>,
|
||||||
{
|
{
|
||||||
fn bounds_error<T: 'a + Send>(
|
fn bounds_error<T: 'a + Send>(
|
||||||
&self,
|
&self,
|
||||||
@ -182,8 +197,9 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesBindable<'a>> BinaryTreesBin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a> + BinaryTreesTryJoin<'a>>
|
impl<'a, BT> BinaryTreesMutable<'a> for BalancedTrees<BT>
|
||||||
BinaryTreesMutable<'a> for BalancedTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a> + BinaryTreesTryJoin<'a> + WithComparator,
|
||||||
{
|
{
|
||||||
fn join_key(
|
fn join_key(
|
||||||
self,
|
self,
|
||||||
|
@ -58,16 +58,19 @@ pub trait BinaryTreesBindable<'a>: MonadTrees<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BT: BinaryTrees> BinaryTrees for BoundTrees<BT> {
|
impl<BT: WithComparator> WithComparator 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;
|
type Comparator = BT::Comparator;
|
||||||
|
|
||||||
fn comparator(&self) -> &Self::Comparator {
|
fn comparator(&self) -> &Self::Comparator {
|
||||||
self.0.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> {
|
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);
|
||||||
@ -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> {
|
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
|
||||||
let ctx = self.0.clone();
|
let ctx = self.0.clone();
|
||||||
let bounds = reference.bounds.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>
|
impl<'a, BT> BinaryTreesTreeOf<'a> for BoundTrees<BT>
|
||||||
for BoundTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a> + WithComparator,
|
||||||
{
|
{
|
||||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||||
Self::fmap(self.0.tree_of(node.node), move |tree| Bound {
|
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>
|
impl<'a, BT> BinaryTreesEmpty<'a> for BoundTrees<BT>
|
||||||
for BoundTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a> + WithComparator,
|
||||||
{
|
{
|
||||||
fn empty(&self) -> Self::Tree {
|
fn empty(&self) -> Self::Tree {
|
||||||
Bound {
|
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 {
|
fn height(&self, tree: &Self::Tree) -> u64 {
|
||||||
self.0.height(&tree.bound)
|
self.0.height(&tree.bound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, BT: BinaryTreesBindable<'a> + TreesHeightError<'a>> TreesHeightError<'a>
|
impl<'a, BT> TreesHeightError<'a> for BoundTrees<BT>
|
||||||
for BoundTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesBindable<'a> + TreesHeightError<'a> + WithComparator,
|
||||||
{
|
{
|
||||||
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> {
|
||||||
self.0.height_error(error)
|
self.0.height_error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoin<'a>
|
impl<'a, BT> BinaryTreesTryJoin<'a> for BoundTrees<BT>
|
||||||
for BoundTrees<BT>
|
where
|
||||||
|
BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a> + WithComparator,
|
||||||
{
|
{
|
||||||
fn try_join(
|
fn try_join(
|
||||||
&self,
|
&self,
|
||||||
@ -202,14 +212,14 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<'a, BT> BinaryTreesMutable<'a> for BoundTrees<BT>
|
||||||
'a,
|
where
|
||||||
BT: BinaryTreesBindable<'a>
|
BT: BinaryTreesBindable<'a>
|
||||||
+ BinaryTreesTreeOf<'a>
|
+ BinaryTreesTreeOf<'a>
|
||||||
+ BinaryTreesEmpty<'a>
|
+ BinaryTreesEmpty<'a>
|
||||||
+ TreesHeightError<'a>
|
+ TreesHeightError<'a>
|
||||||
+ BinaryTreesTryJoin<'a>,
|
+ BinaryTreesTryJoin<'a>
|
||||||
> BinaryTreesMutable<'a> for BoundTrees<BT>
|
+ WithComparator,
|
||||||
{
|
{
|
||||||
fn join_key(
|
fn join_key(
|
||||||
self,
|
self,
|
||||||
|
@ -86,16 +86,19 @@ impl<'a, A: 'a + Send> FunctorContext<'a> for Trees<A> {
|
|||||||
type T = instances::solo::SoloInstance;
|
type T = instances::solo::SoloInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Send + Sync + Ord + Clone> BinaryTrees for Trees<A> {
|
impl<A: Send + Sync + Ord + Clone> WithComparator for Trees<A> {
|
||||||
type Node = Node<A>;
|
|
||||||
type Reference = Reference<A>;
|
|
||||||
type Tree = Tree<A>;
|
|
||||||
type Key = A;
|
|
||||||
type Comparator = DefaultComparator;
|
type Comparator = DefaultComparator;
|
||||||
|
|
||||||
fn comparator(&self) -> &Self::Comparator {
|
fn comparator(&self) -> &Self::Comparator {
|
||||||
&DefaultComparator
|
&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> {
|
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())
|
||||||
|
@ -69,6 +69,21 @@ impl<
|
|||||||
type T = FallibleMonad<'a, Ctx, TreeContextError<'a, Ctx, A, E>>;
|
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<
|
impl<
|
||||||
'a,
|
'a,
|
||||||
Ctx: Context<'a>,
|
Ctx: Context<'a>,
|
||||||
@ -81,11 +96,6 @@ impl<
|
|||||||
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;
|
|
||||||
|
|
||||||
fn comparator(&self) -> &Self::Comparator {
|
|
||||||
self.0 .0.as_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<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())
|
||||||
|
Loading…
Reference in New Issue
Block a user