Skip to content

Commit 12eb1b0

Browse files
ericktbrson
authored andcommitted
---
yaml --- r: 32571 b: refs/heads/dist-snap c: 8299f3a h: refs/heads/master i: 32569: f6c3c50 32567: e474e83 v: v3
1 parent d4c2e36 commit 12eb1b0

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
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 35a418eb88fbb9f8868d87dca5e5858ddf5f9b9f
10+
refs/heads/dist-snap: 8299f3a447ca6416b23c20871172d784a28762bd
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/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)