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