orx_tree/
tree_variant.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use orx_selfref_col::{
    references::iter::ArrayLeftMostPtrIter, MemoryReclaimer, NodePtr, Refs, RefsArrayLeftMost,
    RefsSingle, RefsVec, Variant,
};

/// Variant of a tree.
pub trait TreeVariant:
    Variant<Ends = RefsSingle<Self>, Prev = RefsSingle<Self>, Next = Self::Children>
{
    /// Memory reclaimer of the tree.
    type Reclaimer: MemoryReclaimer<Self>;

    /// Children references of the tree nodes.
    type Children: RefsChildren<Self> + Refs;
}

// children

pub trait RefsChildren<V: Variant> {
    type ChildrenPtrIter<'a>: ExactSizeIterator<Item = &'a NodePtr<V>>
        + DoubleEndedIterator
        + Default
    where
        V: 'a,
        Self: 'a;

    fn num_children(&self) -> usize;

    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_>;

    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>>;

    // mut

    fn push(&mut self, node_ptr: NodePtr<V>);

    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>);

    fn replace_with(
        &mut self,
        old_node_ptr: &NodePtr<V>,
        new_node_ptr: NodePtr<V>,
    ) -> Option<usize>;
}

impl<V: Variant> RefsChildren<V> for RefsVec<V> {
    type ChildrenPtrIter<'a>
        = core::slice::Iter<'a, NodePtr<V>>
    where
        V: 'a,
        Self: 'a;

    #[inline(always)]
    fn num_children(&self) -> usize {
        self.len()
    }

    #[inline(always)]
    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
        self.iter()
    }

    #[inline(always)]
    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
        self.get(i)
    }

    #[inline(always)]
    fn push(&mut self, node_ptr: NodePtr<V>) {
        self.push(node_ptr);
    }

    #[inline(always)]
    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
        RefsVec::insert(self, position, node_ptr);
    }

    #[inline(always)]
    fn replace_with(
        &mut self,
        old_node_ptr: &NodePtr<V>,
        new_node_ptr: NodePtr<V>,
    ) -> Option<usize> {
        RefsVec::replace_with(self, old_node_ptr, new_node_ptr)
    }
}

impl<const D: usize, V: Variant> RefsChildren<V> for RefsArrayLeftMost<D, V> {
    type ChildrenPtrIter<'a>
        = ArrayLeftMostPtrIter<'a, V>
    where
        V: 'a,
        Self: 'a;

    #[inline(always)]
    fn num_children(&self) -> usize {
        self.len()
    }

    #[inline(always)]
    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
        self.iter()
    }

    #[inline(always)]
    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
        self.get(i)
    }

    #[inline(always)]
    fn push(&mut self, node_ptr: NodePtr<V>) {
        self.push(node_ptr);
    }

    #[inline(always)]
    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
        RefsArrayLeftMost::insert(self, position, node_ptr);
    }

    #[inline(always)]
    fn replace_with(
        &mut self,
        old_node_ptr: &NodePtr<V>,
        new_node_ptr: NodePtr<V>,
    ) -> Option<usize> {
        RefsArrayLeftMost::replace_with(self, old_node_ptr, new_node_ptr)
    }
}