TreeContext more impls

This commit is contained in:
AF 2023-06-18 14:41:35 +00:00
parent c3e992bc10
commit fce211571f

View File

@ -1,7 +1,10 @@
use std::{marker::PhantomData, rc::Rc}; use std::{marker::PhantomData, rc::Rc};
use crate::{ use crate::{
flow::{binary::*, comparator::*}, flow::{
binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *},
comparator::*,
},
func::context::*, func::context::*,
rcore::*, rcore::*,
rstd::fallible::*, rstd::fallible::*,
@ -9,32 +12,61 @@ use crate::{
use super::*; use super::*;
struct TreeContext<C, T>(Rc<C>, PhantomData<T>); pub struct TreeContext<Cl, T>(Cl, PhantomData<T>);
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a> impl<Cl: Clone, T> TreeContext<Cl, T> {
TreeContext<C, (Ctx, A, E)> fn new(comparator: Cl) -> Self {
{
fn new(comparator: Rc<C>) -> Self {
Self(comparator, PhantomData) Self(comparator, PhantomData)
} }
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a> Clone impl<Cl: Clone, T> Clone for TreeContext<Cl, T> {
for TreeContext<C, (Ctx, A, E)>
{
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.0.clone()) Self::new(self.0.clone())
} }
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a> pub enum TreeContextError<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> {
FunctorContext<'a> for TreeContext<C, (Ctx, A, E)> 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>
{ {
type T = FallibleMonad<'a, Ctx, Result<ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>, E>>; fn from(value: ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>) -> Self {
Self::Resolution(value)
}
}
type TreeParseError2<'a, Ctx, A> = TreeParseError<ParseError<'a, Ctx, Fctr<'a, Ctx, A>>>;
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, E: 'a> From<TreeParseError2<'a, Ctx, A>>
for TreeContextError<'a, Ctx, A, E>
{
fn from(value: TreeParseError2<'a, Ctx, 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<(Rc<C>, Fctr<'a, Ctx, A>), (Ctx, A, E)>;
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
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> impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
BinaryTrees<'a> for TreeContext<C, (Ctx, A, E)> BinaryTrees<'a> for TreeContext2<'a, Ctx, A, C, E>
{ {
type Node = Node<'a, Ctx, A>; type Node = Node<'a, Ctx, A>;
@ -49,7 +81,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
type _Tm = Self::T; type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator { fn comparator(&self) -> &Self::Comparator {
&self.0 self.0 .0.as_ref()
} }
fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<'a, Self> { fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<'a, Self> {
@ -61,7 +93,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
reference: &Self::Reference, reference: &Self::Reference,
) -> crate::flow::binary::BTWrap<'a, Self, Self::Node> { ) -> crate::flow::binary::BTWrap<'a, Self, Self::Node> {
Ctx::stuff(Ctx::fmap(reference.resolve(), |resolved| { Ctx::stuff(Ctx::fmap(reference.resolve(), |resolved| {
resolved.map(|rc| rc.as_ref().clone()).map_err(Ok) resolved
.map(|rc| rc.as_ref().clone())
.map_err(TreeContextError::Resolution)
})) }))
} }
@ -73,3 +107,84 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
tree.node.as_ref().cloned() tree.node.as_ref().cloned()
} }
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
BinaryTreesEmpty<'a> for TreeContext2<'a, Ctx, A, C, E>
{
fn empty(&self) -> Self::Tree {
Tree {
node: Nullable::Null(NodeFactory(self.0 .1.clone())),
height: 0,
}
}
fn split_key_empty(
&self,
_tree: Self::Tree,
_key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
Self::pure((self.empty(), self.empty()))
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
BinaryTreesHeight<'a> for TreeContext2<'a, Ctx, A, C, E>
{
fn height(&self, tree: &Self::Tree) -> u64 {
tree.height
}
fn height_error<T: 'a>(&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>
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>(&self, error: BalancingError) -> BTWrap<'a, Self, T> {
Self::fail(error.into())
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BoundsError<A>, E>>
{
fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T> {
Self::fail(error.into())
}
}