213 lines
7.9 KiB
Rust
213 lines
7.9 KiB
Rust
use crate::NodeRefContainer;
|
|
use crate::base_context::{
|
|
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeDirection,
|
|
BinaryTreeDirectionDispatcherClojure, BinaryTreeParentGetterContext,
|
|
BinaryTreeParentSetterContext,
|
|
};
|
|
use crate::base_context::{BinaryTreeRootGetter, BinaryTreeRootSetter};
|
|
use crate::directed_context::context::{
|
|
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
|
|
};
|
|
use crate::directed_context::{
|
|
DirectedBinaryTreeDirection, DirectedBinaryTreeDirectionDispatcherClojure,
|
|
};
|
|
use crate::relation_context::{
|
|
BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext,
|
|
DirectedBinaryTreeRelationSetterContext,
|
|
};
|
|
|
|
include!("../_common_macros.rs");
|
|
include!("_directed_common_macros.rs");
|
|
|
|
pub struct DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
|
|
ctx: CtxRef,
|
|
dir: BinaryTreeDirection,
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeContextFromContext<&'ctx Ctx> {
|
|
pub fn wrap(ctx: &'ctx Ctx, initialDirection: BinaryTreeDirection) -> Self {
|
|
return Self {
|
|
ctx,
|
|
dir: initialDirection,
|
|
};
|
|
}
|
|
}
|
|
|
|
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeContextFromContext<&'ctx mut Ctx> {
|
|
pub fn wrap_mut(ctx: &'ctx mut Ctx, initialDirection: BinaryTreeDirection) -> Self {
|
|
return Self {
|
|
ctx,
|
|
dir: initialDirection,
|
|
};
|
|
}
|
|
}
|
|
|
|
impl<CtxRef> DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
|
|
pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) {
|
|
self.dir = newDirection
|
|
}
|
|
}
|
|
|
|
_mut_switch!(_delegate_node_ref!(
|
|
DynamicDirectedBinaryTreeContextFromContext
|
|
));
|
|
|
|
_mut_switch!(_delegate_root_getter!(
|
|
DynamicDirectedBinaryTreeContextFromContext
|
|
));
|
|
|
|
_mut_switch!(_delegate_parent_getter!(
|
|
DynamicDirectedBinaryTreeContextFromContext
|
|
));
|
|
|
|
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
|
|
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
|
|
{
|
|
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => return self.ctx.getLeftChild(node),
|
|
BinaryTreeDirection::RIGHT => return self.ctx.getRightChild(node),
|
|
}
|
|
}
|
|
|
|
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => return self.ctx.getRightChild(node),
|
|
BinaryTreeDirection::RIGHT => return self.ctx.getLeftChild(node),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
|
|
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
|
|
{
|
|
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => return self.ctx.getLeftChild(node),
|
|
BinaryTreeDirection::RIGHT => return self.ctx.getRightChild(node),
|
|
}
|
|
}
|
|
|
|
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => return self.ctx.getRightChild(node),
|
|
BinaryTreeDirection::RIGHT => return self.ctx.getLeftChild(node),
|
|
}
|
|
}
|
|
}
|
|
|
|
_delegate_root_setter!(DynamicDirectedBinaryTreeContextFromContext);
|
|
_delegate_parent_setter!(DynamicDirectedBinaryTreeContextFromContext);
|
|
|
|
unsafe impl<Ctx: BinaryTreeChildrenSetterContext> DirectedBinaryTreeChildrenSetterContext
|
|
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
|
|
{
|
|
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => self.ctx.xSetLeftChild(node, newChild),
|
|
BinaryTreeDirection::RIGHT => self.ctx.xSetRightChild(node, newChild),
|
|
}
|
|
}
|
|
|
|
fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => self.ctx.xSetRightChild(node, newChild),
|
|
BinaryTreeDirection::RIGHT => self.ctx.xSetLeftChild(node, newChild),
|
|
}
|
|
}
|
|
|
|
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => self.ctx.setLeftChild(node, newChild),
|
|
BinaryTreeDirection::RIGHT => self.ctx.setRightChild(node, newChild),
|
|
}
|
|
}
|
|
|
|
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => self.ctx.setRightChild(node, newChild),
|
|
BinaryTreeDirection::RIGHT => self.ctx.setLeftChild(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 clearOppositeChild(&mut self, node: Self::NodeRef) {
|
|
match self.dir {
|
|
BinaryTreeDirection::LEFT => self.ctx.clearRightChild(node),
|
|
BinaryTreeDirection::RIGHT => self.ctx.clearLeftChild(node),
|
|
}
|
|
}
|
|
}
|
|
|
|
_delegate_root_relation_setter!(DynamicDirectedBinaryTreeContextFromContext);
|
|
|
|
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),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Ctx: BinaryTreeDirectionDispatcherClojure> DirectedBinaryTreeDirectionDispatcherClojure
|
|
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
|
|
{
|
|
fn determineDirection(&self, node: Self::NodeRef) -> Option<DirectedBinaryTreeDirection> {
|
|
match self.ctx.determineDirection(node) {
|
|
None => return None,
|
|
Some(BinaryTreeDirection::LEFT) => match self.dir {
|
|
BinaryTreeDirection::LEFT => return Some(DirectedBinaryTreeDirection::FORWARD),
|
|
BinaryTreeDirection::RIGHT => return Some(DirectedBinaryTreeDirection::OPPOSITE),
|
|
},
|
|
Some(BinaryTreeDirection::RIGHT) => match self.dir {
|
|
BinaryTreeDirection::LEFT => return Some(DirectedBinaryTreeDirection::OPPOSITE),
|
|
BinaryTreeDirection::RIGHT => return Some(DirectedBinaryTreeDirection::FORWARD),
|
|
},
|
|
}
|
|
}
|
|
}
|