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