1use core::borrow::Borrow;
87use core::cell::UnsafeCell;
88use core::fmt::Debug;
89use core::ops::{Bound, RangeBounds};
90use core::ptr::NonNull;
91mod entry;
92pub use entry::*;
93mod helper;
94use helper::*;
95mod node;
96use node::*;
97mod inter;
98use inter::*;
99mod leaf;
100use leaf::*;
101mod iter;
102#[allow(unused_imports)]
103use crate::{print_log, trace_log};
104use iter::RangeBase;
105pub use iter::{IntoIter, Iter, IterMut, Keys, Range, RangeMut, Values, ValuesMut};
106
107#[cfg(test)]
108mod tests;
109
110pub struct BTreeMap<K: Ord + Clone + Sized, V: Sized> {
112 root: Option<NonNull<NodeHeader>>,
115 len: usize,
117 _info: UnsafeCell<Option<TreeInfo<K, V>>>,
119 #[cfg(all(test, feature = "trace_log"))]
120 triggers: u32,
121}
122
123#[cfg(all(test, feature = "trace_log"))]
124#[repr(u32)]
125enum TestFlag {
126 LeafSplit = 1,
127 InterSplit = 1 << 1,
128 LeafMoveLeft = 1 << 2,
129 LeafMoveRight = 1 << 3,
130 LeafMergeLeft = 1 << 4,
131 LeafMergeRight = 1 << 5,
132 InterMoveLeft = 1 << 6,
133 InterMoveLeftFirst = 1 << 7,
134 InterMoveRight = 1 << 8,
135 InterMoveRightLast = 1 << 9,
136 InterMergeLeft = 1 << 10,
137 InterMergeRight = 1 << 11,
138 UpdateSepKey = 1 << 12,
139 RemoveOnlyChild = 1 << 13,
140 RemoveChildFirst = 1 << 14,
141 RemoveChildMid = 1 << 15,
142 RemoveChildLast = 1 << 16,
143}
144
145unsafe impl<K: Ord + Clone + Sized + Send, V: Sized + Send> Send for BTreeMap<K, V> {}
146unsafe impl<K: Ord + Clone + Sized + Send, V: Sized + Send> Sync for BTreeMap<K, V> {}
147
148#[cfg(feature = "std")]
149impl<K: Ord + Clone + Sized, V: Sized> std::panic::RefUnwindSafe for BTreeMap<K, V> {}
150
151impl<K: Ord + Sized + Clone, V: Sized> BTreeMap<K, V> {
152 pub fn new() -> Self {
154 Self {
155 root: None,
156 len: 0,
157 _info: UnsafeCell::new(None),
158 #[cfg(all(test, feature = "trace_log"))]
159 triggers: 0,
160 }
161 }
162
163 #[inline(always)]
165 pub fn len(&self) -> usize {
166 self.len
167 }
168
169 #[inline(always)]
171 pub fn is_empty(&self) -> bool {
172 self.len == 0
173 }
174
175 #[inline]
177 pub const fn cap() -> (u32, u32) {
178 let inter_cap = InterNode::<K, V>::cap();
179 let leaf_cap = LeafNode::<K, V>::cap();
180 (inter_cap, leaf_cap)
181 }
182
183 #[inline(always)]
185 pub fn leaf_count(&self) -> usize {
186 if self.root.is_none() {
187 return 0;
188 };
189 if let Some(info) = self._get_info().as_ref() { info.leaf_count() } else { 1 }
190 }
191
192 #[inline(always)]
194 pub fn inter_count(&self) -> usize {
195 if let Some(info) = self._get_info().as_ref() { info.inter_count() as usize } else { 0 }
196 }
197
198 #[inline]
199 pub fn memory_used(&self) -> usize {
200 (self.leaf_count() + self.inter_count()) * NODE_SIZE
201 }
202
203 #[cfg(all(test, feature = "std", feature = "trace_log"))]
204 pub fn print_trigger_flags(&self) {
205 let mut s = String::from("");
206 if self.triggers & TestFlag::InterSplit as u32 > 0 {
207 s += "InterSplit,";
208 }
209 if self.triggers & TestFlag::LeafSplit as u32 > 0 {
210 s += "LeafSplit,";
211 }
212 if self.triggers & TestFlag::LeafMoveLeft as u32 > 0 {
213 s += "LeafMoveLeft,";
214 }
215 if self.triggers & TestFlag::LeafMoveRight as u32 > 0 {
216 s += "LeafMoveRight,";
217 }
218 if self.triggers & TestFlag::InterMoveLeft as u32 > 0 {
219 s += "InterMoveLeft,";
220 }
221 if self.triggers & TestFlag::InterMoveRight as u32 > 0 {
222 s += "InterMoveRight,";
223 }
224 if s.len() > 0 {
225 print_log!("{s}");
226 }
227 let mut s = String::from("");
228 if self.triggers & TestFlag::InterMergeLeft as u32 > 0 {
229 s += "InterMergeLeft,";
230 }
231 if self.triggers & TestFlag::InterMergeRight as u32 > 0 {
232 s += "InterMergeRight,";
233 }
234 if self.triggers & TestFlag::RemoveOnlyChild as u32 > 0 {
235 s += "RemoveOnlyChild,";
236 }
237 if self.triggers
238 & (TestFlag::RemoveChildFirst as u32
239 | TestFlag::RemoveChildMid as u32
240 | TestFlag::RemoveChildLast as u32)
241 > 0
242 {
243 s += "RemoveChild,";
244 }
245 if s.len() > 0 {
246 print_log!("{s}");
247 }
248 }
249
250 #[inline]
254 pub fn get_fill_ratio(&self) -> f32 {
255 if self.len == 0 {
256 0.0
257 } else {
258 let cap = LeafNode::<K, V>::cap() as usize * self.leaf_count();
259 self.len as f32 / cap as f32 * 100.0
260 }
261 }
262
263 #[inline(always)]
265 pub fn height(&self) -> u32 {
266 if let Some(root) = self.get_root() {
267 return root.height() + 1;
268 }
269 1
270 }
271
272 #[inline(always)]
273 fn _get_info(&self) -> &mut Option<TreeInfo<K, V>> {
274 unsafe { &mut *self._info.get() }
275 }
276
277 #[inline(always)]
278 fn get_info_mut(&self) -> &mut TreeInfo<K, V> {
279 self._get_info().as_mut().unwrap()
280 }
281
282 #[inline(always)]
283 fn clear_cache(&self) -> &mut TreeInfo<K, V> {
284 let cache = self.get_info_mut();
285 cache.clear();
286 cache
287 }
288
289 #[inline(always)]
290 fn take_cache(&mut self) -> Option<TreeInfo<K, V>> {
291 let mut cache = self._get_info().take()?;
292 cache.clear();
293 Some(cache)
294 }
295
296 #[inline]
298 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
299 let mut is_seq = true;
300 if let Some(leaf) = self.search_leaf_with(|inter| {
301 inter.find_leaf_with_cache_smart(self.clear_cache(), &key, &mut is_seq)
302 }) {
303 let (idx, is_equal) = leaf.search_smart(&key, is_seq);
304 if is_equal {
305 Entry::Occupied(OccupiedEntry { tree: self, idx, leaf })
306 } else {
307 Entry::Vacant(VacantEntry { tree: self, key, idx, leaf: Some(leaf) })
308 }
309 } else {
310 return Entry::Vacant(VacantEntry { tree: self, key, idx: 0, leaf: None });
311 }
312 }
313
314 #[inline(always)]
315 fn find<Q>(&self, key: &Q) -> Option<(LeafNode<K, V>, u32)>
316 where
317 K: Borrow<Q>,
318 Q: Ord + ?Sized,
319 {
320 let leaf = self.search_leaf_with(|inter| inter.find_leaf(key))?;
321 let (idx, is_equal) = leaf.search(key);
322 trace_log!("find leaf {leaf:?} {idx} exist {is_equal}");
323 if is_equal { Some((leaf, idx)) } else { None }
324 }
325
326 #[inline(always)]
328 pub fn contains_key<Q>(&self, key: &Q) -> bool
329 where
330 K: Borrow<Q>,
331 Q: Ord + ?Sized,
332 {
333 self.find::<Q>(key).is_some()
334 }
335
336 pub fn get<Q>(&self, key: &Q) -> Option<&V>
338 where
339 K: Borrow<Q>,
340 Q: Ord + ?Sized,
341 {
342 if let Some((leaf, idx)) = self.find::<Q>(key) {
343 let value = unsafe { leaf.value_ptr(idx) };
344 debug_assert!(!value.is_null());
345 Some(unsafe { (*value).assume_init_ref() })
346 } else {
347 None
348 }
349 }
350
351 pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
353 where
354 K: Borrow<Q>,
355 Q: Ord + ?Sized,
356 {
357 if let Some((leaf, idx)) = self.find::<Q>(key) {
358 let value = unsafe { leaf.value_ptr(idx) };
359 debug_assert!(!value.is_null());
360 Some(unsafe { (*value).assume_init_mut() })
361 } else {
362 None
363 }
364 }
365
366 #[inline]
367 fn init_empty<'a>(&'a mut self, key: K, value: V) -> &'a mut V {
368 debug_assert!(self.root.is_none());
369 unsafe {
370 let mut leaf = LeafNode::<K, V>::alloc();
372 self.root = Some(leaf.to_root_ptr());
373 self.len = 1;
374 return &mut *leaf.insert_no_split_with_idx(0, key, value);
375 }
376 }
377
378 #[inline]
381 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
382 let mut is_seq = true;
383 if let Some(mut leaf) = self.search_leaf_with(|inter| {
384 inter.find_leaf_with_cache_smart(self.clear_cache(), &key, &mut is_seq)
385 }) {
386 let (idx, is_equal) = leaf.search_smart(&key, is_seq);
387 if is_equal {
388 Some(leaf.replace(idx, value))
389 } else {
390 self.len += 1;
391 let count = leaf.key_count();
393 if count < LeafNode::<K, V>::cap() {
395 leaf.insert_no_split_with_idx(idx, key, value);
396 } else {
397 self.insert_with_split(key, value, leaf, idx);
399 }
400 None
401 }
402 } else {
403 self.init_empty(key, value);
404 None
405 }
406 }
407
408 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
410 where
411 K: Borrow<Q>,
412 Q: Ord + ?Sized,
413 {
414 let mut leaf = self
415 .search_leaf_with(|inter| inter.find_leaf_with_cache::<Q>(self.clear_cache(), key))?;
416 let (idx, is_equal) = leaf.search(key);
417 if is_equal {
418 trace_log!("{leaf:?} remove {idx}");
419 let val = leaf.remove_value_no_borrow(idx);
420 self.len -= 1;
421 let new_count = leaf.key_count();
423 let min_count = LeafNode::<K, V>::cap() >> 1;
424 if new_count < min_count && self.root_is_inter() {
425 self.handle_leaf_underflow(leaf, true);
427 }
428 Some(val)
429 } else {
430 None
431 }
432 }
433
434 #[inline(always)]
435 fn root_is_inter(&self) -> bool {
436 if let Some(root) = self.root { !Node::<K, V>::root_is_leaf(root) } else { false }
437 }
438
439 pub fn remove_range<R>(&mut self, range: R) -> Option<(K, V)>
443 where
444 R: RangeBounds<K>,
445 {
446 self.remove_range_with::<R, _>(range, |_, _| {})
447 }
448
449 #[inline]
455 pub fn remove_range_with<R, F>(&mut self, range: R, mut cb: F) -> Option<(K, V)>
456 where
457 R: RangeBounds<K>,
458 F: FnMut(&K, &V),
459 {
460 macro_rules! end_contains {
461 ($key: expr) => {{
462 match range.end_bound() {
463 Bound::Excluded(k) => $key < k,
464 Bound::Included(k) => $key <= k,
465 Bound::Unbounded => true,
466 }
467 }};
468 }
469 let mut ent = match range.start_bound() {
471 Bound::Excluded(k) => match self.entry(k.clone()).move_forward() {
472 Ok(ent) => {
473 if end_contains!(ent.key()) {
474 ent
475 } else {
476 return None;
477 }
478 }
479 Err(_) => return None,
480 },
481 Bound::Included(k) => match self.entry(k.clone()) {
482 Entry::Occupied(ent) => ent,
483 Entry::Vacant(ent) => match ent.move_forward() {
484 Ok(ent) => {
485 if end_contains!(ent.key()) {
486 ent
487 } else {
488 return None;
489 }
490 }
491 Err(_) => return None,
492 },
493 },
494 Bound::Unbounded => {
495 if let Some(ent) = self.first_entry() {
496 ent
497 } else {
498 return None;
499 }
500 }
501 };
502 loop {
503 if let Some((_next_k, _next_v)) = ent.peak_forward() {
504 if end_contains!(_next_k) {
505 let next_key = _next_k.clone();
506 let (_k, _v) = ent._remove_entry(false);
507 cb(&_k, &_v);
508 if let Entry::Occupied(_ent) = self.entry(next_key) {
509 ent = _ent;
510 continue;
511 } else {
512 unreachable!();
513 }
514 }
515 }
516 let (_k, _v) = ent._remove_entry(true);
517 cb(&_k, &_v);
518 return Some((_k, _v));
519 }
520 }
521
522 #[inline(always)]
523 fn get_root_unwrap(&self) -> Node<K, V> {
524 Node::<K, V>::from_root_ptr(*self.root.as_ref().unwrap())
525 }
526
527 #[inline(always)]
528 fn get_root(&self) -> Option<Node<K, V>> {
529 Some(Node::<K, V>::from_root_ptr(*self.root.as_ref()?))
530 }
531
532 #[inline(always)]
534 fn search_leaf_with<F>(&self, search: F) -> Option<LeafNode<K, V>>
535 where
536 F: FnOnce(InterNode<K, V>) -> LeafNode<K, V>,
537 {
538 let root = self.root?;
539 if !Node::<K, V>::root_is_leaf(root) {
540 Some(search(InterNode::<K, V>::from(root)))
541 } else {
542 Some(LeafNode::<K, V>::from_root_ptr(root))
543 }
544 }
545
546 #[inline(always)]
548 fn update_ancestor_sep_key<const MOVE: bool>(&mut self, sep_key: K) {
549 let cache = self.get_info_mut();
552 let ret = if MOVE {
553 cache.move_to_ancenstor(|_node, idx| -> bool { idx > 0 }, dummy_post_callback)
554 } else {
555 cache.peak_ancenstor(|_node, idx| -> bool { idx > 0 })
556 };
557 if let Some((parent, parent_idx)) = ret {
558 trace_log!("update_ancestor_sep_key move={MOVE} at {parent:?}:{}", parent_idx - 1);
559 parent.change_key(parent_idx - 1, sep_key);
560 #[cfg(all(test, feature = "trace_log"))]
561 {
562 self.triggers |= TestFlag::UpdateSepKey as u32;
563 }
564 }
565 }
566
567 fn insert_with_split(
569 &mut self, key: K, value: V, mut leaf: LeafNode<K, V>, idx: u32,
570 ) -> *mut V {
571 debug_assert!(leaf.is_full());
572 let cap = LeafNode::<K, V>::cap();
573 if idx < cap {
574 if let Some(mut left_node) = leaf.get_left_node()
576 && !left_node.is_full()
577 {
578 trace_log!("insert {leaf:?}:{idx} borrow left {left_node:?}");
579 let val_p = if idx == 0 {
580 left_node.insert_no_split_with_idx(left_node.key_count(), key, value)
583 } else {
584 leaf.insert_borrow_left(&mut left_node, idx, key, value)
585 };
586 #[cfg(all(test, feature = "trace_log"))]
587 {
588 self.triggers |= TestFlag::LeafMoveLeft as u32;
589 }
590 self.update_ancestor_sep_key::<true>(leaf.clone_first_key());
591 return val_p;
592 }
593 } else {
594 }
596 if let Some(mut right_node) = leaf.get_right_node()
597 && !right_node.is_full()
598 {
599 trace_log!("insert {leaf:?}:{idx} borrow right {right_node:?}");
600 let val_p = if idx == cap {
601 right_node.insert_no_split_with_idx(0, key, value)
604 } else {
605 leaf.borrow_right(&mut right_node);
606 leaf.insert_no_split_with_idx(idx, key, value)
607 };
608 #[cfg(all(test, feature = "trace_log"))]
609 {
610 self.triggers |= TestFlag::LeafMoveRight as u32;
611 }
612 self.get_info_mut().move_right();
613 self.update_ancestor_sep_key::<true>(right_node.clone_first_key());
614 return val_p;
615 }
616 #[cfg(all(test, feature = "trace_log"))]
617 {
618 self.triggers |= TestFlag::LeafSplit as u32;
619 }
620 let (new_leaf, ptr_v) = leaf.insert_with_split(idx, key, value);
621 let split_key = unsafe { (*new_leaf.key_ptr(0)).assume_init_ref().clone() };
622
623 let o_info = self._get_info();
624 if let Some(info) = o_info.as_mut() {
625 info.inc_leaf_count();
626 match self.propagate_split(info, split_key, leaf.get_ptr(), new_leaf.get_ptr()) {
627 Ok(_flags) => {
628 #[cfg(all(test, feature = "trace_log"))]
629 {
630 self.triggers |= _flags;
631 }
632 }
633 Err(new_root) => {
634 self.root.replace(new_root.to_root_ptr());
635 }
636 }
637 ptr_v
638 } else {
639 o_info.replace(TreeInfo::new(2, 1));
640 let new_root =
641 InterNode::<K, V>::new_root(1, split_key, leaf.get_ptr(), new_leaf.get_ptr());
642 let _old_root = self.root.replace(new_root.to_root_ptr());
643 debug_assert_eq!(_old_root.unwrap(), leaf.to_root_ptr());
644 ptr_v
645 }
646 }
647
648 #[inline(always)]
655 fn propagate_split(
656 &self, info: &mut TreeInfo<K, V>, mut promote_key: K, mut left_ptr: *mut NodeHeader,
657 mut right_ptr: *mut NodeHeader,
658 ) -> Result<u32, InterNode<K, V>> {
659 let mut height = 0;
660 #[allow(unused_mut)]
661 let mut flags = 0;
662 while let Some((mut parent, idx)) = info.pop() {
664 if !parent.is_full() {
665 trace_log!("propagate_split {parent:?}:{idx} insert {right_ptr:p}");
666 parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
668 return Ok(flags);
669 } else {
670 if let Some((mut grand, grand_idx)) = info.peak_parent() {
672 if grand_idx > 0 {
674 let mut left_parent = grand.get_child_as_inter(grand_idx - 1);
675 if !left_parent.is_full() {
676 #[cfg(all(test, feature = "trace_log"))]
677 {
678 flags |= TestFlag::InterMoveLeft as u32;
679 }
680 if idx == 0 {
681 trace_log!(
682 "propagate_split rotate_left {grand:?}:{} first ->{left_parent:?} left {left_ptr:p} insert {idx} {right_ptr:p}",
683 grand_idx - 1
684 );
685 let demote_key = grand.change_key(grand_idx - 1, promote_key);
687 debug_assert_eq!(parent.get_child_ptr(0), left_ptr);
688 unsafe { (*parent.child_ptr(0)) = right_ptr };
689 left_parent.append(demote_key, left_ptr);
690 #[cfg(all(test, feature = "trace_log"))]
691 {
692 flags |= TestFlag::InterMoveLeftFirst as u32;
693 }
694 } else {
695 trace_log!(
696 "propagate_split insert_rotate_left {grand:?}:{grand_idx} -> {left_parent:?} insert {idx} {right_ptr:p}"
697 );
698 parent.insert_rotate_left(
699 &mut grand,
700 grand_idx,
701 &mut left_parent,
702 idx,
703 promote_key,
704 right_ptr,
705 );
706 }
707 return Ok(flags);
708 }
709 }
710 if grand_idx < grand.key_count() {
712 let mut right_parent = grand.get_child_as_inter(grand_idx + 1);
713 if !right_parent.is_full() {
714 #[cfg(all(test, feature = "trace_log"))]
715 {
716 flags |= TestFlag::InterMoveRight as u32;
717 }
718 if idx == parent.key_count() {
719 trace_log!(
720 "propagate_split rotate_right last {grand:?}:{grand_idx} -> {right_parent:?}:0 insert right {right_parent:?}:0 {right_ptr:p}"
721 );
722 let demote_key = grand.change_key(grand_idx, promote_key);
724 right_parent.insert_at_front(right_ptr, demote_key);
725 #[cfg(all(test, feature = "trace_log"))]
726 {
727 flags |= TestFlag::InterMoveRightLast as u32;
728 }
729 } else {
730 trace_log!(
731 "propagate_split rotate_right {grand:?}:{grand_idx} -> {right_parent:?}:0 insert {parent:?}:{idx} {right_ptr:p}"
732 );
733 parent.rotate_right(&mut grand, grand_idx, &mut right_parent);
734 parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
735 }
736 return Ok(flags);
737 }
738 }
739 }
740 height += 1;
741
742 let (right, _promote_key) = parent.insert_split(promote_key, right_ptr);
744 info.inc_inter_count();
745
746 promote_key = _promote_key;
747 right_ptr = right.get_ptr();
748 left_ptr = parent.get_ptr();
749 #[cfg(all(test, feature = "trace_log"))]
750 {
751 flags |= TestFlag::InterSplit as u32;
752 }
753 }
755 }
756
757 info.ensure_cap(height + 1);
758 info.inc_inter_count();
759 let new_root = InterNode::<K, V>::new_root(height + 1, promote_key, left_ptr, right_ptr);
761 #[cfg(debug_assertions)]
763 {
764 let _old_root = self.root.as_ref().unwrap();
765 assert_eq!(_old_root.as_ptr(), left_ptr);
767 }
768 return Err(new_root);
769 }
770
771 fn handle_leaf_underflow(&mut self, mut leaf: LeafNode<K, V>, merge: bool) {
778 debug_assert!(!self.get_root_unwrap().is_leaf());
779 let cur_count = leaf.key_count();
780 let cap = LeafNode::<K, V>::cap();
781 debug_assert!(cur_count <= cap >> 1);
782 let mut can_unlink: bool = false;
783 let (mut left_avail, mut right_avail) = (0, 0);
784 let mut merge_right = false;
785 if cur_count == 0 {
786 trace_log!("handle_leaf_underflow {leaf:?} unlink");
787 can_unlink = true;
789 }
790 if merge {
791 if !can_unlink && let Some(mut left_node) = leaf.get_left_node() {
792 let left_count = left_node.key_count();
793 if left_count + cur_count <= cap {
794 trace_log!(
795 "handle_leaf_underflow {leaf:?} merge left {left_node:?} {cur_count}"
796 );
797 leaf.copy_left(&mut left_node, cur_count);
798 can_unlink = true;
799 #[cfg(all(test, feature = "trace_log"))]
800 {
801 self.triggers |= TestFlag::LeafMergeLeft as u32;
802 }
803 } else {
804 left_avail = cap - left_count;
805 }
806 }
807 if !can_unlink && let Some(mut right_node) = leaf.get_right_node() {
808 let right_count = right_node.key_count();
809 if right_count + cur_count <= cap {
810 trace_log!(
811 "handle_leaf_underflow {leaf:?} merge right {right_node:?} {cur_count}"
812 );
813 leaf.copy_right::<false>(&mut right_node, 0, cur_count);
814 can_unlink = true;
815 merge_right = true;
816 #[cfg(all(test, feature = "trace_log"))]
817 {
818 self.triggers |= TestFlag::LeafMergeRight as u32;
819 }
820 } else {
821 right_avail = cap - right_count;
822 }
823 }
824 if !can_unlink
827 && left_avail > 0
828 && right_avail > 0
829 && left_avail + right_avail == cur_count
830 {
831 let mut left_node = leaf.get_left_node().unwrap();
832 let mut right_node = leaf.get_right_node().unwrap();
833 debug_assert!(left_avail < cur_count);
834 trace_log!("handle_leaf_underflow {leaf:?} merge left {left_node:?} {left_avail}");
835 leaf.copy_left(&mut left_node, left_avail);
836 trace_log!(
837 "handle_leaf_underflow {leaf:?} merge right {right_node:?} {}",
838 cur_count - left_avail
839 );
840 leaf.copy_right::<false>(&mut right_node, left_avail, cur_count - left_avail);
841 merge_right = true;
842 can_unlink = true;
843 #[cfg(all(test, feature = "trace_log"))]
844 {
845 self.triggers |=
846 TestFlag::LeafMergeLeft as u32 | TestFlag::LeafMergeRight as u32;
847 }
848 }
849 }
850 if !can_unlink {
851 return;
852 }
853 self.get_info_mut().dec_leaf_count();
854 let right_sep = if merge_right {
855 let right_node = leaf.get_right_node().unwrap();
856 Some(right_node.clone_first_key())
857 } else {
858 None
859 };
860 let no_right = leaf.unlink().is_null();
861 leaf.dealloc::<false>();
862 let (mut parent, mut idx) = self.get_info_mut().pop().unwrap();
863 trace_log!("handle_leaf_underflow pop parent {parent:?}:{idx}");
864 if parent.key_count() == 0 {
865 if let Some((grand, grand_idx)) = self.remove_only_child(parent) {
866 trace_log!("handle_leaf_underflow remove_only_child until {grand:?}:{grand_idx}");
867 parent = grand;
868 idx = grand_idx;
869 } else {
870 trace_log!("handle_leaf_underflow remove_only_child all");
871 return;
872 }
873 }
874 self.remove_child_from_inter(&mut parent, idx, right_sep, no_right);
875 if parent.key_count() <= 1 {
876 self.handle_inter_underflow(parent);
877 }
878 }
879
880 #[inline]
883 fn remove_child_from_inter(
884 &mut self, node: &mut InterNode<K, V>, delete_idx: u32, right_sep: Option<K>,
885 _no_right: bool,
886 ) {
887 debug_assert!(node.key_count() > 0, "{:?} {}", node, node.key_count());
888 if delete_idx == node.key_count() {
889 trace_log!("remove_child_from_inter {node:?}:{delete_idx} last");
890 #[cfg(all(test, feature = "trace_log"))]
891 {
892 self.triggers |= TestFlag::RemoveChildLast as u32;
893 }
894 node.remove_last_child();
896 if let Some(key) = right_sep
897 && let Some((grand_parent, grand_idx)) = self.get_info_mut().peak_ancenstor(
898 |_node: &InterNode<K, V>, idx: u32| -> bool { _node.key_count() > idx },
899 )
900 {
901 #[cfg(all(test, feature = "trace_log"))]
902 {
903 self.triggers |= TestFlag::UpdateSepKey as u32;
904 }
905 trace_log!("remove_child_from_inter change_key {grand_parent:?}:{grand_idx}");
906 grand_parent.change_key(grand_idx, key);
908 }
909 } else if delete_idx > 0 {
910 trace_log!("remove_child_from_inter {node:?}:{delete_idx} mid");
911 node.remove_mid_child(delete_idx);
912 #[cfg(all(test, feature = "trace_log"))]
913 {
914 self.triggers |= TestFlag::RemoveChildMid as u32;
915 }
916 if let Some(key) = right_sep {
918 trace_log!("remove_child_from_inter change_key {node:?}:{}", delete_idx - 1);
919 node.change_key(delete_idx - 1, key);
920 #[cfg(all(test, feature = "trace_log"))]
921 {
922 self.triggers |= TestFlag::UpdateSepKey as u32;
923 }
924 }
925 } else {
926 trace_log!("remove_child_from_inter {node:?}:{delete_idx} first");
927 let mut sep_key = node.remove_first_child();
929 #[cfg(all(test, feature = "trace_log"))]
930 {
931 self.triggers |= TestFlag::RemoveChildFirst as u32;
932 }
933 if let Some(key) = right_sep {
934 sep_key = key;
935 }
936 self.update_ancestor_sep_key::<false>(sep_key);
937 }
938 }
939
940 #[inline]
941 fn handle_inter_underflow(&mut self, mut node: InterNode<K, V>) {
942 let cap = InterNode::<K, V>::cap();
943 let mut root_height = 0;
944 let mut _flags = 0;
945 let cache = self.get_info_mut();
946 cache.assert_center();
947 while node.key_count() <= InterNode::<K, V>::UNDERFLOW_CAP {
948 if node.key_count() == 0 {
949 if root_height == 0 {
950 root_height = self.get_root_unwrap().height();
951 }
952 let node_height = node.height();
953 if node_height == root_height
954 || cache
955 .peak_ancenstor(|_node: &InterNode<K, V>, _idx: u32| -> bool {
956 _node.key_count() > 0
957 })
958 .is_none()
959 {
960 let child_ptr = unsafe { *node.child_ptr(0) };
961 debug_assert!(!child_ptr.is_null());
962 let root = if node_height == 1 {
963 LeafNode::<K, V>::wrap_root_ptr(child_ptr)
964 } else {
965 unsafe { NonNull::new_unchecked(child_ptr) }
966 };
967 trace_log!(
968 "handle_inter_underflow downgrade root {:?}",
969 Node::<K, V>::from_root_ptr(root)
970 );
971 let _old_root = self.root.replace(root);
972 debug_assert!(_old_root.is_some());
973
974 let info = self.get_info_mut();
975 while let Some((parent, _)) = info.pop() {
976 parent.dealloc::<false>();
977 info.dec_inter_count();
978 }
979 node.dealloc::<false>(); info.dec_inter_count();
981 }
982 break;
983 } else {
984 if let Some((mut grand, grand_idx)) = cache.pop() {
985 if grand_idx > 0 {
986 let mut left = grand.get_child_as_inter(grand_idx - 1);
987 if left.key_count() + node.key_count() < cap {
989 #[cfg(all(test, feature = "trace_log"))]
990 {
991 _flags |= TestFlag::InterMergeLeft as u32;
992 }
993 trace_log!(
994 "handle_inter_underflow {node:?} merge left {left:?} parent {grand:?}:{grand_idx}"
995 );
996 left.merge(node, &mut grand, grand_idx);
997 node = grand;
998 continue;
999 }
1000 }
1001 if grand_idx < grand.key_count() {
1002 let right = grand.get_child_as_inter(grand_idx + 1);
1003 if right.key_count() + node.key_count() < cap {
1005 #[cfg(all(test, feature = "trace_log"))]
1006 {
1007 _flags |= TestFlag::InterMergeRight as u32;
1008 }
1009 trace_log!(
1010 "handle_inter_underflow {node:?} cap {cap} merge right {right:?} parent {grand:?}:{}",
1011 grand_idx + 1
1012 );
1013 node.merge(right, &mut grand, grand_idx + 1);
1014 node = grand;
1015 continue;
1016 }
1017 }
1018 }
1019 let _ = cache;
1020 break;
1021 }
1022 }
1023 #[cfg(all(test, feature = "trace_log"))]
1024 {
1025 self.triggers |= _flags;
1026 }
1027 }
1028
1029 #[inline]
1030 fn remove_only_child(&mut self, node: InterNode<K, V>) -> Option<(InterNode<K, V>, u32)> {
1031 debug_assert_eq!(node.key_count(), 0);
1032 #[cfg(all(test, feature = "trace_log"))]
1033 {
1034 self.triggers |= TestFlag::RemoveOnlyChild as u32;
1035 }
1036 let info = self.get_info_mut();
1037 if let Some((parent, idx)) = info.move_to_ancenstor(
1038 |node: &InterNode<K, V>, _idx: u32| -> bool { node.key_count() != 0 },
1039 |_info, node| {
1040 _info.dec_inter_count();
1041 node.dealloc::<false>();
1042 },
1043 ) {
1044 node.dealloc::<true>();
1045 info.dec_inter_count();
1046 Some((parent, idx))
1047 } else {
1048 node.dealloc::<true>();
1049 info.dec_inter_count();
1050 self.root = None;
1052 None
1053 }
1054 }
1055
1056 #[cfg(all(test, feature = "std"))]
1058 pub fn dump(&self)
1059 where
1060 K: Debug,
1061 V: Debug,
1062 {
1063 print_log!("=== BTreeMap Dump ===");
1064 print_log!("Length: {}", self.len());
1065 if let Some(root) = self.get_root() {
1066 self.dump_node(&root, 0);
1067 } else {
1068 print_log!("(empty)");
1069 }
1070 print_log!("=====================");
1071 }
1072
1073 #[cfg(all(test, feature = "std"))]
1074 fn dump_node(&self, node: &Node<K, V>, depth: usize)
1075 where
1076 K: Debug,
1077 V: Debug,
1078 {
1079 match node {
1080 Node::Leaf(leaf) => {
1081 print!("{:indent$}", "", indent = depth * 2);
1082 print_log!("{}", leaf);
1083 }
1084 Node::Inter(inter) => {
1085 print!("{:indent$}", "", indent = depth * 2);
1086 print_log!("{}", inter);
1087 let count = inter.key_count() as u32;
1089 for i in 0..=count {
1090 unsafe {
1091 let child_ptr = *inter.child_ptr(i);
1092 if !child_ptr.is_null() {
1093 let child_node = if (*child_ptr).is_leaf() {
1094 Node::Leaf(LeafNode::<K, V>::from_header(child_ptr))
1095 } else {
1096 Node::Inter(InterNode::<K, V>::from_header(child_ptr))
1097 };
1098 self.dump_node(&child_node, depth + 1);
1099 }
1100 }
1101 }
1102 }
1103 }
1104 }
1105
1106 pub fn validate(&self)
1109 where
1110 K: Debug,
1111 V: Debug,
1112 {
1113 let root = if let Some(_root) = self.get_root() {
1114 _root
1115 } else {
1116 assert_eq!(self.len, 0, "Empty tree should have len 0");
1117 return;
1118 };
1119 let mut total_keys = 0usize;
1120 let mut prev_leaf_max: Option<K> = None;
1121
1122 match root {
1123 Node::Leaf(leaf) => {
1124 total_keys += leaf.validate(None, None);
1125 }
1126 Node::Inter(inter) => {
1127 let mut cache = TreeInfo::new(0, 0);
1129 cache.ensure_cap(inter.height());
1130 let mut cur = inter.clone();
1131 loop {
1132 cache.push(cur.clone(), 0);
1133 cur.validate();
1134 match cur.get_child(0) {
1135 Node::Leaf(leaf) => {
1136 let min_key: Option<K> = None;
1138 let max_key = if inter.key_count() > 0 {
1139 unsafe { Some((*inter.key_ptr(0)).assume_init_ref().clone()) }
1140 } else {
1141 None
1142 };
1143 total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1144 if let Some(ref prev_max) = prev_leaf_max {
1145 let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1146 assert!(
1147 prev_max < first_key,
1148 "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1149 leaf,
1150 prev_max,
1151 first_key
1152 );
1153 }
1154 prev_leaf_max = unsafe {
1155 Some(
1156 (*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone(),
1157 )
1158 };
1159 break;
1160 }
1161 Node::Inter(child_inter) => {
1162 cur = child_inter;
1163 }
1164 }
1165 }
1166
1167 while let Some((parent, idx)) =
1169 cache.move_right_and_pop_l1(dummy_post_callback::<K, V>)
1170 {
1171 cache.push(parent.clone(), idx);
1172 if let Node::Leaf(leaf) = parent.get_child(idx) {
1173 let min_key = if idx > 0 {
1175 unsafe { Some((*parent.key_ptr(idx - 1)).assume_init_ref().clone()) }
1176 } else {
1177 None
1178 };
1179 let max_key = if idx < parent.key_count() {
1180 unsafe { Some((*parent.key_ptr(idx)).assume_init_ref().clone()) }
1181 } else {
1182 None
1183 };
1184 total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1185
1186 if let Some(ref prev_max) = prev_leaf_max {
1188 let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1189 assert!(
1190 prev_max < first_key,
1191 "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1192 leaf,
1193 prev_max,
1194 first_key
1195 );
1196 }
1197 prev_leaf_max = unsafe {
1198 Some((*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone())
1199 };
1200 } else {
1201 panic!("{parent:?} child {:?} is not leaf", parent.get_child(idx));
1202 }
1203 }
1204 }
1205 }
1206 assert_eq!(
1207 total_keys, self.len,
1208 "Total keys in tree ({}) doesn't match len ({})",
1209 total_keys, self.len
1210 );
1211 }
1212
1213 #[inline]
1216 pub fn first_key_value(&self) -> Option<(&K, &V)> {
1217 let leaf = self.search_leaf_with(|inter| inter.find_first_leaf(None))?;
1218 debug_assert!(leaf.key_count() > 0);
1219 unsafe {
1220 let key = (*leaf.key_ptr(0)).assume_init_ref();
1221 let value = (*leaf.value_ptr(0)).assume_init_ref();
1222 Some((key, value))
1223 }
1224 }
1225
1226 #[inline]
1229 pub fn last_key_value(&self) -> Option<(&K, &V)> {
1230 let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(None))?;
1231 let count = leaf.key_count();
1232 debug_assert!(count > 0);
1233 unsafe {
1234 let last_idx = count - 1;
1235 let key = (*leaf.key_ptr(last_idx)).assume_init_ref();
1236 let value = (*leaf.value_ptr(last_idx)).assume_init_ref();
1237 Some((key, value))
1238 }
1239 }
1240
1241 #[inline]
1244 pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1245 let leaf =
1246 self.search_leaf_with(|inter| inter.find_first_leaf(Some(self.clear_cache())))?;
1247 if leaf.key_count() > 0 {
1248 Some(OccupiedEntry { tree: self, idx: 0, leaf })
1249 } else {
1250 None
1252 }
1253 }
1254
1255 #[inline]
1258 pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1259 let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(Some(self.clear_cache())))?;
1260 let count = leaf.key_count();
1261 if count > 0 {
1262 Some(OccupiedEntry { tree: self, idx: count - 1, leaf })
1263 } else {
1264 None
1266 }
1267 }
1268
1269 #[inline]
1272 pub fn pop_first(&mut self) -> Option<(K, V)> {
1273 match self.first_entry() {
1274 Some(entry) => Some(entry.remove_entry()),
1275 _ => None,
1276 }
1277 }
1278
1279 #[inline]
1282 pub fn pop_last(&mut self) -> Option<(K, V)> {
1283 match self.last_entry() {
1284 Some(entry) => Some(entry.remove_entry()),
1285 _ => None,
1286 }
1287 }
1288
1289 #[inline]
1291 pub fn iter(&self) -> Iter<'_, K, V> {
1292 Iter::new(self.find_first_and_last_leaf(), self.len)
1293 }
1294
1295 #[inline]
1297 pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
1298 IterMut::new(self.find_first_and_last_leaf(), self.len)
1299 }
1300
1301 #[inline]
1303 pub fn into_iter_rev(self) -> IntoIter<K, V> {
1304 IntoIter::new(self, false)
1305 }
1306
1307 #[inline]
1309 pub fn keys(&self) -> Keys<'_, K, V> {
1310 Keys::new(self.iter())
1311 }
1312
1313 #[inline]
1315 pub fn values(&self) -> Values<'_, K, V> {
1316 Values::new(self.iter())
1317 }
1318
1319 #[inline]
1321 pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
1322 ValuesMut::new(self.iter_mut())
1323 }
1324
1325 #[inline]
1326 fn find_first_and_last_leaf(&self) -> Option<(LeafNode<K, V>, LeafNode<K, V>)> {
1327 let root = self.root?;
1328 if !Node::<K, V>::root_is_leaf(root) {
1329 let inter = InterNode::<K, V>::from(root);
1330 Some((inter.clone().find_first_leaf(None), inter.find_last_leaf(None)))
1331 } else {
1332 let leaf = LeafNode::<K, V>::from_root_ptr(root);
1333 Some((leaf.clone(), leaf))
1334 }
1335 }
1336
1337 #[inline]
1340 fn find_range_bounds<R>(&self, range: R) -> Option<RangeBase<'_, K, V>>
1341 where
1342 R: RangeBounds<K>,
1343 {
1344 let root = self.get_root()?;
1345 let (front_leaf, front_idx) = root.find_leaf_with_bound(range.start_bound(), true);
1346 let (back_leaf, back_idx) = root.find_leaf_with_bound(range.end_bound(), false);
1347 Some(RangeBase::new(front_leaf, front_idx, back_leaf, back_idx))
1348 }
1349
1350 #[inline]
1352 pub fn range<R>(&self, range: R) -> Range<'_, K, V>
1353 where
1354 R: RangeBounds<K>,
1355 {
1356 Range::new(self.find_range_bounds(range))
1357 }
1358
1359 #[inline]
1361 pub fn range_mut<R>(&mut self, range: R) -> RangeMut<'_, K, V>
1362 where
1363 R: RangeBounds<K>,
1364 {
1365 RangeMut::new(self.find_range_bounds(range))
1366 }
1367}
1368
1369impl<K: Ord + Clone + Sized, V: Sized> Default for BTreeMap<K, V> {
1370 fn default() -> Self {
1371 Self::new()
1372 }
1373}
1374
1375impl<K: Ord + Clone + Sized, V: Sized> Drop for BTreeMap<K, V> {
1376 fn drop(&mut self) {
1377 if let Some(root) = self.root {
1378 if Node::<K, V>::root_is_leaf(root) {
1379 let leaf = LeafNode::<K, V>::from_root_ptr(root);
1380 leaf.dealloc::<true>();
1381 } else {
1382 let inter = InterNode::<K, V>::from(root);
1383 let mut cache = self.take_cache().expect("should have cache");
1384 let mut cur = inter.find_first_leaf(Some(&mut cache));
1385 cur.dealloc::<true>();
1386 while let Some((parent, idx)) =
1389 cache.move_right_and_pop_l1(|_info, _node| _node.dealloc::<true>())
1390 {
1391 cache.push(parent.clone(), idx);
1392 cur = parent.get_child_as_leaf(idx);
1393 cur.dealloc::<true>();
1394 }
1395 }
1396 }
1397 }
1398}
1399
1400impl<K: Ord + Clone + Sized, V: Sized> IntoIterator for BTreeMap<K, V> {
1401 type Item = (K, V);
1402 type IntoIter = IntoIter<K, V>;
1403
1404 #[inline]
1405 fn into_iter(self) -> Self::IntoIter {
1406 IntoIter::new(self, true)
1407 }
1408}
1409
1410impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a BTreeMap<K, V> {
1411 type Item = (&'a K, &'a V);
1412 type IntoIter = Iter<'a, K, V>;
1413
1414 #[inline]
1415 fn into_iter(self) -> Self::IntoIter {
1416 self.iter()
1417 }
1418}
1419
1420impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a mut BTreeMap<K, V> {
1421 type Item = (&'a K, &'a mut V);
1422 type IntoIter = IterMut<'a, K, V>;
1423
1424 #[inline]
1425 fn into_iter(self) -> Self::IntoIter {
1426 self.iter_mut()
1427 }
1428}