auto merge of #11242 : Dretch/rust/hashmap-key-value-iterators, r=pcwalton

This commit is contained in:
bors 2014-01-01 17:11:57 -08:00
commit 1b14a9393a
2 changed files with 41 additions and 9 deletions

View File

@ -426,10 +426,9 @@ pub fn add_trait_impl(&self,
pub fn check_implementation_coherence(&self) {
let trait_impls = self.crate_context.tcx.trait_impls.borrow();
trait_impls.get().each_key(|&trait_id| {
for &trait_id in trait_impls.get().keys() {
self.check_implementation_coherence_of(trait_id);
true
});
}
}
pub fn check_implementation_coherence_of(&self, trait_def_id: DefId) {

View File

@ -57,6 +57,7 @@
use cmp::{Eq, Equiv};
use default::Default;
use hash::Hash;
use iter;
use iter::{Iterator, FromIterator, Extendable};
use iter::{FilterMap, Chain, Repeat, Zip};
use num;
@ -525,14 +526,16 @@ pub fn find_equiv<'a, Q:Hash + Equiv<K>>(&'a self, k: &Q)
}
}
/// Visit all keys
pub fn each_key(&self, blk: |k: &K| -> bool) -> bool {
self.iter().advance(|(k, _)| blk(k))
/// An iterator visiting all keys in arbitrary order.
/// Iterator element type is &'a K.
pub fn keys<'a>(&'a self) -> HashMapKeyIterator<'a, K, V> {
self.iter().map(|(k, _v)| k)
}
/// Visit all values
pub fn each_value<'a>(&'a self, blk: |v: &'a V| -> bool) -> bool {
self.iter().advance(|(_, v)| blk(v))
/// An iterator visiting all values in arbitrary order.
/// Iterator element type is &'a V.
pub fn values<'a>(&'a self) -> HashMapValueIterator<'a, K, V> {
self.iter().map(|(_k, v)| v)
}
/// An iterator visiting all key-value pairs in arbitrary order.
@ -609,6 +612,14 @@ pub struct HashMapMoveIterator<K, V> {
priv iter: vec::MoveIterator<Option<Bucket<K, V>>>,
}
/// HashMap keys iterator
pub type HashMapKeyIterator<'a, K, V> =
iter::Map<'static, (&'a K, &'a V), &'a K, HashMapIterator<'a, K, V>>;
/// HashMap values iterator
pub type HashMapValueIterator<'a, K, V> =
iter::Map<'static, (&'a K, &'a V), &'a V, HashMapIterator<'a, K, V>>;
/// HashSet iterator
#[deriving(Clone)]
pub struct HashSetIterator<'a, K> {
@ -1015,6 +1026,28 @@ fn test_iterate() {
assert_eq!(observed, 0xFFFF_FFFF);
}
#[test]
fn test_keys() {
let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')];
let map = vec.move_iter().collect::<HashMap<int, char>>();
let keys = map.keys().map(|&k| k).collect::<~[int]>();
assert_eq!(keys.len(), 3);
assert!(keys.contains(&1));
assert!(keys.contains(&2));
assert!(keys.contains(&3));
}
#[test]
fn test_values() {
let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')];
let map = vec.move_iter().collect::<HashMap<int, char>>();
let values = map.values().map(|&v| v).collect::<~[char]>();
assert_eq!(values.len(), 3);
assert!(values.contains(&'a'));
assert!(values.contains(&'b'));
assert!(values.contains(&'c'));
}
#[test]
fn test_find() {
let mut m = HashMap::new();