goDown and swapNeighbors
This commit is contained in:
parent
5949fc8cd7
commit
fbe424c276
50
src/algos/go_down.rs
Normal file
50
src/algos/go_down.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use crate::BinaryTreeDirection;
|
||||||
|
use crate::context::BinaryTreeChildrenGetterContext;
|
||||||
|
use crate::directed::DirectedBinaryTreeChildrenGetterContext;
|
||||||
|
use crate::direction::DirectedBinaryTreeDirection;
|
||||||
|
|
||||||
|
fn goDown<Ctx: BinaryTreeChildrenGetterContext>(
|
||||||
|
ctx: &Ctx,
|
||||||
|
root: Ctx::NodeRef,
|
||||||
|
dir: BinaryTreeDirection,
|
||||||
|
) -> Ctx::NodeRef {
|
||||||
|
let mut p = root;
|
||||||
|
match dir {
|
||||||
|
BinaryTreeDirection::LEFT => loop {
|
||||||
|
match ctx.getLeftChild(p) {
|
||||||
|
None => return p,
|
||||||
|
Some(c) => p = c,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
BinaryTreeDirection::RIGHT => loop {
|
||||||
|
match ctx.getRightChild(p) {
|
||||||
|
None => return p,
|
||||||
|
Some(c) => p = c,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn goDownDirected<Ctx: DirectedBinaryTreeChildrenGetterContext>(
|
||||||
|
ctx: &Ctx,
|
||||||
|
root: Ctx::NodeRef,
|
||||||
|
dir: DirectedBinaryTreeDirection,
|
||||||
|
) -> Ctx::NodeRef {
|
||||||
|
let mut p = root;
|
||||||
|
match dir {
|
||||||
|
DirectedBinaryTreeDirection::FORWARD => loop {
|
||||||
|
match ctx.getForwardChild(p) {
|
||||||
|
None => return p,
|
||||||
|
Some(c) => p = c,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
DirectedBinaryTreeDirection::OPPOSITE => loop {
|
||||||
|
match ctx.getOppositeChild(p) {
|
||||||
|
None => return p,
|
||||||
|
Some(c) => p = c,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/algos/mod.rs
Normal file
2
src/algos/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mod go_down;
|
||||||
|
mod swap;
|
||||||
38
src/algos/swap/neighbors.rs
Normal file
38
src/algos/swap/neighbors.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
use crate::context::BinaryTreeParentGetterContext;
|
||||||
|
use crate::context::BinaryTreeParentSetterContext;
|
||||||
|
use crate::directed::DirectedBinaryTreeChildrenGetterContext;
|
||||||
|
use crate::directed::DirectedBinaryTreeChildrenSetterContext;
|
||||||
|
use crate::parent2node::clojure::Parent2NodeSetterClojure;
|
||||||
|
|
||||||
|
unsafe fn swapNeighbors<
|
||||||
|
Ctx: DirectedBinaryTreeChildrenGetterContext
|
||||||
|
+ DirectedBinaryTreeChildrenSetterContext
|
||||||
|
+ BinaryTreeParentGetterContext
|
||||||
|
+ BinaryTreeParentSetterContext,
|
||||||
|
>(
|
||||||
|
ctx: &mut Ctx,
|
||||||
|
parentLocation: &mut impl Parent2NodeSetterClojure<NodeRef=Ctx::NodeRef>,
|
||||||
|
grandparent: Option<Ctx::NodeRef>,
|
||||||
|
parent: Ctx::NodeRef,
|
||||||
|
child: Ctx::NodeRef,
|
||||||
|
) {
|
||||||
|
ctx.xSetParent(child, grandparent);
|
||||||
|
parentLocation.setChild(child);
|
||||||
|
ctx.xSetForwardChild(parent, ctx.getForwardChild(child));
|
||||||
|
|
||||||
|
let oppositeChild = ctx.getOppositeChild(parent);
|
||||||
|
ctx.xSetOppositeChild(parent, ctx.getOppositeChild(child));
|
||||||
|
ctx.xSetOppositeChild(child, oppositeChild);
|
||||||
|
|
||||||
|
if let Some(n) = ctx.getOppositeChild(child) {
|
||||||
|
ctx.setParent(n, child)
|
||||||
|
}
|
||||||
|
if let Some(n) = ctx.getOppositeChild(parent) {
|
||||||
|
ctx.setParent(n, parent)
|
||||||
|
}
|
||||||
|
if let Some(n) = ctx.getForwardChild(child) {
|
||||||
|
ctx.setParent(n, parent)
|
||||||
|
}
|
||||||
|
ctx.setForwardChild(child, parent);
|
||||||
|
ctx.setParent(parent, child);
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum BinaryTreeDirection {
|
pub enum BinaryTreeDirection {
|
||||||
LEFT,
|
LEFT,
|
||||||
RIGHT,
|
RIGHT,
|
||||||
}
|
}
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum DirectedBinaryTreeDirection {
|
pub enum DirectedBinaryTreeDirection {
|
||||||
FORWARD,
|
FORWARD,
|
||||||
OPPOSITE,
|
OPPOSITE,
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
mod context;
|
mod context;
|
||||||
mod swap;
|
|
||||||
mod directed;
|
mod directed;
|
||||||
mod direction;
|
mod direction;
|
||||||
mod parent2node;
|
mod parent2node;
|
||||||
|
mod algos;
|
||||||
|
|
||||||
pub use context::NodeRefContainer;
|
pub use context::NodeRefContainer;
|
||||||
pub use direction::BinaryTreeDirection;
|
pub use direction::BinaryTreeDirection;
|
||||||
@ -1,3 +1,3 @@
|
|||||||
mod clojure;
|
pub mod clojure;
|
||||||
mod fixed_wrapper;
|
mod fixed_wrapper;
|
||||||
mod dynamic_wrappers;
|
mod dynamic_wrappers;
|
||||||
@ -1,33 +0,0 @@
|
|||||||
use crate::swap::neighbors::context::OrientedNeighborSwapContext;
|
|
||||||
|
|
||||||
mod context;
|
|
||||||
mod context_impl;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn swapNeighbors<Context: OrientedNeighborSwapContext>(
|
|
||||||
ctx: &mut Context,
|
|
||||||
grandparent: Option<Context::NodeRef>,
|
|
||||||
parent: Context::NodeRef,
|
|
||||||
child: Context::NodeRef,
|
|
||||||
) {
|
|
||||||
ctx.setParent(child, grandparent);
|
|
||||||
ctx.setGrandparentToParent(child);
|
|
||||||
ctx.setForwardChild(parent, ctx.getForwardChild(child));
|
|
||||||
|
|
||||||
let oppositeChild = ctx.getOppositeChild(parent);
|
|
||||||
ctx.setOppositeChild(parent, ctx.getOppositeChild(child));
|
|
||||||
ctx.setOppositeChild(child, oppositeChild);
|
|
||||||
|
|
||||||
if let Some(n) = ctx.getOppositeChild(child) {
|
|
||||||
ctx.setParent(n, Some(child))
|
|
||||||
}
|
|
||||||
if let Some(n) = ctx.getOppositeChild(parent) {
|
|
||||||
ctx.setParent(n, Some(parent))
|
|
||||||
}
|
|
||||||
if let Some(n) = ctx.getForwardChild(child) {
|
|
||||||
ctx.setParent(n, Some(parent))
|
|
||||||
}
|
|
||||||
ctx.setForwardChild(child, Some(parent));
|
|
||||||
ctx.setParent(parent, Some(child));
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user