227 lines
6.9 KiB
Rust
227 lines
6.9 KiB
Rust
use crate::NodeRefContainer;
|
|
use crate::context::{
|
|
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext,
|
|
BinaryTreeParentSetterContext, BinaryTreeRootGetter, BinaryTreeRootSetter,
|
|
};
|
|
use crate::directed::{
|
|
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
|
};
|
|
use crate::parent2node::clojure::{
|
|
Parent2NodeGetterClojure, Parent2NodeSetterClojure, ParentProviderClojure,
|
|
};
|
|
|
|
macro_rules! _mut_switch {
|
|
($macro:tt $excl_mark:tt ( $($args:tt)+) ) => {
|
|
$macro$excl_mark($($args)+);
|
|
$macro$excl_mark($($args)+ mut);
|
|
};
|
|
}
|
|
|
|
macro_rules! _struct {
|
|
($name:ident) => {
|
|
pub struct $name<CtxRef, NodeRef> {
|
|
ctx: CtxRef,
|
|
node: NodeRef,
|
|
}
|
|
};
|
|
}
|
|
|
|
pub struct FixedRootParent2NodeGetterClojureFromContext<NodeRef> {
|
|
ctx: NodeRef,
|
|
}
|
|
_struct!(FixedLeftParent2NodeGetterClojureFromContext);
|
|
_struct!(FixedRightParent2NodeGetterClojureFromContext);
|
|
_struct!(FixedForwardParent2NodeGetterClojureFromDirectedContext);
|
|
_struct!(FixedOppositeParent2NodeGetterClojureFromDirectedContext);
|
|
|
|
macro_rules! _constructor {
|
|
($name:ident) => {
|
|
impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx Ctx, Ctx::NodeRef> {
|
|
pub fn wrap(ctx: &'ctx Ctx, node: Ctx::NodeRef) -> Self {
|
|
return Self { ctx, node };
|
|
}
|
|
}
|
|
};
|
|
|
|
($name:ident mut) => {
|
|
impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx mut Ctx, Ctx::NodeRef> {
|
|
pub fn wrap_mut(ctx: &'ctx mut Ctx, node: Ctx::NodeRef) -> Self {
|
|
return Self { ctx, node };
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer + BinaryTreeRootGetter>
|
|
FixedRootParent2NodeGetterClojureFromContext<&'ctx Ctx>
|
|
{
|
|
fn wrap(ctx: &'ctx Ctx) -> Self {
|
|
return Self { ctx };
|
|
}
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer> FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx> {
|
|
fn wrap_mut(ctx: &'ctx mut Ctx) -> Self {
|
|
return Self { ctx };
|
|
}
|
|
}
|
|
|
|
_mut_switch!(_constructor!(FixedLeftParent2NodeGetterClojureFromContext));
|
|
_mut_switch!(_constructor!(FixedRightParent2NodeGetterClojureFromContext));
|
|
_mut_switch!(_constructor!(
|
|
FixedForwardParent2NodeGetterClojureFromDirectedContext
|
|
));
|
|
_mut_switch!(_constructor!(
|
|
FixedOppositeParent2NodeGetterClojureFromDirectedContext
|
|
));
|
|
|
|
macro_rules! _node_ref {
|
|
($name:ident $($mut:tt)?) => {
|
|
impl<Ctx: NodeRefContainer> NodeRefContainer for $name<& $($mut)? Ctx, Ctx::NodeRef> {
|
|
type NodeRef = Ctx::NodeRef;
|
|
}
|
|
};
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer> NodeRefContainer
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx Ctx>
|
|
{
|
|
type NodeRef = Ctx::NodeRef;
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer> NodeRefContainer
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx>
|
|
{
|
|
type NodeRef = Ctx::NodeRef;
|
|
}
|
|
|
|
_mut_switch!(_node_ref!(FixedLeftParent2NodeGetterClojureFromContext));
|
|
_mut_switch!(_node_ref!(FixedRightParent2NodeGetterClojureFromContext));
|
|
_mut_switch!(_node_ref!(
|
|
FixedForwardParent2NodeGetterClojureFromDirectedContext
|
|
));
|
|
_mut_switch!(_node_ref!(
|
|
FixedOppositeParent2NodeGetterClojureFromDirectedContext
|
|
));
|
|
|
|
macro_rules! _get_child {
|
|
($name:ident $ctx_type:ident $fn:ident $($mut:tt)? ) => {
|
|
impl<'ctx, Ctx: $ctx_type> Parent2NodeGetterClojure
|
|
for $name <&'ctx $($mut)? Ctx, Ctx::NodeRef>
|
|
{
|
|
fn getChild(&self) -> Option<Self::NodeRef> {
|
|
return self.ctx.$fn (self.node.clone());
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl<'ctx, Ctx: BinaryTreeRootGetter> Parent2NodeGetterClojure
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx Ctx>
|
|
{
|
|
fn getChild(&self) -> Option<Self::NodeRef> {
|
|
return self.ctx.getRoot();
|
|
}
|
|
}
|
|
|
|
impl<'ctx, Ctx: BinaryTreeRootGetter> Parent2NodeGetterClojure
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx>
|
|
{
|
|
fn getChild(&self) -> Option<Self::NodeRef> {
|
|
return self.ctx.getRoot();
|
|
}
|
|
}
|
|
|
|
_mut_switch!(_get_child!(
|
|
FixedLeftParent2NodeGetterClojureFromContext
|
|
BinaryTreeChildrenGetterContext getLeftChild
|
|
));
|
|
_mut_switch!(_get_child!(
|
|
FixedRightParent2NodeGetterClojureFromContext
|
|
BinaryTreeChildrenGetterContext getRightChild
|
|
));
|
|
_mut_switch!(_get_child!(
|
|
FixedForwardParent2NodeGetterClojureFromDirectedContext
|
|
DirectedBinaryTreeChildrenGetterContext getForwardChild
|
|
));
|
|
_mut_switch!(_get_child!(
|
|
FixedOppositeParent2NodeGetterClojureFromDirectedContext
|
|
DirectedBinaryTreeChildrenGetterContext getOppositeChild
|
|
));
|
|
|
|
macro_rules! _set_child {
|
|
($name:ident $ctx:ident $xset:ident $set:ident $clear:ident) => {
|
|
impl<'ctx, Ctx: $ctx> Parent2NodeSetterClojure for $name<&'ctx mut Ctx, Ctx::NodeRef> {
|
|
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
|
|
self.ctx.$xset(self.node.clone(), newChild);
|
|
}
|
|
|
|
fn setChild(&mut self, newChild: Self::NodeRef) {
|
|
self.ctx.$set(self.node.clone(), newChild);
|
|
}
|
|
|
|
fn clearChild(&mut self) {
|
|
self.ctx.$clear(self.node.clone());
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl<'ctx, Ctx: BinaryTreeRootSetter> Parent2NodeSetterClojure
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx>
|
|
{
|
|
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
|
|
self.ctx.xSetRoot(newChild)
|
|
}
|
|
|
|
fn setChild(&mut self, newChild: Self::NodeRef) {
|
|
self.ctx.setRoot(newChild)
|
|
}
|
|
|
|
fn clearChild(&mut self) {
|
|
self.ctx.clearRoot()
|
|
}
|
|
}
|
|
|
|
_set_child!(
|
|
FixedLeftParent2NodeGetterClojureFromContext
|
|
BinaryTreeChildrenSetterContext xSetLeftChild setLeftChild clearLeftChild
|
|
);
|
|
_set_child!(
|
|
FixedRightParent2NodeGetterClojureFromContext
|
|
BinaryTreeChildrenSetterContext xSetRightChild setRightChild clearRightChild
|
|
);
|
|
_set_child!(
|
|
FixedForwardParent2NodeGetterClojureFromDirectedContext
|
|
DirectedBinaryTreeChildrenSetterContext xSetForwardChild setForwardChild clearForwardChild
|
|
);
|
|
_set_child!(
|
|
FixedOppositeParent2NodeGetterClojureFromDirectedContext
|
|
DirectedBinaryTreeChildrenSetterContext xSetOppositeChild setOppositeChild clearOppositeChild
|
|
);
|
|
|
|
macro_rules! _set_as_parent_of {
|
|
($name:ident) => {
|
|
impl<'ctx, Ctx: BinaryTreeParentSetterContext> ParentProviderClojure
|
|
for $name<&'ctx mut Ctx, Ctx::NodeRef>
|
|
{
|
|
fn setAsParentOf(&mut self, child: Self::NodeRef) {
|
|
self.ctx.setParent(child, self.node.clone())
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl<'ctx, Ctx: BinaryTreeParentSetterContext> ParentProviderClojure
|
|
for FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx>
|
|
{
|
|
fn setAsParentOf(&mut self, child: Self::NodeRef) {
|
|
self.ctx.clearParent(child)
|
|
}
|
|
}
|
|
|
|
_set_as_parent_of!(FixedLeftParent2NodeGetterClojureFromContext);
|
|
_set_as_parent_of!(FixedRightParent2NodeGetterClojureFromContext);
|
|
_set_as_parent_of!(FixedForwardParent2NodeGetterClojureFromDirectedContext);
|
|
_set_as_parent_of!(FixedOppositeParent2NodeGetterClojureFromDirectedContext);
|