Support for polymorphic access to directed contexts

This commit is contained in:
Andrew Golovashevich 2025-12-26 16:34:59 +03:00
parent e9caf277bf
commit 54972e8215
3 changed files with 145 additions and 4 deletions

View File

@ -1,2 +1,6 @@
mod context;
mod impls;
pub use context::DirectedBinaryTreeBuilder;
pub use context::DirectedBinaryTreeDownBuilder;
pub use context::DirectedBinaryTreeDownWalker;

View File

@ -2,3 +2,7 @@ pub enum BinaryTreeDirection {
LEFT,
RIGHT,
}
pub enum DirectedBinaryTreeDirection {
FORWARD,
OPPOSITE,
}

View File

@ -2,6 +2,7 @@ use crate::NodeRefContainer;
use crate::context::{
BinaryTreeDownBuilder, BinaryTreeDownWalker, BinaryTreeRootGetter, BinaryTreeRootSetter,
};
use crate::directed::{DirectedBinaryTreeDownBuilder, DirectedBinaryTreeDownWalker};
use crate::parent2node::clojure::{Parent2NodeGetter, Parent2NodeSetter};
struct Parent2LeftChildContextWrapper<CtxRef, NodeRef> {
@ -18,6 +19,16 @@ struct Root2NodeContextWrapper<NodeRef> {
ctx: NodeRef,
}
struct Parent2ForwardChildContextWrapper<CtxRef, NodeRef> {
ctx: CtxRef,
node: NodeRef,
}
struct Parent2OppositeChildContextWrapper<CtxRef, NodeRef> {
ctx: CtxRef,
node: NodeRef,
}
impl<'ctx, CtxRef: NodeRefContainer> Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> {
fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
@ -54,14 +65,44 @@ impl<'ctx, CtxRef: NodeRefContainer + BinaryTreeRootGetter> Root2NodeContextWrap
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Root2NodeContextWrapper<&'ctx mut CtxRef>
{
impl<'ctx, CtxRef: NodeRefContainer> Root2NodeContextWrapper<&'ctx mut CtxRef> {
fn wrap(ctx: &'ctx mut CtxRef) -> Self {
return Self { ctx };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer>
Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self {
return Self { ctx, node };
}
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
@ -96,6 +137,30 @@ impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer
for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
type NodeRef = CtxRef::NodeRef;
}
impl<'ctx, CtxRef: BinaryTreeDownWalker> Parent2NodeGetter
for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
@ -144,6 +209,40 @@ impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetter
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getForwardChild(self.node);
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getForwardChild(self.node);
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getOppositeChild(self.node);
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownWalker> Parent2NodeGetter
for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
fn getChild(&self) -> Self::NodeRef {
return self.ctx.getOppositeChild(self.node);
}
}
impl<'ctx, CtxRef: BinaryTreeDownBuilder> Parent2NodeSetter
for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef>
{
@ -191,3 +290,37 @@ impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetter
self.ctx.clearRoot()
}
}
impl<'ctx, CtxRef: DirectedBinaryTreeDownBuilder> Parent2NodeSetter
for Parent2ForwardChildContextWrapper<&'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: DirectedBinaryTreeDownBuilder> Parent2NodeSetter
for Parent2OppositeChildContextWrapper<&'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);
}
}