@@ -24,6 +24,7 @@ pub mod linear {
24
24
use rand;
25
25
use uint;
26
26
use vec;
27
+ use util:: unreachable;
27
28
28
29
static INITIAL_CAPACITY : uint = 32 u; // 2^5
29
30
@@ -192,6 +193,14 @@ pub mod linear {
192
193
}
193
194
}
194
195
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
+
195
204
/// Inserts the key value pair into the buckets.
196
205
/// Assumes that there will be a bucket.
197
206
/// True if there was no previous entry with that key
@@ -338,7 +347,7 @@ pub mod linear {
338
347
}
339
348
}
340
349
341
- /// Return the value corresponding to the key in the map
350
+ /// Return a reference to the value corresponding to the key
342
351
fn find(&self, k: &K) -> Option<&'self V> {
343
352
match self.bucket_for_key(k) {
344
353
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
@@ -410,6 +419,17 @@ pub mod linear {
410
419
old_value
411
420
}
412
421
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
+
413
433
/// Return the value corresponding to the key in the map, or insert
414
434
/// and return the value if it doesn't exist.
415
435
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
@@ -655,6 +675,19 @@ pub mod linear {
655
675
fail_unless ! ( * m. get( & 2 ) == 4 ) ;
656
676
}
657
677
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
+
658
691
#[ test]
659
692
pub fn test_insert_overwrite ( ) {
660
693
let mut m = LinearMap :: new ( ) ;
0 commit comments