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

This commit is contained in:
AF 2023-08-31 23:42:57 +00:00
parent 22abcbbeff
commit d990fb1431
6 changed files with 87 additions and 68 deletions

View File

@ -23,16 +23,17 @@ pub trait BinaryTrees: Clone {
type Tree: Send; type Tree: Send;
type Key: Send + Clone; type Key: Send + Clone;
type Comparator: Comparator<Self::Key>; 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 MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees { pub trait MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees {
type _Tm: Monad<'a>; type _Tm: Monad<'a>;
fn comparator(&self) -> &Self::Comparator;
fn split(&self, node: &Self::Node) -> Split<Self>;
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>; 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>: MonadTrees<'a> { pub trait BinaryTreesTreeOf<'a>: MonadTrees<'a> {
@ -146,8 +147,11 @@ impl Display for HeightError {
} }
} }
pub trait BinaryTreesHeight<'a>: MonadTrees<'a> { pub trait TreesHeight: BinaryTrees {
fn height(&self, tree: &Self::Tree) -> u64; fn height(&self, tree: &Self::Tree) -> u64;
}
pub trait TreesHeightError<'a>: MonadTrees<'a> + TreesHeight {
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>;
} }

View File

@ -56,7 +56,7 @@ fn balancing(hl: u64, hr: u64) -> Balancing {
} }
pub trait BinaryTreesAvl<'a>: pub trait BinaryTreesAvl<'a>:
BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a> TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>
{ {
fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> { fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> {
match self.refer(tree) { match self.refer(tree) {
@ -116,7 +116,7 @@ pub trait BinaryTreesAvl<'a>:
} }
} }
impl<'a, BT: BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>> impl<'a, BT: TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>>
BinaryTreesAvl<'a> for BT BinaryTreesAvl<'a> for BT
{ {
} }

View File

@ -2,7 +2,7 @@ use std::fmt::Display;
use super::{avl::*, bound::*, *}; use super::{avl::*, bound::*, *};
pub trait BinaryTreesUnbalanced<'a>: BinaryTreesHeight<'a> { pub trait BinaryTreesUnbalanced<'a>: TreesHeightError<'a> {
fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree>; fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree>;
fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T>; fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T>;
@ -84,16 +84,12 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
} }
} }
impl<BT: BinaryTrees> BinaryTrees for BalancedTrees<BT> { 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; type Key = BT::Key;
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
self.0.comparator() self.0.comparator()
@ -103,6 +99,18 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
self.0.split(node) self.0.split(node)
} }
fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool {
hl == hr && self.0.equal(rl, rr)
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
Some((self.0.refer(tree)?, self.0.height(tree)))
}
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
type _Tm = Self::T;
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();
@ -111,14 +119,6 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
ctx.balancing_bind(matches_height(hl, hr, hp), |_| Self::pure(node)) ctx.balancing_bind(matches_height(hl, hr, hp), |_| Self::pure(node))
}) })
} }
fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool {
hl == hr && self.0.equal(rl, rr)
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
Some((self.0.refer(tree)?, self.0.height(tree)))
}
} }
impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees<BT> { impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees<BT> {
@ -146,11 +146,13 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<
} }
} }
impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesHeight<'a> for BalancedTrees<BT> { impl<BT: TreesHeight> TreesHeight for BalancedTrees<BT> {
fn height(&self, tree: &Self::Tree) -> u64 { fn height(&self, tree: &Self::Tree) -> u64 {
self.0.height(tree) self.0.height(tree)
} }
}
impl<'a, BT: BinaryTreesUnbalanced<'a>> TreesHeightError<'a> for BalancedTrees<BT> {
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)
} }

View File

@ -64,10 +64,6 @@ impl<BT: BinaryTrees> BinaryTrees for BoundTrees<BT> {
type Tree = Bound<Self::Key, BT::Tree>; type Tree = Bound<Self::Key, BT::Tree>;
type Key = BT::Key; type Key = BT::Key;
type Comparator = BT::Comparator; type Comparator = BT::Comparator;
}
impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
self.0.comparator() self.0.comparator()
@ -88,6 +84,22 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
) )
} }
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
Bounds::equal(&rl.bounds, &rr.bounds, self.comparator())
&& self.0.equal(&rl.bound, &rr.bound)
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
Some(Bound {
bound: self.0.refer(&tree.bound)?,
bounds: tree.bounds.clone(),
})
}
}
impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
type _Tm = Self::T;
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();
@ -106,18 +118,6 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
) )
}) })
} }
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
Bounds::equal(&rl.bounds, &rr.bounds, self.comparator())
&& self.0.equal(&rl.bound, &rr.bound)
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
Some(Bound {
bound: self.0.refer(&tree.bound)?,
bounds: tree.bounds.clone(),
})
}
} }
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a> impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a>
@ -166,13 +166,15 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
} }
} }
impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight<'a> impl<BT: TreesHeight> TreesHeight for BoundTrees<BT> {
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>
for BoundTrees<BT>
{
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)
} }
@ -207,7 +209,7 @@ impl<
BT: BinaryTreesBindable<'a> BT: BinaryTreesBindable<'a>
+ BinaryTreesTreeOf<'a> + BinaryTreesTreeOf<'a>
+ BinaryTreesEmpty<'a> + BinaryTreesEmpty<'a>
+ BinaryTreesHeight<'a> + TreesHeightError<'a>
+ BinaryTreesTryJoin<'a>, + BinaryTreesTryJoin<'a>,
> BinaryTreesMutable<'a> for BoundTrees<BT> > BinaryTreesMutable<'a> for BoundTrees<BT>
{ {

View File

@ -92,10 +92,6 @@ impl<A: Send + Sync + Ord + Clone> BinaryTrees for Trees<A> {
type Tree = Tree<A>; type Tree = Tree<A>;
type Key = A; type Key = A;
type Comparator = DefaultComparator; type Comparator = DefaultComparator;
}
impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
&DefaultComparator &DefaultComparator
@ -105,10 +101,6 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
(node.l.clone(), node.r.clone(), node.key.clone()) (node.l.clone(), node.r.clone(), node.key.clone())
} }
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
reference.node.as_ref().clone()
}
fn equal(&self, _rhs: &Self::Reference, _lhs: &Self::Reference) -> bool { fn equal(&self, _rhs: &Self::Reference, _lhs: &Self::Reference) -> bool {
false false
} }
@ -118,11 +110,21 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
} }
} }
impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTreesHeight<'a> for Trees<A> { impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
type _Tm = Self::T;
fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
reference.node.as_ref().clone()
}
}
impl<A: Send + Sync + Ord + Clone> TreesHeight for Trees<A> {
fn height(&self, tree: &Self::Tree) -> u64 { fn height(&self, tree: &Self::Tree) -> u64 {
tree.height tree.height
} }
}
impl<'a, A: 'a + Send + Sync + Ord + Clone> TreesHeightError<'a> for Trees<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> {
panic!("{error}") panic!("{error}")
} }

View File

@ -82,6 +82,22 @@ impl<
type Tree = Tree<'a, Ctx, A>; type Tree = Tree<'a, Ctx, A>;
type Key = A; type Key = A;
type Comparator = C; 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())
}
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
rl == rr
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
tree.node.as_ref().cloned()
}
} }
impl< impl<
@ -94,14 +110,6 @@ impl<
{ {
type _Tm = Self::T; type _Tm = Self::T;
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())
}
fn resolve( fn resolve(
&self, &self,
reference: &Self::Reference, reference: &Self::Reference,
@ -112,14 +120,6 @@ impl<
.map_err(TreeContextError::Resolution) .map_err(TreeContextError::Resolution)
})) }))
} }
fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
rl == rr
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
tree.node.as_ref().cloned()
}
} }
impl< impl<
@ -155,12 +155,21 @@ impl<
A: Mentionable<'a, Ctx> + Clone, A: Mentionable<'a, Ctx> + Clone,
C: 'a + Comparator<A>, C: 'a + Comparator<A>,
E: 'a + Send, E: 'a + Send,
> BinaryTreesHeight<'a> for TreeContext2<'a, Ctx, A, C, E> > TreesHeight for TreeContext2<'a, Ctx, A, C, E>
{ {
fn height(&self, tree: &Self::Tree) -> u64 { fn height(&self, tree: &Self::Tree) -> u64 {
tree.height tree.height
} }
}
impl<
'a,
Ctx: Context<'a>,
A: Mentionable<'a, Ctx> + Clone,
C: 'a + Comparator<A>,
E: 'a + Send,
> TreesHeightError<'a> for TreeContext2<'a, Ctx, A, C, E>
{
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::fail(error.into()) Self::fail(error.into())
} }