@@ -558,6 +558,28 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
558
558
}
559
559
}
560
560
561
+ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
562
+ unsafe {
563
+ do as_mut_buf(v) |p, ln| {
564
+ let mut i = ln;
565
+ while i > 0 {
566
+ i -= 1;
567
+
568
+ // NB: This unsafe operation counts on init writing 0s to the
569
+ // holes we create in the vector. That ensures that, if the
570
+ // iterator fails then we won't try to clean up the consumed
571
+ // elements during unwinding
572
+ let mut x = intrinsics::init();
573
+ let p = ptr::mut_offset(p, i);
574
+ x <-> *p;
575
+ f(i, x);
576
+ }
577
+ }
578
+
579
+ raw::set_len(&mut v, 0);
580
+ }
581
+ }
582
+
561
583
/// Remove the last element from a vector and return it
562
584
pub fn pop<T>(v: &mut ~[T]) -> T {
563
585
let ln = v.len();
@@ -1983,6 +2005,7 @@ pub trait OwnedVector<T> {
1983
2005
fn truncate ( & mut self , newlen : uint ) ;
1984
2006
fn retain ( & mut self , f : & fn ( t : & T ) -> bool ) ;
1985
2007
fn consume ( self , f : & fn ( uint , v : T ) ) ;
2008
+ fn consume_reverse ( self , f : & fn ( uint , v : T ) ) ;
1986
2009
fn filter ( self , f : & fn ( t : & T ) -> bool ) -> ~[ T ] ;
1987
2010
fn partition ( self , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) ;
1988
2011
fn grow_fn ( & mut self , n : uint , op : iter:: InitOp < T > ) ;
@@ -2044,6 +2067,11 @@ impl<T> OwnedVector<T> for ~[T] {
2044
2067
consume ( self , f)
2045
2068
}
2046
2069
2070
+ #[ inline]
2071
+ fn consume_reverse ( self , f : & fn ( uint , v : T ) ) {
2072
+ consume_reverse ( self , f)
2073
+ }
2074
+
2047
2075
#[ inline]
2048
2076
fn filter ( self , f : & fn ( & T ) -> bool ) -> ~[ T ] {
2049
2077
filter ( self , f)
0 commit comments