diff --git a/src/rstd/collections/tree.rs b/src/rstd/collections/tree.rs index 61c1826..f0bdb14 100644 --- a/src/rstd/collections/tree.rs +++ b/src/rstd/collections/tree.rs @@ -1,3 +1,5 @@ +pub mod context; + use std::{error::Error, fmt::Display, rc::Rc}; use crate::{ @@ -190,3 +192,22 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for TreeFactory 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, + } + } +} diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs new file mode 100644 index 0000000..1e109fa --- /dev/null +++ b/src/rstd/collections/tree/context.rs @@ -0,0 +1,75 @@ +use std::{marker::PhantomData, rc::Rc}; + +use crate::{ + flow::{binary::*, comparator::*}, + func::context::*, + rcore::*, + rstd::fallible::*, +}; + +use super::*; + +struct TreeContext(Rc, PhantomData); + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> + TreeContext +{ + fn new(comparator: Rc) -> Self { + Self(comparator, PhantomData) + } +} + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> 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 +{ + type T = FallibleMonad<'a, Ctx, Result>, E>>; +} + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, E: 'a> + BinaryTrees<'a> for TreeContext +{ + 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 { + tree.node.as_ref().cloned() + } +} diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 78257d3..2012e9f 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -96,6 +96,13 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Nullable<'a, Ctx, A> { fn from_mentionable(mentionable: Rc) -> Self { 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 NullableFactory {