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