@@ -48,13 +48,8 @@ impl<K:Eq + Ord,V:Eq> Eq for TreeMap<K, V> {
48
48
let mut y = other. iter ( ) ;
49
49
for self . len( ) . times {
50
50
unsafe { // unsafe as a purity workaround
51
- map_next( & mut x) ;
52
- map_next( & mut y) ;
53
- // FIXME: #4492 (ICE), x.get() == y.get()
54
- let ( x1, x2) = x. get ( ) . unwrap ( ) ;
55
- let ( y1, y2) = y. get ( ) . unwrap ( ) ;
56
-
57
- if x1 != y1 || x2 != y2 {
51
+ if map_next( & mut x) . unwrap( ) !=
52
+ map_next( & mut y) . unwrap ( ) {
58
53
return false
59
54
}
60
55
}
@@ -73,10 +68,8 @@ pure fn lt<K:Ord,V>(a: &TreeMap<K, V>, b: &TreeMap<K, V>) -> bool {
73
68
let ( a_len, b_len) = ( a. len( ) , b. len( ) ) ;
74
69
for uint:: min( a_len, b_len) . times {
75
70
unsafe { // purity workaround
76
- map_next( & mut x) ;
77
- map_next( & mut y) ;
78
- let ( key_a, _) = x. get( ) . unwrap( ) ;
79
- let ( key_b, _) = y. get( ) . unwrap( ) ;
71
+ let ( key_a, _) = map_next( & mut x) . unwrap( ) ;
72
+ let ( key_b, _) = map_next( & mut y) . unwrap( ) ;
80
73
if * key_a < * key_b { return true ; }
81
74
if * key_a > * key_b { return false ; }
82
75
}
@@ -201,30 +194,21 @@ impl <K:Ord,V> TreeMap<K, V> {
201
194
/// Get a lazy iterator over the key-value pairs in the map.
202
195
/// Requires that it be frozen (immutable).
203
196
pure fn iter ( & self ) -> TreeMapIterator /& self <K , V > {
204
- TreeMapIterator { stack: ~[ ] , node: & self . root , current : None }
197
+ TreeMapIterator { stack: ~[ ] , node: & self . root }
205
198
}
206
199
}
207
200
208
201
/// Lazy forward iterator over a map
209
202
pub struct TreeMapIterator < K , V > {
210
203
priv stack : ~[ & ~TreeNode < K , V > ] ,
211
- priv node : & Option < ~TreeNode < K , V > > ,
212
- priv current : Option < & ~TreeNode < K , V > >
204
+ priv node : & Option < ~TreeNode < K , V > >
213
205
}
214
206
215
- impl < K : Ord , V > TreeMapIterator < K , V > {
216
- // Returns the current node, or None if this iterator is at the end.
217
- fn get ( & const self ) -> Option < ( & self /K , & self /V ) > {
218
- match self . current {
219
- Some ( res) => Some ( ( & res. key , & res. value ) ) ,
220
- None => None
221
- }
222
- }
223
- }
224
-
225
- /// Advance the iterator to the next node (in order). If this iterator
226
- /// is finished, does nothing.
227
- pub fn map_next < K : Ord , V > ( iter : & mut TreeMapIterator /& a < K , V > ) {
207
+ /// Advance the iterator to the next node (in order) and return a
208
+ /// tuple with a reference to the key and value. If there are no
209
+ /// more nodes, return `None`.
210
+ fn map_next < K : Ord , V > ( iter : & mut TreeMapIterator /& r < K , V > )
211
+ -> Option < ( & r /K , & r /V ) > {
228
212
while !iter. stack . is_empty ( ) || iter. node . is_some ( ) {
229
213
match * iter. node {
230
214
Some ( ref x) => {
@@ -234,12 +218,24 @@ pub fn map_next<K:Ord,V>(iter: &mut TreeMapIterator/&a<K, V>) {
234
218
None => {
235
219
let res = iter. stack . pop ( ) ;
236
220
iter. node = & res. right ;
237
- iter. current = Some ( res) ;
238
- return ;
221
+ return Some ( ( & res. key , & res. value ) ) ;
239
222
}
240
223
}
241
224
}
242
- iter. current = None ;
225
+ None
226
+ }
227
+
228
+ /// Advance the iterator through the map
229
+ fn map_advance < K : Ord , V > ( iter : & mut TreeMapIterator /& r < K , V > ,
230
+ f : fn ( ( & r/K , & r/V ) ) -> bool ) {
231
+ loop {
232
+ match map_next ( iter) {
233
+ Some ( x) => {
234
+ if !f ( x) { return }
235
+ }
236
+ None => return
237
+ }
238
+ }
243
239
}
244
240
245
241
pub struct TreeSet < T > {
@@ -308,19 +304,15 @@ impl<T:Ord> Set<T> for TreeSet<T> {
308
304
let mut x = self . iter ( ) ;
309
305
let mut y = other. iter ( ) ;
310
306
unsafe { // purity workaround
311
- set_next ( & mut x) ;
312
- set_next ( & mut y) ;
313
- let mut a = x. get ( ) ;
314
- let mut b = y. get ( ) ;
307
+ let mut a = set_next ( & mut x) ;
308
+ let mut b = set_next ( & mut y) ;
315
309
while a. is_some ( ) && b. is_some ( ) {
316
310
let a1 = a. unwrap ( ) ;
317
311
let b1 = b. unwrap ( ) ;
318
312
if a1 < b1 {
319
- set_next ( & mut x) ;
320
- a = x. get ( ) ;
313
+ a = set_next ( & mut x) ;
321
314
} else if b1 < a1 {
322
- set_next ( & mut y) ;
323
- b = y. get ( ) ;
315
+ b = set_next ( & mut y) ;
324
316
} else {
325
317
return false ;
326
318
}
@@ -339,10 +331,8 @@ impl<T:Ord> Set<T> for TreeSet<T> {
339
331
let mut x = self . iter ( ) ;
340
332
let mut y = other. iter ( ) ;
341
333
unsafe { // purity workaround
342
- set_next ( & mut x) ;
343
- set_next ( & mut y) ;
344
- let mut a = x. get ( ) ;
345
- let mut b = y. get ( ) ;
334
+ let mut a = set_next ( & mut x) ;
335
+ let mut b = set_next ( & mut y) ;
346
336
while b. is_some ( ) {
347
337
if a. is_none ( ) {
348
338
return false
@@ -356,11 +346,9 @@ impl<T:Ord> Set<T> for TreeSet<T> {
356
346
}
357
347
358
348
if !( a1 < b1) {
359
- set_next ( & mut y) ;
360
- b = y. get ( ) ;
349
+ b = set_next ( & mut y) ;
361
350
}
362
- set_next ( & mut x) ;
363
- a = x. get ( ) ;
351
+ a = set_next ( & mut x) ;
364
352
}
365
353
}
366
354
true
@@ -372,15 +360,13 @@ impl<T:Ord> Set<T> for TreeSet<T> {
372
360
let mut y = other. iter ( ) ;
373
361
374
362
unsafe { // purity workaround
375
- set_next ( & mut x) ;
376
- set_next ( & mut y) ;
377
- let mut a = x. get ( ) ;
378
- let mut b = y. get ( ) ;
363
+ let mut a = set_next ( & mut x) ;
364
+ let mut b = set_next ( & mut y) ;
379
365
380
366
while a. is_some ( ) {
381
367
if b. is_none ( ) {
382
368
return do a. while_some ( ) |a1| {
383
- if f ( a1) { set_next ( & mut x) ; x . get ( ) } else { None }
369
+ if f ( a1) { set_next ( & mut x) } else { None }
384
370
}
385
371
}
386
372
@@ -389,12 +375,10 @@ impl<T:Ord> Set<T> for TreeSet<T> {
389
375
390
376
if a1 < b1 {
391
377
if !f ( a1) { return }
392
- set_next ( & mut x) ;
393
- a = x. get ( ) ;
378
+ a = set_next ( & mut x) ;
394
379
} else {
395
- if !( b1 < a1) { set_next ( & mut x) ; a = x. get ( ) }
396
- set_next ( & mut y) ;
397
- b = y. get ( ) ;
380
+ if !( b1 < a1) { a = set_next ( & mut x) }
381
+ b = set_next ( & mut y) ;
398
382
}
399
383
}
400
384
}
@@ -407,15 +391,13 @@ impl<T:Ord> Set<T> for TreeSet<T> {
407
391
let mut y = other. iter ( ) ;
408
392
409
393
unsafe { // purity workaround
410
- set_next ( & mut x) ;
411
- set_next ( & mut y) ;
412
- let mut a = x. get ( ) ;
413
- let mut b = y. get ( ) ;
394
+ let mut a = set_next ( & mut x) ;
395
+ let mut b = set_next ( & mut y) ;
414
396
415
397
while a. is_some ( ) {
416
398
if b. is_none ( ) {
417
399
return do a. while_some ( ) |a1| {
418
- if f ( a1) { set_next ( & mut x) ; x . get ( ) } else { None }
400
+ if f ( a1) { set_next ( & mut x) } else { None }
419
401
}
420
402
}
421
403
@@ -424,21 +406,18 @@ impl<T:Ord> Set<T> for TreeSet<T> {
424
406
425
407
if a1 < b1 {
426
408
if !f ( a1) { return }
427
- set_next ( & mut x) ;
428
- a = x. get ( ) ;
409
+ a = set_next ( & mut x) ;
429
410
} else {
430
411
if b1 < a1 {
431
412
if !f ( b1) { return }
432
413
} else {
433
- set_next ( & mut x) ;
434
- a = x. get ( ) ;
414
+ a = set_next ( & mut x) ;
435
415
}
436
- set_next ( & mut y) ;
437
- b = y. get ( ) ;
416
+ b = set_next ( & mut y) ;
438
417
}
439
418
}
440
419
do b. while_some |b1| {
441
- if f ( b1) { set_next ( & mut y) ; y . get ( ) } else { None }
420
+ if f ( b1) { set_next ( & mut y) } else { None }
442
421
}
443
422
}
444
423
}
@@ -449,23 +428,19 @@ impl<T:Ord> Set<T> for TreeSet<T> {
449
428
let mut y = other. iter ( ) ;
450
429
451
430
unsafe { // purity workaround
452
- set_next ( & mut x) ;
453
- set_next ( & mut y) ;
454
- let mut a = x. get ( ) ;
455
- let mut b = y. get ( ) ;
431
+ let mut a = set_next ( & mut x) ;
432
+ let mut b = set_next ( & mut y) ;
456
433
457
434
while a. is_some ( ) && b. is_some ( ) {
458
435
let a1 = a. unwrap ( ) ;
459
436
let b1 = b. unwrap ( ) ;
460
437
if a1 < b1 {
461
- set_next ( & mut x) ;
462
- a = x. get ( ) ;
438
+ a = set_next ( & mut x) ;
463
439
} else {
464
440
if !( b1 < a1) {
465
441
if !f ( a1) { return }
466
442
}
467
- set_next ( & mut y) ;
468
- b = y. get ( ) ;
443
+ b = set_next ( & mut y) ;
469
444
}
470
445
}
471
446
}
@@ -477,15 +452,13 @@ impl<T:Ord> Set<T> for TreeSet<T> {
477
452
let mut y = other. iter ( ) ;
478
453
479
454
unsafe { // purity workaround
480
- set_next ( & mut x) ;
481
- set_next ( & mut y) ;
482
- let mut a = x. get ( ) ;
483
- let mut b = y. get ( ) ;
455
+ let mut a = set_next ( & mut x) ;
456
+ let mut b = set_next ( & mut y) ;
484
457
485
458
while a. is_some ( ) {
486
459
if b. is_none ( ) {
487
460
return do a. while_some ( ) |a1| {
488
- if f ( a1) { set_next ( & mut x) ; x . get ( ) } else { None }
461
+ if f ( a1) { set_next ( & mut x) } else { None }
489
462
}
490
463
}
491
464
@@ -494,16 +467,13 @@ impl<T:Ord> Set<T> for TreeSet<T> {
494
467
495
468
if b1 < a1 {
496
469
if !f ( b1) { return }
497
- set_next ( & mut y) ;
498
- b = y. get ( ) ;
470
+ b = set_next ( & mut y) ;
499
471
} else {
500
472
if !f ( a1) { return }
501
473
if !( a1 < b1) {
502
- set_next ( & mut y) ;
503
- b = y. get ( )
474
+ b = set_next ( & mut y) ;
504
475
}
505
- set_next ( & mut x) ;
506
- a = x. get ( ) ;
476
+ a = set_next ( & mut x) ;
507
477
}
508
478
}
509
479
}
@@ -526,20 +496,16 @@ pub struct TreeSetIterator<T> {
526
496
priv iter: TreeMapIterator < T , ( ) >
527
497
}
528
498
529
- impl <T : Ord > TreeSetIterator <T > {
530
- /// Returns the current node, or None if this iterator is at the end.
531
- fn get( & const self ) -> Option <& self /T > {
532
- match self . iter. get( ) {
533
- None => None ,
534
- Some ( ( k, _) ) => Some ( k)
535
- }
536
- }
537
- }
538
-
539
499
/// Advance the iterator to the next node (in order). If this iterator is
540
500
/// finished , does nothing.
541
- pub fn set_next<T : Ord >( iter: & mut TreeSetIterator /& a<T >) {
542
- map_next( & mut iter. iter) ;
501
+ pub fn set_next < T : Ord > ( iter : & mut TreeSetIterator /& r < T > ) -> Option < & r /T > {
502
+ do map_next ( & mut iter. iter ) . map |& ( value, _) | { value }
503
+ }
504
+
505
+ /// Advance the iterator through the set
506
+ fn set_advance < T : Ord > ( iter : & mut TreeSetIterator /& r < T > ,
507
+ f : fn ( & r /T ) -> bool ) {
508
+ do map_advance ( & mut iter . iter) |( k , _ ) | { f ( k ) }
543
509
}
544
510
545
511
// Nodes keep track of their level in the tree, starting at 1 in the
@@ -983,23 +949,37 @@ mod test_treemap {
983
949
assert m. insert ( x5, y5) ;
984
950
985
951
let m = m;
986
- let mut iter = m. iter ( ) ;
952
+ let mut a = m. iter ( ) ;
987
953
988
954
// FIXME: #4492 (ICE): iter.get() == Some((&x1, &y1))
989
955
990
- map_next ( & mut iter) ;
991
- assert iter. get ( ) . unwrap ( ) == ( & x1, & y1) ;
992
- map_next ( & mut iter) ;
993
- assert iter. get ( ) . unwrap ( ) == ( & x2, & y2) ;
994
- map_next ( & mut iter) ;
995
- assert iter. get ( ) . unwrap ( ) == ( & x3, & y3) ;
996
- map_next ( & mut iter) ;
997
- assert iter. get ( ) . unwrap ( ) == ( & x4, & y4) ;
998
- map_next ( & mut iter) ;
999
- assert iter. get ( ) . unwrap ( ) == ( & x5, & y5) ;
1000
-
1001
- map_next ( & mut iter) ;
1002
- assert iter. get ( ) . is_none ( ) ;
956
+ assert map_next( & mut a) . unwrap ( ) == ( & x1, & y1) ;
957
+ assert map_next( & mut a) . unwrap ( ) == ( & x2, & y2) ;
958
+ assert map_next( & mut a) . unwrap ( ) == ( & x3, & y3) ;
959
+ assert map_next( & mut a) . unwrap ( ) == ( & x4, & y4) ;
960
+ assert map_next( & mut a) . unwrap ( ) == ( & x5, & y5) ;
961
+
962
+ assert map_next( & mut a) . is_none ( ) ;
963
+
964
+ let mut b = m. iter ( ) ;
965
+
966
+ let expected = [ ( & x1, & y1) , ( & x2, & y2) , ( & x3, & y3) , ( & x4, & y4) ,
967
+ ( & x5, & y5) ] ;
968
+ let mut i = 0 ;
969
+
970
+ for map_advance( & mut b) |x| {
971
+ assert expected[ i] == x;
972
+ i += 1 ;
973
+
974
+ if i == 2 {
975
+ break
976
+ }
977
+ }
978
+
979
+ for map_advance( & mut b) |x| {
980
+ assert expected[ i] == x;
981
+ i += 1 ;
982
+ }
1003
983
}
1004
984
}
1005
985
0 commit comments