241 lines
6.1 KiB
Rust
241 lines
6.1 KiB
Rust
use std::{marker::PhantomData, sync::Arc};
|
|
|
|
use crate::{
|
|
flow::{
|
|
binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *},
|
|
comparator::*,
|
|
},
|
|
func::context::*,
|
|
rcore::*,
|
|
};
|
|
|
|
use super::*;
|
|
|
|
pub struct TreeContext<Cl, T>(Cl, PhantomData<T>);
|
|
|
|
impl<Cl: Clone, T> TreeContext<Cl, T> {
|
|
fn new(comparator: Cl) -> Self {
|
|
Self(comparator, PhantomData)
|
|
}
|
|
}
|
|
|
|
impl<Cl: Clone, T> Clone for TreeContext<Cl, T> {
|
|
fn clone(&self) -> Self {
|
|
Self::new(self.0.clone())
|
|
}
|
|
}
|
|
|
|
pub enum TreeContextError<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> {
|
|
Resolution(ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>),
|
|
Extra(E),
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a>
|
|
From<ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>> for TreeContextError<'a, Ctx, A, E>
|
|
{
|
|
fn from(value: ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>) -> Self {
|
|
Self::Resolution(value)
|
|
}
|
|
}
|
|
|
|
type TreeParseError2<'a, A> = TreeParseError<ParseErrorA<'a, A>>;
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<TreeParseError2<'a, A>>
|
|
for TreeContextError<'a, Ctx, A, E>
|
|
{
|
|
fn from(value: TreeParseError2<'a, A>) -> Self {
|
|
ResolutionError::Parse(value).into()
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<HeightError>
|
|
for TreeContextError<'a, Ctx, A, E>
|
|
{
|
|
fn from(value: HeightError) -> Self {
|
|
TreeParseError::HeightValue(value).into()
|
|
}
|
|
}
|
|
|
|
pub type TreeContext2<'a, Ctx, A, C, E> = TreeContext<(Arc<C>, Fctr<'a, A>), (&'a (), Ctx, A, E)>;
|
|
|
|
impl<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> FunctorContext<'a> for TreeContext2<'a, Ctx, A, C, 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,
|
|
> BinaryTrees for TreeContext2<'a, Ctx, A, C, E>
|
|
{
|
|
type Node = Node<'a, Ctx, A>;
|
|
type Reference = Point<'a, Ctx, Self::Node>;
|
|
type Tree = Tree<'a, Ctx, A>;
|
|
type Key = A;
|
|
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<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> MonadTrees<'a> for TreeContext2<'a, Ctx, A, C, E>
|
|
{
|
|
type _Tm = Self::T;
|
|
|
|
fn resolve(
|
|
&self,
|
|
reference: &Self::Reference,
|
|
) -> crate::flow::binary::BTWrap<'a, Self, Self::Node> {
|
|
Ctx::stuff(reference.resolve_map(|resolved| {
|
|
resolved
|
|
.map(|rc| rc.as_ref().clone())
|
|
.map_err(TreeContextError::Resolution)
|
|
}))
|
|
}
|
|
}
|
|
|
|
impl<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> BinaryTreesEmpty<'a> for TreeContext2<'a, Ctx, A, C, E>
|
|
{
|
|
fn empty(&self) -> Self::Tree {
|
|
Tree {
|
|
node: Nullable::Null(NodeFactory {
|
|
factory: self.0 .1.clone(),
|
|
_ctx: PhantomData,
|
|
}),
|
|
height: 0,
|
|
}
|
|
}
|
|
|
|
fn split_key_empty(
|
|
&self,
|
|
_tree: Self::Tree,
|
|
_key: Self::Key,
|
|
) -> BTWrap<'a, Self, KeySplit<Self>> {
|
|
Self::pure((self.empty(), self.empty()))
|
|
}
|
|
}
|
|
|
|
impl<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> TreesHeight for TreeContext2<'a, Ctx, A, C, E>
|
|
{
|
|
fn height(&self, tree: &Self::Tree) -> u64 {
|
|
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> {
|
|
Self::fail(error.into())
|
|
}
|
|
}
|
|
|
|
pub enum WrappedExtra<W, E> {
|
|
Wrapped(W),
|
|
Extra(E),
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, W: 'a, E: 'a> From<WrappedExtra<W, E>>
|
|
for TreeContextError<'a, Ctx, A, WrappedExtra<W, E>>
|
|
{
|
|
fn from(value: WrappedExtra<W, E>) -> Self {
|
|
Self::Extra(value)
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<BalancingError>
|
|
for TreeContextError<'a, Ctx, A, WrappedExtra<BalancingError, E>>
|
|
{
|
|
fn from(value: BalancingError) -> Self {
|
|
WrappedExtra::Wrapped(value).into()
|
|
}
|
|
}
|
|
|
|
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<BoundsError<A>>
|
|
for TreeContextError<'a, Ctx, A, WrappedExtra<BoundsError<A>, E>>
|
|
{
|
|
fn from(value: BoundsError<A>) -> Self {
|
|
WrappedExtra::Wrapped(value).into()
|
|
}
|
|
}
|
|
|
|
impl<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BalancingError, E>>
|
|
{
|
|
fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree> {
|
|
let node = Nullable::from(node);
|
|
Self::pure(Tree { node, height })
|
|
}
|
|
|
|
fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T> {
|
|
Self::fail(error.into())
|
|
}
|
|
|
|
fn node_heights(&self, node: &Self::Node) -> (u64, u64) {
|
|
(node.l.height, node.r.height)
|
|
}
|
|
}
|
|
|
|
impl<
|
|
'a,
|
|
Ctx: Context<'a>,
|
|
A: Mentionable<'a, Ctx> + Clone,
|
|
C: 'a + Comparator<A>,
|
|
E: 'a + Send,
|
|
> BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BoundsError<A>, E>>
|
|
{
|
|
fn bounds_error<T: 'a + Send>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T> {
|
|
Self::fail(error.into())
|
|
}
|
|
}
|