BoundTrees

This commit is contained in:
AF 2023-06-16 08:42:17 +00:00
parent f0aa3878c4
commit 0a0ec3a882
2 changed files with 101 additions and 0 deletions

View File

@ -1,3 +1,5 @@
pub mod bound;
use crate::flow::comparator::*;
#[derive(Clone)]

View File

@ -0,0 +1,99 @@
use crate::{flow::binary::*, func::context::*};
use super::*;
#[derive(Clone)]
pub struct Bound<A, T> {
bound: T,
bounds: Bounds<A>,
}
pub struct BoundNode2<A, T> {
boundsl: Bounds<A>,
boundsr: Bounds<A>,
bounds: Bounds<A>,
node: T,
}
#[derive(Clone)]
pub struct BoundTrees<BT>(BT);
impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
type T = BT::T;
}
trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>;
}
impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees<BT>
where
BT::Tree: Clone,
{
type Node = BoundNode2<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 _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator {
self.0.comparator()
}
fn split(&self, node: &Self::Node) -> Split<'a, Self> {
let (tl, tr, key) = self.0.split(&node.node);
(
Bound {
bound: tl,
bounds: node.boundsl.clone(),
},
Bound {
bound: tr,
bounds: node.boundsr.clone(),
},
key,
)
}
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();
Self::bind(self.0.resolve(&reference.bound), move |node| {
let (_, _, key) = ctx.0.split(&node);
match bounds.clone().split(&key, ctx.comparator()) {
Ok((boundsl, boundsr)) => Self::pure(BoundNode2 {
boundsl,
boundsr,
bounds,
node,
}),
Err(e) => ctx.0.bounds_error(e),
}
})
}
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(),
})
}
}