Clojure to pass info about local root's parent

This commit is contained in:
Andrew Golovashevich 2025-12-26 15:33:28 +03:00
parent 7c46a55374
commit e9caf277bf
5 changed files with 224 additions and 0 deletions

View File

@ -2,6 +2,16 @@ pub trait NodeRefContainer {
type NodeRef: Sized + Copy;
}
pub trait BinaryTreeRootGetter: NodeRefContainer {
fn getRoot(&self) -> Option<Self::NodeRef>;
}
pub trait BinaryTreeRootSetter: NodeRefContainer {
fn xSetRoot(&mut self, newRoot: Option<Self::NodeRef>);
fn setRoot(&mut self, newRoot: Self::NodeRef);
fn clearRoot(&mut self);
}
pub trait BinaryTreeDownWalker: NodeRefContainer {
fn getLeftChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getRightChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;

View File

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

View File

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

View File

@ -0,0 +1,193 @@
use crate::NodeRefContainer;
use crate::context::{
BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeRootGetter, BinaryTreeRootSetter,
};
use crate::parent2node::clojure::{Parent2NodeGetter, Parent2NodeSetter};
struct Parent2LeftChildContextWrapper<CtxRef, NodeRef> {
ctx: CtxRef,
node: NodeRef,
}
struct Parent2RightChildContextWrapper<CtxRef, NodeRef> {
ctx: CtxRef,
node: NodeRef,
}
struct Root2NodeContextWrapper<NodeRef> {
ctx: NodeRef,
}
impl<'ctx, CtxRef: NodeRefContainer> Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> {
fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer + BinaryTreeRootGetter> Root2NodeContextWrapper<&'ctx CtxRef> {
fn wrap(ctx: &'ctx CtxRef) -> Self {
return Self { ctx };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Root2NodeContextWrapper<&'ctx mut CtxRef>
{
fn wrap(ctx: &'ctx mut CtxRef) -> Self {
return Self { ctx };
}
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer for Root2NodeContextWrapper<&'ctx CtxRef> {
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Root2NodeContextWrapper<&'ctx mut CtxRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getLeftChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getLeftChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
for Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getRightChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getRightChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
for Root2NodeContextWrapper<&'ctx CtxRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getRoot();
}
}
impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
for Root2NodeContextWrapper<&'ctx mut CtxRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getRoot();
}
}
impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
self.ctx.xSetLeftChild(self.node, newChild);
}
fn setChild(&mut self, newChild: Self::NodeRef) {
self.ctx.setLeftChild(self.node, newChild);
}
fn clearChild(&mut self) {
self.ctx.clearLeftChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
self.ctx.xSetRightChild(self.node, newChild);
}
fn setChild(&mut self, newChild: Self::NodeRef) {
self.ctx.setRightChild(self.node, newChild);
}
fn clearChild(&mut self) {
self.ctx.clearRightChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetter
for Root2NodeContextWrapper<&'ctx mut CtxRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
self.ctx.xSetRoot(newChild)
}
fn setChild(&mut self, newChild: Self::NodeRef) {
self.ctx.setRoot(newChild)
}
fn clearChild(&mut self) {
self.ctx.clearRoot()
}
}

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

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