diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 8dbd63478763..ffe52c4131e6 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -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 = @{mut v: [mut option]}; +type smallintmap = @{v: dvec>}; #[doc = "Create a smallintmap"] fn mk() -> smallintmap { - let v: [mut option] = [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(m: smallintmap, key: uint, val: T) { - vec::grow_set::>(m.v, key, none::, some::(val)); +fn insert(self: smallintmap, 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(m: smallintmap, key: uint) -> option { - if key < vec::len::>(m.v) { ret m.v[key]; } +fn find(self: smallintmap, key: uint) -> option { + if key < self.v.len() { ret self.v.get_elt(key); } ret none::; } @@ -39,8 +39,8 @@ Get the value for the specified key If the key does not exist in the map "] -fn get(m: smallintmap, key: uint) -> T { - alt find(m, key) { +fn get(self: smallintmap, 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(m: smallintmap, key: uint) -> T { #[doc = " Returns true if the map contains a value for the specified key "] -fn contains_key(m: smallintmap, key: uint) -> bool { - ret !option::is_none(find::(m, key)); -} - -// FIXME: Are these really useful? - -fn truncate(m: smallintmap, len: uint) { - m.v = vec::to_mut(vec::slice::>(m.v, 0u, len)); -} - -fn max_key(m: smallintmap) -> uint { - ret vec::len::>(m.v); +fn contains_key(self: smallintmap, key: uint) -> bool { + ret !option::is_none(find(self, key)); } #[doc = "Implements the map::map interface for smallintmap"] impl of map::map for smallintmap { 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 of map::map for smallintmap { ret !exists; } fn remove(&&key: uint) -> option { - 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 of map::map for smallintmap { 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 of map::map for smallintmap { 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; } } diff --git a/src/test/bench/vec-append.rs b/src/test/bench/vec-append.rs index a84c776f0cc6..8c5e5195cbc1 100644 --- a/src/test/bench/vec-append.rs +++ b/src/test/bench/vec-append.rs @@ -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;