@@ -18,7 +18,7 @@ use container::{Container, Mutable};
18
18
use cmp;
19
19
use cmp:: { Eq , TotalEq , TotalOrd , Ordering , Less , Equal , Greater } ;
20
20
use clone:: Clone ;
21
- use iterator:: { FromIterator , Iterator , IteratorUtil } ;
21
+ use iterator:: * ;
22
22
use kinds:: Copy ;
23
23
use libc:: c_void;
24
24
use num:: Zero ;
@@ -762,12 +762,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
762
762
}
763
763
#[ inline]
764
764
fn rev_iter( self ) -> VecRevIterator < ' self , T > {
765
- unsafe {
766
- let p = vec : : raw:: to_ptr( self ) ;
767
- VecRevIterator { ptr : p. offset( self . len( ) - 1 ) ,
768
- end : p. offset( -1 ) ,
769
- lifetime : cast:: transmute( p) }
770
- }
765
+ self . iter( ) . invert( )
771
766
}
772
767
773
768
/// Returns an iterator over the subslices of the vector which are
@@ -1143,7 +1138,6 @@ impl<T> OwnedVector<T> for ~[T] {
1143
1138
*
1144
1139
* * n - The number of elements to reserve space for
1145
1140
*/
1146
- #[ inline]
1147
1141
#[ cfg( stage0) ]
1148
1142
fn reserve( & mut self , n: uint) {
1149
1143
// Only make the (slow) call into the runtime if we have to
@@ -1177,7 +1171,6 @@ impl<T> OwnedVector<T> for ~[T] {
1177
1171
*
1178
1172
* * n - The number of elements to reserve space for
1179
1173
*/
1180
- #[ inline]
1181
1174
#[ cfg( not( stage0) ) ]
1182
1175
fn reserve( & mut self , n: uint) {
1183
1176
// Only make the (slow) call into the runtime if we have to
@@ -1235,21 +1228,12 @@ impl<T> OwnedVector<T> for ~[T] {
1235
1228
let repr: * * raw :: VecRepr = transmute ( & mut * self ) ;
1236
1229
let fill = ( * * repr) . unboxed . fill ;
1237
1230
if ( * * repr) . unboxed . alloc <= fill {
1238
- // need more space
1239
- reserve_no_inline ( self ) ;
1231
+ let new_len = self . len ( ) + 1 ;
1232
+ self . reserve_at_least ( new_len ) ;
1240
1233
}
1241
1234
1242
1235
self . push_fast ( t) ;
1243
1236
}
1244
-
1245
- // this peculiar function is because reserve_at_least is very
1246
- // large (because of reserve), and will be inlined, which
1247
- // makes push too large.
1248
- #[ inline( never) ]
1249
- fn reserve_no_inline<T >( v: & mut ~[ T ] ) {
1250
- let new_len = v. len( ) + 1 ;
1251
- v. reserve_at_least( new_len) ;
1252
- }
1253
1237
}
1254
1238
1255
1239
// This doesn't bother to make sure we have space.
@@ -1737,13 +1721,9 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
1737
1721
}
1738
1722
}
1739
1723
1724
+ #[ inline]
1740
1725
fn mut_rev_iter ( self ) -> VecMutRevIterator < ' self , T > {
1741
- unsafe {
1742
- let p = vec:: raw :: to_mut_ptr( self ) ;
1743
- VecMutRevIterator { ptr: p. offset( self . len( ) - 1 ) ,
1744
- end: p. offset( -1 ) ,
1745
- lifetime: cast:: transmute( p) }
1746
- }
1726
+ self . mut_iter ( ) . invert ( )
1747
1727
}
1748
1728
1749
1729
/**
@@ -2103,53 +2083,61 @@ macro_rules! iterator {
2103
2083
priv lifetime: $elem // FIXME: #5922
2104
2084
}
2105
2085
};*/
2106
- ( impl $name: ident -> $elem: ty, $step: expr) => {
2107
- // could be implemented with &[T] with .slice(), but this avoids bounds checks
2086
+ ( impl $name: ident -> $elem: ty) => {
2108
2087
impl <' self , T > Iterator <$elem> for $name<' self , T > {
2109
2088
#[ inline]
2110
2089
fn next( & mut self ) -> Option <$elem> {
2090
+ // could be implemented with slices, but this avoids bounds checks
2111
2091
unsafe {
2112
2092
if self . ptr == self . end {
2113
2093
None
2114
2094
} else {
2115
2095
let old = self . ptr;
2116
- self . ptr = self . ptr. offset( $step ) ;
2096
+ self . ptr = self . ptr. offset( 1 ) ;
2117
2097
Some ( cast:: transmute( old) )
2118
2098
}
2119
2099
}
2120
2100
}
2121
2101
2122
2102
#[ inline]
2123
2103
fn size_hint( & self ) -> ( uint, Option <uint>) {
2124
- let diff = if $step > 0 {
2125
- ( self . end as uint) - ( self . ptr as uint)
2126
- } else {
2127
- ( self . ptr as uint) - ( self . end as uint)
2128
- } ;
2104
+ let diff = ( self . end as uint) - ( self . ptr as uint) ;
2129
2105
let exact = diff / size_of:: <$elem>( ) ;
2130
2106
( exact, Some ( exact) )
2131
2107
}
2132
2108
}
2133
2109
}
2134
2110
}
2135
2111
2112
+ macro_rules! double_ended_iterator {
2113
+ ( impl $name: ident -> $elem: ty) => {
2114
+ impl <' self , T > DoubleEndedIterator <$elem> for $name<' self , T > {
2115
+ #[ inline]
2116
+ fn next_back( & mut self ) -> Option <$elem> {
2117
+ // could be implemented with slices, but this avoids bounds checks
2118
+ unsafe {
2119
+ if self . end == self . ptr {
2120
+ None
2121
+ } else {
2122
+ self . end = self . end. offset( -1 ) ;
2123
+ Some ( cast:: transmute( self . end) )
2124
+ }
2125
+ }
2126
+ }
2127
+ }
2128
+ }
2129
+ }
2130
+
2136
2131
//iterator!{struct VecIterator -> *T, &'self T}
2137
2132
/// An iterator for iterating over a vector.
2138
2133
pub struct VecIterator < ' self , T > {
2139
2134
priv ptr: * T ,
2140
2135
priv end: * T ,
2141
2136
priv lifetime : & ' self T // FIXME: #5922
2142
2137
}
2143
- iterator!{ impl VecIterator -> & ' self T , 1 }
2144
-
2145
- //iterator!{struct VecRevIterator -> *T, &'self T}
2146
- /// An iterator for iterating over a vector in reverse.
2147
- pub struct VecRevIterator <' self , T > {
2148
- priv ptr: * T ,
2149
- priv end: * T ,
2150
- priv lifetime: & ' self T // FIXME: #5922
2151
- }
2152
- iterator!{ impl VecRevIterator -> & ' self T , -1 }
2138
+ iterator ! { impl VecIterator -> & ' self T }
2139
+ double_ended_iterator ! { impl VecIterator -> & ' self T }
2140
+ pub type VecRevIterator < ' self , T > = InvertIterator < & ' self T , VecIterator < ' self , T > > ;
2153
2141
2154
2142
//iterator!{struct VecMutIterator -> *mut T, &'self mut T}
2155
2143
/// An iterator for mutating the elements of a vector.
@@ -2158,16 +2146,9 @@ pub struct VecMutIterator<'self, T> {
2158
2146
priv end: * mut T ,
2159
2147
priv lifetime : & ' self mut T // FIXME: #5922
2160
2148
}
2161
- iterator!{ impl VecMutIterator -> & ' self mut T , 1 }
2162
-
2163
- //iterator!{struct VecMutRevIterator -> *mut T, &'self mut T}
2164
- /// An iterator for mutating the elements of a vector in reverse.
2165
- pub struct VecMutRevIterator <' self , T > {
2166
- priv ptr: * mut T ,
2167
- priv end: * mut T ,
2168
- priv lifetime: & ' self mut T // FIXME: #5922
2169
- }
2170
- iterator!{ impl VecMutRevIterator -> & ' self mut T , -1 }
2149
+ iterator ! { impl VecMutIterator -> & ' self mut T }
2150
+ double_ended_iterator ! { impl VecMutIterator -> & ' self mut T }
2151
+ pub type VecMutRevIterator < ' self , T > = InvertIterator < & ' self mut T , VecMutIterator < ' self , T > > ;
2171
2152
2172
2153
/// An iterator that moves out of a vector.
2173
2154
pub struct VecConsumeIterator < T > {
0 commit comments