bitstring_trees/tree/mut_owned/walk.rs
1use crate::{
2 tree::{
3 mut_gen::{
4 Owned,
5 WalkMut,
6 WalkMutPath,
7 },
8 InsertPosition,
9 Node,
10 TreeProperties,
11 WalkedDirection,
12 },
13 walk_mut::NodeOrTree,
14};
15
16use super::{
17 IterMutOwnedInOrder,
18 IterMutOwnedLeaf,
19 IterMutOwnedLeafFull,
20 IterMutOwnedPostOrder,
21 IterMutOwnedPreOrder,
22};
23
24/// Walk owned mutable tree up and down
25///
26/// Some algorithms need to remember how they reached the current node via [`WalkedDirection`] as `D`.
27///
28/// When walking manually it might be useful to be able to store additional data via `A`.
29pub struct WalkMutOwned<'r, TP: TreeProperties + 'r, D = (), A = ()> {
30 pub(in crate::tree) inner: WalkMut<'r, TP, Owned, D, A>,
31}
32
33impl<'r, TP, D, A> WalkMutOwned<'r, TP, D, A>
34where
35 TP: TreeProperties,
36{
37 /// Walk up to parent node or tree if not at tree
38 pub fn up(&mut self) -> Option<D> {
39 self.inner.up()
40 }
41
42 /// Walk up to parent node or tree if not at tree
43 pub fn up_with(&mut self) -> Option<(D, A)> {
44 self.inner.up_with()
45 }
46
47 /// Current node or tree
48 pub fn current(&self) -> NodeOrTree<Option<&Node<TP>>, &Node<TP>> {
49 self.inner.current()
50 }
51
52 /// Current mutable node or tree
53 ///
54 /// If you need the result to outlive the destruction of the [`WalkMutOwned`] value, see [`into_current_mut`].
55 ///
56 /// [`into_current_mut`]: WalkMutOwned::into_current_mut
57 pub fn current_mut(&mut self) -> NodeOrTree<Option<&mut Node<TP>>, &mut Node<TP>> {
58 self.inner.current_mut()
59 }
60
61 /// Extract mutable node or tree
62 ///
63 /// Also see [`current_mut`]
64 ///
65 /// [`current_mut`]: WalkMutOwned::current_mut
66 pub fn into_current_mut(self) -> NodeOrTree<Option<&'r mut Node<TP>>, &'r mut Node<TP>> {
67 self.inner.into_current_mut()
68 }
69}
70
71impl<'r, TP> WalkMutOwned<'r, TP, WalkedDirection, ()>
72where
73 TP: TreeProperties + 'r,
74{
75 /// Delete current node (or tree)
76 ///
77 /// Afterwards the current node is the previous parent node, which was replaced by the sibling,
78 /// or the (empty) tree when removing the last node.
79 ///
80 /// Returns what [`up_with`] would have returned.
81 ///
82 /// [`up_with`]: WalkMutOwned::up_with
83 pub fn delete_current(&mut self) -> Option<WalkedDirection> {
84 self.inner.delete_current()
85 }
86}
87
88impl<'r, TP, A> WalkMutOwned<'r, TP, WalkedDirection, A>
89where
90 TP: TreeProperties + 'r,
91{
92 /// Delete current node (or tree)
93 ///
94 /// Afterwards the current node is the previous parent node, which was replaced by the sibling,
95 /// or the (empty) tree when removing the last node.
96 ///
97 /// Returns what [`up_with`] would have returned.
98 ///
99 /// [`up_with`]: WalkMutOwned::up_with
100 pub fn delete_current_with(&mut self) -> Option<(WalkedDirection, A)> {
101 self.inner.delete_current_with()
102 }
103
104 /// Remove empty leaf nodes if possible
105 ///
106 /// A node is considered "empty" if the passed function considers its value empty.
107 ///
108 /// Calls this if the current value might just have become empty.
109 ///
110 /// Empty leafs can only be removed if the parent node is empty too, or
111 /// both siblings are empty leafs (then the parent becomes a leaf node).
112 ///
113 /// * if current node isn't empty nothing changes
114 /// * if current node is an empty leaf node:
115 /// * parent and sibling shouldn't have both been empty, as previous [`compact_if_empty`] calls would have cleaned that up
116 /// * if parent is empty: remove leaf and parent, replace parent with sibling. `current` points to sibling afterwards.
117 /// * if sibling is an empty leaf: make parent a leaf, `current` points to parent afterwards.
118 /// * otherwise no tree changes, but `current` points to parent afterwards.
119 /// * if current node has an empty leaf child node, remove that child node and current node.
120 /// I.e. replace current node with other child; `current` points to that child afterwards.
121 /// Other child node shouldn't be an empty leaf, as previous [`compact_if_empty`] calls would have cleaned that up.
122 /// * if current points to tree or root node, clear tree if it is an empty node
123 ///
124 /// [`compact_if_empty`]: Self::compact_if_empty
125 pub fn compact_if_empty<F>(&mut self, is_empty: F)
126 where
127 F: Fn(&TP::Value) -> bool,
128 {
129 self.inner.compact_if_empty(is_empty)
130 }
131}
132
133impl<'r, TP, D, A> WalkMutOwned<'r, TP, D, A>
134where
135 TP: TreeProperties + 'r,
136 D: From<WalkedDirection>,
137{
138 /// Walk down from tree to root node (if at tree and not empty)
139 pub fn down_root_with(&mut self, add: A) -> bool {
140 self.inner.down_root_with(add)
141 }
142
143 /// Walk down to left node if present and not currently at tree
144 pub fn down_left_with(&mut self, add: A) -> bool {
145 self.inner.down_left_with(add)
146 }
147
148 /// Walk down to right node if present and not currently at tree
149 pub fn down_right_with(&mut self, add: A) -> bool {
150 self.inner.down_right_with(add)
151 }
152
153 /// Walk down to specified node if present and not currently at tree
154 ///
155 /// `false` picks left and `true` picks right.
156 pub fn down_with(&mut self, side: bool, add: A) -> bool {
157 self.inner.down_with(side, add)
158 }
159}
160
161impl<'r, TP, D> WalkMutOwned<'r, TP, D, ()>
162where
163 TP: TreeProperties + 'r,
164 D: From<WalkedDirection>,
165{
166 /// Walk down from tree to root node (if at tree and not empty)
167 pub fn down_root(&mut self) -> bool {
168 self.inner.down_root()
169 }
170
171 /// Walk down to left node if present and not currently at tree
172 pub fn down_left(&mut self) -> bool {
173 self.inner.down_left()
174 }
175
176 /// Walk down to right node if present and not currently at tree
177 pub fn down_right(&mut self) -> bool {
178 self.inner.down_right()
179 }
180
181 /// Walk down to specified node if present and not currently at tree
182 ///
183 /// `false` picks left and `true` picks right.
184 pub fn down(&mut self, side: bool) -> bool {
185 self.inner.down(side)
186 }
187}
188
189impl<'r, TP, D> WalkMutOwned<'r, TP, D>
190where
191 TP: TreeProperties + 'r,
192 D: From<WalkedDirection>,
193{
194 /// Start iterator to walk to deepest node that is a prefix of the target key
195 ///
196 /// While consuming the iterator the stack is updated with the position of the returned nodes.
197 ///
198 /// When `self` was in a mismatching subtree (i.e. not a prefix of the target key) before
199 /// the iterator won't find anything.
200 pub fn path(&mut self, key: TP::Key) -> WalkMutOwnedPath<'r, '_, TP, D> {
201 WalkMutOwnedPath {
202 inner: self.inner.path(key),
203 }
204 }
205
206 /// Walk to node where we'd have to insert key at
207 ///
208 /// Returns `None` if tree is empty.
209 pub fn goto_insert(&mut self, key: &TP::Key) -> Option<InsertPosition> {
210 self.inner.goto_insert(key)
211 }
212}
213
214impl<'r, TP, D> WalkMutOwned<'r, TP, D>
215where
216 TP: TreeProperties + 'r,
217 D: From<WalkedDirection>,
218{
219 /// Insert new (possibly inner) node with exact key in tree, walk to it and return reference to it
220 pub fn insert(&mut self, key: TP::Key) -> &mut Node<TP> {
221 self.inner.insert(key)
222 }
223}
224
225impl<'r, TP> WalkMutOwned<'r, TP, WalkedDirection, ()>
226where
227 TP: TreeProperties + 'r,
228{
229 /// Convert into iterator traversing depth-first pre-order
230 pub fn into_iter_pre_order(self) -> IterMutOwnedPreOrder<'r, TP> {
231 self.inner.into_iter_pre_order().into()
232 }
233
234 /// Tree traversal: depth-first pre-order
235 pub fn next_pre_order(&mut self) -> Option<&mut Node<TP>> {
236 self.inner.next_pre_order()
237 }
238
239 /// Convert into iterator traversing depth-first in-order
240 pub fn into_iter_in_order(self) -> IterMutOwnedInOrder<'r, TP> {
241 self.inner.into_iter_in_order().into()
242 }
243
244 /// Tree traversal: depth-first in-order
245 pub fn next_in_order(&mut self) -> Option<&mut Node<TP>> {
246 self.inner.next_in_order()
247 }
248
249 /// Convert into iterator traversing depth-first post-order
250 pub fn into_iter_post_order(self) -> IterMutOwnedPostOrder<'r, TP> {
251 self.inner.into_iter_post_order().into()
252 }
253
254 /// Tree traversal: depth-first post-order
255 pub fn next_post_order(&mut self) -> Option<&mut Node<TP>> {
256 self.inner.next_post_order()
257 }
258
259 /// Convert into iterator over all leafs
260 pub fn into_iter_leafs(self) -> IterMutOwnedLeaf<'r, TP> {
261 self.inner.into_iter_leafs().into()
262 }
263
264 /// Convert into iterator over all leafs and uncovered parts
265 pub fn into_iter_full_leafs(self) -> IterMutOwnedLeafFull<'r, TP> {
266 self.inner.into_iter_full_leafs().into()
267 }
268
269 /// Tree traversal: depth-first in-order leaf nodes only
270 pub fn next_leaf(&mut self) -> Option<&mut Node<TP>> {
271 self.inner.next_leaf()
272 }
273}
274
275/// Iterate over all nodes that are a prefix of target key in a [`WalkMutOwned`] stack
276pub struct WalkMutOwnedPath<'r, 'w, TP, D = ()>
277where
278 TP: TreeProperties + 'r,
279{
280 inner: WalkMutPath<'r, 'w, TP, Owned, D>,
281}
282
283impl<'r, 'w, TP, D> WalkMutOwnedPath<'r, 'w, TP, D>
284where
285 TP: TreeProperties + 'r,
286 D: From<WalkedDirection>,
287{
288 /// Next step towards target node
289 #[allow(clippy::should_implement_trait)] // iterator doesn't allow using lifetime of itself in item
290 pub fn next(&mut self) -> Option<&mut Node<TP>> {
291 self.inner.next()
292 }
293}
294
295/*
296impl<'r, 'w, TP, D> IntoIterator for WalkMutOwnedPath<'r, 'w, TP, D>
297where
298 TP: TreeProperties + 'r,
299 D: From<WalkedDirection>,
300{
301 type IntoIter = IterWalkMutOwnedPath<'r, 'w, TP, D>;
302 type Item = (
303 &'r TP::Key,
304 &'r mut TP::Value,
305 Option<&'r mut TP::LeafValue>,
306 );
307
308 fn into_iter(self) -> Self::IntoIter {
309 IterWalkMutOwnedPath::new(self)
310 }
311}
312*/