Skip to content

Commit 4b59ae0

Browse files
committed
Add some support for using a map like a set.
1 parent cb02425 commit 4b59ae0

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/comp/middle/ast_map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ fn new_smallintmap_adapter[K, V](fn(&K) -> uint key_idx,
105105
idx += 1u;
106106
}
107107
}
108+
iter keys() -> K {
109+
for each (@tup(K, V) p in self.items()) {
110+
put p._0;
111+
}
112+
}
108113
}
109114

110115
auto map = smallintmap::mk[V]();

src/lib/map.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
* At the moment, this is a partial hashmap implementation, not yet fit for
3-
* use, but useful as a stress test for rustboot.
2+
* Hashmap implementation.
43
*/
54
type hashfn[K] = fn(&K) -> uint ;
65

@@ -16,7 +15,13 @@ type hashmap[K, V] =
1615
fn remove(&K) -> option::t[V] ;
1716
fn rehash() ;
1817
iter items() -> @tup(K, V) ;
18+
iter keys() -> K ;
1919
};
20+
type hashset[K] = hashmap[K, ()];
21+
22+
fn set_add[K](hashset[K] set, &K key) -> bool {
23+
ret set.insert(key, ());
24+
}
2025

2126
fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
2227
let uint initial_capacity = 32u; // 2^5
@@ -188,6 +193,14 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
188193
}
189194
}
190195
}
196+
iter keys() -> K {
197+
for (bucket[K, V] b in bkts) {
198+
alt (b) {
199+
case (some(?k, _)) { put k; }
200+
case (_) { }
201+
}
202+
}
203+
}
191204
}
192205
auto bkts = make_buckets[K, V](initial_capacity);
193206
ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor);

0 commit comments

Comments
 (0)