Skip to content

Commit 4b567dd

Browse files
thestingergraydon
authored andcommitted
add TreeSetIterator
1 parent 2b17e2f commit 4b567dd

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

src/libstd/treemap.rs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
4747
pure fn eq(&self, other: &TreeMap<K, V>) -> bool {
4848
if self.len() != other.len() {
4949
false
50-
} else unsafe { // unsafe used as a purity workaround
50+
} else {
5151
let mut x = self.iter();
5252
let mut y = other.iter();
53-
for self.len().times {
53+
for self.len().times unsafe { // unsafe used as a purity workaround
5454
// ICE: x.next() != y.next()
5555

5656
let (x1, x2) = x.next().unwrap();
@@ -149,7 +149,7 @@ impl <K: Ord, V> TreeMap<K, V> {
149149

150150
/// Get a lazy iterator over the key-value pairs in the map.
151151
/// Requires that it be frozen (immutable).
152-
fn iter(&self) -> TreeMapIterator/&self<K, V> {
152+
pure fn iter(&self) -> TreeMapIterator/&self<K, V> {
153153
TreeMapIterator{stack: ~[], node: &self.root}
154154
}
155155
}
@@ -226,6 +226,12 @@ impl <T: Ord> TreeSet<T> {
226226
/// present in the set.
227227
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
228228

229+
/// Get a lazy iterator over the values in the set.
230+
/// Requires that it be frozen (immutable).
231+
pure fn iter(&self) -> TreeSetIterator/&self<T> {
232+
TreeSetIterator{iter: self.map.iter()}
233+
}
234+
229235
/// Return true if the set has no elements in common with `other`.
230236
/// This is equivalent to checking for an empty intersection.
231237
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
@@ -246,25 +252,22 @@ impl <T: Ord> TreeSet<T> {
246252

247253
/// Visit the values (in-order) representing the difference
248254
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
249-
unsafe { // purity workaround
250-
let mut x = self.map.iter();
251-
let mut y = other.map.iter();
255+
let mut x = self.iter();
256+
let mut y = other.iter();
252257

258+
unsafe { // purity workaround
253259
let mut a = x.next();
254260
let mut b = y.next();
255261

256262
while a.is_some() {
257263
if b.is_none() {
258-
while a.is_some() {
259-
let (a1, _) = a.unwrap();
260-
if !f(a1) { return }
261-
a = x.next();
264+
return do a.while_some() |a1| {
265+
if f(a1) { x.next() } else { None }
262266
}
263-
return
264267
}
265268

266-
let (a1, _) = a.unwrap();
267-
let (b1, _) = b.unwrap();
269+
let a1 = a.unwrap();
270+
let b1 = b.unwrap();
268271

269272
if a1 < b1 {
270273
if !f(a1) { return }
@@ -280,25 +283,22 @@ impl <T: Ord> TreeSet<T> {
280283
/// Visit the values (in-order) representing the symmetric difference
281284
pure fn symmetric_difference(&self, other: &TreeSet<T>,
282285
f: fn(&T) -> bool) {
283-
unsafe { // purity workaround
284-
let mut x = self.map.iter();
285-
let mut y = other.map.iter();
286+
let mut x = self.iter();
287+
let mut y = other.iter();
286288

289+
unsafe { // purity workaround
287290
let mut a = x.next();
288291
let mut b = y.next();
289292

290293
while a.is_some() {
291294
if b.is_none() {
292-
while a.is_some() {
293-
let (a1, _) = a.unwrap();
294-
if !f(a1) { return }
295-
a = x.next();
295+
return do a.while_some() |a1| {
296+
if f(a1) { x.next() } else { None }
296297
}
297-
return
298298
}
299299

300-
let (a1, _) = a.unwrap();
301-
let (b1, _) = b.unwrap();
300+
let a1 = a.unwrap();
301+
let b1 = b.unwrap();
302302

303303
if a1 < b1 {
304304
if !f(a1) { return }
@@ -312,10 +312,8 @@ impl <T: Ord> TreeSet<T> {
312312
b = y.next();
313313
}
314314
}
315-
while b.is_some() {
316-
let (b1, _) = b.unwrap();
317-
if !f(b1) { return }
318-
b = y.next();
315+
do b.while_some |b1| {
316+
if f(b1) { y.next() } else { None }
319317
}
320318
}
321319
}
@@ -332,25 +330,22 @@ impl <T: Ord> TreeSet<T> {
332330

333331
/// Visit the values (in-order) representing the union
334332
pure fn union(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
335-
unsafe { // purity workaround
336-
let mut x = self.map.iter();
337-
let mut y = other.map.iter();
333+
let mut x = self.iter();
334+
let mut y = other.iter();
338335

336+
unsafe { // purity workaround
339337
let mut a = x.next();
340338
let mut b = y.next();
341339

342340
while a.is_some() {
343341
if b.is_none() {
344-
while a.is_some() {
345-
let (a1, _) = a.unwrap();
346-
if !f(a1) { return }
347-
a = x.next();
342+
return do a.while_some() |a1| {
343+
if f(a1) { x.next() } else { None }
348344
}
349-
return
350345
}
351346

352-
let (a1, _) = a.unwrap();
353-
let (b1, _) = b.unwrap();
347+
let a1 = a.unwrap();
348+
let b1 = b.unwrap();
354349

355350
if b1 < a1 {
356351
if !f(b1) { return }
@@ -367,6 +362,20 @@ impl <T: Ord> TreeSet<T> {
367362
}
368363
}
369364

365+
/// Lazy forward iterator over a set
366+
pub struct TreeSetIterator<T: Ord> {
367+
priv iter: TreeMapIterator<T, ()>
368+
}
369+
370+
impl <T: Ord> TreeSetIterator<T> {
371+
/// Advance the iterator to the next node (in order) and return a
372+
/// tuple with a reference to the value. If there are no more nodes,
373+
/// return `None`.
374+
fn next(&mut self) -> Option<&self/T> {
375+
self.iter.next().map_consume(|(x, _)| x)
376+
}
377+
}
378+
370379
// Nodes keep track of their level in the tree, starting at 1 in the
371380
// leaves and with a red child sharing the level of the parent.
372381
struct TreeNode<K: Ord, V> {

0 commit comments

Comments
 (0)