Skip to content

Commit d249ff3

Browse files
committed
---
yaml --- r: 125180 b: refs/heads/auto c: 0b339e0 h: refs/heads/master v: v3
1 parent d3f134e commit d249ff3

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 18f7b8f20120897ea2f64f0435e0eca0c095a72b
16+
refs/heads/auto: 0b339e09ab5e6b6eb47adef1d82944dc620ac7d3
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcollections/smallintmap.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::prelude::*;
1717

1818
use core::default::Default;
1919
use core::fmt;
20+
use core::iter;
2021
use core::iter::{Enumerate, FilterMap};
2122
use core::mem::replace;
2223

@@ -194,6 +195,18 @@ impl<V> SmallIntMap<V> {
194195
self.find(key).expect("key not present")
195196
}
196197

198+
/// An iterator visiting all keys in ascending order by the keys.
199+
/// Iterator element type is `uint`.
200+
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
201+
self.iter().map(|(k, _v)| k)
202+
}
203+
204+
/// An iterator visiting all values in ascending order by the keys.
205+
/// Iterator element type is `&'r V`.
206+
pub fn values<'r>(&'r self) -> Values<'r, V> {
207+
self.iter().map(|(_k, v)| v)
208+
}
209+
197210
/// An iterator visiting all key-value pairs in ascending order by the keys.
198211
/// Iterator element type is `(uint, &'r V)`.
199212
///
@@ -422,6 +435,14 @@ pub struct MutEntries<'a, T> {
422435
iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref)
423436
double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref)
424437

438+
/// Forward iterator over the keys of a map
439+
pub type Keys<'a, T> =
440+
iter::Map<'static, (uint, &'a T), uint, Entries<'a, T>>;
441+
442+
/// Forward iterator over the values of a map
443+
pub type Values<'a, T> =
444+
iter::Map<'static, (uint, &'a T), &'a T, Entries<'a, T>>;
445+
425446
#[cfg(test)]
426447
mod test_map {
427448
use std::prelude::*;
@@ -517,6 +538,32 @@ mod test_map {
517538
assert_eq!(m.pop(&1), None);
518539
}
519540

541+
#[test]
542+
fn test_keys() {
543+
let mut map = SmallIntMap::new();
544+
map.insert(1, 'a');
545+
map.insert(2, 'b');
546+
map.insert(3, 'c');
547+
let keys = map.keys().collect::<Vec<uint>>();
548+
assert_eq!(keys.len(), 3);
549+
assert!(keys.contains(&1));
550+
assert!(keys.contains(&2));
551+
assert!(keys.contains(&3));
552+
}
553+
554+
#[test]
555+
fn test_values() {
556+
let mut map = SmallIntMap::new();
557+
map.insert(1, 'a');
558+
map.insert(2, 'b');
559+
map.insert(3, 'c');
560+
let values = map.values().map(|&v| v).collect::<Vec<char>>();
561+
assert_eq!(values.len(), 3);
562+
assert!(values.contains(&'a'));
563+
assert!(values.contains(&'b'));
564+
assert!(values.contains(&'c'));
565+
}
566+
520567
#[test]
521568
fn test_iterator() {
522569
let mut m = SmallIntMap::new();

0 commit comments

Comments
 (0)