diff --git a/Cargo.toml b/Cargo.toml index e38a18e..a8d378f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ version = "0.0.0" edition = "2024" [lints] -rust = { nonstandard_style = "allow" } \ No newline at end of file +rust = { nonstandard_style = "allow", unsafe_op_in_unsafe_fn = "allow" } \ No newline at end of file diff --git a/src/algos/mod.rs b/src/algos/mod.rs index 7245ff7..62b3b87 100644 --- a/src/algos/mod.rs +++ b/src/algos/mod.rs @@ -1,2 +1,3 @@ mod go_down; mod swap; +mod set_relation; diff --git a/src/algos/set_relation.rs b/src/algos/set_relation.rs new file mode 100644 index 0000000..aad6cd6 --- /dev/null +++ b/src/algos/set_relation.rs @@ -0,0 +1,24 @@ +use crate::context::{BinaryTreeParentSetterContext, NodeRef}; +use crate::parent2node::clojure::{Parent2NodeSetterClojure, ParentProviderClojure}; + +pub unsafe fn setRelation( + parent: &mut ( + impl Parent2NodeSetterClojure + ParentProviderClojure + ), + child: NodeRef, +) { + parent.setChild(child.clone()); + parent.setAsParentOf(child); +} + +pub unsafe fn xSetRelation( + parent: &mut ( + impl Parent2NodeSetterClojure + ParentProviderClojure + ), + child: Option, +) { + match child { + None => parent.clearChild(), + Some(c) => setRelation(parent, c), + } +} diff --git a/src/algos/swap/distant.rs b/src/algos/swap/distant.rs new file mode 100644 index 0000000..9eb34fe --- /dev/null +++ b/src/algos/swap/distant.rs @@ -0,0 +1,83 @@ +use crate::algos::set_relation::{setRelation, xSetRelation}; +use crate::context::BinaryTreeParentSetterContext; +use crate::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, + FixedForwardParent2NodeGetterClojureFromDirectedContext as P2N_F, + FixedOppositeParent2NodeGetterClojureFromDirectedContext as P2N_O, +}; + +unsafe fn swapDistant< + Ctx: BinaryTreeChildrenGetterContext + + BinaryTreeChildrenSetterContext + + BinaryTreeParentGetterContext + + BinaryTreeParentSetterContext, +>( + ctx: &mut Ctx, + parent1: &mut ( + impl Parent2NodeSetterClojure + + ParentProviderClojure + ), + node1: Ctx::NodeRef, + parent2: &mut ( + impl Parent2NodeSetterClojure + + ParentProviderClojure + ), + node2: Ctx::NodeRef, +) { + setRelation(parent1, node2.clone()); + setRelation(parent2, node1.clone()); + + { + let child1 = ctx.getLeftChild(node1.clone()); + let child2 = ctx.getLeftChild(node2.clone()); + xSetRelation(&mut P2N_L::wrap_mut(ctx, node2.clone()), child1); + xSetRelation(&mut P2N_L::wrap_mut(ctx, node1.clone()), child2); + } + { + let child1 = ctx.getRightChild(node1.clone()); + let child2 = ctx.getRightChild(node2.clone()); + xSetRelation(&mut P2N_R::wrap_mut(ctx, node2.clone()), child1); + xSetRelation(&mut P2N_R::wrap_mut(ctx, node1.clone()), child2); + } +} + +unsafe fn swapDistantDirected< + Ctx: DirectedBinaryTreeChildrenGetterContext + + DirectedBinaryTreeChildrenSetterContext + + BinaryTreeParentGetterContext + + BinaryTreeParentSetterContext, +>( + ctx: &mut Ctx, + parent1: &mut ( + impl Parent2NodeSetterClojure + + ParentProviderClojure + ), + node1: Ctx::NodeRef, + parent2: &mut ( + impl Parent2NodeSetterClojure + + ParentProviderClojure + ), + node2: Ctx::NodeRef, +) { + setRelation(parent1, node2.clone()); + setRelation(parent2, node1.clone()); + + { + let child1 = ctx.getForwardChild(node1.clone()); + let child2 = ctx.getForwardChild(node2.clone()); + xSetRelation(&mut P2N_F::wrap_mut(ctx, node2.clone()), child1); + xSetRelation(&mut P2N_F::wrap_mut(ctx, node1.clone()), child2); + } + { + let child1 = ctx.getOppositeChild(node1.clone()); + let child2 = ctx.getOppositeChild(node2.clone()); + xSetRelation(&mut P2N_O::wrap_mut(ctx, node2.clone()), child1); + xSetRelation(&mut P2N_O::wrap_mut(ctx, node1.clone()), child2); + } +} diff --git a/src/algos/swap/mod.rs b/src/algos/swap/mod.rs index 22cc63f..3a2f2f5 100644 --- a/src/algos/swap/mod.rs +++ b/src/algos/swap/mod.rs @@ -1 +1,2 @@ -mod neighbors; \ No newline at end of file +mod neighbors; +mod distant; \ No newline at end of file