1use core::{
2 hint,
3 iter::FusedIterator,
4 mem::{self, ManuallyDrop},
5 ops::{Bound, RangeBounds},
6 ptr::NonNull,
7};
8
9use allocator_api2::alloc::{Allocator, Global};
10
11use crate::{
12 BTree, BTreeKey,
13 int::{BTreeInteger, int_from_key},
14 node::{NodePool, NodePos, NodeRef},
15};
16
17#[derive(Clone)]
19pub(crate) struct RawIter<I: BTreeInteger> {
20 pub(crate) node: NodeRef,
22
23 pub(crate) pos: NodePos<I>,
29}
30
31impl<I: BTreeInteger> RawIter<I> {
32 #[inline]
38 unsafe fn is_end<V>(&self, leaf_pool: &NodePool<I, V>) -> bool {
39 unsafe { self.node.key(self.pos, leaf_pool) == I::MAX }
40 }
41
42 #[inline]
49 unsafe fn next_key<V>(&self, leaf_pool: &NodePool<I, V>) -> I::Raw {
50 unsafe { self.node.key(self.pos, leaf_pool) }
51 }
52
53 #[inline]
59 pub(crate) unsafe fn next<V>(&mut self, leaf_pool: &NodePool<I, V>) -> Option<(I, NonNull<V>)> {
60 let key = unsafe { I::from_raw(self.node.key(self.pos, leaf_pool))? };
62 let value = unsafe { self.node.values_ptr(leaf_pool).add(self.pos.index()) };
63
64 self.pos = unsafe { self.pos.next() };
66
67 if unsafe { self.is_end(leaf_pool) } {
70 if let Some(next_leaf) = unsafe { self.node.next_leaf(leaf_pool) } {
73 self.node = next_leaf;
74 self.pos = NodePos::zero();
75
76 unsafe {
80 hint::assert_unchecked(!self.is_end(leaf_pool));
81 }
82 }
83 }
84
85 Some((key, value.cast()))
86 }
87}
88
89pub struct Iter<'a, K: BTreeKey, V, A: Allocator = Global> {
91 pub(crate) raw: RawIter<K::Int>,
92 pub(crate) btree: &'a BTree<K, V, A>,
93}
94
95impl<'a, K: BTreeKey, V, A: Allocator> Iterator for Iter<'a, K, V, A> {
96 type Item = (K, &'a V);
97
98 #[inline]
99 fn next(&mut self) -> Option<Self::Item> {
100 unsafe {
101 self.raw
102 .next(&self.btree.leaf)
103 .map(|(key, value_ptr)| (K::from_int(key), value_ptr.as_ref()))
104 }
105 }
106}
107
108impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for Iter<'a, K, V, A> {}
109
110impl<'a, K: BTreeKey, V, A: Allocator> Clone for Iter<'a, K, V, A> {
111 fn clone(&self) -> Self {
112 Self {
113 raw: self.raw.clone(),
114 btree: self.btree,
115 }
116 }
117}
118
119pub struct IterMut<'a, K: BTreeKey, V, A: Allocator = Global> {
121 pub(crate) raw: RawIter<K::Int>,
122 pub(crate) btree: &'a mut BTree<K, V, A>,
123}
124
125impl<'a, K: BTreeKey, V, A: Allocator> Iterator for IterMut<'a, K, V, A> {
126 type Item = (K, &'a mut V);
127
128 #[inline]
129 fn next(&mut self) -> Option<Self::Item> {
130 unsafe {
131 self.raw
132 .next(&self.btree.leaf)
133 .map(|(key, mut value_ptr)| (K::from_int(key), value_ptr.as_mut()))
134 }
135 }
136}
137
138impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for IterMut<'a, K, V, A> {}
139
140pub struct IntoIter<K: BTreeKey, V, A: Allocator = Global> {
142 raw: RawIter<K::Int>,
143 btree: ManuallyDrop<BTree<K, V, A>>,
144}
145
146impl<K: BTreeKey, V, A: Allocator> Iterator for IntoIter<K, V, A> {
147 type Item = (K, V);
148
149 #[inline]
150 fn next(&mut self) -> Option<Self::Item> {
151 unsafe {
153 self.raw
154 .next(&self.btree.leaf)
155 .map(|(key, value_ptr)| (K::from_int(key), value_ptr.read()))
156 }
157 }
158}
159
160impl<K: BTreeKey, V, A: Allocator> Drop for IntoIter<K, V, A> {
161 #[inline]
162 fn drop(&mut self) {
163 if mem::needs_drop::<V>() {
165 while let Some((_key, value_ptr)) = unsafe { self.raw.next(&self.btree.leaf) } {
166 unsafe {
167 value_ptr.drop_in_place();
168 }
169 }
170 }
171
172 unsafe {
174 let btree = &mut *self.btree;
175 btree.internal.clear_and_free(&btree.alloc);
176 btree.leaf.clear_and_free(&btree.alloc);
177 }
178 }
179}
180
181impl<K: BTreeKey, V, A: Allocator> FusedIterator for IntoIter<K, V, A> {}
182
183pub struct Keys<'a, K: BTreeKey, V, A: Allocator = Global> {
185 raw: RawIter<K::Int>,
186 btree: &'a BTree<K, V, A>,
187}
188
189impl<'a, K: BTreeKey, V, A: Allocator> Iterator for Keys<'a, K, V, A> {
190 type Item = K;
191
192 #[inline]
193 fn next(&mut self) -> Option<Self::Item> {
194 unsafe {
195 self.raw
196 .next(&self.btree.leaf)
197 .map(|(key, _value_ptr)| K::from_int(key))
198 }
199 }
200}
201
202impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for Keys<'a, K, V, A> {}
203
204impl<'a, K: BTreeKey, V, A: Allocator> Clone for Keys<'a, K, V, A> {
205 fn clone(&self) -> Self {
206 Self {
207 raw: self.raw.clone(),
208 btree: self.btree,
209 }
210 }
211}
212
213pub struct Values<'a, K: BTreeKey, V, A: Allocator = Global> {
215 raw: RawIter<K::Int>,
216 btree: &'a BTree<K, V, A>,
217}
218
219impl<'a, K: BTreeKey, V, A: Allocator> Iterator for Values<'a, K, V, A> {
220 type Item = &'a V;
221
222 #[inline]
223 fn next(&mut self) -> Option<Self::Item> {
224 unsafe {
225 self.raw
226 .next(&self.btree.leaf)
227 .map(|(_key, value_ptr)| value_ptr.as_ref())
228 }
229 }
230}
231
232impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for Values<'a, K, V, A> {}
233
234impl<'a, K: BTreeKey, V, A: Allocator> Clone for Values<'a, K, V, A> {
235 fn clone(&self) -> Self {
236 Self {
237 raw: self.raw.clone(),
238 btree: self.btree,
239 }
240 }
241}
242
243pub struct ValuesMut<'a, K: BTreeKey, V, A: Allocator = Global> {
245 raw: RawIter<K::Int>,
246 btree: &'a mut BTree<K, V, A>,
247}
248
249impl<'a, K: BTreeKey, V, A: Allocator> Iterator for ValuesMut<'a, K, V, A> {
250 type Item = &'a mut V;
251
252 #[inline]
253 fn next(&mut self) -> Option<Self::Item> {
254 unsafe {
255 self.raw
256 .next(&self.btree.leaf)
257 .map(|(_key, mut value_ptr)| value_ptr.as_mut())
258 }
259 }
260}
261
262impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for ValuesMut<'a, K, V, A> {}
263
264pub struct Range<'a, K: BTreeKey, V, A: Allocator = Global> {
266 raw: RawIter<K::Int>,
267 end: <K::Int as BTreeInteger>::Raw,
268 btree: &'a BTree<K, V, A>,
269}
270
271impl<'a, K: BTreeKey, V, A: Allocator> Iterator for Range<'a, K, V, A> {
272 type Item = (K, &'a V);
273
274 #[inline]
275 fn next(&mut self) -> Option<Self::Item> {
276 unsafe {
277 if K::Int::cmp(self.raw.next_key(&self.btree.leaf), self.end).is_ge() {
278 return None;
279 }
280
281 self.raw
282 .next(&self.btree.leaf)
283 .map(|(key, value_ptr)| (K::from_int(key), value_ptr.as_ref()))
284 }
285 }
286}
287
288impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for Range<'a, K, V, A> {}
289
290impl<'a, K: BTreeKey, V, A: Allocator> Clone for Range<'a, K, V, A> {
291 fn clone(&self) -> Self {
292 Self {
293 raw: self.raw.clone(),
294 end: self.end,
295 btree: self.btree,
296 }
297 }
298}
299
300pub struct RangeMut<'a, K: BTreeKey, V, A: Allocator = Global> {
302 raw: RawIter<K::Int>,
303 end: <K::Int as BTreeInteger>::Raw,
304 btree: &'a mut BTree<K, V, A>,
305}
306
307impl<'a, K: BTreeKey, V, A: Allocator> Iterator for RangeMut<'a, K, V, A> {
308 type Item = (K, &'a mut V);
309
310 #[inline]
311 fn next(&mut self) -> Option<Self::Item> {
312 unsafe {
313 if K::Int::cmp(self.raw.next_key(&self.btree.leaf), self.end).is_ge() {
314 return None;
315 }
316
317 self.raw
318 .next(&self.btree.leaf)
319 .map(|(key, mut value_ptr)| (K::from_int(key), value_ptr.as_mut()))
320 }
321 }
322}
323
324impl<'a, K: BTreeKey, V, A: Allocator> FusedIterator for RangeMut<'a, K, V, A> {}
325
326impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A> {
327 #[inline]
329 pub(crate) fn raw_iter(&self) -> RawIter<K::Int> {
330 let node = NodeRef::zero();
333 let pos = pos!(0);
334 RawIter { node, pos }
335 }
336
337 #[inline]
340 pub(crate) fn raw_iter_from(&self, key: <K::Int as BTreeInteger>::Raw) -> RawIter<K::Int> {
341 let mut height = self.height;
345 let mut node = self.root;
346 while let Some(down) = height.down() {
347 let keys = unsafe { node.keys(&self.internal) };
348 let pos = unsafe { K::Int::search(keys, key) };
349 node = unsafe { node.value(pos, &self.internal).assume_init_read() };
350 height = down;
351 }
352
353 let keys = unsafe { node.keys(&self.leaf) };
356 let pos = unsafe { K::Int::search(keys, key) };
357 RawIter { node, pos }
358 }
359
360 #[inline]
362 pub fn iter(&self) -> Iter<'_, K, V, A> {
363 Iter {
364 raw: self.raw_iter(),
365 btree: self,
366 }
367 }
368
369 #[inline]
371 pub fn iter_mut(&mut self) -> IterMut<'_, K, V, A> {
372 IterMut {
373 raw: self.raw_iter(),
374 btree: self,
375 }
376 }
377
378 #[inline]
381 pub fn iter_from(&self, bound: Bound<K>) -> Iter<'_, K, V, A> {
382 let raw = match bound {
383 Bound::Included(key) => self.raw_iter_from(int_from_key(key)),
384 Bound::Excluded(key) => self.raw_iter_from(K::Int::increment(int_from_key(key))),
385 Bound::Unbounded => self.raw_iter(),
386 };
387 Iter { raw, btree: self }
388 }
389
390 #[inline]
393 pub fn iter_mut_from(&mut self, bound: Bound<K>) -> IterMut<'_, K, V, A> {
394 let raw = match bound {
395 Bound::Included(key) => self.raw_iter_from(int_from_key(key)),
396 Bound::Excluded(key) => self.raw_iter_from(K::Int::increment(int_from_key(key))),
397 Bound::Unbounded => self.raw_iter(),
398 };
399 IterMut { raw, btree: self }
400 }
401
402 #[inline]
404 pub fn keys(&self) -> Keys<'_, K, V, A> {
405 Keys {
406 raw: self.raw_iter(),
407 btree: self,
408 }
409 }
410
411 #[inline]
413 pub fn values(&self) -> Values<'_, K, V, A> {
414 Values {
415 raw: self.raw_iter(),
416 btree: self,
417 }
418 }
419
420 #[inline]
422 pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, A> {
423 ValuesMut {
424 raw: self.raw_iter(),
425 btree: self,
426 }
427 }
428
429 #[inline]
434 pub fn range(&self, range: impl RangeBounds<K>) -> Range<'_, K, V, A> {
435 let raw = match range.start_bound() {
436 Bound::Included(&key) => self.raw_iter_from(int_from_key(key)),
437 Bound::Excluded(&key) => self.raw_iter_from(K::Int::increment(int_from_key(key))),
438 Bound::Unbounded => self.raw_iter(),
439 };
440 let end = match range.end_bound() {
441 Bound::Included(&key) => K::Int::increment(int_from_key(key)),
442 Bound::Excluded(&key) => int_from_key(key),
443 Bound::Unbounded => K::Int::MAX,
444 };
445 Range {
446 raw,
447 end,
448 btree: self,
449 }
450 }
451
452 #[inline]
457 pub fn range_mut(&mut self, range: impl RangeBounds<K>) -> RangeMut<'_, K, V, A> {
458 let raw = match range.start_bound() {
459 Bound::Included(&key) => self.raw_iter_from(int_from_key(key)),
460 Bound::Excluded(&key) => self.raw_iter_from(K::Int::increment(int_from_key(key))),
461 Bound::Unbounded => self.raw_iter(),
462 };
463 let end = match range.end_bound() {
464 Bound::Included(&key) => K::Int::increment(int_from_key(key)),
465 Bound::Excluded(&key) => int_from_key(key),
466 Bound::Unbounded => K::Int::MAX,
467 };
468 RangeMut {
469 raw,
470 end,
471 btree: self,
472 }
473 }
474}
475
476impl<K: BTreeKey, V, A: Allocator> IntoIterator for BTree<K, V, A> {
477 type Item = (K, V);
478
479 type IntoIter = IntoIter<K, V, A>;
480
481 #[inline]
482 fn into_iter(self) -> Self::IntoIter {
483 IntoIter {
484 raw: self.raw_iter(),
485 btree: ManuallyDrop::new(self),
486 }
487 }
488}
489
490impl<'a, K: BTreeKey, V, A: Allocator> IntoIterator for &'a BTree<K, V, A> {
491 type Item = (K, &'a V);
492
493 type IntoIter = Iter<'a, K, V, A>;
494
495 #[inline]
496 fn into_iter(self) -> Self::IntoIter {
497 self.iter()
498 }
499}
500
501impl<'a, K: BTreeKey, V, A: Allocator> IntoIterator for &'a mut BTree<K, V, A> {
502 type Item = (K, &'a mut V);
503
504 type IntoIter = IterMut<'a, K, V, A>;
505
506 #[inline]
507 fn into_iter(self) -> Self::IntoIter {
508 self.iter_mut()
509 }
510}