2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Module: smallintmap
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
are O(highest integer key).
|
|
|
|
*/
|
2011-09-12 16:13:28 -07:00
|
|
|
import option::{some, none};
|
2011-06-03 16:14:29 -07:00
|
|
|
|
2011-06-19 18:02:37 -07:00
|
|
|
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
|
|
|
|
// to be.
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Type: smallintmap
|
|
|
|
*/
|
2011-08-12 06:37:10 -07:00
|
|
|
type smallintmap<T> = @{mutable v: [mutable option::t<T>]};
|
2011-06-03 16:14:29 -07:00
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Function: mk
|
|
|
|
|
|
|
|
Create a smallintmap
|
|
|
|
*/
|
2011-10-25 15:56:55 +02:00
|
|
|
fn mk<T>() -> smallintmap<T> {
|
2011-08-19 15:16:48 -07:00
|
|
|
let v: [mutable option::t<T>] = [mutable];
|
2011-07-27 14:19:39 +02:00
|
|
|
ret @{mutable v: v};
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Function: insert
|
|
|
|
|
|
|
|
Add a value to the map. If the map already contains a value for
|
|
|
|
the specified key then the original value is replaced.
|
|
|
|
*/
|
2011-10-25 15:56:55 +02:00
|
|
|
fn insert<T>(m: smallintmap<T>, key: uint, val: T) {
|
2011-08-12 10:56:57 -07:00
|
|
|
vec::grow_set::<option::t<T>>(m.v, key, none::<T>, some::<T>(val));
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Function: find
|
|
|
|
|
|
|
|
Get the value for the specified key. If the key does not exist
|
|
|
|
in the map then returns none.
|
|
|
|
*/
|
2011-10-25 15:56:55 +02:00
|
|
|
fn find<T>(m: smallintmap<T>, key: uint) -> option::t<T> {
|
2011-08-19 15:16:48 -07:00
|
|
|
if key < vec::len::<option::t<T>>(m.v) { ret m.v[key]; }
|
2011-08-12 10:56:57 -07:00
|
|
|
ret none::<T>;
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Method: get
|
|
|
|
|
|
|
|
Get the value for the specified key
|
|
|
|
|
|
|
|
Failure:
|
|
|
|
|
|
|
|
If the key does not exist in the map
|
|
|
|
*/
|
2011-10-25 15:56:55 +02:00
|
|
|
fn get<T>(m: smallintmap<T>, key: uint) -> T {
|
2011-08-12 10:56:57 -07:00
|
|
|
alt find::<T>(m, key) {
|
|
|
|
none::<T>. { log_err "smallintmap::get(): key not present"; fail; }
|
|
|
|
some::<T>(v) { ret v; }
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
Method: contains_key
|
|
|
|
|
|
|
|
Returns true if the map contains a value for the specified key
|
|
|
|
*/
|
2011-10-25 15:56:55 +02:00
|
|
|
fn contains_key<T>(m: smallintmap<T>, key: uint) -> bool {
|
2011-08-12 10:56:57 -07:00
|
|
|
ret !option::is_none(find::<T>(m, key));
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 16:24:31 -07:00
|
|
|
// FIXME: Are these really useful?
|
|
|
|
|
2011-10-25 15:56:55 +02:00
|
|
|
fn truncate<T>(m: smallintmap<T>, len: uint) {
|
2011-08-12 10:56:57 -07:00
|
|
|
m.v = vec::slice_mut::<option::t<T>>(m.v, 0u, len);
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn max_key<T>(m: smallintmap<T>) -> uint {
|
2011-08-12 10:56:57 -07:00
|
|
|
ret vec::len::<option::t<T>>(m.v);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-19 18:02:37 -07:00
|
|
|
|