/* Module: smallintmap A simple map based on a vector for small integer keys. Space requirements are O(highest integer key). */ import option::{some, none}; // FIXME: Should not be @; there's a bug somewhere in rustc that requires this // to be. /* Type: smallintmap */ type smallintmap = @{mutable v: [mutable option::t]}; /* Function: mk Create a smallintmap */ fn mk() -> smallintmap { let v: [mutable option::t] = [mutable]; ret @{mutable v: v}; } /* 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. */ fn insert(m: smallintmap, key: uint, val: T) { vec::grow_set::>(m.v, key, none::, some::(val)); } /* Function: find Get the value for the specified key. If the key does not exist in the map then returns none. */ fn find(m: smallintmap, key: uint) -> option::t { if key < vec::len::>(m.v) { ret m.v[key]; } ret none::; } /* Method: get Get the value for the specified key Failure: If the key does not exist in the map */ fn get(m: smallintmap, key: uint) -> T { alt find(m, key) { none. { log_err "smallintmap::get(): key not present"; fail; } some(v) { ret v; } } } /* Method: contains_key Returns true if the map contains a value for the specified key */ fn contains_key(m: smallintmap, key: uint) -> bool { ret !option::is_none(find::(m, key)); } // FIXME: Are these really useful? fn truncate(m: smallintmap, len: uint) { m.v = vec::slice_mut::>(m.v, 0u, len); } fn max_key(m: smallintmap) -> uint { ret vec::len::>(m.v); }