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