@@ -59,9 +59,10 @@ mod linear {
59
59
buckets: vec:: from_fn ( initial_capacity, |_i| none) } )
60
60
}
61
61
62
- priv impl < K , V > & const LinearMap < K , V > {
62
+ priv impl < K , V > LinearMap < K , V > {
63
63
#[ inline( always) ]
64
- pure fn to_bucket ( h : uint ) -> uint {
64
+ pure fn to_bucket ( & const self ,
65
+ h : uint ) -> uint {
65
66
// FIXME(#3041) borrow a more sophisticated technique here from
66
67
// Gecko, for example borrowing from Knuth, as Eich so
67
68
// colorfully argues for here:
@@ -70,7 +71,9 @@ mod linear {
70
71
}
71
72
72
73
#[ inline( always) ]
73
- pure fn next_bucket ( idx : uint , len_buckets : uint ) -> uint {
74
+ pure fn next_bucket ( & const self ,
75
+ idx : uint ,
76
+ len_buckets : uint ) -> uint {
74
77
let n = ( idx + 1 ) % len_buckets;
75
78
unsafe { // argh. log not considered pure.
76
79
debug ! { "next_bucket(%?, %?) = %?" , idx, len_buckets, n} ;
@@ -79,7 +82,9 @@ mod linear {
79
82
}
80
83
81
84
#[ inline( always) ]
82
- pure fn bucket_sequence ( hash : uint , op : fn ( uint ) -> bool ) -> uint {
85
+ pure fn bucket_sequence ( & const self ,
86
+ hash : uint ,
87
+ op : fn ( uint ) -> bool ) -> uint {
83
88
let start_idx = self . to_bucket ( hash) ;
84
89
let len_buckets = self . buckets . len ( ) ;
85
90
let mut idx = start_idx;
@@ -95,20 +100,18 @@ mod linear {
95
100
}
96
101
97
102
#[ inline( always) ]
98
- pure fn bucket_for_key (
99
- buckets : & [ option < Bucket < K , V > > ] ,
100
- k : & K ) -> SearchResult {
101
-
103
+ pure fn bucket_for_key ( & const self ,
104
+ buckets : & [ option < Bucket < K , V > > ] ,
105
+ k : & K ) -> SearchResult {
102
106
let hash = self . hashfn ( k) ;
103
107
self . bucket_for_key_with_hash ( buckets, hash, k)
104
108
}
105
109
106
110
#[ inline( always) ]
107
- pure fn bucket_for_key_with_hash (
108
- buckets : & [ option < Bucket < K , V > > ] ,
109
- hash : uint ,
110
- k : & K ) -> SearchResult {
111
-
111
+ pure fn bucket_for_key_with_hash ( & const self ,
112
+ buckets : & [ option < Bucket < K , V > > ] ,
113
+ hash : uint ,
114
+ k : & K ) -> SearchResult {
112
115
let _ = for self . bucket_sequence( hash) |i| {
113
116
match buckets[ i] {
114
117
some( bkt) => if bkt. hash == hash && self . eqfn ( k, & bkt. key ) {
@@ -119,12 +122,10 @@ mod linear {
119
122
} ;
120
123
return TableFull ;
121
124
}
122
- }
123
125
124
- priv impl < K , V > & mut LinearMap < K , V > {
125
126
/// Expands the capacity of the array and re-inserts each
126
127
/// of the existing buckets.
127
- fn expand ( ) {
128
+ fn expand ( & mut self ) {
128
129
let old_capacity = self . buckets . len ( ) ;
129
130
let new_capacity = old_capacity * 2 ;
130
131
self . resize_at = ( ( new_capacity as float ) * 3.0 / 4.0 ) as uint ;
@@ -141,15 +142,15 @@ mod linear {
141
142
}
142
143
}
143
144
144
- fn insert_bucket ( +bucket : option < Bucket < K , V > > ) {
145
+ fn insert_bucket ( & mut self , +bucket : option < Bucket < K , V > > ) {
145
146
let { hash, key, value} <- option:: unwrap ( bucket ) ;
146
147
let _ = self . insert_internal ( hash, key, value) ;
147
148
}
148
149
149
150
/// Inserts the key value pair into the buckets.
150
151
/// Assumes that there will be a bucket.
151
152
/// True if there was no previous entry with that key
152
- fn insert_internal ( hash : uint , +k : K , +v : V ) -> bool {
153
+ fn insert_internal ( & mut self , hash : uint , +k : K , +v : V ) -> bool {
153
154
match self . bucket_for_key_with_hash ( self . buckets , hash, & k) {
154
155
TableFull => { fail ~"Internal logic error"; }
155
156
FoundHole ( idx) => {
@@ -167,10 +168,16 @@ mod linear {
167
168
}
168
169
}
169
170
}
171
+
172
+ fn search ( & self ,
173
+ hash : uint ,
174
+ op : fn ( x : & option < Bucket < K , V > > ) -> bool ) {
175
+ let _ = self . bucket_sequence ( hash, |i| op ( & self . buckets [ i] ) ) ;
176
+ }
170
177
}
171
178
172
- impl < K , V > & mut LinearMap < K , V > {
173
- fn insert ( +k : K , +v : V ) -> bool {
179
+ impl < K , V > LinearMap < K , V > {
180
+ fn insert ( & mut self , +k : K , +v : V ) -> bool {
174
181
if self . size >= self . resize_at {
175
182
// n.b.: We could also do this after searching, so
176
183
// that we do not resize if this call to insert is
@@ -185,7 +192,7 @@ mod linear {
185
192
self . insert_internal ( hash, k, v)
186
193
}
187
194
188
- fn remove ( k : & K ) -> bool {
195
+ fn remove ( & mut self , k : & K ) -> bool {
189
196
// Removing from an open-addressed hashtable
190
197
// is, well, painful. The problem is that
191
198
// the entry may lie on the probe path for other
@@ -223,69 +230,33 @@ mod linear {
223
230
return true ;
224
231
}
225
232
226
- fn clear ( ) {
233
+ fn clear ( & mut self ) {
227
234
for uint:: range( 0 , self . buckets. len( ) ) |idx| {
228
235
self . buckets [ idx] = none;
229
236
}
230
237
self . size = 0 ;
231
238
}
232
- }
233
-
234
- priv impl < K , V > & LinearMap < K , V > {
235
- fn search ( hash : uint , op : fn ( x : & option < Bucket < K , V > > ) -> bool ) {
236
- let _ = self . bucket_sequence ( hash, |i| op ( & self . buckets [ i] ) ) ;
237
- }
238
- }
239
239
240
- impl < K , V > & const LinearMap < K , V > {
241
- pure fn len ( ) -> uint {
240
+ pure fn len ( & const self ) -> uint {
242
241
self . size
243
242
}
244
243
245
- pure fn is_empty ( ) -> bool {
244
+ pure fn is_empty ( & const self ) -> bool {
246
245
self . len ( ) == 0
247
246
}
248
247
249
- fn contains_key ( k : & K ) -> bool {
248
+ fn contains_key ( & const self ,
249
+ k : & K ) -> bool {
250
250
match self . bucket_for_key ( self . buckets , k) {
251
251
FoundEntry ( _) => { true }
252
252
TableFull | FoundHole ( _) => { false }
253
253
}
254
254
}
255
- }
256
-
257
- impl < K , V : copy > & const LinearMap < K , V > {
258
- fn find ( k : & K ) -> option < V > {
259
- match self . bucket_for_key ( self . buckets , k) {
260
- FoundEntry ( idx) => {
261
- match self . buckets [ idx] {
262
- some( bkt) => { some ( copy bkt. value ) }
263
- // FIXME (#3148): Will be able to get rid of this when we
264
- // redefine SearchResult
265
- none => fail ~"LinearMap :: find: internal logic error"
266
- }
267
- }
268
- TableFull | FoundHole ( _) => {
269
- none
270
- }
271
- }
272
- }
273
255
274
- fn get ( k : & K ) -> V {
275
- let value = self . find ( k) ;
276
- if value. is_none ( ) {
277
- fail fmt ! { "No entry found for key: %?" , k} ;
278
- }
279
- option:: unwrap ( value)
280
- }
281
-
282
- }
283
-
284
- impl < K , V > & LinearMap < K , V > {
285
256
/*
286
257
FIXME(#3148)--region inference fails to capture needed deps
287
258
288
- fn find_ref(k: &K) -> option<&self/V> {
259
+ fn find_ref(&self, k: &K) -> option<&self/V> {
289
260
match self.bucket_for_key(self.buckets, k) {
290
261
FoundEntry(idx) => {
291
262
match check self.buckets[idx] {
@@ -299,7 +270,7 @@ mod linear {
299
270
}
300
271
*/
301
272
302
- fn each_ref ( blk : fn ( k : & K , v : & V ) -> bool ) {
273
+ fn each_ref ( & self , blk : fn ( k : & K , v : & V ) -> bool ) {
303
274
for vec:: each( self . buckets) |slot| {
304
275
let mut broke = false ;
305
276
do slot. iter |bucket| {
@@ -310,26 +281,52 @@ mod linear {
310
281
if broke { break ; }
311
282
}
312
283
}
313
- fn each_key_ref ( blk : fn ( k : & K ) -> bool ) {
284
+
285
+ fn each_key_ref ( & self , blk : fn ( k : & K ) -> bool ) {
314
286
self . each_ref ( |k, _v| blk ( k) )
315
287
}
316
- fn each_value_ref ( blk : fn ( v : & V ) -> bool ) {
288
+
289
+ fn each_value_ref ( & self , blk : fn ( v : & V ) -> bool ) {
317
290
self . each_ref ( |_k, v| blk ( v) )
318
291
}
319
292
}
320
293
321
- impl < K : copy , V : copy > & LinearMap < K , V > {
322
- fn each ( blk : fn ( +K , +V ) -> bool ) {
294
+ impl < K , V : copy > LinearMap < K , V > {
295
+ fn find ( & const self , k : & K ) -> option < V > {
296
+ match self . bucket_for_key ( self . buckets , k) {
297
+ FoundEntry ( idx) => {
298
+ match check self. buckets [ idx] {
299
+ some( bkt) => { some ( copy bkt. value ) }
300
+ }
301
+ }
302
+ TableFull | FoundHole ( _) => {
303
+ none
304
+ }
305
+ }
306
+ }
307
+
308
+ fn get ( & const self , k : & K ) -> V {
309
+ let value = self . find ( k) ;
310
+ if value. is_none ( ) {
311
+ fail fmt ! { "No entry found for key: %?" , k} ;
312
+ }
313
+ option:: unwrap ( value)
314
+ }
315
+
316
+ }
317
+
318
+ impl < K : copy , V : copy > LinearMap < K , V > {
319
+ fn each ( & self , blk : fn ( +K , +V ) -> bool ) {
323
320
self . each_ref ( |k, v| blk ( copy * k, copy * v) ) ;
324
321
}
325
322
}
326
- impl < K : copy , V > & LinearMap < K , V > {
327
- fn each_key ( blk : fn ( +K ) -> bool ) {
323
+ impl < K : copy , V > LinearMap < K , V > {
324
+ fn each_key ( & self , blk : fn ( +K ) -> bool ) {
328
325
self . each_key_ref ( |k| blk ( copy * k) ) ;
329
326
}
330
327
}
331
- impl < K , V : copy > & LinearMap < K , V > {
332
- fn each_value ( blk : fn ( +V ) -> bool ) {
328
+ impl < K , V : copy > LinearMap < K , V > {
329
+ fn each_value ( & self , blk : fn ( +V ) -> bool ) {
333
330
self . each_value_ref ( |v| blk ( copy * v) ) ;
334
331
}
335
332
}
0 commit comments