2010-07-16 20:14:52 -05:00
|
|
|
/**
|
2011-07-21 20:14:39 -05:00
|
|
|
* Hashmap implementation.
|
2010-07-16 20:14:52 -05:00
|
|
|
*/
|
2011-08-19 17:16:48 -05:00
|
|
|
type hashfn<K> = fn(&K) -> uint;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
type eqfn<K> = fn(&K, &K) -> bool;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
type hashmap<K, V> =
|
2011-06-15 13:19:50 -05:00
|
|
|
obj {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn size() -> uint;
|
|
|
|
fn insert(&K, &V) -> bool;
|
|
|
|
fn contains_key(&K) -> bool;
|
|
|
|
fn get(&K) -> V;
|
|
|
|
fn find(&K) -> option::t<V>;
|
|
|
|
fn remove(&K) -> option::t<V>;
|
|
|
|
fn rehash();
|
|
|
|
iter items() -> @{key: K, val: V};
|
|
|
|
iter keys() -> K;
|
2011-06-15 13:19:50 -05:00
|
|
|
};
|
2011-08-12 08:37:10 -05:00
|
|
|
type hashset<K> = hashmap<K, ()>;
|
2011-07-21 20:14:39 -05:00
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
fn set_add<@K>(set: hashset<K>, key: &K) -> bool { ret set.insert(key, ()); }
|
2010-07-16 20:14:52 -05:00
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
fn mk_hashmap<@K, @V>(hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let initial_capacity: uint = 32u; // 2^5
|
2010-09-22 17:44:13 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let load_factor: util::rational = {num: 3, den: 4};
|
2011-08-12 08:37:10 -05:00
|
|
|
tag bucket<@K, @V> { nil; deleted; some(K, V); }
|
2011-08-19 17:16:48 -05:00
|
|
|
fn make_buckets<@K, @V>(nbkts: uint) -> [mutable bucket<K, V>] {
|
2011-08-12 12:56:57 -05:00
|
|
|
ret vec::init_elt_mut::<bucket<K, V>>(nil::<K, V>, nbkts);
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
// Derive two hash functions from the one given by taking the upper
|
|
|
|
// half and lower half of the uint bits. Our bucket probing
|
|
|
|
// sequence is then defined by
|
|
|
|
//
|
2011-04-24 22:58:53 -05:00
|
|
|
// hash(key, i) := hashl(key) * i + hashr(key) for i = 0, 1, 2, ...
|
2010-09-22 17:44:13 -05:00
|
|
|
//
|
|
|
|
// Tearing the hash function apart this way is kosher in practice
|
|
|
|
// as, assuming 32-bit uints, the table would have to be at 2^32
|
|
|
|
// buckets before the resulting pair of hash functions no longer
|
2011-04-24 22:58:53 -05:00
|
|
|
// probes all buckets for a fixed key. Note that hashl is made to
|
2010-09-22 17:44:13 -05:00
|
|
|
// output odd numbers (hence coprime to the number of nbkts, which
|
|
|
|
// is always a power of 2), so that all buckets are probed for a
|
|
|
|
// fixed key.
|
|
|
|
|
2011-08-18 03:01:39 -05:00
|
|
|
fn hashl(n: uint, _nbkts: uint) -> uint { ret (n >>> 16u) * 2u + 1u; }
|
|
|
|
fn hashr(n: uint, _nbkts: uint) -> uint { ret 0x0000_ffff_u & n; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn hash(h: uint, nbkts: uint, i: uint) -> uint {
|
2011-04-24 22:58:53 -05:00
|
|
|
ret (hashl(h, nbkts) * i + hashr(h, nbkts)) % nbkts;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
/**
|
|
|
|
* We attempt to never call this with a full table. If we do, it
|
|
|
|
* will fail.
|
|
|
|
*/
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
fn insert_common<@K,
|
|
|
|
@V>(hasher: &hashfn<K>, eqer: &eqfn<K>,
|
|
|
|
bkts: &[mutable bucket<K, V>], nbkts: uint, key: &K,
|
|
|
|
val: &V) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = 0u;
|
|
|
|
let h: uint = hasher(key);
|
|
|
|
while i < nbkts {
|
|
|
|
let j: uint = hash(h, nbkts, i);
|
2011-08-19 17:16:48 -05:00
|
|
|
alt bkts[j] {
|
2011-07-27 07:19:39 -05:00
|
|
|
some(k, _) {
|
|
|
|
// Copy key to please alias analysis.
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let k_ = k;
|
2011-08-19 17:16:48 -05:00
|
|
|
if eqer(key, k_) { bkts[j] = some(k_, val); ret false; }
|
2011-07-27 07:19:39 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
_ { bkts[j] = some(key, val); ret true; }
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
fail; // full table
|
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
fn find_common<@K,
|
|
|
|
@V>(hasher: &hashfn<K>, eqer: &eqfn<K>,
|
|
|
|
bkts: &[mutable bucket<K, V>], nbkts: uint, key: &K) ->
|
|
|
|
option::t<V> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = 0u;
|
|
|
|
let h: uint = hasher(key);
|
|
|
|
while i < nbkts {
|
|
|
|
let j: uint = hash(h, nbkts, i);
|
2011-08-19 17:16:48 -05:00
|
|
|
alt bkts[j] {
|
2011-07-27 07:19:39 -05:00
|
|
|
some(k, v) {
|
|
|
|
// Copy to please alias analysis.
|
|
|
|
let k_ = k;
|
|
|
|
let v_ = v;
|
2011-08-09 19:36:07 -05:00
|
|
|
if eqer(key, k_) { ret option::some(v_); }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
nil. { ret option::none; }
|
|
|
|
deleted. { }
|
2010-08-26 21:44:38 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
i += 1u;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
ret option::none;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
fn rehash<@K,
|
|
|
|
@V>(hasher: &hashfn<K>, eqer: &eqfn<K>,
|
|
|
|
oldbkts: &[mutable bucket<K, V>], _noldbkts: uint,
|
|
|
|
newbkts: &[mutable bucket<K, V>], nnewbkts: uint) {
|
2011-08-10 11:27:11 -05:00
|
|
|
for b: bucket<K, V> in oldbkts {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt b {
|
|
|
|
some(k_, v_) {
|
|
|
|
let k = k_;
|
|
|
|
let v = v_;
|
2011-08-09 19:36:07 -05:00
|
|
|
insert_common(hasher, eqer, newbkts, nnewbkts, k, v);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
obj hashmap<@K,
|
|
|
|
@V>(hasher: hashfn<K>,
|
|
|
|
eqer: eqfn<K>,
|
|
|
|
mutable bkts: [mutable bucket<K, V>],
|
|
|
|
mutable nbkts: uint,
|
|
|
|
mutable nelts: uint,
|
|
|
|
lf: util::rational) {
|
2011-06-15 13:19:50 -05:00
|
|
|
fn size() -> uint { ret nelts; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn insert(key: &K, val: &V) -> bool {
|
|
|
|
let load: util::rational =
|
|
|
|
{num: nelts + 1u as int, den: nbkts as int};
|
|
|
|
if !util::rational_leq(load, lf) {
|
|
|
|
let nnewbkts: uint = uint::next_power_of_two(nbkts + 1u);
|
2011-08-09 19:36:07 -05:00
|
|
|
let newbkts = make_buckets(nnewbkts);
|
|
|
|
rehash(hasher, eqer, bkts, nbkts, newbkts, nnewbkts);
|
2011-06-15 13:19:50 -05:00
|
|
|
bkts = newbkts;
|
|
|
|
nbkts = nnewbkts;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
if insert_common(hasher, eqer, bkts, nbkts, key, val) {
|
2011-06-15 13:19:50 -05:00
|
|
|
nelts += 1u;
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn contains_key(key: &K) -> bool {
|
2011-08-09 19:36:07 -05:00
|
|
|
ret alt find_common(hasher, eqer, bkts, nbkts, key) {
|
2011-07-27 07:19:39 -05:00
|
|
|
option::some(_) { true }
|
|
|
|
_ { false }
|
2011-05-21 23:53:14 -05:00
|
|
|
};
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get(key: &K) -> V {
|
2011-08-09 19:36:07 -05:00
|
|
|
ret alt find_common(hasher, eqer, bkts, nbkts, key) {
|
2011-07-27 07:19:39 -05:00
|
|
|
option::some(val) { val }
|
|
|
|
_ { fail }
|
2011-05-21 23:53:14 -05:00
|
|
|
};
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-08-10 11:27:11 -05:00
|
|
|
fn find(key: &K) -> option::t<V> {
|
2011-08-09 19:36:07 -05:00
|
|
|
be find_common(hasher, eqer, bkts, nbkts, key);
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-08-10 11:27:11 -05:00
|
|
|
fn remove(key: &K) -> option::t<V> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = 0u;
|
|
|
|
let h: uint = hasher(key);
|
|
|
|
while i < nbkts {
|
|
|
|
let j: uint = hash(h, nbkts, i);
|
2011-08-19 17:16:48 -05:00
|
|
|
alt bkts[j] {
|
2011-07-27 07:19:39 -05:00
|
|
|
some(k, v) {
|
|
|
|
let k_ = k;
|
|
|
|
let vo = option::some(v);
|
|
|
|
if eqer(key, k_) {
|
2011-08-19 17:16:48 -05:00
|
|
|
bkts[j] = deleted;
|
2011-07-27 07:19:39 -05:00
|
|
|
nelts -= 1u;
|
|
|
|
ret vo;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
deleted. { }
|
2011-08-09 19:36:07 -05:00
|
|
|
nil. { ret option::none; }
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
i += 1u;
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
ret option::none;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
fn rehash() {
|
2011-08-09 19:36:07 -05:00
|
|
|
let newbkts = make_buckets(nbkts);
|
|
|
|
rehash(hasher, eqer, bkts, nbkts, newbkts, nbkts);
|
2011-06-15 13:19:50 -05:00
|
|
|
bkts = newbkts;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
iter items() -> @{key: K, val: V} {
|
2011-08-10 11:27:11 -05:00
|
|
|
for b: bucket<K, V> in bkts {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt b { some(k, v) { put @{key: k, val: v}; } _ { } }
|
2010-09-22 19:04:08 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
}
|
2011-07-21 20:14:39 -05:00
|
|
|
iter keys() -> K {
|
2011-08-10 11:27:11 -05:00
|
|
|
for b: bucket<K, V> in bkts {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt b { some(k, _) { put k; } _ { } }
|
2011-07-21 20:14:39 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
let bkts = make_buckets(initial_capacity);
|
|
|
|
ret hashmap(hasher, eqer, bkts, initial_capacity, 0u, load_factor);
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
|
|
|
|
// Hash map constructors for basic types
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn new_str_hash<@V>() -> hashmap<str, V> {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret mk_hashmap(str::hash, str::eq);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
fn new_int_hash<@V>() -> hashmap<int, V> {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn hash_int(x: &int) -> uint { ret x as uint; }
|
|
|
|
fn eq_int(a: &int, b: &int) -> bool { ret a == b; }
|
2011-08-09 19:36:07 -05:00
|
|
|
ret mk_hashmap(hash_int, eq_int);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
fn new_uint_hash<@V>() -> hashmap<uint, V> {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn hash_uint(x: &uint) -> uint { ret x; }
|
|
|
|
fn eq_uint(a: &uint, b: &uint) -> bool { ret a == b; }
|
2011-08-09 19:36:07 -05:00
|
|
|
ret mk_hashmap(hash_uint, eq_uint);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-04-19 16:00:04 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 17:44:13 -05:00
|
|
|
// End:
|