Skip to content

Commit a2f922f

Browse files
committed
implement ReverseIter for TreeMap and TreeSet
1 parent 3e0a28c commit a2f922f

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/libstd/treemap.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
use core::container::{Container, Mutable, Map, Set};
1616
use core::cmp::{Eq, Ord};
17+
use core::iter::{BaseIter, ReverseIter};
1718
use core::option::{Option, Some, None};
1819
use core::prelude::*;
1920

@@ -103,14 +104,21 @@ impl <K: Ord, V> TreeMap<K, V>: Ord {
103104
}
104105
}
105106

106-
impl <K: Ord, V> TreeMap<K, V>: iter::BaseIter<(&K, &V)> {
107+
impl <K: Ord, V> TreeMap<K, V>: BaseIter<(&K, &V)> {
107108
/// Visit all key-value pairs in order
108109
pure fn each(&self, f: fn(&(&self/K, &self/V)) -> bool) {
109110
each(&self.root, f)
110111
}
111112
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
112113
}
113114

115+
impl <K: Ord, V> TreeMap<K, V>: ReverseIter<(&K, &V)> {
116+
/// Visit all key-value pairs in reverse order
117+
pure fn each_reverse(&self, f: fn(&(&self/K, &self/V)) -> bool) {
118+
each_reverse(&self.root, f);
119+
}
120+
}
121+
114122
impl <K: Ord, V> TreeMap<K, V>: Container {
115123
/// Return the number of elements in the map
116124
pure fn len(&self) -> uint { self.length }
@@ -180,11 +188,6 @@ impl <K: Ord, V> TreeMap<K, V> {
180188
/// Create an empty TreeMap
181189
static pure fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
182190

183-
/// Visit all key-value pairs in reverse order
184-
pure fn each_reverse(&self, f: fn(&(&self/K, &self/V)) -> bool) {
185-
each_reverse(&self.root, f);
186-
}
187-
188191
/// Visit all keys in reverse order
189192
pure fn each_key_reverse(&self, f: fn(&K) -> bool) {
190193
self.each_reverse(|&(k, _)| f(k))
@@ -243,12 +246,19 @@ pub struct TreeSet<T> {
243246
priv map: TreeMap<T, ()>
244247
}
245248

246-
impl <T: Ord> TreeSet<T>: iter::BaseIter<T> {
249+
impl <T: Ord> TreeSet<T>: BaseIter<T> {
247250
/// Visit all values in order
248251
pure fn each(&self, f: fn(&T) -> bool) { self.map.each_key(f) }
249252
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
250253
}
251254

255+
impl <T: Ord> TreeSet<T>: ReverseIter<T> {
256+
/// Visit all values in reverse order
257+
pure fn each_reverse(&self, f: fn(&T) -> bool) {
258+
self.map.each_key_reverse(f)
259+
}
260+
}
261+
252262
impl <T: Eq Ord> TreeSet<T>: Eq {
253263
pure fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
254264
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
@@ -504,11 +514,6 @@ impl <T: Ord> TreeSet<T> {
504514
/// Create an empty TreeSet
505515
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
506516

507-
/// Visit all values in reverse order
508-
pure fn each_reverse(&self, f: fn(&T) -> bool) {
509-
self.map.each_key_reverse(f)
510-
}
511-
512517
/// Get a lazy iterator over the values in the set.
513518
/// Requires that it be frozen (immutable).
514519
pure fn iter(&self) -> TreeSetIterator/&self<T> {

0 commit comments

Comments
 (0)