Skip to content

Commit 8e354f1

Browse files
committed
---
yaml --- r: 139265 b: refs/heads/try2 c: d774333 h: refs/heads/master i: 139263: 8878be9 v: v3
1 parent 0605edb commit 8e354f1

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 794814945674d27a4fb68aedcea86c39dd5ccd4e
8+
refs/heads/try2: d77433386be3d5afa201cfd40dbaaa4d20dbbd80
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/hashmap.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod linear {
2424
use rand;
2525
use uint;
2626
use vec;
27+
use util::unreachable;
2728

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

@@ -192,6 +193,14 @@ pub mod linear {
192193
}
193194
}
194195
196+
#[inline(always)]
197+
fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V {
198+
match self.buckets[idx] {
199+
Some(ref mut bkt) => &mut bkt.value,
200+
None => unreachable()
201+
}
202+
}
203+
195204
/// Inserts the key value pair into the buckets.
196205
/// Assumes that there will be a bucket.
197206
/// True if there was no previous entry with that key
@@ -338,7 +347,7 @@ pub mod linear {
338347
}
339348
}
340349
341-
/// Return the value corresponding to the key in the map
350+
/// Return a reference to the value corresponding to the key
342351
fn find(&self, k: &K) -> Option<&'self V> {
343352
match self.bucket_for_key(k) {
344353
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
@@ -410,6 +419,17 @@ pub mod linear {
410419
old_value
411420
}
412421
422+
/// Return a mutable reference to the value corresponding to the key
423+
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
424+
let idx = match self.bucket_for_key(k) {
425+
FoundEntry(idx) => idx,
426+
TableFull | FoundHole(_) => return None
427+
};
428+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
429+
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
430+
}
431+
}
432+
413433
/// Return the value corresponding to the key in the map, or insert
414434
/// and return the value if it doesn't exist.
415435
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
@@ -655,6 +675,19 @@ pub mod linear {
655675
fail_unless!(*m.get(&2) == 4);
656676
}
657677

678+
#[test]
679+
fn test_find_mut() {
680+
let mut m = LinearMap::new();
681+
fail_unless!(m.insert(1, 12));
682+
fail_unless!(m.insert(2, 8));
683+
fail_unless!(m.insert(5, 14));
684+
let new = 100;
685+
match m.find_mut(&5) {
686+
None => fail!(), Some(x) => *x = new
687+
}
688+
assert_eq!(m.find(&5), Some(&new));
689+
}
690+
658691
#[test]
659692
pub fn test_insert_overwrite() {
660693
let mut m = LinearMap::new();

0 commit comments

Comments
 (0)