port smallintmap over to dvec

also: add a non-operator-overloaded method for [] to work around #2378
This commit is contained in:
Niko Matsakis 2012-05-12 19:09:09 -07:00
parent 774ea145ec
commit bfde2ba524
2 changed files with 26 additions and 30 deletions

View File

@ -4,31 +4,31 @@ are O(highest integer key).
"];
import core::option;
import core::option::{some, none};
import dvec::{dvec, extensions};
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
// to be. (#2347)
type smallintmap<T: copy> = @{mut v: [mut option<T>]};
type smallintmap<T: copy> = @{v: dvec<option<T>>};
#[doc = "Create a smallintmap"]
fn mk<T: copy>() -> smallintmap<T> {
let v: [mut option<T>] = [mut];
ret @{mut v: v};
ret @{v: dvec()};
}
#[doc = "
Add a value to the map. If the map already contains a value for
the specified key then the original value is replaced.
"]
fn insert<T: copy>(m: smallintmap<T>, key: uint, val: T) {
vec::grow_set::<option<T>>(m.v, key, none::<T>, some::<T>(val));
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
self.v.grow_set_elt(key, none, some(val));
}
#[doc = "
Get the value for the specified key. If the key does not exist
in the map then returns none
"]
fn find<T: copy>(m: smallintmap<T>, key: uint) -> option<T> {
if key < vec::len::<option<T>>(m.v) { ret m.v[key]; }
fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
if key < self.v.len() { ret self.v.get_elt(key); }
ret none::<T>;
}
@ -39,8 +39,8 @@ Get the value for the specified key
If the key does not exist in the map
"]
fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
alt find(m, key) {
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
alt find(self, key) {
none { #error("smallintmap::get(): key not present"); fail; }
some(v) { ret v; }
}
@ -49,25 +49,15 @@ fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
#[doc = "
Returns true if the map contains a value for the specified key
"]
fn contains_key<T: copy>(m: smallintmap<T>, key: uint) -> bool {
ret !option::is_none(find::<T>(m, key));
}
// FIXME: Are these really useful?
fn truncate<T: copy>(m: smallintmap<T>, len: uint) {
m.v = vec::to_mut(vec::slice::<option<T>>(m.v, 0u, len));
}
fn max_key<T: copy>(m: smallintmap<T>) -> uint {
ret vec::len::<option<T>>(m.v);
fn contains_key<T: copy>(self: smallintmap<T>, key: uint) -> bool {
ret !option::is_none(find(self, key));
}
#[doc = "Implements the map::map interface for smallintmap"]
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
fn size() -> uint {
let mut sz = 0u;
for vec::each(self.v) {|item|
for self.v.each {|item|
alt item { some(_) { sz += 1u; } _ {} }
}
sz
@ -78,9 +68,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
ret !exists;
}
fn remove(&&key: uint) -> option<V> {
if key >= vec::len(self.v) { ret none; }
let old = self.v[key];
self.v[key] = none;
if key >= self.v.len() { ret none; }
let old = self.v.get_elt(key);
self.v.set_elt(key, none);
old
}
fn contains_key(&&key: uint) -> bool {
@ -92,9 +82,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
fn each(it: fn(&&uint, V) -> bool) {
let mut idx = 0u, l = self.v.len();
while idx < l {
alt self.v[idx] {
alt self.v.get_elt(idx) {
some(elt) {
if !it(idx, copy elt) { break; }
if !it(idx, elt) { break; }
}
none { }
}
@ -104,7 +94,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
fn each_key(it: fn(&&uint) -> bool) {
let mut idx = 0u, l = self.v.len();
while idx < l {
if self.v[idx] != none && !it(idx) { ret; }
if self.v.get_elt(idx) != none && !it(idx) { ret; }
idx += 1u;
}
}

View File

@ -24,11 +24,17 @@ fn main(args: [str]) {
let args = if vec::len(args) <= 1u {["", "100000"]} else {args};
let max = uint::from_str(args[1]).get();
let start = std::time::precise_time_s();
collect_raw(max);
let raw_v = collect_raw(max);
let mid = std::time::precise_time_s();
collect_dvec(max);
let dvec_v = collect_dvec(max);
let end = std::time::precise_time_s();
// check each vector
assert raw_v.len() == max;
for raw_v.eachi { |i, v| assert i == v; }
assert dvec_v.len() == max;
for dvec_v.eachi { |i, v| assert i == v; }
let raw = mid - start;
let dvec = end - mid;