use std::{marker::PhantomData, sync::Arc}; use crate::{ flow::{ binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *}, comparator::*, }, func::context::*, rcore::*, }; use super::*; pub struct TreeContext(Cl, PhantomData); impl TreeContext { fn new(comparator: Cl) -> Self { Self(comparator, PhantomData) } } impl Clone for TreeContext { 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>> 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>; impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From> 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 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, Fctr<'a, A>), (&'a (), Ctx, A, E)>; impl< 'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, 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, 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 { (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 { tree.node.as_ref().cloned() } } impl< 'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, 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, 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::pure((self.empty(), self.empty())) } } impl< 'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, 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, E: 'a + Send, > TreesHeightError<'a> for TreeContext2<'a, Ctx, A, C, E> { fn height_error(&self, error: HeightError) -> BTWrap<'a, Self, T> { Self::fail(error.into()) } } pub enum WrappedExtra { Wrapped(W), Extra(E), } impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, W: 'a, E: 'a> From> for TreeContextError<'a, Ctx, A, WrappedExtra> { fn from(value: WrappedExtra) -> Self { Self::Extra(value) } } impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From for TreeContextError<'a, Ctx, A, WrappedExtra> { fn from(value: BalancingError) -> Self { WrappedExtra::Wrapped(value).into() } } impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From> for TreeContextError<'a, Ctx, A, WrappedExtra, E>> { fn from(value: BoundsError) -> Self { WrappedExtra::Wrapped(value).into() } } impl< 'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a + Send, > BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra> { 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(&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, E: 'a + Send, > BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra, E>> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T> { Self::fail(error.into()) } }