diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2012-05-12 19:09:09 -0700 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2012-05-18 20:00:51 -0700 |
| commit | bfde2ba524559aeb5abcfce72d933d74d1226d67 (patch) | |
| tree | d6df18b140fc843db65576a547046591587e112d /src/libstd | |
| parent | 774ea145ec4525e63ba97502306036e773c19eca (diff) | |
| download | rust-bfde2ba524559aeb5abcfce72d933d74d1226d67.tar.gz rust-bfde2ba524559aeb5abcfce72d933d74d1226d67.zip | |
port smallintmap over to dvec
also: add a non-operator-overloaded method for [] to work around #2378
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/smallintmap.rs | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 8dbd6347876..ffe52c4131e 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<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; } } |
