bitstring_trees/tree/mut_gen/
iter.rs

1use crate::{
2	iter::{
3		iter_between,
4		IterBetween,
5	},
6	tree::{
7		mut_gen::{
8			WalkMut,
9			WalkMutPath,
10		},
11		MutPath,
12		Node,
13		TreeProperties,
14		WalkedDirection,
15	},
16};
17
18use super::walk::OwnedTreeMarker;
19
20// safety: must only call once per node per iterator lifetime
21unsafe fn extract_from_node<'r, TP: TreeProperties>(
22	node: &mut Node<TP>,
23) -> (
24	&'r TP::Key,
25	&'r mut TP::Value,
26	Option<&'r mut TP::LeafValue>,
27) {
28	let key: *const <TP as TreeProperties>::Key = node.get_key() as *const _;
29	let value = node.get_value_mut() as *mut _;
30	let leaf_value = node.get_leaf_value_mut().map(|v| v as *mut _);
31	(
32		unsafe { &*key },
33		unsafe { &mut *value },
34		leaf_value.map(|v| unsafe { &mut *v }),
35	)
36}
37
38/// Iterate over keys and mutable values of tree that are a prefix of target key
39pub struct IterMutPath<'r, TP: TreeProperties> {
40	path: MutPath<'r, TP>,
41}
42
43impl<'r, TP: TreeProperties> IterMutPath<'r, TP> {
44	pub(in crate::tree) fn new(path: MutPath<'r, TP>) -> Self {
45		Self { path }
46	}
47}
48
49impl<'r, TP: TreeProperties> Iterator for IterMutPath<'r, TP> {
50	type Item = (
51		&'r TP::Key,
52		&'r mut TP::Value,
53		Option<&'r mut TP::LeafValue>,
54	);
55
56	fn next(&mut self) -> Option<Self::Item> {
57		let node = self.path.next()?;
58		// safety: only once per node per iteration
59		Some(unsafe { extract_from_node(node) })
60	}
61}
62
63/// Iterate over all nodes that are a prefix of target key in a [`WalkMut`] stack
64pub struct IterWalkMutPath<'r, 'w, TP, O, D = ()>
65where
66	TP: TreeProperties + 'r,
67	O: OwnedTreeMarker<'r, TP, D, ()>,
68{
69	path: WalkMutPath<'r, 'w, TP, O, D>,
70}
71
72impl<'r, 'w, TP, O, D> IterWalkMutPath<'r, 'w, TP, O, D>
73where
74	TP: TreeProperties + 'r,
75	O: OwnedTreeMarker<'r, TP, D, ()>,
76{
77	pub(in crate::tree) fn new(path: WalkMutPath<'r, 'w, TP, O, D>) -> Self {
78		Self { path }
79	}
80}
81
82impl<'r, TP, O, D> Iterator for IterWalkMutPath<'r, '_, TP, O, D>
83where
84	TP: TreeProperties + 'r,
85	O: OwnedTreeMarker<'r, TP, D, ()>,
86	D: From<WalkedDirection>,
87{
88	type Item = (
89		&'r TP::Key,
90		&'r mut TP::Value,
91		Option<&'r mut TP::LeafValue>,
92	);
93
94	fn next(&mut self) -> Option<Self::Item> {
95		let node = self.path.next()?;
96		// safety: only once per node per iteration
97		Some(unsafe { extract_from_node(node) })
98	}
99}
100
101/// Iterate over keys and mutable values of tree depth-first pre-order
102pub(in crate::tree) struct IterMutPreOrder<'r, TP, O>
103where
104	TP: TreeProperties,
105	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
106{
107	pub(super) walk: WalkMut<'r, TP, O, WalkedDirection>,
108}
109
110impl<'r, TP, O> Iterator for IterMutPreOrder<'r, TP, O>
111where
112	TP: TreeProperties + 'r,
113	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
114{
115	type Item = (
116		&'r TP::Key,
117		&'r mut TP::Value,
118		Option<&'r mut TP::LeafValue>,
119	);
120
121	fn next(&mut self) -> Option<Self::Item> {
122		let node = self.walk.next_pre_order()?;
123		// safety: only once per node per iteration
124		Some(unsafe { extract_from_node(node) })
125	}
126}
127
128/// Iterate over keys and mutable values of tree depth-first in-order
129pub(in crate::tree) struct IterMutInOrder<'r, TP, O>
130where
131	TP: TreeProperties + 'r,
132	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
133{
134	pub(super) walk: WalkMut<'r, TP, O, WalkedDirection>,
135}
136
137impl<'r, TP, O> Iterator for IterMutInOrder<'r, TP, O>
138where
139	TP: TreeProperties + 'r,
140	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
141{
142	type Item = (
143		&'r TP::Key,
144		&'r mut TP::Value,
145		Option<&'r mut TP::LeafValue>,
146	);
147
148	fn next(&mut self) -> Option<Self::Item> {
149		let node = self.walk.next_in_order()?;
150		// safety: only once per node per iteration
151		Some(unsafe { extract_from_node(node) })
152	}
153}
154
155/// Iterate over keys and mutable values of tree depth-first post-order
156pub(in crate::tree) struct IterMutPostOrder<'r, TP, O>
157where
158	TP: TreeProperties + 'r,
159	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
160{
161	pub(super) walk: WalkMut<'r, TP, O, WalkedDirection>,
162}
163
164impl<'r, TP, O> Iterator for IterMutPostOrder<'r, TP, O>
165where
166	TP: TreeProperties + 'r,
167	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
168{
169	type Item = (
170		&'r TP::Key,
171		&'r mut TP::Value,
172		Option<&'r mut TP::LeafValue>,
173	);
174
175	fn next(&mut self) -> Option<Self::Item> {
176		let node = self.walk.next_post_order()?;
177		// safety: only once per node per iteration
178		Some(unsafe { extract_from_node(node) })
179	}
180}
181
182/// Iterate over keys and mutable leaf values of tree in-order
183pub(in crate::tree) struct IterMutLeaf<'r, TP, O>
184where
185	TP: TreeProperties + 'r,
186	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
187{
188	pub(super) walk: WalkMut<'r, TP, O, WalkedDirection>,
189}
190
191impl<'r, TP, O> Iterator for IterMutLeaf<'r, TP, O>
192where
193	TP: TreeProperties + 'r,
194	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
195{
196	type Item = (&'r TP::Key, &'r mut TP::LeafValue);
197
198	fn next(&mut self) -> Option<Self::Item> {
199		let node = self.walk.next_leaf()?;
200		// safety: only once per node per iteration
201		let (key, _, leaf_value) = unsafe { extract_from_node(node) };
202		Some((key, leaf_value.expect("leaf node")))
203	}
204}
205
206/// Iterate over keys and mutable leaf values and uncovered keys of tree in-order
207pub(in crate::tree) struct IterMutLeafFull<'r, TP, O>
208where
209	TP: TreeProperties + 'r,
210	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
211{
212	walk: Option<WalkMut<'r, TP, O, WalkedDirection>>,
213	previous_key: Option<TP::Key>,
214	uncovered: IterBetween<TP::Key>,
215	next: Option<(TP::Key, &'r mut TP::LeafValue)>,
216}
217
218impl<'r, TP, O> IterMutLeafFull<'r, TP, O>
219where
220	TP: TreeProperties + 'r,
221	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
222{
223	pub(in crate::tree) fn new(walk: WalkMut<'r, TP, O, WalkedDirection>) -> Self {
224		Self {
225			walk: Some(walk),
226			previous_key: None,
227			uncovered: Default::default(),
228			next: None,
229		}
230	}
231}
232
233impl<'r, TP, O> Iterator for IterMutLeafFull<'r, TP, O>
234where
235	TP: TreeProperties + 'r,
236	O: OwnedTreeMarker<'r, TP, WalkedDirection>,
237{
238	type Item = (TP::Key, Option<&'r mut TP::LeafValue>);
239
240	fn next(&mut self) -> Option<Self::Item> {
241		loop {
242			if let Some(k) = self.uncovered.next() {
243				return Some((k, None));
244			}
245			if let Some((key, value)) = self.next.take() {
246				return Some((key, Some(value)));
247			}
248			match self.walk.as_mut()?.next_leaf() {
249				None => {
250					self.walk = None;
251					// return final uncovered prefixes
252					self.uncovered = iter_between(self.previous_key.clone(), None);
253				},
254				Some(node) => {
255					// safety: only once per node per iteration
256					let (key, _, leaf_value) = unsafe { extract_from_node(node) };
257					let key = key.clone();
258					let leaf_value = leaf_value.expect("leaf node");
259					// return uncovered prefixes before
260					let start = core::mem::replace(&mut self.previous_key, Some(key.clone()));
261					self.uncovered = iter_between(start, Some(key.clone()));
262					self.next = Some((key, leaf_value));
263				},
264			}
265		}
266	}
267}