Skip to content

Commit e9b7ce6

Browse files
committed
refactor send_map impl to be based on structs
1 parent 0031617 commit e9b7ce6

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

src/libcore/send_map.rs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,38 @@ Sendable hash maps. Very much a work in progress.
1313
type HashFn<K> = pure fn~(K) -> uint;
1414
type EqFn<K> = pure fn~(K, K) -> bool;
1515

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+
1632
/// Open addressing with linear probing.
1733
mod linear {
1834
export LinearMap, linear_map, linear_map_with_capacity, public_methods;
1935

2036
const initial_capacity: uint = 32u; // 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>>];
2948
}
3049

3150
// FIXME(#3148) -- we could rewrite found_entry
@@ -51,12 +70,13 @@ mod linear {
5170
+eqfn: pure fn~(x: &K, y: &K) -> bool,
5271
initial_capacity: uint) -> LinearMap<K,V> {
5372

54-
LinearMap_({
73+
LinearMap {
5574
hashfn: hashfn,
5675
eqfn: eqfn,
5776
resize_at: resize_at(initial_capacity),
5877
size: 0,
59-
buckets: vec::from_fn(initial_capacity, |_i| None)})
78+
buckets: vec::from_fn(initial_capacity, |_i| None)
79+
}
6080
}
6181

6282
priv impl<K, V> LinearMap<K,V> {
@@ -136,15 +156,19 @@ mod linear {
136156
for uint::range(0, old_capacity) |i| {
137157
let mut bucket = None;
138158
bucket <-> old_buckets[i];
139-
if bucket.is_some() {
140-
self.insert_bucket(bucket);
141-
}
159+
self.insert_opt_bucket(bucket);
142160
}
143161
}
144162

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+
}
148172
}
149173

150174
/// Inserts the key value pair into the buckets.
@@ -156,14 +180,18 @@ mod linear {
156180
FoundHole(idx) => {
157181
debug!("insert fresh (%?->%?) at idx %?, hash %?",
158182
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});
160186
self.size += 1;
161187
return true;
162188
}
163189
FoundEntry(idx) => {
164190
debug!("insert overwrite (%?->%?) at idx %?, hash %?",
165191
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});
167195
return false;
168196
}
169197
}
@@ -223,7 +251,7 @@ mod linear {
223251
while self.buckets[idx].is_some() {
224252
let mut bucket = None;
225253
bucket <-> self.buckets[idx];
226-
self.insert_bucket(bucket);
254+
self.insert_opt_bucket(bucket);
227255
idx = self.next_bucket(idx, len_buckets);
228256
}
229257
self.size -= 1;

0 commit comments

Comments
 (0)