rm each method from the Map trait

the map types should implement BaseIter instead
This commit is contained in:
Daniel Micay 2013-02-07 17:49:57 -05:00
parent d903231f1e
commit 83270d2d79
5 changed files with 41 additions and 39 deletions

View File

@ -29,9 +29,6 @@ pub trait Map<K, V>: 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);

View File

@ -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<V> {
let hash = k.hash_keyed(self.k0, self.k1) as uint;
self.pop_internal(hash, k)

View File

@ -48,16 +48,6 @@ impl<V> SmallIntMap<V>: Map<uint, V> {
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<V> SmallIntMap<V> {
/// Create an empty SmallIntMap
static pure fn new() -> SmallIntMap<V> { 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")
}

View File

@ -125,9 +125,6 @@ impl <K: Ord, V> TreeMap<K, V>: Map<K, V> {
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 <K: Ord, V> TreeMap<K, V> {
/// Create an empty TreeMap
static pure fn new() -> TreeMap<K, V> { 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);

View File

@ -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<T> cat<T> {
}
}
impl<T> cat<T>: 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<uint> { Some(self.len()) }
}
impl<T> cat<T>: Container {
pure fn len(&self) -> uint { self.meows as uint }
pure fn is_empty(&self) -> bool { self.meows == 0 }
@ -60,20 +73,12 @@ impl<T> cat<T>: Mutable {
impl<T> cat<T>: Map<int, T> {
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 {