From bfde2ba524559aeb5abcfce72d933d74d1226d67 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 12 May 2012 19:09:09 -0700 Subject: port smallintmap over to dvec also: add a non-operator-overloaded method for [] to work around #2378 --- src/libstd/smallintmap.rs | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src/libstd') 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 = @{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; } } -- cgit 1.4.1-3-g733a5