Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Created July 31, 2012 20:47
Show Gist options
  • Save nikomatsakis/3220381 to your computer and use it in GitHub Desktop.
Save nikomatsakis/3220381 to your computer and use it in GitHub Desktop.
diff --git a/src/libcore/send_map.rs b/src/libcore/send_map.rs
index 2c48507..4eec333 100644
--- a/src/libcore/send_map.rs
+++ b/src/libcore/send_map.rs
@@ -252,6 +252,22 @@ mod linear {
}
}
+ impl public_methods<K: copy, V: copy> for &const linear_map<K,V> {
+ fn each(f: fn(+k: K, +v: V) -> bool) {
+ for uint::range(0, self.buckets.len()) |i| {
+ let opt_bucket = self.buckets[i];
+ alt opt_bucket {
+ none => {}
+ some(bucket) {
+ if !f(bucket.key, bucket.value) {
+ ret;
+ }
+ }
+ }
+ }
+ }
+ }
+
impl public_methods<K,V: copy> for &const linear_map<K,V> {
fn find(k: &K) -> option<V> {
alt self.bucket_for_key(self.buckets, k) {
@@ -342,4 +358,18 @@ mod test {
assert m.get(&9) == 4;
assert m.get(&5) == 3;
}
+
+ #[test]
+ fn iterate() {
+ let mut m = ~linear::linear_map_with_capacity(uint_hash, uint_eq, 4);
+ for uint::range(0, 32) |i| {
+ assert m.insert(i, i*2);
+ }
+ let mut observed = 0;
+ for m.each |k, v| {
+ assert v == k*2;
+ observed |= (1 << k);
+ }
+ assert observed == 0xFFFF_FFFF;
+ }
}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment