@@ -13,19 +13,38 @@ Sendable hash maps. Very much a work in progress.
13
13
type HashFn < K > = pure fn~( K ) -> uint ;
14
14
type EqFn < K > = pure fn~( K , K ) -> bool ;
15
15
16
+ trait send_map < K , V : copy > {
17
+ // FIXME(#3148) ^^^^ once find_ref() works, we can drop V:copy
18
+
19
+ fn insert ( & mut self , +k : K , +v : V ) -> bool ;
20
+ fn remove ( & mut self , k : & K ) -> bool ;
21
+ fn clear ( & mut self ) ;
22
+ pure fn len ( & const self ) -> uint ;
23
+ pure fn is_empty ( & const self ) -> bool ;
24
+ fn contains_key ( & const self , k : & K ) -> bool ;
25
+ fn each_ref ( & self , blk : fn ( k : & K , v : & V ) -> bool ) ;
26
+ fn each_key_ref ( & self , blk : fn ( k : & K ) -> bool ) ;
27
+ fn each_value_ref ( & self , blk : fn ( v : & V ) -> bool ) ;
28
+ fn find ( & const self , k : & K ) -> Option < V > ;
29
+ fn get ( & const self , k : & K ) -> V ;
30
+ }
31
+
16
32
/// Open addressing with linear probing.
17
33
mod linear {
18
34
export LinearMap , linear_map, linear_map_with_capacity, public_methods;
19
35
20
36
const initial_capacity: uint = 32 u; // 2^5
21
- type Bucket < K , V > = { hash : uint , key : K , value : V } ;
22
- enum LinearMap < K , V > {
23
- LinearMap_ ( {
24
- hashfn : pure fn~( x : & K ) -> uint ,
25
- eqfn : pure fn~( x : & K , y : & K ) -> bool ,
26
- resize_at : uint ,
27
- size : uint ,
28
- buckets : ~[ Option < Bucket < K , V > > ] } )
37
+ struct Bucket < K , V > {
38
+ hash : uint ;
39
+ key: K ;
40
+ value: V ;
41
+ }
42
+ struct LinearMap < K , V > {
43
+ hashfn : pure fn~( x : & K ) -> uint ;
44
+ eqfn: pure fn~( x: & K , y: & K ) -> bool;
45
+ resize_at: uint;
46
+ size: uint;
47
+ buckets: ~[ Option <Bucket <K , V >>] ;
29
48
}
30
49
31
50
// FIXME(#3148) -- we could rewrite found_entry
@@ -51,12 +70,13 @@ mod linear {
51
70
+eqfn : pure fn~( x : & K , y : & K ) -> bool ,
52
71
initial_capacity : uint ) -> LinearMap < K , V > {
53
72
54
- LinearMap_ ( {
73
+ LinearMap {
55
74
hashfn : hashfn,
56
75
eqfn : eqfn,
57
76
resize_at : resize_at ( initial_capacity) ,
58
77
size : 0 ,
59
- buckets: vec:: from_fn ( initial_capacity, |_i| None ) } )
78
+ buckets : vec:: from_fn ( initial_capacity, |_i| None )
79
+ }
60
80
}
61
81
62
82
priv impl < K , V > LinearMap < K , V > {
@@ -136,15 +156,19 @@ mod linear {
136
156
for uint:: range( 0 , old_capacity) |i| {
137
157
let mut bucket = None ;
138
158
bucket <-> old_buckets[ i] ;
139
- if bucket. is_some ( ) {
140
- self . insert_bucket ( bucket) ;
141
- }
159
+ self . insert_opt_bucket ( bucket) ;
142
160
}
143
161
}
144
162
145
- fn insert_bucket ( & mut self , +bucket : Option < Bucket < K , V > > ) {
146
- let { hash, key, value} <- option:: unwrap ( bucket ) ;
147
- let _ = self . insert_internal ( hash, key, value) ;
163
+ fn insert_opt_bucket ( & mut self , +bucket : Option < Bucket < K , V > > ) {
164
+ match move bucket {
165
+ Some ( Bucket { hash : move hash,
166
+ key : move key,
167
+ value : move value} ) => {
168
+ self . insert_internal ( hash, key, value) ;
169
+ }
170
+ None => { }
171
+ }
148
172
}
149
173
150
174
/// Inserts the key value pair into the buckets.
@@ -156,14 +180,18 @@ mod linear {
156
180
FoundHole ( idx) => {
157
181
debug ! ( "insert fresh (%?->%?) at idx %?, hash %?" ,
158
182
k, v, idx, hash) ;
159
- self . buckets [ idx] = Some ( { hash: hash, key: k, value: v} ) ;
183
+ self . buckets [ idx] = Some ( Bucket { hash : hash,
184
+ key : k,
185
+ value : v} ) ;
160
186
self . size += 1 ;
161
187
return true ;
162
188
}
163
189
FoundEntry ( idx) => {
164
190
debug ! ( "insert overwrite (%?->%?) at idx %?, hash %?" ,
165
191
k, v, idx, hash) ;
166
- self . buckets [ idx] = Some ( { hash: hash, key: k, value: v} ) ;
192
+ self . buckets [ idx] = Some ( Bucket { hash : hash,
193
+ key : k,
194
+ value : v} ) ;
167
195
return false ;
168
196
}
169
197
}
@@ -223,7 +251,7 @@ mod linear {
223
251
while self . buckets [ idx] . is_some ( ) {
224
252
let mut bucket = None ;
225
253
bucket <-> self . buckets [ idx] ;
226
- self . insert_bucket ( bucket) ;
254
+ self . insert_opt_bucket ( bucket) ;
227
255
idx = self . next_bucket ( idx, len_buckets) ;
228
256
}
229
257
self . size -= 1 ;
0 commit comments