@@ -49,6 +49,25 @@ pub struct DictSize {
49
49
}
50
50
51
51
impl < T : Clone > Dict < T > {
52
+ fn resize ( & mut self ) {
53
+ let mut new_indices = HashMap :: with_capacity ( self . size ) ;
54
+ let mut new_entries = Vec :: with_capacity ( self . size ) ;
55
+ for maybe_entry in self . entries . drain ( 0 ..) {
56
+ if let Some ( entry) = maybe_entry {
57
+ let mut hash_index = entry. hash ;
58
+ // Faster version of lookup. No equality checks here.
59
+ // We assume dict doesn't contatins any duplicate keys
60
+ while new_indices. contains_key ( & hash_index) {
61
+ hash_index = Self :: next_index ( entry. hash , hash_index) ;
62
+ }
63
+ new_indices. insert ( hash_index, new_entries. len ( ) ) ;
64
+ new_entries. push ( Some ( entry) ) ;
65
+ }
66
+ }
67
+ self . indices = new_indices;
68
+ self . entries = new_entries;
69
+ }
70
+
52
71
fn unchecked_push (
53
72
& mut self ,
54
73
hash_index : HashIndex ,
@@ -79,6 +98,9 @@ impl<T: Clone> Dict<T> {
79
98
key : K ,
80
99
value : T ,
81
100
) -> PyResult < ( ) > {
101
+ if self . indices . len ( ) > 2 * self . size {
102
+ self . resize ( ) ;
103
+ }
82
104
match self . lookup ( vm, key) ? {
83
105
LookupResult :: Existing ( index) => {
84
106
// Update existing key
@@ -240,14 +262,18 @@ impl<T: Clone> Dict<T> {
240
262
}
241
263
242
264
// Update i to next probe location:
243
- hash_index = hash_index
244
- . wrapping_mul ( 5 )
245
- . wrapping_add ( perturb)
246
- . wrapping_add ( 1 ) ;
265
+ hash_index = Self :: next_index ( perturb, hash_index)
247
266
// warn!("Perturb value: {}", i);
248
267
}
249
268
}
250
269
270
+ fn next_index ( perturb : HashValue , hash_index : HashIndex ) -> HashIndex {
271
+ hash_index
272
+ . wrapping_mul ( 5 )
273
+ . wrapping_add ( perturb)
274
+ . wrapping_add ( 1 )
275
+ }
276
+
251
277
/// Retrieve and delete a key
252
278
pub fn pop < K : DictKey + Copy > ( & mut self , vm : & VirtualMachine , key : K ) -> PyResult < Option < T > > {
253
279
if let LookupResult :: Existing ( index) = self . lookup ( vm, key) ? {
0 commit comments