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 get_info(&self) -> &TreeInfo<K, V> {
284 self._get_info().as_ref().unwrap()
285 }
286
287 #[inline(always)]
288 fn clear_cache(&self) -> &mut TreeInfo<K, V> {
289 let cache = self.get_info_mut();
290 cache.clear();
291 cache
292 }
293
294 #[inline(always)]
295 fn take_cache(&mut self) -> Option<TreeInfo<K, V>> {
296 let mut cache = self._get_info().take()?;
297 cache.clear();
298 Some(cache)
299 }
300
301 #[inline]
303 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
304 let mut is_seq = true;
305 if let Some(leaf) = self.search_leaf_with(|inter| {
306 inter.find_leaf_with_cache_smart(self.clear_cache(), &key, &mut is_seq)
307 }) {
308 let (idx, is_equal) = leaf.search_smart(&key, is_seq);
309 if is_equal {
310 Entry::Occupied(OccupiedEntry { tree: self, idx, leaf })
311 } else {
312 Entry::Vacant(VacantEntry { tree: self, key, idx, leaf: Some(leaf) })
313 }
314 } else {
315 return Entry::Vacant(VacantEntry { tree: self, key, idx: 0, leaf: None });
316 }
317 }
318
319 #[inline(always)]
320 fn find<Q>(&self, key: &Q) -> Option<(LeafNode<K, V>, u32)>
321 where
322 K: Borrow<Q>,
323 Q: Ord + ?Sized,
324 {
325 let leaf = self.search_leaf_with(|inter| inter.find_leaf(key))?;
326 let (idx, is_equal) = leaf.search(key);
327 trace_log!("find leaf {leaf:?} {idx} exist {is_equal}");
328 if is_equal { Some((leaf, idx)) } else { None }
329 }
330
331 #[inline(always)]
333 pub fn contains_key<Q>(&self, key: &Q) -> bool
334 where
335 K: Borrow<Q>,
336 Q: Ord + ?Sized,
337 {
338 self.find::<Q>(key).is_some()
339 }
340
341 pub fn get<Q>(&self, key: &Q) -> Option<&V>
343 where
344 K: Borrow<Q>,
345 Q: Ord + ?Sized,
346 {
347 if let Some((leaf, idx)) = self.find::<Q>(key) {
348 let value = unsafe { leaf.value_ptr(idx) };
349 debug_assert!(!value.is_null());
350 Some(unsafe { (*value).assume_init_ref() })
351 } else {
352 None
353 }
354 }
355
356 pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
358 where
359 K: Borrow<Q>,
360 Q: Ord + ?Sized,
361 {
362 if let Some((leaf, idx)) = self.find::<Q>(key) {
363 let value = unsafe { leaf.value_ptr(idx) };
364 debug_assert!(!value.is_null());
365 Some(unsafe { (*value).assume_init_mut() })
366 } else {
367 None
368 }
369 }
370
371 #[inline]
372 fn init_empty<'a>(&'a mut self, key: K, value: V) -> &'a mut V {
373 debug_assert!(self.root.is_none());
374 unsafe {
375 let mut leaf = LeafNode::<K, V>::alloc();
377 self.root = Some(leaf.to_root_ptr());
378 self.len = 1;
379 return &mut *leaf.insert_no_split_with_idx(0, key, value);
380 }
381 }
382
383 #[inline]
386 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
387 let mut is_seq = true;
388 if let Some(mut leaf) = self.search_leaf_with(|inter| {
389 inter.find_leaf_with_cache_smart(self.clear_cache(), &key, &mut is_seq)
390 }) {
391 let (idx, is_equal) = leaf.search_smart(&key, is_seq);
392 if is_equal {
393 Some(leaf.replace(idx, value))
394 } else {
395 self.len += 1;
396 let count = leaf.key_count();
398 if count < LeafNode::<K, V>::cap() {
400 leaf.insert_no_split_with_idx(idx, key, value);
401 } else {
402 self.insert_with_split(key, value, leaf, idx);
404 }
405 None
406 }
407 } else {
408 self.init_empty(key, value);
409 None
410 }
411 }
412
413 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
415 where
416 K: Borrow<Q>,
417 Q: Ord + ?Sized,
418 {
419 let mut leaf = self
420 .search_leaf_with(|inter| inter.find_leaf_with_cache::<Q>(self.clear_cache(), key))?;
421 let (idx, is_equal) = leaf.search(key);
422 if is_equal {
423 trace_log!("{leaf:?} remove {idx}");
424 let val = leaf.remove_value_no_borrow(idx);
425 self.len -= 1;
426 let new_count = leaf.key_count();
428 let min_count = LeafNode::<K, V>::cap() >> 1;
429 if new_count < min_count && self.root_is_inter() {
430 self.handle_leaf_underflow(leaf, true);
432 }
433 Some(val)
434 } else {
435 None
436 }
437 }
438
439 #[inline(always)]
440 fn root_is_inter(&self) -> bool {
441 if let Some(root) = self.root { !Node::<K, V>::root_is_leaf(root) } else { false }
442 }
443
444 pub fn remove_range<R>(&mut self, range: R) -> Option<(K, V)>
448 where
449 R: RangeBounds<K>,
450 {
451 self.remove_range_with::<R, _>(range, |_, _| {})
452 }
453
454 #[inline]
460 pub fn remove_range_with<R, F>(&mut self, range: R, mut cb: F) -> Option<(K, V)>
461 where
462 R: RangeBounds<K>,
463 F: FnMut(&K, &V),
464 {
465 macro_rules! end_contains {
466 ($key: expr) => {{
467 match range.end_bound() {
468 Bound::Excluded(k) => $key < k,
469 Bound::Included(k) => $key <= k,
470 Bound::Unbounded => true,
471 }
472 }};
473 }
474 let mut ent = match range.start_bound() {
476 Bound::Excluded(k) => match self.entry(k.clone()).move_forward() {
477 Ok(ent) => {
478 if end_contains!(ent.key()) {
479 ent
480 } else {
481 return None;
482 }
483 }
484 Err(_) => return None,
485 },
486 Bound::Included(k) => match self.entry(k.clone()) {
487 Entry::Occupied(ent) => ent,
488 Entry::Vacant(ent) => match ent.move_forward() {
489 Ok(ent) => {
490 if end_contains!(ent.key()) {
491 ent
492 } else {
493 return None;
494 }
495 }
496 Err(_) => return None,
497 },
498 },
499 Bound::Unbounded => {
500 if let Some(ent) = self.first_entry() {
501 ent
502 } else {
503 return None;
504 }
505 }
506 };
507 loop {
508 if let Some((_next_k, _next_v)) = ent.peak_forward() {
509 if end_contains!(_next_k) {
510 let next_key = _next_k.clone();
511 let (_k, _v) = ent._remove_entry(false);
512 cb(&_k, &_v);
513 if let Entry::Occupied(_ent) = self.entry(next_key) {
514 ent = _ent;
515 continue;
516 } else {
517 unreachable!();
518 }
519 }
520 }
521 let (_k, _v) = ent._remove_entry(true);
522 cb(&_k, &_v);
523 return Some((_k, _v));
524 }
525 }
526
527 #[inline(always)]
528 fn get_root_unwrap(&self) -> Node<K, V> {
529 Node::<K, V>::from_root_ptr(*self.root.as_ref().unwrap())
530 }
531
532 #[inline(always)]
533 fn get_root(&self) -> Option<Node<K, V>> {
534 Some(Node::<K, V>::from_root_ptr(*self.root.as_ref()?))
535 }
536
537 #[inline(always)]
539 fn search_leaf_with<F>(&self, search: F) -> Option<LeafNode<K, V>>
540 where
541 F: FnOnce(InterNode<K, V>) -> LeafNode<K, V>,
542 {
543 let root = self.root?;
544 if !Node::<K, V>::root_is_leaf(root) {
545 Some(search(InterNode::<K, V>::from(root)))
546 } else {
547 Some(LeafNode::<K, V>::from_root_ptr(root))
548 }
549 }
550
551 #[inline(always)]
553 fn update_ancestor_sep_key<const MOVE: bool>(&mut self, sep_key: K) {
554 let ret = if MOVE {
557 self.get_info_mut()
558 .move_to_ancenstor(|_node, idx| -> bool { idx > 0 }, dummy_post_callback)
559 } else {
560 self.get_info().peak_ancenstor(|_node, idx| -> bool { idx > 0 })
561 };
562 if let Some((parent, parent_idx)) = ret {
563 trace_log!("update_ancestor_sep_key move={MOVE} at {parent:?}:{}", parent_idx - 1);
564 parent.change_key(parent_idx - 1, sep_key);
565 #[cfg(all(test, feature = "trace_log"))]
566 {
567 self.triggers |= TestFlag::UpdateSepKey as u32;
568 }
569 }
570 }
571
572 fn insert_with_split(
574 &mut self, key: K, value: V, mut leaf: LeafNode<K, V>, idx: u32,
575 ) -> *mut V {
576 debug_assert!(leaf.is_full());
577 let cap = LeafNode::<K, V>::cap();
578 if idx < cap {
579 if let Some(mut left_node) = leaf.get_left_node()
581 && !left_node.is_full()
582 {
583 trace_log!("insert {leaf:?}:{idx} borrow left {left_node:?}");
584 let val_p = if idx == 0 {
585 left_node.insert_no_split_with_idx(left_node.key_count(), key, value)
588 } else {
589 leaf.insert_borrow_left(&mut left_node, idx, key, value)
590 };
591 #[cfg(all(test, feature = "trace_log"))]
592 {
593 self.triggers |= TestFlag::LeafMoveLeft as u32;
594 }
595 self.update_ancestor_sep_key::<true>(leaf.clone_first_key());
596 return val_p;
597 }
598 } else {
599 }
601 if let Some(mut right_node) = leaf.get_right_node()
602 && !right_node.is_full()
603 {
604 trace_log!("insert {leaf:?}:{idx} borrow right {right_node:?}");
605 let val_p = if idx == cap {
606 right_node.insert_no_split_with_idx(0, key, value)
609 } else {
610 leaf.borrow_right(&mut right_node);
611 leaf.insert_no_split_with_idx(idx, key, value)
612 };
613 #[cfg(all(test, feature = "trace_log"))]
614 {
615 self.triggers |= TestFlag::LeafMoveRight as u32;
616 }
617 self.get_info_mut().move_right();
618 self.update_ancestor_sep_key::<true>(right_node.clone_first_key());
619 return val_p;
620 }
621 #[cfg(all(test, feature = "trace_log"))]
622 {
623 self.triggers |= TestFlag::LeafSplit as u32;
624 }
625 let (new_leaf, ptr_v) = leaf.insert_with_split(idx, key, value);
626 let split_key = unsafe { (*new_leaf.key_ptr(0)).assume_init_ref().clone() };
627
628 let o_info = self._get_info();
629 if let Some(info) = o_info.as_mut() {
630 info.inc_leaf_count();
631 match self.propagate_split(info, split_key, leaf.get_ptr(), new_leaf.get_ptr()) {
632 Ok(_flags) => {
633 #[cfg(all(test, feature = "trace_log"))]
634 {
635 self.triggers |= _flags;
636 }
637 }
638 Err(new_root) => {
639 self.root.replace(new_root.to_root_ptr());
640 }
641 }
642 ptr_v
643 } else {
644 o_info.replace(TreeInfo::new(2, 1));
645 let new_root =
646 InterNode::<K, V>::new_root(1, split_key, leaf.get_ptr(), new_leaf.get_ptr());
647 let _old_root = self.root.replace(new_root.to_root_ptr());
648 debug_assert_eq!(_old_root.unwrap(), leaf.to_root_ptr());
649 ptr_v
650 }
651 }
652
653 #[inline(always)]
660 fn propagate_split(
661 &self, info: &mut TreeInfo<K, V>, mut promote_key: K, mut left_ptr: *mut NodeHeader,
662 mut right_ptr: *mut NodeHeader,
663 ) -> Result<u32, InterNode<K, V>> {
664 let mut height = 0;
665 #[allow(unused_mut)]
666 let mut flags = 0;
667 while let Some((mut parent, idx)) = info.pop() {
669 if !parent.is_full() {
670 trace_log!("propagate_split {parent:?}:{idx} insert {right_ptr:p}");
671 parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
673 return Ok(flags);
674 } else {
675 if let Some((mut grand, grand_idx)) = info.peak_next() {
677 if grand_idx > 0 {
679 let mut left_parent = grand.get_child_as_inter(grand_idx - 1);
680 if !left_parent.is_full() {
681 #[cfg(all(test, feature = "trace_log"))]
682 {
683 flags |= TestFlag::InterMoveLeft as u32;
684 }
685 if idx == 0 {
686 trace_log!(
687 "propagate_split rotate_left {grand:?}:{} first ->{left_parent:?} left {left_ptr:p} insert {idx} {right_ptr:p}",
688 grand_idx - 1
689 );
690 let demote_key = grand.change_key(grand_idx - 1, promote_key);
692 debug_assert_eq!(parent.get_child_ptr(0), left_ptr);
693 unsafe { (*parent.child_ptr(0)) = right_ptr };
694 left_parent.append(demote_key, left_ptr);
695 #[cfg(all(test, feature = "trace_log"))]
696 {
697 flags |= TestFlag::InterMoveLeftFirst as u32;
698 }
699 } else {
700 trace_log!(
701 "propagate_split insert_rotate_left {grand:?}:{grand_idx} -> {left_parent:?} insert {idx} {right_ptr:p}"
702 );
703 parent.insert_rotate_left(
704 &mut grand,
705 grand_idx,
706 &mut left_parent,
707 idx,
708 promote_key,
709 right_ptr,
710 );
711 }
712 return Ok(flags);
713 }
714 }
715 if grand_idx < grand.key_count() {
717 let mut right_parent = grand.get_child_as_inter(grand_idx + 1);
718 if !right_parent.is_full() {
719 #[cfg(all(test, feature = "trace_log"))]
720 {
721 flags |= TestFlag::InterMoveRight as u32;
722 }
723 if idx == parent.key_count() {
724 trace_log!(
725 "propagate_split rotate_right last {grand:?}:{grand_idx} -> {right_parent:?}:0 insert right {right_parent:?}:0 {right_ptr:p}"
726 );
727 let demote_key = grand.change_key(grand_idx, promote_key);
729 right_parent.insert_at_front(right_ptr, demote_key);
730 #[cfg(all(test, feature = "trace_log"))]
731 {
732 flags |= TestFlag::InterMoveRightLast as u32;
733 }
734 } else {
735 trace_log!(
736 "propagate_split rotate_right {grand:?}:{grand_idx} -> {right_parent:?}:0 insert {parent:?}:{idx} {right_ptr:p}"
737 );
738 parent.rotate_right(&mut grand, grand_idx, &mut right_parent);
739 parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
740 }
741 return Ok(flags);
742 }
743 }
744 }
745 height += 1;
746
747 let (right, _promote_key) = parent.insert_split(promote_key, right_ptr);
749 info.inc_inter_count();
750
751 promote_key = _promote_key;
752 right_ptr = right.get_ptr();
753 left_ptr = parent.get_ptr();
754 #[cfg(all(test, feature = "trace_log"))]
755 {
756 flags |= TestFlag::InterSplit as u32;
757 }
758 }
760 }
761
762 info.ensure_cap(height + 1);
763 info.inc_inter_count();
764 let new_root = InterNode::<K, V>::new_root(height + 1, promote_key, left_ptr, right_ptr);
766 #[cfg(debug_assertions)]
768 {
769 let _old_root = self.root.as_ref().unwrap();
770 assert_eq!(_old_root.as_ptr(), left_ptr);
772 }
773 return Err(new_root);
774 }
775
776 fn handle_leaf_underflow(&mut self, mut leaf: LeafNode<K, V>, merge: bool) {
783 debug_assert!(!self.get_root_unwrap().is_leaf());
784 let cur_count = leaf.key_count();
785 let cap = LeafNode::<K, V>::cap();
786 debug_assert!(cur_count <= cap >> 1);
787 let mut can_unlink: bool = false;
788 let (mut left_avail, mut right_avail) = (0, 0);
789 let mut merge_right = false;
790 if cur_count == 0 {
791 trace_log!("handle_leaf_underflow {leaf:?} unlink");
792 can_unlink = true;
794 }
795 if merge {
796 if !can_unlink && let Some(mut left_node) = leaf.get_left_node() {
797 let left_count = left_node.key_count();
798 if left_count + cur_count <= cap {
799 trace_log!(
800 "handle_leaf_underflow {leaf:?} merge left {left_node:?} {cur_count}"
801 );
802 leaf.copy_left(&mut left_node, cur_count);
803 can_unlink = true;
804 #[cfg(all(test, feature = "trace_log"))]
805 {
806 self.triggers |= TestFlag::LeafMergeLeft as u32;
807 }
808 } else {
809 left_avail = cap - left_count;
810 }
811 }
812 if !can_unlink && let Some(mut right_node) = leaf.get_right_node() {
813 let right_count = right_node.key_count();
814 if right_count + cur_count <= cap {
815 trace_log!(
816 "handle_leaf_underflow {leaf:?} merge right {right_node:?} {cur_count}"
817 );
818 leaf.copy_right::<false>(&mut right_node, 0, cur_count);
819 can_unlink = true;
820 merge_right = true;
821 #[cfg(all(test, feature = "trace_log"))]
822 {
823 self.triggers |= TestFlag::LeafMergeRight as u32;
824 }
825 } else {
826 right_avail = cap - right_count;
827 }
828 }
829 if !can_unlink
832 && left_avail > 0
833 && right_avail > 0
834 && left_avail + right_avail == cur_count
835 {
836 let mut left_node = leaf.get_left_node().unwrap();
837 let mut right_node = leaf.get_right_node().unwrap();
838 debug_assert!(left_avail < cur_count);
839 trace_log!("handle_leaf_underflow {leaf:?} merge left {left_node:?} {left_avail}");
840 leaf.copy_left(&mut left_node, left_avail);
841 trace_log!(
842 "handle_leaf_underflow {leaf:?} merge right {right_node:?} {}",
843 cur_count - left_avail
844 );
845 leaf.copy_right::<false>(&mut right_node, left_avail, cur_count - left_avail);
846 merge_right = true;
847 can_unlink = true;
848 #[cfg(all(test, feature = "trace_log"))]
849 {
850 self.triggers |=
851 TestFlag::LeafMergeLeft as u32 | TestFlag::LeafMergeRight as u32;
852 }
853 }
854 }
855 if !can_unlink {
856 return;
857 }
858 self.get_info_mut().dec_leaf_count();
859 let right_sep = if merge_right {
860 let right_node = leaf.get_right_node().unwrap();
861 Some(right_node.clone_first_key())
862 } else {
863 None
864 };
865 let no_right = leaf.unlink().is_null();
866 leaf.dealloc::<false>();
867 let (mut parent, mut idx) = self.get_info_mut().pop().unwrap();
868 trace_log!("handle_leaf_underflow pop parent {parent:?}:{idx}");
869 if parent.key_count() == 0 {
870 if let Some((grand, grand_idx)) = self.remove_only_child(parent) {
871 trace_log!("handle_leaf_underflow remove_only_child until {grand:?}:{grand_idx}");
872 parent = grand;
873 idx = grand_idx;
874 } else {
875 trace_log!("handle_leaf_underflow remove_only_child all");
876 return;
877 }
878 }
879 self.remove_child_from_inter(&mut parent, idx, right_sep, no_right);
880 if parent.key_count() <= 1 {
881 self.handle_inter_underflow(parent);
882 }
883 }
884
885 #[inline]
888 fn remove_child_from_inter(
889 &mut self, node: &mut InterNode<K, V>, delete_idx: u32, right_sep: Option<K>,
890 _no_right: bool,
891 ) {
892 self.get_info().assert_center();
893 debug_assert!(node.key_count() > 0, "{:?} {}", node, node.key_count());
894 if delete_idx == node.key_count() {
895 trace_log!("remove_child_from_inter {node:?}:{delete_idx} last");
896 #[cfg(all(test, feature = "trace_log"))]
897 {
898 self.triggers |= TestFlag::RemoveChildLast as u32;
899 }
900 node.remove_last_child();
902 if let Some(key) = right_sep
903 && let Some((grand_parent, grand_idx)) =
904 self.get_info().peak_ancenstor(|_node: &InterNode<K, V>, idx: u32| -> bool {
905 _node.key_count() > idx
906 })
907 {
908 #[cfg(all(test, feature = "trace_log"))]
909 {
910 self.triggers |= TestFlag::UpdateSepKey as u32;
911 }
912 trace_log!("remove_child_from_inter change_key {grand_parent:?}:{grand_idx}");
913 grand_parent.change_key(grand_idx, key);
915 }
916 } else if delete_idx > 0 {
917 trace_log!("remove_child_from_inter {node:?}:{delete_idx} mid");
918 node.remove_mid_child(delete_idx);
919 #[cfg(all(test, feature = "trace_log"))]
920 {
921 self.triggers |= TestFlag::RemoveChildMid as u32;
922 }
923 if let Some(key) = right_sep {
925 trace_log!("remove_child_from_inter change_key {node:?}:{}", delete_idx - 1);
926 node.change_key(delete_idx - 1, key);
927 #[cfg(all(test, feature = "trace_log"))]
928 {
929 self.triggers |= TestFlag::UpdateSepKey as u32;
930 }
931 }
932 } else {
933 trace_log!("remove_child_from_inter {node:?}:{delete_idx} first");
934 let mut sep_key = node.remove_first_child();
936 #[cfg(all(test, feature = "trace_log"))]
937 {
938 self.triggers |= TestFlag::RemoveChildFirst as u32;
939 }
940 if let Some(key) = right_sep {
941 sep_key = key;
942 }
943 self.update_ancestor_sep_key::<false>(sep_key);
944 }
945 }
946
947 #[inline]
948 fn handle_inter_underflow(&mut self, mut node: InterNode<K, V>) {
949 let cap = InterNode::<K, V>::cap();
950 let mut root_height = 0;
951 let mut _flags = 0;
952 let cache = self.get_info_mut();
953 cache.assert_center();
954 while node.key_count() <= InterNode::<K, V>::UNDERFLOW_CAP {
955 if node.key_count() == 0 {
956 if root_height == 0 {
957 root_height = self.get_root_unwrap().height();
958 }
959 let node_height = node.height();
960 if node_height == root_height
961 || cache
962 .peak_ancenstor(|_node: &InterNode<K, V>, _idx: u32| -> bool {
963 _node.key_count() > 0
964 })
965 .is_none()
966 {
967 let child_ptr = unsafe { *node.child_ptr(0) };
968 debug_assert!(!child_ptr.is_null());
969 let root = if node_height == 1 {
970 LeafNode::<K, V>::wrap_root_ptr(child_ptr)
971 } else {
972 unsafe { NonNull::new_unchecked(child_ptr) }
973 };
974 trace_log!(
975 "handle_inter_underflow downgrade root {:?}",
976 Node::<K, V>::from_root_ptr(root)
977 );
978 let _old_root = self.root.replace(root);
979 debug_assert!(_old_root.is_some());
980
981 let info = self.get_info_mut();
982 while let Some((parent, _)) = info.pop() {
983 parent.dealloc::<false>();
984 info.dec_inter_count();
985 }
986 node.dealloc::<false>(); info.dec_inter_count();
988 }
989 break;
990 } else {
991 if let Some((mut grand, grand_idx)) = cache.pop() {
992 if grand_idx > 0 {
993 let mut left = grand.get_child_as_inter(grand_idx - 1);
994 if left.key_count() + node.key_count() < cap {
996 #[cfg(all(test, feature = "trace_log"))]
997 {
998 _flags |= TestFlag::InterMergeLeft as u32;
999 }
1000 trace_log!(
1001 "handle_inter_underflow {node:?} merge left {left:?} parent {grand:?}:{grand_idx}"
1002 );
1003 left.merge(node, &mut grand, grand_idx);
1004 node = grand;
1005 continue;
1006 }
1007 }
1008 if grand_idx < grand.key_count() {
1009 let right = grand.get_child_as_inter(grand_idx + 1);
1010 if right.key_count() + node.key_count() < cap {
1012 #[cfg(all(test, feature = "trace_log"))]
1013 {
1014 _flags |= TestFlag::InterMergeRight as u32;
1015 }
1016 trace_log!(
1017 "handle_inter_underflow {node:?} cap {cap} merge right {right:?} parent {grand:?}:{}",
1018 grand_idx + 1
1019 );
1020 node.merge(right, &mut grand, grand_idx + 1);
1021 node = grand;
1022 continue;
1023 }
1024 }
1025 }
1026 let _ = cache;
1027 break;
1028 }
1029 }
1030 #[cfg(all(test, feature = "trace_log"))]
1031 {
1032 self.triggers |= _flags;
1033 }
1034 }
1035
1036 #[inline]
1037 fn remove_only_child(&mut self, node: InterNode<K, V>) -> Option<(InterNode<K, V>, u32)> {
1038 debug_assert_eq!(node.key_count(), 0);
1039 #[cfg(all(test, feature = "trace_log"))]
1040 {
1041 self.triggers |= TestFlag::RemoveOnlyChild as u32;
1042 }
1043 let info = self.get_info_mut();
1044 if let Some((parent, idx)) = info.move_to_ancenstor(
1045 |node: &InterNode<K, V>, _idx: u32| -> bool { node.key_count() != 0 },
1046 |_info, node| {
1047 _info.dec_inter_count();
1048 node.dealloc::<false>();
1049 },
1050 ) {
1051 node.dealloc::<true>();
1052 info.dec_inter_count();
1053 Some((parent, idx))
1054 } else {
1055 node.dealloc::<true>();
1056 info.dec_inter_count();
1057 self.root = None;
1059 None
1060 }
1061 }
1062
1063 #[cfg(all(test, feature = "std"))]
1065 pub fn dump(&self)
1066 where
1067 K: Debug,
1068 V: Debug,
1069 {
1070 print_log!("=== BTreeMap Dump ===");
1071 print_log!("Length: {}", self.len());
1072 if let Some(root) = self.get_root() {
1073 self.dump_node(&root, 0);
1074 } else {
1075 print_log!("(empty)");
1076 }
1077 print_log!("=====================");
1078 }
1079
1080 #[cfg(all(test, feature = "std"))]
1081 fn dump_node(&self, node: &Node<K, V>, depth: usize)
1082 where
1083 K: Debug,
1084 V: Debug,
1085 {
1086 match node {
1087 Node::Leaf(leaf) => {
1088 print!("{:indent$}", "", indent = depth * 2);
1089 print_log!("{}", leaf);
1090 }
1091 Node::Inter(inter) => {
1092 print!("{:indent$}", "", indent = depth * 2);
1093 print_log!("{}", inter);
1094 let count = inter.key_count() as u32;
1096 for i in 0..=count {
1097 unsafe {
1098 let child_ptr = *inter.child_ptr(i);
1099 if !child_ptr.is_null() {
1100 let child_node = if (*child_ptr).is_leaf() {
1101 Node::Leaf(LeafNode::<K, V>::from_header(child_ptr))
1102 } else {
1103 Node::Inter(InterNode::<K, V>::from_header(child_ptr))
1104 };
1105 self.dump_node(&child_node, depth + 1);
1106 }
1107 }
1108 }
1109 }
1110 }
1111 }
1112
1113 pub fn validate(&self)
1116 where
1117 K: Debug,
1118 V: Debug,
1119 {
1120 let root = if let Some(_root) = self.get_root() {
1121 _root
1122 } else {
1123 assert_eq!(self.len, 0, "Empty tree should have len 0");
1124 return;
1125 };
1126 let mut total_keys = 0usize;
1127 let mut prev_leaf_max: Option<K> = None;
1128
1129 match root {
1130 Node::Leaf(leaf) => {
1131 total_keys += leaf.validate(None, None);
1132 }
1133 Node::Inter(inter) => {
1134 let mut cache = TreeInfo::new(0, 0);
1136 cache.ensure_cap(inter.height());
1137 let mut cur = inter.clone();
1138 loop {
1139 cache.push(cur.clone(), 0);
1140 cur.validate();
1141 match cur.get_child(0) {
1142 Node::Leaf(leaf) => {
1143 let min_key: Option<K> = None;
1145 let max_key = if inter.key_count() > 0 {
1146 unsafe { Some((*inter.key_ptr(0)).assume_init_ref().clone()) }
1147 } else {
1148 None
1149 };
1150 total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1151 if let Some(ref prev_max) = prev_leaf_max {
1152 let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1153 assert!(
1154 prev_max < first_key,
1155 "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1156 leaf,
1157 prev_max,
1158 first_key
1159 );
1160 }
1161 prev_leaf_max = unsafe {
1162 Some(
1163 (*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone(),
1164 )
1165 };
1166 break;
1167 }
1168 Node::Inter(child_inter) => {
1169 cur = child_inter;
1170 }
1171 }
1172 }
1173
1174 while let Some((parent, idx)) =
1176 cache.move_right_and_pop_l1(dummy_post_callback::<K, V>)
1177 {
1178 cache.push(parent.clone(), idx);
1179 if let Node::Leaf(leaf) = parent.get_child(idx) {
1180 let min_key = if idx > 0 {
1182 unsafe { Some((*parent.key_ptr(idx - 1)).assume_init_ref().clone()) }
1183 } else {
1184 None
1185 };
1186 let max_key = if idx < parent.key_count() {
1187 unsafe { Some((*parent.key_ptr(idx)).assume_init_ref().clone()) }
1188 } else {
1189 None
1190 };
1191 total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1192
1193 if let Some(ref prev_max) = prev_leaf_max {
1195 let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1196 assert!(
1197 prev_max < first_key,
1198 "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1199 leaf,
1200 prev_max,
1201 first_key
1202 );
1203 }
1204 prev_leaf_max = unsafe {
1205 Some((*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone())
1206 };
1207 } else {
1208 panic!("{parent:?} child {:?} is not leaf", parent.get_child(idx));
1209 }
1210 }
1211 }
1212 }
1213 assert_eq!(
1214 total_keys, self.len,
1215 "Total keys in tree ({}) doesn't match len ({})",
1216 total_keys, self.len
1217 );
1218 }
1219
1220 #[inline]
1223 pub fn first_key_value(&self) -> Option<(&K, &V)> {
1224 let leaf = self.search_leaf_with(|inter| inter.find_first_leaf(None))?;
1225 debug_assert!(leaf.key_count() > 0);
1226 unsafe {
1227 let key = (*leaf.key_ptr(0)).assume_init_ref();
1228 let value = (*leaf.value_ptr(0)).assume_init_ref();
1229 Some((key, value))
1230 }
1231 }
1232
1233 #[inline]
1236 pub fn last_key_value(&self) -> Option<(&K, &V)> {
1237 let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(None))?;
1238 let count = leaf.key_count();
1239 debug_assert!(count > 0);
1240 unsafe {
1241 let last_idx = count - 1;
1242 let key = (*leaf.key_ptr(last_idx)).assume_init_ref();
1243 let value = (*leaf.value_ptr(last_idx)).assume_init_ref();
1244 Some((key, value))
1245 }
1246 }
1247
1248 #[inline]
1251 pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1252 let leaf =
1253 self.search_leaf_with(|inter| inter.find_first_leaf(Some(self.clear_cache())))?;
1254 if leaf.key_count() > 0 {
1255 Some(OccupiedEntry { tree: self, idx: 0, leaf })
1256 } else {
1257 None
1259 }
1260 }
1261
1262 #[inline]
1265 pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1266 let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(Some(self.clear_cache())))?;
1267 let count = leaf.key_count();
1268 if count > 0 {
1269 Some(OccupiedEntry { tree: self, idx: count - 1, leaf })
1270 } else {
1271 None
1273 }
1274 }
1275
1276 #[inline]
1279 pub fn pop_first(&mut self) -> Option<(K, V)> {
1280 match self.first_entry() {
1281 Some(entry) => Some(entry.remove_entry()),
1282 _ => None,
1283 }
1284 }
1285
1286 #[inline]
1289 pub fn pop_last(&mut self) -> Option<(K, V)> {
1290 match self.last_entry() {
1291 Some(entry) => Some(entry.remove_entry()),
1292 _ => None,
1293 }
1294 }
1295
1296 #[inline]
1298 pub fn iter(&self) -> Iter<'_, K, V> {
1299 Iter::new(self.find_first_and_last_leaf(), self.len)
1300 }
1301
1302 #[inline]
1304 pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
1305 IterMut::new(self.find_first_and_last_leaf(), self.len)
1306 }
1307
1308 #[inline]
1310 pub fn into_iter_rev(self) -> IntoIter<K, V> {
1311 IntoIter::new(self, false)
1312 }
1313
1314 #[inline]
1316 pub fn keys(&self) -> Keys<'_, K, V> {
1317 Keys::new(self.iter())
1318 }
1319
1320 #[inline]
1322 pub fn values(&self) -> Values<'_, K, V> {
1323 Values::new(self.iter())
1324 }
1325
1326 #[inline]
1328 pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
1329 ValuesMut::new(self.iter_mut())
1330 }
1331
1332 #[inline]
1333 fn find_first_and_last_leaf(&self) -> Option<(LeafNode<K, V>, LeafNode<K, V>)> {
1334 let root = self.root?;
1335 if !Node::<K, V>::root_is_leaf(root) {
1336 let inter = InterNode::<K, V>::from(root);
1337 Some((inter.clone().find_first_leaf(None), inter.find_last_leaf(None)))
1338 } else {
1339 let leaf = LeafNode::<K, V>::from_root_ptr(root);
1340 Some((leaf.clone(), leaf))
1341 }
1342 }
1343
1344 #[inline]
1347 fn find_range_bounds<R>(&self, range: R) -> Option<RangeBase<'_, K, V>>
1348 where
1349 R: RangeBounds<K>,
1350 {
1351 let root = self.get_root()?;
1352 let (front_leaf, front_idx) = root.find_leaf_with_bound(range.start_bound(), true);
1353 let (back_leaf, back_idx) = root.find_leaf_with_bound(range.end_bound(), false);
1354 Some(RangeBase::new(front_leaf, front_idx, back_leaf, back_idx))
1355 }
1356
1357 #[inline]
1359 pub fn range<R>(&self, range: R) -> Range<'_, K, V>
1360 where
1361 R: RangeBounds<K>,
1362 {
1363 Range::new(self.find_range_bounds(range))
1364 }
1365
1366 #[inline]
1368 pub fn range_mut<R>(&mut self, range: R) -> RangeMut<'_, K, V>
1369 where
1370 R: RangeBounds<K>,
1371 {
1372 RangeMut::new(self.find_range_bounds(range))
1373 }
1374}
1375
1376impl<K: Ord + Clone + Sized, V: Sized> Default for BTreeMap<K, V> {
1377 fn default() -> Self {
1378 Self::new()
1379 }
1380}
1381
1382impl<K: Ord + Clone + Sized, V: Sized> Drop for BTreeMap<K, V> {
1383 fn drop(&mut self) {
1384 if let Some(root) = self.root {
1385 if Node::<K, V>::root_is_leaf(root) {
1386 let leaf = LeafNode::<K, V>::from_root_ptr(root);
1387 leaf.dealloc::<true>();
1388 } else {
1389 let inter = InterNode::<K, V>::from(root);
1390 let mut cache = self.take_cache().expect("should have cache");
1391 let mut cur = inter.find_first_leaf(Some(&mut cache));
1392 cur.dealloc::<true>();
1393 while let Some((parent, idx)) =
1396 cache.move_right_and_pop_l1(|_info, _node| _node.dealloc::<true>())
1397 {
1398 cache.push(parent.clone(), idx);
1399 cur = parent.get_child_as_leaf(idx);
1400 cur.dealloc::<true>();
1401 }
1402 }
1403 }
1404 }
1405}
1406
1407impl<K: Ord + Clone + Sized, V: Sized> IntoIterator for BTreeMap<K, V> {
1408 type Item = (K, V);
1409 type IntoIter = IntoIter<K, V>;
1410
1411 #[inline]
1412 fn into_iter(self) -> Self::IntoIter {
1413 IntoIter::new(self, true)
1414 }
1415}
1416
1417impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a BTreeMap<K, V> {
1418 type Item = (&'a K, &'a V);
1419 type IntoIter = Iter<'a, K, V>;
1420
1421 #[inline]
1422 fn into_iter(self) -> Self::IntoIter {
1423 self.iter()
1424 }
1425}
1426
1427impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a mut BTreeMap<K, V> {
1428 type Item = (&'a K, &'a mut V);
1429 type IntoIter = IterMut<'a, K, V>;
1430
1431 #[inline]
1432 fn into_iter(self) -> Self::IntoIter {
1433 self.iter_mut()
1434 }
1435}