@@ -125,21 +125,20 @@ mod chained {
125
125
} ;
126
126
127
127
tag search_result<copy K , copy V > {
128
- not_found ( uint ) ;
128
+ not_found;
129
129
found_first ( uint, @entry<K , V >) ;
130
130
found_after ( @entry<K , V >, @entry<K , V >) ;
131
131
}
132
132
133
133
fn search_rem < copy K , copy V > ( tbl : t < K , V > ,
134
134
k : K ,
135
135
h : uint ,
136
- idx : uint ,
137
136
e_root : @entry < K , V > ) -> search_result < K , V > {
138
137
let e0 = e_root;
139
138
while true {
140
139
alt e0. next {
141
140
absent. {
142
- ret not_found ( idx ) ;
141
+ ret not_found ;
143
142
}
144
143
present ( e1) {
145
144
let e1_key = e1. key ; // Satisfy alias checker.
@@ -161,23 +160,25 @@ mod chained {
161
160
162
161
alt tbl. chains [ idx] {
163
162
absent. {
164
- ret not_found ( idx ) ;
163
+ ret not_found ;
165
164
}
166
165
present ( e) {
167
166
let e_key = e. key ; // Satisfy alias checker.
168
167
if e. hash == h && tbl. eqer ( e_key, k) {
169
168
ret found_first ( idx, e) ;
170
169
} else {
171
- ret search_rem ( tbl, k, h, idx , e) ;
170
+ ret search_rem ( tbl, k, h, e) ;
172
171
}
173
172
}
174
173
}
175
174
}
176
175
177
- fn insert_h < copy K , copy V > ( tbl : t < K , V > , k : K , v : V , hash : uint ) -> bool {
178
- // internal routine: does not update size
176
+ fn insert < copy K , copy V > ( tbl : t < K , V > , k : K , v : V ) -> bool {
177
+ let hash = tbl . hasher ( k ) ;
179
178
alt search_tbl( tbl, k, hash) {
180
- not_found ( idx) {
179
+ not_found. {
180
+ tbl . size += 1 u;
181
+ let idx = hash % vec:: len ( tbl. chains ) ;
181
182
let old_chain = tbl. chains [ idx] ;
182
183
tbl. chains [ idx] = present ( @{
183
184
hash: hash,
@@ -197,14 +198,9 @@ mod chained {
197
198
}
198
199
}
199
200
200
- fn insert < copy K , copy V > ( tbl : t < K , V > , k : K , v : V ) -> bool {
201
- tbl. size += 1 u;
202
- ret insert_h( tbl, k, v, tbl. hasher ( k) ) ;
203
- }
204
-
205
201
fn get < copy K , copy V > ( tbl : t < K , V > , k : K ) -> option:: t < V > {
206
202
alt search_tbl ( tbl, k, tbl. hasher ( k) ) {
207
- not_found ( _ ) {
203
+ not_found. {
208
204
ret option :: none;
209
205
}
210
206
@@ -220,7 +216,7 @@ mod chained {
220
216
221
217
fn remove < copy K , copy V > ( tbl : t < K , V > , k : K ) -> option:: t < V > {
222
218
alt search_tbl ( tbl, k, tbl. hasher ( k) ) {
223
- not_found ( _ ) {
219
+ not_found. {
224
220
ret option :: none;
225
221
}
226
222
@@ -247,8 +243,9 @@ mod chained {
247
243
alt chain {
248
244
absent. { ret; }
249
245
present ( entry) {
250
- blk ( entry) ;
251
- chain = entry. next ;
246
+ let next = entry. next ;
247
+ blk ( entry) ; // may modify entry.next!
248
+ chain = next;
252
249
}
253
250
}
254
251
}
@@ -269,7 +266,9 @@ mod chained {
269
266
let n_new_chains: uint = uint:: next_power_of_two ( n_old_chains + 1 u) ;
270
267
tbl. chains = chains ( n_new_chains) ;
271
268
foreach_chain ( old_chains) { |entry|
272
- insert_h ( tbl, entry. key , entry. value , entry. hash ) ;
269
+ let idx = entry. hash % n_new_chains;
270
+ entry. next = tbl. chains [ idx] ;
271
+ tbl. chains [ idx] = present ( entry) ;
273
272
}
274
273
}
275
274
0 commit comments