@@ -86,7 +86,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
8686 self . each ( |& ( _, v) | blk ( v) )
8787 }
8888
89- /// Visit all key-value pairs in order
89+ /// Iterate over the map and mutate the contained values
9090 fn mutate_values ( & mut self , it : & fn ( & uint , & ' self mut V ) -> bool ) {
9191 for uint:: range( 0 , self . v. len( ) ) |i| {
9292 match self . v [ i] {
@@ -96,7 +96,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
9696 }
9797 }
9898
99- /// Iterate over the map and mutate the contained values
99+ /// Return a reference to the value corresponding to the key
100100 fn find ( & self , key : & uint ) -> Option < & ' self V > {
101101 if * key < self . v . len ( ) {
102102 match self . v [ * key] {
@@ -140,6 +140,18 @@ pub impl<V> SmallIntMap<V> {
140140 fn get ( & self , key : & uint ) -> & ' self V {
141141 self . find ( key) . expect ( "key not present" )
142142 }
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+ }
143155}
144156
145157pub impl < V : Copy > SmallIntMap < V > {
@@ -160,6 +172,20 @@ pub impl<V:Copy> SmallIntMap<V> {
160172#[ cfg( test) ]
161173mod tests {
162174 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+ }
163189
164190 #[ test]
165191 fn test_len ( ) {
0 commit comments