@@ -90,7 +90,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
90
90
self . root . mutate_values ( f) ;
91
91
}
92
92
93
- /// Return the value corresponding to the key in the map
93
+ /// Return a reference to the value corresponding to the key
94
94
#[ inline( hint) ]
95
95
fn find ( & self , key : & uint ) -> Option < & ' self T > {
96
96
let mut node: & ' self TrieNode < T > = & self . root ;
@@ -153,6 +153,12 @@ pub impl<T> TrieMap<T> {
153
153
fn each_value_reverse ( & self , f : & fn ( & T ) -> bool ) {
154
154
self . each_reverse ( |& ( _, v) | f ( v) )
155
155
}
156
+
157
+ /// Return a mutable reference to the value corresponding to the key
158
+ #[ inline( always) ]
159
+ fn find_mut ( & mut self , key : & uint ) -> Option < & ' self mut T > {
160
+ find_mut ( & mut self . root . children [ chunk ( * key, 0 ) ] , * key, 1 )
161
+ }
156
162
}
157
163
158
164
pub struct TrieSet {
@@ -276,6 +282,17 @@ fn chunk(n: uint, idx: uint) -> uint {
276
282
( n >> sh) & MASK
277
283
}
278
284
285
+ fn find_mut < T > ( child : & ' r mut Child < T > , key : uint , idx : uint ) -> Option < & ' r mut T > {
286
+ unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
287
+ ( match * child {
288
+ External ( _, ref value) => Some ( cast:: transmute_mut ( value) ) ,
289
+ Internal ( ref x) => find_mut ( cast:: transmute_mut ( & x. children [ chunk ( key, idx) ] ) ,
290
+ key, idx + 1 ) ,
291
+ Nothing => None
292
+ } ) . map_consume ( |x| cast:: transmute_mut_region ( x) )
293
+ }
294
+ }
295
+
279
296
fn insert < T > ( count : & mut uint , child : & mut Child < T > , key : uint , value : T ,
280
297
idx : uint ) -> bool {
281
298
let mut tmp = Nothing ;
@@ -357,8 +374,22 @@ pub fn check_integrity<T>(trie: &TrieNode<T>) {
357
374
#[ cfg( test) ]
358
375
mod tests {
359
376
use super :: * ;
377
+ use core:: option:: { Some , None } ;
360
378
use uint;
361
379
380
+ #[ test]
381
+ fn test_find_mut ( ) {
382
+ let mut m = TrieMap :: new ( ) ;
383
+ fail_unless ! ( m. insert( 1 , 12 ) ) ;
384
+ fail_unless ! ( m. insert( 2 , 8 ) ) ;
385
+ fail_unless ! ( m. insert( 5 , 14 ) ) ;
386
+ let new = 100 ;
387
+ match m. find_mut ( & 5 ) {
388
+ None => fail ! ( ) , Some ( x) => * x = new
389
+ }
390
+ assert_eq ! ( m. find( & 5 ) , Some ( & new) ) ;
391
+ }
392
+
362
393
#[ test]
363
394
fn test_step ( ) {
364
395
let mut trie = TrieMap :: new ( ) ;
0 commit comments