2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
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).
|
2012-03-07 20:17:30 -06:00
|
|
|
"];
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
|
|
|
import core::option::{some, none};
|
2012-05-12 21:09:09 -05:00
|
|
|
import dvec::{dvec, extensions};
|
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
|
2012-05-03 18:56:55 -05:00
|
|
|
// to be. (#2347)
|
2012-05-12 21:09:09 -05:00
|
|
|
type smallintmap<T: copy> = @{v: dvec<option<T>>};
|
2011-06-03 18:14:29 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Create a smallintmap"]
|
2012-03-19 04:45:29 -05:00
|
|
|
fn mk<T: copy>() -> smallintmap<T> {
|
2012-05-12 21:09:09 -05:00
|
|
|
ret @{v: dvec()};
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
Add a value to the map. If the map already contains a value for
|
|
|
|
the specified key then the original value is replaced.
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-05-12 21:09:09 -05:00
|
|
|
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
|
|
|
|
self.v.grow_set_elt(key, none, some(val));
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
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
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-05-12 21:09:09 -05:00
|
|
|
fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
|
|
|
|
if key < self.v.len() { ret self.v.get_elt(key); }
|
2011-08-12 12:56:57 -05:00
|
|
|
ret none::<T>;
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
Get the value for the specified key
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Failure
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
If the key does not exist in the map
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-05-12 21:09:09 -05:00
|
|
|
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
|
|
|
|
alt find(self, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
Returns true if the map contains a value for the specified key
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-05-12 21:09:09 -05:00
|
|
|
fn contains_key<T: copy>(self: smallintmap<T>, key: uint) -> bool {
|
|
|
|
ret !option::is_none(find(self, key));
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-19 20:02:37 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Implements the map::map interface for smallintmap"]
|
2012-01-09 09:24:53 -06:00
|
|
|
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
|
|
|
|
fn size() -> uint {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut sz = 0u;
|
2012-05-12 21:09:09 -05:00
|
|
|
for self.v.each {|item|
|
2012-01-09 09:24:53 -06:00
|
|
|
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-05-12 21:09:09 -05:00
|
|
|
if key >= self.v.len() { ret none; }
|
|
|
|
let old = self.v.get_elt(key);
|
|
|
|
self.v.set_elt(key, none);
|
2012-01-09 09:24:53 -06:00
|
|
|
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-04-23 06:42:15 -05:00
|
|
|
fn each(it: fn(&&uint, V) -> bool) {
|
2012-04-06 13:01:43 -05:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-05-12 21:09:09 -05:00
|
|
|
alt self.v.get_elt(idx) {
|
2012-01-09 09:24:53 -06:00
|
|
|
some(elt) {
|
2012-05-12 21:09:09 -05:00
|
|
|
if !it(idx, elt) { break; }
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none { }
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 06:42:15 -05:00
|
|
|
fn each_key(it: fn(&&uint) -> bool) {
|
2012-04-06 13:01:43 -05:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-05-12 21:09:09 -05:00
|
|
|
if self.v.get_elt(idx) != none && !it(idx) { ret; }
|
2012-01-09 09:24:53 -06:00
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 06:42:15 -05:00
|
|
|
fn each_value(it: fn(V) -> bool) {
|
|
|
|
self.each {|_i, v| it(v)}
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Cast the given smallintmap to a map::map"]
|
2012-03-19 04:45:29 -05:00
|
|
|
fn as_map<V: copy>(s: smallintmap<V>) -> map::map<uint, V> {
|
2012-01-09 09:24:53 -06:00
|
|
|
s as map::map::<uint, V>
|
|
|
|
}
|