From 7c46a553749d107b84c1547f8dcf78c604f4ce80 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 24 Dec 2025 19:04:02 +0300 Subject: [PATCH] Merged mutable and immutable structs of same directed wrappers --- src/directed/impls/dynamic_wrapper.rs | 38 ++++++++---------- src/directed/impls/fixed_wrapper.rs | 56 ++++++++++++--------------- 2 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/directed/impls/dynamic_wrapper.rs b/src/directed/impls/dynamic_wrapper.rs index b8a45b1..48eca08 100644 --- a/src/directed/impls/dynamic_wrapper.rs +++ b/src/directed/impls/dynamic_wrapper.rs @@ -2,68 +2,62 @@ use crate::NodeRefContainer; use crate::context::{ BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker, }; -use crate::direction::BinaryTreeDirection; use crate::directed::context::{ DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker, }; +use crate::direction::BinaryTreeDirection; -pub struct DynamicDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx Ctx, +pub struct DynamicDirectedBinaryTreeWrapper { + ctx: CtxRef, dir: BinaryTreeDirection, } -pub struct DynamicDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx mut Ctx, - dir: BinaryTreeDirection, -} - -impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWalker<'ctx, Ctx> { +impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWrapper<&'ctx Ctx> { pub fn wrap(ctx: &'ctx Ctx, initialDirection: BinaryTreeDirection) -> Self { return Self { ctx, 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 { return Self { ctx, dir: initialDirection, }; } +} +impl DynamicDirectedBinaryTreeWrapper { pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) { self.dir = newDirection } } -impl NodeRefContainer for DynamicDirectedBinaryTreeWalker<'_, Ctx> { +impl NodeRefContainer for DynamicDirectedBinaryTreeWrapper<&Ctx> { type NodeRef = Ctx::NodeRef; } -impl NodeRefContainer for DynamicDirectedBinaryTreeEditor<'_, Ctx> { +impl NodeRefContainer for DynamicDirectedBinaryTreeWrapper<&mut Ctx> { type NodeRef = Ctx::NodeRef; } -impl BinaryTreeUpWalker for DynamicDirectedBinaryTreeWalker<'_, Ctx> { +impl BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } -impl BinaryTreeUpWalker for DynamicDirectedBinaryTreeEditor<'_, Ctx> { +impl BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&mut Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } impl DirectedBinaryTreeDownWalker - for DynamicDirectedBinaryTreeWalker<'_, Ctx> + for DynamicDirectedBinaryTreeWrapper<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -81,7 +75,7 @@ impl DirectedBinaryTreeDownWalker } impl DirectedBinaryTreeDownWalker - for DynamicDirectedBinaryTreeEditor<'_, Ctx> + for DynamicDirectedBinaryTreeWrapper<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -99,7 +93,7 @@ impl DirectedBinaryTreeDownWalker } unsafe impl DirectedBinaryTreeDownBuilder - for DynamicDirectedBinaryTreeEditor<'_, Ctx> + for DynamicDirectedBinaryTreeWrapper<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { match self.dir { @@ -145,7 +139,7 @@ unsafe impl DirectedBinaryTreeDownBuilder } unsafe impl DirectedBinaryTreeBuilder - for DynamicDirectedBinaryTreeEditor<'_, Ctx> + for DynamicDirectedBinaryTreeWrapper<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent); diff --git a/src/directed/impls/fixed_wrapper.rs b/src/directed/impls/fixed_wrapper.rs index e8c01f5..0d1e2b1 100644 --- a/src/directed/impls/fixed_wrapper.rs +++ b/src/directed/impls/fixed_wrapper.rs @@ -6,87 +6,79 @@ use crate::directed::context::{ DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker, }; -pub struct LeftDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx Ctx, +pub struct LeftDirectedBinaryTreeWalker { + ctx: CtxRef, } -pub struct LeftDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx mut Ctx, +pub struct RightDirectedBinaryTreeWalker { + ctx: CtxRef, } -pub struct RightDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx Ctx, -} - -pub struct RightDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> { - ctx: &'ctx mut Ctx, -} - -impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<'ctx, Ctx> { +impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<&'ctx Ctx> { pub fn wrap(ctx: &'ctx Ctx) -> Self { 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 { return Self { ctx }; } } -impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<'ctx, Ctx> { +impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<&'ctx Ctx> { pub fn wrap(ctx: &'ctx Ctx) -> Self { 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 { return Self { ctx }; } } -impl NodeRefContainer for LeftDirectedBinaryTreeWalker<'_, Ctx> { +impl NodeRefContainer for LeftDirectedBinaryTreeWalker<&Ctx> { type NodeRef = Ctx::NodeRef; } -impl NodeRefContainer for LeftDirectedBinaryTreeEditor<'_, Ctx> { +impl NodeRefContainer for LeftDirectedBinaryTreeWalker<&mut Ctx> { type NodeRef = Ctx::NodeRef; } -impl NodeRefContainer for RightDirectedBinaryTreeWalker<'_, Ctx> { +impl NodeRefContainer for RightDirectedBinaryTreeWalker<&Ctx> { type NodeRef = Ctx::NodeRef; } -impl NodeRefContainer for RightDirectedBinaryTreeEditor<'_, Ctx> { +impl NodeRefContainer for RightDirectedBinaryTreeWalker<&mut Ctx> { type NodeRef = Ctx::NodeRef; } -impl BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<'_, Ctx> { +impl BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } -impl BinaryTreeUpWalker for LeftDirectedBinaryTreeEditor<'_, Ctx> { +impl BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&mut Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } -impl BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<'_, Ctx> { +impl BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } -impl BinaryTreeUpWalker for RightDirectedBinaryTreeEditor<'_, Ctx> { +impl BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&mut Ctx> { fn getParent(&self, node: Self::NodeRef) -> Option { return self.ctx.getParent(node); } } impl DirectedBinaryTreeDownWalker - for LeftDirectedBinaryTreeWalker<'_, Ctx> + for LeftDirectedBinaryTreeWalker<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { return self.ctx.getLeftChild(node); @@ -98,7 +90,7 @@ impl DirectedBinaryTreeDownWalker } impl DirectedBinaryTreeDownWalker - for LeftDirectedBinaryTreeEditor<'_, Ctx> + for LeftDirectedBinaryTreeWalker<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { return self.ctx.getLeftChild(node); @@ -110,7 +102,7 @@ impl DirectedBinaryTreeDownWalker } impl DirectedBinaryTreeDownWalker - for RightDirectedBinaryTreeWalker<'_, Ctx> + for RightDirectedBinaryTreeWalker<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { return self.ctx.getRightChild(node); @@ -122,7 +114,7 @@ impl DirectedBinaryTreeDownWalker } impl DirectedBinaryTreeDownWalker - for RightDirectedBinaryTreeEditor<'_, Ctx> + for RightDirectedBinaryTreeWalker<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { return self.ctx.getRightChild(node); @@ -134,7 +126,7 @@ impl DirectedBinaryTreeDownWalker } unsafe impl DirectedBinaryTreeDownBuilder - for LeftDirectedBinaryTreeEditor<'_, Ctx> + for LeftDirectedBinaryTreeWalker<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { self.ctx.xSetLeftChild(node, newChild) @@ -162,7 +154,7 @@ unsafe impl DirectedBinaryTreeDownBuilder } unsafe impl DirectedBinaryTreeDownBuilder - for RightDirectedBinaryTreeEditor<'_, Ctx> + for RightDirectedBinaryTreeWalker<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { self.ctx.xSetRightChild(node, newChild) @@ -189,7 +181,7 @@ unsafe impl DirectedBinaryTreeDownBuilder } } unsafe impl DirectedBinaryTreeBuilder - for LeftDirectedBinaryTreeEditor<'_, Ctx> + for LeftDirectedBinaryTreeWalker<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent); @@ -205,7 +197,7 @@ unsafe impl DirectedBinaryTreeBuilder } unsafe impl DirectedBinaryTreeBuilder - for RightDirectedBinaryTreeEditor<'_, Ctx> + for RightDirectedBinaryTreeWalker<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent);