@@ -23,7 +23,7 @@ use iter::{Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator,
23
23
use marker:: Sized ;
24
24
use mem:: { self , swap, replace} ;
25
25
use num:: { Int , UnsignedInt } ;
26
- use ops:: { Deref , DerefMut , Drop , FnMut , Index , IndexMut } ;
26
+ use ops:: { Drop , FnMut , Index , IndexMut } ;
27
27
use option:: Option :: { self , Some , None } ;
28
28
use rand:: { self , Rng } ;
29
29
use result:: Result :: { self , Ok , Err } ;
@@ -33,7 +33,6 @@ use super::table::{
33
33
Bucket ,
34
34
EmptyBucket ,
35
35
FullBucket ,
36
- FullBucketImm ,
37
36
FullBucketMut ,
38
37
RawTable ,
39
38
SafeHash ,
@@ -441,19 +440,19 @@ fn robin_hood<'a, K: 'a, V: 'a>(bucket: FullBucketMut<'a, K, V>,
441
440
442
441
// Performs insertion with relaxed requirements.
443
442
// The caller should ensure that invariants of Robin Hood linear probing hold.
444
- fn insert_hashed_ordered < M : Put , K , V > ( arg : M , hash : SafeHash , key : K , val : V ) -> M
443
+ fn insert_hashed_ordered < M : Put , K , V > ( table : M , hash : SafeHash , key : K , val : V ) -> M
445
444
where M : BorrowMut < RawTable < K , V > >
446
445
{
447
446
let cap = table. borrow ( ) . capacity ( ) ;
448
- let mut buckets = Bucket :: new ( table, h ) . unwrap ( ) ;
447
+ let mut buckets = Bucket :: new ( table, hash ) . unwrap ( ) ;
449
448
let ib = buckets. index ( ) ;
450
449
451
450
while buckets. index ( ) != ib + cap {
452
451
// We don't need to compare hashes for value swap.
453
452
// Not even DIBs for Robin Hood.
454
453
buckets = match buckets. peek ( ) {
455
454
Empty ( empty) => {
456
- return empty. put ( h , k , v ) . into_table ( ) ;
455
+ return empty. put ( hash , key , val ) . into_table ( ) ;
457
456
}
458
457
Full ( full) => full. into_bucket ( )
459
458
} ;
@@ -474,19 +473,19 @@ impl<K, V, S> HashMap<K, V, S>
474
473
/// If you already have the hash for the key lying around, use
475
474
/// search_hashed.
476
475
fn search < ' a , Q : ?Sized > ( & ' a self , q : & Q )
477
- -> Option < InternalEntry < K , V , & ' a RawTable < K , V > > >
476
+ -> ( InternalEntry < K , V , & ' a RawTable < K , V > > , SafeHash )
478
477
where K : Borrow < Q > , Q : Eq + Hash
479
478
{
480
479
let hash = self . make_hash ( q) ;
481
- search_hashed ( & self . table , hash, |k| q. eq ( k. borrow ( ) ) )
480
+ ( search_hashed ( & self . table , hash, |k| q. eq ( k. borrow ( ) ) ) , hash )
482
481
}
483
482
484
483
fn search_mut < ' a , Q : ?Sized > ( & ' a mut self , q : & Q )
485
- -> Option < InternalEntry < K , V , & ' a mut RawTable < K , V > > >
484
+ -> ( InternalEntry < K , V , & ' a mut RawTable < K , V > > , SafeHash )
486
485
where K : Borrow < Q > , Q : Eq + Hash
487
486
{
488
487
let hash = self . make_hash ( q) ;
489
- search_hashed ( & mut self . table , hash, |k| q. eq ( k. borrow ( ) ) )
488
+ ( search_hashed ( & mut self . table , hash, |k| q. eq ( k. borrow ( ) ) ) , hash )
490
489
}
491
490
}
492
491
@@ -645,7 +644,7 @@ impl<K, V, S> HashMap<K, V, S>
645
644
// Grow the table.
646
645
let is_inplace = self . table . grow_inplace ( new_capacity) ;
647
646
648
- let mut destination = if is_inplace {
647
+ let destination = if is_inplace {
649
648
// Resizing in-place.
650
649
None
651
650
} else {
@@ -764,9 +763,9 @@ impl<K, V, S> HashMap<K, V, S>
764
763
765
764
// Shrink the table. Naive algorithm for resizing:
766
765
while let Some ( ( h, k, v) ) = old_table. take_front ( ) {
767
- match search_hashed ( & mut self . table , hash , |key| * key == k) {
766
+ match search_hashed ( & mut self . table , h , |key| * key == k) {
768
767
InternalEntry :: Vacant ( entry) => {
769
- entry. insert ( hash , k, v) ;
768
+ entry. insert ( h , k, v) ;
770
769
}
771
770
InternalEntry :: Occupied ( mut entry) => {
772
771
entry. insert ( v) ;
@@ -779,24 +778,24 @@ impl<K, V, S> HashMap<K, V, S>
779
778
}
780
779
}
781
780
782
- /// Insert a pre-hashed key-value pair, without first checking
783
- /// that there's enough room in the buckets. Returns a reference to the
784
- /// newly insert value.
785
- ///
786
- /// If the key already exists, the hashtable will be returned untouched
787
- /// and a reference to the existing element will be returned.
788
- fn insert_hashed_nocheck ( & mut self , hash : SafeHash , k : K , v : V ) -> Option < V > {
789
- match search_hashed ( & mut self . table , hash, |key| * key == k) {
790
- InternalEntry :: Vacant ( entry) => {
791
- entry. insert ( hash, k, v) ;
792
- return None ;
793
- }
794
- InternalEntry :: Occupied ( mut entry) => {
795
- return Some ( entry. insert ( v) ) ;
796
- }
797
- InternalEntry :: TableIsEmpty => unreachable ! ( )
798
- }
799
- }
781
+ // // / Insert a pre-hashed key-value pair, without first checking
782
+ // // / that there's enough room in the buckets. Returns a reference to the
783
+ // // / newly insert value.
784
+ // // /
785
+ // // / If the key already exists, the hashtable will be returned untouched
786
+ // // / and a reference to the existing element will be returned.
787
+ // fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> Option<V> {
788
+ // match search_hashed(&mut self.table, hash, |key| *key == k) {
789
+ // InternalEntry::Vacant(entry) => {
790
+ // entry.insert(hash, k, v);
791
+ // return None;
792
+ // }
793
+ // InternalEntry::Occupied(mut entry) => {
794
+ // return Some(entry.insert(v));
795
+ // }
796
+ // InternalEntry::TableIsEmpty => unreachable!()
797
+ // }
798
+ // }
800
799
801
800
/// An iterator visiting all keys in arbitrary order.
802
801
/// Iterator element type is `&'a K`.
@@ -935,13 +934,13 @@ impl<K, V, S> HashMap<K, V, S>
935
934
self . reserve ( 1 ) ;
936
935
937
936
match self . search_mut ( & key) {
938
- InternalEntry :: Occupied ( state) => Occupied ( state) ,
939
- InternalEntry :: Vacant ( bucket) => Vacant ( VacantEntry {
940
- elem : bucket,
941
- hash : hash,
937
+ ( InternalEntry :: Occupied ( state) , _) => Occupied ( state) ,
938
+ ( InternalEntry :: Vacant ( bucket) , hash) => Vacant ( VacantEntry {
942
939
key : key,
940
+ hash : hash,
941
+ elem : bucket,
943
942
} ) ,
944
- InternalEntry :: TableIsEmpty => unreachable ! ( )
943
+ ( InternalEntry :: TableIsEmpty , _ ) => unreachable ! ( )
945
944
}
946
945
}
947
946
@@ -1043,7 +1042,7 @@ impl<K, V, S> HashMap<K, V, S>
1043
1042
pub fn get < Q : ?Sized > ( & self , k : & Q ) -> Option < & V >
1044
1043
where K : Borrow < Q > , Q : Hash + Eq
1045
1044
{
1046
- self . search ( k) . into_option ( ) . map ( |bucket| bucket. into_refs ( ) . 1 )
1045
+ self . search ( k) . 0 . into_option ( ) . map ( |bucket| bucket. into_refs ( ) . 1 )
1047
1046
}
1048
1047
1049
1048
/// Returns true if the map contains a value for the specified key.
@@ -1066,7 +1065,7 @@ impl<K, V, S> HashMap<K, V, S>
1066
1065
pub fn contains_key < Q : ?Sized > ( & self , k : & Q ) -> bool
1067
1066
where K : Borrow < Q > , Q : Hash + Eq
1068
1067
{
1069
- self . search ( k) . into_option ( ) . is_some ( )
1068
+ self . search ( k) . 0 . into_option ( ) . is_some ( )
1070
1069
}
1071
1070
1072
1071
/// Returns a mutable reference to the value corresponding to the key.
@@ -1092,7 +1091,7 @@ impl<K, V, S> HashMap<K, V, S>
1092
1091
pub fn get_mut < Q : ?Sized > ( & mut self , k : & Q ) -> Option < & mut V >
1093
1092
where K : Borrow < Q > , Q : Hash + Eq
1094
1093
{
1095
- self . search_mut ( k) . into_option ( ) . map ( |bucket| bucket. into_mut_refs ( ) . 1 )
1094
+ self . search_mut ( k) . 0 . into_option ( ) . map ( |bucket| bucket. into_mut_refs ( ) . 1 )
1096
1095
}
1097
1096
1098
1097
/// Inserts a key-value pair from the map. If the key already had a value
@@ -1116,14 +1115,14 @@ impl<K, V, S> HashMap<K, V, S>
1116
1115
self . reserve ( 1 ) ;
1117
1116
1118
1117
match self . search_mut ( & key) {
1119
- InternalEntry :: Vacant ( entry) => {
1118
+ ( InternalEntry :: Occupied ( mut entry) , _) => {
1119
+ return Some ( entry. insert ( value) ) ;
1120
+ }
1121
+ ( InternalEntry :: Vacant ( entry) , hash) => {
1120
1122
entry. insert ( hash, key, value) ;
1121
1123
return None ;
1122
1124
}
1123
- InternalEntry :: Occupied ( mut entry) => {
1124
- return Some ( entry. insert ( value) ) ;
1125
- }
1126
- InternalEntry :: TableIsEmpty => unreachable ! ( )
1125
+ ( InternalEntry :: TableIsEmpty , _) => unreachable ! ( )
1127
1126
}
1128
1127
}
1129
1128
@@ -1152,7 +1151,7 @@ impl<K, V, S> HashMap<K, V, S>
1152
1151
return None
1153
1152
}
1154
1153
1155
- self . search_mut ( k) . into_option ( ) . map ( |bucket| pop_internal ( bucket) . 1 )
1154
+ self . search_mut ( k) . 0 . into_option ( ) . map ( |bucket| pop_internal ( bucket) . 1 )
1156
1155
}
1157
1156
}
1158
1157
0 commit comments