@@ -151,7 +151,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
151151 let mut out_tree = BTreeMap { root : Some ( node:: Root :: new_leaf ( ) ) , length : 0 } ;
152152
153153 {
154- let root = out_tree. root . as_mut ( ) . unwrap ( ) ;
154+ let root = out_tree. root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because we just wrapped
155155 let mut out_node = match root. as_mut ( ) . force ( ) {
156156 Leaf ( leaf) => leaf,
157157 Internal ( _) => unreachable ! ( ) ,
@@ -171,14 +171,10 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
171171 }
172172 Internal ( internal) => {
173173 let mut out_tree = clone_subtree ( internal. first_edge ( ) . descend ( ) ) ;
174- out_tree. ensure_root_is_owned ( ) ;
175174
176175 {
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 ( ) ;
182178 let mut in_edge = internal. first_edge ( ) ;
183179 while let Ok ( kv) = in_edge. right_kv ( ) {
184180 let ( k, v) = kv. into_kv ( ) ;
@@ -212,7 +208,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
212208 // Ord` constraint, which this method lacks.
213209 BTreeMap { root : None , length : 0 }
214210 } 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
216212 }
217213 }
218214}
@@ -243,8 +239,8 @@ where
243239 }
244240
245241 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) {
248244 Found ( handle) => Some ( mem:: replace ( handle. into_kv_mut ( ) . 0 , key) ) ,
249245 GoDown ( handle) => {
250246 VacantEntry { key, handle, length : & mut self . length , _marker : PhantomData }
@@ -943,7 +939,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
943939
944940 // Second, we build a tree from the sorted sequence in linear time.
945941 self . from_sorted_iter ( iter) ;
946- self . fix_right_edge ( ) ;
947942 }
948943
949944 /// 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> {
10581053 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
10591054 pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
10601055 // 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) {
10631058 Found ( handle) => {
10641059 Occupied ( OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData } )
10651060 }
@@ -1070,8 +1065,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
10701065 }
10711066
10721067 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 ( ) ;
10751070 // Iterate through all key-value pairs, pushing them into nodes at the right level.
10761071 for ( key, value) in iter {
10771072 // Try to push key-value pair into the current leaf node.
@@ -1116,11 +1111,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
11161111
11171112 self . length += 1 ;
11181113 }
1114+ Self :: fix_right_edge ( root)
11191115 }
11201116
1121- fn fix_right_edge ( & mut self ) {
1117+ fn fix_right_edge ( root : & mut node :: Root < K , V > ) {
11221118 // 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 ( ) ;
11241120 while let Internal ( internal) = cur_node. force ( ) {
11251121 // Check if right-most child is underfull.
11261122 let mut last_edge = internal. last_edge ( ) ;
@@ -1179,16 +1175,17 @@ impl<K: Ord, V> BTreeMap<K, V> {
11791175 }
11801176
11811177 let total_num = self . len ( ) ;
1178+ let left_root = self . root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because not empty
11821179
11831180 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 ( ) {
11861183 right_root. push_level ( ) ;
11871184 }
11881185
11891186 {
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 ( ) ;
11921189
11931190 loop {
11941191 let mut split_edge = match search:: search_node ( left_node, key) {
@@ -1214,12 +1211,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
12141211 }
12151212 }
12161213
1217- self . fix_right_border ( ) ;
1218- right . fix_left_border ( ) ;
1214+ left_root . fix_right_border ( ) ;
1215+ right_root . fix_left_border ( ) ;
12191216
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 ( ) {
12231218 self . recalc_length ( ) ;
12241219 right. length = total_num - self . len ( ) ;
12251220 } else {
@@ -1301,69 +1296,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
13011296
13021297 self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
13031298 }
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- }
13671299}
13681300
13691301#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -2321,9 +2253,9 @@ impl<K, V> BTreeMap<K, V> {
23212253 }
23222254
23232255 /// 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)
23272259 }
23282260}
23292261
@@ -2825,6 +2757,65 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
28252757 }
28262758}
28272759
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+
28282819enum UnderflowResult < ' a , K , V > {
28292820 AtRoot ,
28302821 Merged ( Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > , marker:: Edge > , bool , usize ) ,
0 commit comments