Merged mutable and immutable structs of same directed wrappers

This commit is contained in:
Andrew Golovashevich 2025-12-24 19:04:02 +03:00
parent bd89611f70
commit 7c46a55374
2 changed files with 40 additions and 54 deletions

View File

@ -2,68 +2,62 @@ use crate::NodeRefContainer;
use crate::context::{ use crate::context::{
BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker, BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker,
}; };
use crate::direction::BinaryTreeDirection;
use crate::directed::context::{ use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker, DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
}; };
use crate::direction::BinaryTreeDirection;
pub struct DynamicDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { pub struct DynamicDirectedBinaryTreeWrapper<CtxRef> {
ctx: &'ctx Ctx, ctx: CtxRef,
dir: BinaryTreeDirection, dir: BinaryTreeDirection,
} }
pub struct DynamicDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> { impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWrapper<&'ctx Ctx> {
ctx: &'ctx mut Ctx,
dir: BinaryTreeDirection,
}
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWalker<'ctx, Ctx> {
pub fn wrap(ctx: &'ctx Ctx, initialDirection: BinaryTreeDirection) -> Self { pub fn wrap(ctx: &'ctx Ctx, initialDirection: BinaryTreeDirection) -> Self {
return Self { return Self {
ctx, ctx,
dir: initialDirection, dir: initialDirection,
}; };
} }
pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) {
self.dir = newDirection
}
} }
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeEditor<'ctx, Ctx> {
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWrapper<&'ctx mut Ctx> {
pub fn wrap(ctx: &'ctx mut Ctx, initialDirection: BinaryTreeDirection) -> Self { pub fn wrap(ctx: &'ctx mut Ctx, initialDirection: BinaryTreeDirection) -> Self {
return Self { return Self {
ctx, ctx,
dir: initialDirection, dir: initialDirection,
}; };
} }
}
impl<CtxRef> DynamicDirectedBinaryTreeWrapper<CtxRef> {
pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) { pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) {
self.dir = newDirection self.dir = newDirection
} }
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeWrapper<&Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeWrapper<&mut Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for DynamicDirectedBinaryTreeWalker<'_, Ctx> for DynamicDirectedBinaryTreeWrapper<&Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
match self.dir { match self.dir {
@ -81,7 +75,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for DynamicDirectedBinaryTreeEditor<'_, Ctx> for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
match self.dir { match self.dir {
@ -99,7 +93,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for DynamicDirectedBinaryTreeEditor<'_, Ctx> for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{ {
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match self.dir { match self.dir {
@ -145,7 +139,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
} }
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for DynamicDirectedBinaryTreeEditor<'_, Ctx> for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{ {
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
self.ctx.xSetParent(node, newParent); self.ctx.xSetParent(node, newParent);

View File

@ -6,87 +6,79 @@ use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker, DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
}; };
pub struct LeftDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { pub struct LeftDirectedBinaryTreeWalker<CtxRef> {
ctx: &'ctx Ctx, ctx: CtxRef,
} }
pub struct LeftDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> { pub struct RightDirectedBinaryTreeWalker<CtxRef> {
ctx: &'ctx mut Ctx, ctx: CtxRef,
} }
pub struct RightDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<&'ctx Ctx> {
ctx: &'ctx Ctx,
}
pub struct RightDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> {
ctx: &'ctx mut Ctx,
}
impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<'ctx, Ctx> {
pub fn wrap(ctx: &'ctx Ctx) -> Self { pub fn wrap(ctx: &'ctx Ctx) -> Self {
return Self { ctx }; return Self { ctx };
} }
} }
impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeEditor<'ctx, Ctx> { impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<&'ctx mut Ctx> {
pub fn wrap(ctx: &'ctx mut Ctx) -> Self { pub fn wrap(ctx: &'ctx mut Ctx) -> Self {
return Self { ctx }; return Self { ctx };
} }
} }
impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<'ctx, Ctx> { impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<&'ctx Ctx> {
pub fn wrap(ctx: &'ctx Ctx) -> Self { pub fn wrap(ctx: &'ctx Ctx) -> Self {
return Self { ctx }; return Self { ctx };
} }
} }
impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeEditor<'ctx, Ctx> { impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<&'ctx mut Ctx> {
pub fn wrap(ctx: &'ctx mut Ctx) -> Self { pub fn wrap(ctx: &'ctx mut Ctx) -> Self {
return Self { ctx }; return Self { ctx };
} }
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeWalker<&Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeWalker<&mut Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeWalker<&Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeWalker<&mut Ctx> {
type NodeRef = Ctx::NodeRef; type NodeRef = Ctx::NodeRef;
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeEditor<'_, Ctx> { impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node); return self.ctx.getParent(node);
} }
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for LeftDirectedBinaryTreeWalker<'_, Ctx> for LeftDirectedBinaryTreeWalker<&Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node); return self.ctx.getLeftChild(node);
@ -98,7 +90,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for LeftDirectedBinaryTreeEditor<'_, Ctx> for LeftDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node); return self.ctx.getLeftChild(node);
@ -110,7 +102,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for RightDirectedBinaryTreeWalker<'_, Ctx> for RightDirectedBinaryTreeWalker<&Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node); return self.ctx.getRightChild(node);
@ -122,7 +114,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for RightDirectedBinaryTreeEditor<'_, Ctx> for RightDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> { fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node); return self.ctx.getRightChild(node);
@ -134,7 +126,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
} }
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for LeftDirectedBinaryTreeEditor<'_, Ctx> for LeftDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetLeftChild(node, newChild) self.ctx.xSetLeftChild(node, newChild)
@ -162,7 +154,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
} }
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for RightDirectedBinaryTreeEditor<'_, Ctx> for RightDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetRightChild(node, newChild) self.ctx.xSetRightChild(node, newChild)
@ -189,7 +181,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
} }
} }
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for LeftDirectedBinaryTreeEditor<'_, Ctx> for LeftDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
self.ctx.xSetParent(node, newParent); self.ctx.xSetParent(node, newParent);
@ -205,7 +197,7 @@ unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
} }
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for RightDirectedBinaryTreeEditor<'_, Ctx> for RightDirectedBinaryTreeWalker<&mut Ctx>
{ {
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
self.ctx.xSetParent(node, newParent); self.ctx.xSetParent(node, newParent);