2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
2011-06-03 16:14:29 -07:00
|
|
|
/// A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
/// are O(highest integer key).
|
|
|
|
import option::none;
|
|
|
|
import option::some;
|
|
|
|
|
2011-06-19 18:02:37 -07:00
|
|
|
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
|
|
|
|
// to be.
|
|
|
|
type smallintmap[T] = @rec(mutable (option::t[T])[mutable] v);
|
2011-06-03 16:14:29 -07:00
|
|
|
|
|
|
|
fn mk[T]() -> smallintmap[T] {
|
2011-06-19 18:02:37 -07:00
|
|
|
let (option::t[T])[mutable] v = ~[mutable];
|
|
|
|
ret @rec(mutable v=v);
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert[T](&smallintmap[T] m, uint key, &T val) {
|
2011-06-19 18:02:37 -07:00
|
|
|
ivec::grow_set[option::t[T]](m.v, key, none[T], some[T](val));
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find[T](&smallintmap[T] m, uint key) -> option::t[T] {
|
2011-06-19 18:02:37 -07:00
|
|
|
if (key < ivec::len[option::t[T]](m.v)) { ret m.v.(key); }
|
2011-06-03 16:14:29 -07:00
|
|
|
ret none[T];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get[T](&smallintmap[T] m, uint key) -> T {
|
|
|
|
alt (find[T](m, key)) {
|
|
|
|
case (none[T]) {
|
|
|
|
log_err "smallintmap::get(): key not present";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
case (some[T](?v)) { ret v; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains_key[T](&smallintmap[T] m, uint key) -> bool {
|
|
|
|
ret !option::is_none(find[T](m, key));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn truncate[T](&smallintmap[T] m, uint len) {
|
2011-06-19 18:02:37 -07:00
|
|
|
m.v = ivec::slice_mut[option::t[T]](m.v, 0u, len);
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2011-06-19 18:02:37 -07:00
|
|
|
fn max_key[T](&smallintmap[T] m) -> uint { ret ivec::len[option::t[T]](m.v); }
|
|
|
|
|