orx_tree/
tree_variant.rs

1use orx_selfref_col::{
2    MemoryReclaimer, NodePtr, Refs, RefsArrayLeftMost, RefsSingle, RefsVec, Variant,
3    references::iter::ArrayLeftMostPtrIter,
4};
5
6/// Variant of a tree.
7pub trait TreeVariant:
8    Variant<Ends = RefsSingle<Self>, Prev = RefsSingle<Self>, Next = Self::Children>
9{
10    /// Memory reclaimer of the tree.
11    type Reclaimer: MemoryReclaimer<Self>;
12
13    /// Children references of the tree nodes.
14    type Children: RefsChildren<Self> + Refs;
15}
16
17// children
18
19pub trait RefsChildren<V: Variant> {
20    type ChildrenPtrIter<'a>: ExactSizeIterator<Item = &'a NodePtr<V>>
21        + DoubleEndedIterator
22        + Default
23    where
24        V: 'a,
25        Self: 'a;
26
27    fn num_children(&self) -> usize;
28
29    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_>;
30
31    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>>;
32
33    // mut
34
35    fn push(&mut self, node_ptr: NodePtr<V>);
36
37    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>);
38
39    fn replace_with(
40        &mut self,
41        old_node_ptr: &NodePtr<V>,
42        new_node_ptr: NodePtr<V>,
43    ) -> Option<usize>;
44}
45
46impl<V: Variant> RefsChildren<V> for RefsVec<V> {
47    type ChildrenPtrIter<'a>
48        = core::slice::Iter<'a, NodePtr<V>>
49    where
50        V: 'a,
51        Self: 'a;
52
53    #[inline(always)]
54    fn num_children(&self) -> usize {
55        self.len()
56    }
57
58    #[inline(always)]
59    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
60        self.iter()
61    }
62
63    #[inline(always)]
64    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
65        self.get(i)
66    }
67
68    #[inline(always)]
69    fn push(&mut self, node_ptr: NodePtr<V>) {
70        self.push(node_ptr);
71    }
72
73    #[inline(always)]
74    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
75        RefsVec::insert(self, position, node_ptr);
76    }
77
78    #[inline(always)]
79    fn replace_with(
80        &mut self,
81        old_node_ptr: &NodePtr<V>,
82        new_node_ptr: NodePtr<V>,
83    ) -> Option<usize> {
84        RefsVec::replace_with(self, old_node_ptr, new_node_ptr)
85    }
86}
87
88impl<const D: usize, V: Variant> RefsChildren<V> for RefsArrayLeftMost<D, V> {
89    type ChildrenPtrIter<'a>
90        = ArrayLeftMostPtrIter<'a, V>
91    where
92        V: 'a,
93        Self: 'a;
94
95    #[inline(always)]
96    fn num_children(&self) -> usize {
97        self.len()
98    }
99
100    #[inline(always)]
101    fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
102        self.iter()
103    }
104
105    #[inline(always)]
106    fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
107        self.get(i)
108    }
109
110    #[inline(always)]
111    fn push(&mut self, node_ptr: NodePtr<V>) {
112        self.push(node_ptr);
113    }
114
115    #[inline(always)]
116    fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
117        RefsArrayLeftMost::insert(self, position, node_ptr);
118    }
119
120    #[inline(always)]
121    fn replace_with(
122        &mut self,
123        old_node_ptr: &NodePtr<V>,
124        new_node_ptr: NodePtr<V>,
125    ) -> Option<usize> {
126        RefsArrayLeftMost::replace_with(self, old_node_ptr, new_node_ptr)
127    }
128}