Add some support for using a map like a set.

This commit is contained in:
Michael Sullivan 2011-07-21 18:14:39 -07:00
parent cb02425376
commit 4b59ae0aa9
2 changed files with 20 additions and 2 deletions

View File

@ -105,6 +105,11 @@ fn new_smallintmap_adapter[K, V](fn(&K) -> uint key_idx,
idx += 1u;
}
}
iter keys() -> K {
for each (@tup(K, V) p in self.items()) {
put p._0;
}
}
}
auto map = smallintmap::mk[V]();

View File

@ -1,6 +1,5 @@
/**
* At the moment, this is a partial hashmap implementation, not yet fit for
* use, but useful as a stress test for rustboot.
* Hashmap implementation.
*/
type hashfn[K] = fn(&K) -> uint ;
@ -16,7 +15,13 @@ type hashmap[K, V] =
fn remove(&K) -> option::t[V] ;
fn rehash() ;
iter items() -> @tup(K, V) ;
iter keys() -> K ;
};
type hashset[K] = hashmap[K, ()];
fn set_add[K](hashset[K] set, &K key) -> bool {
ret set.insert(key, ());
}
fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
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] {
}
}
}
iter keys() -> K {
for (bucket[K, V] b in bkts) {
alt (b) {
case (some(?k, _)) { put k; }
case (_) { }
}
}
}
}
auto bkts = make_buckets[K, V](initial_capacity);
ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor);