diff options
| author | nham <hamann.nick@gmail.com> | 2014-07-25 00:48:05 -0400 |
|---|---|---|
| committer | nham <hamann.nick@gmail.com> | 2014-07-25 00:48:05 -0400 |
| commit | 0b339e09ab5e6b6eb47adef1d82944dc620ac7d3 (patch) | |
| tree | 991790547141aa974f231828aec8cf176e5d1577 | |
| parent | 18f7b8f20120897ea2f64f0435e0eca0c095a72b (diff) | |
| download | rust-0b339e09ab5e6b6eb47adef1d82944dc620ac7d3.tar.gz rust-0b339e09ab5e6b6eb47adef1d82944dc620ac7d3.zip | |
Add methods for obtaining iterators over the keys and values of a SmallIntMap
| -rw-r--r-- | src/libcollections/smallintmap.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index 994a6d6c5f3..e1980e1549a 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -17,6 +17,7 @@ use core::prelude::*; use core::default::Default; use core::fmt; +use core::iter; use core::iter::{Enumerate, FilterMap}; use core::mem::replace; @@ -194,6 +195,18 @@ impl<V> SmallIntMap<V> { self.find(key).expect("key not present") } + /// An iterator visiting all keys in ascending order by the keys. + /// Iterator element type is `uint`. + pub fn keys<'r>(&'r self) -> Keys<'r, V> { + self.iter().map(|(k, _v)| k) + } + + /// An iterator visiting all values in ascending order by the keys. + /// Iterator element type is `&'r V`. + pub fn values<'r>(&'r self) -> Values<'r, V> { + self.iter().map(|(_k, v)| v) + } + /// An iterator visiting all key-value pairs in ascending order by the keys. /// Iterator element type is `(uint, &'r V)`. /// @@ -422,6 +435,14 @@ pub struct MutEntries<'a, T> { iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref) double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref) +/// Forward iterator over the keys of a map +pub type Keys<'a, T> = + iter::Map<'static, (uint, &'a T), uint, Entries<'a, T>>; + +/// Forward iterator over the values of a map +pub type Values<'a, T> = + iter::Map<'static, (uint, &'a T), &'a T, Entries<'a, T>>; + #[cfg(test)] mod test_map { use std::prelude::*; @@ -518,6 +539,32 @@ mod test_map { } #[test] + fn test_keys() { + let mut map = SmallIntMap::new(); + map.insert(1, 'a'); + map.insert(2, 'b'); + map.insert(3, 'c'); + let keys = map.keys().collect::<Vec<uint>>(); + assert_eq!(keys.len(), 3); + assert!(keys.contains(&1)); + assert!(keys.contains(&2)); + assert!(keys.contains(&3)); + } + + #[test] + fn test_values() { + let mut map = SmallIntMap::new(); + map.insert(1, 'a'); + map.insert(2, 'b'); + map.insert(3, 'c'); + let values = map.values().map(|&v| v).collect::<Vec<char>>(); + assert_eq!(values.len(), 3); + assert!(values.contains(&'a')); + assert!(values.contains(&'b')); + assert!(values.contains(&'c')); + } + + #[test] fn test_iterator() { let mut m = SmallIntMap::new(); |
