1use core::borrow::Borrow;
2use core::ops::RangeBounds;
3use core::{hint, ptr};
4
5use super::node::ForceResult::*;
6use super::node::{Handle, NodeRef, marker};
7use super::search::SearchBound;
8use crate::alloc::AllocatorClone;
9pub(super) struct LeafRange<BorrowType, K, V> {
11 front: Option<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>>,
12 back: Option<Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>>,
13}
14
15impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
16 fn clone(&self) -> Self {
17 LeafRange { front: self.front.clone(), back: self.back.clone() }
18 }
19}
20
21impl<B, K, V> Default for LeafRange<B, K, V> {
22 fn default() -> Self {
23 LeafRange { front: None, back: None }
24 }
25}
26
27impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
28 pub(super) fn none() -> Self {
29 LeafRange { front: None, back: None }
30 }
31
32 fn is_empty(&self) -> bool {
33 self.front == self.back
34 }
35
36 pub(super) fn reborrow(&self) -> LeafRange<marker::Immut<'_>, K, V> {
38 LeafRange {
39 front: self.front.as_ref().map(|f| f.reborrow()),
40 back: self.back.as_ref().map(|b| b.reborrow()),
41 }
42 }
43}
44
45impl<'a, K, V> LeafRange<marker::Immut<'a>, K, V> {
46 #[inline]
47 pub(super) fn next_checked(&mut self) -> Option<(&'a K, &'a V)> {
48 self.perform_next_checked(|kv| kv.into_kv())
49 }
50
51 #[inline]
52 pub(super) fn next_back_checked(&mut self) -> Option<(&'a K, &'a V)> {
53 self.perform_next_back_checked(|kv| kv.into_kv())
54 }
55}
56
57impl<'a, K, V> LeafRange<marker::ValMut<'a>, K, V> {
58 #[inline]
59 pub(super) fn next_checked(&mut self) -> Option<(&'a K, &'a mut V)> {
60 self.perform_next_checked(|kv| unsafe { ptr::read(kv) }.into_kv_valmut())
62 }
63
64 #[inline]
65 pub(super) fn next_back_checked(&mut self) -> Option<(&'a K, &'a mut V)> {
66 self.perform_next_back_checked(|kv| unsafe { ptr::read(kv) }.into_kv_valmut())
68 }
69}
70
71impl<BorrowType: marker::BorrowType, K, V> LeafRange<BorrowType, K, V> {
72 fn perform_next_checked<F, R>(&mut self, f: F) -> Option<R>
74 where
75 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
76 {
77 if self.is_empty() {
78 None
79 } else {
80 Some(unsafe { self.front.as_mut().unwrap().perform_next_unchecked(f) })
82 }
83 }
84
85 fn perform_next_back_checked<F, R>(&mut self, f: F) -> Option<R>
87 where
88 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
89 {
90 if self.is_empty() {
91 None
92 } else {
93 Some(unsafe { self.back.as_mut().unwrap().perform_next_back_unchecked(f) })
95 }
96 }
97}
98
99enum LazyLeafHandle<BorrowType, K, V> {
100 Root(NodeRef<BorrowType, K, V, marker::LeafOrInternal>), Edge(Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>),
102}
103
104impl<'a, K: 'a, V: 'a> Clone for LazyLeafHandle<marker::Immut<'a>, K, V> {
105 fn clone(&self) -> Self {
106 match self {
107 LazyLeafHandle::Root(root) => LazyLeafHandle::Root(*root),
108 LazyLeafHandle::Edge(edge) => LazyLeafHandle::Edge(*edge),
109 }
110 }
111}
112
113impl<BorrowType, K, V> LazyLeafHandle<BorrowType, K, V> {
114 fn reborrow(&self) -> LazyLeafHandle<marker::Immut<'_>, K, V> {
115 match self {
116 LazyLeafHandle::Root(root) => LazyLeafHandle::Root(root.reborrow()),
117 LazyLeafHandle::Edge(edge) => LazyLeafHandle::Edge(edge.reborrow()),
118 }
119 }
120}
121
122pub(super) struct LazyLeafRange<BorrowType, K, V> {
124 front: Option<LazyLeafHandle<BorrowType, K, V>>,
125 back: Option<LazyLeafHandle<BorrowType, K, V>>,
126}
127
128impl<B, K, V> Default for LazyLeafRange<B, K, V> {
129 fn default() -> Self {
130 LazyLeafRange { front: None, back: None }
131 }
132}
133
134impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
135 fn clone(&self) -> Self {
136 LazyLeafRange { front: self.front.clone(), back: self.back.clone() }
137 }
138}
139
140impl<BorrowType, K, V> LazyLeafRange<BorrowType, K, V> {
141 pub(super) fn none() -> Self {
142 LazyLeafRange { front: None, back: None }
143 }
144
145 pub(super) fn reborrow(&self) -> LazyLeafRange<marker::Immut<'_>, K, V> {
147 LazyLeafRange {
148 front: self.front.as_ref().map(|f| f.reborrow()),
149 back: self.back.as_ref().map(|b| b.reborrow()),
150 }
151 }
152}
153
154impl<'a, K, V> LazyLeafRange<marker::Immut<'a>, K, V> {
155 #[inline]
156 pub(super) unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
157 unsafe { self.init_front().unwrap().next_unchecked() }
159 }
160
161 #[inline]
162 pub(super) unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
163 unsafe { self.init_back().unwrap().next_back_unchecked() }
165 }
166}
167
168impl<'a, K, V> LazyLeafRange<marker::ValMut<'a>, K, V> {
169 #[inline]
170 pub(super) unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
171 unsafe { self.init_front().unwrap().next_unchecked() }
173 }
174
175 #[inline]
176 pub(super) unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
177 unsafe { self.init_back().unwrap().next_back_unchecked() }
179 }
180}
181
182impl<K, V> LazyLeafRange<marker::Dying, K, V> {
183 fn take_front(
184 &mut self,
185 ) -> Option<Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge>> {
186 match self.front.take()? {
187 LazyLeafHandle::Root(root) => Some(root.first_leaf_edge()),
188 LazyLeafHandle::Edge(edge) => Some(edge),
189 }
190 }
191
192 #[inline]
193 pub(super) unsafe fn deallocating_next_unchecked<A: AllocatorClone>(
194 &mut self,
195 alloc: A,
196 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
197 if true {
if !self.front.is_some() {
::core::panicking::panic("assertion failed: self.front.is_some()")
};
};debug_assert!(self.front.is_some());
198 let front = self.init_front().unwrap();
199 unsafe { front.deallocating_next_unchecked(alloc) }
201 }
202
203 #[inline]
204 pub(super) unsafe fn deallocating_next_back_unchecked<A: AllocatorClone>(
205 &mut self,
206 alloc: A,
207 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
208 if true {
if !self.back.is_some() {
::core::panicking::panic("assertion failed: self.back.is_some()")
};
};debug_assert!(self.back.is_some());
209 let back = self.init_back().unwrap();
210 unsafe { back.deallocating_next_back_unchecked(alloc) }
212 }
213
214 #[inline]
215 pub(super) fn deallocating_end<A: AllocatorClone>(&mut self, alloc: A) {
216 if let Some(front) = self.take_front() {
217 front.deallocating_end(alloc)
218 }
219 }
220}
221
222impl<BorrowType: marker::BorrowType, K, V> LazyLeafRange<BorrowType, K, V> {
223 fn init_front(
224 &mut self,
225 ) -> Option<&mut Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>> {
226 if let Some(LazyLeafHandle::Root(root)) = &self.front {
227 self.front = Some(LazyLeafHandle::Edge(unsafe { ptr::read(root) }.first_leaf_edge()));
229 }
230 match &mut self.front {
231 None => None,
232 Some(LazyLeafHandle::Edge(edge)) => Some(edge),
233 Some(LazyLeafHandle::Root(_)) => unsafe { hint::unreachable_unchecked() },
235 }
236 }
237
238 fn init_back(
239 &mut self,
240 ) -> Option<&mut Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>> {
241 if let Some(LazyLeafHandle::Root(root)) = &self.back {
242 self.back = Some(LazyLeafHandle::Edge(unsafe { ptr::read(root) }.last_leaf_edge()));
244 }
245 match &mut self.back {
246 None => None,
247 Some(LazyLeafHandle::Edge(edge)) => Some(edge),
248 Some(LazyLeafHandle::Root(_)) => unsafe { hint::unreachable_unchecked() },
250 }
251 }
252}
253
254impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
255 unsafe fn find_leaf_edges_spanning_range<Q: ?Sized, R>(
269 self,
270 range: R,
271 ) -> LeafRange<BorrowType, K, V>
272 where
273 Q: Ord,
274 K: Borrow<Q>,
275 R: RangeBounds<Q>,
276 {
277 match self.search_tree_for_bifurcation(&range) {
278 Err(_) => LeafRange::none(),
279 Ok((
280 node,
281 lower_edge_idx,
282 upper_edge_idx,
283 mut lower_child_bound,
284 mut upper_child_bound,
285 )) => {
286 let mut lower_edge = unsafe { Handle::new_edge(ptr::read(&node), lower_edge_idx) };
288 let mut upper_edge = unsafe { Handle::new_edge(node, upper_edge_idx) };
290 loop {
291 match (lower_edge.force(), upper_edge.force()) {
292 (Leaf(f), Leaf(b)) => return LeafRange { front: Some(f), back: Some(b) },
293 (Internal(f), Internal(b)) => {
294 (lower_edge, lower_child_bound) =
295 f.descend().find_lower_bound_edge(lower_child_bound);
296 (upper_edge, upper_child_bound) =
297 b.descend().find_upper_bound_edge(upper_child_bound);
298 }
299 _ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("BTreeMap has different depths")));
}unreachable!("BTreeMap has different depths"),
300 }
301 }
302 }
303 }
304 }
305}
306
307fn full_range<BorrowType: marker::BorrowType, K, V>(
308 root1: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
309 root2: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
310) -> LazyLeafRange<BorrowType, K, V> {
311 LazyLeafRange {
312 front: Some(LazyLeafHandle::Root(root1)),
313 back: Some(LazyLeafHandle::Root(root2)),
314 }
315}
316
317impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
318 pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::Immut<'a>, K, V>
323 where
324 Q: ?Sized + Ord,
325 K: Borrow<Q>,
326 R: RangeBounds<Q>,
327 {
328 unsafe { self.find_leaf_edges_spanning_range(range) }
330 }
331
332 pub(super) fn full_range(self) -> LazyLeafRange<marker::Immut<'a>, K, V> {
334 full_range(self, self)
335 }
336}
337
338impl<'a, K: 'a, V: 'a> NodeRef<marker::ValMut<'a>, K, V, marker::LeafOrInternal> {
339 pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::ValMut<'a>, K, V>
349 where
350 Q: ?Sized + Ord,
351 K: Borrow<Q>,
352 R: RangeBounds<Q>,
353 {
354 unsafe { self.find_leaf_edges_spanning_range(range) }
356 }
357
358 pub(super) fn full_range(self) -> LazyLeafRange<marker::ValMut<'a>, K, V> {
362 let self2 = unsafe { ptr::read(&self) };
365 full_range(self, self2)
366 }
367}
368
369impl<K, V> NodeRef<marker::Dying, K, V, marker::LeafOrInternal> {
370 pub(super) fn full_range(self) -> LazyLeafRange<marker::Dying, K, V> {
374 let self2 = unsafe { ptr::read(&self) };
377 full_range(self, self2)
378 }
379}
380
381impl<BorrowType: marker::BorrowType, K, V>
382 Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
383{
384 pub(super) fn next_kv(
388 self,
389 ) -> Result<
390 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>,
391 NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
392 > {
393 let mut edge = self.forget_node_type();
394 loop {
395 edge = match edge.right_kv() {
396 Ok(kv) => return Ok(kv),
397 Err(last_edge) => match last_edge.into_node().ascend() {
398 Ok(parent_edge) => parent_edge.forget_node_type(),
399 Err(root) => return Err(root),
400 },
401 }
402 }
403 }
404
405 pub(super) fn next_back_kv(
409 self,
410 ) -> Result<
411 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>,
412 NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
413 > {
414 let mut edge = self.forget_node_type();
415 loop {
416 edge = match edge.left_kv() {
417 Ok(kv) => return Ok(kv),
418 Err(last_edge) => match last_edge.into_node().ascend() {
419 Ok(parent_edge) => parent_edge.forget_node_type(),
420 Err(root) => return Err(root),
421 },
422 }
423 }
424 }
425}
426
427impl<BorrowType: marker::BorrowType, K, V>
428 Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>
429{
430 fn next_kv(
434 self,
435 ) -> Result<
436 Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::KV>,
437 NodeRef<BorrowType, K, V, marker::Internal>,
438 > {
439 let mut edge = self;
440 loop {
441 edge = match edge.right_kv() {
442 Ok(internal_kv) => return Ok(internal_kv),
443 Err(last_edge) => match last_edge.into_node().ascend() {
444 Ok(parent_edge) => parent_edge,
445 Err(root) => return Err(root),
446 },
447 }
448 }
449 }
450}
451
452impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
453 unsafe fn deallocating_next<A: AllocatorClone>(
467 self,
468 alloc: A,
469 ) -> Option<(Self, Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV>)>
470 {
471 let mut edge = self.forget_node_type();
472 loop {
473 edge = match edge.right_kv() {
474 Ok(kv) => return Some((unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)),
476 Err(last_edge) => {
477 match unsafe { last_edge.into_node().deallocate_and_ascend(alloc.clone()) } {
479 Some(parent_edge) => parent_edge.forget_node_type(),
480 None => return None,
481 }
482 }
483 }
484 }
485 }
486
487 unsafe fn deallocating_next_back<A: AllocatorClone>(
501 self,
502 alloc: A,
503 ) -> Option<(Self, Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV>)>
504 {
505 let mut edge = self.forget_node_type();
506 loop {
507 edge = match edge.left_kv() {
508 Ok(kv) => return Some((unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)),
510 Err(last_edge) => {
511 match unsafe { last_edge.into_node().deallocate_and_ascend(alloc.clone()) } {
513 Some(parent_edge) => parent_edge.forget_node_type(),
514 None => return None,
515 }
516 }
517 }
518 }
519 }
520
521 fn deallocating_end<A: AllocatorClone>(self, alloc: A) {
528 let mut edge = self.forget_node_type();
529 while let Some(parent_edge) =
530 unsafe { edge.into_node().deallocate_and_ascend(alloc.clone()) }
532 {
533 edge = parent_edge.forget_node_type();
534 }
535 }
536}
537
538impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Edge> {
539 unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
545 unsafe { self.perform_next_unchecked(|node| node.into_kv()) }
547 }
548
549 unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
555 unsafe { self.perform_next_back_unchecked(|node| node.into_kv()) }
557 }
558}
559
560impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::Edge> {
561 unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
569 unsafe { self.perform_next_unchecked(|kv| ptr::read(kv).into_kv_valmut()) }
571 }
572
573 unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
581 unsafe { self.perform_next_back_unchecked(|kv| ptr::read(kv).into_kv_valmut()) }
583 }
584}
585
586impl<BorrowType: marker::BorrowType, K, V>
587 Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
588{
589 unsafe fn perform_next_unchecked<F, R>(&mut self, f: F) -> R
594 where
595 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
596 {
597 let idx = self.idx();
598 let len = self.reborrow().into_node().len();
599 if idx < len {
601 let node = unsafe { ptr::read(self) }.into_node();
603 *self = unsafe { Handle::new_edge(ptr::read(&node), idx + 1) };
606 f(&unsafe { Handle::new_kv(node, idx) }.forget_node_type())
608 } else {
609 let kv = super::mem::replace(self, |leaf_edge| {
610 let kv = leaf_edge.next_kv().ok().unwrap();
611 (unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)
613 });
614 f(&kv)
615 }
616 }
617
618 unsafe fn perform_next_back_unchecked<F, R>(&mut self, f: F) -> R
623 where
624 F: Fn(&Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>) -> R,
625 {
626 let idx = self.idx();
627 if idx > 0 {
629 let node = unsafe { ptr::read(self) }.into_node();
631 *self = unsafe { Handle::new_edge(ptr::read(&node), idx - 1) };
634 f(&unsafe { Handle::new_kv(node, idx - 1) }.forget_node_type())
636 } else {
637 let kv = super::mem::replace(self, |leaf_edge| {
638 let kv = leaf_edge.next_back_kv().ok().unwrap();
639 (unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)
641 });
642 f(&kv)
643 }
644 }
645}
646
647impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
648 unsafe fn deallocating_next_unchecked<A: AllocatorClone>(
661 &mut self,
662 alloc: A,
663 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
664 super::mem::replace(self, |leaf_edge| unsafe {
666 leaf_edge.deallocating_next(alloc).unwrap()
667 })
668 }
669
670 unsafe fn deallocating_next_back_unchecked<A: AllocatorClone>(
683 &mut self,
684 alloc: A,
685 ) -> Handle<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>, marker::KV> {
686 super::mem::replace(self, |leaf_edge| unsafe {
688 leaf_edge.deallocating_next_back(alloc).unwrap()
689 })
690 }
691}
692
693impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
694 #[inline]
697 pub(super) fn first_leaf_edge(
698 self,
699 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
700 let mut node = self;
701 loop {
702 match node.force() {
703 Leaf(leaf) => return leaf.first_edge(),
704 Internal(internal) => node = internal.first_edge().descend(),
705 }
706 }
707 }
708
709 #[inline]
712 pub(super) fn last_leaf_edge(
713 self,
714 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
715 let mut node = self;
716 loop {
717 match node.force() {
718 Leaf(leaf) => return leaf.last_edge(),
719 Internal(internal) => node = internal.last_edge().descend(),
720 }
721 }
722 }
723}
724
725pub(super) enum Position<BorrowType, K, V> {
726 Leaf(NodeRef<BorrowType, K, V, marker::Leaf>),
727 Internal(NodeRef<BorrowType, K, V, marker::Internal>),
728 InternalKV,
729}
730
731impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
732 pub(super) fn visit_nodes_in_order<F>(self, mut visit: F)
736 where
737 F: FnMut(Position<marker::Immut<'a>, K, V>),
738 {
739 match self.force() {
740 Leaf(leaf) => visit(Position::Leaf(leaf)),
741 Internal(internal) => {
742 visit(Position::Internal(internal));
743 let mut edge = internal.first_edge();
744 loop {
745 edge = match edge.descend().force() {
746 Leaf(leaf) => {
747 visit(Position::Leaf(leaf));
748 match edge.next_kv() {
749 Ok(kv) => {
750 visit(Position::InternalKV);
751 kv.right_edge()
752 }
753 Err(_) => return,
754 }
755 }
756 Internal(internal) => {
757 visit(Position::Internal(internal));
758 internal.first_edge()
759 }
760 }
761 }
762 }
763 }
764 }
765
766 pub(super) fn calc_length(self) -> usize {
768 let mut result = 0;
769 self.visit_nodes_in_order(|pos| match pos {
770 Position::Leaf(node) => result += node.len(),
771 Position::Internal(node) => result += node.len(),
772 Position::InternalKV => (),
773 });
774 result
775 }
776}
777
778impl<BorrowType: marker::BorrowType, K, V>
779 Handle<NodeRef<BorrowType, K, V, marker::LeafOrInternal>, marker::KV>
780{
781 pub(super) fn next_leaf_edge(
783 self,
784 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
785 match self.force() {
786 Leaf(leaf_kv) => leaf_kv.right_edge(),
787 Internal(internal_kv) => {
788 let next_internal_edge = internal_kv.right_edge();
789 next_internal_edge.descend().first_leaf_edge()
790 }
791 }
792 }
793
794 pub(super) fn next_back_leaf_edge(
796 self,
797 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge> {
798 match self.force() {
799 Leaf(leaf_kv) => leaf_kv.left_edge(),
800 Internal(internal_kv) => {
801 let next_internal_edge = internal_kv.left_edge();
802 next_internal_edge.descend().last_leaf_edge()
803 }
804 }
805 }
806}
807
808impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
809 pub(super) fn lower_bound<Q: ?Sized>(
812 self,
813 mut bound: SearchBound<&Q>,
814 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
815 where
816 Q: Ord,
817 K: Borrow<Q>,
818 {
819 let mut node = self;
820 loop {
821 let (edge, new_bound) = node.find_lower_bound_edge(bound);
822 match edge.force() {
823 Leaf(edge) => return edge,
824 Internal(edge) => {
825 node = edge.descend();
826 bound = new_bound;
827 }
828 }
829 }
830 }
831
832 pub(super) fn upper_bound<Q: ?Sized>(
835 self,
836 mut bound: SearchBound<&Q>,
837 ) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
838 where
839 Q: Ord,
840 K: Borrow<Q>,
841 {
842 let mut node = self;
843 loop {
844 let (edge, new_bound) = node.find_upper_bound_edge(bound);
845 match edge.force() {
846 Leaf(edge) => return edge,
847 Internal(edge) => {
848 node = edge.descend();
849 bound = new_bound;
850 }
851 }
852 }
853 }
854}
855
856impl<K, V> NodeRef<marker::Dying, K, V, marker::LeafOrInternal> {
857 pub(super) unsafe fn drop_tree<A: AllocatorClone>(self, alloc: A) {
861 struct DropGuard<K, V, A: AllocatorClone> {
862 node: Option<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>>,
863 alloc: A,
864 }
865
866 impl<K, V, A: AllocatorClone> Drop for DropGuard<K, V, A> {
867 fn drop(&mut self) {
868 if let Some(cur) = self.node.take() {
869 let mut next =
870 unsafe { deallocate_and_next_postorder_node(cur, self.alloc.clone()) };
874
875 while let Some(mut node) = next {
876 unsafe { node.drop_kvs() };
878 next =
879 unsafe { deallocate_and_next_postorder_node(node, self.alloc.clone()) }
882 }
883 }
884 }
885 }
886 unsafe fn deallocate_and_next_postorder_node<K, V, A: AllocatorClone>(
894 node: NodeRef<marker::Dying, K, V, marker::LeafOrInternal>,
895 alloc: A,
896 ) -> Option<NodeRef<marker::Dying, K, V, marker::LeafOrInternal>> {
897 let parent = unsafe { node.deallocate_and_ascend(alloc) }?;
899
900 Some(match parent.right_kv() {
901 Ok(kv) => kv.right_edge().descend().first_leaf_edge().into_node().forget_type(),
902 Err(last_edge) => last_edge.into_node().forget_type(),
903 })
904 }
905
906 let node = self.first_leaf_edge().into_node().forget_type();
907 let mut guard = DropGuard { node: Some(node), alloc };
908 loop {
909 unsafe { guard.node.as_mut().unwrap().drop_kvs() };
911 if let Some(next) = unsafe {
913 deallocate_and_next_postorder_node(guard.node.take().unwrap(), guard.alloc.clone())
914 } {
915 guard.node = Some(next);
916 } else {
917 return;
918 }
919 }
920 }
921}