@@ -151,7 +151,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
151
151
let mut out_tree = BTreeMap { root : Some ( node:: Root :: new_leaf ( ) ) , length : 0 } ;
152
152
153
153
{
154
- let root = out_tree. root . as_mut ( ) . unwrap ( ) ;
154
+ let root = out_tree. root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because we just wrapped
155
155
let mut out_node = match root. as_mut ( ) . force ( ) {
156
156
Leaf ( leaf) => leaf,
157
157
Internal ( _) => unreachable ! ( ) ,
@@ -171,14 +171,10 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
171
171
}
172
172
Internal ( internal) => {
173
173
let mut out_tree = clone_subtree ( internal. first_edge ( ) . descend ( ) ) ;
174
- out_tree. ensure_root_is_owned ( ) ;
175
174
176
175
{
177
- // Ideally we'd use the return of ensure_root_is_owned
178
- // instead of re-unwrapping here but unfortunately that
179
- // borrows all of out_tree and we need access to the
180
- // length below.
181
- let mut out_node = out_tree. root . as_mut ( ) . unwrap ( ) . push_level ( ) ;
176
+ let out_root = BTreeMap :: ensure_is_owned ( & mut out_tree. root ) ;
177
+ let mut out_node = out_root. push_level ( ) ;
182
178
let mut in_edge = internal. first_edge ( ) ;
183
179
while let Ok ( kv) = in_edge. right_kv ( ) {
184
180
let ( k, v) = kv. into_kv ( ) ;
@@ -212,7 +208,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
212
208
// Ord` constraint, which this method lacks.
213
209
BTreeMap { root : None , length : 0 }
214
210
} else {
215
- clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) )
211
+ clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) // unwrap succeeds because not empty
216
212
}
217
213
}
218
214
}
@@ -243,8 +239,8 @@ where
243
239
}
244
240
245
241
fn replace ( & mut self , key : K ) -> Option < K > {
246
- self . ensure_root_is_owned ( ) ;
247
- match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( self . root . as_mut ( ) ? . as_mut ( ) , & key) {
242
+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
243
+ match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( root. as_mut ( ) , & key) {
248
244
Found ( handle) => Some ( mem:: replace ( handle. into_kv_mut ( ) . 0 , key) ) ,
249
245
GoDown ( handle) => {
250
246
VacantEntry { key, handle, length : & mut self . length , _marker : PhantomData }
@@ -943,7 +939,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
943
939
944
940
// Second, we build a tree from the sorted sequence in linear time.
945
941
self . from_sorted_iter ( iter) ;
946
- self . fix_right_edge ( ) ;
947
942
}
948
943
949
944
/// Constructs a double-ended iterator over a sub-range of elements in the map.
@@ -1058,8 +1053,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
1058
1053
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1059
1054
pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
1060
1055
// FIXME(@porglezomp) Avoid allocating if we don't insert
1061
- self . ensure_root_is_owned ( ) ;
1062
- match search:: search_tree ( self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) , & key) {
1056
+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
1057
+ match search:: search_tree ( root. as_mut ( ) , & key) {
1063
1058
Found ( handle) => {
1064
1059
Occupied ( OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData } )
1065
1060
}
@@ -1070,8 +1065,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
1070
1065
}
1071
1066
1072
1067
fn from_sorted_iter < I : Iterator < Item = ( K , V ) > > ( & mut self , iter : I ) {
1073
- self . ensure_root_is_owned ( ) ;
1074
- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
1068
+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
1069
+ let mut cur_node = root. as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
1075
1070
// Iterate through all key-value pairs, pushing them into nodes at the right level.
1076
1071
for ( key, value) in iter {
1077
1072
// Try to push key-value pair into the current leaf node.
@@ -1116,11 +1111,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
1116
1111
1117
1112
self . length += 1 ;
1118
1113
}
1114
+ Self :: fix_right_edge ( root)
1119
1115
}
1120
1116
1121
- fn fix_right_edge ( & mut self ) {
1117
+ fn fix_right_edge ( root : & mut node :: Root < K , V > ) {
1122
1118
// Handle underfull nodes, start from the top.
1123
- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1119
+ let mut cur_node = root. as_mut ( ) ;
1124
1120
while let Internal ( internal) = cur_node. force ( ) {
1125
1121
// Check if right-most child is underfull.
1126
1122
let mut last_edge = internal. last_edge ( ) ;
@@ -1179,16 +1175,17 @@ impl<K: Ord, V> BTreeMap<K, V> {
1179
1175
}
1180
1176
1181
1177
let total_num = self . len ( ) ;
1178
+ let left_root = self . root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because not empty
1182
1179
1183
1180
let mut right = Self :: new ( ) ;
1184
- let right_root = right. ensure_root_is_owned ( ) ;
1185
- for _ in 0 ..( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( ) ) {
1181
+ let right_root = Self :: ensure_is_owned ( & mut right. root ) ;
1182
+ for _ in 0 ..left_root . height ( ) {
1186
1183
right_root. push_level ( ) ;
1187
1184
}
1188
1185
1189
1186
{
1190
- let mut left_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1191
- let mut right_node = right . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1187
+ let mut left_node = left_root . as_mut ( ) ;
1188
+ let mut right_node = right_root . as_mut ( ) ;
1192
1189
1193
1190
loop {
1194
1191
let mut split_edge = match search:: search_node ( left_node, key) {
@@ -1214,12 +1211,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
1214
1211
}
1215
1212
}
1216
1213
1217
- self . fix_right_border ( ) ;
1218
- right . fix_left_border ( ) ;
1214
+ left_root . fix_right_border ( ) ;
1215
+ right_root . fix_left_border ( ) ;
1219
1216
1220
- if self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( )
1221
- < right. root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( )
1222
- {
1217
+ if left_root. height ( ) < right_root. height ( ) {
1223
1218
self . recalc_length ( ) ;
1224
1219
right. length = total_num - self . len ( ) ;
1225
1220
} else {
@@ -1301,69 +1296,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
1301
1296
1302
1297
self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
1303
1298
}
1304
-
1305
- /// Removes empty levels on the top.
1306
- fn fix_top ( & mut self ) {
1307
- loop {
1308
- {
1309
- let node = self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ;
1310
- if node. height ( ) == 0 || node. len ( ) > 0 {
1311
- break ;
1312
- }
1313
- }
1314
- self . root . as_mut ( ) . unwrap ( ) . pop_level ( ) ;
1315
- }
1316
- }
1317
-
1318
- fn fix_right_border ( & mut self ) {
1319
- self . fix_top ( ) ;
1320
-
1321
- {
1322
- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1323
-
1324
- while let Internal ( node) = cur_node. force ( ) {
1325
- let mut last_kv = node. last_kv ( ) ;
1326
-
1327
- if last_kv. can_merge ( ) {
1328
- cur_node = last_kv. merge ( ) . descend ( ) ;
1329
- } else {
1330
- let right_len = last_kv. reborrow ( ) . right_edge ( ) . descend ( ) . len ( ) ;
1331
- // `MINLEN + 1` to avoid readjust if merge happens on the next level.
1332
- if right_len < node:: MIN_LEN + 1 {
1333
- last_kv. bulk_steal_left ( node:: MIN_LEN + 1 - right_len) ;
1334
- }
1335
- cur_node = last_kv. right_edge ( ) . descend ( ) ;
1336
- }
1337
- }
1338
- }
1339
-
1340
- self . fix_top ( ) ;
1341
- }
1342
-
1343
- /// The symmetric clone of `fix_right_border`.
1344
- fn fix_left_border ( & mut self ) {
1345
- self . fix_top ( ) ;
1346
-
1347
- {
1348
- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1349
-
1350
- while let Internal ( node) = cur_node. force ( ) {
1351
- let mut first_kv = node. first_kv ( ) ;
1352
-
1353
- if first_kv. can_merge ( ) {
1354
- cur_node = first_kv. merge ( ) . descend ( ) ;
1355
- } else {
1356
- let left_len = first_kv. reborrow ( ) . left_edge ( ) . descend ( ) . len ( ) ;
1357
- if left_len < node:: MIN_LEN + 1 {
1358
- first_kv. bulk_steal_right ( node:: MIN_LEN + 1 - left_len) ;
1359
- }
1360
- cur_node = first_kv. left_edge ( ) . descend ( ) ;
1361
- }
1362
- }
1363
- }
1364
-
1365
- self . fix_top ( ) ;
1366
- }
1367
1299
}
1368
1300
1369
1301
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -2321,9 +2253,9 @@ impl<K, V> BTreeMap<K, V> {
2321
2253
}
2322
2254
2323
2255
/// If the root node is the empty (non-allocated) root node, allocate our
2324
- /// own node.
2325
- fn ensure_root_is_owned ( & mut self ) -> & mut node:: Root < K , V > {
2326
- self . root . get_or_insert_with ( node:: Root :: new_leaf)
2256
+ /// own node. Is an associated function to avoid borrowing the entire BTreeMap.
2257
+ fn ensure_is_owned ( root : & mut Option < node :: Root < K , V > > ) -> & mut node:: Root < K , V > {
2258
+ root. get_or_insert_with ( node:: Root :: new_leaf)
2327
2259
}
2328
2260
}
2329
2261
@@ -2825,6 +2757,65 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2825
2757
}
2826
2758
}
2827
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
+
2828
2819
enum UnderflowResult < ' a , K , V > {
2829
2820
AtRoot ,
2830
2821
Merged ( Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > , marker:: Edge > , bool , usize ) ,
0 commit comments