binary-tree-core-0.rs/src/directed_context/dynamic_wrapper.rs

225 lines
7.9 KiB
Rust

use crate::NodeRefContainer;
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeDirection,
BinaryTreeParentGetterContext, BinaryTreeParentSetterContext,
};
use crate::directed_context::context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
};
use crate::relation_context::{BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, DirectedBinaryTreeRelationSetterContext};
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
}
}
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
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),
}
}
}
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),
}
}
}
unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
self.ctx.xSetParent(node, newParent);
}
fn setParent(&mut self, node: Self::NodeRef, newParent: Self::NodeRef) {
self.ctx.setParent(node, newParent);
}
fn clearParent(&mut self, node: Self::NodeRef) {
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),
}
}
}