1use alloc::vec::Vec;
14use core::ops::Bound;
15
16use crate::node::{Internal, Leaf, Node};
17use crate::store::{InMemoryStore, NodeId, NodeStore};
18
19type Path<'a, K> = Vec<(&'a Internal<K>, usize)>;
22
23struct Cursor<'a, K, V> {
26 store: &'a InMemoryStore<K, V>,
27 path: Path<'a, K>,
28 leaf: &'a Leaf<K, V>,
29 idx: usize,
30}
31
32impl<'a, K, V> Cursor<'a, K, V> {
33 #[inline]
35 fn current(&self) -> (&'a K, &'a V) {
36 (&self.leaf.keys[self.idx], &self.leaf.vals[self.idx])
37 }
38
39 #[inline]
41 fn key(&self) -> &'a K {
42 &self.leaf.keys[self.idx]
43 }
44
45 #[inline]
48 fn same_position(&self, other: &Self) -> bool {
49 core::ptr::eq(self.leaf, other.leaf) && self.idx == other.idx
50 }
51
52 fn first(store: &'a InMemoryStore<K, V>, root: NodeId) -> Option<Self> {
54 let mut path = Vec::new();
55 let mut node = store.get(root);
56 loop {
57 match node {
58 Node::Internal(internal) => {
59 path.push((internal, 0));
60 node = store.get(internal.children[0]);
61 }
62 Node::Leaf(leaf) => {
63 return if leaf.keys.is_empty() {
64 None
65 } else {
66 Some(Cursor {
67 store,
68 path,
69 leaf,
70 idx: 0,
71 })
72 };
73 }
74 }
75 }
76 }
77
78 fn last(store: &'a InMemoryStore<K, V>, root: NodeId) -> Option<Self> {
80 let mut path = Vec::new();
81 let mut node = store.get(root);
82 loop {
83 match node {
84 Node::Internal(internal) => {
85 let ci = internal.children.len() - 1;
86 path.push((internal, ci));
87 node = store.get(internal.children[ci]);
88 }
89 Node::Leaf(leaf) => {
90 return if leaf.keys.is_empty() {
91 None
92 } else {
93 Some(Cursor {
94 store,
95 path,
96 leaf,
97 idx: leaf.keys.len() - 1,
98 })
99 };
100 }
101 }
102 }
103 }
104
105 fn step_forward(&mut self) -> bool {
107 if self.idx + 1 < self.leaf.keys.len() {
108 self.idx += 1;
109 return true;
110 }
111 loop {
112 let (parent, ci) = match self.path.last() {
113 Some(&frame) => frame,
114 None => return false,
115 };
116 if ci + 1 < parent.children.len() {
117 if let Some(frame) = self.path.last_mut() {
118 frame.1 = ci + 1;
119 }
120 self.descend_leftmost(self.store.get(parent.children[ci + 1]));
121 return true;
122 }
123 let _popped = self.path.pop();
124 }
125 }
126
127 fn step_backward(&mut self) -> bool {
129 if self.idx > 0 {
130 self.idx -= 1;
131 return true;
132 }
133 loop {
134 let (parent, ci) = match self.path.last() {
135 Some(&frame) => frame,
136 None => return false,
137 };
138 if ci > 0 {
139 if let Some(frame) = self.path.last_mut() {
140 frame.1 = ci - 1;
141 }
142 self.descend_rightmost(self.store.get(parent.children[ci - 1]));
143 return true;
144 }
145 let _popped = self.path.pop();
146 }
147 }
148
149 fn descend_leftmost(&mut self, node: &'a Node<K, V>) {
152 let mut node = node;
153 loop {
154 match node {
155 Node::Internal(internal) => {
156 self.path.push((internal, 0));
157 node = self.store.get(internal.children[0]);
158 }
159 Node::Leaf(leaf) => {
160 self.leaf = leaf;
161 self.idx = 0;
162 return;
163 }
164 }
165 }
166 }
167
168 fn descend_rightmost(&mut self, node: &'a Node<K, V>) {
171 let mut node = node;
172 loop {
173 match node {
174 Node::Internal(internal) => {
175 let ci = internal.children.len() - 1;
176 self.path.push((internal, ci));
177 node = self.store.get(internal.children[ci]);
178 }
179 Node::Leaf(leaf) => {
180 self.leaf = leaf;
181 self.idx = leaf.keys.len() - 1;
182 return;
183 }
184 }
185 }
186 }
187}
188
189impl<'a, K: Ord, V> Cursor<'a, K, V> {
190 fn lower_bound(store: &'a InMemoryStore<K, V>, root: NodeId, bound: Bound<&K>) -> Option<Self> {
192 let probe = match bound {
193 Bound::Unbounded => return Self::first(store, root),
194 Bound::Included(b) | Bound::Excluded(b) => b,
195 };
196 let (path, leaf) = descend_to(store, root, probe);
197 if leaf.keys.is_empty() {
198 return None;
199 }
200 let idx = match leaf.keys.binary_search(probe) {
201 Ok(i) => match bound {
202 Bound::Excluded(_) => i + 1,
203 _ => i,
204 },
205 Err(i) => i,
206 };
207 let mut cursor = Cursor {
208 store,
209 path,
210 leaf,
211 idx: leaf.keys.len() - 1,
212 };
213 if idx < leaf.keys.len() {
214 cursor.idx = idx;
215 Some(cursor)
216 } else if cursor.step_forward() {
217 Some(cursor)
218 } else {
219 None
220 }
221 }
222
223 fn upper_bound(store: &'a InMemoryStore<K, V>, root: NodeId, bound: Bound<&K>) -> Option<Self> {
225 let probe = match bound {
226 Bound::Unbounded => return Self::last(store, root),
227 Bound::Included(b) | Bound::Excluded(b) => b,
228 };
229 let (path, leaf) = descend_to(store, root, probe);
230 if leaf.keys.is_empty() {
231 return None;
232 }
233 let idx = match leaf.keys.binary_search(probe) {
234 Ok(i) => match bound {
235 Bound::Excluded(_) => i.checked_sub(1),
236 _ => Some(i),
237 },
238 Err(i) => i.checked_sub(1),
239 };
240 let mut cursor = Cursor {
241 store,
242 path,
243 leaf,
244 idx: 0,
245 };
246 match idx {
247 Some(idx) => {
248 cursor.idx = idx;
249 Some(cursor)
250 }
251 None if cursor.step_backward() => Some(cursor),
252 None => None,
253 }
254 }
255}
256
257fn descend_to<'a, K: Ord, V>(
260 store: &'a InMemoryStore<K, V>,
261 root: NodeId,
262 probe: &K,
263) -> (Path<'a, K>, &'a Leaf<K, V>) {
264 let mut path = Vec::new();
265 let mut node = store.get(root);
266 loop {
267 match node {
268 Node::Internal(internal) => {
269 let ci = internal.child_index(probe);
270 path.push((internal, ci));
271 node = store.get(internal.children[ci]);
272 }
273 Node::Leaf(leaf) => return (path, leaf),
274 }
275 }
276}
277
278pub struct Iter<'a, K, V> {
303 span: Option<Span<'a, K, V>>,
304}
305
306struct Span<'a, K, V> {
309 front: Cursor<'a, K, V>,
310 back: Cursor<'a, K, V>,
311}
312
313impl<'a, K, V> Iter<'a, K, V> {
314 pub(crate) fn full(store: &'a InMemoryStore<K, V>, root: NodeId) -> Self {
316 match (Cursor::first(store, root), Cursor::last(store, root)) {
317 (Some(front), Some(back)) => Iter {
318 span: Some(Span { front, back }),
319 },
320 _ => Iter { span: None },
321 }
322 }
323}
324
325impl<'a, K: Ord, V> Iter<'a, K, V> {
326 pub(crate) fn range(
328 store: &'a InMemoryStore<K, V>,
329 root: NodeId,
330 lower: Bound<&K>,
331 upper: Bound<&K>,
332 ) -> Self {
333 match (
334 Cursor::lower_bound(store, root, lower),
335 Cursor::upper_bound(store, root, upper),
336 ) {
337 (Some(front), Some(back)) if front.key() <= back.key() => Iter {
338 span: Some(Span { front, back }),
339 },
340 _ => Iter { span: None },
341 }
342 }
343}
344
345impl<'a, K, V> Iterator for Iter<'a, K, V> {
346 type Item = (&'a K, &'a V);
347
348 fn next(&mut self) -> Option<Self::Item> {
349 let span = self.span.as_mut()?;
350 let out = span.front.current();
351 if span.front.same_position(&span.back) {
352 self.span = None;
353 } else {
354 let _advanced = span.front.step_forward();
355 }
356 Some(out)
357 }
358}
359
360impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
361 fn next_back(&mut self) -> Option<Self::Item> {
362 let span = self.span.as_mut()?;
363 let out = span.back.current();
364 if span.front.same_position(&span.back) {
365 self.span = None;
366 } else {
367 let _retreated = span.back.step_backward();
368 }
369 Some(out)
370 }
371}