2010-07-16 20:14:52 -05:00
|
|
|
/**
|
|
|
|
* At the moment, this is a partial hashmap implementation, not yet fit for
|
|
|
|
* use, but useful as a stress test for rustboot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import std._int;
|
|
|
|
import std.sys;
|
|
|
|
import std.util;
|
|
|
|
import std._vec;
|
|
|
|
|
|
|
|
|
2010-08-03 20:06:31 -05:00
|
|
|
type hashfn[K] = fn(&K) -> uint;
|
2010-08-03 20:43:57 -05:00
|
|
|
type eqfn[K] = fn(&K, &K) -> bool;
|
2010-07-16 20:14:52 -05:00
|
|
|
|
|
|
|
type hashmap[K, V] = obj {
|
2010-08-26 21:44:38 -05:00
|
|
|
fn size() -> uint;
|
2010-08-03 20:57:44 -05:00
|
|
|
fn insert(&K key, &V val) -> bool;
|
2010-07-16 20:14:52 -05:00
|
|
|
fn contains_key(&K key) -> bool;
|
|
|
|
fn get(&K key) -> V;
|
|
|
|
fn find(&K key) -> util.option[V];
|
|
|
|
fn remove(&K key) -> util.option[V];
|
|
|
|
fn rehash();
|
|
|
|
};
|
|
|
|
|
|
|
|
fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|
|
|
|
2010-07-27 21:21:51 -05:00
|
|
|
let uint initial_capacity = 32u; // 2^5
|
2010-07-16 20:14:52 -05:00
|
|
|
let util.rational load_factor = rec(num=3, den=4);
|
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
tag bucket[K, V] {
|
2010-09-21 01:56:43 -05:00
|
|
|
nil;
|
|
|
|
deleted;
|
2010-09-09 17:59:29 -05:00
|
|
|
some(K, V);
|
|
|
|
}
|
2010-08-03 19:52:35 -05:00
|
|
|
|
|
|
|
fn make_buckets[K, V](uint nbkts) -> vec[mutable bucket[K, V]] {
|
2010-09-21 01:56:43 -05:00
|
|
|
ret _vec.init_elt[mutable bucket[K, V]](nil[K, V], nbkts);
|
2010-08-03 19:52:35 -05:00
|
|
|
}
|
2010-07-16 20:14:52 -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
|
|
|
|
//
|
|
|
|
// hash(key, i) := hashl(key) + i * hashr(key) for i = 0, 1, 2, ...
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// probes all buckets for a fixed key. Note that hashr is made to
|
|
|
|
// 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.
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn hashl[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
2010-08-24 19:23:09 -05:00
|
|
|
ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u));
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn hashr[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
|
2010-07-27 21:21:51 -05:00
|
|
|
ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
|
2010-08-24 19:23:09 -05:00
|
|
|
& hasher(key)) * 2u + 1u);
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn hash[K](&hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint {
|
2010-08-24 19:23:09 -05:00
|
|
|
ret (hashl[K](hasher, nbkts, key)
|
|
|
|
+ i * hashr[K](hasher, nbkts, key)) % nbkts;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 19:52:35 -05:00
|
|
|
/**
|
|
|
|
* We attempt to never call this with a full table. If we do, it
|
|
|
|
* will fail.
|
|
|
|
*/
|
2010-08-03 20:43:57 -05:00
|
|
|
fn insert_common[K, V](&hashfn[K] hasher,
|
|
|
|
&eqfn[K] eqer,
|
2010-08-03 19:52:35 -05:00
|
|
|
vec[mutable bucket[K, V]] bkts,
|
|
|
|
uint nbkts,
|
|
|
|
&K key,
|
|
|
|
&V val)
|
2010-08-03 20:43:57 -05:00
|
|
|
-> bool
|
2010-08-03 19:52:35 -05:00
|
|
|
{
|
|
|
|
let uint i = 0u;
|
|
|
|
while (i < nbkts) {
|
2010-08-25 19:42:36 -05:00
|
|
|
let uint j = hash[K](hasher, nbkts, key, i);
|
2010-08-03 19:52:35 -05:00
|
|
|
alt (bkts.(j)) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (some[K, V](?k, _)) {
|
2010-08-03 20:43:57 -05:00
|
|
|
if (eqer(key, k)) {
|
2010-08-03 20:57:44 -05:00
|
|
|
bkts.(j) = some[K, V](k, val);
|
2010-08-03 20:43:57 -05:00
|
|
|
ret false;
|
|
|
|
}
|
2010-08-03 19:52:35 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
bkts.(j) = some[K, V](key, val);
|
2010-08-03 20:43:57 -05:00
|
|
|
ret true;
|
2010-08-03 19:52:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail; // full table
|
|
|
|
}
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn find_common[K, V](&hashfn[K] hasher,
|
|
|
|
&eqfn[K] eqer,
|
2010-08-03 19:52:35 -05:00
|
|
|
vec[mutable bucket[K, V]] bkts,
|
2010-07-16 20:14:52 -05:00
|
|
|
uint nbkts,
|
|
|
|
&K key)
|
|
|
|
-> util.option[V]
|
|
|
|
{
|
2010-07-27 21:21:51 -05:00
|
|
|
let uint i = 0u;
|
2010-07-16 20:14:52 -05:00
|
|
|
while (i < nbkts) {
|
2010-08-05 12:10:39 -05:00
|
|
|
let uint j = (hash[K](hasher, nbkts, key, i));
|
2010-07-16 20:14:52 -05:00
|
|
|
alt (bkts.(j)) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (some[K, V](?k, ?v)) {
|
2010-08-03 20:43:57 -05:00
|
|
|
if (eqer(key, k)) {
|
|
|
|
ret util.some[V](v);
|
|
|
|
}
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-21 18:18:47 -05:00
|
|
|
case (nil[K, V]) {
|
2010-09-21 01:56:43 -05:00
|
|
|
ret util.none[V];
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-21 18:18:47 -05:00
|
|
|
case (deleted[K, V]) { }
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-08-03 20:43:57 -05:00
|
|
|
i += 1u;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
ret util.none[V];
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 19:52:35 -05:00
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn rehash[K, V](&hashfn[K] hasher,
|
|
|
|
&eqfn[K] eqer,
|
2010-08-03 19:52:35 -05:00
|
|
|
vec[mutable bucket[K, V]] oldbkts, uint noldbkts,
|
|
|
|
vec[mutable bucket[K, V]] newbkts, uint nnewbkts)
|
|
|
|
{
|
|
|
|
for (bucket[K, V] b in oldbkts) {
|
|
|
|
alt (b) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (some[K, V](?k, ?v)) {
|
2010-08-03 20:43:57 -05:00
|
|
|
insert_common[K, V](hasher, eqer, newbkts, nnewbkts, k, v);
|
2010-08-03 19:52:35 -05:00
|
|
|
}
|
|
|
|
case (_) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 20:14:52 -05:00
|
|
|
obj hashmap[K, V](hashfn[K] hasher,
|
|
|
|
eqfn[K] eqer,
|
2010-08-03 19:52:35 -05:00
|
|
|
mutable vec[mutable bucket[K, V]] bkts,
|
2010-07-16 20:14:52 -05:00
|
|
|
mutable uint nbkts,
|
|
|
|
mutable uint nelts,
|
|
|
|
util.rational lf)
|
|
|
|
{
|
2010-08-26 21:44:38 -05:00
|
|
|
fn size() -> uint { ret nelts; }
|
|
|
|
|
2010-08-03 20:57:44 -05:00
|
|
|
fn insert(&K key, &V val) -> bool {
|
2010-08-03 19:52:35 -05:00
|
|
|
let util.rational load = rec(num=(nelts + 1u) as int, den=nbkts as int);
|
|
|
|
if (!util.rational_leq(load, lf)) {
|
2010-08-20 13:40:59 -05:00
|
|
|
let uint nnewbkts = _uint.next_power_of_two(nbkts + 1u);
|
2010-08-03 19:52:35 -05:00
|
|
|
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nnewbkts);
|
2010-08-03 20:43:57 -05:00
|
|
|
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nnewbkts);
|
2010-08-25 19:42:36 -05:00
|
|
|
bkts = newbkts;
|
|
|
|
nbkts = nnewbkts;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-08-25 19:42:36 -05:00
|
|
|
|
2010-08-03 20:57:44 -05:00
|
|
|
if (insert_common[K, V](hasher, eqer, bkts, nbkts, key, val)) {
|
|
|
|
nelts += 1u;
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
ret false;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn contains_key(&K key) -> bool {
|
2010-08-03 20:43:57 -05:00
|
|
|
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
|
2010-07-16 20:14:52 -05:00
|
|
|
case (util.some[V](_)) { ret true; }
|
|
|
|
case (_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&K key) -> V {
|
2010-08-03 20:43:57 -05:00
|
|
|
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (util.some[V](?val)) { ret val; }
|
2010-07-16 20:14:52 -05:00
|
|
|
case (_) { fail; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find(&K key) -> util.option[V] {
|
2010-08-03 20:43:57 -05:00
|
|
|
be find_common[K, V](hasher, eqer, bkts, nbkts, key);
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&K key) -> util.option[V] {
|
2010-07-27 21:21:51 -05:00
|
|
|
let uint i = 0u;
|
2010-07-16 20:14:52 -05:00
|
|
|
while (i < nbkts) {
|
2010-08-05 12:10:39 -05:00
|
|
|
let uint j = (hash[K](hasher, nbkts, key, i));
|
2010-07-16 20:14:52 -05:00
|
|
|
alt (bkts.(j)) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (some[K, V](?k, ?v)) {
|
2010-08-26 21:44:38 -05:00
|
|
|
if (eqer(key, k)) {
|
2010-09-21 01:56:43 -05:00
|
|
|
bkts.(j) = deleted[K, V];
|
2010-08-26 21:44:38 -05:00
|
|
|
nelts -= 1u;
|
|
|
|
ret util.some[V](v);
|
|
|
|
}
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (deleted[K, V]) { }
|
|
|
|
case (nil[K, V]) {
|
|
|
|
ret util.none[V];
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 21:44:38 -05:00
|
|
|
i += 1u;
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
ret util.none[V];
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 19:52:35 -05:00
|
|
|
fn rehash() {
|
|
|
|
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nbkts);
|
2010-08-03 20:43:57 -05:00
|
|
|
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nbkts);
|
2010-08-03 19:52:35 -05:00
|
|
|
bkts = newbkts;
|
|
|
|
}
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 19:52:35 -05:00
|
|
|
let vec[mutable bucket[K, V]] bkts = make_buckets[K, V](initial_capacity);
|
2010-07-16 20:14:52 -05:00
|
|
|
|
2010-08-03 19:52:35 -05:00
|
|
|
ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor);
|
2010-07-16 20:14:52 -05:00
|
|
|
}
|