@@ -297,7 +297,7 @@ pub struct HashMap<K, V, H = RandomSipHasher> {
297
297
298
298
/// Search for a pre-hashed key.
299
299
fn search_hashed < K , V , M , F > ( table : M ,
300
- hash : & SafeHash ,
300
+ hash : SafeHash ,
301
301
mut is_match : F )
302
302
-> SearchResult < K , V , M > where
303
303
M : Deref < RawTable < K , V > > ,
@@ -320,14 +320,9 @@ fn search_hashed<K, V, M, F>(table: M,
320
320
}
321
321
322
322
// If the hash doesn't match, it can't be this one..
323
- if * hash == full. hash ( ) {
324
- let matched = {
325
- let ( k, _) = full. read ( ) ;
326
- is_match ( k)
327
- } ;
328
-
323
+ if hash == full. hash ( ) {
329
324
// If the key doesn't match, it can't be this one..
330
- if matched {
325
+ if is_match ( full . read ( ) . 0 ) {
331
326
return FoundExisting ( full) ;
332
327
}
333
328
}
@@ -353,7 +348,7 @@ fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
353
348
}
354
349
355
350
// Now we've done all our shifting. Return the value we grabbed earlier.
356
- return ( retkey, retval) ;
351
+ ( retkey, retval)
357
352
}
358
353
359
354
/// Perform robin hood bucket stealing at the given `bucket`. You must
@@ -389,10 +384,11 @@ fn robin_hood<'a, K: 'a, V: 'a>(mut bucket: FullBucketMut<'a, K, V>,
389
384
let b = bucket. put ( old_hash, old_key, old_val) ;
390
385
// Now that it's stolen, just read the value's pointer
391
386
// right out of the table!
392
- let ( _, v) = Bucket :: at_index ( b. into_table ( ) , starting_index) . peek ( )
393
- . expect_full ( )
394
- . into_mut_refs ( ) ;
395
- return v;
387
+ return Bucket :: at_index ( b. into_table ( ) , starting_index)
388
+ . peek ( )
389
+ . expect_full ( )
390
+ . into_mut_refs ( )
391
+ . 1 ;
396
392
} ,
397
393
table:: Full ( bucket) => bucket
398
394
} ;
@@ -441,14 +437,14 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
441
437
fn search_equiv < ' a , Sized ? Q : Hash < S > + Equiv < K > > ( & ' a self , q : & Q )
442
438
-> Option < FullBucketImm < ' a , K , V > > {
443
439
let hash = self . make_hash ( q) ;
444
- search_hashed ( & self . table , & hash, |k| q. equiv ( k) ) . into_option ( )
440
+ search_hashed ( & self . table , hash, |k| q. equiv ( k) ) . into_option ( )
445
441
}
446
442
447
443
#[ allow( deprecated) ]
448
444
fn search_equiv_mut < ' a , Sized ? Q : Hash < S > + Equiv < K > > ( & ' a mut self , q : & Q )
449
445
-> Option < FullBucketMut < ' a , K , V > > {
450
446
let hash = self . make_hash ( q) ;
451
- search_hashed ( & mut self . table , & hash, |k| q. equiv ( k) ) . into_option ( )
447
+ search_hashed ( & mut self . table , hash, |k| q. equiv ( k) ) . into_option ( )
452
448
}
453
449
454
450
/// Search for a key, yielding the index if it's found in the hashtable.
@@ -458,22 +454,22 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
458
454
where Q : BorrowFrom < K > + Eq + Hash < S >
459
455
{
460
456
let hash = self . make_hash ( q) ;
461
- search_hashed ( & self . table , & hash, |k| q. eq ( BorrowFrom :: borrow_from ( k) ) )
457
+ search_hashed ( & self . table , hash, |k| q. eq ( BorrowFrom :: borrow_from ( k) ) )
462
458
. into_option ( )
463
459
}
464
460
465
461
fn search_mut < ' a , Sized ? Q > ( & ' a mut self , q : & Q ) -> Option < FullBucketMut < ' a , K , V > >
466
462
where Q : BorrowFrom < K > + Eq + Hash < S >
467
463
{
468
464
let hash = self . make_hash ( q) ;
469
- search_hashed ( & mut self . table , & hash, |k| q. eq ( BorrowFrom :: borrow_from ( k) ) )
465
+ search_hashed ( & mut self . table , hash, |k| q. eq ( BorrowFrom :: borrow_from ( k) ) )
470
466
. into_option ( )
471
467
}
472
468
473
469
// The caller should ensure that invariants by Robin Hood Hashing hold.
474
470
fn insert_hashed_ordered ( & mut self , hash : SafeHash , k : K , v : V ) {
475
471
let cap = self . table . capacity ( ) ;
476
- let mut buckets = Bucket :: new ( & mut self . table , & hash) ;
472
+ let mut buckets = Bucket :: new ( & mut self . table , hash) ;
477
473
let ib = buckets. index ( ) ;
478
474
479
475
while buckets. index ( ) != ib + cap {
@@ -762,26 +758,22 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
762
758
{
763
759
// Worst case, we'll find one empty bucket among `size + 1` buckets.
764
760
let size = self . table . size ( ) ;
765
- let mut probe = Bucket :: new ( & mut self . table , & hash) ;
761
+ let mut probe = Bucket :: new ( & mut self . table , hash) ;
766
762
let ib = probe. index ( ) ;
767
763
768
764
loop {
769
765
let mut bucket = match probe. peek ( ) {
770
766
Empty ( bucket) => {
771
767
// Found a hole!
772
- let bucket = bucket. put ( hash, k, v) ;
773
- let ( _, val) = bucket. into_mut_refs ( ) ;
774
- return val;
775
- } ,
768
+ return bucket. put ( hash, k, v) . into_mut_refs ( ) . 1 ;
769
+ }
776
770
Full ( bucket) => bucket
777
771
} ;
778
772
773
+ // hash matches?
779
774
if bucket. hash ( ) == hash {
780
- let found_match = {
781
- let ( bucket_k, _) = bucket. read_mut ( ) ;
782
- k == * bucket_k
783
- } ;
784
- if found_match {
775
+ // key matches?
776
+ if k == * bucket. read_mut ( ) . 0 {
785
777
let ( bucket_k, bucket_v) = bucket. into_mut_refs ( ) ;
786
778
debug_assert ! ( k == * bucket_k) ;
787
779
// Key already exists. Get its reference.
@@ -811,13 +803,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
811
803
/// Deprecated: use `get` and `BorrowFrom` instead.
812
804
#[ deprecated = "use get and BorrowFrom instead" ]
813
805
pub fn find_equiv < ' a , Sized ? Q : Hash < S > + Equiv < K > > ( & ' a self , k : & Q ) -> Option < & ' a V > {
814
- match self . search_equiv ( k) {
815
- None => None ,
816
- Some ( bucket) => {
817
- let ( _, v_ref) = bucket. into_refs ( ) ;
818
- Some ( v_ref)
819
- }
820
- }
806
+ self . search_equiv ( k) . map ( |bucket| bucket. into_refs ( ) . 1 )
821
807
}
822
808
823
809
/// Deprecated: use `remove` and `BorrowFrom` instead.
@@ -829,13 +815,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
829
815
830
816
self . reserve ( 1 ) ;
831
817
832
- match self . search_equiv_mut ( k) {
833
- Some ( bucket) => {
834
- let ( _k, val) = pop_internal ( bucket) ;
835
- Some ( val)
836
- }
837
- _ => None
838
- }
818
+ self . search_equiv_mut ( k) . map ( |bucket| pop_internal ( bucket) . 1 )
839
819
}
840
820
841
821
/// An iterator visiting all keys in arbitrary order.
@@ -1022,11 +1002,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
1022
1002
1023
1003
while buckets. index ( ) != cap {
1024
1004
buckets = match buckets. peek ( ) {
1025
- Empty ( b) => b. next ( ) ,
1026
- Full ( full) => {
1027
- let ( b, _, _) = full. take ( ) ;
1028
- b. next ( )
1029
- }
1005
+ Empty ( b) => b. next ( ) ,
1006
+ Full ( full) => full. take ( ) . 0 . next ( ) ,
1030
1007
} ;
1031
1008
}
1032
1009
}
@@ -1057,10 +1034,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
1057
1034
pub fn get < Sized ? Q > ( & self , k : & Q ) -> Option < & V >
1058
1035
where Q : Hash < S > + Eq + BorrowFrom < K >
1059
1036
{
1060
- self . search ( k) . map ( |bucket| {
1061
- let ( _, v) = bucket. into_refs ( ) ;
1062
- v
1063
- } )
1037
+ self . search ( k) . map ( |bucket| bucket. into_refs ( ) . 1 )
1064
1038
}
1065
1039
1066
1040
/// Returns true if the map contains a value for the specified key.
@@ -1115,13 +1089,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
1115
1089
pub fn get_mut < Sized ? Q > ( & mut self , k : & Q ) -> Option < & mut V >
1116
1090
where Q : Hash < S > + Eq + BorrowFrom < K >
1117
1091
{
1118
- match self . search_mut ( k) {
1119
- Some ( bucket) => {
1120
- let ( _, v) = bucket. into_mut_refs ( ) ;
1121
- Some ( v)
1122
- }
1123
- _ => None
1124
- }
1092
+ self . search_mut ( k) . map ( |bucket| bucket. into_mut_refs ( ) . 1 )
1125
1093
}
1126
1094
1127
1095
/// Deprecated: Renamed to `insert`.
@@ -1189,18 +1157,15 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
1189
1157
return None
1190
1158
}
1191
1159
1192
- self . search_mut ( k) . map ( |bucket| {
1193
- let ( _k, val) = pop_internal ( bucket) ;
1194
- val
1195
- } )
1160
+ self . search_mut ( k) . map ( |bucket| pop_internal ( bucket) . 1 )
1196
1161
}
1197
1162
}
1198
1163
1199
1164
fn search_entry_hashed < ' a , K : Eq , V > ( table : & ' a mut RawTable < K , V > , hash : SafeHash , k : K )
1200
1165
-> Entry < ' a , K , V > {
1201
1166
// Worst case, we'll find one empty bucket among `size + 1` buckets.
1202
1167
let size = table. size ( ) ;
1203
- let mut probe = Bucket :: new ( table, & hash) ;
1168
+ let mut probe = Bucket :: new ( table, hash) ;
1204
1169
let ib = probe. index ( ) ;
1205
1170
1206
1171
loop {
@@ -1216,13 +1181,10 @@ fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHas
1216
1181
Full ( bucket) => bucket
1217
1182
} ;
1218
1183
1184
+ // hash matches?
1219
1185
if bucket. hash ( ) == hash {
1220
- let is_eq = {
1221
- let ( bucket_k, _) = bucket. read ( ) ;
1222
- k == * bucket_k
1223
- } ;
1224
-
1225
- if is_eq {
1186
+ // key matches?
1187
+ if k == * bucket. read ( ) . 0 {
1226
1188
return Occupied ( OccupiedEntry {
1227
1189
elem : bucket,
1228
1190
} ) ;
@@ -1310,10 +1272,7 @@ impl<K: Hash<S> + Eq, Sized? Q, V, S, H: Hasher<S>> IndexMut<Q, V> for HashMap<K
1310
1272
{
1311
1273
#[ inline]
1312
1274
fn index_mut < ' a > ( & ' a mut self , index : & Q ) -> & ' a mut V {
1313
- match self . get_mut ( index) {
1314
- Some ( v) => v,
1315
- None => panic ! ( "no entry found for key" )
1316
- }
1275
+ self . get_mut ( index) . expect ( "no entry found for key" )
1317
1276
}
1318
1277
}
1319
1278
@@ -1404,21 +1363,18 @@ impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
1404
1363
impl < ' a , K , V > OccupiedEntry < ' a , K , V > {
1405
1364
/// Gets a reference to the value in the entry
1406
1365
pub fn get ( & self ) -> & V {
1407
- let ( _, v) = self . elem . read ( ) ;
1408
- v
1366
+ self . elem . read ( ) . 1
1409
1367
}
1410
1368
1411
1369
/// Gets a mutable reference to the value in the entry
1412
1370
pub fn get_mut ( & mut self ) -> & mut V {
1413
- let ( _, v) = self . elem . read_mut ( ) ;
1414
- v
1371
+ self . elem . read_mut ( ) . 1
1415
1372
}
1416
1373
1417
1374
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
1418
1375
/// with a lifetime bound to the map itself
1419
1376
pub fn into_mut ( self ) -> & ' a mut V {
1420
- let ( _, v) = self . elem . into_mut_refs ( ) ;
1421
- v
1377
+ self . elem . into_mut_refs ( ) . 1
1422
1378
}
1423
1379
1424
1380
/// Sets the value of the entry, and returns the entry's old value
@@ -1430,8 +1386,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
1430
1386
1431
1387
/// Takes the value out of the entry, and returns it
1432
1388
pub fn take ( self ) -> V {
1433
- let ( _, v) = pop_internal ( self . elem ) ;
1434
- v
1389
+ pop_internal ( self . elem ) . 1
1435
1390
}
1436
1391
}
1437
1392
@@ -1444,17 +1399,15 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
1444
1399
robin_hood ( bucket, ib, self . hash , self . key , value)
1445
1400
}
1446
1401
NoElem ( bucket) => {
1447
- let full = bucket. put ( self . hash , self . key , value) ;
1448
- let ( _, v) = full. into_mut_refs ( ) ;
1449
- v
1402
+ bucket. put ( self . hash , self . key , value) . into_mut_refs ( ) . 1
1450
1403
}
1451
1404
}
1452
1405
}
1453
1406
}
1454
1407
1455
1408
impl < K : Eq + Hash < S > , V , S , H : Hasher < S > + Default > FromIterator < ( K , V ) > for HashMap < K , V , H > {
1456
1409
fn from_iter < T : Iterator < ( K , V ) > > ( iter : T ) -> HashMap < K , V , H > {
1457
- let ( lower, _ ) = iter. size_hint ( ) ;
1410
+ let lower = iter. size_hint ( ) . 0 ;
1458
1411
let mut map = HashMap :: with_capacity_and_hasher ( lower, Default :: default ( ) ) ;
1459
1412
map. extend ( iter) ;
1460
1413
map
0 commit comments