2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Module: map
|
|
|
|
|
|
|
|
A hashmap
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Section: Types */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Type: hashfn
|
|
|
|
|
2011-12-07 10:14:57 -06:00
|
|
|
A function that returns a hash of a value.
|
|
|
|
The hash should concentrate entropy in the
|
|
|
|
lower bits.
|
2011-10-26 13:28:23 -05:00
|
|
|
*/
|
2011-10-25 10:57:26 -05:00
|
|
|
type hashfn<K> = fn(K) -> uint;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Type: eqfn
|
|
|
|
|
|
|
|
Equality
|
|
|
|
*/
|
2011-10-25 10:57:26 -05:00
|
|
|
type eqfn<K> = fn(K, K) -> bool;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Type: hashset
|
|
|
|
|
|
|
|
A convenience type to treat a hashmap as a set
|
|
|
|
*/
|
2011-08-12 08:37:10 -05:00
|
|
|
type hashset<K> = hashmap<K, ()>;
|
2011-07-21 20:14:39 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Obj: hashmap
|
|
|
|
*/
|
|
|
|
type hashmap<K, V> = obj {
|
|
|
|
/*
|
|
|
|
Method: size
|
|
|
|
|
|
|
|
Return the number of elements in the map
|
|
|
|
*/
|
|
|
|
fn size() -> uint;
|
|
|
|
/*
|
|
|
|
Method: insert
|
|
|
|
|
|
|
|
Add a value to the map. If the map already contains a value for
|
|
|
|
the specified key then the original value is replaced.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
True if the key did not already exist in the map
|
|
|
|
*/
|
|
|
|
fn insert(K, V) -> bool;
|
|
|
|
/*
|
|
|
|
Method: contains_key
|
|
|
|
|
|
|
|
Returns true if the map contains a value for the specified key
|
|
|
|
*/
|
|
|
|
fn contains_key(K) -> bool;
|
|
|
|
/*
|
|
|
|
Method: get
|
|
|
|
|
|
|
|
Get the value for the specified key
|
|
|
|
|
|
|
|
Failure:
|
|
|
|
|
|
|
|
If the key does not exist in the map
|
|
|
|
*/
|
|
|
|
fn get(K) -> V;
|
|
|
|
/*
|
|
|
|
Method: find
|
2010-07-16 20:14:52 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
Get the value for the specified key. If the key does not exist
|
|
|
|
in the map then returns none.
|
|
|
|
*/
|
2011-12-13 18:25:51 -06:00
|
|
|
fn find(K) -> core::option::t<V>;
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Method: remove
|
|
|
|
|
|
|
|
Remove and return a value from the map. If the key does not exist
|
|
|
|
in the map then returns none.
|
|
|
|
*/
|
2011-12-13 18:25:51 -06:00
|
|
|
fn remove(K) -> core::option::t<V>;
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Method: rehash
|
|
|
|
|
|
|
|
Force map growth and rehashing
|
|
|
|
*/
|
|
|
|
fn rehash();
|
|
|
|
/*
|
|
|
|
Method: items
|
|
|
|
|
|
|
|
Iterate over all the key/value pairs in the map
|
|
|
|
*/
|
|
|
|
fn items(block(K, V));
|
|
|
|
/*
|
|
|
|
Method: keys
|
|
|
|
|
|
|
|
Iterate over all the keys in the map
|
|
|
|
*/
|
|
|
|
fn keys(block(K));
|
2011-12-20 20:43:00 -06:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Iterate over all the values in the map
|
|
|
|
*/
|
|
|
|
fn values(block(V));
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Section: Operations */
|
|
|
|
|
2011-12-06 21:56:47 -06:00
|
|
|
mod chained {
|
2012-01-05 08:35:37 -06:00
|
|
|
type entry<K: copy, V: copy> = {
|
2011-12-06 21:56:47 -06:00
|
|
|
hash: uint,
|
|
|
|
key: K,
|
|
|
|
mutable value: V,
|
|
|
|
mutable next: chain<K, V>
|
|
|
|
};
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
tag chain<K: copy, V: copy> {
|
2011-12-06 21:56:47 -06:00
|
|
|
present(@entry<K, V>);
|
|
|
|
absent;
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
type t<K: copy, V: copy> = {
|
2011-12-06 21:56:47 -06:00
|
|
|
mutable size: uint,
|
|
|
|
mutable chains: [mutable chain<K,V>],
|
|
|
|
hasher: hashfn<K>,
|
|
|
|
eqer: eqfn<K>
|
|
|
|
};
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
tag search_result<K: copy, V: copy> {
|
2011-12-06 22:22:12 -06:00
|
|
|
not_found;
|
2011-12-06 21:56:47 -06:00
|
|
|
found_first(uint, @entry<K,V>);
|
|
|
|
found_after(@entry<K,V>, @entry<K,V>);
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn search_rem<K: copy, V: copy>(tbl: t<K,V>,
|
2011-12-06 21:56:47 -06:00
|
|
|
k: K,
|
|
|
|
h: uint,
|
2011-12-06 23:33:47 -06:00
|
|
|
idx: uint,
|
2011-12-06 21:56:47 -06:00
|
|
|
e_root: @entry<K,V>) -> search_result<K,V> {
|
|
|
|
let e0 = e_root;
|
2011-12-06 23:33:47 -06:00
|
|
|
let comp = 1u; // for logging
|
2011-12-06 21:56:47 -06:00
|
|
|
while true {
|
|
|
|
alt e0.next {
|
|
|
|
absent. {
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
|
|
|
|
comp, h, idx);
|
2011-12-06 22:22:12 -06:00
|
|
|
ret not_found;
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
present(e1) {
|
2011-12-06 23:33:47 -06:00
|
|
|
comp += 1u;
|
2011-12-06 21:56:47 -06:00
|
|
|
let e1_key = e1.key; // Satisfy alias checker.
|
|
|
|
if e1.hash == h && tbl.eqer(e1_key, k) {
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("search_tbl: present, comp %u, hash %u, idx %u",
|
|
|
|
comp, h, idx);
|
2011-12-06 21:56:47 -06:00
|
|
|
ret found_after(e0, e1);
|
|
|
|
} else {
|
|
|
|
e0 = e1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util::unreachable();
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn search_tbl<K: copy, V: copy>(
|
2011-12-06 21:56:47 -06:00
|
|
|
tbl: t<K,V>, k: K, h: uint) -> search_result<K,V> {
|
|
|
|
let idx = h % vec::len(tbl.chains);
|
|
|
|
alt tbl.chains[idx] {
|
|
|
|
absent. {
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
|
|
|
|
0u, h, idx);
|
2011-12-06 22:22:12 -06:00
|
|
|
ret not_found;
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
present(e) {
|
|
|
|
let e_key = e.key; // Satisfy alias checker.
|
|
|
|
if e.hash == h && tbl.eqer(e_key, k) {
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("search_tbl: present, comp %u, hash %u, idx %u",
|
|
|
|
1u, h, idx);
|
2011-12-06 21:56:47 -06:00
|
|
|
ret found_first(idx, e);
|
|
|
|
} else {
|
2011-12-06 23:33:47 -06:00
|
|
|
ret search_rem(tbl, k, h, idx, e);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn insert<K: copy, V: copy>(tbl: t<K,V>, k: K, v: V) -> bool {
|
2011-12-06 22:22:12 -06:00
|
|
|
let hash = tbl.hasher(k);
|
2011-12-06 21:56:47 -06:00
|
|
|
alt search_tbl(tbl, k, hash) {
|
2011-12-06 22:22:12 -06:00
|
|
|
not_found. {
|
|
|
|
tbl.size += 1u;
|
|
|
|
let idx = hash % vec::len(tbl.chains);
|
2011-12-06 21:56:47 -06:00
|
|
|
let old_chain = tbl.chains[idx];
|
|
|
|
tbl.chains[idx] = present(@{
|
|
|
|
hash: hash,
|
|
|
|
key: k,
|
|
|
|
mutable value: v,
|
|
|
|
mutable next: old_chain});
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
found_first(_, entry) {
|
|
|
|
entry.value = v;
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
found_after(_, entry) {
|
|
|
|
entry.value = v;
|
|
|
|
ret false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn get<K: copy, V: copy>(tbl: t<K,V>, k: K) -> core::option::t<V> {
|
2011-12-06 21:56:47 -06:00
|
|
|
alt search_tbl(tbl, k, tbl.hasher(k)) {
|
2011-12-06 22:22:12 -06:00
|
|
|
not_found. {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::none;
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
found_first(_, entry) {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::some(entry.value);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
found_after(_, entry) {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::some(entry.value);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn remove<K: copy, V: copy>(tbl: t<K,V>, k: K) -> core::option::t<V> {
|
2011-12-06 21:56:47 -06:00
|
|
|
alt search_tbl(tbl, k, tbl.hasher(k)) {
|
2011-12-06 22:22:12 -06:00
|
|
|
not_found. {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::none;
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
found_first(idx, entry) {
|
2011-12-06 22:45:25 -06:00
|
|
|
tbl.size -= 1u;
|
2011-12-06 21:56:47 -06:00
|
|
|
tbl.chains[idx] = entry.next;
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::some(entry.value);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
found_after(eprev, entry) {
|
2011-12-06 22:45:25 -06:00
|
|
|
tbl.size -= 1u;
|
2011-12-06 21:56:47 -06:00
|
|
|
eprev.next = entry.next;
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::some(entry.value);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn chains<K: copy, V: copy>(nchains: uint) -> [mutable chain<K,V>] {
|
2011-12-06 21:56:47 -06:00
|
|
|
ret vec::init_elt_mut(absent, nchains);
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn foreach_entry<K: copy, V: copy>(chain0: chain<K,V>,
|
2011-12-06 21:56:47 -06:00
|
|
|
blk: block(@entry<K,V>)) {
|
|
|
|
let chain = chain0;
|
|
|
|
while true {
|
|
|
|
alt chain {
|
|
|
|
absent. { ret; }
|
|
|
|
present(entry) {
|
2011-12-06 22:22:12 -06:00
|
|
|
let next = entry.next;
|
|
|
|
blk(entry); // may modify entry.next!
|
|
|
|
chain = next;
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn foreach_chain<K: copy, V: copy>(chains: [const chain<K,V>],
|
2011-12-06 21:56:47 -06:00
|
|
|
blk: block(@entry<K,V>)) {
|
|
|
|
let i = 0u, n = vec::len(chains);
|
|
|
|
while i < n {
|
|
|
|
foreach_entry(chains[i], blk);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn rehash<K: copy, V: copy>(tbl: t<K,V>) {
|
2011-12-06 21:56:47 -06:00
|
|
|
let old_chains = tbl.chains;
|
|
|
|
let n_old_chains = vec::len(old_chains);
|
|
|
|
let n_new_chains: uint = uint::next_power_of_two(n_old_chains + 1u);
|
|
|
|
tbl.chains = chains(n_new_chains);
|
|
|
|
foreach_chain(old_chains) { |entry|
|
2011-12-06 22:22:12 -06:00
|
|
|
let idx = entry.hash % n_new_chains;
|
|
|
|
entry.next = tbl.chains[idx];
|
|
|
|
tbl.chains[idx] = present(entry);
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn items<K: copy, V: copy>(tbl: t<K,V>, blk: block(K,V)) {
|
2011-12-06 21:56:47 -06:00
|
|
|
let tbl_chains = tbl.chains; // Satisfy alias checker.
|
|
|
|
foreach_chain(tbl_chains) { |entry|
|
|
|
|
let key = entry.key;
|
|
|
|
let value = entry.value;
|
|
|
|
blk(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
obj o<K: copy, V: copy>(tbl: @t<K,V>,
|
2011-12-07 18:56:49 -06:00
|
|
|
lf: util::rational) {
|
2011-12-06 21:56:47 -06:00
|
|
|
fn size() -> uint {
|
|
|
|
ret tbl.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(k: K, v: V) -> bool {
|
|
|
|
let nchains = vec::len(tbl.chains);
|
2011-12-07 18:56:49 -06:00
|
|
|
let load = {num:tbl.size + 1u as int, den:nchains as int};
|
|
|
|
if !util::rational_leq(load, lf) {
|
2011-12-06 21:56:47 -06:00
|
|
|
rehash(*tbl);
|
|
|
|
}
|
|
|
|
ret insert(*tbl, k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains_key(k: K) -> bool {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::is_some(get(*tbl, k));
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get(k: K) -> V {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret core::option::get(get(*tbl, k));
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
fn find(k: K) -> core::option::t<V> {
|
2011-12-06 21:56:47 -06:00
|
|
|
ret get(*tbl, k);
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
fn remove(k: K) -> core::option::t<V> {
|
2011-12-06 21:56:47 -06:00
|
|
|
ret remove(*tbl, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rehash() {
|
|
|
|
rehash(*tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn items(blk: block(K, V)) {
|
|
|
|
items(*tbl, blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn keys(blk: block(K)) {
|
|
|
|
items(*tbl) { |k, _v| blk(k) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn values(blk: block(V)) {
|
|
|
|
items(*tbl) { |_k, v| blk(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn mk<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>)
|
|
|
|
-> hashmap<K,V> {
|
2011-12-06 21:56:47 -06:00
|
|
|
let initial_capacity: uint = 32u; // 2^5
|
|
|
|
let t = @{mutable size: 0u,
|
|
|
|
mutable chains: chains(initial_capacity),
|
|
|
|
hasher: hasher,
|
|
|
|
eqer: eqer};
|
2011-12-07 18:56:49 -06:00
|
|
|
ret o(t, {num:3, den:4});
|
2011-12-06 21:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 10:14:57 -06:00
|
|
|
/*
|
|
|
|
Function: mk_hashmap
|
|
|
|
|
|
|
|
Construct a hashmap.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
hasher - The hash function for key type K
|
|
|
|
eqer - The equality function for key type K
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn mk_hashmap<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>)
|
2011-12-06 21:56:47 -06:00
|
|
|
-> hashmap<K, V> {
|
|
|
|
ret chained::mk(hasher, eqer);
|
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: new_str_hash
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
Construct a hashmap for string keys
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn new_str_hash<V: copy>() -> 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
|
|
|
}
|
|
|
|
|
2012-01-06 09:36:56 -06:00
|
|
|
/*
|
|
|
|
Function: new_bytes_hash
|
|
|
|
|
|
|
|
Construct a hashmap for byte string keys
|
|
|
|
*/
|
|
|
|
fn new_bytes_hash<V: copy>() -> hashmap<[u8], V> {
|
|
|
|
ret mk_hashmap(vec::u8::hash, vec::u8::eq);
|
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: new_int_hash
|
|
|
|
|
|
|
|
Construct a hashmap for int keys
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn new_int_hash<V: copy>() -> hashmap<int, V> {
|
2011-12-26 20:59:50 -06:00
|
|
|
fn hash_int(&&x: int) -> uint { int::hash(x) }
|
2011-10-06 05:26:12 -05:00
|
|
|
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-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: new_uint_hash
|
|
|
|
|
|
|
|
Construct a hashmap for uint keys
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn new_uint_hash<V: copy>() -> hashmap<uint, V> {
|
2011-12-26 20:59:50 -06:00
|
|
|
fn hash_uint(&&x: uint) -> uint { uint::hash(x) }
|
2011-10-06 05:26:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-26 13:28:23 -05:00
|
|
|
/*
|
|
|
|
Function: set_add
|
|
|
|
|
|
|
|
Convenience function for adding keys to a hashmap with nil type keys
|
|
|
|
*/
|
|
|
|
fn set_add<K>(set: hashset<K>, key: K) -> bool { ret set.insert(key, ()); }
|
|
|
|
|
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
|
|
|
|
// End:
|