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 { 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 DynamicDirectedBinaryTreeContextFromContext { pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) { self.dir = newDirection } } impl NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { type NodeRef = Ctx::NodeRef; } impl NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { type NodeRef = Ctx::NodeRef; } impl BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } impl BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } impl DirectedBinaryTreeChildrenGetterContext for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { BinaryTreeDirection::LEFT => return self.ctx.getLeftChild(node), BinaryTreeDirection::RIGHT => return self.ctx.getRightChild(node), } } fn getOppositeChild(&self, node: Self::NodeRef) -> Option { match self.dir { BinaryTreeDirection::LEFT => return self.ctx.getRightChild(node), BinaryTreeDirection::RIGHT => return self.ctx.getLeftChild(node), } } } impl DirectedBinaryTreeChildrenGetterContext for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { BinaryTreeDirection::LEFT => return self.ctx.getLeftChild(node), BinaryTreeDirection::RIGHT => return self.ctx.getRightChild(node), } } fn getOppositeChild(&self, node: Self::NodeRef) -> Option { match self.dir { BinaryTreeDirection::LEFT => return self.ctx.getRightChild(node), BinaryTreeDirection::RIGHT => return self.ctx.getLeftChild(node), } } } unsafe impl DirectedBinaryTreeChildrenSetterContext for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { 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) { 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 BinaryTreeParentSetterContext for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { 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 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), } } }