@@ -1598,34 +1598,6 @@ pub fn eachi<'r,T>(v: &'r [T], f: &fn(uint, v: &'r T) -> bool) -> bool {
1598
1598
return true ;
1599
1599
}
1600
1600
1601
- /**
1602
- * Iterates over a vector's elements in reverse
1603
- *
1604
- * Return true to continue, false to break.
1605
- */
1606
- #[ inline( always) ]
1607
- pub fn each_reverse < ' r , T > ( v : & ' r [ T ] , blk : & fn ( v : & ' r T ) -> bool ) -> bool {
1608
- eachi_reverse ( v, |_i, v| blk ( v) )
1609
- }
1610
-
1611
- /**
1612
- * Iterates over a vector's elements and indices in reverse
1613
- *
1614
- * Return true to continue, false to break.
1615
- */
1616
- #[ inline( always) ]
1617
- pub fn eachi_reverse < ' r , T > ( v : & ' r [ T ] ,
1618
- blk : & fn ( i : uint , v : & ' r T ) -> bool ) -> bool {
1619
- let mut i = v. len ( ) ;
1620
- while i > 0 {
1621
- i -= 1 ;
1622
- if !blk ( i, & v[ i] ) {
1623
- return false ;
1624
- }
1625
- }
1626
- return true ;
1627
- }
1628
-
1629
1601
/**
1630
1602
* Iterate over all permutations of vector `v`.
1631
1603
*
@@ -1964,6 +1936,7 @@ impl<'self,T:Copy> CopyableVector<T> for &'self [T] {
1964
1936
pub trait ImmutableVector < ' self , T > {
1965
1937
fn slice ( & self , start : uint , end : uint ) -> & ' self [ T ] ;
1966
1938
fn iter ( self ) -> VecIterator < ' self , T > ;
1939
+ fn rev_iter ( self ) -> VecRevIterator < ' self , T > ;
1967
1940
fn head ( & self ) -> & ' self T ;
1968
1941
fn head_opt ( & self ) -> Option < & ' self T > ;
1969
1942
fn tail ( & self ) -> & ' self [ T ] ;
@@ -1974,8 +1947,6 @@ pub trait ImmutableVector<'self, T> {
1974
1947
fn last_opt ( & self ) -> Option < & ' self T > ;
1975
1948
fn position ( & self , f : & fn ( t : & T ) -> bool ) -> Option < uint > ;
1976
1949
fn rposition ( & self , f : & fn ( t : & T ) -> bool ) -> Option < uint > ;
1977
- fn each_reverse ( & self , blk : & fn ( & T ) -> bool ) -> bool ;
1978
- fn eachi_reverse ( & self , blk : & fn ( uint , & T ) -> bool ) -> bool ;
1979
1950
fn foldr < ' a , U > ( & ' a self , z : U , p : & fn ( t : & ' a T , u : U ) -> U ) -> U ;
1980
1951
fn map < U > ( & self , f : & fn ( t : & T ) -> U ) -> ~[ U ] ;
1981
1952
fn mapi < U > ( & self , f : & fn ( uint , t : & T ) -> U ) -> ~[ U ] ;
@@ -2002,6 +1973,15 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
2002
1973
lifetime : cast:: transmute ( p) }
2003
1974
}
2004
1975
}
1976
+ #[ inline]
1977
+ fn rev_iter ( self ) -> VecRevIterator < ' self , T > {
1978
+ unsafe {
1979
+ let p = vec:: raw:: to_ptr ( self ) ;
1980
+ VecRevIterator { ptr : p. offset ( self . len ( ) - 1 ) ,
1981
+ end : p. offset ( -1 ) ,
1982
+ lifetime : cast:: transmute ( p) }
1983
+ }
1984
+ }
2005
1985
2006
1986
/// Returns the first element of a vector, failing if the vector is empty.
2007
1987
#[ inline]
@@ -2059,18 +2039,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
2059
2039
rposition ( * self , f)
2060
2040
}
2061
2041
2062
- /// Iterates over a vector's elements in reverse.
2063
- #[ inline]
2064
- fn each_reverse ( & self , blk : & fn ( & T ) -> bool ) -> bool {
2065
- each_reverse ( * self , blk)
2066
- }
2067
-
2068
- /// Iterates over a vector's elements and indices in reverse.
2069
- #[ inline]
2070
- fn eachi_reverse ( & self , blk : & fn ( uint , & T ) -> bool ) -> bool {
2071
- eachi_reverse ( * self , blk)
2072
- }
2073
-
2074
2042
/// Reduce a vector from right to left
2075
2043
#[ inline]
2076
2044
fn foldr < ' a , U > ( & ' a self , z : U , p : & fn ( t : & ' a T , u : U ) -> U ) -> U {
@@ -2350,7 +2318,8 @@ impl<T:Eq> OwnedEqVector<T> for ~[T] {
2350
2318
#[ allow( missing_doc) ]
2351
2319
pub trait MutableVector < ' self , T > {
2352
2320
fn mut_slice ( self , start : uint , end : uint ) -> & ' self mut [ T ] ;
2353
- fn mut_iter ( self ) -> MutVecIterator < ' self , T > ;
2321
+ fn mut_iter ( self ) -> VecMutIterator < ' self , T > ;
2322
+ fn mut_rev_iter ( self ) -> VecMutRevIterator < ' self , T > ;
2354
2323
2355
2324
unsafe fn unsafe_mut_ref ( & self , index : uint ) -> * mut T ;
2356
2325
unsafe fn unsafe_set ( & self , index : uint , val : T ) ;
@@ -2363,14 +2332,23 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
2363
2332
}
2364
2333
2365
2334
#[ inline]
2366
- fn mut_iter ( self ) -> MutVecIterator < ' self , T > {
2335
+ fn mut_iter ( self ) -> VecMutIterator < ' self , T > {
2367
2336
unsafe {
2368
2337
let p = vec:: raw:: to_mut_ptr ( self ) ;
2369
- MutVecIterator { ptr : p, end : p. offset ( self . len ( ) ) ,
2338
+ VecMutIterator { ptr : p, end : p. offset ( self . len ( ) ) ,
2370
2339
lifetime : cast:: transmute ( p) }
2371
2340
}
2372
2341
}
2373
2342
2343
+ fn mut_rev_iter ( self ) -> VecMutRevIterator < ' self , T > {
2344
+ unsafe {
2345
+ let p = vec:: raw:: to_mut_ptr ( self ) ;
2346
+ VecMutRevIterator { ptr : p. offset ( self . len ( ) - 1 ) ,
2347
+ end : p. offset ( -1 ) ,
2348
+ lifetime : cast:: transmute ( p) }
2349
+ }
2350
+ }
2351
+
2374
2352
#[ inline( always) ]
2375
2353
unsafe fn unsafe_mut_ref ( & self , index : uint ) -> * mut T {
2376
2354
let pair_ptr: & ( * mut T , uint ) = transmute ( self ) ;
@@ -2872,52 +2850,69 @@ impl<A:Clone> Clone for ~[A] {
2872
2850
}
2873
2851
}
2874
2852
2875
- /// An external iterator for vectors (use with the std::iterator module)
2853
+ macro_rules! iterator {
2854
+ /* FIXME: #4375 Cannot attach documentation/attributes to a macro generated struct.
2855
+ (struct $name:ident -> $ptr:ty, $elem:ty) => {
2856
+ pub struct $name<'self, T> {
2857
+ priv ptr: $ptr,
2858
+ priv end: $ptr,
2859
+ priv lifetime: $elem // FIXME: #5922
2860
+ }
2861
+ };*/
2862
+ ( impl $name: ident -> $elem: ty, $step: expr) => {
2863
+ // could be implemented with &[T] with .slice(), but this avoids bounds checks
2864
+ impl <' self , T > Iterator <$elem> for $name<' self , T > {
2865
+ #[ inline]
2866
+ fn next( & mut self ) -> Option <$elem> {
2867
+ unsafe {
2868
+ if self . ptr == self . end {
2869
+ None
2870
+ } else {
2871
+ let old = self . ptr;
2872
+ self . ptr = self . ptr. offset( $step) ;
2873
+ Some ( cast:: transmute( old) )
2874
+ }
2875
+ }
2876
+ }
2877
+ }
2878
+ }
2879
+ }
2880
+
2881
+ //iterator!{struct VecIterator -> *T, &'self T}
2882
+ /// An iterator for iterating over a vector
2876
2883
pub struct VecIterator < ' self , T > {
2877
2884
priv ptr: * T ,
2878
2885
priv end: * T ,
2879
2886
priv lifetime : & ' self T // FIXME: #5922
2880
2887
}
2888
+ iterator ! { impl VecIterator -> & ' self T , 1 }
2881
2889
2882
- // could be implemented with &[T] with .slice(), but this avoids bounds checks
2883
- impl < ' self , T > Iterator < & ' self T > for VecIterator < ' self , T > {
2884
- #[ inline]
2885
- fn next ( & mut self ) -> Option < & ' self T > {
2886
- unsafe {
2887
- if self . ptr == self . end {
2888
- None
2889
- } else {
2890
- let old = self . ptr ;
2891
- self . ptr = self . ptr . offset ( 1 ) ;
2892
- Some ( cast:: transmute ( old) )
2893
- }
2894
- }
2895
- }
2890
+ //iterator!{struct VecRevIterator -> *T, &'self T}
2891
+ /// An iterator for iterating over a vector in reverse
2892
+ pub struct VecRevIterator < ' self , T > {
2893
+ priv ptr: * T ,
2894
+ priv end: * T ,
2895
+ priv lifetime : & ' self T // FIXME: #5922
2896
2896
}
2897
+ iterator ! { impl VecRevIterator -> & ' self T , -1 }
2897
2898
2898
- /// An external iterator for vectors with the possibility of mutating
2899
- /// elements. (use with the std::iterator module)
2900
- pub struct MutVecIterator < ' self , T > {
2899
+ //iterator!{struct VecMutIterator -> *mut T, &'self mut T}
2900
+ /// An iterator for mutating the elements of a vector
2901
+ pub struct VecMutIterator < ' self , T > {
2901
2902
priv ptr: * mut T ,
2902
2903
priv end: * mut T ,
2903
2904
priv lifetime : & ' self mut T // FIXME: #5922
2904
2905
}
2906
+ iterator ! { impl VecMutIterator -> & ' self mut T , 1 }
2905
2907
2906
- // could be implemented with &[T] with .slice(), but this avoids bounds checks
2907
- impl < ' self , T > Iterator < & ' self mut T > for MutVecIterator < ' self , T > {
2908
- #[ inline]
2909
- fn next ( & mut self ) -> Option < & ' self mut T > {
2910
- unsafe {
2911
- if self . ptr == self . end {
2912
- None
2913
- } else {
2914
- let old = self . ptr ;
2915
- self . ptr = self . ptr . offset ( 1 ) ;
2916
- Some ( cast:: transmute ( old) )
2917
- }
2918
- }
2919
- }
2908
+ //iterator!{struct VecMutRevIterator -> *mut T, &'self mut T}
2909
+ /// An iterator for mutating the elements of a vector in reverse
2910
+ pub struct VecMutRevIterator < ' self , T > {
2911
+ priv ptr: * mut T ,
2912
+ priv end: * mut T ,
2913
+ priv lifetime : & ' self mut T // FIXME: #5922
2920
2914
}
2915
+ iterator ! { impl VecMutRevIterator -> & ' self mut T , -1 }
2921
2916
2922
2917
impl < T > FromIter < T > for ~[ T ] {
2923
2918
#[ inline( always) ]
@@ -3527,43 +3522,6 @@ mod tests {
3527
3522
assert_eq ! ( i, 6 ) ;
3528
3523
}
3529
3524
3530
- #[ test]
3531
- fn test_each_reverse_empty ( ) {
3532
- let v: ~[ int ] = ~[ ] ;
3533
- for v. each_reverse |_v| {
3534
- fail ! ( ) ; // should never execute
3535
- }
3536
- }
3537
-
3538
- #[ test]
3539
- fn test_each_reverse_nonempty ( ) {
3540
- let mut i = 0 ;
3541
- for each_reverse( [ 1 , 2 , 3 ] ) |v| {
3542
- if i == 0 { assert ! ( * v == 3 ) ; }
3543
- i += * v
3544
- }
3545
- assert_eq ! ( i, 6 ) ;
3546
- }
3547
-
3548
- #[ test]
3549
- fn test_eachi_reverse ( ) {
3550
- let mut i = 0 ;
3551
- for eachi_reverse( [ 0 , 1 , 2 ] ) |j, v| {
3552
- if i == 0 { assert ! ( * v == 2 ) ; }
3553
- assert_eq ! ( j, * v as uint) ;
3554
- i += * v;
3555
- }
3556
- assert_eq ! ( i, 3 ) ;
3557
- }
3558
-
3559
- #[ test]
3560
- fn test_eachi_reverse_empty ( ) {
3561
- let v: ~[ int ] = ~[ ] ;
3562
- for v. eachi_reverse |_i, _v| {
3563
- fail ! ( ) ; // should never execute
3564
- }
3565
- }
3566
-
3567
3525
#[ test]
3568
3526
fn test_each_ret_len0 ( ) {
3569
3527
let mut a0 : [ int , .. 0 ] = [ ] ;
@@ -4642,6 +4600,30 @@ mod tests {
4642
4600
assert_eq ! ( xs, [ 2 , 3 , 4 , 5 , 6 ] )
4643
4601
}
4644
4602
4603
+ #[ test]
4604
+ fn test_rev_iterator( ) {
4605
+ use iterator:: * ;
4606
+
4607
+ let xs = [ 1 , 2 , 5 , 10 , 11 ] ;
4608
+ let ys = [ 11 , 10 , 5 , 2 , 1 ] ;
4609
+ let mut i = 0 ;
4610
+ for xs. rev_iter( ) . advance |& x| {
4611
+ assert_eq ! ( x, ys[ i] ) ;
4612
+ i += 1 ;
4613
+ }
4614
+ assert_eq ! ( i, 5 ) ;
4615
+ }
4616
+
4617
+ #[ test]
4618
+ fn test_mut_rev_iterator( ) {
4619
+ use iterator:: * ;
4620
+ let mut xs = [ 1 , 2 , 3 , 4 , 5 ] ;
4621
+ for xs. mut_rev_iter( ) . enumerate( ) . advance |( i, x) | {
4622
+ * x += i;
4623
+ }
4624
+ assert_eq ! ( xs, [ 5 , 5 , 5 , 5 , 5 ] )
4625
+ }
4626
+
4645
4627
#[ test]
4646
4628
fn test_reverse_part( ) {
4647
4629
let mut values = [ 1 , 2 , 3 , 4 , 5 ] ;
0 commit comments