Skip to content

Commit

Permalink
Add methods for obtaining iterators over the keys and values of a Sma…
Browse files Browse the repository at this point in the history
…llIntMap
  • Loading branch information
nham committed Jul 25, 2014
1 parent 18f7b8f commit 0b339e0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/libcollections/smallintmap.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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)`.
///
Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -517,6 +538,32 @@ mod test_map {
assert_eq!(m.pop(&1), None);
}

#[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();
Expand Down

0 comments on commit 0b339e0

Please sign in to comment.