2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Module: smallintmap
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
are O(highest integer key).
|
|
|
|
*/
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
|
|
|
import core::option::{some, none};
|
2011-06-03 18:14:29 -05:00
|
|
|
|
2011-06-19 20:02:37 -05:00
|
|
|
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
|
|
|
|
// to be.
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Type: smallintmap
|
|
|
|
*/
|
2012-01-31 19:05:20 -06:00
|
|
|
type smallintmap<T> = @{mutable v: [mutable option<T>]};
|
2011-06-03 18:14:29 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Function: mk
|
|
|
|
|
|
|
|
Create a smallintmap
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn mk<T>() -> smallintmap<T> {
|
2012-01-31 19:05:20 -06:00
|
|
|
let v: [mutable option<T>] = [mutable];
|
2011-07-27 07:19:39 -05:00
|
|
|
ret @{mutable v: v};
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05: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.
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn insert<T: copy>(m: smallintmap<T>, key: uint, val: T) {
|
2012-01-31 19:05:20 -06:00
|
|
|
vec::grow_set::<option<T>>(m.v, key, none::<T>, some::<T>(val));
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Function: find
|
|
|
|
|
|
|
|
Get the value for the specified key. If the key does not exist
|
2012-01-19 00:37:22 -06:00
|
|
|
in the map then returns none
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2012-01-31 19:05:20 -06:00
|
|
|
fn find<T: copy>(m: smallintmap<T>, key: uint) -> option<T> {
|
|
|
|
if key < vec::len::<option<T>>(m.v) { ret m.v[key]; }
|
2011-08-12 12:56:57 -05:00
|
|
|
ret none::<T>;
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Method: get
|
|
|
|
|
|
|
|
Get the value for the specified key
|
|
|
|
|
|
|
|
Failure:
|
|
|
|
|
|
|
|
If the key does not exist in the map
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
|
2011-11-18 05:39:20 -06:00
|
|
|
alt find(m, key) {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { #error("smallintmap::get(): key not present"); fail; }
|
2011-11-18 05:39:20 -06:00
|
|
|
some(v) { ret v; }
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Method: contains_key
|
|
|
|
|
|
|
|
Returns true if the map contains a value for the specified key
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn contains_key<T: copy>(m: smallintmap<T>, key: uint) -> bool {
|
2011-08-12 12:56:57 -05:00
|
|
|
ret !option::is_none(find::<T>(m, key));
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
// FIXME: Are these really useful?
|
|
|
|
|
2012-01-05 08:35:37 -06:00
|
|
|
fn truncate<T: copy>(m: smallintmap<T>, len: uint) {
|
2012-03-01 01:44:52 -06:00
|
|
|
m.v = vec::to_mut(vec::slice::<option<T>>(m.v, 0u, len));
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn max_key<T>(m: smallintmap<T>) -> uint {
|
2012-01-31 19:05:20 -06:00
|
|
|
ret vec::len::<option<T>>(m.v);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-19 20:02:37 -05:00
|
|
|
|
2012-01-09 09:24:53 -06:00
|
|
|
/*
|
|
|
|
Impl: map
|
|
|
|
|
|
|
|
Implements the map::map interface for smallintmap
|
|
|
|
*/
|
|
|
|
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
|
|
|
|
fn size() -> uint {
|
|
|
|
let sz = 0u;
|
|
|
|
for item in self.v {
|
|
|
|
alt item { some(_) { sz += 1u; } _ {} }
|
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
|
|
|
fn insert(&&key: uint, value: V) -> bool {
|
|
|
|
let exists = contains_key(self, key);
|
|
|
|
insert(self, key, value);
|
|
|
|
ret !exists;
|
|
|
|
}
|
2012-01-31 19:05:20 -06:00
|
|
|
fn remove(&&key: uint) -> option<V> {
|
2012-01-09 09:24:53 -06:00
|
|
|
if key >= vec::len(self.v) { ret none; }
|
|
|
|
let old = self.v[key];
|
|
|
|
self.v[key] = none;
|
|
|
|
old
|
|
|
|
}
|
|
|
|
fn contains_key(&&key: uint) -> bool {
|
|
|
|
contains_key(self, key)
|
|
|
|
}
|
|
|
|
fn get(&&key: uint) -> V { get(self, key) }
|
2012-01-31 19:05:20 -06:00
|
|
|
fn find(&&key: uint) -> option<V> { find(self, key) }
|
2012-01-09 09:24:53 -06:00
|
|
|
fn rehash() { fail }
|
2012-01-23 16:59:00 -06:00
|
|
|
fn items(it: fn(&&uint, V)) {
|
2012-01-09 09:24:53 -06:00
|
|
|
let idx = 0u;
|
|
|
|
for item in self.v {
|
|
|
|
alt item {
|
|
|
|
some(elt) {
|
|
|
|
it(idx, elt);
|
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none { }
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-01-23 16:59:00 -06:00
|
|
|
fn keys(it: fn(&&uint)) {
|
2012-01-09 09:24:53 -06:00
|
|
|
let idx = 0u;
|
|
|
|
for item in self.v {
|
|
|
|
if item != none { it(idx); }
|
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-01-23 16:59:00 -06:00
|
|
|
fn values(it: fn(V)) {
|
2012-01-09 09:24:53 -06:00
|
|
|
for item in self.v {
|
|
|
|
alt item { some(elt) { it(elt); } _ {} }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Funtion: as_map
|
|
|
|
|
|
|
|
Cast the given smallintmap to a map::map
|
|
|
|
*/
|
|
|
|
fn as_map<V>(s: smallintmap<V>) -> map::map<uint, V> {
|
|
|
|
s as map::map::<uint, V>
|
|
|
|
}
|