From 3ce9dba6775c7e1dbfb510626c073a8f926b6880 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 May 2013 18:33:27 -0400 Subject: std: Use the new `for` protocol --- src/libstd/smallintmap.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'src/libstd/smallintmap.rs') diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index fc83a39cacf..afc1d0fe65f 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -51,6 +51,7 @@ impl Map for SmallIntMap { } /// Visit all key-value pairs in order + #[cfg(stage0)] fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) { for uint::range(0, self.v.len()) |i| { match self.v[i] { @@ -60,25 +61,62 @@ impl Map for SmallIntMap { } } + /// Visit all key-value pairs in order + #[cfg(not(stage0))] + fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) -> bool { + for uint::range(0, self.v.len()) |i| { + match self.v[i] { + Some(ref elt) => if !it(&i, elt) { return false; }, + None => () + } + } + return true; + } + /// Visit all keys in order + #[cfg(stage0)] fn each_key(&self, blk: &fn(key: &uint) -> bool) { self.each(|k, _| blk(k)) } + #[cfg(not(stage0))] + /// Visit all keys in order + fn each_key(&self, blk: &fn(key: &uint) -> bool) -> bool { + self.each(|k, _| blk(k)) + } /// Visit all values in order + #[cfg(stage0)] fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) { self.each(|_, v| blk(v)) } + /// Visit all values in order + #[cfg(not(stage0))] + fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) -> bool { + self.each(|_, v| blk(v)) + } + /// Iterate over the map and mutate the contained values + #[cfg(stage0)] fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) { for uint::range(0, self.v.len()) |i| { match self.v[i] { - Some(ref mut elt) => if !it(&i, elt) { break }, + Some(ref mut elt) => if !it(&i, elt) { return; }, None => () } } } + /// Iterate over the map and mutate the contained values + #[cfg(not(stage0))] + fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) -> bool { + for uint::range(0, self.v.len()) |i| { + match self.v[i] { + Some(ref mut elt) => if !it(&i, elt) { return false; }, + None => () + } + } + return true; + } /// Return a reference to the value corresponding to the key fn find<'a>(&'a self, key: &uint) -> Option<&'a V> { @@ -149,6 +187,7 @@ pub impl SmallIntMap { fn new() -> SmallIntMap { SmallIntMap{v: ~[]} } /// Visit all key-value pairs in reverse order + #[cfg(stage0)] fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) { for uint::range_rev(self.v.len(), 0) |i| { match self.v[i - 1] { @@ -158,6 +197,18 @@ pub impl SmallIntMap { } } + /// Visit all key-value pairs in reverse order + #[cfg(not(stage0))] + fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) -> bool { + for uint::range_rev(self.v.len(), 0) |i| { + match self.v[i - 1] { + Some(ref elt) => if !it(i - 1, elt) { return false; }, + None => () + } + } + return true; + } + fn get<'a>(&'a self, key: &uint) -> &'a V { self.find(key).expect("key not present") } -- cgit 1.4.1-3-g733a5