Splited context to multiple traits and extracted directed contexts as independent

This commit is contained in:
Andrew Golovashevich 2025-12-24 12:48:32 +03:00
parent 6855724041
commit bd89611f70
9 changed files with 478 additions and 33 deletions

View File

@ -1,11 +1,46 @@
pub unsafe trait BinTreeContext {
type NodeRef: Sized;
pub trait NodeRefContainer {
type NodeRef: Sized + Copy;
}
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
pub trait BinaryTreeDownWalker: NodeRefContainer {
fn getLeftChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getRightChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn setParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>);
fn setLeftChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setRightChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
}
pub trait BinaryTreeUpWalker: NodeRefContainer {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
}
pub unsafe trait BinaryTreeDownBuilder: NodeRefContainer {
fn xSetLeftChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearLeftChild(node),
Some(nn) => self.setLeftChild(node, nn),
}
}
fn xSetRightChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearLeftChild(node),
Some(nn) => self.setRightChild(node, nn),
}
}
fn setLeftChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef);
fn setRightChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef);
fn clearLeftChild(&mut self, node: Self::NodeRef);
fn clearRightChild(&mut self, node: Self::NodeRef);
}
pub unsafe trait BinaryTreeBuilder: BinaryTreeDownBuilder {
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);
}

40
src/directed/context.rs Normal file
View File

@ -0,0 +1,40 @@
use crate::NodeRefContainer;
pub trait DirectedBinaryTreeDownWalker: NodeRefContainer {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
}
pub unsafe trait DirectedBinaryTreeDownBuilder: NodeRefContainer {
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearForwardChild(node),
Some(nn) => self.setForwardChild(node, nn),
}
}
fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match newChild {
None => self.clearForwardChild(node),
Some(nn) => self.setOppositeChild(node, nn),
}
}
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef);
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef);
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

@ -0,0 +1,161 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker,
};
use crate::direction::BinaryTreeDirection;
use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
};
pub struct DynamicDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> {
ctx: &'ctx Ctx,
dir: BinaryTreeDirection,
}
pub struct DynamicDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> {
ctx: &'ctx mut Ctx,
dir: BinaryTreeDirection,
}
impl<'ctx, Ctx: NodeRefContainer> DynamicDirectedBinaryTreeWalker<'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> {
pub fn wrap(ctx: &'ctx mut Ctx, initialDirection: BinaryTreeDirection) -> Self {
return Self {
ctx,
dir: initialDirection,
};
}
pub fn changeDirection(&mut self, newDirection: BinaryTreeDirection) {
self.dir = newDirection
}
}
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeWalker<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeEditor<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeWalker<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for DynamicDirectedBinaryTreeEditor<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for DynamicDirectedBinaryTreeWalker<'_, 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: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for DynamicDirectedBinaryTreeEditor<'_, 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: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for DynamicDirectedBinaryTreeEditor<'_, 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: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for DynamicDirectedBinaryTreeEditor<'_, 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)
}
}

View File

@ -0,0 +1,221 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeBuilder, BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeUpWalker,
};
use crate::directed::context::{
DirectedBinaryTreeBuilder, DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker,
};
pub struct LeftDirectedBinaryTreeWalker<'ctx, Ctx: NodeRefContainer> {
ctx: &'ctx Ctx,
}
pub struct LeftDirectedBinaryTreeEditor<'ctx, Ctx: NodeRefContainer> {
ctx: &'ctx mut Ctx,
}
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> {
pub fn wrap(ctx: &'ctx Ctx) -> Self {
return Self { ctx };
}
}
impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeEditor<'ctx, Ctx> {
pub fn wrap(ctx: &'ctx mut Ctx) -> Self {
return Self { 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> {
pub fn wrap(ctx: &'ctx mut Ctx) -> Self {
return Self { ctx };
}
}
impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeWalker<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer for LeftDirectedBinaryTreeEditor<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeWalker<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer for RightDirectedBinaryTreeEditor<'_, Ctx> {
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeWalker<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for LeftDirectedBinaryTreeEditor<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeWalker<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeUpWalker> BinaryTreeUpWalker for RightDirectedBinaryTreeEditor<'_, Ctx> {
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for LeftDirectedBinaryTreeWalker<'_, Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node);
}
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for LeftDirectedBinaryTreeEditor<'_, Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node);
}
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for RightDirectedBinaryTreeWalker<'_, Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node);
}
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node);
}
}
impl<Ctx: BinaryTreeDownWalker> DirectedBinaryTreeDownWalker
for RightDirectedBinaryTreeEditor<'_, Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getRightChild(node);
}
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getLeftChild(node);
}
}
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for LeftDirectedBinaryTreeEditor<'_, Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetLeftChild(node, newChild)
}
fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetRightChild(node, newChild)
}
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
self.ctx.setLeftChild(node, newChild)
}
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
self.ctx.setRightChild(node, newChild)
}
fn clearForwardChild(&mut self, node: Self::NodeRef) {
self.ctx.clearLeftChild(node)
}
fn clearOppositeChild(&mut self, node: Self::NodeRef) {
self.ctx.clearRightChild(node)
}
}
unsafe impl<Ctx: BinaryTreeDownBuilder> DirectedBinaryTreeDownBuilder
for RightDirectedBinaryTreeEditor<'_, Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetRightChild(node, newChild)
}
fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
self.ctx.xSetLeftChild(node, newChild)
}
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
self.ctx.setRightChild(node, newChild)
}
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) {
self.ctx.setLeftChild(node, newChild)
}
fn clearForwardChild(&mut self, node: Self::NodeRef) {
self.ctx.clearRightChild(node)
}
fn clearOppositeChild(&mut self, node: Self::NodeRef) {
self.ctx.clearLeftChild(node)
}
}
unsafe impl<Ctx: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for LeftDirectedBinaryTreeEditor<'_, 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: BinaryTreeBuilder> DirectedBinaryTreeBuilder
for RightDirectedBinaryTreeEditor<'_, 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)
}
}

View File

@ -0,0 +1,2 @@
mod fixed_wrapper;
mod dynamic_wrapper;

2
src/directed/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod context;
mod impls;

4
src/direction.rs Normal file
View File

@ -0,0 +1,4 @@
pub enum BinaryTreeDirection {
LEFT,
RIGHT,
}

View File

@ -1,2 +1,7 @@
mod context;
mod swap;
mod swap;
mod directed;
mod direction;
pub use context::NodeRefContainer;
pub use direction::BinaryTreeDirection;

View File

@ -1,25 +0,0 @@
pub trait OrientedNeighborSwapContext {
type NodeRef: Sized + Copy;
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn setParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>);
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setGrandparentToParent(&mut self, newParent: Self::NodeRef);
}
pub trait NeighbourSwapSupport {
fn leftChild_unknownParent(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_root(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_grandparentToLeft(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_grandparentToRight(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_unknownParent(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_root(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_grandparentToLeft(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_grandparentToRight(&self) -> impl OrientedNeighborSwapContext;
}