Relation setter contexts
This commit is contained in:
parent
bde59410d5
commit
8aceddd48e
@ -6,6 +6,7 @@ use crate::base_context::{
|
|||||||
use crate::directed_context::context::{
|
use crate::directed_context::context::{
|
||||||
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
||||||
};
|
};
|
||||||
|
use crate::relation_context::{BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, DirectedBinaryTreeRelationSetterContext};
|
||||||
|
|
||||||
pub struct DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
|
pub struct DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
|
||||||
ctx: CtxRef,
|
ctx: CtxRef,
|
||||||
@ -159,3 +160,65 @@ unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
|
|||||||
self.ctx.clearParent(node)
|
self.ctx.clearParent(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl<Ctx: BinaryTreeRootRelationSetterContext> BinaryTreeRootRelationSetterContext
|
||||||
|
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
|
||||||
|
{
|
||||||
|
fn xSetRootRelation(&mut self, newRoot: Option<Self::NodeRef>) {
|
||||||
|
self.ctx.xSetRootRelation(newRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setRootRelation(&mut self, newRoot: Self::NodeRef) {
|
||||||
|
self.ctx.setRootRelation(newRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clearRoot(&mut self) {
|
||||||
|
self.ctx.clearRoot()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl<Ctx: BinaryTreeRelationSetterContext> DirectedBinaryTreeRelationSetterContext
|
||||||
|
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
|
||||||
|
{
|
||||||
|
fn xSetForwardRelation(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
|
||||||
|
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<Self::NodeRef>) {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -3,9 +3,13 @@ use crate::base_context::{
|
|||||||
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext,
|
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext,
|
||||||
BinaryTreeParentGetterContext, BinaryTreeParentSetterContext,
|
BinaryTreeParentGetterContext, BinaryTreeParentSetterContext,
|
||||||
};
|
};
|
||||||
use crate::directed_context::context::{
|
use crate::directed_context::{
|
||||||
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
||||||
};
|
};
|
||||||
|
use crate::relation_context::{
|
||||||
|
BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext,
|
||||||
|
DirectedBinaryTreeRelationSetterContext,
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! _mut_switch {
|
macro_rules! _mut_switch {
|
||||||
($macro:tt $excl_mark:tt ( $($args:tt)+) ) => {
|
($macro:tt $excl_mark:tt ( $($args:tt)+) ) => {
|
||||||
@ -85,8 +89,12 @@ macro_rules! _children_get {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_mut_switch!(_children_get!(FixedLeftDirectedBinaryTreeContextFromContext getLeftChild getRightChild));
|
_mut_switch!(_children_get!(
|
||||||
_mut_switch!(_children_get!(FixedRightDirectedBinaryTreeContextFromContext getRightChild getLeftChild));
|
FixedLeftDirectedBinaryTreeContextFromContext getLeftChild getRightChild
|
||||||
|
));
|
||||||
|
_mut_switch!(_children_get!(
|
||||||
|
FixedRightDirectedBinaryTreeContextFromContext getRightChild getLeftChild
|
||||||
|
));
|
||||||
|
|
||||||
macro_rules! _parent_set {
|
macro_rules! _parent_set {
|
||||||
($name:ident) => {
|
($name:ident) => {
|
||||||
@ -155,3 +163,79 @@ _children_set!(
|
|||||||
xSetRightChild setRightChild clearRightChild
|
xSetRightChild setRightChild clearRightChild
|
||||||
xSetLeftChild setLeftChild clearLeftChild
|
xSetLeftChild setLeftChild clearLeftChild
|
||||||
);
|
);
|
||||||
|
|
||||||
|
macro_rules! _root_relation_set {
|
||||||
|
($name:ident) => {
|
||||||
|
unsafe impl<Ctx: BinaryTreeRootRelationSetterContext> BinaryTreeRootRelationSetterContext
|
||||||
|
for $name<&mut Ctx>
|
||||||
|
{
|
||||||
|
fn xSetRootRelation(&mut self, newRoot: Option<Self::NodeRef>) {
|
||||||
|
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<Ctx: BinaryTreeRelationSetterContext> DirectedBinaryTreeRelationSetterContext
|
||||||
|
for $name<&mut Ctx>
|
||||||
|
{
|
||||||
|
fn xSetForwardRelation(
|
||||||
|
&mut self,
|
||||||
|
node: Self::NodeRef,
|
||||||
|
newChild: Option<Self::NodeRef>,
|
||||||
|
) {
|
||||||
|
self.ctx.$fwd_xset(node, newChild)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn xSetOppositeRelation(
|
||||||
|
&mut self,
|
||||||
|
node: Self::NodeRef,
|
||||||
|
newChild: Option<Self::NodeRef>,
|
||||||
|
) {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|||||||
@ -5,3 +5,4 @@ pub mod algo;
|
|||||||
pub mod base_context;
|
pub mod base_context;
|
||||||
pub mod directed_context;
|
pub mod directed_context;
|
||||||
pub mod parent2node_clojure;
|
pub mod parent2node_clojure;
|
||||||
|
pub mod relation_context;
|
||||||
|
|||||||
@ -9,6 +9,10 @@ use crate::directed_context::{
|
|||||||
DirectedBinaryTreeDirection,
|
DirectedBinaryTreeDirection,
|
||||||
};
|
};
|
||||||
use crate::parent2node_clojure::clojure::Parent2NodeGetterClojure;
|
use crate::parent2node_clojure::clojure::Parent2NodeGetterClojure;
|
||||||
|
use crate::relation_context::{
|
||||||
|
BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext,
|
||||||
|
DirectedBinaryTreeRelationSetterContext, Parent2NodeRelationSetterClojure,
|
||||||
|
};
|
||||||
use std::mem::ManuallyDrop;
|
use std::mem::ManuallyDrop;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
@ -85,10 +89,12 @@ macro_rules! _constructor {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_mut_switch!(_constructor!(DynamicParent2NodeGetterClojureFromContext BinaryTreeDirection));
|
_mut_switch!(_constructor!(
|
||||||
_mut_switch!(
|
DynamicParent2NodeGetterClojureFromContext BinaryTreeDirection
|
||||||
_constructor!(DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection)
|
));
|
||||||
);
|
_mut_switch!(_constructor!(
|
||||||
|
DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection
|
||||||
|
));
|
||||||
|
|
||||||
macro_rules! _node_ref {
|
macro_rules! _node_ref {
|
||||||
($name:ident $($mut:tt)?) => {
|
($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!(
|
_mut_switch!(_node_ref!(
|
||||||
DynamicParent2NodeGetterClojureFromDirectedContext
|
DynamicParent2NodeGetterClojureFromDirectedContext
|
||||||
));
|
));
|
||||||
@ -233,6 +241,71 @@ _set_as_parent_of!(
|
|||||||
DirectedBinaryTreeDirection FORWARD OPPOSITE
|
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<Self::NodeRef>) {
|
||||||
|
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 {
|
macro_rules! _drop {
|
||||||
($name:ident $($mut:tt)?) => {
|
($name:ident $($mut:tt)?) => {
|
||||||
impl<CtxRef, NodeRef> Drop for $name<CtxRef, NodeRef> {
|
impl<CtxRef, NodeRef> Drop for $name<CtxRef, NodeRef> {
|
||||||
|
|||||||
@ -9,6 +9,7 @@ use crate::directed_context::{
|
|||||||
use crate::parent2node_clojure::clojure::{
|
use crate::parent2node_clojure::clojure::{
|
||||||
Parent2NodeGetterClojure, Parent2NodeSetterClojure, ParentProviderClojure,
|
Parent2NodeGetterClojure, Parent2NodeSetterClojure, ParentProviderClojure,
|
||||||
};
|
};
|
||||||
|
use crate::relation_context::{BinaryTreeRootRelationSetterContext, Parent2NodeRelationSetterClojure};
|
||||||
|
|
||||||
macro_rules! _mut_switch {
|
macro_rules! _mut_switch {
|
||||||
($macro:tt $excl_mark:tt ( $($args:tt)+) ) => {
|
($macro:tt $excl_mark:tt ( $($args:tt)+) ) => {
|
||||||
@ -224,3 +225,61 @@ _set_as_parent_of!(FixedLeftParent2NodeGetterClojureFromContext);
|
|||||||
_set_as_parent_of!(FixedRightParent2NodeGetterClojureFromContext);
|
_set_as_parent_of!(FixedRightParent2NodeGetterClojureFromContext);
|
||||||
_set_as_parent_of!(FixedForwardParent2NodeGetterClojureFromDirectedContext);
|
_set_as_parent_of!(FixedForwardParent2NodeGetterClojureFromDirectedContext);
|
||||||
_set_as_parent_of!(FixedOppositeParent2NodeGetterClojureFromDirectedContext);
|
_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::NodeRef>) {
|
||||||
|
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::NodeRef>) {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|||||||
65
src/relation_context/context.rs
Normal file
65
src/relation_context/context.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
use crate::NodeRefContainer;
|
||||||
|
|
||||||
|
pub unsafe trait BinaryTreeRootRelationSetterContext: NodeRefContainer {
|
||||||
|
fn xSetRootRelation(&mut self, newRoot: Option<Self::NodeRef>) {
|
||||||
|
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<Self::NodeRef>) {
|
||||||
|
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<Self::NodeRef>){
|
||||||
|
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<Self::NodeRef>){
|
||||||
|
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<Self::NodeRef>){
|
||||||
|
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<Self::NodeRef>) {
|
||||||
|
match newChild {
|
||||||
|
None => self.clearChild(),
|
||||||
|
Some(nn) => self.setRelation(nn),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setRelation(&mut self, newChild: Self::NodeRef);
|
||||||
|
|
||||||
|
fn clearChild(&mut self);
|
||||||
|
}
|
||||||
171
src/relation_context/converter.rs
Normal file
171
src/relation_context/converter.rs
Normal file
@ -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<CtxRef> {
|
||||||
|
ctx: CtxRef,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DirectedBinaryTreeRelationContextFromBaseContextConverter<CtxRef> {
|
||||||
|
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<Ctx: NodeRefContainer> 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<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
|
||||||
|
for $name<& $($mut)? Ctx>
|
||||||
|
{
|
||||||
|
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
||||||
|
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: $ctx> $ctx
|
||||||
|
for $name <& $($mut)? Ctx>
|
||||||
|
{
|
||||||
|
fn $d1(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
||||||
|
return self.ctx.$d1(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn $d2(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
||||||
|
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<Ctx: BinaryTreeParentSetterContext + BinaryTreeRootSetter>
|
||||||
|
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<Ctx: BinaryTreeParentSetterContext + BinaryTreeChildrenSetterContext>
|
||||||
|
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<Ctx: BinaryTreeParentSetterContext + DirectedBinaryTreeChildrenSetterContext>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/relation_context/mod.rs
Normal file
5
src/relation_context/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod context;
|
||||||
|
mod converter;
|
||||||
|
|
||||||
|
pub use context::*;
|
||||||
|
pub use converter::*;
|
||||||
Loading…
Reference in New Issue
Block a user