1#[cfg(feature = "orx-parallel")]
2use orx_parallel::*;
3use orx_selfref_col::{
4 MemoryReclaimer, NodePtr, Refs, RefsArrayLeftMost, RefsSingle, RefsVec, Variant,
5 references::iter::ArrayLeftMostPtrIter,
6};
7
8pub trait TreeVariant:
10 Variant<Ends = RefsSingle<Self>, Prev = RefsSingle<Self>, Next = Self::Children>
11{
12 type Reclaimer: MemoryReclaimer<Self>;
14
15 type Children: RefsChildren<Self> + Refs;
17}
18
19pub trait RefsChildren<V: Variant> {
22 type ChildrenPtrIter<'a>: ExactSizeIterator<Item = &'a NodePtr<V>>
23 + DoubleEndedIterator
24 + Default
25 where
26 V: 'a,
27 Self: 'a;
28
29 fn num_children(&self) -> usize;
30
31 fn children_ptr(&self) -> Self::ChildrenPtrIter<'_>;
32
33 #[cfg(feature = "orx-parallel")]
34 fn children_ptr_par<'a>(&'a self) -> impl ParIter<Item = &'a NodePtr<V>>
35 where
36 V: 'a,
37 V::Item: Send + Sync;
38
39 fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>>;
40
41 fn push(&mut self, node_ptr: NodePtr<V>);
44
45 fn insert(&mut self, position: usize, node_ptr: NodePtr<V>);
46
47 fn replace_with(
48 &mut self,
49 old_node_ptr: &NodePtr<V>,
50 new_node_ptr: NodePtr<V>,
51 ) -> Option<usize>;
52}
53
54impl<V: Variant> RefsChildren<V> for RefsVec<V> {
55 type ChildrenPtrIter<'a>
56 = core::slice::Iter<'a, NodePtr<V>>
57 where
58 V: 'a,
59 Self: 'a;
60
61 #[inline(always)]
62 fn num_children(&self) -> usize {
63 self.len()
64 }
65
66 #[inline(always)]
67 fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
68 self.iter()
69 }
70
71 #[cfg(feature = "orx-parallel")]
72 fn children_ptr_par<'a>(&'a self) -> impl ParIter<Item = &'a NodePtr<V>>
73 where
74 V: 'a,
75 V::Item: Send + Sync,
76 {
77 self.as_slice().par()
78 }
79
80 #[inline(always)]
81 fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
82 self.get(i)
83 }
84
85 #[inline(always)]
86 fn push(&mut self, node_ptr: NodePtr<V>) {
87 self.push(node_ptr);
88 }
89
90 #[inline(always)]
91 fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
92 RefsVec::insert(self, position, node_ptr);
93 }
94
95 #[inline(always)]
96 fn replace_with(
97 &mut self,
98 old_node_ptr: &NodePtr<V>,
99 new_node_ptr: NodePtr<V>,
100 ) -> Option<usize> {
101 RefsVec::replace_with(self, old_node_ptr, new_node_ptr)
102 }
103}
104
105impl<const D: usize, V: Variant> RefsChildren<V> for RefsArrayLeftMost<D, V> {
106 type ChildrenPtrIter<'a>
107 = ArrayLeftMostPtrIter<'a, V>
108 where
109 V: 'a,
110 Self: 'a;
111
112 #[inline(always)]
113 fn num_children(&self) -> usize {
114 self.len()
115 }
116
117 #[inline(always)]
118 fn children_ptr(&self) -> Self::ChildrenPtrIter<'_> {
119 self.iter()
120 }
121
122 #[cfg(feature = "orx-parallel")]
123 fn children_ptr_par<'a>(&'a self) -> impl ParIter<Item = &'a NodePtr<V>>
124 where
125 V: 'a,
126 V::Item: Send + Sync,
127 {
128 self.as_slice().par().map(|x| {
129 x.as_ref()
130 .expect("all elements of RefsArrayLeftMost::as_slice are of Some variant")
131 })
132 }
133
134 #[inline(always)]
135 fn get_ptr(&self, i: usize) -> Option<&NodePtr<V>> {
136 self.get(i)
137 }
138
139 #[inline(always)]
140 fn push(&mut self, node_ptr: NodePtr<V>) {
141 self.push(node_ptr);
142 }
143
144 #[inline(always)]
145 fn insert(&mut self, position: usize, node_ptr: NodePtr<V>) {
146 RefsArrayLeftMost::insert(self, position, node_ptr);
147 }
148
149 #[inline(always)]
150 fn replace_with(
151 &mut self,
152 old_node_ptr: &NodePtr<V>,
153 new_node_ptr: NodePtr<V>,
154 ) -> Option<usize> {
155 RefsArrayLeftMost::replace_with(self, old_node_ptr, new_node_ptr)
156 }
157}