From 1f14f14e362e2dff99fae89a8196f6b971ce913f Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Mon, 29 Dec 2025 00:42:52 +0300 Subject: [PATCH] Stub of swap_context --- src/lib.rs | 1 + src/swap_context/context.rs | 236 ++++++++++++++++++++++++++++++++++++ src/swap_context/mod.rs | 1 + 3 files changed, 238 insertions(+) create mode 100644 src/swap_context/context.rs create mode 100644 src/swap_context/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 074eb55..f661324 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,4 @@ pub mod base_context; pub mod directed_context; pub mod parent2node_clojure; pub mod relation_context; +mod swap_context; diff --git a/src/swap_context/context.rs b/src/swap_context/context.rs new file mode 100644 index 0000000..8d77834 --- /dev/null +++ b/src/swap_context/context.rs @@ -0,0 +1,236 @@ +use crate::NodeRefContainer; +use crate::base_context::BinaryTreeDirection; + +pub unsafe trait BinaryTreeSwapContext: NodeRefContainer { + fn swapNeighbors( + &mut self, + grandparent: Option<(Self::NodeRef, BinaryTreeDirection)>, + parent: Self::NodeRef, + parent2child: BinaryTreeDirection, + child: Self::NodeRef, + ); + + fn swapNeighbors_root_dyn( + &mut self, + parent: Self::NodeRef, + parent2child: BinaryTreeDirection, + child: Self::NodeRef, + ) { + match parent2child { + BinaryTreeDirection::LEFT => self.swapNeighbors_root_left(parent, child), + BinaryTreeDirection::RIGHT => self.swapNeighbors_root_right(parent, child), + } + } + + fn swapNeighbors_left_dyn( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + parent2child: BinaryTreeDirection, + child: Self::NodeRef, + ) { + match parent2child { + BinaryTreeDirection::LEFT => self.swapNeighbors_left_left(grandparent, parent, child), + BinaryTreeDirection::RIGHT => self.swapNeighbors_left_right(grandparent, parent, child), + } + } + + fn swapNeighbors_right_dyn( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + parent2child: BinaryTreeDirection, + child: Self::NodeRef, + ) { + match parent2child { + BinaryTreeDirection::LEFT => self.swapNeighbors_right_left(grandparent, parent, child), + BinaryTreeDirection::RIGHT => { + self.swapNeighbors_right_right(grandparent, parent, child) + } + } + } + + fn swapNeighbors_dyn_left( + &mut self, + grandparent: Option<(Self::NodeRef, BinaryTreeDirection)>, + parent: Self::NodeRef, + child: Self::NodeRef, + ) { + match grandparent { + None => self.swapNeighbors_root_left(parent, child), + Some((g, BinaryTreeDirection::LEFT)) => self.swapNeighbors_left_left(g, parent, child), + Some((g, BinaryTreeDirection::RIGHT)) => { + self.swapNeighbors_right_left(g, parent, child) + } + } + } + + fn swapNeighbors_dyn_right( + &mut self, + grandparent: Option<(Self::NodeRef, BinaryTreeDirection)>, + parent: Self::NodeRef, + child: Self::NodeRef, + ) { + match grandparent { + None => self.swapNeighbors_root_right(parent, child), + Some((g, BinaryTreeDirection::LEFT)) => self.swapNeighbors_left_right(g, parent, child), + Some((g, BinaryTreeDirection::RIGHT)) => { + self.swapNeighbors_right_right(g, parent, child) + } + } + } + + fn swapNeighbors_root_left(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + + fn swapNeighbors_root_right(&mut self, parent: Self::NodeRef, child: Self::NodeRef); + + fn swapNeighbors_left_left( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + child: Self::NodeRef, + ); + + fn swapNeighbors_left_right( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + child: Self::NodeRef, + ); + + fn swapNeighbors_right_left( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + child: Self::NodeRef, + ); + + fn swapNeighbors_right_right( + &mut self, + grandparent: Self::NodeRef, + parent: Self::NodeRef, + child: Self::NodeRef, + ); + + fn swapDistant_dyn_left( + &mut self, + parent1: Option<(Self::NodeRef, BinaryTreeDirection)>, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ) { + match parent1 { + None => self.swapDistant_root_left(node1, parent2, node2), + Some((g, BinaryTreeDirection::LEFT)) => { + self.swapDistant_left_left(g, node1, parent2, node2) + } + Some((g, BinaryTreeDirection::RIGHT)) => { + self.swapDistant_right_left(g, node1, parent2, node2) + } + } + } + + fn swapDistant_dyn_right( + &mut self, + parent1: Option<(Self::NodeRef, BinaryTreeDirection)>, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ) { + match parent1 { + None => self.swapDistant_root_right(node1, parent2, node2), + Some((g, BinaryTreeDirection::LEFT)) => { + self.swapDistant_left_right(g, node1, parent2, node2) + } + Some((g, BinaryTreeDirection::RIGHT)) => { + self.swapDistant_right_right(g, node1, parent2, node2) + } + } + } + + fn swapDistant_root_dyn( + &mut self, + node1: Self::NodeRef, + parent2: (Self::NodeRef, BinaryTreeDirection), + node2: Self::NodeRef, + ) { + match parent2.1 { + BinaryTreeDirection::LEFT => self.swapDistant_root_left(node1, parent2.0, node2), + BinaryTreeDirection::RIGHT => self.swapDistant_root_right(node1, parent2.0, node2), + } + } + + fn swapDistant_left_dyn( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: (Self::NodeRef, BinaryTreeDirection), + node2: Self::NodeRef, + ) { + match parent2.1 { + BinaryTreeDirection::LEFT => { + self.swapDistant_left_left(parent1, node1, parent2.0, node2) + } + BinaryTreeDirection::RIGHT => { + self.swapDistant_left_right(parent1, node1, parent2.0, node2) + } + } + } + fn swapDistant_right_dyn( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: (Self::NodeRef, BinaryTreeDirection), + node2: Self::NodeRef, + ) { + match parent2.1 { + BinaryTreeDirection::LEFT => { + self.swapDistant_right_left(parent1, node1, parent2.0, node2) + } + BinaryTreeDirection::RIGHT => { + self.swapDistant_right_right(parent1, node1, parent2.0, node2) + } + } + } + + fn swapDistant_root_left( + &mut self, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); + fn swapDistant_root_right( + &mut self, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); + fn swapDistant_left_left( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); + fn swapDistant_left_right( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); + fn swapDistant_right_left( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); + fn swapDistant_right_right( + &mut self, + parent1: Self::NodeRef, + node1: Self::NodeRef, + parent2: Self::NodeRef, + node2: Self::NodeRef, + ); +} diff --git a/src/swap_context/mod.rs b/src/swap_context/mod.rs new file mode 100644 index 0000000..019f068 --- /dev/null +++ b/src/swap_context/mod.rs @@ -0,0 +1 @@ +mod context; \ No newline at end of file