@@ -49,8 +49,8 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
4949 let mut y = other. iter ( ) ;
5050 for self . len( ) . times {
5151 unsafe { // unsafe as a purity workaround
52- x = x . next ( ) ;
53- y = y . next ( ) ;
52+ map_next ( & mut x ) ;
53+ map_next ( & mut y ) ;
5454 // FIXME: #4492 (ICE), x.get() == y.get()
5555 let ( x1, x2) = x. get ( ) . unwrap ( ) ;
5656 let ( y1, y2) = y. get ( ) . unwrap ( ) ;
@@ -74,8 +74,8 @@ pure fn lt<K: Ord, V>(a: &TreeMap<K, V>, b: &TreeMap<K, V>) -> bool {
7474 let ( a_len, b_len) = ( a. len ( ) , b. len ( ) ) ;
7575 for uint:: min( a_len, b_len) . times {
7676 unsafe { // purity workaround
77- x = x . next ( ) ;
78- y = y . next ( ) ;
77+ map_next ( & mut x ) ;
78+ map_next ( & mut y ) ;
7979 let ( key_a, _) = x. get( ) . unwrap( ) ;
8080 let ( key_b, _) = y. get( ) . unwrap( ) ;
8181 if * key_a < * key_b { return true ; }
@@ -210,32 +210,30 @@ impl <K: Ord, V> TreeMapIterator<K, V> {
210210 // Returns the current node, or None if this iterator is at the end.
211211 fn get ( & const self ) -> Option < ( & self /K , & self /V ) > {
212212 match self . current {
213- Some ( res) => Some ( ( & res. key , & res. value ) ) ,
214- None => None
213+ Some ( res) => Some ( ( & res. key , & res. value ) ) ,
214+ None => None
215215 }
216216 }
217+ }
217218
218- /// Advance the iterator to the next node (in order). If this iterator
219- /// is finished, does nothing.
220- fn next ( self ) -> TreeMapIterator /& self <K , V > {
221- let mut this = self ;
222- while !this. stack. is_empty ( ) || this. node. is_some ( ) {
223- match * this. node {
224- Some ( ref x) => {
225- this. stack . push ( x) ;
226- this. node = & x. left ;
227- }
228- None => {
229- let res = this. stack . pop ( ) ;
230- this. node = & res. right ;
231- this. current = Some ( res) ;
232- return this;
233- }
234- }
219+ /// Advance the iterator to the next node (in order). If this iterator
220+ /// is finished, does nothing.
221+ fn map_next < K : Ord , V > ( iter : & mut TreeMapIterator /& a < K , V > ) {
222+ while !iter. stack . is_empty ( ) || iter. node . is_some ( ) {
223+ match * iter. node {
224+ Some ( ref x) => {
225+ iter. stack . push ( x) ;
226+ iter. node = & x. left ;
227+ }
228+ None => {
229+ let res = iter. stack . pop ( ) ;
230+ iter. node = & res. right ;
231+ iter. current = Some ( res) ;
232+ return ;
233+ }
235234 }
236- this. current = None ;
237- return this;
238235 }
236+ iter. current = None ;
239237}
240238
241239pub struct TreeSet < T > {
@@ -297,18 +295,18 @@ impl <T: Ord> TreeSet<T>: Set<T> {
297295 let mut x = self . iter ( ) ;
298296 let mut y = other. iter ( ) ;
299297 unsafe { // purity workaround
300- x = x . next ( ) ;
301- y = y . next ( ) ;
298+ set_next ( & mut x ) ;
299+ set_next ( & mut y ) ;
302300 let mut a = x. get ( ) ;
303301 let mut b = y. get ( ) ;
304302 while a. is_some ( ) && b. is_some ( ) {
305303 let a1 = a. unwrap ( ) ;
306304 let b1 = b. unwrap ( ) ;
307305 if a1 < b1 {
308- x = x . next ( ) ;
306+ set_next ( & mut x ) ;
309307 a = x. get ( ) ;
310308 } else if b1 < a1 {
311- y = y . next ( ) ;
309+ set_next ( & mut y ) ;
312310 b = y. get ( ) ;
313311 } else {
314312 return false ;
@@ -328,8 +326,8 @@ impl <T: Ord> TreeSet<T>: Set<T> {
328326 let mut x = self . iter ( ) ;
329327 let mut y = other. iter ( ) ;
330328 unsafe { // purity workaround
331- x = x . next ( ) ;
332- y = y . next ( ) ;
329+ set_next ( & mut x ) ;
330+ set_next ( & mut y ) ;
333331 let mut a = x. get ( ) ;
334332 let mut b = y. get ( ) ;
335333 while b. is_some ( ) {
@@ -345,10 +343,10 @@ impl <T: Ord> TreeSet<T>: Set<T> {
345343 }
346344
347345 if !( a1 < b1) {
348- y = y . next ( ) ;
346+ set_next ( & mut y ) ;
349347 b = y. get ( ) ;
350348 }
351- x = x . next ( ) ;
349+ set_next ( & mut x ) ;
352350 a = x. get ( ) ;
353351 }
354352 }
@@ -361,15 +359,15 @@ impl <T: Ord> TreeSet<T>: Set<T> {
361359 let mut y = other. iter ( ) ;
362360
363361 unsafe { // purity workaround
364- x = x . next ( ) ;
365- y = y . next ( ) ;
362+ set_next ( & mut x ) ;
363+ set_next ( & mut y ) ;
366364 let mut a = x. get ( ) ;
367365 let mut b = y. get ( ) ;
368366
369367 while a. is_some ( ) {
370368 if b. is_none ( ) {
371369 return do a. while_some ( ) |a1| {
372- if f ( a1) { x = x . next ( ) ; x. get ( ) } else { None }
370+ if f ( a1) { set_next ( & mut x ) ; x. get ( ) } else { None }
373371 }
374372 }
375373
@@ -378,11 +376,11 @@ impl <T: Ord> TreeSet<T>: Set<T> {
378376
379377 if a1 < b1 {
380378 if !f ( a1) { return }
381- x = x . next ( ) ;
379+ set_next ( & mut x ) ;
382380 a = x. get ( ) ;
383381 } else {
384- if !( b1 < a1) { x = x . next ( ) ; a = x. get ( ) }
385- y = y . next ( ) ;
382+ if !( b1 < a1) { set_next ( & mut x ) ; a = x. get ( ) }
383+ set_next ( & mut y ) ;
386384 b = y. get ( ) ;
387385 }
388386 }
@@ -396,15 +394,15 @@ impl <T: Ord> TreeSet<T>: Set<T> {
396394 let mut y = other. iter ( ) ;
397395
398396 unsafe { // purity workaround
399- x = x . next ( ) ;
400- y = y . next ( ) ;
397+ set_next ( & mut x ) ;
398+ set_next ( & mut y ) ;
401399 let mut a = x. get ( ) ;
402400 let mut b = y. get ( ) ;
403401
404402 while a. is_some ( ) {
405403 if b. is_none ( ) {
406404 return do a. while_some ( ) |a1| {
407- if f ( a1) { x . next ( ) ; x. get ( ) } else { None }
405+ if f ( a1) { set_next ( & mut x ) ; x. get ( ) } else { None }
408406 }
409407 }
410408
@@ -413,21 +411,21 @@ impl <T: Ord> TreeSet<T>: Set<T> {
413411
414412 if a1 < b1 {
415413 if !f ( a1) { return }
416- x = x . next ( ) ;
414+ set_next ( & mut x ) ;
417415 a = x. get ( ) ;
418416 } else {
419417 if b1 < a1 {
420418 if !f ( b1) { return }
421419 } else {
422- x = x . next ( ) ;
420+ set_next ( & mut x ) ;
423421 a = x. get ( ) ;
424422 }
425- y = y . next ( ) ;
423+ set_next ( & mut y ) ;
426424 b = y. get ( ) ;
427425 }
428426 }
429427 do b. while_some |b1| {
430- if f ( b1) { y = y . next ( ) ; y. get ( ) } else { None }
428+ if f ( b1) { set_next ( & mut y ) ; y. get ( ) } else { None }
431429 }
432430 }
433431 }
@@ -438,22 +436,22 @@ impl <T: Ord> TreeSet<T>: Set<T> {
438436 let mut y = other. iter ( ) ;
439437
440438 unsafe { // purity workaround
441- x = x . next ( ) ;
442- y = y . next ( ) ;
439+ set_next ( & mut x ) ;
440+ set_next ( & mut y ) ;
443441 let mut a = x. get ( ) ;
444442 let mut b = y. get ( ) ;
445443
446444 while a. is_some ( ) && b. is_some ( ) {
447445 let a1 = a. unwrap ( ) ;
448446 let b1 = b. unwrap ( ) ;
449447 if a1 < b1 {
450- x = x . next ( ) ;
448+ set_next ( & mut x ) ;
451449 a = x. get ( ) ;
452450 } else {
453451 if !( b1 < a1) {
454452 if !f ( a1) { return }
455453 }
456- y = y . next ( ) ;
454+ set_next ( & mut y ) ;
457455 b = y. get ( ) ;
458456 }
459457 }
@@ -466,15 +464,15 @@ impl <T: Ord> TreeSet<T>: Set<T> {
466464 let mut y = other. iter ( ) ;
467465
468466 unsafe { // purity workaround
469- x = x . next ( ) ;
470- y = y . next ( ) ;
467+ set_next ( & mut x ) ;
468+ set_next ( & mut y ) ;
471469 let mut a = x. get ( ) ;
472470 let mut b = y. get ( ) ;
473471
474472 while a. is_some ( ) {
475473 if b. is_none ( ) {
476474 return do a. while_some ( ) |a1| {
477- if f ( a1) { x = x . next ( ) ; x. get ( ) } else { None }
475+ if f ( a1) { set_next ( & mut x ) ; x. get ( ) } else { None }
478476 }
479477 }
480478
@@ -483,15 +481,15 @@ impl <T: Ord> TreeSet<T>: Set<T> {
483481
484482 if b1 < a1 {
485483 if !f ( b1) { return }
486- y = y . next ( ) ;
484+ set_next ( & mut y ) ;
487485 b = y. get ( ) ;
488486 } else {
489487 if !f ( a1) { return }
490488 if !( a1 < b1) {
491- y = y . next ( ) ;
489+ set_next ( & mut y ) ;
492490 b = y. get ( )
493491 }
494- x = x . next ( ) ;
492+ set_next ( & mut x ) ;
495493 a = x. get ( ) ;
496494 }
497495 }
@@ -524,16 +522,16 @@ impl <T: Ord> TreeSetIterator<T> {
524522 /// Returns the current node, or None if this iterator is at the end.
525523 fn get( & const self ) -> Option <& self /T > {
526524 match self . iter. get( ) {
527- None => None ,
528- Some ( ( k, _) ) => Some ( k)
525+ None => None ,
526+ Some ( ( k, _) ) => Some ( k)
529527 }
530528 }
529+ }
531530
532- /// Advance the iterator to the next node (in order). If this iterator is
533- /// finished, does nothing.
534- fn next( self ) -> TreeSetIterator /& self <T > {
535- TreeSetIterator { iter: self . iter. next( ) }
536- }
531+ /// Advance the iterator to the next node (in order). If this iterator is
532+ /// finished, does nothing.
533+ fn set_next<T : Ord >( iter: & mut TreeSetIterator /& a<T >) {
534+ map_next( & mut iter. iter) ;
537535}
538536
539537// Nodes keep track of their level in the tree, starting at 1 in the
@@ -967,18 +965,18 @@ mod test_treemap {
967965
968966 // FIXME: #4492 (ICE): iter.next() == Some((&x1, &y1))
969967
970- iter = iter. next ( ) ;
968+ map_next ( & mut iter) ;
971969 assert iter. get ( ) . unwrap ( ) == ( & x1, & y1) ;
972- iter = iter. next ( ) ;
970+ map_next ( & mut iter) ;
973971 assert iter. get ( ) . unwrap ( ) == ( & x2, & y2) ;
974- iter = iter. next ( ) ;
972+ map_next ( & mut iter) ;
975973 assert iter. get ( ) . unwrap ( ) == ( & x3, & y3) ;
976- iter = iter. next ( ) ;
974+ map_next ( & mut iter) ;
977975 assert iter. get ( ) . unwrap ( ) == ( & x4, & y4) ;
978- iter = iter. next ( ) ;
976+ map_next ( & mut iter) ;
979977 assert iter. get ( ) . unwrap ( ) == ( & x5, & y5) ;
980978
981- iter = iter. next ( ) ;
979+ map_next ( & mut iter) ;
982980 assert iter. get ( ) . is_none ( ) ;
983981 }
984982}
0 commit comments