BinaryTreesTreeOf
This commit is contained in:
parent
f38efb0ae0
commit
53cd98aa86
@ -1,4 +1,5 @@
|
||||
pub mod avl;
|
||||
pub mod balancing;
|
||||
pub mod bounds;
|
||||
|
||||
use crate::flow::comparator::*;
|
||||
@ -24,10 +25,13 @@ pub trait BinaryTrees<'a>: FunctorContext<'a, T = Self::_Tm> + Clone {
|
||||
|
||||
fn comparator(&self) -> &Self::Comparator;
|
||||
fn split(&self, node: &Self::Node) -> Split<'a, Self>;
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree>;
|
||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
|
||||
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
|
||||
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
|
||||
}
|
||||
|
||||
pub trait BinaryTreesTreeOf<'a>: BinaryTrees<'a> {
|
||||
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> {
|
||||
Self::bind(fnode, move |node| self.tree_of(node))
|
||||
@ -44,7 +48,7 @@ pub trait BinaryTreesEmpty<'a>: BinaryTrees<'a> {
|
||||
) -> BTWrap<'a, Self, KeySplit<'a, Self>>;
|
||||
}
|
||||
|
||||
pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> {
|
||||
pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
|
||||
fn join_key(
|
||||
self,
|
||||
tl: Self::Tree,
|
||||
|
@ -1,6 +1,8 @@
|
||||
use super::*;
|
||||
|
||||
pub trait BinaryTreesAvl<'a>: BinaryTreesHeight<'a> + BinaryTreesTryJoin<'a> {
|
||||
pub trait BinaryTreesAvl<'a>:
|
||||
BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>
|
||||
{
|
||||
fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> {
|
||||
match self.refer(tree) {
|
||||
Some(reference) => self.resolve(&reference),
|
||||
@ -68,4 +70,7 @@ pub trait BinaryTreesAvl<'a>: BinaryTreesHeight<'a> + BinaryTreesTryJoin<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, BT: BinaryTreesHeight<'a> + BinaryTreesTryJoin<'a>> BinaryTreesAvl<'a> for BT {}
|
||||
impl<'a, BT: BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>>
|
||||
BinaryTreesAvl<'a> for BT
|
||||
{
|
||||
}
|
||||
|
0
src/flow/binary/balancing.rs
Normal file
0
src/flow/binary/balancing.rs
Normal file
@ -70,13 +70,6 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
|
||||
)
|
||||
}
|
||||
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
Self::fmap(self.0.tree_of(node.node), move |tree| Bound {
|
||||
bound: tree,
|
||||
bounds: node.bounds,
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
|
||||
let ctx = self.clone();
|
||||
let bounds = reference.bounds.clone();
|
||||
@ -109,6 +102,17 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a>
|
||||
for BoundTrees<BT>
|
||||
{
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
Self::fmap(self.0.tree_of(node.node), move |tree| Bound {
|
||||
bound: tree,
|
||||
bounds: node.bounds,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a>
|
||||
for BoundTrees<BT>
|
||||
{
|
||||
|
@ -126,15 +126,6 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTrees<'a> for AvlTs<A> {
|
||||
(node.l.clone(), node.r.clone(), node.key.clone())
|
||||
}
|
||||
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
AvlT {
|
||||
height: std::cmp::max(node.l.height, node.r.height)
|
||||
.checked_add(1)
|
||||
.unwrap(),
|
||||
reference: Some(AvlR { node: node.into() }),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
|
||||
reference.node.as_ref().clone()
|
||||
}
|
||||
@ -148,6 +139,17 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTrees<'a> for AvlTs<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesTreeOf<'a> for AvlTs<A> {
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
AvlT {
|
||||
height: std::cmp::max(node.l.height, node.r.height)
|
||||
.checked_add(1)
|
||||
.unwrap(),
|
||||
reference: Some(AvlR { node: node.into() }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesEmpty<'a> for AvlTs<A> {
|
||||
fn empty(&self) -> Self::Tree {
|
||||
AvlT {
|
||||
|
@ -48,13 +48,6 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
|
||||
node.split()
|
||||
}
|
||||
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
match node.into_tree() {
|
||||
Ok(tree) => Self::pure(tree),
|
||||
Err(e) => Self::fail(ResolutionError::Parse(e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
|
||||
Ctx::stuff(reference.resolve(self.comparator.clone()))
|
||||
}
|
||||
@ -68,6 +61,17 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>>
|
||||
BinaryTreesTreeOf<'a> for BoundContext<'a, Ctx, A, C>
|
||||
{
|
||||
fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> {
|
||||
match node.into_tree() {
|
||||
Ok(tree) => Self::pure(tree),
|
||||
Err(e) => Self::fail(ResolutionError::Parse(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>>
|
||||
BinaryTreesEmpty<'a> for BoundContext<'a, Ctx, A, C>
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user