container: remove internal iterators from Map
the maps are being migrated to external iterators
This commit is contained in:
parent
5242e8d2ba
commit
64ee9668a2
@ -56,38 +56,6 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
|
||||
self.find(key).is_some()
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) -> bool {
|
||||
for uint::range(0, self.v.len()) |i| {
|
||||
match self.v[i] {
|
||||
Some(ref elt) => if !it(&i, elt) { return false; },
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
fn each_key(&self, blk: &fn(key: &uint) -> bool) -> bool {
|
||||
self.each(|k, _| blk(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) -> bool {
|
||||
self.each(|_, v| blk(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) -> bool {
|
||||
for uint::range(0, self.v.len()) |i| {
|
||||
match self.v[i] {
|
||||
Some(ref mut elt) => if !it(&i, elt) { return false; },
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Return a reference to the value corresponding to the key
|
||||
fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
|
||||
if *key < self.v.len() {
|
||||
@ -156,6 +124,38 @@ impl<V> SmallIntMap<V> {
|
||||
/// Create an empty SmallIntMap
|
||||
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
pub fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) -> bool {
|
||||
for uint::range(0, self.v.len()) |i| {
|
||||
match self.v[i] {
|
||||
Some(ref elt) => if !it(&i, elt) { return false; },
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
pub fn each_key(&self, blk: &fn(key: &uint) -> bool) -> bool {
|
||||
self.each(|k, _| blk(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
pub fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) -> bool {
|
||||
self.each(|_, v| blk(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
pub fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) -> bool {
|
||||
for uint::range(0, self.v.len()) |i| {
|
||||
match self.v[i] {
|
||||
Some(ref mut elt) => if !it(&i, elt) { return false; },
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in reverse order
|
||||
pub fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) -> bool {
|
||||
for uint::range_rev(self.v.len(), 0) |i| {
|
||||
|
@ -107,26 +107,6 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
|
||||
self.find(key).is_some()
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
fn each<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) -> bool {
|
||||
each(&self.root, f)
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
fn each_key(&self, f: &fn(&K) -> bool) -> bool {
|
||||
self.each(|k, _| f(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool) -> bool {
|
||||
self.each(|_, v| f(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool) -> bool {
|
||||
mutate_values(&mut self.root, f)
|
||||
}
|
||||
|
||||
/// Return a reference to the value corresponding to the key
|
||||
fn find<'a>(&'a self, key: &K) -> Option<&'a V> {
|
||||
let mut current: &'a Option<~TreeNode<K, V>> = &self.root;
|
||||
@ -184,6 +164,26 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
|
||||
/// Create an empty TreeMap
|
||||
pub fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
pub fn each<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) -> bool {
|
||||
each(&self.root, f)
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
pub fn each_key(&self, f: &fn(&K) -> bool) -> bool {
|
||||
self.each(|k, _| f(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
pub fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool) -> bool {
|
||||
self.each(|_, v| f(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
pub fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool) -> bool {
|
||||
mutate_values(&mut self.root, f)
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in reverse order
|
||||
pub fn each_reverse<'a>(&'a self, f: &fn(&'a K, &'a V) -> bool) -> bool {
|
||||
each_reverse(&self.root, f)
|
||||
|
@ -34,18 +34,6 @@ pub trait Map<K, V>: Mutable {
|
||||
/// Return true if the map contains a value for the specified key
|
||||
fn contains_key(&self, key: &K) -> bool;
|
||||
|
||||
/// Visits all keys and values
|
||||
fn each<'a>(&'a self, f: &fn(&K, &'a V) -> bool) -> bool;
|
||||
|
||||
/// Visit all keys
|
||||
fn each_key(&self, f: &fn(&K) -> bool) -> bool;
|
||||
|
||||
/// Visit all values
|
||||
fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool) -> bool;
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool) -> bool;
|
||||
|
||||
/// Return a reference to the value corresponding to the key
|
||||
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
|
||||
|
||||
|
@ -307,34 +307,6 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs
|
||||
fn each<'a>(&'a self, blk: &fn(&K, &'a V) -> bool) -> bool {
|
||||
self.iter().advance(|(k, v)| blk(k, v))
|
||||
}
|
||||
|
||||
/// Visit all keys
|
||||
fn each_key(&self, blk: &fn(k: &K) -> bool) -> bool {
|
||||
self.iter().advance(|(k, _)| blk(k))
|
||||
}
|
||||
|
||||
/// Visit all values
|
||||
fn each_value<'a>(&'a self, blk: &fn(v: &'a V) -> bool) -> bool {
|
||||
self.iter().advance(|(_, v)| blk(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
fn mutate_values(&mut self, blk: &fn(&K, &mut V) -> bool) -> bool {
|
||||
for uint::range(0, self.buckets.len()) |i| {
|
||||
match self.buckets[i] {
|
||||
Some(Bucket{key: ref key, value: ref mut value, _}) => {
|
||||
if !blk(key, value) { return false; }
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Return a reference to the value corresponding to the key
|
||||
fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
|
||||
match self.bucket_for_key(k) {
|
||||
@ -516,6 +488,34 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs
|
||||
pub fn each<'a>(&'a self, blk: &fn(&K, &'a V) -> bool) -> bool {
|
||||
self.iter().advance(|(k, v)| blk(k, v))
|
||||
}
|
||||
|
||||
/// Visit all keys
|
||||
pub fn each_key(&self, blk: &fn(k: &K) -> bool) -> bool {
|
||||
self.iter().advance(|(k, _)| blk(k))
|
||||
}
|
||||
|
||||
/// Visit all values
|
||||
pub fn each_value<'a>(&'a self, blk: &fn(v: &'a V) -> bool) -> bool {
|
||||
self.iter().advance(|(_, v)| blk(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
pub fn mutate_values(&mut self, blk: &fn(&K, &mut V) -> bool) -> bool {
|
||||
for uint::range(0, self.buckets.len()) |i| {
|
||||
match self.buckets[i] {
|
||||
Some(Bucket{key: ref key, value: ref mut value, _}) => {
|
||||
if !blk(key, value) { return false; }
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// An iterator visiting all key-value pairs in arbitrary order.
|
||||
/// Iterator element type is (&'a K, &'a V).
|
||||
pub fn iter<'a>(&'a self) -> HashMapIterator<'a, K, V> {
|
||||
|
@ -58,30 +58,6 @@ impl<T> Map<uint, T> for TrieMap<T> {
|
||||
self.find(key).is_some()
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
#[inline]
|
||||
fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool {
|
||||
self.root.each(f)
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
#[inline]
|
||||
fn each_key(&self, f: &fn(&uint) -> bool) -> bool {
|
||||
self.each(|k, _| f(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
#[inline]
|
||||
fn each_value<'a>(&'a self, f: &fn(&'a T) -> bool) -> bool {
|
||||
self.each(|_, v| f(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
#[inline]
|
||||
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
|
||||
self.root.mutate_values(f)
|
||||
}
|
||||
|
||||
/// Return a reference to the value corresponding to the key
|
||||
#[inline]
|
||||
fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
|
||||
@ -158,6 +134,30 @@ impl<T> TrieMap<T> {
|
||||
self.root.each_reverse(f)
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs in order
|
||||
#[inline]
|
||||
pub fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool {
|
||||
self.root.each(f)
|
||||
}
|
||||
|
||||
/// Visit all keys in order
|
||||
#[inline]
|
||||
pub fn each_key(&self, f: &fn(&uint) -> bool) -> bool {
|
||||
self.each(|k, _| f(k))
|
||||
}
|
||||
|
||||
/// Visit all values in order
|
||||
#[inline]
|
||||
pub fn each_value<'a>(&'a self, f: &fn(&'a T) -> bool) -> bool {
|
||||
self.each(|_, v| f(v))
|
||||
}
|
||||
|
||||
/// Iterate over the map and mutate the contained values
|
||||
#[inline]
|
||||
pub fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
|
||||
self.root.mutate_values(f)
|
||||
}
|
||||
|
||||
/// Visit all keys in reverse order
|
||||
#[inline]
|
||||
pub fn each_key_reverse(&self, f: &fn(&uint) -> bool) -> bool {
|
||||
|
@ -61,29 +61,8 @@ impl<T> Mutable for cat<T> {
|
||||
}
|
||||
|
||||
impl<T> Map<int, T> for cat<T> {
|
||||
fn each<'a>(&'a self, f: &fn(&int, &'a T) -> bool) -> bool {
|
||||
let mut n = int::abs(self.meows);
|
||||
while n > 0 {
|
||||
if !f(&n, &self.name) { return false; }
|
||||
n -= 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
|
||||
|
||||
fn each_key(&self, f: &fn(v: &int) -> bool) -> bool {
|
||||
self.each(|k, _| f(k))
|
||||
}
|
||||
|
||||
fn each_value<'a>(&'a self, f: &fn(v: &'a T) -> bool) -> bool {
|
||||
self.each(|_, v| f(v))
|
||||
}
|
||||
|
||||
fn mutate_values(&mut self, _f: &fn(&int, &mut T) -> bool) -> bool {
|
||||
fail!("nope")
|
||||
}
|
||||
|
||||
fn insert(&mut self, k: int, _: T) -> bool {
|
||||
self.meows += k;
|
||||
true
|
||||
|
Loading…
Reference in New Issue
Block a user