@@ -1211,8 +1211,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
1211
1211
}
1212
1212
}
1213
1213
1214
- Self :: fix_right_border ( left_root ) ;
1215
- Self :: fix_left_border ( right_root ) ;
1214
+ left_root . fix_right_border ( ) ;
1215
+ right_root . fix_left_border ( ) ;
1216
1216
1217
1217
if left_root. height ( ) < right_root. height ( ) {
1218
1218
self . recalc_length ( ) ;
@@ -1296,63 +1296,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
1296
1296
1297
1297
self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
1298
1298
}
1299
-
1300
- /// Removes empty levels on the top.
1301
- fn fix_top ( root : & mut node:: Root < K , V > ) {
1302
- while root. height ( ) > 0 && root. as_ref ( ) . len ( ) == 0 {
1303
- root. pop_level ( ) ;
1304
- }
1305
- }
1306
-
1307
- fn fix_right_border ( root : & mut node:: Root < K , V > ) {
1308
- Self :: fix_top ( root) ;
1309
-
1310
- {
1311
- let mut cur_node = root. as_mut ( ) ;
1312
-
1313
- while let Internal ( node) = cur_node. force ( ) {
1314
- let mut last_kv = node. last_kv ( ) ;
1315
-
1316
- if last_kv. can_merge ( ) {
1317
- cur_node = last_kv. merge ( ) . descend ( ) ;
1318
- } else {
1319
- let right_len = last_kv. reborrow ( ) . right_edge ( ) . descend ( ) . len ( ) ;
1320
- // `MINLEN + 1` to avoid readjust if merge happens on the next level.
1321
- if right_len < node:: MIN_LEN + 1 {
1322
- last_kv. bulk_steal_left ( node:: MIN_LEN + 1 - right_len) ;
1323
- }
1324
- cur_node = last_kv. right_edge ( ) . descend ( ) ;
1325
- }
1326
- }
1327
- }
1328
-
1329
- Self :: fix_top ( root) ;
1330
- }
1331
-
1332
- /// The symmetric clone of `fix_right_border`.
1333
- fn fix_left_border ( root : & mut node:: Root < K , V > ) {
1334
- Self :: fix_top ( root) ;
1335
-
1336
- {
1337
- let mut cur_node = root. as_mut ( ) ;
1338
-
1339
- while let Internal ( node) = cur_node. force ( ) {
1340
- let mut first_kv = node. first_kv ( ) ;
1341
-
1342
- if first_kv. can_merge ( ) {
1343
- cur_node = first_kv. merge ( ) . descend ( ) ;
1344
- } else {
1345
- let left_len = first_kv. reborrow ( ) . left_edge ( ) . descend ( ) . len ( ) ;
1346
- if left_len < node:: MIN_LEN + 1 {
1347
- first_kv. bulk_steal_right ( node:: MIN_LEN + 1 - left_len) ;
1348
- }
1349
- cur_node = first_kv. left_edge ( ) . descend ( ) ;
1350
- }
1351
- }
1352
- }
1353
-
1354
- Self :: fix_top ( root) ;
1355
- }
1356
1299
}
1357
1300
1358
1301
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -2814,6 +2757,65 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2814
2757
}
2815
2758
}
2816
2759
2760
+ impl < K , V > node:: Root < K , V > {
2761
+ /// Removes empty levels on the top, but keep an empty leaf if the entire tree is empty.
2762
+ fn fix_top ( & mut self ) {
2763
+ while self . height ( ) > 0 && self . as_ref ( ) . len ( ) == 0 {
2764
+ self . pop_level ( ) ;
2765
+ }
2766
+ }
2767
+
2768
+ fn fix_right_border ( & mut self ) {
2769
+ self . fix_top ( ) ;
2770
+
2771
+ {
2772
+ let mut cur_node = self . as_mut ( ) ;
2773
+
2774
+ while let Internal ( node) = cur_node. force ( ) {
2775
+ let mut last_kv = node. last_kv ( ) ;
2776
+
2777
+ if last_kv. can_merge ( ) {
2778
+ cur_node = last_kv. merge ( ) . descend ( ) ;
2779
+ } else {
2780
+ let right_len = last_kv. reborrow ( ) . right_edge ( ) . descend ( ) . len ( ) ;
2781
+ // `MINLEN + 1` to avoid readjust if merge happens on the next level.
2782
+ if right_len < node:: MIN_LEN + 1 {
2783
+ last_kv. bulk_steal_left ( node:: MIN_LEN + 1 - right_len) ;
2784
+ }
2785
+ cur_node = last_kv. right_edge ( ) . descend ( ) ;
2786
+ }
2787
+ }
2788
+ }
2789
+
2790
+ self . fix_top ( ) ;
2791
+ }
2792
+
2793
+ /// The symmetric clone of `fix_right_border`.
2794
+ fn fix_left_border ( & mut self ) {
2795
+ self . fix_top ( ) ;
2796
+
2797
+ {
2798
+ let mut cur_node = self . as_mut ( ) ;
2799
+
2800
+ while let Internal ( node) = cur_node. force ( ) {
2801
+ let mut first_kv = node. first_kv ( ) ;
2802
+
2803
+ if first_kv. can_merge ( ) {
2804
+ cur_node = first_kv. merge ( ) . descend ( ) ;
2805
+ } else {
2806
+ let left_len = first_kv. reborrow ( ) . left_edge ( ) . descend ( ) . len ( ) ;
2807
+ if left_len < node:: MIN_LEN + 1 {
2808
+ first_kv. bulk_steal_right ( node:: MIN_LEN + 1 - left_len) ;
2809
+ }
2810
+ cur_node = first_kv. left_edge ( ) . descend ( ) ;
2811
+ }
2812
+ }
2813
+ }
2814
+
2815
+ self . fix_top ( ) ;
2816
+ }
2817
+ }
2818
+
2817
2819
enum UnderflowResult < ' a , K , V > {
2818
2820
AtRoot ,
2819
2821
Merged ( Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > , marker:: Edge > , bool , usize ) ,
0 commit comments