Skip to content

Commit

Permalink
Fix issue #11216 - Replace std::hashmap::{each_key, each_value} with …
Browse files Browse the repository at this point in the history
…iterators.
  • Loading branch information
Dretch committed Dec 31, 2013
1 parent 250ca0e commit d435f4f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/librustc/middle/typeck/coherence.rs
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
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

5 comments on commit d435f4f

@bors
Copy link
Contributor

@bors bors commented on d435f4f Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at Dretch@d435f4f

@bors
Copy link
Contributor

@bors bors commented on d435f4f Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Dretch/rust/hashmap-key-value-iterators = d435f4f into auto

@bors
Copy link
Contributor

@bors bors commented on d435f4f Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dretch/rust/hashmap-key-value-iterators = d435f4f merged ok, testing candidate = 1b14a93

@bors
Copy link
Contributor

@bors bors commented on d435f4f Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on d435f4f Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 1b14a93

Please sign in to comment.