Simplified swaps with relation setter contexts

This commit is contained in:
Andrew Golovashevich 2025-12-29 00:48:09 +03:00
parent 97a27629a1
commit 03b6925b0c
2 changed files with 32 additions and 80 deletions

View File

@ -1,86 +1,58 @@
use crate::algo::set_relation::{setRelation, xSetRelation}; use crate::base_context::BinaryTreeChildrenGetterContext;
use crate::base_context::BinaryTreeParentSetterContext; use crate::directed_context::DirectedBinaryTreeChildrenGetterContext;
use crate::base_context::{ use crate::relation_context::{
BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeParentGetterContext, BinaryTreeRelationSetterContext, DirectedBinaryTreeRelationSetterContext,
}; Parent2NodeRelationSetterClojure,
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,
}; };
pub unsafe fn swapDistant< pub unsafe fn swapDistant<
Ctx: BinaryTreeChildrenGetterContext Ctx: BinaryTreeChildrenGetterContext + BinaryTreeRelationSetterContext,
+ BinaryTreeChildrenSetterContext
+ BinaryTreeParentGetterContext
+ BinaryTreeParentSetterContext,
>( >(
ctx: &mut Ctx, ctx: &mut Ctx,
parent1: &mut ( parent1: &mut impl Parent2NodeRelationSetterClojure<NodeRef = Ctx::NodeRef>,
impl Parent2NodeSetterClojure<NodeRef = Ctx::NodeRef>
+ ParentProviderClojure<NodeRef = Ctx::NodeRef>
),
node1: Ctx::NodeRef, node1: Ctx::NodeRef,
parent2: &mut ( parent2: &mut impl Parent2NodeRelationSetterClojure<NodeRef = Ctx::NodeRef>,
impl Parent2NodeSetterClojure<NodeRef = Ctx::NodeRef>
+ ParentProviderClojure<NodeRef = Ctx::NodeRef>
),
node2: Ctx::NodeRef, node2: Ctx::NodeRef,
) { ) {
setRelation(parent1, node2.clone()); parent1.setRelation(node2.clone());
setRelation(parent2, node1.clone()); parent2.setRelation(node1.clone());
{ {
let child1 = ctx.getLeftChild(node1.clone()); let child1 = ctx.getLeftChild(node1.clone());
let child2 = ctx.getLeftChild(node2.clone()); let child2 = ctx.getLeftChild(node2.clone());
xSetRelation(&mut P2N_L::wrap_mut(ctx, node2.clone()), child1); ctx.xSetLeftRelation(node2.clone(), child1);
xSetRelation(&mut P2N_L::wrap_mut(ctx, node1.clone()), child2); ctx.xSetLeftRelation(node1.clone(), child2);
} }
{ {
let child1 = ctx.getRightChild(node1.clone()); let child1 = ctx.getRightChild(node1.clone());
let child2 = ctx.getRightChild(node2.clone()); let child2 = ctx.getRightChild(node2.clone());
xSetRelation(&mut P2N_R::wrap_mut(ctx, node2.clone()), child1); ctx.xSetRightRelation(node2.clone(), child1);
xSetRelation(&mut P2N_R::wrap_mut(ctx, node1.clone()), child2); ctx.xSetRightRelation(node1.clone(), child2);
} }
} }
pub unsafe fn swapDistantDirected< pub unsafe fn swapDistantDirected<
Ctx: DirectedBinaryTreeChildrenGetterContext Ctx: DirectedBinaryTreeChildrenGetterContext + DirectedBinaryTreeRelationSetterContext,
+ DirectedBinaryTreeChildrenSetterContext
+ BinaryTreeParentGetterContext
+ BinaryTreeParentSetterContext,
>( >(
ctx: &mut Ctx, ctx: &mut Ctx,
parent1: &mut ( parent1: &mut impl Parent2NodeRelationSetterClojure<NodeRef = Ctx::NodeRef>,
impl Parent2NodeSetterClojure<NodeRef = Ctx::NodeRef>
+ ParentProviderClojure<NodeRef = Ctx::NodeRef>
),
node1: Ctx::NodeRef, node1: Ctx::NodeRef,
parent2: &mut ( parent2: &mut impl Parent2NodeRelationSetterClojure<NodeRef = Ctx::NodeRef>,
impl Parent2NodeSetterClojure<NodeRef = Ctx::NodeRef>
+ ParentProviderClojure<NodeRef = Ctx::NodeRef>
),
node2: Ctx::NodeRef, node2: Ctx::NodeRef,
) { ) {
setRelation(parent1, node2.clone()); parent1.setRelation(node2.clone());
setRelation(parent2, node1.clone()); parent2.setRelation(node1.clone());
{ {
let child1 = ctx.getForwardChild(node1.clone()); let child1 = ctx.getForwardChild(node1.clone());
let child2 = ctx.getForwardChild(node2.clone()); let child2 = ctx.getForwardChild(node2.clone());
xSetRelation(&mut P2N_F::wrap_mut(ctx, node2.clone()), child1); ctx.xSetForwardRelation(node2.clone(), child1);
xSetRelation(&mut P2N_F::wrap_mut(ctx, node1.clone()), child2); ctx.xSetForwardRelation(node1.clone(), child2);
} }
{ {
let child1 = ctx.getOppositeChild(node1.clone()); let child1 = ctx.getOppositeChild(node1.clone());
let child2 = ctx.getOppositeChild(node2.clone()); let child2 = ctx.getOppositeChild(node2.clone());
xSetRelation(&mut P2N_O::wrap_mut(ctx, node2.clone()), child1); ctx.xSetOppositeRelation(node2.clone(), child1);
xSetRelation(&mut P2N_O::wrap_mut(ctx, node1.clone()), child2); ctx.xSetOppositeRelation(node1.clone(), child2);
} }
} }

View File

@ -1,40 +1,20 @@
use crate::base_context::BinaryTreeParentGetterContext;
use crate::base_context::BinaryTreeParentSetterContext;
use crate::directed_context::DirectedBinaryTreeChildrenGetterContext; use crate::directed_context::DirectedBinaryTreeChildrenGetterContext;
use crate::directed_context::DirectedBinaryTreeChildrenSetterContext; use crate::relation_context::{DirectedBinaryTreeRelationSetterContext, Parent2NodeRelationSetterClojure};
use crate::parent2node_clojure::{Parent2NodeSetterClojure, ParentProviderClojure};
pub unsafe fn swapNeighbors< pub unsafe fn swapNeighbors<
Ctx: DirectedBinaryTreeChildrenGetterContext Ctx: DirectedBinaryTreeChildrenGetterContext
+ DirectedBinaryTreeChildrenSetterContext + DirectedBinaryTreeRelationSetterContext,
+ BinaryTreeParentGetterContext
+ BinaryTreeParentSetterContext,
>( >(
ctx: &mut Ctx, ctx: &mut Ctx,
grandparent: &mut ( grandparent: &mut impl Parent2NodeRelationSetterClojure<NodeRef = Ctx::NodeRef>,
impl Parent2NodeSetterClojure<NodeRef = Ctx::NodeRef>
+ ParentProviderClojure<NodeRef = Ctx::NodeRef>
),
parent: Ctx::NodeRef, parent: Ctx::NodeRef,
child: Ctx::NodeRef, child: Ctx::NodeRef,
) { ) {
grandparent.setAsParentOf(child.clone()); grandparent.setRelation(child.clone());
grandparent.setChild(child.clone()); ctx.xSetForwardRelation(parent.clone(), ctx.getForwardChild(child.clone()));
ctx.xSetForwardChild(parent.clone(), ctx.getForwardChild(child.clone())); ctx.setForwardRelation(child.clone(), parent.clone());
let oppositeChild = ctx.getOppositeChild(parent.clone()); let oppositeChild = ctx.getOppositeChild(parent.clone());
ctx.xSetOppositeChild(parent.clone(), ctx.getOppositeChild(child.clone())); ctx.xSetOppositeRelation(parent.clone(), ctx.getOppositeChild(child.clone()));
ctx.xSetOppositeChild(child.clone(), oppositeChild); ctx.xSetOppositeRelation(child.clone(), oppositeChild);
if let Some(n) = ctx.getOppositeChild(child.clone()) {
ctx.setParent(n, child.clone())
}
if let Some(n) = ctx.getOppositeChild(parent.clone()) {
ctx.setParent(n, parent.clone())
}
if let Some(n) = ctx.getForwardChild(child.clone()) {
ctx.setParent(n, parent.clone())
}
ctx.setForwardChild(child.clone(), parent.clone());
ctx.setParent(parent, child);
} }