Skip to content

Commit c58772c

Browse files
ericktbrson
authored andcommitted
---
yaml --- r: 28487 b: refs/heads/try c: 8299f3a h: refs/heads/master i: 28485: 81da9f2 28483: 3b107e7 28479: 13ef407 v: v3
1 parent ee331f7 commit c58772c

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
@@ -2,7 +2,7 @@
22
refs/heads/master: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
5-
refs/heads/try: 35a418eb88fbb9f8868d87dca5e5858ddf5f9b9f
5+
refs/heads/try: 8299f3a447ca6416b23c20871172d784a28762bd
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df

branches/try/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)