Skip to content

add find_mut method to the Map trait #5528

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

Merged
merged 6 commits into from
Mar 26, 2013
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
hashmap: add find_mut method
  • Loading branch information
thestinger committed Mar 24, 2013
commit d77433386be3d5afa201cfd40dbaaa4d20dbbd80
35 changes: 34 additions & 1 deletion src/libcore/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod linear {
use rand;
use uint;
use vec;
use util::unreachable;

static INITIAL_CAPACITY: uint = 32u; // 2^5

Expand Down Expand Up @@ -192,6 +193,14 @@ pub mod linear {
}
}

#[inline(always)]
fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V {
match self.buckets[idx] {
Some(ref mut bkt) => &mut bkt.value,
None => unreachable()
}
}

/// Inserts the key value pair into the buckets.
/// Assumes that there will be a bucket.
/// True if there was no previous entry with that key
Expand Down Expand Up @@ -338,7 +347,7 @@ pub mod linear {
}
}

/// Return the value corresponding to the key in the map
/// Return a reference to the value corresponding to the key
fn find(&self, k: &K) -> Option<&'self V> {
match self.bucket_for_key(k) {
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
Expand Down Expand Up @@ -410,6 +419,17 @@ pub mod linear {
old_value
}

/// Return a mutable reference to the value corresponding to the key
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
let idx = match self.bucket_for_key(k) {
FoundEntry(idx) => idx,
TableFull | FoundHole(_) => return None
};
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
}
}

/// Return the value corresponding to the key in the map, or insert
/// and return the value if it doesn't exist.
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
Expand Down Expand Up @@ -655,6 +675,19 @@ pub mod linear {
fail_unless!(*m.get(&2) == 4);
}

#[test]
fn test_find_mut() {
let mut m = LinearMap::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]
pub fn test_insert_overwrite() {
let mut m = LinearMap::new();
Expand Down