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).
|
|
|
|
*/
|
2012-10-04 19:58:31 -07:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-09-02 18:05:19 -07:00
|
|
|
|
2012-09-04 11:23:53 -07:00
|
|
|
use core::option;
|
|
|
|
use core::option::{Some, None};
|
|
|
|
use dvec::DVec;
|
2012-09-10 15:38:28 -07:00
|
|
|
use 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-09-07 14:52:28 -07:00
|
|
|
type SmallIntMap_<T: Copy> = {v: DVec<Option<T>>};
|
2012-07-11 15:00:40 -07:00
|
|
|
|
2012-10-01 14:01:42 -07:00
|
|
|
pub enum SmallIntMap<T:Copy> {
|
2012-09-04 16:04:10 -07:00
|
|
|
SmallIntMap_(@SmallIntMap_<T>)
|
2012-07-11 15:00:40 -07:00
|
|
|
}
|
2011-06-03 16:14:29 -07:00
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Create a smallintmap
|
2012-10-01 14:01:42 -07:00
|
|
|
pub fn mk<T: Copy>() -> SmallIntMap<T> {
|
2012-08-27 14:22:25 -07:00
|
|
|
let v = DVec();
|
2012-09-10 17:50:48 -07:00
|
|
|
return SmallIntMap_(@{v: move 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-10-03 12:21:48 -07:00
|
|
|
pub fn insert<T: Copy>(self: SmallIntMap<T>, key: uint, val: T) {
|
2012-08-22 17:24:52 -07:00
|
|
|
//io::println(fmt!("%?", key));
|
2012-09-27 22:20:47 -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-10-01 14:01:42 -07:00
|
|
|
pub 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); }
|
2012-08-20 12:23:37 -07:00
|
|
|
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-10-01 14:01:42 -07:00
|
|
|
pub pure fn get<T: Copy>(self: SmallIntMap<T>, key: uint) -> T {
|
2012-08-06 12:34:08 -07:00
|
|
|
match find(self, key) {
|
2012-08-20 12:23:37 -07:00
|
|
|
None => {
|
2012-08-22 17:24:52 -07:00
|
|
|
error!("smallintmap::get(): key not present");
|
2012-08-03 19:59:04 -07:00
|
|
|
fail;
|
|
|
|
}
|
2012-09-28 00:22:18 -07:00
|
|
|
Some(move 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-10-01 14:01:42 -07:00
|
|
|
pub fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
|
2012-09-21 19:37:57 -07:00
|
|
|
return !find(self, key).is_none();
|
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-09-10 15:38:28 -07:00
|
|
|
impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
|
2012-08-27 16:26:35 -07:00
|
|
|
pure 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-09-19 16:55:01 -07:00
|
|
|
match *item {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(_) => sz += 1u,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => ()
|
|
|
|
}
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
2012-06-14 11:38:45 -07:00
|
|
|
#[inline(always)]
|
2012-10-03 12:21:48 -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-10-03 12:21:48 -07:00
|
|
|
fn remove(key: uint) -> bool {
|
2012-08-02 15:42:56 -07:00
|
|
|
if key >= self.v.len() {
|
2012-08-21 15:55:17 -07:00
|
|
|
return false;
|
2012-08-02 15:42:56 -07:00
|
|
|
}
|
2012-05-12 19:09:09 -07:00
|
|
|
let old = self.v.get_elt(key);
|
2012-08-20 12:23:37 -07:00
|
|
|
self.v.set_elt(key, None);
|
2012-08-21 15:55:17 -07:00
|
|
|
old.is_some()
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
2012-07-08 16:04:57 -07:00
|
|
|
fn clear() {
|
2012-09-12 10:38:17 -07:00
|
|
|
self.v.set(~[]);
|
2012-07-08 16:04:57 -07:00
|
|
|
}
|
2012-10-03 12:21:48 -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)
|
|
|
|
}
|
2012-10-03 12:21:48 -07:00
|
|
|
fn get(key: uint) -> V { get(self, key) }
|
|
|
|
pure fn find(key: uint) -> Option<V> { find(self, key) }
|
2012-01-09 16:24:53 +01:00
|
|
|
fn rehash() { fail }
|
2012-09-18 21:41:37 -07:00
|
|
|
|
2012-10-04 19:58:31 -07:00
|
|
|
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
2012-09-18 21:41:37 -07:00
|
|
|
self.each_ref(|k, v| it(*k, *v))
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
2012-10-03 12:21:48 -07:00
|
|
|
pure fn each_key(it: fn(key: uint) -> bool) {
|
2012-09-18 21:41:37 -07:00
|
|
|
self.each_ref(|k, _v| it(*k))
|
2012-08-02 15:42:56 -07:00
|
|
|
}
|
2012-10-03 12:21:48 -07:00
|
|
|
pure fn each_value(it: fn(value: V) -> bool) {
|
2012-09-18 21:41:37 -07:00
|
|
|
self.each_ref(|_k, v| it(*v))
|
2012-08-02 15:42:56 -07:00
|
|
|
}
|
2012-08-27 16:26:35 -07:00
|
|
|
pure 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-06 12:34:08 -07:00
|
|
|
match self.v.get_elt(idx) {
|
2012-09-28 00:22:18 -07:00
|
|
|
Some(ref elt) => if !it(&idx, elt) { break },
|
2012-08-20 12:23:37 -07:00
|
|
|
None => ()
|
2012-08-02 15:42:56 -07:00
|
|
|
}
|
2012-01-09 16:24:53 +01:00
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 16:26:35 -07:00
|
|
|
pure fn each_key_ref(blk: fn(key: &uint) -> bool) {
|
2012-08-02 15:42:56 -07:00
|
|
|
self.each_ref(|k, _v| blk(k))
|
|
|
|
}
|
2012-08-27 16:26:35 -07:00
|
|
|
pure fn each_value_ref(blk: fn(value: &V) -> bool) {
|
2012-08-02 15:42:56 -07:00
|
|
|
self.each_ref(|_k, v| blk(v))
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 14:52:28 -07:00
|
|
|
impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
|
2012-10-04 19:58:31 -07:00
|
|
|
pure fn index(key: uint) -> V {
|
2012-09-18 11:17:40 -07:00
|
|
|
unsafe {
|
2012-07-27 14:51:19 -07:00
|
|
|
get(self, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Cast the given smallintmap to a map::map
|
2012-10-01 14:01:42 -07:00
|
|
|
pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> map::Map<uint, V> {
|
2012-09-10 15:38:28 -07:00
|
|
|
s as map::Map::<uint, V>
|
2012-01-09 16:24:53 +01:00
|
|
|
}
|