TreeContext

This commit is contained in:
AF 2023-06-18 13:53:51 +00:00
parent 0aa6feaa2d
commit 41106290b9
3 changed files with 103 additions and 0 deletions

View File

@ -1,3 +1,5 @@
pub mod context;
use std::{error::Error, fmt::Display, rc::Rc}; use std::{error::Error, fmt::Display, rc::Rc};
use crate::{ use crate::{
@ -190,3 +192,22 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory
Ok(mentionable) Ok(mentionable)
} }
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> Clone for Node<'a, Ctx, A> {
fn clone(&self) -> Self {
Self {
l: self.l.clone(),
r: self.r.clone(),
key: self.key.clone(),
}
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> Clone for Tree<'a, Ctx, A> {
fn clone(&self) -> Self {
Self {
node: self.node.clone(),
height: self.height,
}
}
}

View File

@ -0,0 +1,75 @@
use std::{marker::PhantomData, rc::Rc};
use crate::{
flow::{binary::*, comparator::*},
func::context::*,
rcore::*,
rstd::fallible::*,
};
use super::*;
struct TreeContext<C, T>(Rc<C>, PhantomData<T>);
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
TreeContext<C, (Ctx, A, E)>
{
fn new(comparator: Rc<C>) -> Self {
Self(comparator, PhantomData)
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a> Clone
for TreeContext<C, (Ctx, A, E)>
{
fn clone(&self) -> Self {
Self::new(self.0.clone())
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
FunctorContext<'a> for TreeContext<C, (Ctx, A, E)>
{
type T = FallibleMonad<'a, Ctx, Result<ResolutionFailure<'a, Ctx, Node<'a, Ctx, A>>, E>>;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A>, E: 'a>
BinaryTrees<'a> for TreeContext<C, (Ctx, A, 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;
type _Tm = Self::T;
fn comparator(&self) -> &Self::Comparator {
&self.0
}
fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<'a, Self> {
(node.l.clone(), node.r.clone(), node.key.clone())
}
fn resolve(
&self,
reference: &Self::Reference,
) -> 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)
}))
}
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()
}
}

View File

@ -96,6 +96,13 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Nullable<'a, Ctx, A> {
fn from_mentionable(mentionable: Rc<A>) -> Self { fn from_mentionable(mentionable: Rc<A>) -> Self {
Self::NotNull(mentionable.into()) Self::NotNull(mentionable.into())
} }
pub fn as_ref(&self) -> Option<&Point<'a, Ctx, A>> {
match self {
Nullable::Null(_) => None,
Nullable::NotNull(point) => Some(point),
}
}
} }
impl<F> NullableFactory<F> { impl<F> NullableFactory<F> {