From 8aceddd48e02c2fa3645ed7cf6f6e5af4b082a35 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 28 Dec 2025 17:10:15 +0300 Subject: [PATCH] Relation setter contexts --- src/directed_context/dynamic_wrapper.rs | 77 ++++++++- src/directed_context/fixed_wrapper.rs | 96 ++++++++++- src/lib.rs | 1 + src/parent2node_clojure/dynamic_wrappers.rs | 83 +++++++++- src/parent2node_clojure/fixed_wrapper.rs | 63 +++++++- src/relation_context/context.rs | 65 ++++++++ src/relation_context/converter.rs | 171 ++++++++++++++++++++ src/relation_context/mod.rs | 5 + 8 files changed, 541 insertions(+), 20 deletions(-) create mode 100644 src/relation_context/context.rs create mode 100644 src/relation_context/converter.rs create mode 100644 src/relation_context/mod.rs diff --git a/src/directed_context/dynamic_wrapper.rs b/src/directed_context/dynamic_wrapper.rs index 7e40ff9..b04d2eb 100644 --- a/src/directed_context/dynamic_wrapper.rs +++ b/src/directed_context/dynamic_wrapper.rs @@ -6,6 +6,7 @@ use crate::base_context::{ use crate::directed_context::context::{ DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, }; +use crate::relation_context::{BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, DirectedBinaryTreeRelationSetterContext}; pub struct DynamicDirectedBinaryTreeContextFromContext { ctx: CtxRef, @@ -41,13 +42,13 @@ impl NodeRefContainer for DynamicDirectedBinaryTreeContex } impl NodeRefContainer - for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { type NodeRef = Ctx::NodeRef; } impl BinaryTreeParentGetterContext - for DynamicDirectedBinaryTreeContextFromContext<&Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); @@ -55,7 +56,7 @@ impl BinaryTreeParentGetterContext } impl BinaryTreeParentGetterContext - for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); @@ -63,7 +64,7 @@ impl BinaryTreeParentGetterContext } impl DirectedBinaryTreeChildrenGetterContext - for DynamicDirectedBinaryTreeContextFromContext<&Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -81,7 +82,7 @@ impl DirectedBinaryTreeChildrenGetterConte } impl DirectedBinaryTreeChildrenGetterContext - for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -99,7 +100,7 @@ impl DirectedBinaryTreeChildrenGetterConte } unsafe impl DirectedBinaryTreeChildrenSetterContext - for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { match self.dir { @@ -145,7 +146,7 @@ unsafe impl DirectedBinaryTreeChildrenSett } unsafe impl BinaryTreeParentSetterContext - for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent); @@ -159,3 +160,65 @@ unsafe impl BinaryTreeParentSetterContext self.ctx.clearParent(node) } } + +unsafe impl BinaryTreeRootRelationSetterContext +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +{ + fn xSetRootRelation(&mut self, newRoot: Option) { + self.ctx.xSetRootRelation(newRoot) + } + + fn setRootRelation(&mut self, newRoot: Self::NodeRef) { + self.ctx.setRootRelation(newRoot) + } + + fn clearRoot(&mut self) { + self.ctx.clearRoot() + } +} + +unsafe impl DirectedBinaryTreeRelationSetterContext +for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> +{ + fn xSetForwardRelation(&mut self, node: Self::NodeRef, newChild: Option) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.xSetLeftRelation(node, newChild), + BinaryTreeDirection::RIGHT => self.ctx.xSetRightRelation(node, newChild), + } + } + + fn setForwardRelation(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.setLeftRelation(node, newChild), + BinaryTreeDirection::RIGHT => self.ctx.setRightRelation(node, newChild), + } + } + + fn clearForwardChild(&mut self, node: Self::NodeRef) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.clearLeftChild(node), + BinaryTreeDirection::RIGHT => self.ctx.clearRightChild(node), + } + } + + fn xSetOppositeRelation(&mut self, node: Self::NodeRef, newChild: Option) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.xSetRightRelation(node, newChild), + BinaryTreeDirection::RIGHT => self.ctx.xSetLeftRelation(node, newChild), + } + } + + fn setOppositeRelation(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.setRightRelation(node, newChild), + BinaryTreeDirection::RIGHT => self.ctx.setLeftRelation(node, newChild), + } + } + + fn clearOppositeChild(&mut self, node: Self::NodeRef) { + match self.dir { + BinaryTreeDirection::LEFT => self.ctx.clearRightChild(node), + BinaryTreeDirection::RIGHT => self.ctx.clearLeftChild(node), + } + } +} diff --git a/src/directed_context/fixed_wrapper.rs b/src/directed_context/fixed_wrapper.rs index 1f9b640..28a2dd3 100644 --- a/src/directed_context/fixed_wrapper.rs +++ b/src/directed_context/fixed_wrapper.rs @@ -3,9 +3,13 @@ use crate::base_context::{ BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeParentGetterContext, BinaryTreeParentSetterContext, }; -use crate::directed_context::context::{ +use crate::directed_context::{ DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, }; +use crate::relation_context::{ + BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, + DirectedBinaryTreeRelationSetterContext, +}; macro_rules! _mut_switch { ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { @@ -24,7 +28,7 @@ pub struct FixedRightDirectedBinaryTreeContextFromContext { macro_rules! _constructor { ($name:ident) => { - impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx Ctx> { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx> { pub fn wrap(ctx: &'ctx Ctx) -> Self { return Self { ctx }; } @@ -32,7 +36,7 @@ macro_rules! _constructor { }; ($name:ident mut) => { - impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx mut Ctx> { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx> { pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self { return Self { ctx }; } @@ -85,13 +89,17 @@ macro_rules! _children_get { }; } -_mut_switch!(_children_get!(FixedLeftDirectedBinaryTreeContextFromContext getLeftChild getRightChild)); -_mut_switch!(_children_get!(FixedRightDirectedBinaryTreeContextFromContext getRightChild getLeftChild)); +_mut_switch!(_children_get!( + FixedLeftDirectedBinaryTreeContextFromContext getLeftChild getRightChild +)); +_mut_switch!(_children_get!( + FixedRightDirectedBinaryTreeContextFromContext getRightChild getLeftChild +)); macro_rules! _parent_set { ($name:ident) => { unsafe impl BinaryTreeParentSetterContext - for $name <&mut Ctx> + for $name<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent); @@ -155,3 +163,79 @@ _children_set!( xSetRightChild setRightChild clearRightChild xSetLeftChild setLeftChild clearLeftChild ); + +macro_rules! _root_relation_set { + ($name:ident) => { + unsafe impl BinaryTreeRootRelationSetterContext + for $name<&mut Ctx> + { + fn xSetRootRelation(&mut self, newRoot: Option) { + self.ctx.xSetRootRelation(newRoot) + } + + fn setRootRelation(&mut self, newRoot: Self::NodeRef) { + self.ctx.setRootRelation(newRoot) + } + + fn clearRoot(&mut self) { + self.ctx.clearRoot() + } + } + }; +} +_root_relation_set!(FixedLeftDirectedBinaryTreeContextFromContext); +_root_relation_set!(FixedRightDirectedBinaryTreeContextFromContext); + +macro_rules! _relation_set { + ( + $name:ident + $fwd_xset:ident $fwd_set:ident $fwd_clear:ident + $op_xset:ident $op_set:ident $op_clear:ident + ) => { + unsafe impl DirectedBinaryTreeRelationSetterContext + for $name<&mut Ctx> + { + fn xSetForwardRelation( + &mut self, + node: Self::NodeRef, + newChild: Option, + ) { + self.ctx.$fwd_xset(node, newChild) + } + + fn xSetOppositeRelation( + &mut self, + node: Self::NodeRef, + newChild: Option, + ) { + self.ctx.$op_xset(node, newChild) + } + + fn setForwardRelation(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + self.ctx.$fwd_set(node, newChild) + } + + fn setOppositeRelation(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + self.ctx.$op_set(node, newChild) + } + + fn clearForwardChild(&mut self, node: Self::NodeRef) { + self.ctx.$fwd_clear(node) + } + + fn clearOppositeChild(&mut self, node: Self::NodeRef) { + self.ctx.$op_clear(node) + } + } + }; +} +_relation_set!( + FixedLeftDirectedBinaryTreeContextFromContext + xSetLeftRelation setLeftRelation clearLeftChild + xSetRightRelation setRightRelation clearRightChild +); +_relation_set!( + FixedRightDirectedBinaryTreeContextFromContext + xSetRightRelation setRightRelation clearRightChild + xSetLeftRelation setLeftRelation clearLeftChild +); diff --git a/src/lib.rs b/src/lib.rs index 91d2809..074eb55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,4 @@ pub mod algo; pub mod base_context; pub mod directed_context; pub mod parent2node_clojure; +pub mod relation_context; diff --git a/src/parent2node_clojure/dynamic_wrappers.rs b/src/parent2node_clojure/dynamic_wrappers.rs index b007cc5..fc48a07 100644 --- a/src/parent2node_clojure/dynamic_wrappers.rs +++ b/src/parent2node_clojure/dynamic_wrappers.rs @@ -9,6 +9,10 @@ use crate::directed_context::{ DirectedBinaryTreeDirection, }; use crate::parent2node_clojure::clojure::Parent2NodeGetterClojure; +use crate::relation_context::{ + BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, + DirectedBinaryTreeRelationSetterContext, Parent2NodeRelationSetterClojure, +}; use std::mem::ManuallyDrop; use std::ops::Deref; @@ -85,10 +89,12 @@ macro_rules! _constructor { }; } -_mut_switch!(_constructor!(DynamicParent2NodeGetterClojureFromContext BinaryTreeDirection)); -_mut_switch!( - _constructor!(DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection) -); +_mut_switch!(_constructor!( + DynamicParent2NodeGetterClojureFromContext BinaryTreeDirection +)); +_mut_switch!(_constructor!( + DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection +)); macro_rules! _node_ref { ($name:ident $($mut:tt)?) => { @@ -98,7 +104,9 @@ macro_rules! _node_ref { }; } -_mut_switch!(_node_ref!(DynamicParent2NodeGetterClojureFromContext)); +_mut_switch!(_node_ref!( + DynamicParent2NodeGetterClojureFromContext +)); _mut_switch!(_node_ref!( DynamicParent2NodeGetterClojureFromDirectedContext )); @@ -233,6 +241,71 @@ _set_as_parent_of!( DirectedBinaryTreeDirection FORWARD OPPOSITE ); +macro_rules! _set_relation { + ( + $name:ident $ctx_constraint:ident $dir_enum_lbl:ident + $d1_lbl:ident $d1_xset:ident $d1_set:ident $d1_clear:ident + $d2_lbl:ident $d2_xset:ident $d2_set:ident $d2_clear:ident + ) => { + unsafe impl<'ctx, Ctx: BinaryTreeRootRelationSetterContext + $ctx_constraint> + Parent2NodeRelationSetterClojure for $name<&'ctx mut Ctx, Ctx::NodeRef> + { + fn xSetRelation(&mut self, newChild: Option) { + match self.dir { + None => self.ctx.xSetRootRelation(newChild), + Some($dir_enum_lbl::$d1_lbl) => unsafe { + self.ctx + .$d1_xset((*self.node.node.deref()).clone(), newChild) + }, + Some($dir_enum_lbl::$d2_lbl) => unsafe { + self.ctx + .$d2_xset((*self.node.node.deref()).clone(), newChild) + }, + }; + } + + fn setRelation(&mut self, newChild: Self::NodeRef) { + match self.dir { + None => self.ctx.setRootRelation(newChild), + Some($dir_enum_lbl::$d1_lbl) => unsafe { + self.ctx + .$d1_set((*self.node.node.deref()).clone(), newChild) + }, + Some($dir_enum_lbl::$d2_lbl) => unsafe { + self.ctx + .$d2_set((*self.node.node.deref()).clone(), newChild) + }, + }; + } + + fn clearChild(&mut self) { + match self.dir { + None => self.ctx.clearRoot(), + Some($dir_enum_lbl::$d1_lbl) => unsafe { + self.ctx.$d1_clear((*self.node.node.deref()).clone()) + }, + Some($dir_enum_lbl::$d2_lbl) => unsafe { + self.ctx.$d2_clear((*self.node.node.deref()).clone()) + }, + }; + } + } + }; +} + +_set_relation!( + DynamicParent2NodeGetterClojureFromContext + BinaryTreeRelationSetterContext BinaryTreeDirection + LEFT xSetLeftRelation setLeftRelation clearLeftChild + RIGHT xSetRightRelation setRightRelation clearRightChild +); +_set_relation!( + DynamicParent2NodeGetterClojureFromDirectedContext + DirectedBinaryTreeRelationSetterContext DirectedBinaryTreeDirection + FORWARD xSetForwardRelation setForwardRelation clearForwardChild + OPPOSITE xSetOppositeRelation setOppositeRelation clearOppositeChild +); + macro_rules! _drop { ($name:ident $($mut:tt)?) => { impl Drop for $name { diff --git a/src/parent2node_clojure/fixed_wrapper.rs b/src/parent2node_clojure/fixed_wrapper.rs index 6925b7b..80129c2 100644 --- a/src/parent2node_clojure/fixed_wrapper.rs +++ b/src/parent2node_clojure/fixed_wrapper.rs @@ -9,6 +9,7 @@ use crate::directed_context::{ use crate::parent2node_clojure::clojure::{ Parent2NodeGetterClojure, Parent2NodeSetterClojure, ParentProviderClojure, }; +use crate::relation_context::{BinaryTreeRootRelationSetterContext, Parent2NodeRelationSetterClojure}; macro_rules! _mut_switch { ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { @@ -36,7 +37,7 @@ _struct!(FixedOppositeParent2NodeGetterClojureFromDirectedContext); macro_rules! _constructor { ($name:ident) => { - impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx Ctx, Ctx::NodeRef> { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx, Ctx::NodeRef> { pub fn wrap(ctx: &'ctx Ctx, node: Ctx::NodeRef) -> Self { return Self { ctx, node }; } @@ -44,7 +45,7 @@ macro_rules! _constructor { }; ($name:ident mut) => { - impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx mut Ctx, Ctx::NodeRef> { + 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 }; } @@ -224,3 +225,61 @@ _set_as_parent_of!(FixedLeftParent2NodeGetterClojureFromContext); _set_as_parent_of!(FixedRightParent2NodeGetterClojureFromContext); _set_as_parent_of!(FixedForwardParent2NodeGetterClojureFromDirectedContext); _set_as_parent_of!(FixedOppositeParent2NodeGetterClojureFromDirectedContext); + +macro_rules! _relation_setter { + ($name:ident $ctx:ident $xset:ident $set:ident $clear:ident) => { + unsafe impl<'ctx, Ctx: $ctx> Parent2NodeRelationSetterClojure + for $name<&'ctx mut Ctx, Ctx::NodeRef> + { + fn xSetRelation(&mut self, newChild: Option) { + self.ctx.$xset(self.node.clone(), newChild); + } + + fn setRelation(&mut self, newChild: Self::NodeRef) { + self.ctx.$set(self.node.clone(), newChild); + } + + fn clearChild(&mut self) { + self.ctx.$clear(self.node.clone()); + } + } + }; +} + +unsafe impl<'ctx, Ctx: BinaryTreeRootRelationSetterContext> + Parent2NodeRelationSetterClojure + for FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx> +{ + fn xSetRelation(&mut self, newChild: Option) { + self.ctx.xSetRootRelation(newChild); + } + + fn setRelation(&mut self, newChild: Self::NodeRef) { + self.ctx.setRootRelation(newChild); + } + + fn clearChild(&mut self) { + self.ctx.clearRoot() + } +} + +_relation_setter!( + FixedLeftParent2NodeGetterClojureFromContext + BinaryTreeChildrenSetterContext + xSetLeftChild setLeftChild clearLeftChild +); +_relation_setter!( + FixedRightParent2NodeGetterClojureFromContext + BinaryTreeChildrenSetterContext + xSetRightChild setRightChild clearRightChild +); +_relation_setter!( + FixedForwardParent2NodeGetterClojureFromDirectedContext + DirectedBinaryTreeChildrenSetterContext + xSetForwardChild setForwardChild clearForwardChild +); +_relation_setter!( + FixedOppositeParent2NodeGetterClojureFromDirectedContext + DirectedBinaryTreeChildrenSetterContext + xSetOppositeChild setOppositeChild clearOppositeChild +); diff --git a/src/relation_context/context.rs b/src/relation_context/context.rs new file mode 100644 index 0000000..9507a7f --- /dev/null +++ b/src/relation_context/context.rs @@ -0,0 +1,65 @@ +use crate::NodeRefContainer; + +pub unsafe trait BinaryTreeRootRelationSetterContext: NodeRefContainer { + fn xSetRootRelation(&mut self, newRoot: Option) { + match newRoot { + None => self.clearRoot(), + Some(nn) => self.setRootRelation(nn), + } + } + fn setRootRelation(&mut self, newRoot: Self::NodeRef); + fn clearRoot(&mut self); +} + +pub unsafe trait BinaryTreeRelationSetterContext: NodeRefContainer { + fn xSetLeftRelation(&mut self, parent: Self::NodeRef, child: Option) { + match child { + None => self.clearLeftChild(parent), + Some(nn) => self.setLeftRelation(parent, nn), + } + } + fn setLeftRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + fn clearLeftChild(&mut self, parent: Self::NodeRef); + + fn xSetRightRelation(&mut self, parent: Self::NodeRef, child: Option){ + match child { + None => self.clearRightChild(parent), + Some(nn) => self.setRightRelation(parent, nn), + } + } + fn setRightRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + fn clearRightChild(&mut self, parent: Self::NodeRef); +} + +pub unsafe trait DirectedBinaryTreeRelationSetterContext: NodeRefContainer { + fn xSetForwardRelation(&mut self, parent: Self::NodeRef, child: Option){ + match child { + None => self.clearForwardChild(parent), + Some(nn) => self.setForwardRelation(parent, nn), + } + } + fn setForwardRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + fn clearForwardChild(&mut self, parent: Self::NodeRef); + + fn xSetOppositeRelation(&mut self, parent: Self::NodeRef, child: Option){ + match child { + None => self.clearOppositeChild(parent), + Some(nn) => self.setOppositeRelation(parent, nn), + } + } + fn setOppositeRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + fn clearOppositeChild(&mut self, parent: Self::NodeRef); +} + +pub unsafe trait Parent2NodeRelationSetterClojure: NodeRefContainer { + fn xSetRelation(&mut self, newChild: Option) { + match newChild { + None => self.clearChild(), + Some(nn) => self.setRelation(nn), + } + } + + fn setRelation(&mut self, newChild: Self::NodeRef); + + fn clearChild(&mut self); +} diff --git a/src/relation_context/converter.rs b/src/relation_context/converter.rs new file mode 100644 index 0000000..88b9086 --- /dev/null +++ b/src/relation_context/converter.rs @@ -0,0 +1,171 @@ +use crate::NodeRefContainer; +use crate::base_context::{BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeParentGetterContext, BinaryTreeParentSetterContext, BinaryTreeRootSetter}; +use crate::directed_context::{DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext}; +use crate::relation_context::{ + BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, + DirectedBinaryTreeRelationSetterContext, +}; + +macro_rules! _mut_switch { + ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { + $macro$excl_mark($($args)+); + $macro$excl_mark($($args)+ mut); + }; +} + +pub struct BinaryTreeRelationContextFromBaseContextConverter { + ctx: CtxRef, +} + +pub struct DirectedBinaryTreeRelationContextFromBaseContextConverter { + ctx: CtxRef, +} + +macro_rules! _constructor { + ($name:ident) => { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx> { + pub fn wrap(ctx: &'ctx Ctx) -> Self { + return Self { ctx }; + } + } + }; + + ($name:ident mut) => { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx> { + pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self { + return Self { ctx }; + } + } + }; +} + +_mut_switch!(_constructor!( + BinaryTreeRelationContextFromBaseContextConverter +)); +_mut_switch!(_constructor!( + DirectedBinaryTreeRelationContextFromBaseContextConverter +)); + +macro_rules! _node_ref { + ($name:ident $($mut:tt)?) => { + impl NodeRefContainer for $name<& $($mut)? Ctx> { + type NodeRef = Ctx::NodeRef; + } + }; +} + +_mut_switch!(_node_ref!( + BinaryTreeRelationContextFromBaseContextConverter +)); +_mut_switch!(_node_ref!( + DirectedBinaryTreeRelationContextFromBaseContextConverter +)); + +macro_rules! _parent_get { + ($name:ident $($mut:tt)?) => { + impl BinaryTreeParentGetterContext + for $name<& $($mut)? Ctx> + { + fn getParent(&self, node: Self::NodeRef) -> Option { + return self.ctx.getParent(node); + } + } + }; +} + +_mut_switch!(_parent_get!( + BinaryTreeRelationContextFromBaseContextConverter +)); +_mut_switch!(_parent_get!( + DirectedBinaryTreeRelationContextFromBaseContextConverter +)); + +macro_rules! _children_get { + ($name:ident $ctx:ident $d1:ident $d2:ident $($mut:tt)? ) => { + impl $ctx + for $name <& $($mut)? Ctx> + { + fn $d1(&self, node: Self::NodeRef) -> Option { + return self.ctx.$d1(node); + } + + fn $d2(&self, node: Self::NodeRef) -> Option { + return self.ctx.$d2(node); + } + } + }; +} + +_mut_switch!(_children_get!( + BinaryTreeRelationContextFromBaseContextConverter + BinaryTreeChildrenGetterContext getLeftChild getRightChild +)); +_mut_switch!(_children_get!( + DirectedBinaryTreeRelationContextFromBaseContextConverter + DirectedBinaryTreeChildrenGetterContext getForwardChild getOppositeChild +)); + +macro_rules! _root_relation_set { + ($name:ident) => { + unsafe impl + BinaryTreeRootRelationSetterContext for $name<&mut Ctx> + { + fn setRootRelation(&mut self, newRoot: Self::NodeRef) { + self.ctx.setRoot(newRoot.clone()); + self.ctx.clearParent(newRoot) + } + + fn clearRoot(&mut self) { + self.ctx.clearRoot() + } + } + }; +} +_root_relation_set!(BinaryTreeRelationContextFromBaseContextConverter); +_root_relation_set!(DirectedBinaryTreeRelationContextFromBaseContextConverter); + +unsafe impl + BinaryTreeRelationSetterContext + for BinaryTreeRelationContextFromBaseContextConverter<&mut Ctx> +{ + fn setLeftRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef) { + self.ctx.setParent(child.clone(), parent.clone()); + self.ctx.setLeftChild(parent, child) + } + + fn clearLeftChild(&mut self, parent: Self::NodeRef) { + self.ctx.clearLeftChild(parent) + } + + fn setRightRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef) { + self.ctx.setParent(child.clone(), parent.clone()); + self.ctx.setRightChild(parent, child) + } + + fn clearRightChild(&mut self, parent: Self::NodeRef) { + self.ctx.clearRightChild(parent); + } +} + +unsafe impl + DirectedBinaryTreeRelationSetterContext + for DirectedBinaryTreeRelationContextFromBaseContextConverter<&mut Ctx> +{ + fn setForwardRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef) { + self.ctx.setParent(child.clone(), parent.clone()); + self.ctx.setForwardChild(parent, child) + } + + fn clearForwardChild(&mut self, parent: Self::NodeRef) { + self.ctx.clearForwardChild(parent) + } + + fn setOppositeRelation(&mut self, parent: Self::NodeRef, child: Self::NodeRef) { + self.ctx.setParent(child.clone(), parent.clone()); + self.ctx.setOppositeChild(parent, child) + } + + fn clearOppositeChild(&mut self, parent: Self::NodeRef) { + self.ctx.clearOppositeChild(parent); + } +} diff --git a/src/relation_context/mod.rs b/src/relation_context/mod.rs new file mode 100644 index 0000000..2aeaf06 --- /dev/null +++ b/src/relation_context/mod.rs @@ -0,0 +1,5 @@ +mod context; +mod converter; + +pub use context::*; +pub use converter::*; \ No newline at end of file