Skip to content

Commit b73d926

Browse files
committed
Merge pull request #4623 from thestinger/map
clean up LinearMap implementation
2 parents c3f4f65 + 5c14b91 commit b73d926

File tree

1 file changed

+59
-78
lines changed

1 file changed

+59
-78
lines changed

src/libcore/hashmap.rs

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,52 @@ pub mod linear {
3636

3737
const INITIAL_CAPACITY: uint = 32u; // 2^5
3838

39-
struct Bucket<K:Eq Hash,V> {
39+
struct Bucket<K: Eq Hash, V> {
4040
hash: uint,
4141
key: K,
4242
value: V,
4343
}
44-
pub struct LinearMap<K:Eq Hash,V> {
44+
45+
pub struct LinearMap<K: Eq Hash, V> {
4546
k0: u64,
4647
k1: u64,
4748
resize_at: uint,
4849
size: uint,
49-
buckets: ~[Option<Bucket<K,V>>],
50+
buckets: ~[Option<Bucket<K, V>>],
5051
}
5152

52-
// FIXME(#3148) -- we could rewrite found_entry
53-
// to have type option<&bucket<K,V>> which would be nifty
53+
// FIXME(#3148) -- we could rewrite FoundEntry
54+
// to have type Option<&Bucket<K, V>> which would be nifty
5455
// However, that won't work until #3148 is fixed
5556
enum SearchResult {
5657
FoundEntry(uint), FoundHole(uint), TableFull
5758
}
5859

59-
fn resize_at(capacity: uint) -> uint {
60+
pure fn resize_at(capacity: uint) -> uint {
6061
((capacity as float) * 3. / 4.) as uint
6162
}
6263

63-
pub fn linear_map_with_capacity<K:Eq Hash,V>(
64-
initial_capacity: uint) -> LinearMap<K,V> {
64+
pub fn linear_map_with_capacity<K: Eq Hash, V>(
65+
initial_capacity: uint) -> LinearMap<K, V> {
6566
let r = rand::Rng();
6667
linear_map_with_capacity_and_keys(r.gen_u64(), r.gen_u64(),
6768
initial_capacity)
6869
}
6970

70-
fn linear_map_with_capacity_and_keys<K:Eq Hash,V> (
71+
pure fn linear_map_with_capacity_and_keys<K: Eq Hash, V>(
7172
k0: u64, k1: u64,
72-
initial_capacity: uint) -> LinearMap<K,V> {
73+
initial_capacity: uint) -> LinearMap<K, V> {
7374
LinearMap {
7475
k0: k0, k1: k1,
7576
resize_at: resize_at(initial_capacity),
7677
size: 0,
77-
buckets: vec::from_fn(initial_capacity, |_i| None)
78+
buckets: vec::from_fn(initial_capacity, |_| None)
7879
}
7980
}
8081

81-
priv impl<K:Hash IterBytes Eq, V> LinearMap<K,V> {
82+
priv impl<K: Hash IterBytes Eq, V> LinearMap<K, V> {
8283
#[inline(always)]
83-
pure fn to_bucket(&const self,
84-
h: uint) -> uint {
84+
pure fn to_bucket(&self, h: uint) -> uint {
8585
// FIXME(#3041) borrow a more sophisticated technique here from
8686
// Gecko, for example borrowing from Knuth, as Eich so
8787
// colorfully argues for here:
@@ -90,17 +90,14 @@ pub mod linear {
9090
}
9191

9292
#[inline(always)]
93-
pure fn next_bucket(&const self,
94-
idx: uint,
95-
len_buckets: uint) -> uint {
93+
pure fn next_bucket(&self, idx: uint, len_buckets: uint) -> uint {
9694
let n = (idx + 1) % len_buckets;
9795
debug!("next_bucket(%?, %?) = %?", idx, len_buckets, n);
98-
return n;
96+
n
9997
}
10098

10199
#[inline(always)]
102-
pure fn bucket_sequence(&const self,
103-
hash: uint,
100+
pure fn bucket_sequence(&self, hash: uint,
104101
op: fn(uint) -> bool) -> uint {
105102
let start_idx = self.to_bucket(hash);
106103
let len_buckets = self.buckets.len();
@@ -117,16 +114,15 @@ pub mod linear {
117114
}
118115

119116
#[inline(always)]
120-
pure fn bucket_for_key(&const self,
121-
buckets: &[Option<Bucket<K,V>>],
117+
pure fn bucket_for_key(&self, buckets: &[Option<Bucket<K, V>>],
122118
k: &K) -> SearchResult {
123119
let hash = k.hash_keyed(self.k0, self.k1) as uint;
124120
self.bucket_for_key_with_hash(buckets, hash, k)
125121
}
126122

127123
#[inline(always)]
128-
pure fn bucket_for_key_with_hash(&const self,
129-
buckets: &[Option<Bucket<K,V>>],
124+
pure fn bucket_for_key_with_hash(&self,
125+
buckets: &[Option<Bucket<K, V>>],
130126
hash: uint,
131127
k: &K) -> SearchResult {
132128
let _ = for self.bucket_sequence(hash) |i| {
@@ -137,7 +133,7 @@ pub mod linear {
137133
None => return FoundHole(i)
138134
}
139135
};
140-
return TableFull;
136+
TableFull
141137
}
142138

143139
/// Expands the capacity of the array and re-inserts each
@@ -147,23 +143,21 @@ pub mod linear {
147143
let new_capacity = old_capacity * 2;
148144
self.resize_at = ((new_capacity as float) * 3.0 / 4.0) as uint;
149145

150-
let mut old_buckets = vec::from_fn(new_capacity, |_i| None);
146+
let mut old_buckets = vec::from_fn(new_capacity, |_| None);
151147
self.buckets <-> old_buckets;
152148

153149
self.size = 0;
154150
for uint::range(0, old_capacity) |i| {
155151
let mut bucket = None;
156152
bucket <-> old_buckets[i];
157-
self.insert_opt_bucket(move bucket);
153+
self.insert_opt_bucket(bucket);
158154
}
159155
}
160156

161-
fn insert_opt_bucket(&mut self, bucket: Option<Bucket<K,V>>) {
162-
match move bucket {
163-
Some(Bucket {hash: move hash,
164-
key: move key,
165-
value: move value}) => {
166-
self.insert_internal(hash, move key, move value);
157+
fn insert_opt_bucket(&mut self, bucket: Option<Bucket<K, V>>) {
158+
match bucket {
159+
Some(Bucket{hash: hash, key: key, value: value}) => {
160+
self.insert_internal(hash, key, value);
167161
}
168162
None => {}
169163
}
@@ -178,18 +172,16 @@ pub mod linear {
178172
FoundHole(idx) => {
179173
debug!("insert fresh (%?->%?) at idx %?, hash %?",
180174
k, v, idx, hash);
181-
self.buckets[idx] = Some(Bucket {hash: hash,
182-
key: move k,
183-
value: move v});
175+
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
176+
value: v});
184177
self.size += 1;
185178
true
186179
}
187180
FoundEntry(idx) => {
188181
debug!("insert overwrite (%?->%?) at idx %?, hash %?",
189182
k, v, idx, hash);
190-
self.buckets[idx] = Some(Bucket {hash: hash,
191-
key: move k,
192-
value: move v});
183+
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
184+
value: v});
193185
false
194186
}
195187
}
@@ -220,30 +212,28 @@ pub mod linear {
220212
let mut bucket = None;
221213
self.buckets[idx] <-> bucket;
222214

223-
let value = match move bucket {
215+
let value = match bucket {
224216
None => None,
225-
Some(move bucket) => {
226-
let Bucket { value: move value, _ } = move bucket;
227-
Some(move value)
217+
Some(bucket) => {
218+
let Bucket{value: value, _} = bucket;
219+
Some(value)
228220
},
229221
};
230222

231223
idx = self.next_bucket(idx, len_buckets);
232224
while self.buckets[idx].is_some() {
233225
let mut bucket = None;
234226
bucket <-> self.buckets[idx];
235-
self.insert_opt_bucket(move bucket);
227+
self.insert_opt_bucket(bucket);
236228
idx = self.next_bucket(idx, len_buckets);
237229
}
238230
self.size -= 1;
239231

240-
move value
241-
232+
value
242233
}
243234

244-
fn search(&self,
245-
hash: uint,
246-
op: fn(x: &Option<Bucket<K,V>>) -> bool) {
235+
fn search(&self, hash: uint,
236+
op: fn(x: &Option<Bucket<K, V>>) -> bool) {
247237
let _ = self.bucket_sequence(hash, |i| op(&self.buckets[i]));
248238
}
249239
}
@@ -277,7 +267,7 @@ pub mod linear {
277267

278268
/// Visit all key-value pairs
279269
pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) {
280-
for vec::each(self.buckets) |slot| {
270+
for self.buckets.each |slot| {
281271
let mut broke = false;
282272
do slot.iter |bucket| {
283273
if !blk(&bucket.key, &bucket.value) {
@@ -290,12 +280,12 @@ pub mod linear {
290280

291281
/// Visit all keys
292282
pure fn each_key(&self, blk: fn(k: &K) -> bool) {
293-
self.each(|k, _v| blk(k))
283+
self.each(|k, _| blk(k))
294284
}
295285

296286
/// Visit all values
297287
pure fn each_value(&self, blk: fn(v: &V) -> bool) {
298-
self.each(|_k, v| blk(v))
288+
self.each(|_, v| blk(v))
299289
}
300290

301291
/// Return the value corresponding to the key in the map
@@ -305,7 +295,7 @@ pub mod linear {
305295
match self.buckets[idx] {
306296
Some(ref bkt) => {
307297
// FIXME(#3148)---should be inferred
308-
let bkt: &self/Bucket<K,V> = bkt;
298+
let bkt: &self/Bucket<K, V> = bkt;
309299
Some(&bkt.value)
310300
}
311301
None => {
@@ -334,20 +324,17 @@ pub mod linear {
334324
}
335325

336326
let hash = k.hash_keyed(self.k0, self.k1) as uint;
337-
self.insert_internal(hash, move k, move v)
327+
self.insert_internal(hash, k, v)
338328
}
339329

340330
/// Remove a key-value pair from the map. Return true if the key
341331
/// was present in the map, otherwise false.
342332
fn remove(&mut self, k: &K) -> bool {
343-
match self.pop(k) {
344-
Some(_) => true,
345-
None => false,
346-
}
333+
self.pop(k).is_some()
347334
}
348335
}
349336

350-
pub impl<K:Hash IterBytes Eq,V> LinearMap<K,V> {
337+
pub impl<K:Hash IterBytes Eq, V> LinearMap<K, V> {
351338
/// Create an empty LinearMap
352339
static fn new() -> LinearMap<K, V> {
353340
linear_map_with_capacity(INITIAL_CAPACITY)
@@ -373,26 +360,22 @@ pub mod linear {
373360
self.expand();
374361
}
375362

376-
self.insert_internal(hash, move k, move v);
363+
self.insert_internal(hash, k, v);
377364

378-
move old_value
365+
old_value
379366
}
380367

381368
fn consume(&mut self, f: fn(K, V)) {
382369
let mut buckets = ~[];
383370
self.buckets <-> buckets;
384371
self.size = 0;
385372

386-
do vec::consume(move buckets) |_i, bucket| {
387-
match move bucket {
388-
None => { },
389-
Some(move bucket) => {
390-
let Bucket {
391-
key: move key,
392-
value: move value,
393-
_
394-
} = move bucket;
395-
f(move key, move value)
373+
do vec::consume(buckets) |_, bucket| {
374+
match bucket {
375+
None => {},
376+
Some(bucket) => {
377+
let Bucket{key: key, value: value, _} = bucket;
378+
f(key, value)
396379
}
397380
}
398381
}
@@ -406,8 +389,8 @@ pub mod linear {
406389
}
407390
}
408391

409-
impl<K:Hash IterBytes Eq, V: Copy> LinearMap<K, V> {
410-
pure fn find_copy(&const self, k: &K) -> Option<V> {
392+
impl<K: Hash IterBytes Eq, V: Copy> LinearMap<K, V> {
393+
pure fn find_copy(&self, k: &K) -> Option<V> {
411394
match self.bucket_for_key(self.buckets, k) {
412395
FoundEntry(idx) => {
413396
// FIXME (#3148): Once we rewrite found_entry, this
@@ -424,7 +407,7 @@ pub mod linear {
424407
}
425408
}
426409

427-
impl<K:Hash IterBytes Eq, V: Eq> LinearMap<K, V>: Eq {
410+
impl<K: Hash IterBytes Eq, V: Eq> LinearMap<K, V>: Eq {
428411
pure fn eq(&self, other: &LinearMap<K, V>) -> bool {
429412
if self.len() != other.len() { return false; }
430413

@@ -435,12 +418,10 @@ pub mod linear {
435418
}
436419
}
437420

438-
return true;
421+
true
439422
}
440423

441-
pure fn ne(&self, other: &LinearMap<K, V>) -> bool {
442-
!self.eq(other)
443-
}
424+
pure fn ne(&self, other: &LinearMap<K, V>) -> bool { !self.eq(other) }
444425
}
445426

446427
pub struct LinearSet<T: Hash IterBytes Eq> {

0 commit comments

Comments
 (0)