Skip to content

Fix issue #11216 - Replace std::hashmap::{each_key, each_value} with iterators #11242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/librustc/middle/typeck/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,9 @@ impl CoherenceChecker {

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) {
Expand Down
45 changes: 39 additions & 6 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use clone::Clone;
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;
Expand Down Expand Up @@ -525,14 +526,16 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
}
}

/// 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.
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -1015,6 +1026,28 @@ mod test_map {
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();
Expand Down