@@ -18,6 +18,7 @@ use core::default::Default;
18
18
use core:: mem:: zeroed;
19
19
use core:: mem;
20
20
use core:: uint;
21
+ use core:: iter;
21
22
use std:: hash:: { Writer , Hash } ;
22
23
23
24
use { Collection , Mutable , Map , MutableMap , Set , MutableSet } ;
@@ -196,6 +197,18 @@ impl<T> TrieMap<T> {
196
197
self . root . each_reverse ( f)
197
198
}
198
199
200
+ /// Get an iterator visiting all keys in ascending order by the keys.
201
+ /// Iterator element type is `uint`.
202
+ pub fn keys < ' r > ( & ' r self ) -> Keys < ' r , T > {
203
+ self . iter ( ) . map ( |( k, _v) | k)
204
+ }
205
+
206
+ /// Get an iterator visiting all values in ascending order by the keys.
207
+ /// Iterator element type is `&'r T`.
208
+ pub fn values < ' r > ( & ' r self ) -> Values < ' r , T > {
209
+ self . iter ( ) . map ( |( _k, v) | v)
210
+ }
211
+
199
212
/// Get an iterator over the key-value pairs in the map, ordered by keys.
200
213
///
201
214
/// # Example
@@ -783,6 +796,14 @@ pub struct MutEntries<'a, T> {
783
796
remaining_max : uint
784
797
}
785
798
799
+ /// Forward iterator over the keys of a map
800
+ pub type Keys < ' a , T > =
801
+ iter:: Map < ' static , ( uint , & ' a T ) , uint , Entries < ' a , T > > ;
802
+
803
+ /// Forward iterator over the values of a map
804
+ pub type Values < ' a , T > =
805
+ iter:: Map < ' static , ( uint , & ' a T ) , & ' a T , Entries < ' a , T > > ;
806
+
786
807
// FIXME #5846: see `addr!` above.
787
808
macro_rules! item { ( $i: item) => { $i} }
788
809
@@ -1070,6 +1091,28 @@ mod test_map {
1070
1091
}
1071
1092
}
1072
1093
1094
+ #[ test]
1095
+ fn test_keys ( ) {
1096
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
1097
+ let map = vec. move_iter ( ) . collect :: < TrieMap < char > > ( ) ;
1098
+ let keys = map. keys ( ) . collect :: < Vec < uint > > ( ) ;
1099
+ assert_eq ! ( keys. len( ) , 3 ) ;
1100
+ assert ! ( keys. contains( & 1 ) ) ;
1101
+ assert ! ( keys. contains( & 2 ) ) ;
1102
+ assert ! ( keys. contains( & 3 ) ) ;
1103
+ }
1104
+
1105
+ #[ test]
1106
+ fn test_values ( ) {
1107
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
1108
+ let map = vec. move_iter ( ) . collect :: < TrieMap < char > > ( ) ;
1109
+ let values = map. values ( ) . map ( |& v| v) . collect :: < Vec < char > > ( ) ;
1110
+ assert_eq ! ( values. len( ) , 3 ) ;
1111
+ assert ! ( values. contains( & 'a' ) ) ;
1112
+ assert ! ( values. contains( & 'b' ) ) ;
1113
+ assert ! ( values. contains( & 'c' ) ) ;
1114
+ }
1115
+
1073
1116
#[ test]
1074
1117
fn test_iteration ( ) {
1075
1118
let empty_map : TrieMap < uint > = TrieMap :: new ( ) ;
0 commit comments