flow::keyed
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 21:09:38 +00:00
parent ea69b8414d
commit 2105513959
7 changed files with 44 additions and 16 deletions

View File

@ -4,6 +4,7 @@
pub mod binary; pub mod binary;
pub mod comparator; pub mod comparator;
pub mod keyed;
pub mod query; pub mod query;
pub mod speculative; pub mod speculative;
pub mod traversible; pub mod traversible;

View File

@ -8,33 +8,28 @@ use std::fmt::Display;
use crate::flow::comparator::*; use crate::flow::comparator::*;
use crate::func::{context::*, *}; use crate::func::{context::*, *};
use super::keyed::*;
pub type Split<BT> = ( pub type Split<BT> = (
<BT as BinaryTrees>::Tree, <BT as BinaryTrees>::Tree,
<BT as BinaryTrees>::Tree, <BT as BinaryTrees>::Tree,
<BT as BinaryTrees>::Key, <BT as Keyed>::Key,
); );
pub type KeySplit<BT> = (<BT as BinaryTrees>::Tree, <BT as BinaryTrees>::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: Clone { pub trait BinaryTrees: Keyed {
type Node: Send; type Node: Send;
type Reference: Send; type Reference: Send;
type Tree: Send; type Tree: Send;
type Key: Send + Clone;
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>;
} }

View File

@ -84,7 +84,11 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
} }
} }
impl<BT: TreesHeight + WithComparator> WithComparator for BalancedTrees<BT> { impl<BT: Keyed> Keyed for BalancedTrees<BT> {
type Key = BT::Key;
}
impl<BT: WithComparator> WithComparator for BalancedTrees<BT> {
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
@ -96,7 +100,6 @@ impl<BT: TreesHeight> 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;
fn split(&self, node: &Self::Node) -> Split<Self> { fn split(&self, node: &Self::Node) -> Split<Self> {
self.0.split(node) self.0.split(node)

View File

@ -58,6 +58,10 @@ pub trait BinaryTreesBindable<'a>: MonadTrees<'a> {
} }
} }
impl<BT: Keyed> Keyed for BoundTrees<BT> {
type Key = BT::Key;
}
impl<BT: WithComparator> WithComparator for BoundTrees<BT> { impl<BT: WithComparator> WithComparator for BoundTrees<BT> {
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
@ -66,11 +70,10 @@ impl<BT: WithComparator> WithComparator for BoundTrees<BT> {
} }
} }
impl<BT: WithComparator> BinaryTrees for BoundTrees<BT> { impl<BT: WithComparator + 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;
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);

11
src/flow/keyed.rs Normal file
View File

@ -0,0 +1,11 @@
use super::comparator::*;
pub trait Keyed: Clone {
type Key: Send + Clone;
}
pub trait WithComparator: Keyed {
type Comparator: Comparator<Self::Key>;
fn comparator(&self) -> &Self::Comparator;
}

View File

@ -3,6 +3,7 @@ use std::{fmt::Display, marker::PhantomData, sync::Arc};
use crate::flow::{ use crate::flow::{
binary::{balancing::*, bound::*, *}, binary::{balancing::*, bound::*, *},
comparator::*, comparator::*,
keyed::*,
}; };
use crate::func::{context::*, *}; use crate::func::{context::*, *};
@ -86,6 +87,10 @@ 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> Keyed for Trees<A> {
type Key = A;
}
impl<A: Send + Sync + Ord + Clone> WithComparator for Trees<A> { impl<A: Send + Sync + Ord + Clone> WithComparator for Trees<A> {
type Comparator = DefaultComparator; type Comparator = DefaultComparator;
@ -98,7 +103,6 @@ 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;
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())

View File

@ -2,8 +2,9 @@ use std::{marker::PhantomData, sync::Arc};
use crate::{ use crate::{
flow::{ flow::{
binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *}, binary::{balancing::*, bound::*, bounds::*, *},
comparator::*, comparator::*,
keyed::*,
}, },
func::context::*, func::context::*,
rcore::*, rcore::*,
@ -69,6 +70,17 @@ 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,
> Keyed for TreeContext2<'a, Ctx, A, C, E>
{
type Key = A;
}
impl< impl<
'a, 'a,
Ctx: Context<'a>, Ctx: Context<'a>,
@ -95,7 +107,6 @@ impl<
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;
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())