bitstring_trees/tree/mut_borrowed/
walk.rs

1use crate::{
2	tree::{
3		mut_gen::{
4			Borrowed,
5			WalkMut,
6			WalkMutPath,
7		},
8		InsertPosition,
9		Node,
10		TreeProperties,
11		WalkedDirection,
12	},
13	walk_mut::NodeOrTree,
14};
15
16use super::{
17	IterMutBorrowedInOrder,
18	IterMutBorrowedLeaf,
19	IterMutBorrowedLeafFull,
20	IterMutBorrowedPostOrder,
21	IterMutBorrowedPreOrder,
22};
23
24/// Walk borrowed 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 WalkMutBorrowed<'r, TP: TreeProperties + 'r, D = (), A = ()> {
30	pub(in crate::tree) inner: WalkMut<'r, TP, Borrowed, D, A>,
31}
32
33impl<'r, TP, D, A> WalkMutBorrowed<'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 [`WalkMutBorrowed`] value, see [`into_current_mut`].
55	///
56	/// [`into_current_mut`]: WalkMutBorrowed::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`]: WalkMutBorrowed::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, D, A> WalkMutBorrowed<'r, TP, D, A>
72where
73	TP: TreeProperties + 'r,
74	D: From<WalkedDirection>,
75{
76	/// Walk down from tree to root node (if at tree and not empty)
77	pub fn down_root_with(&mut self, add: A) -> bool {
78		self.inner.down_root_with(add)
79	}
80
81	/// Walk down to left node if present and not currently at tree
82	pub fn down_left_with(&mut self, add: A) -> bool {
83		self.inner.down_left_with(add)
84	}
85
86	/// Walk down to right node if present and not currently at tree
87	pub fn down_right_with(&mut self, add: A) -> bool {
88		self.inner.down_right_with(add)
89	}
90
91	/// Walk down to specified node if present and not currently at tree
92	///
93	/// `false` picks left and `true` picks right.
94	pub fn down_with(&mut self, side: bool, add: A) -> bool {
95		self.inner.down_with(side, add)
96	}
97}
98
99impl<'r, TP, D> WalkMutBorrowed<'r, TP, D, ()>
100where
101	TP: TreeProperties + 'r,
102	D: From<WalkedDirection>,
103{
104	/// Walk down from tree to root node (if at tree and not empty)
105	pub fn down_root(&mut self) -> bool {
106		self.inner.down_root()
107	}
108
109	/// Walk down to left node if present and not currently at tree
110	pub fn down_left(&mut self) -> bool {
111		self.inner.down_left()
112	}
113
114	/// Walk down to right node if present and not currently at tree
115	pub fn down_right(&mut self) -> bool {
116		self.inner.down_right()
117	}
118
119	/// Walk down to specified node if present and not currently at tree
120	///
121	/// `false` picks left and `true` picks right.
122	pub fn down(&mut self, side: bool) -> bool {
123		self.inner.down(side)
124	}
125}
126
127impl<'r, TP, D> WalkMutBorrowed<'r, TP, D>
128where
129	TP: TreeProperties + 'r,
130	D: From<WalkedDirection>,
131{
132	/// Start iterator to walk to deepest node that is a prefix of the target key
133	///
134	/// While consuming the iterator the stack is updated with the position of the returned nodes.
135	///
136	/// When `self` was in a mismatching subtree (i.e. not a prefix of the target key) before
137	/// the iterator won't find anything.
138	pub fn path(&mut self, key: TP::Key) -> WalkMutBorrowedPath<'r, '_, TP, D> {
139		WalkMutBorrowedPath {
140			inner: self.inner.path(key),
141		}
142	}
143
144	/// Walk to node where we'd have to insert key at
145	///
146	/// Returns `None` if tree is empty.
147	pub fn goto_insert(&mut self, key: &TP::Key) -> Option<InsertPosition> {
148		self.inner.goto_insert(key)
149	}
150}
151
152impl<'r, TP> WalkMutBorrowed<'r, TP, WalkedDirection, ()>
153where
154	TP: TreeProperties + 'r,
155{
156	/// Convert into iterator traversing depth-first pre-order
157	pub fn into_iter_pre_order(self) -> IterMutBorrowedPreOrder<'r, TP> {
158		self.inner.into_iter_pre_order().into()
159	}
160
161	/// Tree traversal: depth-first pre-order
162	pub fn next_pre_order(&mut self) -> Option<&mut Node<TP>> {
163		self.inner.next_pre_order()
164	}
165
166	/// Convert into iterator traversing depth-first in-order
167	pub fn into_iter_in_order(self) -> IterMutBorrowedInOrder<'r, TP> {
168		self.inner.into_iter_in_order().into()
169	}
170
171	/// Tree traversal: depth-first in-order
172	pub fn next_in_order(&mut self) -> Option<&mut Node<TP>> {
173		self.inner.next_in_order()
174	}
175
176	/// Convert into iterator traversing depth-first post-order
177	pub fn into_iter_post_order(self) -> IterMutBorrowedPostOrder<'r, TP> {
178		self.inner.into_iter_post_order().into()
179	}
180
181	/// Tree traversal: depth-first post-order
182	pub fn next_post_order(&mut self) -> Option<&mut Node<TP>> {
183		self.inner.next_post_order()
184	}
185
186	/// Convert into iterator over all leafs
187	pub fn into_iter_leafs(self) -> IterMutBorrowedLeaf<'r, TP> {
188		self.inner.into_iter_leafs().into()
189	}
190
191	/// Convert into iterator over all leafs and uncovered parts
192	pub fn into_iter_full_leafs(self) -> IterMutBorrowedLeafFull<'r, TP> {
193		self.inner.into_iter_full_leafs().into()
194	}
195
196	/// Tree traversal: depth-first in-order leaf nodes only
197	pub fn next_leaf(&mut self) -> Option<&mut Node<TP>> {
198		self.inner.next_leaf()
199	}
200}
201
202/// Iterate over all nodes that are a prefix of target key in a [`WalkMutBorrowed`] stack
203pub struct WalkMutBorrowedPath<'r, 'w, TP, D = ()>
204where
205	TP: TreeProperties + 'r,
206{
207	inner: WalkMutPath<'r, 'w, TP, Borrowed, D>,
208}
209
210impl<'r, 'w, TP, D> WalkMutBorrowedPath<'r, 'w, TP, D>
211where
212	TP: TreeProperties + 'r,
213	D: From<WalkedDirection>,
214{
215	/// Next step towards target node
216	#[allow(clippy::should_implement_trait)] // iterator doesn't allow using lifetime of itself in item
217	pub fn next(&mut self) -> Option<&mut Node<TP>> {
218		self.inner.next()
219	}
220}
221
222/*
223impl<'r, 'w, TP, D> IntoIterator for WalkMutOwnedPath<'r, 'w, TP, D>
224where
225	TP: TreeProperties + 'r,
226	D: From<WalkedDirection>,
227{
228	type IntoIter = IterWalkMutOwnedPath<'r, 'w, TP, D>;
229	type Item = (
230		&'r TP::Key,
231		&'r mut TP::Value,
232		Option<&'r mut TP::LeafValue>,
233	);
234
235	fn into_iter(self) -> Self::IntoIter {
236		IterWalkMutOwnedPath::new(self)
237	}
238}
239*/