More flexable context components with clearer naming

This commit is contained in:
Andrew Golovashevich 2025-12-26 18:46:53 +03:00
parent 54972e8215
commit cbebfec06d
7 changed files with 57 additions and 70 deletions

View File

@ -7,21 +7,26 @@ pub trait BinaryTreeRootGetter: NodeRefContainer {
}
pub trait BinaryTreeRootSetter: NodeRefContainer {
fn xSetRoot(&mut self, newRoot: Option<Self::NodeRef>);
fn xSetRoot(&mut self, newRoot: Option<Self::NodeRef>) {
match newRoot {
None => self.clearRoot(),
Some(nn) => self.setRoot(nn),
}
}
fn setRoot(&mut self, newRoot: Self::NodeRef);
fn clearRoot(&mut self);
}
pub trait BinaryTreeDownWalker: NodeRefContainer {
pub trait BinaryTreeChildrenGetterContext: NodeRefContainer {
fn getLeftChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getRightChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
}
pub trait BinaryTreeUpWalker: NodeRefContainer {
pub trait BinaryTreeParentGetterContext: NodeRefContainer {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
}
pub unsafe trait BinaryTreeDownBuilder: NodeRefContainer {
pub unsafe trait BinaryTreeChildrenSetterContext: NodeRefContainer {
fn xSetLeftChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearLeftChild(node),
@ -42,7 +47,7 @@ pub unsafe trait BinaryTreeDownBuilder: NodeRefContainer {
fn clearRightChild(&mut self, node: Self::NodeRef);
}
pub unsafe trait BinaryTreeBuilder: BinaryTreeDownBuilder {
pub unsafe trait BinaryTreeParentSetterContext: NodeRefContainer {
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
match newParent {
None => self.clearParent(node),

View File

@ -1,11 +1,11 @@
use crate::NodeRefContainer;
pub trait DirectedBinaryTreeDownWalker: NodeRefContainer {
pub trait DirectedBinaryTreeChildrenGetterContext: NodeRefContainer {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
}
pub unsafe trait DirectedBinaryTreeDownBuilder: NodeRefContainer {
pub unsafe trait DirectedBinaryTreeChildrenSetterContext: NodeRefContainer {
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearForwardChild(node),
@ -25,16 +25,3 @@ pub unsafe trait DirectedBinaryTreeDownBuilder: NodeRefContainer {
fn clearForwardChild(&mut self, node: Self::NodeRef);
fn clearOppositeChild(&mut self, node: Self::NodeRef);
}
pub unsafe trait DirectedBinaryTreeBuilder: DirectedBinaryTreeDownBuilder {
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
match newParent {
None => self.clearParent(node),
Some(nn) => self.setParent(node, nn),
}
}
fn setParent(&mut self, node: Self::NodeRef, newParent: Self::NodeRef);
fn clearParent(&mut self, node: Self::NodeRef);
}

View File

@ -1,9 +1,7 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker,
};
use crate::context::{BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeParentSetterContext, BinaryTreeParentGetterContext};
use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext,
};
use crate::direction::BinaryTreeDirection;
@ -44,19 +42,19 @@ impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeWrappe
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeWrapper<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWrapper<&mut Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeWrapper<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for DynamicDirectedBinaryTreeWrapper<&Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -74,7 +72,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -92,7 +90,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
unsafe impl<Ctx: BinaryTreeChildrenSetterContext> DirectedBinaryTreeChildrenSetterContext
for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
@ -138,7 +136,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
}
}
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
for DynamicDirectedBinaryTreeWrapper<&mut Ctx>
{
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {

View File

@ -1,9 +1,7 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker,
};
use crate::context::{BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeParentSetterContext, BinaryTreeParentGetterContext};
use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext,
};
pub struct LeftDirectedBinaryTreeWalker<CtxRef> {
@ -53,31 +51,31 @@ impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeWalker<&
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for LeftDirectedBinaryTreeWalker<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<&mut Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for LeftDirectedBinaryTreeWalker<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for RightDirectedBinaryTreeWalker<&Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<&mut Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for RightDirectedBinaryTreeWalker<&mut Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for LeftDirectedBinaryTreeWalker<&Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -89,7 +87,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for LeftDirectedBinaryTreeWalker<&mut Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -101,7 +99,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for RightDirectedBinaryTreeWalker<&Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -113,7 +111,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for RightDirectedBinaryTreeWalker<&mut Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
@ -125,7 +123,7 @@ impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
}
}
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
unsafe impl<Ctx: BinaryTreeChildrenSetterContext> DirectedBinaryTreeChildrenSetterContext
for LeftDirectedBinaryTreeWalker<&mut Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
@ -153,7 +151,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
}
}
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
unsafe impl<Ctx: BinaryTreeChildrenSetterContext> DirectedBinaryTreeChildrenSetterContext
for RightDirectedBinaryTreeWalker<&mut Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
@ -180,7 +178,7 @@ unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
self.ctx.clearLeftChild(node)
}
}
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
for LeftDirectedBinaryTreeWalker<&mut Ctx>
{
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
@ -196,7 +194,7 @@ unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
}
}
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
for RightDirectedBinaryTreeWalker<&mut Ctx>
{
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {

View File

@ -1,6 +1,5 @@
mod context;
mod impls;
pub use context::DirectedBinaryTreeBuilder;
pub use context::DirectedBinaryTreeDownBuilder;
pub use context::DirectedBinaryTreeDownWalker;
pub use context::DirectedBinaryTreeChildrenSetterContext;
pub use context::DirectedBinaryTreeChildrenGetterContext;

View File

@ -1,10 +1,10 @@
use crate::NodeRefContainer;
pub trait Parent2NodeGetter: NodeRefContainer {
pub trait Parent2NodeGetterClojure: NodeRefContainer {
fn getChild(&self) -> Self::NodeRef;
}
pub trait Parent2NodeSetter: NodeRefContainer {
pub trait Parent2NodeSetterClojure: NodeRefContainer {
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearChild(),

View File

@ -1,9 +1,9 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeRootGetter, BinaryTreeRootSetter,
BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeRootGetter, BinaryTreeRootSetter,
};
use crate::directed::{DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker};
use crate::parent2node::clojure::{Parent2NodeGetter, Parent2NodeSetter};
use crate::directed::{DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext};
use crate::parent2node::clojure::{Parent2NodeGetterClojure, Parent2NodeSetterClojure};
struct Parent2LeftChildContextWrapper<CtxRef, NodeRef> {
ctx: CtxRef,
@ -161,7 +161,7 @@ impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -169,7 +169,7 @@ impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -177,7 +177,7 @@ impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -185,7 +185,7 @@ impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -193,7 +193,7 @@ impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetterClojure
for Root2NodeContextWrapper<&'ctx CtxRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -201,7 +201,7 @@ impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetterClojure
for Root2NodeContextWrapper<&'ctx mut CtxRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -211,7 +211,7 @@ impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -219,7 +219,7 @@ for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -227,7 +227,7 @@ for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -235,7 +235,7 @@ for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure
for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
@ -243,7 +243,7 @@ for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
}
}
impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
impl<'ctx, CtxRef: BinaryTreeChildrenSetterContext> Parent2NodeSetterClojure
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
@ -259,7 +259,7 @@ impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
}
}
impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
impl<'ctx, CtxRef: BinaryTreeChildrenSetterContext> Parent2NodeSetterClojure
for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
@ -275,7 +275,7 @@ impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
}
}
impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetter
impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetterClojure
for Root2NodeContextWrapper<&'ctx mut CtxRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
@ -292,7 +292,7 @@ impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetter
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownBuilder> Parent2NodeSetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenSetterContext> Parent2NodeSetterClojure
for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
@ -308,7 +308,7 @@ for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownBuilder> Parent2NodeSetter
impl<'ctx, CtxRef: DirectedBinaryTreeChildrenSetterContext> Parent2NodeSetterClojure
for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {