From fce211571fb39829cc7ce2f906721d6c934f74ab Mon Sep 17 00:00:00 2001 From: timofey Date: Sun, 18 Jun 2023 14:41:35 +0000 Subject: [PATCH] `TreeContext` more `impl`s --- src/rstd/collections/tree/context.rs | 145 ++++++++++++++++++++++++--- 1 file changed, 130 insertions(+), 15 deletions(-) diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index 1e109fa..71f547f 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -1,7 +1,10 @@ use std::{marker::PhantomData, rc::Rc}; use crate::{ - flow::{binary::*, comparator::*}, + flow::{ + binary::{balancing::*, bound::BinaryTreesBindable, bounds::*, *}, + comparator::*, + }, func::context::*, rcore::*, rstd::fallible::*, @@ -9,32 +12,61 @@ use crate::{ use super::*; -struct TreeContext(Rc, PhantomData); +pub struct TreeContext(Cl, PhantomData); -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> - TreeContext -{ - fn new(comparator: Rc) -> Self { +impl TreeContext { + fn new(comparator: Cl) -> Self { Self(comparator, PhantomData) } } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> Clone - for TreeContext -{ +impl Clone for TreeContext { fn clone(&self) -> Self { Self::new(self.0.clone()) } } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> - FunctorContext<'a> for TreeContext +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> { - type T = FallibleMonad<'a, Ctx, Result>, E>>; + fn from(value: ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>) -> Self { + Self::Resolution(value) + } +} + +type TreeParseError2<'a, Ctx, 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, Ctx, 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<(Rc, Fctr<'a, Ctx, A>), (Ctx, A, E)>; + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, 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, E: 'a> - BinaryTrees<'a> for TreeContext + BinaryTrees<'a> for TreeContext2<'a, Ctx, A, C, E> { type Node = Node<'a, Ctx, A>; @@ -49,7 +81,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator &Self::Comparator { - &self.0 + self.0 .0.as_ref() } 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 crate::flow::binary::BTWrap<'a, Self, Self::Node> { 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: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, 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, E: 'a> + BinaryTreesHeight<'a> for TreeContext2<'a, Ctx, A, C, E> +{ + fn height(&self, tree: &Self::Tree) -> u64 { + tree.height + } + + 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> + 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()) + } +} + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> + BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra, E>> +{ + fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T> { + Self::fail(error.into()) + } +}