implement ReverseIter for TreeMap and TreeSet
This commit is contained in:
parent
3e0a28c7da
commit
a2f922f282
1 changed files with 17 additions and 12 deletions
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
use core::container::{Container, Mutable, Map, Set};
|
||||
use core::cmp::{Eq, Ord};
|
||||
use core::iter::{BaseIter, ReverseIter};
|
||||
use core::option::{Option, Some, None};
|
||||
use core::prelude::*;
|
||||
|
||||
|
|
@ -103,7 +104,7 @@ impl <K: Ord, V> TreeMap<K, V>: Ord {
|
|||
}
|
||||
}
|
||||
|
||||
impl <K: Ord, V> TreeMap<K, V>: iter::BaseIter<(&K, &V)> {
|
||||
impl <K: Ord, V> TreeMap<K, V>: BaseIter<(&K, &V)> {
|
||||
/// Visit all key-value pairs in order
|
||||
pure fn each(&self, f: fn(&(&self/K, &self/V)) -> bool) {
|
||||
each(&self.root, f)
|
||||
|
|
@ -111,6 +112,13 @@ impl <K: Ord, V> TreeMap<K, V>: iter::BaseIter<(&K, &V)> {
|
|||
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
||||
}
|
||||
|
||||
impl <K: Ord, V> TreeMap<K, V>: ReverseIter<(&K, &V)> {
|
||||
/// Visit all key-value pairs in reverse order
|
||||
pure fn each_reverse(&self, f: fn(&(&self/K, &self/V)) -> bool) {
|
||||
each_reverse(&self.root, f);
|
||||
}
|
||||
}
|
||||
|
||||
impl <K: Ord, V> TreeMap<K, V>: Container {
|
||||
/// Return the number of elements in the map
|
||||
pure fn len(&self) -> uint { self.length }
|
||||
|
|
@ -180,11 +188,6 @@ 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 reverse order
|
||||
pure fn each_reverse(&self, f: fn(&(&self/K, &self/V)) -> bool) {
|
||||
each_reverse(&self.root, f);
|
||||
}
|
||||
|
||||
/// Visit all keys in reverse order
|
||||
pure fn each_key_reverse(&self, f: fn(&K) -> bool) {
|
||||
self.each_reverse(|&(k, _)| f(k))
|
||||
|
|
@ -243,12 +246,19 @@ pub struct TreeSet<T> {
|
|||
priv map: TreeMap<T, ()>
|
||||
}
|
||||
|
||||
impl <T: Ord> TreeSet<T>: iter::BaseIter<T> {
|
||||
impl <T: Ord> TreeSet<T>: BaseIter<T> {
|
||||
/// Visit all values in order
|
||||
pure fn each(&self, f: fn(&T) -> bool) { self.map.each_key(f) }
|
||||
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
||||
}
|
||||
|
||||
impl <T: Ord> TreeSet<T>: ReverseIter<T> {
|
||||
/// Visit all values in reverse order
|
||||
pure fn each_reverse(&self, f: fn(&T) -> bool) {
|
||||
self.map.each_key_reverse(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: Eq Ord> TreeSet<T>: Eq {
|
||||
pure fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
|
||||
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
|
||||
|
|
@ -504,11 +514,6 @@ impl <T: Ord> TreeSet<T> {
|
|||
/// Create an empty TreeSet
|
||||
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
|
||||
|
||||
/// Visit all values in reverse order
|
||||
pure fn each_reverse(&self, f: fn(&T) -> bool) {
|
||||
self.map.each_key_reverse(f)
|
||||
}
|
||||
|
||||
/// Get a lazy iterator over the values in the set.
|
||||
/// Requires that it be frozen (immutable).
|
||||
pure fn iter(&self) -> TreeSetIterator/&self<T> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue