Skip to content

Commit dad0f9f

Browse files
committed
port smallintmap over to dvec
also: add a non-operator-overloaded method for [] to work around rust-lang#2378
1 parent 6f84bfb commit dad0f9f

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/libstd/smallintmap.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ are O(highest integer key).
44
"];
55
import core::option;
66
import core::option::{some, none};
7+
import dvec::{dvec, extensions};
78

89
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
910
// to be. (#2347)
10-
type smallintmap<T: copy> = @{mut v: [mut option<T>]};
11+
type smallintmap<T: copy> = @{v: dvec<option<T>>};
1112

1213
#[doc = "Create a smallintmap"]
1314
fn mk<T: copy>() -> smallintmap<T> {
14-
let v: [mut option<T>] = [mut];
15-
ret @{mut v: v};
15+
ret @{v: dvec()};
1616
}
1717

1818
#[doc = "
1919
Add a value to the map. If the map already contains a value for
2020
the specified key then the original value is replaced.
2121
"]
22-
fn insert<T: copy>(m: smallintmap<T>, key: uint, val: T) {
23-
vec::grow_set::<option<T>>(m.v, key, none::<T>, some::<T>(val));
22+
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
23+
self.v.grow_set_elt(key, none, some(val));
2424
}
2525

2626
#[doc = "
2727
Get the value for the specified key. If the key does not exist
2828
in the map then returns none
2929
"]
30-
fn find<T: copy>(m: smallintmap<T>, key: uint) -> option<T> {
31-
if key < vec::len::<option<T>>(m.v) { ret m.v[key]; }
30+
fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
31+
if key < self.v.len() { ret self.v.get_elt(key); }
3232
ret none::<T>;
3333
}
3434

@@ -39,8 +39,8 @@ Get the value for the specified key
3939
4040
If the key does not exist in the map
4141
"]
42-
fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
43-
alt find(m, key) {
42+
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
43+
alt find(self, key) {
4444
none { #error("smallintmap::get(): key not present"); fail; }
4545
some(v) { ret v; }
4646
}
@@ -49,25 +49,15 @@ fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
4949
#[doc = "
5050
Returns true if the map contains a value for the specified key
5151
"]
52-
fn contains_key<T: copy>(m: smallintmap<T>, key: uint) -> bool {
53-
ret !option::is_none(find::<T>(m, key));
54-
}
55-
56-
// FIXME: Are these really useful?
57-
58-
fn truncate<T: copy>(m: smallintmap<T>, len: uint) {
59-
m.v = vec::to_mut(vec::slice::<option<T>>(m.v, 0u, len));
60-
}
61-
62-
fn max_key<T: copy>(m: smallintmap<T>) -> uint {
63-
ret vec::len::<option<T>>(m.v);
52+
fn contains_key<T: copy>(self: smallintmap<T>, key: uint) -> bool {
53+
ret !option::is_none(find(self, key));
6454
}
6555

6656
#[doc = "Implements the map::map interface for smallintmap"]
6757
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
6858
fn size() -> uint {
6959
let mut sz = 0u;
70-
for vec::each(self.v) {|item|
60+
for self.v.each {|item|
7161
alt item { some(_) { sz += 1u; } _ {} }
7262
}
7363
sz
@@ -78,9 +68,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
7868
ret !exists;
7969
}
8070
fn remove(&&key: uint) -> option<V> {
81-
if key >= vec::len(self.v) { ret none; }
82-
let old = self.v[key];
83-
self.v[key] = none;
71+
if key >= self.v.len() { ret none; }
72+
let old = self.v.get_elt(key);
73+
self.v.set_elt(key, none);
8474
old
8575
}
8676
fn contains_key(&&key: uint) -> bool {
@@ -92,9 +82,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
9282
fn each(it: fn(&&uint, V) -> bool) {
9383
let mut idx = 0u, l = self.v.len();
9484
while idx < l {
95-
alt self.v[idx] {
85+
alt self.v.get_elt(idx) {
9686
some(elt) {
97-
if !it(idx, copy elt) { break; }
87+
if !it(idx, elt) { break; }
9888
}
9989
none { }
10090
}
@@ -104,7 +94,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
10494
fn each_key(it: fn(&&uint) -> bool) {
10595
let mut idx = 0u, l = self.v.len();
10696
while idx < l {
107-
if self.v[idx] != none && !it(idx) { ret; }
97+
if self.v.get_elt(idx) != none && !it(idx) { ret; }
10898
idx += 1u;
10999
}
110100
}

src/test/bench/vec-append.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ fn main(args: [str]) {
2424
let args = if vec::len(args) <= 1u {["", "100000"]} else {args};
2525
let max = uint::from_str(args[1]).get();
2626
let start = std::time::precise_time_s();
27-
collect_raw(max);
27+
let raw_v = collect_raw(max);
2828
let mid = std::time::precise_time_s();
29-
collect_dvec(max);
29+
let dvec_v = collect_dvec(max);
3030
let end = std::time::precise_time_s();
3131

32+
// check each vector
33+
assert raw_v.len() == max;
34+
for raw_v.eachi { |i, v| assert i == v; }
35+
assert dvec_v.len() == max;
36+
for dvec_v.eachi { |i, v| assert i == v; }
37+
3238
let raw = mid - start;
3339
let dvec = end - mid;
3440

0 commit comments

Comments
 (0)