Public mod structure and related minor fixes

This commit is contained in:
Andrew Golovashevich 2025-12-28 01:39:05 +03:00
parent 6e33460e63
commit bde59410d5
22 changed files with 149 additions and 126 deletions

View File

@ -1,9 +1,7 @@
use crate::BinaryTreeDirection;
use crate::context::BinaryTreeChildrenGetterContext;
use crate::directed::DirectedBinaryTreeChildrenGetterContext;
use crate::direction::DirectedBinaryTreeDirection;
use crate::base_context::{BinaryTreeChildrenGetterContext, BinaryTreeDirection};
use crate::directed_context::{DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeDirection};
fn goDown<Ctx: BinaryTreeChildrenGetterContext>(
pub fn goDown<Ctx: BinaryTreeChildrenGetterContext>(
ctx: &Ctx,
root: Ctx::NodeRef,
dir: BinaryTreeDirection,
@ -26,7 +24,7 @@ fn goDown<Ctx: BinaryTreeChildrenGetterContext>(
}
}
fn goDownDirected<Ctx: DirectedBinaryTreeChildrenGetterContext>(
pub fn goDownDirected<Ctx: DirectedBinaryTreeChildrenGetterContext>(
ctx: &Ctx,
root: Ctx::NodeRef,
dir: DirectedBinaryTreeDirection,

7
src/algo/mod.rs Normal file
View File

@ -0,0 +1,7 @@
mod go_down;
mod set_relation;
mod swap;
pub use go_down::*;
pub use set_relation::*;
pub use swap::*;

View File

@ -1,7 +1,6 @@
use crate::context::{BinaryTreeParentSetterContext, NodeRef};
use crate::parent2node::clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
use crate::parent2node_clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
pub unsafe fn setRelation<NodeRef: crate::context::NodeRef>(
pub unsafe fn setRelation<NodeRef: crate::NodeRef>(
parent: &mut (
impl Parent2NodeSetterClojure<NodeRef = NodeRef> + ParentProviderClojure<NodeRef = NodeRef>
),
@ -11,7 +10,7 @@ pub unsafe fn setRelation<NodeRef: crate::context::NodeRef>(
parent.setAsParentOf(child);
}
pub unsafe fn xSetRelation<NodeRef: crate::context::NodeRef>(
pub unsafe fn xSetRelation<NodeRef: crate::NodeRef>(
parent: &mut (
impl Parent2NodeSetterClojure<NodeRef = NodeRef> + ParentProviderClojure<NodeRef = NodeRef>
),

View File

@ -1,18 +1,21 @@
use crate::algos::set_relation::{setRelation, xSetRelation};
use crate::context::BinaryTreeParentSetterContext;
use crate::context::{
use crate::algo::set_relation::{setRelation, xSetRelation};
use crate::base_context::BinaryTreeParentSetterContext;
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeParentGetterContext,
};
use crate::directed::{DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext};
use crate::parent2node::clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
use crate::parent2node::fixed_wrapper::{
FixedLeftParent2NodeGetterClojureFromContext as P2N_L,
FixedRightParent2NodeGetterClojureFromContext as P2N_R,
use crate::directed_context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
};
use crate::parent2node_clojure::{
FixedForwardParent2NodeGetterClojureFromDirectedContext as P2N_F,
FixedLeftParent2NodeGetterClojureFromContext as P2N_L,
FixedOppositeParent2NodeGetterClojureFromDirectedContext as P2N_O,
FixedRightParent2NodeGetterClojureFromContext as P2N_R,
Parent2NodeSetterClojure,
ParentProviderClojure,
};
unsafe fn swapDistant<
pub unsafe fn swapDistant<
Ctx: BinaryTreeChildrenGetterContext
+ BinaryTreeChildrenSetterContext
+ BinaryTreeParentGetterContext
@ -47,7 +50,7 @@ unsafe fn swapDistant<
}
}
unsafe fn swapDistantDirected<
pub unsafe fn swapDistantDirected<
Ctx: DirectedBinaryTreeChildrenGetterContext
+ DirectedBinaryTreeChildrenSetterContext
+ BinaryTreeParentGetterContext

5
src/algo/swap/mod.rs Normal file
View File

@ -0,0 +1,5 @@
mod neighbors;
mod distant;
pub use distant::*;
pub use neighbors::*;

View File

@ -1,10 +1,10 @@
use crate::context::BinaryTreeParentGetterContext;
use crate::context::BinaryTreeParentSetterContext;
use crate::directed::DirectedBinaryTreeChildrenGetterContext;
use crate::directed::DirectedBinaryTreeChildrenSetterContext;
use crate::parent2node::clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
use crate::base_context::BinaryTreeParentGetterContext;
use crate::base_context::BinaryTreeParentSetterContext;
use crate::directed_context::DirectedBinaryTreeChildrenGetterContext;
use crate::directed_context::DirectedBinaryTreeChildrenSetterContext;
use crate::parent2node_clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
unsafe fn swapNeighbors<
pub unsafe fn swapNeighbors<
Ctx: DirectedBinaryTreeChildrenGetterContext
+ DirectedBinaryTreeChildrenSetterContext
+ BinaryTreeParentGetterContext

View File

@ -1,3 +0,0 @@
mod go_down;
mod swap;
mod set_relation;

View File

@ -1,2 +0,0 @@
mod neighbors;
mod distant;

View File

@ -1,7 +1,8 @@
pub trait NodeRef: Sized + Clone {}
use crate::NodeRefContainer;
pub trait NodeRefContainer {
type NodeRef: NodeRef;
pub enum BinaryTreeDirection {
LEFT,
RIGHT,
}
pub trait BinaryTreeRootGetter: NodeRefContainer {

View File

@ -1,6 +0,0 @@
mod context;
mod fixed_wrapper;
mod dynamic_wrapper;
pub use context::DirectedBinaryTreeChildrenSetterContext;
pub use context::DirectedBinaryTreeChildrenGetterContext;

View File

@ -1,5 +1,10 @@
use crate::NodeRefContainer;
pub enum DirectedBinaryTreeDirection {
FORWARD,
OPPOSITE,
}
pub trait DirectedBinaryTreeChildrenGetterContext: NodeRefContainer {
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;

View File

@ -1,9 +1,11 @@
use crate::NodeRefContainer;
use crate::context::{BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeParentSetterContext, BinaryTreeParentGetterContext};
use crate::directed::context::{
DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext,
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeDirection,
BinaryTreeParentGetterContext, BinaryTreeParentSetterContext,
};
use crate::directed_context::context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
};
use crate::direction::BinaryTreeDirection;
pub struct DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
ctx: CtxRef,
@ -38,17 +40,23 @@ impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeContex
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> {
impl<Ctx: NodeRefContainer> NodeRefContainer
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
type NodeRef = Ctx::NodeRef;
}
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeContextFromContext<&Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}

View File

@ -1,9 +1,9 @@
use crate::NodeRefContainer;
use crate::context::{
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext,
BinaryTreeParentGetterContext, BinaryTreeParentSetterContext,
};
use crate::directed::context::{
use crate::directed_context::context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
};

View File

@ -0,0 +1,7 @@
mod context;
mod fixed_wrapper;
mod dynamic_wrapper;
pub use context::*;
pub use fixed_wrapper::*;
pub use dynamic_wrapper::*;

View File

@ -1,10 +0,0 @@
#[derive(PartialEq, Eq)]
pub enum BinaryTreeDirection {
LEFT,
RIGHT,
}
#[derive(PartialEq, Eq)]
pub enum DirectedBinaryTreeDirection {
FORWARD,
OPPOSITE,
}

View File

@ -1,8 +1,7 @@
mod context;
mod directed;
mod direction;
mod parent2node;
mod algos;
mod misc;
pub use context::NodeRefContainer;
pub use direction::BinaryTreeDirection;
pub use misc::*;
pub mod algo;
pub mod base_context;
pub mod directed_context;
pub mod parent2node_clojure;

5
src/misc.rs Normal file
View File

@ -0,0 +1,5 @@
pub trait NodeRef: Sized + Clone {}
pub trait NodeRefContainer {
type NodeRef: NodeRef;
}

View File

@ -1,3 +0,0 @@
pub mod clojure;
pub mod fixed_wrapper;
pub mod dynamic_wrappers;

View File

@ -1,14 +1,14 @@
use super::clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
use crate::BinaryTreeDirection;
use super::{Parent2NodeSetterClojure, ParentProviderClojure};
use crate::NodeRefContainer;
use crate::context::{BinaryTreeParentSetterContext, BinaryTreeRootGetter};
use crate::context::BinaryTreeRootSetter;
use crate::context::{BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext};
use crate::directed::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeDirection,
BinaryTreeParentSetterContext, BinaryTreeRootGetter, BinaryTreeRootSetter,
};
use crate::direction::DirectedBinaryTreeDirection;
use crate::parent2node::clojure::Parent2NodeGetterClojure;
use crate::directed_context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
DirectedBinaryTreeDirection,
};
use crate::parent2node_clojure::clojure::Parent2NodeGetterClojure;
use std::mem::ManuallyDrop;
use std::ops::Deref;
@ -24,13 +24,13 @@ union _NodeOrNothing<NodeRef> {
node: ManuallyDrop<NodeRef>,
}
struct DynamicParent2NodeGetterClojureFromContext<CtxRef, NodeRef> {
pub struct DynamicParent2NodeGetterClojureFromContext<CtxRef, NodeRef> {
dir: Option<BinaryTreeDirection>,
ctx: CtxRef,
node: _NodeOrNothing<NodeRef>,
}
struct DynamicParent2NodeGetterClojureFromDirectedContext<CtxRef, NodeRef> {
pub struct DynamicParent2NodeGetterClojureFromDirectedContext<CtxRef, NodeRef> {
dir: Option<DirectedBinaryTreeDirection>,
ctx: CtxRef,
node: _NodeOrNothing<NodeRef>,
@ -38,47 +38,47 @@ struct DynamicParent2NodeGetterClojureFromDirectedContext<CtxRef, NodeRef> {
macro_rules! _constructor {
($name:ident $direction:ident) => {
impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx Ctx, Ctx::NodeRef> {
pub fn wrapNode(
ctx: &'ctx Ctx, node: Ctx::NodeRef, direction: $direction
) -> Self {
impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx, Ctx::NodeRef> {
pub fn wrapNode(ctx: &'ctx Ctx, node: Ctx::NodeRef, direction: $direction) -> Self {
return Self {
dir: Some(direction),
ctx: ctx,
node: _NodeOrNothing { node: ManuallyDrop::new(node) }
node: _NodeOrNothing {
node: ManuallyDrop::new(node),
},
};
}
pub fn wrapRoot(
ctx: &'ctx Ctx
) -> Self {
pub fn wrapRoot(ctx: &'ctx Ctx) -> Self {
return Self {
dir: None,
ctx: ctx,
node: _NodeOrNothing { nothing: () }
node: _NodeOrNothing { nothing: () },
};
}
}
};
($name:ident $direction:ident mut) => {
impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx mut Ctx, Ctx::NodeRef> {
impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx, Ctx::NodeRef> {
pub fn wrapNode_mut(
ctx: &'ctx mut Ctx, node: Ctx::NodeRef, direction: $direction
ctx: &'ctx mut Ctx,
node: Ctx::NodeRef,
direction: $direction,
) -> Self {
return Self {
dir: Some(direction),
ctx: ctx,
node: _NodeOrNothing { node: ManuallyDrop::new(node) }
node: _NodeOrNothing {
node: ManuallyDrop::new(node),
},
};
}
pub fn wrapRoot_mut(
ctx: &'ctx mut Ctx
) -> Self {
pub fn wrapRoot_mut(ctx: &'ctx mut Ctx) -> Self {
return Self {
dir: None,
ctx: ctx,
node: _NodeOrNothing { nothing: () }
node: _NodeOrNothing { nothing: () },
};
}
}
@ -86,7 +86,9 @@ macro_rules! _constructor {
}
_mut_switch!(_constructor!(DynamicParent2NodeGetterClojureFromContext BinaryTreeDirection));
_mut_switch!(_constructor!(DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection));
_mut_switch!(
_constructor!(DynamicParent2NodeGetterClojureFromDirectedContext DirectedBinaryTreeDirection)
);
macro_rules! _node_ref {
($name:ident $($mut:tt)?) => {
@ -97,7 +99,9 @@ macro_rules! _node_ref {
}
_mut_switch!(_node_ref!(DynamicParent2NodeGetterClojureFromContext));
_mut_switch!(_node_ref!(DynamicParent2NodeGetterClojureFromDirectedContext));
_mut_switch!(_node_ref!(
DynamicParent2NodeGetterClojureFromDirectedContext
));
macro_rules! _get_child {
(
@ -141,21 +145,20 @@ macro_rules! _set_child {
$d1_lbl:ident $d1_xset:ident $d1_set:ident $d1_clear:ident
$d2_lbl:ident $d2_xset:ident $d2_set:ident $d2_clear:ident
) => {
impl<
'ctx,
Ctx: NodeRefContainer + BinaryTreeRootSetter + $ctx_constraint
> Parent2NodeSetterClojure
for $name <&'ctx mut Ctx, Ctx::NodeRef>
impl<'ctx, Ctx: NodeRefContainer + BinaryTreeRootSetter + $ctx_constraint>
Parent2NodeSetterClojure for $name<&'ctx mut Ctx, Ctx::NodeRef>
{
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
match self.dir {
match self.dir {
None => self.ctx.xSetRoot(newChild),
Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_xset ((*self.node.node.deref()).clone(), newChild)
self.ctx
.$d1_xset((*self.node.node.deref()).clone(), newChild)
},
Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_xset ((*self.node.node.deref()).clone(), newChild)
}
self.ctx
.$d2_xset((*self.node.node.deref()).clone(), newChild)
},
};
}
@ -163,11 +166,13 @@ macro_rules! _set_child {
match self.dir {
None => self.ctx.setRoot(newChild),
Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_set ((*self.node.node.deref()).clone(), newChild)
self.ctx
.$d1_set((*self.node.node.deref()).clone(), newChild)
},
Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_set ((*self.node.node.deref()).clone(), newChild)
}
self.ctx
.$d2_set((*self.node.node.deref()).clone(), newChild)
},
};
}
@ -175,11 +180,11 @@ macro_rules! _set_child {
match self.dir {
None => self.ctx.clearRoot(),
Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_clear ((*self.node.node.deref()).clone())
self.ctx.$d1_clear((*self.node.node.deref()).clone())
},
Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_clear((*self.node.node.deref()).clone())
},
Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_clear ((*self.node.node.deref()).clone())
}
};
}
}
@ -199,7 +204,6 @@ _set_child!(
OPPOSITE xSetOppositeChild setOppositeChild clearOppositeChild
);
macro_rules! _set_as_parent_of {
($name:ident $dir_enum_lbl:ident $d1_lbl:ident $d2_lbl:ident) => {
impl<'ctx, Ctx: BinaryTreeParentSetterContext> ParentProviderClojure
@ -209,11 +213,11 @@ macro_rules! _set_as_parent_of {
match self.dir {
None => self.ctx.clearParent(child),
Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.setParent(child, (*self.node.node.deref()).clone())
self.ctx.setParent(child, (*self.node.node.deref()).clone())
},
Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.setParent(child, (*self.node.node.deref()).clone())
}
self.ctx.setParent(child, (*self.node.node.deref()).clone())
},
};
}
}
@ -243,4 +247,3 @@ macro_rules! _drop {
_drop!(DynamicParent2NodeGetterClojureFromContext);
_drop!(DynamicParent2NodeGetterClojureFromDirectedContext);

View File

@ -1,12 +1,12 @@
use crate::NodeRefContainer;
use crate::context::{
use crate::base_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext,
BinaryTreeParentSetterContext, BinaryTreeRootGetter, BinaryTreeRootSetter,
};
use crate::directed::{
use crate::directed_context::{
DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext,
};
use crate::parent2node::clojure::{
use crate::parent2node_clojure::clojure::{
Parent2NodeGetterClojure, Parent2NodeSetterClojure, ParentProviderClojure,
};
@ -55,13 +55,13 @@ macro_rules! _constructor {
impl<'ctx, Ctx: NodeRefContainer + BinaryTreeRootGetter>
FixedRootParent2NodeGetterClojureFromContext<&'ctx Ctx>
{
fn wrap(ctx: &'ctx Ctx) -> Self {
pub fn wrap(ctx: &'ctx Ctx) -> Self {
return Self { ctx };
}
}
impl<'ctx, Ctx: NodeRefContainer> FixedRootParent2NodeGetterClojureFromContext<&'ctx mut Ctx> {
fn wrap_mut(ctx: &'ctx mut Ctx) -> Self {
pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self {
return Self { ctx };
}
}

View File

@ -0,0 +1,7 @@
mod clojure;
mod dynamic_wrappers;
mod fixed_wrapper;
pub use clojure::*;
pub use dynamic_wrappers::*;
pub use fixed_wrapper::*;