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