Skip to main content

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