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