@@ -21,8 +21,6 @@ Do not use ==, !=, <, etc on doubly-linked lists -- it may not terminate.
21
21
use core:: prelude:: * ;
22
22
23
23
use core:: managed;
24
- use core:: old_iter;
25
- use core:: vec;
26
24
27
25
pub type DListLink < T > = Option < @mut DListNode < T > > ;
28
26
@@ -213,6 +211,42 @@ impl<T> DList<T> {
213
211
}
214
212
215
213
impl < T > DList < T > {
214
+ /**
215
+ * Iterates through the current contents.
216
+ *
217
+ * Attempts to access this dlist during iteration are allowed (to
218
+ * allow for e.g. breadth-first search with in-place enqueues), but
219
+ * removing the current node is forbidden.
220
+ */
221
+ pub fn each ( @mut self , f : & fn ( v : & T ) -> bool ) -> bool {
222
+ let mut link = self . peek_n ( ) ;
223
+ while link. is_some ( ) {
224
+ let nobe = link. get ( ) ;
225
+ assert ! ( nobe. linked) ;
226
+
227
+ {
228
+ let frozen_nobe = & * nobe;
229
+ if !f ( & frozen_nobe. data ) { return false ; }
230
+ }
231
+
232
+ // Check (weakly) that the user didn't do a remove.
233
+ if self . size == 0 {
234
+ fail ! ( "The dlist became empty during iteration??" )
235
+ }
236
+ if !nobe. linked ||
237
+ ( !( ( nobe. prev . is_some ( )
238
+ || managed:: mut_ptr_eq ( self . hd . expect ( "headless dlist?" ) ,
239
+ nobe) )
240
+ && ( nobe. next . is_some ( )
241
+ || managed:: mut_ptr_eq ( self . tl . expect ( "tailless dlist?" ) ,
242
+ nobe) ) ) ) {
243
+ fail ! ( "Removing a dlist node during iteration is forbidden!" )
244
+ }
245
+ link = nobe. next_link ( ) ;
246
+ }
247
+ return true ;
248
+ }
249
+
216
250
/// Get the size of the list. O(1).
217
251
pub fn len ( @mut self ) -> uint { self . size }
218
252
/// Returns true if the list is empty. O(1).
@@ -484,56 +518,6 @@ impl<T:Copy> DList<T> {
484
518
485
519
/// Get data at the list's tail, failing if empty. O(1).
486
520
pub fn tail ( @mut self ) -> T { copy self . tail_n ( ) . data }
487
-
488
- /// Get the elements of the list as a vector. O(n).
489
- pub fn to_vec ( @mut self ) -> ~[ T ] {
490
- let mut v = vec:: with_capacity ( self . size ) ;
491
- for old_iter:: eachi( & self ) |index, data| {
492
- v[ index] = copy * data;
493
- }
494
- v
495
- }
496
- }
497
-
498
- impl <T > BaseIter < T > for @mut DList < T > {
499
- /**
500
- * Iterates through the current contents.
501
- *
502
- * Attempts to access this dlist during iteration are allowed (to
503
- * allow for e.g. breadth-first search with in-place enqueues), but
504
- * removing the current node is forbidden.
505
- */
506
- fn each ( & self , f : & fn ( v : & T ) -> bool ) -> bool {
507
- let mut link = self . peek_n ( ) ;
508
- while link. is_some ( ) {
509
- let nobe = link. get ( ) ;
510
- assert ! ( nobe. linked) ;
511
-
512
- {
513
- let frozen_nobe = & * nobe;
514
- if !f ( & frozen_nobe. data ) { return false ; }
515
- }
516
-
517
- // Check (weakly) that the user didn't do a remove.
518
- if self . size == 0 {
519
- fail ! ( "The dlist became empty during iteration??" )
520
- }
521
- if !nobe. linked ||
522
- ( !( ( nobe. prev . is_some ( )
523
- || managed:: mut_ptr_eq ( self . hd . expect ( "headless dlist?" ) ,
524
- nobe) )
525
- && ( nobe. next . is_some ( )
526
- || managed:: mut_ptr_eq ( self . tl . expect ( "tailless dlist?" ) ,
527
- nobe) ) ) ) {
528
- fail ! ( "Removing a dlist node during iteration is forbidden!" )
529
- }
530
- link = nobe. next_link ( ) ;
531
- }
532
- return true ;
533
- }
534
-
535
- #[ inline]
536
- fn size_hint ( & self ) -> Option < uint > { Some ( self . len ( ) ) }
537
521
}
538
522
539
523
#[ cfg( test) ]
@@ -542,7 +526,6 @@ mod tests {
542
526
543
527
use super :: * ;
544
528
545
- use core:: old_iter;
546
529
use core:: vec;
547
530
548
531
#[ test]
@@ -759,11 +742,6 @@ mod tests {
759
742
assert_eq ! ( l. len( ) , 3 ) ;
760
743
}
761
744
#[ test]
762
- fn test_dlist_foldl ( ) {
763
- let l = from_vec ( vec:: from_fn ( 101 , |x|x) ) ;
764
- assert_eq ! ( old_iter:: foldl( & l, 0 , |accum, elem| * accum+* elem) , 5050 ) ;
765
- }
766
- #[ test]
767
745
fn test_dlist_break_early ( ) {
768
746
let l = from_vec ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
769
747
let mut x = 0 ;
0 commit comments