Skip to content

Commit e663799

Browse files
committed
---
yaml --- r: 125886 b: refs/heads/try c: 0b339e0 h: refs/heads/master v: v3
1 parent 25c946e commit e663799

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
@@ -2,7 +2,7 @@
22
refs/heads/master: f2fa55903e378368ed9173560f03a0ef16e371c2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 9fc8394d3bce22ab483f98842434c84c396212ae
5-
refs/heads/try: 18f7b8f20120897ea2f64f0435e0eca0c095a72b
5+
refs/heads/try: 0b339e09ab5e6b6eb47adef1d82944dc620ac7d3
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/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)