rust/src/libstd/smallintmap.rs

152 lines
3.9 KiB
Rust
Raw Normal View History

/*!
* A simple map based on a vector for small integer keys. Space requirements
* are O(highest integer key).
*/
2012-09-02 20:05:19 -05:00
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
2012-09-04 13:23:53 -05:00
use core::option;
use core::option::{Some, None};
use dvec::DVec;
use map::map;
// FIXME (#2347): Should not be @; there's a bug somewhere in rustc that
// requires this to be.
2012-09-04 18:04:10 -05:00
type SmallIntMap_<T: copy> = {v: DVec<Option<T>>};
2012-09-04 18:04:10 -05:00
enum SmallIntMap<T:copy> {
SmallIntMap_(@SmallIntMap_<T>)
}
/// Create a smallintmap
2012-09-04 18:04:10 -05:00
fn mk<T: copy>() -> SmallIntMap<T> {
2012-08-27 16:22:25 -05:00
let v = DVec();
2012-09-04 18:04:10 -05:00
return SmallIntMap_(@{v: v});
}
/**
* 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 13:38:45 -05:00
#[inline(always)]
2012-09-04 18:04:10 -05:00
fn insert<T: copy>(self: SmallIntMap<T>, key: uint, +val: T) {
2012-08-22 19:24:52 -05:00
//io::println(fmt!("%?", key));
2012-08-20 14:23:37 -05:00
self.v.grow_set_elt(key, None, Some(val));
}
/**
* Get the value for the specified key. If the key does not exist
* in the map then returns none
*/
2012-09-04 18:04:10 -05:00
pure fn find<T: copy>(self: SmallIntMap<T>, key: uint) -> Option<T> {
2012-08-01 19:30:05 -05:00
if key < self.v.len() { return self.v.get_elt(key); }
2012-08-20 14:23:37 -05:00
return None::<T>;
}
/**
* Get the value for the specified key
*
* # Failure
*
* If the key does not exist in the map
*/
2012-09-04 18:04:10 -05:00
pure fn get<T: copy>(self: SmallIntMap<T>, key: uint) -> T {
2012-08-06 14:34:08 -05:00
match find(self, key) {
2012-08-20 14:23:37 -05:00
None => {
2012-08-22 19:24:52 -05:00
error!("smallintmap::get(): key not present");
2012-08-03 21:59:04 -05:00
fail;
}
2012-08-20 14:23:37 -05:00
Some(v) => return v
}
}
/// Returns true if the map contains a value for the specified key
2012-09-04 18:04:10 -05:00
fn contains_key<T: copy>(self: SmallIntMap<T>, key: uint) -> bool {
2012-08-01 19:30:05 -05:00
return !option::is_none(find(self, key));
2011-07-27 07:19:39 -05:00
}
/// Implements the map::map interface for smallintmap
2012-09-04 18:04:10 -05:00
impl<V: copy> SmallIntMap<V>: map::map<uint, V> {
2012-08-27 18:26:35 -05:00
pure fn size() -> uint {
let mut sz = 0u;
2012-06-30 18:19:07 -05:00
for self.v.each |item| {
2012-08-06 14:34:08 -05:00
match item {
2012-08-20 14:23:37 -05:00
Some(_) => sz += 1u,
2012-08-03 21:59:04 -05:00
_ => ()
}
}
sz
}
2012-06-14 13:38:45 -05:00
#[inline(always)]
fn insert(+key: uint, +value: V) -> bool {
let exists = contains_key(self, key);
insert(self, key, value);
2012-08-01 19:30:05 -05:00
return !exists;
}
fn remove(+key: uint) -> bool {
if key >= self.v.len() {
return false;
}
let old = self.v.get_elt(key);
2012-08-20 14:23:37 -05:00
self.v.set_elt(key, None);
old.is_some()
}
fn clear() {
self.v.set(~[mut]);
}
fn contains_key(+key: uint) -> bool {
contains_key(self, key)
}
fn contains_key_ref(key: &uint) -> bool {
contains_key(self, *key)
}
fn get(+key: uint) -> V { get(self, key) }
2012-08-27 18:26:35 -05:00
pure fn find(+key: uint) -> Option<V> { find(self, key) }
fn rehash() { fail }
2012-08-27 18:26:35 -05:00
pure fn each(it: fn(+key: uint, +value: V) -> bool) {
let mut idx = 0u, l = self.v.len();
while idx < l {
2012-08-06 14:34:08 -05:00
match self.v.get_elt(idx) {
2012-08-20 14:23:37 -05:00
Some(elt) => if !it(idx, elt) { break },
None => ()
}
idx += 1u;
}
}
2012-08-27 18:26:35 -05:00
pure fn each_key(it: fn(+key: uint) -> bool) {
self.each(|k, _v| it(k))
}
2012-08-27 18:26:35 -05:00
pure fn each_value(it: fn(+value: V) -> bool) {
self.each(|_k, v| it(v))
}
2012-08-27 18:26:35 -05:00
pure fn each_ref(it: fn(key: &uint, value: &V) -> bool) {
let mut idx = 0u, l = self.v.len();
while idx < l {
2012-08-06 14:34:08 -05:00
match self.v.get_elt(idx) {
2012-08-20 14:23:37 -05:00
Some(elt) => if !it(&idx, &elt) { break },
None => ()
}
idx += 1u;
}
}
2012-08-27 18:26:35 -05:00
pure fn each_key_ref(blk: fn(key: &uint) -> bool) {
self.each_ref(|k, _v| blk(k))
}
2012-08-27 18:26:35 -05:00
pure fn each_value_ref(blk: fn(value: &V) -> bool) {
self.each_ref(|_k, v| blk(v))
}
}
2012-09-04 18:04:10 -05:00
impl<V: copy> SmallIntMap<V>: ops::Index<uint, V> {
pure fn index(&&key: uint) -> V {
unchecked {
get(self, key)
}
}
}
/// Cast the given smallintmap to a map::map
2012-09-04 18:04:10 -05:00
fn as_map<V: copy>(s: SmallIntMap<V>) -> map::map<uint, V> {
s as map::map::<uint, V>
}