From e6fb9bf0617ee42be57b55dbd7006f533653cf37 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Mon, 29 Dec 2025 21:25:29 +0300 Subject: [PATCH] Cleaning up 'add' algorithm, naming and publishing --- src/algo/add.kt | 79 ------------------------- src/algo/add.rs | 153 +++++++++++++++--------------------------------- src/algo/mod.rs | 2 +- src/lib.rs | 4 +- 4 files changed, 49 insertions(+), 189 deletions(-) delete mode 100644 src/algo/add.kt diff --git a/src/algo/add.kt b/src/algo/add.kt deleted file mode 100644 index bf87ec0..0000000 --- a/src/algo/add.kt +++ /dev/null @@ -1,79 +0,0 @@ - fun balanceAfterLinking(node: NODE) { - this._setColor(node, Color.RED) - var current: NODE = node - - while (true) { - val parent = this._getParent(current) - if (parent == null) { - current.__assertIsRoot() - this._setColor(current, Color.BLACK) - break - } - if (this._getColor(parent) == Color.BLACK) { - break - } - - val grandParent = this._getParent(parent) - if (grandParent == null) { - parent.__assertIsRoot() - this._setColor(parent, Color.BLACK) - break - } - - when { - this._checkSame(parent, this._getLeftChild(grandParent)) -> { - val uncle = this._getRightChild(grandParent) - if (uncle != null && this._getColor(uncle) == Color.RED) { - this._setColor(parent, Color.BLACK) - this._setColor(uncle, Color.BLACK) - this._setColor(grandParent, Color.RED) - current = grandParent - continue - } - - when { - this._checkSame(current, this._getLeftChild(parent)) -> { - this.__rotateRight_Switch(grandParent) - continue - } - - this._checkSame(current, this._getRightChild(parent)) -> { - this.__rotateLeft_Left(parent, grandParent) - current = parent - continue - } - - else -> this.__throwTreeCorruptedNotChild(current, parent) - } - } - - this._checkSame(parent, this._getRightChild(grandParent)) -> { - val uncle = this._getLeftChild(grandParent) - if (uncle != null && this._getColor(uncle) == Color.RED) { - this._setColor(parent, Color.BLACK) - this._setColor(uncle, Color.BLACK) - this._setColor(grandParent, Color.RED) - current = grandParent - continue - } - - when { - this._checkSame(current, this._getRightChild(parent)) -> { - this.__rotateLeft_Switch(grandParent) - continue - } - - this._checkSame(current, this._getLeftChild(parent)) -> { - this.__rotateRight_Right(parent, grandParent) - current = parent - continue - } - - else -> this.__throwTreeCorruptedNotChild(current, parent) - } - } - - else -> this.__throwTreeCorruptedNotChild(parent, grandParent) - } - } - } \ No newline at end of file diff --git a/src/algo/add.rs b/src/algo/add.rs index b4b098e..78053fa 100644 --- a/src/algo/add.rs +++ b/src/algo/add.rs @@ -1,5 +1,4 @@ use crate::context::{Color, RedBlackTreeColorContext}; -use binary_tree_core_0::NodeRefContainer; use binary_tree_core_0::base_context::{ BinaryTreeChildrenGetterContext, BinaryTreeDirection, BinaryTreeDirectionDispatcherClojure, BinaryTreeRootGetter, @@ -10,109 +9,27 @@ use binary_tree_core_0::directed_context::{ use binary_tree_core_0::relation_context::{ BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, }; -use std::mem::MaybeUninit; -use std::ptr::NonNull; -pub trait AddRecContext: +pub trait RedBlackTreeAddingContext: BinaryTreeRootGetter - + BinaryTreeChildrenGetterContext + BinaryTreeRootRelationSetterContext + BinaryTreeRelationSetterContext - + BinaryTreeDirectionDispatcherClojure + RedBlackTreeColorContext { + fn getLinkingContext( + &mut self, + ) -> impl RedBlackTreeAddingContext_Linking; + fn getBalancingContext( + &mut self, + ) -> impl RedBlackTreeAddingContext_Balancing; } -struct Frame { +pub struct ParentInfo { node: NodeRef, dir: BinaryTreeDirection, } -struct Stack { - start: NonNull, - ptr: NonNull, -} - -impl Stack { - fn init(buffer: &mut [MaybeUninit; SIZE]) -> Self { - unsafe { - let start = NonNull::new_unchecked( - ((*buffer.as_mut_ptr()).as_mut_ptr()) - .offset(const { 0isize.strict_add_unsigned(SIZE) }), - ); - return Self { start, ptr: start }; - } - } - - fn push(&mut self, value: T) { - unsafe { - let newPtr = self.ptr.offset(-1); - newPtr.as_ptr().write(value); - self.ptr = newPtr; - } - } - - fn _get(&self) -> &T { - unsafe { - let loc = self - .ptr - .offset(const { 0isize.strict_add_unsigned(OFFSET) }); - return loc.as_ref(); - } - } - fn getParent(&self) -> &T { - return self._get::<0>(); - } - - fn getGrandParent(&self) -> &T { - return self._get::<1>(); - } - - fn getGrandGrandParent(&self) -> Option<&T> { - match self.hasGrandGrandParent() { - false => None, - true => Some(self._get::<2>()), - } - } - - fn pop(&mut self) -> T { - unsafe { - let newPtr = self.ptr.offset(1); - let value = self.ptr.read(); - self.ptr = newPtr; - return value; - } - } - - fn popDiscard(&mut self) { - unsafe { - self.ptr = self.ptr.offset(1); - } - } - fn replaceTop(&mut self, value: T) { - unsafe { - self.ptr.write(value); - } - } - - fn _has(&self) -> bool { - unsafe { - return self - .ptr - .offset(const { 0isize.strict_add_unsigned(OFFSET) }) - < self.start; - } - } - - fn hasGrandParent(&self) -> bool { - return self._has::<1>(); - } - fn hasGrandGrandParent(&self) -> bool { - return self._has::<2>(); - } -} - -pub fn add_recursive(ctx: &mut Ctx, newNode: Ctx::NodeRef) { +pub fn add(ctx: &mut Ctx, newNode: Ctx::NodeRef) { ctx.clearLeftChild(newNode.clone()); ctx.clearRightChild(newNode.clone()); ctx.setColor(newNode.clone(), Color::RED); @@ -125,13 +42,13 @@ pub fn add_recursive(ctx: &mut Ctx, newNode: Ctx::NodeRef) { return; } Some(root) => { - link(ctx, root, newNode); - balance(ctx); + _link(&mut ctx.getLinkingContext(), root, newNode); + _balance(&mut ctx.getBalancingContext()); } } } -trait LinkingContext: +pub trait RedBlackTreeAddingContext_Linking: BinaryTreeChildrenGetterContext + BinaryTreeRelationSetterContext + BinaryTreeDirectionDispatcherClojure @@ -139,7 +56,11 @@ trait LinkingContext: fn stackAddParentInfo(&mut self, node: Self::NodeRef, dir: BinaryTreeDirection); } -fn link(ctx: &mut Ctx, mut it: Ctx::NodeRef, newNode: Ctx::NodeRef) { +fn _link( + ctx: &mut Ctx, + mut it: Ctx::NodeRef, + newNode: Ctx::NodeRef, +) { loop { let dir = ctx .determineDirection(it.clone()) @@ -166,22 +87,25 @@ fn link(ctx: &mut Ctx, mut it: Ctx::NodeRef, newNode: Ctx:: } } -trait BalancingContext: RedBlackTreeColorContext { - fn getLeftDirectedContext(&mut self) -> impl DirectedBalancingContext; - fn getRightDirectedContext(&mut self) - -> impl DirectedBalancingContext; +pub trait RedBlackTreeAddingContext_Balancing: RedBlackTreeColorContext { + fn getLeftDirectedContext( + &mut self, + ) -> impl RedBlackTreeAddingContext_DirectedBalancing; + fn getRightDirectedContext( + &mut self, + ) -> impl RedBlackTreeAddingContext_DirectedBalancing; - fn stackPopParentInfo(&self) -> Option>; + fn stackPopParentInfo(&self) -> Option>; } -trait DirectedBalancingContext: +pub trait RedBlackTreeAddingContext_DirectedBalancing: RedBlackTreeColorContext + DirectedBinaryTreeChildrenGetterContext { fn rotateForward_Forward(&mut self, parent: Self::NodeRef, pivot: Self::NodeRef); fn rotateOpposite_GrandParent(&mut self, pivot: Self::NodeRef); } -fn balance(ctx: &mut Ctx) { +fn _balance(ctx: &mut Ctx) { loop { let parent; match ctx.stackPopParentInfo() { @@ -215,10 +139,10 @@ fn balance(ctx: &mut Ctx) { } } - fn _directed( + fn _directed( ctx: &mut Ctx, - grandparent: Frame, - parent: Frame, + grandparent: ParentInfo, + parent: ParentInfo, ) -> bool { let uncle = ctx.getOppositeChild(grandparent.node.clone()); if let Some(uncle) = uncle { @@ -233,7 +157,7 @@ fn balance(ctx: &mut Ctx) { let normalizedParent; if grandparent.dir != parent.dir { - normalizedParent = Frame { + normalizedParent = ParentInfo { node: ctx.getOppositeChild(parent.node.clone()).unwrap(), dir: grandparent.dir, }; @@ -250,3 +174,18 @@ fn balance(ctx: &mut Ctx) { } } } + +pub fn balanceAfterLinking< + Ctx: RedBlackTreeAddingContext_Balancing + BinaryTreeRelationSetterContext + BinaryTreeRootGetter, +>( + ctx: &mut Ctx, + newNode: Ctx::NodeRef, +) { + ctx.clearLeftChild(newNode.clone()); + ctx.clearRightChild(newNode.clone()); + ctx.setColor(newNode.clone(), Color::RED); + + _balance(ctx); + + ctx.setColor(ctx.getRoot().unwrap(), Color::BLACK) +} diff --git a/src/algo/mod.rs b/src/algo/mod.rs index b4128ae..71ae505 100644 --- a/src/algo/mod.rs +++ b/src/algo/mod.rs @@ -1 +1 @@ -mod add; \ No newline at end of file +pub mod add; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 68d7b63..1d27edf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -mod algo; -mod context; +pub mod algo; +pub mod context;