@@ -174,7 +174,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
174
174
175
175
{
176
176
let out_root = BTreeMap :: ensure_is_owned ( & mut out_tree. root ) ;
177
- let mut out_node = out_root. push_level ( ) ;
177
+ let mut out_node = out_root. push_internal_level ( ) ;
178
178
let mut in_edge = internal. first_edge ( ) ;
179
179
while let Ok ( kv) = in_edge. right_kv ( ) {
180
180
let ( k, v) = kv. into_kv ( ) ;
@@ -1080,9 +1080,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
1080
1080
test_node = parent. forget_type ( ) ;
1081
1081
}
1082
1082
}
1083
- Err ( node ) => {
1083
+ Err ( _ ) => {
1084
1084
// We are at the top, create a new root node and push there.
1085
- open_node = node . into_root_mut ( ) . push_level ( ) ;
1085
+ open_node = root . push_internal_level ( ) ;
1086
1086
break ;
1087
1087
}
1088
1088
}
@@ -1092,7 +1092,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1092
1092
let tree_height = open_node. height ( ) - 1 ;
1093
1093
let mut right_tree = node:: Root :: new_leaf ( ) ;
1094
1094
for _ in 0 ..tree_height {
1095
- right_tree. push_level ( ) ;
1095
+ right_tree. push_internal_level ( ) ;
1096
1096
}
1097
1097
open_node. push ( key, value, right_tree) ;
1098
1098
@@ -1171,7 +1171,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1171
1171
let mut right = Self :: new ( ) ;
1172
1172
let right_root = Self :: ensure_is_owned ( & mut right. root ) ;
1173
1173
for _ in 0 ..left_root. height ( ) {
1174
- right_root. push_level ( ) ;
1174
+ right_root. push_internal_level ( ) ;
1175
1175
}
1176
1176
1177
1177
{
@@ -1255,7 +1255,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
1255
1255
}
1256
1256
pub ( super ) fn drain_filter_inner ( & mut self ) -> DrainFilterInner < ' _ , K , V > {
1257
1257
let front = self . root . as_mut ( ) . map ( |r| r. as_mut ( ) . first_leaf_edge ( ) ) ;
1258
- DrainFilterInner { length : & mut self . length , cur_leaf_edge : front }
1258
+ DrainFilterInner {
1259
+ length : & mut self . length ,
1260
+ cur_leaf_edge : front,
1261
+ emptied_internal_root : false ,
1262
+ }
1259
1263
}
1260
1264
1261
1265
/// Calculates the number of elements if it is incorrect.
@@ -1625,6 +1629,7 @@ where
1625
1629
pub ( super ) struct DrainFilterInner < ' a , K : ' a , V : ' a > {
1626
1630
length : & ' a mut usize ,
1627
1631
cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1632
+ emptied_internal_root : bool ,
1628
1633
}
1629
1634
1630
1635
#[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
@@ -1665,6 +1670,17 @@ where
1665
1670
}
1666
1671
}
1667
1672
1673
+ impl < K , V > Drop for DrainFilterInner < ' _ , K , V > {
1674
+ fn drop ( & mut self ) {
1675
+ if self . emptied_internal_root {
1676
+ if let Some ( handle) = self . cur_leaf_edge . take ( ) {
1677
+ let root = handle. into_node ( ) . into_root_mut ( ) ;
1678
+ root. pop_internal_level ( ) ;
1679
+ }
1680
+ }
1681
+ }
1682
+ }
1683
+
1668
1684
impl < ' a , K : ' a , V : ' a > DrainFilterInner < ' a , K , V > {
1669
1685
/// Allow Debug implementations to predict the next element.
1670
1686
pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
@@ -1681,9 +1697,10 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
1681
1697
let ( k, v) = kv. kv_mut ( ) ;
1682
1698
if pred ( k, v) {
1683
1699
* self . length -= 1 ;
1684
- let ( k, v, leaf_edge_location) = kv. remove_kv_tracking ( ) ;
1685
- self . cur_leaf_edge = Some ( leaf_edge_location) ;
1686
- return Some ( ( k, v) ) ;
1700
+ let RemoveResult { old_kv, pos, emptied_internal_root } = kv. remove_kv_tracking ( ) ;
1701
+ self . cur_leaf_edge = Some ( pos) ;
1702
+ self . emptied_internal_root |= emptied_internal_root;
1703
+ return Some ( old_kv) ;
1687
1704
}
1688
1705
self . cur_leaf_edge = Some ( kv. next_leaf_edge ( ) ) ;
1689
1706
}
@@ -2477,7 +2494,7 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
2477
2494
}
2478
2495
} ,
2479
2496
Err ( root) => {
2480
- root. push_level ( ) . push ( ins_k, ins_v, ins_edge) ;
2497
+ root. push_internal_level ( ) . push ( ins_k, ins_v, ins_edge) ;
2481
2498
return unsafe { & mut * out_ptr } ;
2482
2499
}
2483
2500
}
@@ -2647,20 +2664,35 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
2647
2664
self . remove_kv ( ) . 1
2648
2665
}
2649
2666
2667
+ // Body of `remove_entry`, separate to keep the above implementations short.
2650
2668
fn remove_kv ( self ) -> ( K , V ) {
2651
2669
* self . length -= 1 ;
2652
2670
2653
- let ( old_key, old_val, _) = self . handle . remove_kv_tracking ( ) ;
2654
- ( old_key, old_val)
2671
+ let RemoveResult { old_kv, pos, emptied_internal_root } = self . handle . remove_kv_tracking ( ) ;
2672
+ let root = pos. into_node ( ) . into_root_mut ( ) ;
2673
+ if emptied_internal_root {
2674
+ root. pop_internal_level ( ) ;
2675
+ }
2676
+ old_kv
2655
2677
}
2656
2678
}
2657
2679
2680
+ struct RemoveResult < ' a , K , V > {
2681
+ // Key and value removed.
2682
+ old_kv : ( K , V ) ,
2683
+ // Unique location at the leaf level that the removed KV lopgically collapsed into.
2684
+ pos : Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > ,
2685
+ // Whether the remove left behind and empty internal root node, that should be removed
2686
+ // using `pop_internal_level`.
2687
+ emptied_internal_root : bool ,
2688
+ }
2689
+
2658
2690
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
2659
- /// Removes a key/value-pair from the map , and returns that pair, as well as
2660
- /// the leaf edge corresponding to that former pair.
2661
- fn remove_kv_tracking (
2662
- self ,
2663
- ) -> ( K , V , Handle < NodeRef < marker :: Mut < ' a > , K , V , marker :: Leaf > , marker :: Edge > ) {
2691
+ /// Removes a key/value-pair from the tree , and returns that pair, as well as
2692
+ /// the leaf edge corresponding to that former pair. It's possible this leaves
2693
+ /// an empty internal root node, which the caller should subsequently pop from
2694
+ /// the map holding the tree. The caller should also decrement the map's length.
2695
+ fn remove_kv_tracking ( self ) -> RemoveResult < ' a , K , V > {
2664
2696
let ( mut pos, old_key, old_val, was_internal) = match self . force ( ) {
2665
2697
Leaf ( leaf) => {
2666
2698
let ( hole, old_key, old_val) = leaf. remove ( ) ;
@@ -2689,6 +2721,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2689
2721
} ;
2690
2722
2691
2723
// Handle underflow
2724
+ let mut emptied_internal_root = false ;
2692
2725
let mut cur_node = unsafe { ptr:: read ( & pos) . into_node ( ) . forget_type ( ) } ;
2693
2726
let mut at_leaf = true ;
2694
2727
while cur_node. len ( ) < node:: MIN_LEN {
@@ -2709,8 +2742,8 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2709
2742
2710
2743
let parent = edge. into_node ( ) ;
2711
2744
if parent. len ( ) == 0 {
2712
- // We must be at the root
2713
- parent . into_root_mut ( ) . pop_level ( ) ;
2745
+ // This empty parent must be the root, and should be popped off the tree.
2746
+ emptied_internal_root = true ;
2714
2747
break ;
2715
2748
} else {
2716
2749
cur_node = parent. forget_type ( ) ;
@@ -2737,15 +2770,15 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2737
2770
pos = unsafe { unwrap_unchecked ( pos. next_kv ( ) . ok ( ) ) . next_leaf_edge ( ) } ;
2738
2771
}
2739
2772
2740
- ( old_key, old_val, pos)
2773
+ RemoveResult { old_kv : ( old_key, old_val) , pos, emptied_internal_root }
2741
2774
}
2742
2775
}
2743
2776
2744
2777
impl < K , V > node:: Root < K , V > {
2745
2778
/// Removes empty levels on the top, but keep an empty leaf if the entire tree is empty.
2746
2779
fn fix_top ( & mut self ) {
2747
2780
while self . height ( ) > 0 && self . as_ref ( ) . len ( ) == 0 {
2748
- self . pop_level ( ) ;
2781
+ self . pop_internal_level ( ) ;
2749
2782
}
2750
2783
}
2751
2784
@@ -2817,8 +2850,16 @@ fn handle_underfull_node<K, V>(
2817
2850
let ( is_left, mut handle) = match parent. left_kv ( ) {
2818
2851
Ok ( left) => ( true , left) ,
2819
2852
Err ( parent) => {
2820
- let right = unsafe { unwrap_unchecked ( parent. right_kv ( ) . ok ( ) ) } ;
2821
- ( false , right)
2853
+ match parent. right_kv ( ) {
2854
+ Ok ( right) => ( false , right) ,
2855
+ Err ( _) => {
2856
+ // The underfull node has an empty parent, so it is the only child
2857
+ // of an empty root. It is destined to become the new root, thus
2858
+ // allowed to be underfull. The empty parent should be removed later
2859
+ // by `pop_internal_level`.
2860
+ return AtRoot ;
2861
+ }
2862
+ }
2822
2863
}
2823
2864
} ;
2824
2865
0 commit comments