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();
|
2012-08-01 17:30:05 -07:00
|
|
|
return 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-30 16:01:07 -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-07-27 14:51:19 -07:00
|
|
|
pure fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
|
2012-08-01 17:30:05 -07:00
|
|
|
if key < self.v.len() { return self.v.get_elt(key); }
|
|
|
|
return 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-07-27 14:51:19 -07:00
|
|
|
pure fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
|
2012-05-12 19:09:09 -07:00
|
|
|
alt find(self, key) {
|
2012-07-30 16:01:07 -07:00
|
|
|
none { error!{"smallintmap::get(): key not present"}; fail; }
|
2012-08-01 17:30:05 -07:00
|
|
|
some(v) { return 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 {
|
2012-08-01 17:30:05 -07:00
|
|
|
return !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);
|
2012-08-01 17:30:05 -07:00
|
|
|
return !exists;
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
2012-08-02 15:42:56 -07:00
|
|
|
fn remove(+key: uint) -> option<V> {
|
|
|
|
if key >= self.v.len() {
|
|
|
|
return none;
|
|
|
|
}
|
2012-05-12 19:09:09 -07:00
|
|
|
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-08-02 15:42:56 -07:00
|
|
|
fn contains_key(+key: uint) -> bool {
|
2012-01-09 16:24:53 +01:00
|
|
|
contains_key(self, key)
|
|
|
|
}
|
2012-08-02 15:42:56 -07:00
|
|
|
fn contains_key_ref(key: &uint) -> bool {
|
|
|
|
contains_key(self, *key)
|
|
|
|
}
|
|
|
|
fn get(+key: uint) -> V { get(self, key) }
|
|
|
|
fn find(+key: uint) -> option<V> { find(self, key) }
|
2012-01-09 16:24:53 +01:00
|
|
|
fn rehash() { fail }
|
2012-08-02 15:42:56 -07:00
|
|
|
fn each(it: fn(+key: uint, +value: 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-08-02 15:42:56 -07:00
|
|
|
fn each_key(it: fn(+key: uint) -> bool) {
|
|
|
|
self.each(|k, _v| it(k))
|
|
|
|
}
|
|
|
|
fn each_value(it: fn(+value: V) -> bool) {
|
|
|
|
self.each(|_k, v| it(v))
|
|
|
|
}
|
|
|
|
fn each_ref(it: fn(key: &uint, value: &V) -> bool) {
|
2012-04-06 20:01:43 +02:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-08-02 15:42:56 -07:00
|
|
|
alt self.v.get_elt(idx) {
|
|
|
|
some(elt) {
|
|
|
|
if !it(&idx, &elt) { break; }
|
|
|
|
}
|
|
|
|
none { }
|
|
|
|
}
|
2012-01-09 16:24:53 +01:00
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 15:42:56 -07:00
|
|
|
fn each_key_ref(blk: fn(key: &uint) -> bool) {
|
|
|
|
self.each_ref(|k, _v| blk(k))
|
|
|
|
}
|
|
|
|
fn each_value_ref(blk: fn(value: &V) -> bool) {
|
|
|
|
self.each_ref(|_k, v| blk(v))
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:51:19 -07:00
|
|
|
impl extensions<V: copy> of ops::index<uint, V> for smallintmap<V> {
|
|
|
|
pure fn index(&&key: uint) -> V {
|
|
|
|
unchecked {
|
|
|
|
get(self, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
}
|