Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the find_mut_equiv method to hashmap::HashMap #14968

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/libstd/collections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,18 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
}
}

/// Return a mutable reference to the value corresponding to the key in the map, using
/// equivalence.
pub fn find_equiv_mut<'a, Q: Hash<S> + Equiv<K>>(&'a mut self, k: &Q) -> Option<&'a mut V> {
match self.search_equiv(k) {
None => None,
Some(idx) => {
let (_, v_ref) = self.table.read_mut(&idx);
Some(v_ref)
}
}
}

/// An iterator visiting all keys in arbitrary order.
/// Iterator element type is &'a K.
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
Expand Down Expand Up @@ -2139,6 +2151,23 @@ mod test_map {
assert_eq!(m.find_equiv(&("qux")), None);
}

#[test]
fn test_find_equiv_mut() {
let mut m = HashMap::new();

let (foo, bar, baz) = (1,2,3);
m.insert("foo".to_string(), foo);
m.insert("bar".to_string(), bar);
m.insert("baz".to_string(), baz);

let new = 100;
match m.find_equiv_mut(&("bar")) {
None => fail!(),
Some(x) => *x = new
}
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)];
Expand Down