@@ -24,6 +24,7 @@ pub mod linear {
2424 use rand;
2525 use uint;
2626 use vec;
27+ use util:: unreachable;
2728
2829 static INITIAL_CAPACITY : uint = 32 u; // 2^5
2930
@@ -192,6 +193,14 @@ pub mod linear {
192193 }
193194 }
194195
196+ #[inline(always)]
197+ fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V {
198+ match self.buckets[idx] {
199+ Some(ref mut bkt) => &mut bkt.value,
200+ None => unreachable()
201+ }
202+ }
203+
195204 /// Inserts the key value pair into the buckets.
196205 /// Assumes that there will be a bucket.
197206 /// True if there was no previous entry with that key
@@ -338,7 +347,7 @@ pub mod linear {
338347 }
339348 }
340349
341- /// Return the value corresponding to the key in the map
350+ /// Return a reference to the value corresponding to the key
342351 fn find(&self, k: &K) -> Option<&'self V> {
343352 match self.bucket_for_key(k) {
344353 FoundEntry(idx) => Some(self.value_for_bucket(idx)),
@@ -410,6 +419,17 @@ pub mod linear {
410419 old_value
411420 }
412421
422+ /// Return a mutable reference to the value corresponding to the key
423+ fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
424+ let idx = match self.bucket_for_key(k) {
425+ FoundEntry(idx) => idx,
426+ TableFull | FoundHole(_) => return None
427+ };
428+ unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
429+ Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
430+ }
431+ }
432+
413433 /// Return the value corresponding to the key in the map, or insert
414434 /// and return the value if it doesn't exist.
415435 fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
@@ -655,6 +675,19 @@ pub mod linear {
655675 fail_unless ! ( * m. get( & 2 ) == 4 ) ;
656676 }
657677
678+ #[ test]
679+ fn test_find_mut ( ) {
680+ let mut m = LinearMap :: new ( ) ;
681+ fail_unless ! ( m. insert( 1 , 12 ) ) ;
682+ fail_unless ! ( m. insert( 2 , 8 ) ) ;
683+ fail_unless ! ( m. insert( 5 , 14 ) ) ;
684+ let new = 100 ;
685+ match m. find_mut ( & 5 ) {
686+ None => fail ! ( ) , Some ( x) => * x = new
687+ }
688+ assert_eq ! ( m. find( & 5 ) , Some ( & new) ) ;
689+ }
690+
658691 #[ test]
659692 pub fn test_insert_overwrite ( ) {
660693 let mut m = LinearMap :: new ( ) ;
0 commit comments