@@ -1255,11 +1255,7 @@ 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 {
1259
- length : & mut self . length ,
1260
- cur_leaf_edge : front,
1261
- emptied_internal_root : false ,
1262
- }
1258
+ DrainFilterInner { length : & mut self . length , cur_leaf_edge : front }
1263
1259
}
1264
1260
1265
1261
/// Calculates the number of elements if it is incorrect.
@@ -1629,7 +1625,6 @@ where
1629
1625
pub ( super ) struct DrainFilterInner < ' a , K : ' a , V : ' a > {
1630
1626
length : & ' a mut usize ,
1631
1627
cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1632
- emptied_internal_root : bool ,
1633
1628
}
1634
1629
1635
1630
#[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
@@ -1670,17 +1665,6 @@ where
1670
1665
}
1671
1666
}
1672
1667
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
-
1684
1668
impl < ' a , K : ' a , V : ' a > DrainFilterInner < ' a , K , V > {
1685
1669
/// Allow Debug implementations to predict the next element.
1686
1670
pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
@@ -1697,10 +1681,9 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
1697
1681
let ( k, v) = kv. kv_mut ( ) ;
1698
1682
if pred ( k, v) {
1699
1683
* self . length -= 1 ;
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) ;
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) ) ;
1704
1687
}
1705
1688
self . cur_leaf_edge = Some ( kv. next_leaf_edge ( ) ) ;
1706
1689
}
@@ -2668,31 +2651,17 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
2668
2651
fn remove_kv ( self ) -> ( K , V ) {
2669
2652
* self . length -= 1 ;
2670
2653
2671
- let RemoveResult { old_kv, pos, emptied_internal_root } = self . handle . remove_kv_tracking ( ) ;
2672
- if emptied_internal_root {
2673
- let root = pos. into_node ( ) . into_root_mut ( ) ;
2674
- root. pop_internal_level ( ) ;
2675
- }
2676
- old_kv
2654
+ let ( old_key, old_val, _) = self . handle . remove_kv_tracking ( ) ;
2655
+ ( old_key, old_val)
2677
2656
}
2678
2657
}
2679
2658
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
-
2690
2659
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
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 > {
2660
+ /// Removes a key/value-pair from the map , and returns that pair, as well as
2661
+ /// the leaf edge corresponding to that former pair.
2662
+ fn remove_kv_tracking (
2663
+ self ,
2664
+ ) -> ( K , V , Handle < NodeRef < marker :: Mut < ' a > , K , V , marker :: Leaf > , marker :: Edge > ) {
2696
2665
let ( mut pos, old_key, old_val, was_internal) = match self . force ( ) {
2697
2666
Leaf ( leaf) => {
2698
2667
let ( hole, old_key, old_val) = leaf. remove ( ) ;
@@ -2721,7 +2690,6 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2721
2690
} ;
2722
2691
2723
2692
// Handle underflow
2724
- let mut emptied_internal_root = false ;
2725
2693
let mut cur_node = unsafe { ptr:: read ( & pos) . into_node ( ) . forget_type ( ) } ;
2726
2694
let mut at_leaf = true ;
2727
2695
while cur_node. len ( ) < node:: MIN_LEN {
@@ -2743,7 +2711,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2743
2711
let parent = edge. into_node ( ) ;
2744
2712
if parent. len ( ) == 0 {
2745
2713
// This empty parent must be the root, and should be popped off the tree.
2746
- emptied_internal_root = true ;
2714
+ parent . into_root_mut ( ) . pop_internal_level ( ) ;
2747
2715
break ;
2748
2716
} else {
2749
2717
cur_node = parent. forget_type ( ) ;
@@ -2770,7 +2738,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2770
2738
pos = unsafe { unwrap_unchecked ( pos. next_kv ( ) . ok ( ) ) . next_leaf_edge ( ) } ;
2771
2739
}
2772
2740
2773
- RemoveResult { old_kv : ( old_key, old_val) , pos, emptied_internal_root }
2741
+ ( old_key, old_val, pos)
2774
2742
}
2775
2743
}
2776
2744
@@ -2850,16 +2818,8 @@ fn handle_underfull_node<K, V>(
2850
2818
let ( is_left, mut handle) = match parent. left_kv ( ) {
2851
2819
Ok ( left) => ( true , left) ,
2852
2820
Err ( parent) => {
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
- }
2821
+ let right = unsafe { unwrap_unchecked ( parent. right_kv ( ) . ok ( ) ) } ;
2822
+ ( false , right)
2863
2823
}
2864
2824
} ;
2865
2825
0 commit comments