diff options
Diffstat (limited to 'src/libstd/smallintmap.rs')
| -rw-r--r-- | src/libstd/smallintmap.rs | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 811cd710a62..d50804ba47b 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -51,6 +51,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> { } /// Visit all key-value pairs in order + #[cfg(stage0)] fn each(&self, it: &fn(&uint, &'self V) -> bool) { for uint::range(0, self.v.len()) |i| { match self.v[i] { @@ -60,18 +61,40 @@ impl<V> Map<uint, V> for SmallIntMap<V> { } } + /// Visit all key-value pairs in order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) { + for uint::range(0, self.v.len()) |i| { + match self.v[i] { + Some(ref elt) => if !it(&i, elt) { break }, + None => () + } + } + } + /// Visit all keys in order fn each_key(&self, blk: &fn(key: &uint) -> bool) { self.each(|k, _| blk(k)) } /// Visit all values in order + #[cfg(stage0)] fn each_value(&self, blk: &fn(value: &V) -> bool) { self.each(|_, v| blk(v)) } + /// Visit all values in order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) { + self.each(|_, v| blk(v)) + } + /// Iterate over the map and mutate the contained values - fn mutate_values(&mut self, it: &fn(&uint, &'self mut V) -> bool) { + 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 }, @@ -81,6 +104,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> { } /// Return a reference to the value corresponding to the key + #[cfg(stage0)] fn find(&self, key: &uint) -> Option<&'self V> { if *key < self.v.len() { match self.v[*key] { @@ -92,7 +116,23 @@ impl<V> Map<uint, V> for SmallIntMap<V> { } } + /// Return a reference to the value corresponding to the key + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn find<'a>(&'a self, key: &uint) -> Option<&'a V> { + if *key < self.v.len() { + match self.v[*key] { + Some(ref value) => Some(value), + None => None + } + } else { + None + } + } + /// Return a mutable reference to the value corresponding to the key + #[cfg(stage0)] fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> { if *key < self.v.len() { match self.v[*key] { @@ -104,6 +144,21 @@ impl<V> Map<uint, V> for SmallIntMap<V> { } } + /// Return a mutable reference to the value corresponding to the key + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> { + if *key < self.v.len() { + match self.v[*key] { + Some(ref mut value) => Some(value), + None => None + } + } else { + None + } + } + /// Insert a key-value pair into the map. An existing value for a /// key is replaced by the new value. Return true if the key did /// not already exist in the map. @@ -134,6 +189,7 @@ pub impl<V> SmallIntMap<V> { fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} } /// Visit all key-value pairs in reverse order + #[cfg(stage0)] fn each_reverse(&self, it: &fn(uint, &'self V) -> bool) { for uint::range_rev(self.v.len(), 0) |i| { match self.v[i - 1] { @@ -143,9 +199,30 @@ pub impl<V> SmallIntMap<V> { } } + /// Visit all key-value pairs in reverse order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + 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] { + Some(ref elt) => if !it(i - 1, elt) { break }, + None => () + } + } + } + + #[cfg(stage0)] fn get(&self, key: &uint) -> &'self V { self.find(key).expect("key not present") } + + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn get<'a>(&'a self, key: &uint) -> &'a V { + self.find(key).expect("key not present") + } } pub impl<V:Copy> SmallIntMap<V> { |
