Skip to content

Commit 80dd31d

Browse files
ericktbrson
authored andcommitted
---
yaml --- r: 21801 b: refs/heads/snap-stage3 c: 8299f3a h: refs/heads/master i: 21799: 2f20e00 v: v3
1 parent 498ad56 commit 80dd31d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 35a418eb88fbb9f8868d87dca5e5858ddf5f9b9f
4+
refs/heads/snap-stage3: 8299f3a447ca6416b23c20871172d784a28762bd
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/send_map.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ trait SendMap<K:Eq Hash, V: Copy> {
2222
fn each_value_ref(&self, blk: fn(v: &V) -> bool);
2323
fn find(&const self, k: &K) -> Option<V>;
2424
fn get(&const self, k: &K) -> V;
25+
fn with_find_ref<T>(&const self, k: &K, blk: fn(Option<&V>) -> T) -> T;
26+
fn with_get_ref<T>(&const self, k: &K, blk: fn(v: &V) -> T) -> T;
2527
}
2628

2729
/// Open addressing with linear probing.
@@ -290,6 +292,27 @@ mod linear {
290292
}
291293
*/
292294

295+
fn with_find_ref<T>(&self, k: &K, blk: fn(Option<&V>) -> T) -> T {
296+
match self.bucket_for_key(self.buckets, k) {
297+
FoundEntry(idx) => {
298+
match self.buckets[idx] {
299+
Some(bkt) => blk(Some(&bkt.value)),
300+
None => fail ~"LinearMap::find: internal logic error"
301+
}
302+
}
303+
TableFull | FoundHole(_) => blk(None),
304+
}
305+
}
306+
307+
fn with_get_ref<T>(&self, k: &K, blk: fn(v: &V) -> T) -> T {
308+
do self.with_find_ref(k) |v| {
309+
match v {
310+
Some(v) => blk(v),
311+
None => fail fmt!("No entry found for key: %?", k),
312+
}
313+
}
314+
}
315+
293316
fn each_ref(&self, blk: fn(k: &K, v: &V) -> bool) {
294317
for vec::each(self.buckets) |slot| {
295318
let mut broke = false;
@@ -426,4 +449,12 @@ mod test {
426449
}
427450
assert observed == 0xFFFF_FFFF;
428451
}
452+
453+
#[test]
454+
fn with_find_ref() {
455+
let mut m = ~LinearMap();
456+
m.with_find_ref(&1, |v| assert v.is_none());
457+
m.insert(1, 2);
458+
m.with_find_ref(&1, |v| assert *v.get() == 2);
459+
}
429460
}

0 commit comments

Comments
 (0)