@@ -86,7 +86,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
86
86
self . each ( |& ( _, v) | blk ( v) )
87
87
}
88
88
89
- /// Visit all key-value pairs in order
89
+ /// Iterate over the map and mutate the contained values
90
90
fn mutate_values ( & mut self , it : & fn ( & uint , & ' self mut V ) -> bool ) {
91
91
for uint:: range( 0 , self . v. len( ) ) |i| {
92
92
match self . v [ i] {
@@ -96,7 +96,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
96
96
}
97
97
}
98
98
99
- /// Iterate over the map and mutate the contained values
99
+ /// Return a reference to the value corresponding to the key
100
100
fn find ( & self , key : & uint ) -> Option < & ' self V > {
101
101
if * key < self . v . len ( ) {
102
102
match self . v [ * key] {
@@ -140,6 +140,18 @@ pub impl<V> SmallIntMap<V> {
140
140
fn get ( & self , key : & uint ) -> & ' self V {
141
141
self . find ( key) . expect ( "key not present" )
142
142
}
143
+
144
+ /// Return a mutable reference to the value corresponding to the key
145
+ fn find_mut ( & mut self , key : & uint ) -> Option < & ' self mut V > {
146
+ if * key < self . v . len ( ) {
147
+ match self . v [ * key] {
148
+ Some ( ref mut value) => Some ( value) ,
149
+ None => None
150
+ }
151
+ } else {
152
+ None
153
+ }
154
+ }
143
155
}
144
156
145
157
pub impl < V : Copy > SmallIntMap < V > {
@@ -160,6 +172,20 @@ pub impl<V:Copy> SmallIntMap<V> {
160
172
#[ cfg( test) ]
161
173
mod tests {
162
174
use super :: SmallIntMap ;
175
+ use core:: prelude:: * ;
176
+
177
+ #[ test]
178
+ fn test_find_mut ( ) {
179
+ let mut m = SmallIntMap :: new ( ) ;
180
+ fail_unless ! ( m. insert( 1 , 12 ) ) ;
181
+ fail_unless ! ( m. insert( 2 , 8 ) ) ;
182
+ fail_unless ! ( m. insert( 5 , 14 ) ) ;
183
+ let new = 100 ;
184
+ match m. find_mut ( & 5 ) {
185
+ None => fail ! ( ) , Some ( x) => * x = new
186
+ }
187
+ assert_eq ! ( m. find( & 5 ) , Some ( & new) ) ;
188
+ }
163
189
164
190
#[ test]
165
191
fn test_len ( ) {
0 commit comments