self.root.mutate_values(f);
}
- /// Return the value corresponding to the key in the map
+ /// Return a reference to the value corresponding to the key
#[inline(hint)]
fn find(&self, key: &uint) -> Option<&'self T> {
let mut node: &'self TrieNode<T> = &self.root;
fn each_value_reverse(&self, f: &fn(&T) -> bool) {
self.each_reverse(|&(_, v)| f(v))
}
+
+ /// Return a mutable reference to the value corresponding to the key
+ #[inline(always)]
+ fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> {
+ find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
+ }
}
pub struct TrieSet {
(n >> sh) & MASK
}
+fn find_mut<T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
+ unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
+ (match *child {
+ External(_, ref value) => Some(cast::transmute_mut(value)),
+ Internal(ref x) => find_mut(cast::transmute_mut(&x.children[chunk(key, idx)]),
+ key, idx + 1),
+ Nothing => None
+ }).map_consume(|x| cast::transmute_mut_region(x))
+ }
+}
+
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
idx: uint) -> bool {
let mut tmp = Nothing;
#[cfg(test)]
mod tests {
use super::*;
+ use core::option::{Some, None};
use uint;
+ #[test]
+ fn test_find_mut() {
+ let mut m = TrieMap::new();
+ fail_unless!(m.insert(1, 12));
+ fail_unless!(m.insert(2, 8));
+ fail_unless!(m.insert(5, 14));
+ let new = 100;
+ match m.find_mut(&5) {
+ None => fail!(), Some(x) => *x = new
+ }
+ assert_eq!(m.find(&5), Some(&new));
+ }
+
#[test]
fn test_step() {
let mut trie = TrieMap::new();