Skip to main content

embed_collections/btree/
mod.rs

1//! B+Tree Map - A in memory cache-optimized B+Tree for single-threaded use.
2//!
3//! ## Feature outlines
4//! - Designed for CPU cache efficiency and memory efficiency
5//! - Optimised for numeric key type
6//!   - Typical scenario: [range-tree-rs](https://docs.rs/range-tree-rs)
7//! - A B+tree. Data stores only at leaf level, with links at leaf level.
8//!   - Provides efficient iteration of data
9//!   - Linear search within nodes, respecting cacheline boundaries
10//! - Nodes are filled up in 4 cache lines (256 bytes on x86_64)
11//!   - keys stored in first 128B (with header)
12//!   - Values/pointers stored in last 128B
13//!   - the capacity is calculated according to the size of K, V, with limitations:
14//!     - K & V should <= CACHE_LINE_SIZE - 16  (If K & V is larger should put into `Box`)
15//! - Specially Cursor & Entry API which allow to modify after moving the cursor to adjacent data.
16//! - The detail design notes are with the source in mod.rs and node.rs
17//!
18//! ## Special APIs
19//!
20//! Batch removal:
21//! - [BTreeMap::remove_range()]
22//! - [BTreeMap::remove_range_with()]
23//!
24//! Adjacent entry:
25//! - [Entry::peek_forward()]
26//! - [Entry::peek_backward()]
27//! - [Entry::move_forward()]
28//! - [Entry::move_backward()]
29//! - [VacantEntry::peek_forward()]
30//! - [VacantEntry::peek_backward()]
31//! - [VacantEntry::move_forward()]
32//! - [VacantEntry::move_backward()]
33//! - [OccupiedEntry::peek_forward()]
34//! - [OccupiedEntry::peek_backward()]
35//! - [OccupiedEntry::move_forward()]
36//! - [OccupiedEntry::move_backward()]
37//! - [OccupiedEntry::alter_key()]
38//!
39//! Readonly [Cursor]:
40//! - [BTreeMap::cursor()]
41//! - [BTreeMap::first_cursor()]
42//! - [BTreeMap::last_cursor()]
43
44/*
45
46# Designer notes
47
48 Target scenario:  To maintain slab tree for disk, lets say 1 billion nodes, this design is aim for high fan-out.
49
50 Since There're no parent pointer, no fence keys. So we maintain a cache to accelerate the look
51 up for parent nodes.
52 Since we support combining cursor moving in Entry API (try to merge with previous or next adjacent
53 node). But user can call `remove()` on moved cursor.
54
55## Acceleration Search for finding the parent using PathCache
56
57PathCache is embedded the the end of TreeInfo, will delay allocation until the first split of leaf,
58which lead to the first InterNode allocation.
59
60- Assume PathCache is for Node A, Node B is the right brother of node A. A's ptr is at ParentA[idx]
61  - To find parent for Node B
62    - If idx < last (Cap - 1), then A and B has common parent,
63    - otherwise A and B have no common parent. Should continue iteration to upper PathCache. There will be common ancestor until idx < last
64
65- Assume PathCache is for Node B, Node A is the left brother of node B, B's ptr is at ParentB[idx]
66  - To find parent for Node A
67    - If idx > 0, then A and B has common parent.
68    - otherwise A and B have no common parent. Should continue iteration to upper PathCache. There will be common ancestor until idx > 0
69
70## Rebalance
71
72- When insert on a full LeafNode, we try to move items to left/right brother, to delay split operation.
73
74- One entry removed, current node usage < 50% (the threshold can be adjust according to tree size)
75
76  - we don't need to borrow data from brothers (to avoid trashing), and we have already done borrowing on insert.
77
78  - try to merge data, with left + current < cap, or current + right < cap, or left + current + right == 2 * cap
79
80  - No need to mere 3 -> 1, because before reaching 30% average usage, we already checked 3 -> 2.
81
82- The threshold to check merge for InterNode can be lower (like 30%), to avoid trashing
83
84
85## Future ideas
86
87- dynamically incress InterNode size, or variable size
88
89*/
90
91use 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
117/// B+Tree Map for single-threaded usage, optimized for numeric type.
118pub struct BTreeMap<K: Ord + Clone + Sized, V: Sized> {
119    /// Root node (may be None for empty tree)
120    /// Option<Node> is larger than Option<NonNull<NodeHeader>>
121    root: Option<NonNull<NodeHeader>>,
122    /// Number of elements in the tree
123    len: usize,
124    // use unsafe to avoid borrow problems
125    _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    /// Create a new empty BTreeMap
160    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    /// Returns the number of elements in the map
171    #[inline(always)]
172    pub fn len(&self) -> usize {
173        self.len
174    }
175
176    /// Returns true if the map is empty
177    #[inline(always)]
178    pub fn is_empty(&self) -> bool {
179        self.len == 0
180    }
181
182    /// return (cap_of_inter_node, cap_of_leaf_node)
183    #[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    /// Return the number of leaf nodes
191    #[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    /// Return the number of inter nodes
200    #[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    /// Return the average fill ratio of leaf nodes
258    ///
259    /// The range is [0.0, 100]
260    #[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    /// When root is leaf, returns 1, otherwise return the number of layers of inter node
271    #[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    /// Returns an entry to the key in the map
304    #[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            return 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    /// Returns true if the map contains the key
334    #[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    /// Returns a reference to the value corresponding to the key
344    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    /// Returns a mutable reference to the value corresponding to the key
359    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<'a>(&'a mut self, key: K, value: V) -> &'a mut V {
375        debug_assert!(self.root.is_none());
376        unsafe {
377            // empty tree
378            let mut leaf = LeafNode::<K, V>::alloc();
379            self.root = Some(leaf.to_root_ptr());
380            self.len = 1;
381            return &mut *leaf.insert_no_split_with_idx(0, key, value);
382        }
383    }
384
385    /// Insert a key-value pair into the map
386    /// Returns the old value if the key already existed
387    #[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                // Get the leaf node where we should insert
399                let count = leaf.key_count();
400                // Check if leaf has space
401                if count < LeafNode::<K, V>::cap() {
402                    leaf.insert_no_split_with_idx(idx, key, value);
403                } else {
404                    // Leaf is full, need to split
405                    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    /// Remove a key from the map, returning the value if it existed
416    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
417    where
418        K: Borrow<Q>,
419        Q: Ord + ?Sized,
420    {
421        let mut leaf = self
422            .search_leaf_with(|inter| inter.find_leaf_with_cache::<Q>(self.clear_cache(), key))?;
423        let (idx, is_equal) = leaf.search(key);
424        if is_equal {
425            trace_log!("{leaf:?} remove {idx}");
426            let val = leaf.remove_value_no_borrow(idx);
427            self.len -= 1;
428            // Check for underflow and handle merge
429            let new_count = leaf.key_count();
430            let min_count = LeafNode::<K, V>::cap() >> 1;
431            if new_count < min_count && self.root_is_inter() {
432                // The cache should already contain the path from the entry lookup
433                self.handle_leaf_underflow(leaf, true);
434            }
435            Some(val)
436        } else {
437            None
438        }
439    }
440
441    #[inline(always)]
442    fn root_is_inter(&self) -> bool {
443        if let Some(root) = self.root { !Node::<K, V>::root_is_leaf(root) } else { false }
444    }
445
446    /// Perform removal in batch mode and return the last item removed
447    ///
448    /// NOTE: this function speed up by skipping the underflow of LeafNode until the last operation.
449    pub fn remove_range<R>(&mut self, range: R) -> Option<(K, V)>
450    where
451        R: RangeBounds<K>,
452    {
453        self.remove_range_with::<R, _>(range, |_, _| {})
454    }
455
456    /// Perform removal in batch mode and return the last item removed
457    ///
458    /// On each removal, callback function `cb` is invoke with (ref of key, ref of value)
459    ///
460    /// NOTE: this function speed up by skipping the underflow of LeafNode until the last operation.
461    #[inline]
462    pub fn remove_range_with<R, F>(&mut self, range: R, mut cb: F) -> Option<(K, V)>
463    where
464        R: RangeBounds<K>,
465        F: FnMut(&K, &V),
466    {
467        macro_rules! end_contains {
468            ($key: expr) => {{
469                match range.end_bound() {
470                    Bound::Excluded(k) => $key < k,
471                    Bound::Included(k) => $key <= k,
472                    Bound::Unbounded => true,
473                }
474            }};
475        }
476        // Note: do not use find_leaf_with_bound, it's a different behavior
477        let mut ent = match range.start_bound() {
478            Bound::Excluded(k) => match self.entry(k.clone()).move_forward() {
479                Ok(ent) => {
480                    if end_contains!(ent.key()) {
481                        ent
482                    } else {
483                        return None;
484                    }
485                }
486                Err(_) => return None,
487            },
488            Bound::Included(k) => match self.entry(k.clone()) {
489                Entry::Occupied(ent) => ent,
490                Entry::Vacant(ent) => match ent.move_forward() {
491                    Ok(ent) => {
492                        if end_contains!(ent.key()) {
493                            ent
494                        } else {
495                            return None;
496                        }
497                    }
498                    Err(_) => return None,
499                },
500            },
501            Bound::Unbounded => {
502                if let Some(ent) = self.first_entry() {
503                    ent
504                } else {
505                    return None;
506                }
507            }
508        };
509        loop {
510            if let Some((_next_k, _next_v)) = ent.peek_forward() {
511                if end_contains!(_next_k) {
512                    let next_key = _next_k.clone();
513                    let (_k, _v) = ent._remove_entry(false);
514                    cb(&_k, &_v);
515                    if let Entry::Occupied(_ent) = self.entry(next_key) {
516                        ent = _ent;
517                        continue;
518                    } else {
519                        unreachable!();
520                    }
521                }
522            }
523            let (_k, _v) = ent._remove_entry(true);
524            cb(&_k, &_v);
525            return Some((_k, _v));
526        }
527    }
528
529    #[inline(always)]
530    fn get_root_unwrap(&self) -> Node<K, V> {
531        Node::<K, V>::from_root_ptr(*self.root.as_ref().unwrap())
532    }
533
534    #[inline(always)]
535    fn get_root(&self) -> Option<Node<K, V>> {
536        Some(Node::<K, V>::from_root_ptr(*self.root.as_ref()?))
537    }
538
539    /// return Some(leaf)
540    #[inline(always)]
541    fn search_leaf_with<F>(&self, search: F) -> Option<LeafNode<K, V>>
542    where
543        F: FnOnce(InterNode<K, V>) -> LeafNode<K, V>,
544    {
545        let root = self.root?;
546        if !Node::<K, V>::root_is_leaf(root) {
547            Some(search(InterNode::<K, V>::from(root)))
548        } else {
549            Some(LeafNode::<K, V>::from_root_ptr(root))
550        }
551    }
552
553    /// update the separate_key in parent after borrowing space from left/right node
554    #[inline(always)]
555    fn update_ancestor_sep_key<const MOVE: bool>(&mut self, sep_key: K) {
556        // if idx == 0, this is the leftmost ptr in the InterNode, we go up until finding a
557        // split key
558        let cache = self.get_info_mut();
559        let ret = if MOVE {
560            cache.move_to_ancenstor(|_node, idx| -> bool { idx > 0 }, dummy_post_callback)
561        } else {
562            cache.peek_ancenstor(|_node, idx| -> bool { idx > 0 })
563        };
564        if let Some((parent, parent_idx)) = ret {
565            trace_log!("update_ancestor_sep_key move={MOVE} at {parent:?}:{}", parent_idx - 1);
566            parent.change_key(parent_idx - 1, sep_key);
567            #[cfg(all(test, feature = "trace_log"))]
568            {
569                self.triggers |= TestFlag::UpdateSepKey as u32;
570            }
571        }
572    }
573
574    /// Insert with split handling - called when leaf is full
575    fn insert_with_split(
576        &mut self, key: K, value: V, mut leaf: LeafNode<K, V>, idx: u32,
577    ) -> *mut V {
578        debug_assert!(leaf.is_full());
579        let cap = LeafNode::<K, V>::cap();
580        if idx < cap {
581            // random insert, try borrow space from left and right
582            if let Some(mut left_node) = leaf.get_left_node()
583                && !left_node.is_full()
584            {
585                trace_log!("insert {leaf:?}:{idx} borrow left {left_node:?}");
586                let val_p = if idx == 0 {
587                    // leaf is not change, but since the insert pos is leftmost of this node, mean parent
588                    // separate_key <= key, need to update its separate_key
589                    left_node.insert_no_split_with_idx(left_node.key_count(), key, value)
590                } else {
591                    leaf.insert_borrow_left(&mut left_node, idx, key, value)
592                };
593                #[cfg(all(test, feature = "trace_log"))]
594                {
595                    self.triggers |= TestFlag::LeafMoveLeft as u32;
596                }
597                self.update_ancestor_sep_key::<true>(leaf.clone_first_key());
598                return val_p;
599            }
600        } else {
601            // insert into empty new node, left is probably full, right is probably none
602        }
603        if let Some(mut right_node) = leaf.get_right_node()
604            && !right_node.is_full()
605        {
606            trace_log!("insert {leaf:?}:{idx} borrow right {right_node:?}");
607            let val_p = if idx == cap {
608                // leaf is not change, in this condition, right_node is the leftmost child
609                // of its parent, key < right_node.get_keys()[0]
610                right_node.insert_no_split_with_idx(0, key, value)
611            } else {
612                leaf.borrow_right(&mut right_node);
613                leaf.insert_no_split_with_idx(idx, key, value)
614            };
615            #[cfg(all(test, feature = "trace_log"))]
616            {
617                self.triggers |= TestFlag::LeafMoveRight as u32;
618            }
619            self.get_info_mut().move_right();
620            self.update_ancestor_sep_key::<true>(right_node.clone_first_key());
621            return val_p;
622        }
623        #[cfg(all(test, feature = "trace_log"))]
624        {
625            self.triggers |= TestFlag::LeafSplit as u32;
626        }
627        let (new_leaf, ptr_v) = leaf.insert_with_split(idx, key, value);
628        let split_key = unsafe { (*new_leaf.key_ptr(0)).assume_init_ref().clone() };
629
630        let o_info = self._get_info();
631        if let Some(info) = o_info.as_mut() {
632            info.inc_leaf_count();
633            match self.propagate_split(info, split_key, leaf.get_ptr(), new_leaf.get_ptr()) {
634                Ok(_flags) => {
635                    #[cfg(all(test, feature = "trace_log"))]
636                    {
637                        self.triggers |= _flags;
638                    }
639                }
640                Err(new_root) => {
641                    self.root.replace(new_root.to_root_ptr());
642                }
643            }
644            ptr_v
645        } else {
646            o_info.replace(TreeInfo::new(2, 1));
647            let new_root =
648                InterNode::<K, V>::new_root(1, split_key, leaf.get_ptr(), new_leaf.get_ptr());
649            let _old_root = self.root.replace(new_root.to_root_ptr());
650            debug_assert_eq!(_old_root.unwrap(), leaf.to_root_ptr());
651            ptr_v
652        }
653    }
654
655    /// Propagate node split up the tree using iteration (non-recursive)
656    /// First tries to borrow space from left/right sibling before splitting
657    ///
658    /// left_ptr: existing child
659    /// right_ptr: new_child split from left_ptr
660    /// promote_key: sep_key to split left_ptr & right_ptr
661    #[inline(always)]
662    fn propagate_split(
663        &self, info: &mut TreeInfo<K, V>, mut promote_key: K, mut left_ptr: *mut NodeHeader,
664        mut right_ptr: *mut NodeHeader,
665    ) -> Result<u32, InterNode<K, V>> {
666        let mut height = 0;
667        #[allow(unused_mut)]
668        let mut flags = 0;
669        // If we have parent nodes in cache, process them iteratively
670        while let Some((mut parent, idx)) = info.pop() {
671            if !parent.is_full() {
672                trace_log!("propagate_split {parent:?}:{idx} insert {right_ptr:p}");
673                // should insert next to left_ptr
674                parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
675                return Ok(flags);
676            } else {
677                // Parent is full, try to borrow space from sibling through grand_parent
678                if let Some((mut grand, grand_idx)) = info.peek_parent() {
679                    // Try to borrow from left sibling of parent
680                    if grand_idx > 0 {
681                        let mut left_parent = grand.get_child_as_inter(grand_idx - 1);
682                        if !left_parent.is_full() {
683                            #[cfg(all(test, feature = "trace_log"))]
684                            {
685                                flags |= TestFlag::InterMoveLeft as u32;
686                            }
687                            if idx == 0 {
688                                trace_log!(
689                                    "propagate_split rotate_left {grand:?}:{} first ->{left_parent:?} left {left_ptr:p} insert {idx} {right_ptr:p}",
690                                    grand_idx - 1
691                                );
692                                // special case: split from first child of parent
693                                let demote_key = grand.change_key(grand_idx - 1, promote_key);
694                                debug_assert_eq!(parent.get_child_ptr(0), left_ptr);
695                                unsafe { (*parent.child_ptr(0)) = right_ptr };
696                                left_parent.append(demote_key, left_ptr);
697                                #[cfg(all(test, feature = "trace_log"))]
698                                {
699                                    flags |= TestFlag::InterMoveLeftFirst as u32;
700                                }
701                            } else {
702                                trace_log!(
703                                    "propagate_split insert_rotate_left {grand:?}:{grand_idx} -> {left_parent:?} insert {idx} {right_ptr:p}"
704                                );
705                                parent.insert_rotate_left(
706                                    &mut grand,
707                                    grand_idx,
708                                    &mut left_parent,
709                                    idx,
710                                    promote_key,
711                                    right_ptr,
712                                );
713                            }
714                            return Ok(flags);
715                        }
716                    }
717                    // Try to borrow from right sibling of parent
718                    if grand_idx < grand.key_count() {
719                        let mut right_parent = grand.get_child_as_inter(grand_idx + 1);
720                        if !right_parent.is_full() {
721                            #[cfg(all(test, feature = "trace_log"))]
722                            {
723                                flags |= TestFlag::InterMoveRight as u32;
724                            }
725                            if idx == parent.key_count() {
726                                trace_log!(
727                                    "propagate_split rotate_right last {grand:?}:{grand_idx} -> {right_parent:?}:0 insert right {right_parent:?}:0 {right_ptr:p}"
728                                );
729                                // split from last child of parent
730                                let demote_key = grand.change_key(grand_idx, promote_key);
731                                right_parent.insert_at_front(right_ptr, demote_key);
732                                #[cfg(all(test, feature = "trace_log"))]
733                                {
734                                    flags |= TestFlag::InterMoveRightLast as u32;
735                                }
736                            } else {
737                                trace_log!(
738                                    "propagate_split rotate_right {grand:?}:{grand_idx} -> {right_parent:?}:0 insert {parent:?}:{idx} {right_ptr:p}"
739                                );
740                                parent.rotate_right(&mut grand, grand_idx, &mut right_parent);
741                                parent.insert_no_split_with_idx(idx, promote_key, right_ptr);
742                            }
743                            return Ok(flags);
744                        }
745                    }
746                }
747                height += 1;
748
749                // Cannot borrow from siblings, need to split internal node
750                let (right, _promote_key) = parent.insert_split(promote_key, right_ptr);
751                info.inc_inter_count();
752
753                promote_key = _promote_key;
754                right_ptr = right.get_ptr();
755                left_ptr = parent.get_ptr();
756                #[cfg(all(test, feature = "trace_log"))]
757                {
758                    flags |= TestFlag::InterSplit as u32;
759                }
760                // Continue to next parent in cache (loop will pop next parent)
761            }
762        }
763
764        info.ensure_cap(height + 1);
765        info.inc_inter_count();
766        // No more parents in cache, create new root
767        let new_root = InterNode::<K, V>::new_root(height + 1, promote_key, left_ptr, right_ptr);
768        // to avoid borrow issue, set root outside
769        #[cfg(debug_assertions)]
770        {
771            let _old_root = self.root.as_ref().unwrap();
772            // must be inter, don't have LEAF_MASK
773            assert_eq!(_old_root.as_ptr(), left_ptr);
774        }
775        return Err(new_root);
776    }
777
778    /// Handle leaf node underflow by merging with sibling
779    /// Uses PathCache to accelerate parent lookup
780    /// Following the merge strategy from Designer Notes:
781    /// - Try merge with left sibling (if left + current <= cap)
782    /// - Try merge with right sibling (if current + right <= cap)
783    /// - Try 3-node merge (if left + current + right <= 2 * cap)
784    fn handle_leaf_underflow(&mut self, mut leaf: LeafNode<K, V>, merge: bool) {
785        debug_assert!(!self.get_root_unwrap().is_leaf());
786        let cur_count = leaf.key_count();
787        let cap = LeafNode::<K, V>::cap();
788        debug_assert!(cur_count <= cap >> 1);
789        let mut can_unlink: bool = false;
790        let (mut left_avail, mut right_avail) = (0, 0);
791        let mut merge_right = false;
792        if cur_count == 0 {
793            trace_log!("handle_leaf_underflow {leaf:?} unlink");
794            // if the right and left are full, or they not exist, can come to this
795            can_unlink = true;
796        }
797        if merge {
798            if !can_unlink && let Some(mut left_node) = leaf.get_left_node() {
799                let left_count = left_node.key_count();
800                if left_count + cur_count <= cap {
801                    trace_log!(
802                        "handle_leaf_underflow {leaf:?} merge left {left_node:?} {cur_count}"
803                    );
804                    leaf.copy_left(&mut left_node, cur_count);
805                    can_unlink = true;
806                    #[cfg(all(test, feature = "trace_log"))]
807                    {
808                        self.triggers |= TestFlag::LeafMergeLeft as u32;
809                    }
810                } else {
811                    left_avail = cap - left_count;
812                }
813            }
814            if !can_unlink && let Some(mut right_node) = leaf.get_right_node() {
815                let right_count = right_node.key_count();
816                if right_count + cur_count <= cap {
817                    trace_log!(
818                        "handle_leaf_underflow {leaf:?} merge right {right_node:?} {cur_count}"
819                    );
820                    leaf.copy_right::<false>(&mut right_node, 0, cur_count);
821                    can_unlink = true;
822                    merge_right = true;
823                    #[cfg(all(test, feature = "trace_log"))]
824                    {
825                        self.triggers |= TestFlag::LeafMergeRight as u32;
826                    }
827                } else {
828                    right_avail = cap - right_count;
829                }
830            }
831            // if we require left_avail + right_avail > cur_count, not possible to construct a 3-2
832            // merge, only either triggering merge left or merge right.
833            if !can_unlink
834                && left_avail > 0
835                && right_avail > 0
836                && left_avail + right_avail == cur_count
837            {
838                let mut left_node = leaf.get_left_node().unwrap();
839                let mut right_node = leaf.get_right_node().unwrap();
840                debug_assert!(left_avail < cur_count);
841                trace_log!("handle_leaf_underflow {leaf:?} merge left {left_node:?} {left_avail}");
842                leaf.copy_left(&mut left_node, left_avail);
843                trace_log!(
844                    "handle_leaf_underflow {leaf:?} merge right {right_node:?} {}",
845                    cur_count - left_avail
846                );
847                leaf.copy_right::<false>(&mut right_node, left_avail, cur_count - left_avail);
848                merge_right = true;
849                can_unlink = true;
850                #[cfg(all(test, feature = "trace_log"))]
851                {
852                    self.triggers |=
853                        TestFlag::LeafMergeLeft as u32 | TestFlag::LeafMergeRight as u32;
854                }
855            }
856        }
857        if !can_unlink {
858            return;
859        }
860        self.get_info_mut().dec_leaf_count();
861        let right_sep = if merge_right {
862            let right_node = leaf.get_right_node().unwrap();
863            Some(right_node.clone_first_key())
864        } else {
865            None
866        };
867        let no_right = leaf.unlink().is_null();
868        leaf.dealloc::<false>();
869        let (mut parent, mut idx) = self.get_info_mut().pop().unwrap();
870        trace_log!("handle_leaf_underflow pop parent {parent:?}:{idx}");
871        if parent.key_count() == 0 {
872            if let Some((grand, grand_idx)) = self.remove_only_child(parent) {
873                trace_log!("handle_leaf_underflow remove_only_child until {grand:?}:{grand_idx}");
874                parent = grand;
875                idx = grand_idx;
876            } else {
877                trace_log!("handle_leaf_underflow remove_only_child all");
878                return;
879            }
880        }
881        self.remove_child_from_inter(&mut parent, idx, right_sep, no_right);
882        if parent.key_count() <= 1 {
883            self.handle_inter_underflow(parent);
884        }
885    }
886
887    /// To simplify the logic, we perform delete first.
888    /// return the Some(node) when need to rebalance
889    #[inline]
890    fn remove_child_from_inter(
891        &mut self, node: &mut InterNode<K, V>, delete_idx: u32, right_sep: Option<K>,
892        _no_right: bool,
893    ) {
894        debug_assert!(node.key_count() > 0, "{:?} {}", node, node.key_count());
895        if delete_idx == node.key_count() {
896            trace_log!("remove_child_from_inter {node:?}:{delete_idx} last");
897            #[cfg(all(test, feature = "trace_log"))]
898            {
899                self.triggers |= TestFlag::RemoveChildLast as u32;
900            }
901            // delete the last child of this node
902            node.remove_last_child();
903            if let Some(key) = right_sep
904                && let Some((grand_parent, grand_idx)) = self.get_info_mut().peek_ancenstor(
905                    |_node: &InterNode<K, V>, idx: u32| -> bool { _node.key_count() > idx },
906                )
907            {
908                #[cfg(all(test, feature = "trace_log"))]
909                {
910                    self.triggers |= TestFlag::UpdateSepKey as u32;
911                }
912                trace_log!("remove_child_from_inter change_key {grand_parent:?}:{grand_idx}");
913                // key idx = child idx - 1 , and + 1 for right node
914                grand_parent.change_key(grand_idx, key);
915            }
916        } else if delete_idx > 0 {
917            trace_log!("remove_child_from_inter {node:?}:{delete_idx} mid");
918            node.remove_mid_child(delete_idx);
919            #[cfg(all(test, feature = "trace_log"))]
920            {
921                self.triggers |= TestFlag::RemoveChildMid as u32;
922            }
923            // sep key of right node shift left
924            if let Some(key) = right_sep {
925                trace_log!("remove_child_from_inter change_key {node:?}:{}", delete_idx - 1);
926                node.change_key(delete_idx - 1, key);
927                #[cfg(all(test, feature = "trace_log"))]
928                {
929                    self.triggers |= TestFlag::UpdateSepKey as u32;
930                }
931            }
932        } else {
933            trace_log!("remove_child_from_inter {node:?}:{delete_idx} first");
934            // delete_idx is the first but not the last
935            let mut sep_key = node.remove_first_child();
936            #[cfg(all(test, feature = "trace_log"))]
937            {
938                self.triggers |= TestFlag::RemoveChildFirst as u32;
939            }
940            if let Some(key) = right_sep {
941                sep_key = key;
942            }
943            self.update_ancestor_sep_key::<false>(sep_key);
944        }
945    }
946
947    #[inline]
948    fn handle_inter_underflow(&mut self, mut node: InterNode<K, V>) {
949        let cap = InterNode::<K, V>::cap();
950        let mut root_height = 0;
951        let mut _flags = 0;
952        let cache = self.get_info_mut();
953        cache.assert_center();
954        while node.key_count() <= InterNode::<K, V>::UNDERFLOW_CAP {
955            if node.key_count() == 0 {
956                if root_height == 0 {
957                    root_height = self.get_root_unwrap().height();
958                }
959                let node_height = node.height();
960                if node_height == root_height
961                    || cache
962                        .peek_ancenstor(|_node: &InterNode<K, V>, _idx: u32| -> bool {
963                            _node.key_count() > 0
964                        })
965                        .is_none()
966                {
967                    let child_ptr = unsafe { *node.child_ptr(0) };
968                    debug_assert!(!child_ptr.is_null());
969                    let root = if node_height == 1 {
970                        LeafNode::<K, V>::wrap_root_ptr(child_ptr)
971                    } else {
972                        unsafe { NonNull::new_unchecked(child_ptr) }
973                    };
974                    trace_log!(
975                        "handle_inter_underflow downgrade root {:?}",
976                        Node::<K, V>::from_root_ptr(root)
977                    );
978                    let _old_root = self.root.replace(root);
979                    debug_assert!(_old_root.is_some());
980
981                    let info = self.get_info_mut();
982                    while let Some((parent, _)) = info.pop() {
983                        parent.dealloc::<false>();
984                        info.dec_inter_count();
985                    }
986                    node.dealloc::<false>(); // they all have no key
987                    info.dec_inter_count();
988                }
989                break;
990            } else {
991                if let Some((mut grand, grand_idx)) = cache.pop() {
992                    if grand_idx > 0 {
993                        let mut left = grand.get_child_as_inter(grand_idx - 1);
994                        // the sep key should pull down,  key+1 + key + 1 > cap + 1
995                        if left.key_count() + node.key_count() < cap {
996                            #[cfg(all(test, feature = "trace_log"))]
997                            {
998                                _flags |= TestFlag::InterMergeLeft as u32;
999                            }
1000                            trace_log!(
1001                                "handle_inter_underflow {node:?} merge left {left:?} parent {grand:?}:{grand_idx}"
1002                            );
1003                            left.merge(node, &mut grand, grand_idx);
1004                            node = grand;
1005                            continue;
1006                        }
1007                    }
1008                    if grand_idx < grand.key_count() {
1009                        let right = grand.get_child_as_inter(grand_idx + 1);
1010                        // the sep key should pull down,  key+1 + key + 1 > cap + 1
1011                        if right.key_count() + node.key_count() < cap {
1012                            #[cfg(all(test, feature = "trace_log"))]
1013                            {
1014                                _flags |= TestFlag::InterMergeRight as u32;
1015                            }
1016                            trace_log!(
1017                                "handle_inter_underflow {node:?} cap {cap} merge right {right:?} parent {grand:?}:{}",
1018                                grand_idx + 1
1019                            );
1020                            node.merge(right, &mut grand, grand_idx + 1);
1021                            node = grand;
1022                            continue;
1023                        }
1024                    }
1025                }
1026                let _ = cache;
1027                break;
1028            }
1029        }
1030        #[cfg(all(test, feature = "trace_log"))]
1031        {
1032            self.triggers |= _flags;
1033        }
1034    }
1035
1036    #[inline]
1037    fn remove_only_child(&mut self, node: InterNode<K, V>) -> Option<(InterNode<K, V>, u32)> {
1038        debug_assert_eq!(node.key_count(), 0);
1039        #[cfg(all(test, feature = "trace_log"))]
1040        {
1041            self.triggers |= TestFlag::RemoveOnlyChild as u32;
1042        }
1043        let info = self.get_info_mut();
1044        if let Some((parent, idx)) = info.move_to_ancenstor(
1045            |node: &InterNode<K, V>, _idx: u32| -> bool { node.key_count() != 0 },
1046            |_info, node| {
1047                _info.dec_inter_count();
1048                node.dealloc::<false>();
1049            },
1050        ) {
1051            node.dealloc::<true>();
1052            info.dec_inter_count();
1053            Some((parent, idx))
1054        } else {
1055            node.dealloc::<true>();
1056            info.dec_inter_count();
1057            // we are empty, my ancestor are all empty and delete by move_to_ancenstor
1058            self.root = None;
1059            None
1060        }
1061    }
1062
1063    /// Dump the entire tree structure for debugging
1064    #[cfg(all(test, feature = "std"))]
1065    pub fn dump(&self)
1066    where
1067        K: Debug,
1068        V: Debug,
1069    {
1070        print_log!("=== BTreeMap Dump ===");
1071        print_log!("Length: {}", self.len());
1072        if let Some(root) = self.get_root() {
1073            self.dump_node(&root, 0);
1074        } else {
1075            print_log!("(empty)");
1076        }
1077        print_log!("=====================");
1078    }
1079
1080    #[cfg(all(test, feature = "std"))]
1081    fn dump_node(&self, node: &Node<K, V>, depth: usize)
1082    where
1083        K: Debug,
1084        V: Debug,
1085    {
1086        match node {
1087            Node::Leaf(leaf) => {
1088                print!("{:indent$}", "", indent = depth * 2);
1089                print_log!("{}", leaf);
1090            }
1091            Node::Inter(inter) => {
1092                print!("{:indent$}", "", indent = depth * 2);
1093                print_log!("{}", inter);
1094                // Dump children
1095                let count = inter.key_count() as u32;
1096                for i in 0..=count {
1097                    unsafe {
1098                        let child_ptr = *inter.child_ptr(i);
1099                        if !child_ptr.is_null() {
1100                            let child_node = if (*child_ptr).is_leaf() {
1101                                Node::Leaf(LeafNode::<K, V>::from_header(child_ptr))
1102                            } else {
1103                                Node::Inter(InterNode::<K, V>::from_header(child_ptr))
1104                            };
1105                            self.dump_node(&child_node, depth + 1);
1106                        }
1107                    }
1108                }
1109            }
1110        }
1111    }
1112
1113    /// Validate the entire tree structure
1114    /// Uses the same traversal logic as Drop to avoid recursion
1115    pub fn validate(&self)
1116    where
1117        K: Debug,
1118        V: Debug,
1119    {
1120        let root = if let Some(_root) = self.get_root() {
1121            _root
1122        } else {
1123            assert_eq!(self.len, 0, "Empty tree should have len 0");
1124            return;
1125        };
1126        let mut total_keys = 0usize;
1127        let mut prev_leaf_max: Option<K> = None;
1128
1129        match root {
1130            Node::Leaf(leaf) => {
1131                total_keys += leaf.validate(None, None);
1132            }
1133            Node::Inter(inter) => {
1134                // Do not use btree internal PathCache (might distrupt test scenario)
1135                let mut cache = TreeInfo::new(0, 0);
1136                cache.ensure_cap(inter.height());
1137                let mut cur = inter.clone();
1138                loop {
1139                    cache.push(cur.clone(), 0);
1140                    cur.validate();
1141                    match cur.get_child(0) {
1142                        Node::Leaf(leaf) => {
1143                            // Validate first leaf with no min/max bounds from parent
1144                            let min_key: Option<K> = None;
1145                            let max_key = if inter.key_count() > 0 {
1146                                unsafe { Some((*inter.key_ptr(0)).assume_init_ref().clone()) }
1147                            } else {
1148                                None
1149                            };
1150                            total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1151                            if let Some(ref prev_max) = prev_leaf_max {
1152                                let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1153                                assert!(
1154                                    prev_max < first_key,
1155                                    "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1156                                    leaf,
1157                                    prev_max,
1158                                    first_key
1159                                );
1160                            }
1161                            prev_leaf_max = unsafe {
1162                                Some(
1163                                    (*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone(),
1164                                )
1165                            };
1166                            break;
1167                        }
1168                        Node::Inter(child_inter) => {
1169                            cur = child_inter;
1170                        }
1171                    }
1172                }
1173
1174                // Continue traversal like Drop does
1175                while let Some((parent, idx)) =
1176                    cache.move_right_and_pop_l1(dummy_post_callback::<K, V>)
1177                {
1178                    cache.push(parent.clone(), idx);
1179                    if let Node::Leaf(leaf) = parent.get_child(idx) {
1180                        // Calculate bounds for this leaf
1181                        let min_key = if idx > 0 {
1182                            unsafe { Some((*parent.key_ptr(idx - 1)).assume_init_ref().clone()) }
1183                        } else {
1184                            None
1185                        };
1186                        let max_key = if idx < parent.key_count() {
1187                            unsafe { Some((*parent.key_ptr(idx)).assume_init_ref().clone()) }
1188                        } else {
1189                            None
1190                        };
1191                        total_keys += leaf.validate(min_key.as_ref(), max_key.as_ref());
1192
1193                        // Check ordering with previous leaf
1194                        if let Some(ref prev_max) = prev_leaf_max {
1195                            let first_key = unsafe { (*leaf.key_ptr(0)).assume_init_ref() };
1196                            assert!(
1197                                prev_max < first_key,
1198                                "{:?} Leaf keys not in order: prev max {:?} >= current min {:?}",
1199                                leaf,
1200                                prev_max,
1201                                first_key
1202                            );
1203                        }
1204                        prev_leaf_max = unsafe {
1205                            Some((*leaf.key_ptr(leaf.key_count() - 1)).assume_init_ref().clone())
1206                        };
1207                    } else {
1208                        panic!("{parent:?} child {:?} is not leaf", parent.get_child(idx));
1209                    }
1210                }
1211            }
1212        }
1213        assert_eq!(
1214            total_keys, self.len,
1215            "Total keys in tree ({}) doesn't match len ({})",
1216            total_keys, self.len
1217        );
1218    }
1219
1220    /// Returns the first key-value pair in the map
1221    /// Returns `None` if the map is empty
1222    #[inline]
1223    pub fn first_key_value(&self) -> Option<(&K, &V)> {
1224        let leaf = self.search_leaf_with(|inter| inter.find_first_leaf(None))?;
1225        debug_assert!(leaf.key_count() > 0);
1226        unsafe {
1227            let key = (*leaf.key_ptr(0)).assume_init_ref();
1228            let value = (*leaf.value_ptr(0)).assume_init_ref();
1229            Some((key, value))
1230        }
1231    }
1232
1233    /// Returns the last key-value pair in the map
1234    /// Returns `None` if the map is empty
1235    #[inline]
1236    pub fn last_key_value(&self) -> Option<(&K, &V)> {
1237        let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(None))?;
1238        let count = leaf.key_count();
1239        debug_assert!(count > 0);
1240        unsafe {
1241            let last_idx = count - 1;
1242            let key = (*leaf.key_ptr(last_idx)).assume_init_ref();
1243            let value = (*leaf.value_ptr(last_idx)).assume_init_ref();
1244            Some((key, value))
1245        }
1246    }
1247
1248    /// Returns an entry to the first key in the map
1249    /// Returns `None` if the map is empty
1250    #[inline]
1251    pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1252        let leaf =
1253            self.search_leaf_with(|inter| inter.find_first_leaf(Some(self.clear_cache())))?;
1254        if leaf.key_count() > 0 {
1255            Some(OccupiedEntry { tree: self, idx: 0, leaf })
1256        } else {
1257            // when root is leaf, remove_entry does not dealloc the leaf
1258            None
1259        }
1260    }
1261
1262    /// Returns an entry to the last key in the map
1263    /// Returns `None` if the map is empty
1264    #[inline]
1265    pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
1266        let leaf = self.search_leaf_with(|inter| inter.find_last_leaf(Some(self.clear_cache())))?;
1267        let count = leaf.key_count();
1268        if count > 0 {
1269            Some(OccupiedEntry { tree: self, idx: count - 1, leaf })
1270        } else {
1271            // when root is leaf, remove_entry does not dealloc the leaf
1272            None
1273        }
1274    }
1275
1276    /// Removes and returns the first key-value pair in the map
1277    /// Returns `None` if the map is empty
1278    #[inline]
1279    pub fn pop_first(&mut self) -> Option<(K, V)> {
1280        match self.first_entry() {
1281            Some(entry) => Some(entry.remove_entry()),
1282            _ => None,
1283        }
1284    }
1285
1286    /// Removes and returns the last key-value pair in the map
1287    /// Returns `None` if the map is empty
1288    #[inline]
1289    pub fn pop_last(&mut self) -> Option<(K, V)> {
1290        match self.last_entry() {
1291            Some(entry) => Some(entry.remove_entry()),
1292            _ => None,
1293        }
1294    }
1295
1296    /// Returns an iterator over the map's entries
1297    #[inline]
1298    pub fn iter(&self) -> Iter<'_, K, V> {
1299        Iter::new(self.find_first_and_last_leaf(), self.len)
1300    }
1301
1302    /// Returns a mutable iterator over the map's entries
1303    #[inline]
1304    pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
1305        IterMut::new(self.find_first_and_last_leaf(), self.len)
1306    }
1307
1308    /// Return a consuming iterator in reversed order
1309    #[inline]
1310    pub fn into_iter_rev(self) -> IntoIter<K, V> {
1311        IntoIter::new(self, false)
1312    }
1313
1314    /// Returns an iterator over the map's keys
1315    #[inline]
1316    pub fn keys(&self) -> Keys<'_, K, V> {
1317        Keys::new(self.iter())
1318    }
1319
1320    /// Returns an iterator over the map's values
1321    #[inline]
1322    pub fn values(&self) -> Values<'_, K, V> {
1323        Values::new(self.iter())
1324    }
1325
1326    /// Returns a mutable iterator over the map's values
1327    #[inline]
1328    pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
1329        ValuesMut::new(self.iter_mut())
1330    }
1331
1332    #[inline]
1333    fn find_first_and_last_leaf(&self) -> Option<(LeafNode<K, V>, LeafNode<K, V>)> {
1334        let root = self.root?;
1335        if !Node::<K, V>::root_is_leaf(root) {
1336            let inter = InterNode::<K, V>::from(root);
1337            Some((inter.clone().find_first_leaf(None), inter.find_last_leaf(None)))
1338        } else {
1339            let leaf = LeafNode::<K, V>::from_root_ptr(root);
1340            Some((leaf.clone(), leaf))
1341        }
1342    }
1343
1344    /// Internal helper to find range bounds
1345    /// Returns (front_leaf, front_idx, back_leaf, back_idx) where both leaves are Some or both are None
1346    #[inline]
1347    fn find_range_bounds<R>(&self, range: R) -> Option<RangeBase<'_, K, V>>
1348    where
1349        R: RangeBounds<K>,
1350    {
1351        let root = self.get_root()?;
1352        let (front_leaf, front_idx) = root.find_leaf_with_bound(range.start_bound(), true);
1353        let (back_leaf, back_idx) = root.find_leaf_with_bound(range.end_bound(), false);
1354        Some(RangeBase::new(front_leaf, front_idx, back_leaf, back_idx))
1355    }
1356
1357    /// Returns an iterator over a sub-range of entries in the map
1358    #[inline]
1359    pub fn range<R>(&self, range: R) -> Range<'_, K, V>
1360    where
1361        R: RangeBounds<K>,
1362    {
1363        Range::new(self.find_range_bounds(range))
1364    }
1365
1366    /// Returns a mutable iterator over a sub-range of entries in the map
1367    #[inline]
1368    pub fn range_mut<R>(&mut self, range: R) -> RangeMut<'_, K, V>
1369    where
1370        R: RangeBounds<K>,
1371    {
1372        RangeMut::new(self.find_range_bounds(range))
1373    }
1374
1375    /// Returns a cursor positioned at the first entry of the map.
1376    /// Returns `None` if the map is empty.
1377    #[inline]
1378    pub fn first_cursor(&self) -> Cursor<'_, K, V> {
1379        if let Some(leaf) = self.search_leaf_with(|inter| inter.find_first_leaf(None)) {
1380            if leaf.key_count() > 0 {
1381                return Cursor {
1382                    leaf: Some(leaf),
1383                    is_exist: true,
1384                    idx: 0,
1385                    _marker: Default::default(),
1386                };
1387            }
1388        }
1389        Cursor { leaf: None, is_exist: true, idx: 0, _marker: Default::default() }
1390    }
1391
1392    /// Returns a cursor positioned at the last entry of the map.
1393    /// Returns `None` if the map is empty.
1394    #[inline]
1395    pub fn last_cursor(&self) -> Cursor<'_, K, V> {
1396        if let Some(leaf) = self.search_leaf_with(|inter| inter.find_last_leaf(None)) {
1397            let count = leaf.key_count();
1398            if count > 0 {
1399                return Cursor {
1400                    leaf: Some(leaf),
1401                    idx: count - 1,
1402                    is_exist: true,
1403                    _marker: Default::default(),
1404                };
1405            }
1406        }
1407        Cursor { leaf: None, idx: 0, is_exist: false, _marker: Default::default() }
1408    }
1409
1410    /// Returns a cursor positioned at the given key.
1411    ///
1412    /// NOTE: There's slight different for existing/non-existing key, refer to the doc of [Cursor]
1413    #[inline]
1414    pub fn cursor<Q>(&self, key: &Q) -> Cursor<'_, K, V>
1415    where
1416        K: Borrow<Q>,
1417        Q: Ord + ?Sized,
1418    {
1419        if let Some(leaf) = self.search_leaf_with(|inter| inter.find_leaf(key)) {
1420            let (idx, is_exist) = leaf.search(key);
1421            Cursor { leaf: Some(leaf), idx, is_exist, _marker: Default::default() }
1422        } else {
1423            Cursor { leaf: None, idx: 0, is_exist: false, _marker: Default::default() }
1424        }
1425    }
1426}
1427
1428impl<K: Ord + Clone + Sized, V: Sized> Default for BTreeMap<K, V> {
1429    fn default() -> Self {
1430        Self::new()
1431    }
1432}
1433
1434impl<K: Ord + Clone + Sized, V: Sized> Drop for BTreeMap<K, V> {
1435    fn drop(&mut self) {
1436        if let Some(root) = self.root {
1437            if Node::<K, V>::root_is_leaf(root) {
1438                let leaf = LeafNode::<K, V>::from_root_ptr(root);
1439                leaf.dealloc::<true>();
1440            } else {
1441                let inter = InterNode::<K, V>::from(root);
1442                let mut cache = self.take_cache().expect("should have cache");
1443                let mut cur = inter.find_first_leaf(Some(&mut cache));
1444                cur.dealloc::<true>();
1445                // To navigate to next leaf,
1446                // return None when reach the end
1447                while let Some((parent, idx)) =
1448                    cache.move_right_and_pop_l1(|_info, _node| _node.dealloc::<true>())
1449                {
1450                    cache.push(parent.clone(), idx);
1451                    cur = parent.get_child_as_leaf(idx);
1452                    cur.dealloc::<true>();
1453                }
1454            }
1455        }
1456    }
1457}
1458
1459impl<K: Ord + Clone + Sized, V: Sized> IntoIterator for BTreeMap<K, V> {
1460    type Item = (K, V);
1461    type IntoIter = IntoIter<K, V>;
1462
1463    #[inline]
1464    fn into_iter(self) -> Self::IntoIter {
1465        IntoIter::new(self, true)
1466    }
1467}
1468
1469impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a BTreeMap<K, V> {
1470    type Item = (&'a K, &'a V);
1471    type IntoIter = Iter<'a, K, V>;
1472
1473    #[inline]
1474    fn into_iter(self) -> Self::IntoIter {
1475        self.iter()
1476    }
1477}
1478
1479impl<'a, K: Ord + Clone + Sized, V: Sized> IntoIterator for &'a mut BTreeMap<K, V> {
1480    type Item = (&'a K, &'a mut V);
1481    type IntoIter = IterMut<'a, K, V>;
1482
1483    #[inline]
1484    fn into_iter(self) -> Self::IntoIter {
1485        self.iter_mut()
1486    }
1487}