2
2
3
3
use core:: borrow:: Borrow ;
4
4
use core:: cmp:: Ordering ;
5
- use core:: fmt:: Debug ;
5
+ use core:: fmt:: { self , Debug } ;
6
6
use core:: hash:: { Hash , Hasher } ;
7
7
use core:: iter:: { FromIterator , FusedIterator , Peekable } ;
8
8
use core:: marker:: PhantomData ;
9
9
use core:: mem:: { self , ManuallyDrop } ;
10
10
use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
11
11
use core:: ops:: { Index , RangeBounds } ;
12
- use core:: { fmt , ptr} ;
12
+ use core:: ptr;
13
13
14
14
use super :: node:: { self , marker, ForceResult :: * , Handle , InsertResult :: * , NodeRef } ;
15
15
use super :: search:: { self , SearchResult :: * } ;
@@ -154,7 +154,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
154
154
155
155
{
156
156
let root = out_tree. root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because we just wrapped
157
- let mut out_node = match root. as_mut ( ) . force ( ) {
157
+ let mut out_node = match root. node_as_mut ( ) . force ( ) {
158
158
Leaf ( leaf) => leaf,
159
159
Internal ( _) => unreachable ! ( ) ,
160
160
} ;
@@ -210,7 +210,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
210
210
// Ord` constraint, which this method lacks.
211
211
BTreeMap { root : None , length : 0 }
212
212
} else {
213
- clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) // unwrap succeeds because not empty
213
+ clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . node_as_ref ( ) ) // unwrap succeeds because not empty
214
214
}
215
215
}
216
216
}
@@ -223,14 +223,16 @@ where
223
223
type Key = K ;
224
224
225
225
fn get ( & self , key : & Q ) -> Option < & K > {
226
- match search:: search_tree ( self . root . as_ref ( ) ?. as_ref ( ) , key) {
226
+ let root_node = self . root . as_ref ( ) ?. node_as_ref ( ) ;
227
+ match search:: search_tree ( root_node, key) {
227
228
Found ( handle) => Some ( handle. into_kv ( ) . 0 ) ,
228
229
GoDown ( _) => None ,
229
230
}
230
231
}
231
232
232
233
fn take ( & mut self , key : & Q ) -> Option < K > {
233
- match search:: search_tree ( self . root . as_mut ( ) ?. as_mut ( ) , key) {
234
+ let root_node = self . root . as_mut ( ) ?. node_as_mut ( ) ;
235
+ match search:: search_tree ( root_node, key) {
234
236
Found ( handle) => Some (
235
237
OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData }
236
238
. remove_kv ( )
@@ -242,7 +244,7 @@ where
242
244
243
245
fn replace ( & mut self , key : K ) -> Option < K > {
244
246
let root = Self :: ensure_is_owned ( & mut self . root ) ;
245
- match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( root. as_mut ( ) , & key) {
247
+ match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( root. node_as_mut ( ) , & key) {
246
248
Found ( handle) => Some ( mem:: replace ( handle. into_kv_mut ( ) . 0 , key) ) ,
247
249
GoDown ( handle) => {
248
250
VacantEntry { key, handle, length : & mut self . length , _marker : PhantomData }
@@ -565,7 +567,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
565
567
K : Borrow < Q > ,
566
568
Q : Ord ,
567
569
{
568
- match search:: search_tree ( self . root . as_ref ( ) ?. as_ref ( ) , key) {
570
+ let root_node = self . root . as_ref ( ) ?. node_as_ref ( ) ;
571
+ match search:: search_tree ( root_node, key) {
569
572
Found ( handle) => Some ( handle. into_kv ( ) . 1 ) ,
570
573
GoDown ( _) => None ,
571
574
}
@@ -592,7 +595,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
592
595
K : Borrow < Q > ,
593
596
Q : Ord ,
594
597
{
595
- match search:: search_tree ( self . root . as_ref ( ) ?. as_ref ( ) , k) {
598
+ let root_node = self . root . as_ref ( ) ?. node_as_ref ( ) ;
599
+ match search:: search_tree ( root_node, k) {
596
600
Found ( handle) => Some ( handle. into_kv ( ) ) ,
597
601
GoDown ( _) => None ,
598
602
}
@@ -617,8 +621,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
617
621
/// ```
618
622
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
619
623
pub fn first_key_value ( & self ) -> Option < ( & K , & V ) > {
620
- let front = self . root . as_ref ( ) ?. as_ref ( ) . first_leaf_edge ( ) ;
621
- front . right_kv ( ) . ok ( ) . map ( Handle :: into_kv)
624
+ let root_node = self . root . as_ref ( ) ?. node_as_ref ( ) ;
625
+ root_node . first_leaf_edge ( ) . right_kv ( ) . ok ( ) . map ( Handle :: into_kv)
622
626
}
623
627
624
628
/// Returns the first entry in the map for in-place manipulation.
@@ -643,8 +647,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
643
647
/// ```
644
648
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
645
649
pub fn first_entry ( & mut self ) -> Option < OccupiedEntry < ' _ , K , V > > {
646
- let front = self . root . as_mut ( ) ?. as_mut ( ) . first_leaf_edge ( ) ;
647
- let kv = front . right_kv ( ) . ok ( ) ?;
650
+ let root_node = self . root . as_mut ( ) ?. node_as_mut ( ) ;
651
+ let kv = root_node . first_leaf_edge ( ) . right_kv ( ) . ok ( ) ?;
648
652
Some ( OccupiedEntry {
649
653
handle : kv. forget_node_type ( ) ,
650
654
length : & mut self . length ,
@@ -694,8 +698,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
694
698
/// ```
695
699
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
696
700
pub fn last_key_value ( & self ) -> Option < ( & K , & V ) > {
697
- let back = self . root . as_ref ( ) ?. as_ref ( ) . last_leaf_edge ( ) ;
698
- back . left_kv ( ) . ok ( ) . map ( Handle :: into_kv)
701
+ let root_node = self . root . as_ref ( ) ?. node_as_ref ( ) ;
702
+ root_node . last_leaf_edge ( ) . left_kv ( ) . ok ( ) . map ( Handle :: into_kv)
699
703
}
700
704
701
705
/// Returns the last entry in the map for in-place manipulation.
@@ -720,8 +724,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
720
724
/// ```
721
725
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
722
726
pub fn last_entry ( & mut self ) -> Option < OccupiedEntry < ' _ , K , V > > {
723
- let back = self . root . as_mut ( ) ?. as_mut ( ) . last_leaf_edge ( ) ;
724
- let kv = back . left_kv ( ) . ok ( ) ?;
727
+ let root_node = self . root . as_mut ( ) ?. node_as_mut ( ) ;
728
+ let kv = root_node . last_leaf_edge ( ) . left_kv ( ) . ok ( ) ?;
725
729
Some ( OccupiedEntry {
726
730
handle : kv. forget_node_type ( ) ,
727
731
length : & mut self . length ,
@@ -805,7 +809,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
805
809
K : Borrow < Q > ,
806
810
Q : Ord ,
807
811
{
808
- match search:: search_tree ( self . root . as_mut ( ) ?. as_mut ( ) , key) {
812
+ let root_node = self . root . as_mut ( ) ?. node_as_mut ( ) ;
813
+ match search:: search_tree ( root_node, key) {
809
814
Found ( handle) => Some ( handle. into_kv_mut ( ) . 1 ) ,
810
815
GoDown ( _) => None ,
811
816
}
@@ -899,7 +904,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
899
904
K : Borrow < Q > ,
900
905
Q : Ord ,
901
906
{
902
- match search:: search_tree ( self . root . as_mut ( ) ?. as_mut ( ) , key) {
907
+ let root_node = self . root . as_mut ( ) ?. node_as_mut ( ) ;
908
+ match search:: search_tree ( root_node, key) {
903
909
Found ( handle) => Some (
904
910
OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData }
905
911
. remove_entry ( ) ,
@@ -995,7 +1001,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
995
1001
R : RangeBounds < T > ,
996
1002
{
997
1003
if let Some ( root) = & self . root {
998
- let ( f, b) = range_search ( root. as_ref ( ) , range) ;
1004
+ let ( f, b) = range_search ( root. node_as_ref ( ) , range) ;
999
1005
1000
1006
Range { front : Some ( f) , back : Some ( b) }
1001
1007
} else {
@@ -1041,7 +1047,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1041
1047
R : RangeBounds < T > ,
1042
1048
{
1043
1049
if let Some ( root) = & mut self . root {
1044
- let ( f, b) = range_search ( root. as_mut ( ) , range) ;
1050
+ let ( f, b) = range_search ( root. node_as_mut ( ) , range) ;
1045
1051
1046
1052
RangeMut { front : Some ( f) , back : Some ( b) , _marker : PhantomData }
1047
1053
} else {
@@ -1071,7 +1077,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1071
1077
pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
1072
1078
// FIXME(@porglezomp) Avoid allocating if we don't insert
1073
1079
let root = Self :: ensure_is_owned ( & mut self . root ) ;
1074
- match search:: search_tree ( root. as_mut ( ) , & key) {
1080
+ match search:: search_tree ( root. node_as_mut ( ) , & key) {
1075
1081
Found ( handle) => {
1076
1082
Occupied ( OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData } )
1077
1083
}
@@ -1083,7 +1089,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1083
1089
1084
1090
fn from_sorted_iter < I : Iterator < Item = ( K , V ) > > ( & mut self , iter : I ) {
1085
1091
let root = Self :: ensure_is_owned ( & mut self . root ) ;
1086
- let mut cur_node = root. as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
1092
+ let mut cur_node = root. node_as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
1087
1093
// Iterate through all key-value pairs, pushing them into nodes at the right level.
1088
1094
for ( key, value) in iter {
1089
1095
// Try to push key-value pair into the current leaf node.
@@ -1133,7 +1139,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1133
1139
1134
1140
fn fix_right_edge ( root : & mut node:: Root < K , V > ) {
1135
1141
// Handle underfull nodes, start from the top.
1136
- let mut cur_node = root. as_mut ( ) ;
1142
+ let mut cur_node = root. node_as_mut ( ) ;
1137
1143
while let Internal ( internal) = cur_node. force ( ) {
1138
1144
// Check if right-most child is underfull.
1139
1145
let mut last_edge = internal. last_edge ( ) ;
@@ -1201,8 +1207,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
1201
1207
}
1202
1208
1203
1209
{
1204
- let mut left_node = left_root. as_mut ( ) ;
1205
- let mut right_node = right_root. as_mut ( ) ;
1210
+ let mut left_node = left_root. node_as_mut ( ) ;
1211
+ let mut right_node = right_root. node_as_mut ( ) ;
1206
1212
1207
1213
loop {
1208
1214
let mut split_edge = match search:: search_node ( left_node, key) {
@@ -1280,7 +1286,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
1280
1286
DrainFilter { pred, inner : self . drain_filter_inner ( ) }
1281
1287
}
1282
1288
pub ( super ) fn drain_filter_inner ( & mut self ) -> DrainFilterInner < ' _ , K , V > {
1283
- let front = self . root . as_mut ( ) . map ( |r| r. as_mut ( ) . first_leaf_edge ( ) ) ;
1289
+ let root_node = self . root . as_mut ( ) . map ( |r| r. node_as_mut ( ) ) ;
1290
+ let front = root_node. map ( |rn| rn. first_leaf_edge ( ) ) ;
1284
1291
DrainFilterInner {
1285
1292
length : & mut self . length ,
1286
1293
cur_leaf_edge : front,
@@ -1315,7 +1322,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1315
1322
res
1316
1323
}
1317
1324
1318
- self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
1325
+ self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . node_as_ref ( ) ) ;
1319
1326
}
1320
1327
1321
1328
/// Creates a consuming iterator visiting all the keys, in sorted order.
@@ -2251,7 +2258,7 @@ impl<K, V> BTreeMap<K, V> {
2251
2258
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2252
2259
pub fn iter ( & self ) -> Iter < ' _ , K , V > {
2253
2260
if let Some ( root) = & self . root {
2254
- let ( f, b) = full_range_search ( root. as_ref ( ) ) ;
2261
+ let ( f, b) = full_range_search ( root. node_as_ref ( ) ) ;
2255
2262
2256
2263
Iter { range : Range { front : Some ( f) , back : Some ( b) } , length : self . length }
2257
2264
} else {
@@ -2283,7 +2290,7 @@ impl<K, V> BTreeMap<K, V> {
2283
2290
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2284
2291
pub fn iter_mut ( & mut self ) -> IterMut < ' _ , K , V > {
2285
2292
if let Some ( root) = & mut self . root {
2286
- let ( f, b) = full_range_search ( root. as_mut ( ) ) ;
2293
+ let ( f, b) = full_range_search ( root. node_as_mut ( ) ) ;
2287
2294
2288
2295
IterMut {
2289
2296
range : RangeMut { front : Some ( f) , back : Some ( b) , _marker : PhantomData } ,
@@ -2895,7 +2902,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2895
2902
impl < K , V > node:: Root < K , V > {
2896
2903
/// Removes empty levels on the top, but keep an empty leaf if the entire tree is empty.
2897
2904
fn fix_top ( & mut self ) {
2898
- while self . height ( ) > 0 && self . as_ref ( ) . len ( ) == 0 {
2905
+ while self . height ( ) > 0 && self . node_as_ref ( ) . len ( ) == 0 {
2899
2906
self . pop_internal_level ( ) ;
2900
2907
}
2901
2908
}
@@ -2904,7 +2911,7 @@ impl<K, V> node::Root<K, V> {
2904
2911
self . fix_top ( ) ;
2905
2912
2906
2913
{
2907
- let mut cur_node = self . as_mut ( ) ;
2914
+ let mut cur_node = self . node_as_mut ( ) ;
2908
2915
2909
2916
while let Internal ( node) = cur_node. force ( ) {
2910
2917
let mut last_kv = node. last_kv ( ) ;
@@ -2930,7 +2937,7 @@ impl<K, V> node::Root<K, V> {
2930
2937
self . fix_top ( ) ;
2931
2938
2932
2939
{
2933
- let mut cur_node = self . as_mut ( ) ;
2940
+ let mut cur_node = self . node_as_mut ( ) ;
2934
2941
2935
2942
while let Internal ( node) = cur_node. force ( ) {
2936
2943
let mut first_kv = node. first_kv ( ) ;
0 commit comments