diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index b5d5b8a6149bd..6d12679c31354 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -449,6 +449,16 @@ impl HashMap { } } + /// Return a mutable reference to the value corresponding to the key + /// in the map, using equivalence + pub fn find_mut_equiv<'a, Q:Hash + Equiv>(&'a mut self, k: &Q) + -> Option<&'a mut V> { + match self.bucket_for_key_equiv(k) { + FoundEntry(idx) => Some(self.mut_value_for_bucket(idx)), + TableFull | FoundHole(_) => None, + } + } + /// Visit all keys pub fn each_key(&self, blk: |k: &K| -> bool) -> bool { self.iter().advance(|(k, _)| blk(k)) @@ -1003,6 +1013,24 @@ mod test_map { assert_eq!(m.find_equiv(&("qux")), None); } + #[test] + fn test_find_mut_equiv() { + let mut m = HashMap::new(); + + let (foo, bar, baz) = (1,2,3); + m.insert(~"foo", foo); + m.insert(~"bar", bar); + m.insert(~"baz", baz); + + let new = 4; + match m.find_mut_equiv(&("bar")) { + Some(value) => value = new, + None => fail!() + } + + assert_eq!(m.find_equiv(&("bar")), Some(&new)); + } + #[test] fn test_from_iter() { let xs = ~[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];