radn-rs/src/rstd/collections/avl/context.rs

112 lines
3.2 KiB
Rust

use crate::flow::{binary::*, comparator::*};
use crate::func::*;
use crate::rstd::fallible::*;
use super::{bounds::*, *};
pub struct BoundContext<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> {
factory: A::Fctr,
comparator: Rc<C>,
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> Clone
for BoundContext<'a, Ctx, A, C>
{
fn clone(&self) -> Self {
Self {
factory: self.factory.clone(),
comparator: self.comparator.clone(),
}
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> BinaryTrees<'a>
for BoundContext<'a, Ctx, A, C>
{
type Node = BoundNode<'a, Ctx, A>;
type Reference = BoundReference<'a, Ctx, A>;
type Tree = BoundTree<'a, Ctx, A>;
type Key = A;
type Comparator = C;
type T = FallibleMonad<'a, Ctx, BoundResolutionError<'a, Ctx, A>>;
fn comparator(&self) -> &Self::Comparator {
&self.comparator
}
fn split(&self, node: &Self::Node) -> Split<'a, Self> {
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()))
}
fn equal(&self, rhs: &Self::Reference, lhs: &Self::Reference) -> bool {
BoundReference::equal(lhs, rhs, self.comparator.as_ref())
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
tree.reference()
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> BinaryTreesMutable<'a>
for BoundContext<'a, Ctx, A, C>
{
fn empty(&self) -> Self::Tree {
BoundTree::empty(self.factory.clone())
}
fn join_key(
self,
tl: Self::Tree,
key: KeyRc<'a, Self>,
tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Node> {
self.join_key_balanced(tl, key, tr)
}
fn split_key_empty(
&self,
tree: Self::Tree,
key: KeyRc<'a, Self>,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
Ctx::stuff(tree.split_empty_res(key, self.factory.clone(), self.comparator.clone()))
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator<A>> BinaryTreesAvl<'a>
for BoundContext<'a, Ctx, A, C>
{
fn height(&self, tree: &Self::Tree) -> u64 {
tree.height()
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
<FallibleMonad<'a, Ctx, _> as Fail<_>>::fail(ResolutionError::Parse(BoundError::Avl(
AvlError::LeafHeight(height),
)))
}
fn join_key_unbalanced(
&self,
tl: Self::Tree,
key: KeyRc<'a, Self>,
tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Node> {
match BoundNode::new(tl, tr, key, self.comparator.as_ref()) {
Ok(n) => <FallibleMonad<'a, Ctx, _> as Pure>::pure(n),
Err(e) => <FallibleMonad<'a, Ctx, _> as Fail<_>>::fail(ResolutionError::Parse(e)),
}
}
}