diff options
Diffstat (limited to 'src/libstd/treemap.rs')
| -rw-r--r-- | src/libstd/treemap.rs | 83 |
1 files changed, 81 insertions, 2 deletions
diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index 041ea855cb3..006455c44e4 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -105,26 +105,45 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> { } /// Visit all key-value pairs in order + #[cfg(stage0)] fn each(&self, f: &fn(&'self K, &'self V) -> bool) { each(&self.root, f) } + /// Visit all key-value pairs in order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn each<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) { + each(&self.root, f) + } + /// Visit all keys in order fn each_key(&self, f: &fn(&K) -> bool) { self.each(|k, _| f(k)) } /// Visit all values in order + #[cfg(stage0)] fn each_value(&self, f: &fn(&V) -> bool) { self.each(|_, v| f(v)) } + /// Visit all values in order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool) { + self.each(|_, v| f(v)) + } + /// Iterate over the map and mutate the contained values - fn mutate_values(&mut self, f: &fn(&'self K, &'self mut V) -> bool) { + fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool) { mutate_values(&mut self.root, f); } /// Return a reference to the value corresponding to the key + #[cfg(stage0)] fn find(&self, key: &K) -> Option<&'self V> { let mut current: &'self Option<~TreeNode<K, V>> = &self.root; loop { @@ -141,12 +160,42 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> { } } + /// Return a reference to the value corresponding to the key + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn find<'a>(&'a self, key: &K) -> Option<&'a V> { + let mut current: &'a Option<~TreeNode<K, V>> = &self.root; + loop { + match *current { + Some(ref r) => { + match key.cmp(&r.key) { + Less => current = &r.left, + Greater => current = &r.right, + Equal => return Some(&r.value) + } + } + None => return None + } + } + } + /// Return a mutable reference to the value corresponding to the key #[inline(always)] + #[cfg(stage0)] fn find_mut(&mut self, key: &K) -> Option<&'self mut V> { find_mut(&mut self.root, key) } + /// Return a mutable reference to the value corresponding to the key + #[inline(always)] + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> { + find_mut(&mut self.root, key) + } + /// 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. @@ -170,7 +219,16 @@ pub impl<K: TotalOrd, V> TreeMap<K, V> { fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} } /// Visit all key-value pairs in reverse order - fn each_reverse(&'self self, f: &fn(&'self K, &'self V) -> bool) { + #[cfg(stage0)] + fn each_reverse(&self, f: &fn(&'self K, &'self V) -> bool) { + each_reverse(&self.root, f); + } + + /// Visit all key-value pairs in reverse order + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn each_reverse<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) { each_reverse(&self.root, f); } @@ -186,9 +244,19 @@ pub impl<K: TotalOrd, V> TreeMap<K, V> { /// Get a lazy iterator over the key-value pairs in the map. /// Requires that it be frozen (immutable). + #[cfg(stage0)] fn iter(&self) -> TreeMapIterator<'self, K, V> { TreeMapIterator{stack: ~[], node: &self.root} } + + /// Get a lazy iterator over the key-value pairs in the map. + /// Requires that it be frozen (immutable). + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn iter<'a>(&'a self) -> TreeMapIterator<'a, K, V> { + TreeMapIterator{stack: ~[], node: &self.root} + } } /// Lazy forward iterator over a map @@ -490,9 +558,20 @@ pub impl <T: TotalOrd> TreeSet<T> { /// Get a lazy iterator over the values in the set. /// Requires that it be frozen (immutable). #[inline(always)] + #[cfg(stage0)] fn iter(&self) -> TreeSetIterator<'self, T> { TreeSetIterator{iter: self.map.iter()} } + + /// Get a lazy iterator over the values in the set. + /// Requires that it be frozen (immutable). + #[inline(always)] + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> { + TreeSetIterator{iter: self.map.iter()} + } } /// Lazy forward iterator over a set |
