2012-07-04 22:53:12 +01:00
|
|
|
/*!
|
|
|
|
* A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
* are O(highest integer key).
|
|
|
|
*/
|
2011-12-13 16:25:51 -08:00
|
|
|
import core::option;
|
|
|
|
import core::option::{some, none};
|
2012-05-12 19:09:09 -07:00
|
|
|
import dvec::{dvec, extensions};
|
2012-07-11 15:00:40 -07:00
|
|
|
import map::map;
|
2011-06-03 16:14:29 -07:00
|
|
|
|
2012-06-21 16:44:10 -07:00
|
|
|
// FIXME (#2347): Should not be @; there's a bug somewhere in rustc that
|
|
|
|
// requires this to be.
|
2012-07-11 15:00:40 -07:00
|
|
|
type smallintmap_<T: copy> = {v: dvec<option<T>>};
|
|
|
|
|
|
|
|
enum smallintmap<T:copy> {
|
|
|
|
smallintmap_(@smallintmap_<T>)
|
|
|
|
}
|
2011-06-03 16:14:29 -07:00
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Create a smallintmap
|
2012-03-19 10:45:29 +01:00
|
|
|
fn mk<T: copy>() -> smallintmap<T> {
|
2012-07-25 17:29:34 -07:00
|
|
|
let v = dvec();
|
|
|
|
ret smallintmap_(@{v: v});
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01: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-06-14 11:38:45 -07:00
|
|
|
#[inline(always)]
|
2012-05-12 19:09:09 -07:00
|
|
|
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
|
2012-07-25 18:00:29 -07:00
|
|
|
//io::println(#fmt("%?", key));
|
2012-05-12 19:09:09 -07:00
|
|
|
self.v.grow_set_elt(key, none, some(val));
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/**
|
|
|
|
* Get the value for the specified key. If the key does not exist
|
|
|
|
* in the map then returns none
|
|
|
|
*/
|
2012-05-12 19:09:09 -07: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 10:56:57 -07:00
|
|
|
ret none::<T>;
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/**
|
|
|
|
* Get the value for the specified key
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* If the key does not exist in the map
|
|
|
|
*/
|
2012-05-12 19:09:09 -07:00
|
|
|
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
|
|
|
|
alt find(self, key) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { #error("smallintmap::get(): key not present"); fail; }
|
2011-11-18 12:39:20 +01:00
|
|
|
some(v) { ret v; }
|
2011-06-03 16:14:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Returns true if the map contains a value for the specified key
|
2012-05-12 19:09:09 -07:00
|
|
|
fn contains_key<T: copy>(self: smallintmap<T>, key: uint) -> bool {
|
|
|
|
ret !option::is_none(find(self, key));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-19 18:02:37 -07:00
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Implements the map::map interface for smallintmap
|
2012-01-09 16:24:53 +01:00
|
|
|
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
|
|
|
|
fn size() -> uint {
|
2012-03-14 14:03:56 -04:00
|
|
|
let mut sz = 0u;
|
2012-06-30 16:19:07 -07:00
|
|
|
for self.v.each |item| {
|
2012-01-09 16:24:53 +01:00
|
|
|
alt item { some(_) { sz += 1u; } _ {} }
|
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
2012-06-14 11:38:45 -07:00
|
|
|
#[inline(always)]
|
2012-06-08 14:35:11 -07:00
|
|
|
fn insert(+key: uint, +value: V) -> bool {
|
2012-01-09 16:24:53 +01:00
|
|
|
let exists = contains_key(self, key);
|
|
|
|
insert(self, key, value);
|
|
|
|
ret !exists;
|
|
|
|
}
|
2012-01-31 17:05:20 -08:00
|
|
|
fn remove(&&key: uint) -> option<V> {
|
2012-05-12 19:09:09 -07:00
|
|
|
if key >= self.v.len() { ret none; }
|
|
|
|
let old = self.v.get_elt(key);
|
|
|
|
self.v.set_elt(key, none);
|
2012-01-09 16:24:53 +01:00
|
|
|
old
|
|
|
|
}
|
2012-07-08 16:04:57 -07:00
|
|
|
fn clear() {
|
|
|
|
self.v.set(~[mut]);
|
|
|
|
}
|
2012-01-09 16:24:53 +01:00
|
|
|
fn contains_key(&&key: uint) -> bool {
|
|
|
|
contains_key(self, key)
|
|
|
|
}
|
|
|
|
fn get(&&key: uint) -> V { get(self, key) }
|
2012-06-27 16:34:53 -07:00
|
|
|
fn [](&&key: uint) -> V { get(self, key) }
|
2012-01-31 17:05:20 -08:00
|
|
|
fn find(&&key: uint) -> option<V> { find(self, key) }
|
2012-01-09 16:24:53 +01:00
|
|
|
fn rehash() { fail }
|
2012-04-23 13:42:15 +02:00
|
|
|
fn each(it: fn(&&uint, V) -> bool) {
|
2012-04-06 20:01:43 +02:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-05-12 19:09:09 -07:00
|
|
|
alt self.v.get_elt(idx) {
|
2012-01-09 16:24:53 +01:00
|
|
|
some(elt) {
|
2012-05-12 19:09:09 -07:00
|
|
|
if !it(idx, elt) { break; }
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
none { }
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 13:42:15 +02:00
|
|
|
fn each_key(it: fn(&&uint) -> bool) {
|
2012-04-06 20:01:43 +02:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-05-12 19:09:09 -07:00
|
|
|
if self.v.get_elt(idx) != none && !it(idx) { ret; }
|
2012-01-09 16:24:53 +01:00
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 13:42:15 +02:00
|
|
|
fn each_value(it: fn(V) -> bool) {
|
2012-06-30 16:19:07 -07:00
|
|
|
self.each(|_i, v| it(v));
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Cast the given smallintmap to a map::map
|
2012-03-19 10:45:29 +01:00
|
|
|
fn as_map<V: copy>(s: smallintmap<V>) -> map::map<uint, V> {
|
2012-01-09 16:24:53 +01:00
|
|
|
s as map::map::<uint, V>
|
|
|
|
}
|