diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 2a5ca7d8dfa..36424d1bfaa 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -29,9 +29,6 @@ pub trait Map: Mutable { /// Return true if the map contains a value for the specified key pure fn contains_key(&self, key: &K) -> bool; - /// Visit all key-value pairs - pure fn each(&self, f: fn(&K, &V) -> bool); - /// Visit all keys pure fn each_key(&self, f: fn(&K) -> bool); diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index be785863f71..6bc9e4f88cf 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -262,19 +262,6 @@ pub mod linear { } } - /// Visit all key-value pairs - pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) { - for self.buckets.each |slot| { - let mut broke = false; - do slot.iter |bucket| { - if !blk(&bucket.key, &bucket.value) { - broke = true; // FIXME(#3064) just write "break;" - } - } - if broke { break; } - } - } - /// Visit all keys pure fn each_key(&self, blk: fn(k: &K) -> bool) { self.each(|k, _| blk(k)) @@ -335,6 +322,19 @@ pub mod linear { linear_map_with_capacity(INITIAL_CAPACITY) } + /// Visit all key-value pairs + pure fn each(&self, blk: fn(k: &K, v: &V) -> bool) { + for self.buckets.each |slot| { + let mut broke = false; + do slot.iter |bucket| { + if !blk(&bucket.key, &bucket.value) { + broke = true; // FIXME(#3064) just write "break;" + } + } + if broke { break; } + } + } + fn pop(&mut self, k: &K) -> Option { let hash = k.hash_keyed(self.k0, self.k1) as uint; self.pop_internal(hash, k) diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 9af596eb1f5..218964695e2 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -48,16 +48,6 @@ impl SmallIntMap: Map { self.find(key).is_some() } - /// Visit all key-value pairs - pure fn each(&self, it: fn(key: &uint, value: &V) -> bool) { - for uint::range(0, self.v.len()) |i| { - match self.v[i] { - Some(ref elt) => if !it(&i, elt) { break }, - None => () - } - } - } - /// Visit all keys pure fn each_key(&self, blk: fn(key: &uint) -> bool) { self.each(|k, _| blk(k)) @@ -109,6 +99,16 @@ pub impl SmallIntMap { /// Create an empty SmallIntMap static pure fn new() -> SmallIntMap { SmallIntMap{v: ~[]} } + /// Visit all key-value pairs + pure fn each(&self, it: fn(key: &uint, value: &V) -> bool) { + for uint::range(0, self.v.len()) |i| { + match self.v[i] { + Some(ref elt) => if !it(&i, elt) { break }, + None => () + } + } + } + pure fn get(&self, key: &uint) -> &self/V { self.find(key).expect("key not present") } diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index 1e90abcc03d..70226c7277d 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -125,9 +125,6 @@ impl TreeMap: Map { self.find(key).is_some() } - /// Visit all key-value pairs in order - pure fn each(&self, f: fn(&K, &V) -> bool) { each(&self.root, f) } - /// Visit all keys in order pure fn each_key(&self, f: fn(&K) -> bool) { self.each(|k, _| f(k)) } @@ -175,6 +172,9 @@ impl TreeMap { /// Create an empty TreeMap static pure fn new() -> TreeMap { TreeMap{root: None, length: 0} } + /// Visit all key-value pairs in order + pure fn each(&self, f: fn(&K, &V) -> bool) { each(&self.root, f) } + /// Visit all key-value pairs in reverse order pure fn each_reverse(&self, f: fn(&K, &V) -> bool) { each_reverse(&self.root, f); diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index b7c8322316f..a0230d02981 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -11,6 +11,7 @@ // xfail-fast use core::container::{Container, Mutable, Map}; +use core::iter::BaseIter; enum cat_type { tuxedo, tabby, tortoiseshell } @@ -48,6 +49,18 @@ impl cat { } } +impl cat: BaseIter<(int, &T)> { + pure fn each(&self, f: fn(&(int, &self/T)) -> bool) { + let mut n = int::abs(self.meows); + while n > 0 { + if !f(&(n, &self.name)) { break; } + n -= 1; + } + } + + pure fn size_hint(&self) -> Option { Some(self.len()) } +} + impl cat: Container { pure fn len(&self) -> uint { self.meows as uint } pure fn is_empty(&self) -> bool { self.meows == 0 } @@ -60,20 +73,12 @@ impl cat: Mutable { impl cat: Map { pure fn contains_key(&self, k: &int) -> bool { *k <= self.meows } - pure fn each(&self, f: fn(v: &int, v: &T) -> bool) { - let mut n = int::abs(self.meows); - while n > 0 { - if !f(&n, &self.name) { break; } - n -= 1; - } - } - pure fn each_key(&self, f: fn(v: &int) -> bool) { - for self.each |k, _| { if !f(k) { break; } loop;}; + for self.each |&(k, _)| { if !f(&k) { break; } loop;}; } pure fn each_value(&self, f: fn(v: &T) -> bool) { - for self.each |_, v| { if !f(v) { break; } loop;}; + for self.each |&(_, v)| { if !f(v) { break; } loop;}; } fn insert(&mut self, k: int, _: T) -> bool {