@@ -47,10 +47,10 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
47
47
pure fn eq ( & self , other : & TreeMap < K , V > ) -> bool {
48
48
if self . len ( ) != other. len ( ) {
49
49
false
50
- } else unsafe { // unsafe used as a purity workaround
50
+ } else {
51
51
let mut x = self . iter ( ) ;
52
52
let mut y = other. iter ( ) ;
53
- for self . len( ) . times {
53
+ for self . len( ) . times unsafe { // unsafe used as a purity workaround
54
54
// ICE: x.next() != y.next()
55
55
56
56
let ( x1, x2) = x. next( ) . unwrap( ) ;
@@ -149,7 +149,7 @@ impl <K: Ord, V> TreeMap<K, V> {
149
149
150
150
/// Get a lazy iterator over the key-value pairs in the map.
151
151
/// Requires that it be frozen (immutable).
152
- fn iter ( & self ) -> TreeMapIterator /& self <K , V > {
152
+ pure fn iter ( & self ) -> TreeMapIterator /& self <K , V > {
153
153
TreeMapIterator { stack: ~[ ] , node : & self . root}
154
154
}
155
155
}
@@ -226,6 +226,12 @@ impl <T: Ord> TreeSet<T> {
226
226
/// present in the set.
227
227
fn remove( & mut self , value: & T ) -> bool { self . map. remove( value) }
228
228
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
+
229
235
/// Return true if the set has no elements in common with `other`.
230
236
/// This is equivalent to checking for an empty intersection.
231
237
pure fn is_disjoint( & self , other: & TreeSet <T >) -> bool {
@@ -246,25 +252,22 @@ impl <T: Ord> TreeSet<T> {
246
252
247
253
/// Visit the values (in-order) representing the difference
248
254
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( ) ;
252
257
258
+ unsafe { // purity workaround
253
259
let mut a = x. next( ) ;
254
260
let mut b = y. next( ) ;
255
261
256
262
while a. is_some( ) {
257
263
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 }
262
266
}
263
- return
264
267
}
265
268
266
- let ( a1 , _ ) = a. unwrap ( ) ;
267
- let ( b1 , _ ) = b. unwrap ( ) ;
269
+ let a1 = a. unwrap( ) ;
270
+ let b1 = b. unwrap( ) ;
268
271
269
272
if a1 < b1 {
270
273
if !f( a1) { return }
@@ -280,25 +283,22 @@ impl <T: Ord> TreeSet<T> {
280
283
/// Visit the values (in-order) representing the symmetric difference
281
284
pure fn symmetric_difference( & self , other: & TreeSet <T >,
282
285
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( ) ;
286
288
289
+ unsafe { // purity workaround
287
290
let mut a = x. next( ) ;
288
291
let mut b = y. next( ) ;
289
292
290
293
while a. is_some( ) {
291
294
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 }
296
297
}
297
- return
298
298
}
299
299
300
- let ( a1 , _ ) = a. unwrap ( ) ;
301
- let ( b1 , _ ) = b. unwrap ( ) ;
300
+ let a1 = a. unwrap( ) ;
301
+ let b1 = b. unwrap( ) ;
302
302
303
303
if a1 < b1 {
304
304
if !f( a1) { return }
@@ -312,10 +312,8 @@ impl <T: Ord> TreeSet<T> {
312
312
b = y. next( ) ;
313
313
}
314
314
}
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 }
319
317
}
320
318
}
321
319
}
@@ -332,25 +330,22 @@ impl <T: Ord> TreeSet<T> {
332
330
333
331
/// Visit the values (in-order) representing the union
334
332
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( ) ;
338
335
336
+ unsafe { // purity workaround
339
337
let mut a = x. next( ) ;
340
338
let mut b = y. next( ) ;
341
339
342
340
while a. is_some( ) {
343
341
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 }
348
344
}
349
- return
350
345
}
351
346
352
- let ( a1 , _ ) = a. unwrap ( ) ;
353
- let ( b1 , _ ) = b. unwrap ( ) ;
347
+ let a1 = a. unwrap( ) ;
348
+ let b1 = b. unwrap( ) ;
354
349
355
350
if b1 < a1 {
356
351
if !f( b1) { return }
@@ -367,6 +362,20 @@ impl <T: Ord> TreeSet<T> {
367
362
}
368
363
}
369
364
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
+
370
379
// Nodes keep track of their level in the tree, starting at 1 in the
371
380
// leaves and with a red child sharing the level of the parent.
372
381
struct TreeNode <K : Ord , V > {
0 commit comments