1use core::borrow::Borrow;
2use core::cmp::Ordering;
3use core::hint;
4use core::ops::RangeBounds;
5
6use crate::alloc::Allocator;
7use crate::ptr;
8
9use super::node::{marker, ForceResult::*, Handle, NodeRef};
10use super::search::SearchBound;
11
12enum Never {}
13
14fn into_ok<T>(result: Result<T, Never>) -> T {
15 match result {
16 Ok(value) => value,
17 #[allow(unreachable_patterns)]
18 Err(never) => match never {},
19 }
20}
21
22pub(crate) struct LeafRange<BorrowType, K, V> {
24 front: Option<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>>,
25 back: Option<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>>,
26}
27
28impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
29 fn clone(&self) -> Self {
30 LeafRange {
31 front: self.front,
32 back: self.back,
33 }
34 }
35}
36
37impl<B, K, V> Default for LeafRange<B, K, V> {
38 fn default() -> Self {
39 LeafRange {
40 front: None,
41 back: None,
42 }
43 }
44}
45
46impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
47 pub(crate) fn none() -> Self {
48 LeafRange {
49 front: None,
50 back: None,
51 }
52 }
53
54 fn is_empty(&self) -> bool {
55 self.front == self.back
56 }
57
58 pub(crate) fn reborrow(&self) -> LeafRange<marker::Immut<'_>, K, V> {
60 LeafRange {
61 front: self.front.as_ref().map(|f| f.reborrow()),
62 back: self.back.as_ref().map(|b| b.reborrow()),
63 }
64 }
65}
66
67impl<'a, K, V> LeafRange<marker::Immut<'a>, K, V> {
68 #[inline]
69 pub(crate) fn next_checked(&mut self) -> Option<(&'a K, &'a V)> {
70 self.perform_next_checked(|kv| kv.into_kv())
71 }
72
73 #[inline]
74 pub(crate) fn next_back_checked(&mut self) -> Option<(&'a K, &'a V)> {
75 self.perform_next_back_checked(|kv| kv.into_kv())
76 }
77}
78
79impl<'a, K, V> LeafRange<marker::ValMut<'a>, K, V> {
80 #[inline]
81 pub(crate) fn next_checked(&mut self) -> Option<(&'a K, &'a mut V)> {
82 self.perform_next_checked(|kv| unsafe { ptr::read(kv) }.into_kv_valmut())
83 }
84
85 #[inline]
86 pub(crate) fn next_back_checked(&mut self) -> Option<(&'a K, &'a mut V)> {
87 self.perform_next_back_checked(|kv| unsafe { ptr::read(kv) }.into_kv_valmut())
88 }
89}
90
91impl<BorrowType: marker::BorrowType, K, V> LeafRange<BorrowType, K, V> {
92 fn perform_next_checked<F, R>(&mut self, f: F) -> Option<R>
94 where
95 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
96 {
97 if self.is_empty() {
98 None
99 } else {
100 into_ok(super::mem::replace(self.front.as_mut().unwrap(), |front| {
101 let kv = front.next_kv().ok().unwrap();
102 let result = f(&kv);
103 Ok((kv.next_leaf_edge(), Some(result)))
104 }))
105 }
106 }
107
108 fn perform_next_back_checked<F, R>(&mut self, f: F) -> Option<R>
110 where
111 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
112 {
113 if self.is_empty() {
114 None
115 } else {
116 into_ok(super::mem::replace(self.back.as_mut().unwrap(), |back| {
117 let kv = back.next_back_kv().ok().unwrap();
118 let result = f(&kv);
119 Ok((kv.next_back_leaf_edge(), Some(result)))
120 }))
121 }
122 }
123}
124
125enum LazyLeafHandle<BorrowType, K, V> {
126 Root(NodeRef<BorrowType, K, V, marker::LeafOrInternal>), Edge(Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>),
128}
129
130impl<'a, K: 'a, V: 'a> Clone for LazyLeafHandle<marker::Immut<'a>, K, V> {
131 fn clone(&self) -> Self {
132 match self {
133 LazyLeafHandle::Root(root) => LazyLeafHandle::Root(*root),
134 LazyLeafHandle::Edge(edge) => LazyLeafHandle::Edge(*edge),
135 }
136 }
137}
138
139impl<K, V> Clone for LazyLeafHandle<marker::Raw, K, V> {
140 fn clone(&self) -> Self {
141 match self {
142 LazyLeafHandle::Root(root) => LazyLeafHandle::Root(*root),
143 LazyLeafHandle::Edge(edge) => LazyLeafHandle::Edge(*edge),
144 }
145 }
146}
147
148impl<BorrowType, K, V> LazyLeafHandle<BorrowType, K, V> {
149 fn reborrow(&self) -> LazyLeafHandle<marker::Immut<'_>, K, V> {
150 match self {
151 LazyLeafHandle::Root(root) => LazyLeafHandle::Root(root.reborrow()),
152 LazyLeafHandle::Edge(edge) => LazyLeafHandle::Edge(edge.reborrow()),
153 }
154 }
155}
156
157pub(crate) struct LazyLeafRange<BorrowType, K, V> {
159 front: Option<LazyLeafHandle<BorrowType, K, V>>,
160 back: Option<LazyLeafHandle<BorrowType, K, V>>,
161}
162
163impl<B, K, V> Default for LazyLeafRange<B, K, V> {
164 fn default() -> Self {
165 LazyLeafRange {
166 front: None,
167 back: None,
168 }
169 }
170}
171
172impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
173 fn clone(&self) -> Self {
174 LazyLeafRange {
175 front: self.front.clone(),
176 back: self.back.clone(),
177 }
178 }
179}
180
181impl<K, V> Clone for LazyLeafRange<marker::Raw, K, V> {
182 fn clone(&self) -> Self {
183 LazyLeafRange {
184 front: self.front.clone(),
185 back: self.back.clone(),
186 }
187 }
188}
189
190impl<BorrowType, K, V> LazyLeafRange<BorrowType, K, V> {
191 pub(crate) fn none() -> Self {
192 LazyLeafRange {
193 front: None,
194 back: None,
195 }
196 }
197
198 pub(crate) fn reborrow(&self) -> LazyLeafRange<marker::Immut<'_>, K, V> {
200 LazyLeafRange {
201 front: self.front.as_ref().map(|f| f.reborrow()),
202 back: self.back.as_ref().map(|b| b.reborrow()),
203 }
204 }
205}
206
207impl<'a, K, V> LazyLeafRange<marker::Immut<'a>, K, V> {
208 #[inline]
209 pub(crate) unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
210 unsafe { self.init_front().unwrap().next_unchecked() }
211 }
212
213 #[inline]
214 pub(crate) unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
215 unsafe { self.init_back().unwrap().next_back_unchecked() }
216 }
217}
218
219impl<K, V> LazyLeafRange<marker::Raw, K, V> {
220 #[inline]
221 pub(crate) unsafe fn next_unchecked(&mut self) -> (*const K, *const V) {
222 unsafe { self.init_front().unwrap().next_unchecked() }
223 }
224
225 #[inline]
226 pub(crate) unsafe fn next_back_unchecked(&mut self) -> (*const K, *const V) {
227 unsafe { self.init_back().unwrap().next_back_unchecked() }
228 }
229}
230
231impl<'a, K, V> LazyLeafRange<marker::ValMut<'a>, K, V> {
232 #[inline]
233 pub(crate) unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
234 unsafe { self.init_front().unwrap().next_unchecked() }
235 }
236
237 #[inline]
238 pub(crate) unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
239 unsafe { self.init_back().unwrap().next_back_unchecked() }
240 }
241}
242
243impl<K, V> LazyLeafRange<marker::Dying, K, V> {
244 fn take_front(
245 &mut self,
246 ) -> Option<Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge>> {
247 match self.front.take()? {
248 LazyLeafHandle::Root(root) => Some(root.first_leaf_edge()),
249 LazyLeafHandle::Edge(edge) => Some(edge),
250 }
251 }
252
253 #[inline]
254 pub(crate) unsafe fn deallocating_next_unchecked<A: Allocator>(
255 &mut self,
256 alloc: &A,
257 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
258 debug_assert!(self.front.is_some());
259 let front = self.init_front().unwrap();
260 unsafe { front.deallocating_next_unchecked(alloc) }
261 }
262
263 #[inline]
264 pub(crate) unsafe fn deallocating_next_back_unchecked<A: Allocator>(
265 &mut self,
266 alloc: &A,
267 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
268 debug_assert!(self.back.is_some());
269 let back = self.init_back().unwrap();
270 unsafe { back.deallocating_next_back_unchecked(alloc) }
271 }
272
273 #[inline]
274 pub(crate) fn deallocating_end<A: Allocator>(&mut self, alloc: &A) {
275 if let Some(front) = self.take_front() {
276 front.deallocating_end(alloc)
277 }
278 }
279}
280
281impl<BorrowType: marker::BorrowType, K, V> LazyLeafRange<BorrowType, K, V> {
282 fn init_front(
283 &mut self,
284 ) -> Option<&mut Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>> {
285 if let Some(LazyLeafHandle::Root(root)) = &self.front {
286 self.front = Some(LazyLeafHandle::Edge(
287 unsafe { ptr::read(root) }.first_leaf_edge(),
288 ));
289 }
290 match &mut self.front {
291 None => None,
292 Some(LazyLeafHandle::Edge(edge)) => Some(edge),
293 Some(LazyLeafHandle::Root(_)) => unsafe { hint::unreachable_unchecked() },
295 }
296 }
297
298 fn init_back(
299 &mut self,
300 ) -> Option<&mut Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>> {
301 if let Some(LazyLeafHandle::Root(root)) = &self.back {
302 self.back = Some(LazyLeafHandle::Edge(
303 unsafe { ptr::read(root) }.last_leaf_edge(),
304 ));
305 }
306 match &mut self.back {
307 None => None,
308 Some(LazyLeafHandle::Edge(edge)) => Some(edge),
309 Some(LazyLeafHandle::Root(_)) => unsafe { hint::unreachable_unchecked() },
311 }
312 }
313}
314
315impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
316 unsafe fn find_leaf_edges_spanning_range<C: ?Sized, Q: ?Sized, R, E>(
330 self,
331 cx: &mut C,
332 range: R,
333 cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
334 ) -> Result<LeafRange<BorrowType, K, V>, E>
335 where
336 K: Borrow<Q>,
337 R: RangeBounds<Q>,
338 {
339 match self.search_tree_for_bifurcation(cx, &range, cmp)? {
340 Err(_) => Ok(LeafRange::none()),
341 Ok((
342 node,
343 lower_edge_idx,
344 upper_edge_idx,
345 mut lower_child_bound,
346 mut upper_child_bound,
347 )) => {
348 let mut lower_edge = unsafe { Handle::new_edge(ptr::read(&node), lower_edge_idx) };
349 let mut upper_edge = unsafe { Handle::new_edge(node, upper_edge_idx) };
350 loop {
351 match (lower_edge.force(), upper_edge.force()) {
352 (Leaf(f), Leaf(b)) => {
353 return Ok(LeafRange {
354 front: Some(f),
355 back: Some(b),
356 })
357 }
358 (Internal(f), Internal(b)) => {
359 (lower_edge, lower_child_bound) =
360 f.descend()
361 .find_lower_bound_edge(cx, lower_child_bound, cmp)?;
362 (upper_edge, upper_child_bound) =
363 b.descend()
364 .find_upper_bound_edge(cx, upper_child_bound, cmp)?;
365 }
366 _ => unreachable!("BTreeMap has different depths"),
367 }
368 }
369 }
370 }
371 }
372}
373
374fn full_range<BorrowType: marker::BorrowType, K, V>(
375 root1: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
376 root2: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
377) -> LazyLeafRange<BorrowType, K, V> {
378 LazyLeafRange {
379 front: Some(LazyLeafHandle::Root(root1)),
380 back: Some(LazyLeafHandle::Root(root2)),
381 }
382}
383
384impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
385 pub(crate) fn range_search<C: ?Sized, Q: ?Sized, R, E>(
390 self,
391 cx: &mut C,
392 range: R,
393 cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
394 ) -> Result<LeafRange<marker::Immut<'a>, K, V>, E>
395 where
396 K: Borrow<Q>,
397 R: RangeBounds<Q>,
398 {
399 unsafe { self.find_leaf_edges_spanning_range(cx, range, cmp) }
401 }
402
403 pub(crate) fn full_range(self) -> LazyLeafRange<marker::Immut<'a>, K, V> {
405 full_range(self, self)
406 }
407}
408
409impl<K, V> NodeRef<marker::Raw, K, V, marker::LeafOrInternal> {
410 pub(crate) fn full_range(self) -> LazyLeafRange<marker::Raw, K, V> {
412 full_range(self, self)
413 }
414}
415
416impl<'a, K: 'a, V: 'a> NodeRef<marker::ValMut<'a>, K, V, marker::LeafOrInternal> {
417 pub(crate) fn range_search<C: ?Sized, Q: ?Sized, R, E>(
427 self,
428 cx: &mut C,
429 range: R,
430 cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
431 ) -> Result<LeafRange<marker::ValMut<'a>, K, V>, E>
432 where
433 K: Borrow<Q>,
434 R: RangeBounds<Q>,
435 {
436 unsafe { self.find_leaf_edges_spanning_range(cx, range, cmp) }
437 }
438
439 pub(crate) fn full_range(self) -> LazyLeafRange<marker::ValMut<'a>, K, V> {
443 let self2 = unsafe { ptr::read(&self) };
446 full_range(self, self2)
447 }
448}
449
450impl<K, V> NodeRef<marker::Dying, K, V, marker::LeafOrInternal> {
451 pub(crate) fn full_range(self) -> LazyLeafRange<marker::Dying, K, V> {
455 let self2 = unsafe { ptr::read(&self) };
458 full_range(self, self2)
459 }
460}
461
462impl<BorrowType: marker::BorrowType, K, V>
463 Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
464{
465 pub(crate) fn next_kv(
469 self,
470 ) -> Result<
471 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>,
472 NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
473 > {
474 let mut edge = self.forget_node_type();
475 loop {
476 edge = match edge.right_kv() {
477 Ok(kv) => return Ok(kv),
478 Err(last_edge) => match last_edge.into_node().ascend() {
479 Ok(parent_edge) => parent_edge.forget_node_type(),
480 Err(root) => return Err(root),
481 },
482 }
483 }
484 }
485
486 pub(crate) fn next_back_kv(
490 self,
491 ) -> Result<
492 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>,
493 NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
494 > {
495 let mut edge = self.forget_node_type();
496 loop {
497 edge = match edge.left_kv() {
498 Ok(kv) => return Ok(kv),
499 Err(last_edge) => match last_edge.into_node().ascend() {
500 Ok(parent_edge) => parent_edge.forget_node_type(),
501 Err(root) => return Err(root),
502 },
503 }
504 }
505 }
506}
507
508impl<BorrowType: marker::BorrowType, K, V>
509 Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>
510{
511 fn next_kv(
515 self,
516 ) -> Result<
517 Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::KV>,
518 NodeRef<BorrowType, K, V, marker::Internal>,
519 > {
520 let mut edge = self;
521 loop {
522 edge = match edge.right_kv() {
523 Ok(internal_kv) => return Ok(internal_kv),
524 Err(last_edge) => last_edge.into_node().ascend()?,
525 }
526 }
527 }
528}
529
530impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
531 unsafe fn deallocating_next<A: Allocator>(
545 self,
546 alloc: &A,
547 ) -> Option<(
548 Self,
549 Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV>,
550 )> {
551 let mut edge = self.forget_node_type();
552 loop {
553 edge = match edge.right_kv() {
554 Ok(kv) => return Some((unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)),
555 Err(last_edge) => {
556 match unsafe { last_edge.into_node().deallocate_and_ascend(alloc) } {
557 Some(parent_edge) => parent_edge.forget_node_type(),
558 None => return None,
559 }
560 }
561 }
562 }
563 }
564
565 unsafe fn deallocating_next_back<A: Allocator>(
579 self,
580 alloc: &A,
581 ) -> Option<(
582 Self,
583 Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV>,
584 )> {
585 let mut edge = self.forget_node_type();
586 loop {
587 edge = match edge.left_kv() {
588 Ok(kv) => return Some((unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)),
589 Err(last_edge) => {
590 match unsafe { last_edge.into_node().deallocate_and_ascend(alloc) } {
591 Some(parent_edge) => parent_edge.forget_node_type(),
592 None => return None,
593 }
594 }
595 }
596 }
597 }
598
599 fn deallocating_end<A: Allocator>(self, alloc: &A) {
606 let mut edge = self.forget_node_type();
607 while let Some(parent_edge) = unsafe { edge.into_node().deallocate_and_ascend(alloc) } {
608 edge = parent_edge.forget_node_type();
609 }
610 }
611}
612
613impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Edge> {
614 unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
620 into_ok(super::mem::replace(self, |leaf_edge| {
621 let kv = leaf_edge.next_kv().ok().unwrap();
622 Ok((kv.next_leaf_edge(), kv.into_kv()))
623 }))
624 }
625
626 unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
632 into_ok(super::mem::replace(self, |leaf_edge| {
633 let kv = leaf_edge.next_back_kv().ok().unwrap();
634 Ok((kv.next_back_leaf_edge(), kv.into_kv()))
635 }))
636 }
637}
638
639impl<K, V> Handle<NodeRef<marker::Raw, K, V, marker::Leaf>, marker::Edge> {
640 unsafe fn next_unchecked(&mut self) -> (*const K, *const V) {
646 into_ok(super::mem::replace(self, |leaf_edge| {
647 let kv = leaf_edge.next_kv().ok().unwrap();
648 Ok((kv.next_leaf_edge(), kv.into_kv_raw()))
649 }))
650 }
651
652 unsafe fn next_back_unchecked(&mut self) -> (*const K, *const V) {
658 into_ok(super::mem::replace(self, |leaf_edge| {
659 let kv = leaf_edge.next_back_kv().ok().unwrap();
660 Ok((kv.next_back_leaf_edge(), kv.into_kv_raw()))
661 }))
662 }
663}
664
665impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::Edge> {
666 unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
672 let kv = into_ok(super::mem::replace(self, |leaf_edge| {
673 let kv = leaf_edge.next_kv().ok().unwrap();
674 Ok((unsafe { ptr::read(&kv) }.next_leaf_edge(), kv))
675 }));
676 kv.into_kv_valmut()
678 }
679
680 unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
686 let kv = into_ok(super::mem::replace(self, |leaf_edge| {
687 let kv = leaf_edge.next_back_kv().ok().unwrap();
688 Ok((unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv))
689 }));
690 kv.into_kv_valmut()
692 }
693}
694
695impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
696 unsafe fn deallocating_next_unchecked<A: Allocator>(
709 &mut self,
710 alloc: &A,
711 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
712 into_ok(super::mem::replace(self, |leaf_edge| unsafe {
713 Ok(leaf_edge.deallocating_next(alloc).unwrap())
714 }))
715 }
716
717 unsafe fn deallocating_next_back_unchecked<A: Allocator>(
730 &mut self,
731 alloc: &A,
732 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
733 into_ok(super::mem::replace(self, |leaf_edge| unsafe {
734 Ok::<_, Never>(leaf_edge.deallocating_next_back(alloc).unwrap())
735 }))
736 }
737}
738
739impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
740 #[inline]
743 pub(crate) fn first_leaf_edge(
744 self,
745 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
746 let mut node = self;
747 loop {
748 match node.force() {
749 Leaf(leaf) => return leaf.first_edge(),
750 Internal(internal) => node = internal.first_edge().descend(),
751 }
752 }
753 }
754
755 #[inline]
758 pub(crate) fn last_leaf_edge(
759 self,
760 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
761 let mut node = self;
762 loop {
763 match node.force() {
764 Leaf(leaf) => return leaf.last_edge(),
765 Internal(internal) => node = internal.last_edge().descend(),
766 }
767 }
768 }
769}
770
771pub(crate) enum Position<BorrowType, K, V> {
772 Leaf(NodeRef<BorrowType, K, V, marker::Leaf>),
773 Internal(NodeRef<BorrowType, K, V, marker::Internal>),
774 InternalKV(#[allow(unused)] Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::KV>),
775}
776
777impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
778 pub(crate) fn visit_nodes_in_order<F>(self, mut visit: F)
782 where
783 F: FnMut(Position<marker::Immut<'a>, K, V>),
784 {
785 match self.force() {
786 Leaf(leaf) => visit(Position::Leaf(leaf)),
787 Internal(internal) => {
788 visit(Position::Internal(internal));
789 let mut edge = internal.first_edge();
790 loop {
791 edge = match edge.descend().force() {
792 Leaf(leaf) => {
793 visit(Position::Leaf(leaf));
794 match edge.next_kv() {
795 Ok(kv) => {
796 visit(Position::InternalKV(kv));
797 kv.right_edge()
798 }
799 Err(_) => return,
800 }
801 }
802 Internal(internal) => {
803 visit(Position::Internal(internal));
804 internal.first_edge()
805 }
806 }
807 }
808 }
809 }
810 }
811
812 pub(crate) fn calc_length(self) -> usize {
814 let mut result = 0;
815 self.visit_nodes_in_order(|pos| match pos {
816 Position::Leaf(node) => result += node.len(),
817 Position::Internal(node) => result += node.len(),
818 Position::InternalKV(_) => (),
819 });
820 result
821 }
822}
823
824impl<BorrowType: marker::BorrowType, K, V>
825 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>
826{
827 pub(crate) fn next_leaf_edge(
829 self,
830 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
831 match self.force() {
832 Leaf(leaf_kv) => leaf_kv.right_edge(),
833 Internal(internal_kv) => {
834 let next_internal_edge = internal_kv.right_edge();
835 next_internal_edge.descend().first_leaf_edge()
836 }
837 }
838 }
839
840 pub(crate) fn next_back_leaf_edge(
842 self,
843 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
844 match self.force() {
845 Leaf(leaf_kv) => leaf_kv.left_edge(),
846 Internal(internal_kv) => {
847 let next_internal_edge = internal_kv.left_edge();
848 next_internal_edge.descend().last_leaf_edge()
849 }
850 }
851 }
852}
853
854impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
855 pub(crate) fn lower_bound<C: ?Sized, Q: ?Sized, E>(
858 self,
859 cx: &mut C,
860 mut bound: SearchBound<&Q>,
861 cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
862 ) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>, E>
863 where
864 K: Borrow<Q>,
865 {
866 let mut node = self;
867 loop {
868 let (edge, new_bound) = node.find_lower_bound_edge(cx, bound, cmp)?;
869 match edge.force() {
870 Leaf(edge) => return Ok(edge),
871 Internal(edge) => {
872 node = edge.descend();
873 bound = new_bound;
874 }
875 }
876 }
877 }
878
879 pub(crate) fn upper_bound<C: ?Sized, Q: ?Sized, E>(
882 self,
883 cx: &mut C,
884 mut bound: SearchBound<&Q>,
885 cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
886 ) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>, E>
887 where
888 K: Borrow<Q>,
889 {
890 let mut node = self;
891 loop {
892 let (edge, new_bound) = node.find_upper_bound_edge(cx, bound, cmp)?;
893 match edge.force() {
894 Leaf(edge) => return Ok(edge),
895 Internal(edge) => {
896 node = edge.descend();
897 bound = new_bound;
898 }
899 }
900 }
901 }
902}