@@ -558,6 +558,28 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
558558 }
559559}
560560
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+
561583/// Remove the last element from a vector and return it
562584pub fn pop<T>(v: &mut ~[T]) -> T {
563585 let ln = v.len();
@@ -1983,6 +2005,7 @@ pub trait OwnedVector<T> {
19832005 fn truncate ( & mut self , newlen : uint ) ;
19842006 fn retain ( & mut self , f : & fn ( t : & T ) -> bool ) ;
19852007 fn consume ( self , f : & fn ( uint , v : T ) ) ;
2008+ fn consume_reverse ( self , f : & fn ( uint , v : T ) ) ;
19862009 fn filter ( self , f : & fn ( t : & T ) -> bool ) -> ~[ T ] ;
19872010 fn partition ( self , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) ;
19882011 fn grow_fn ( & mut self , n : uint , op : iter:: InitOp < T > ) ;
@@ -2044,6 +2067,11 @@ impl<T> OwnedVector<T> for ~[T] {
20442067 consume ( self , f)
20452068 }
20462069
2070+ #[ inline]
2071+ fn consume_reverse ( self , f : & fn ( uint , v : T ) ) {
2072+ consume_reverse ( self , f)
2073+ }
2074+
20472075 #[ inline]
20482076 fn filter ( self , f : & fn ( & T ) -> bool ) -> ~[ T ] {
20492077 filter ( self , f)
0 commit comments