@@ -17,7 +17,7 @@ use std::num;
17
17
use std:: util;
18
18
use std:: uint;
19
19
use std:: vec;
20
- use std:: iterator:: FromIterator ;
20
+ use std:: iterator:: { FromIterator , InvertIterator } ;
21
21
22
22
use container:: Deque ;
23
23
@@ -180,37 +180,37 @@ impl<T> RingBuf<T> {
180
180
181
181
/// Front-to-back iterator.
182
182
pub fn iter < ' a > ( & ' a self ) -> RingBufIterator < ' a , T > {
183
- RingBufIterator { index: 0 , nelts: self . nelts, elts: self . elts, lo: self . lo}
183
+ RingBufIterator { index : 0 , rindex : self . nelts - 1 ,
184
+ nelts : self . nelts , elts : self . elts , lo : self . lo }
184
185
}
185
186
186
- /// Front -to-back iterator which returns mutable values .
187
- pub fn mut_iter <' a>( & ' a mut self ) -> RingBufMutIterator < ' a , T > {
188
- RingBufMutIterator { index : 0 , nelts : self . nelts , elts : self . elts , lo : self . lo }
187
+ /// Back -to-front iterator.
188
+ pub fn rev_iter < ' a > ( & ' a self ) -> InvertIterator < & ' a T , RingBufIterator < ' a , T > > {
189
+ self . iter ( ) . invert ( )
189
190
}
190
191
191
- /// Back -to-front iterator.
192
- pub fn rev_iter <' a>( & ' a self ) -> RingBufRevIterator <' a, T > {
193
- RingBufRevIterator { index: self . nelts- 1 , nelts : self . nelts, elts : self . elts ,
194
- lo: self . lo}
192
+ /// Front -to-back iterator which returns mutable values .
193
+ pub fn mut_iter < ' a > ( & ' a mut self ) -> RingBufMutIterator < ' a , T > {
194
+ RingBufMutIterator { index : 0 , rindex : self . nelts - 1 ,
195
+ nelts : self . nelts , elts : self . elts , lo : self . lo }
195
196
}
196
197
197
198
/// Back-to-front iterator which returns mutable values.
198
- pub fn mut_rev_iter < ' a > ( & ' a mut self ) -> RingBufMutRevIterator < ' a , T > {
199
- RingBufMutRevIterator { index : self . nelts -1 , nelts : self . nelts , elts : self . elts ,
200
- lo : self . lo }
199
+ pub fn mut_rev_iter < ' a > ( & ' a mut self ) -> InvertIterator < & ' a mut T , RingBufMutIterator < ' a , T > > {
200
+ self . mut_iter ( ) . invert ( )
201
201
}
202
202
}
203
203
204
204
macro_rules! iterator {
205
- ( impl $name: ident -> $elem: ty, $getter: ident, $step : expr ) => {
205
+ ( impl $name: ident -> $elem: ty, $getter: ident) => {
206
206
impl <' self , T > Iterator <$elem> for $name<' self , T > {
207
207
#[ inline]
208
208
fn next( & mut self ) -> Option <$elem> {
209
209
if self . nelts == 0 {
210
210
return None ;
211
211
}
212
212
let raw_index = raw_index( self . lo, self . elts. len( ) , self . index) ;
213
- self . index += $step ;
213
+ self . index += 1 ;
214
214
self . nelts -= 1 ;
215
215
Some ( self . elts[ raw_index] . $getter ( ) )
216
216
}
@@ -223,41 +223,44 @@ macro_rules! iterator {
223
223
}
224
224
}
225
225
226
- /// RingBuf iterator
227
- pub struct RingBufIterator < ' self , T > {
228
- priv lo: uint ,
229
- priv nelts : uint ,
230
- priv index : uint ,
231
- priv elts : & ' self [ Option < T > ] ,
226
+ macro_rules! iterator_rev {
227
+ ( impl $name: ident -> $elem: ty, $getter: ident) => {
228
+ impl <' self , T > DoubleEndedIterator <$elem> for $name<' self , T > {
229
+ #[ inline]
230
+ fn next_back( & mut self ) -> Option <$elem> {
231
+ if self . nelts == 0 {
232
+ return None ;
233
+ }
234
+ let raw_index = raw_index( self . lo, self . elts. len( ) , self . rindex) ;
235
+ self . rindex -= 1 ;
236
+ self . nelts -= 1 ;
237
+ Some ( self . elts[ raw_index] . $getter ( ) )
238
+ }
239
+ }
240
+ }
232
241
}
233
- iterator ! { impl RingBufIterator -> & ' self T , get_ref, 1 }
234
242
235
- /// RingBuf reverse iterator
236
- pub struct RingBufRevIterator < ' self , T > {
243
+ /// RingBuf iterator
244
+ pub struct RingBufIterator < ' self , T > {
237
245
priv lo: uint ,
238
246
priv nelts : uint ,
239
247
priv index : uint ,
248
+ priv rindex : uint ,
240
249
priv elts : & ' self [ Option < T > ] ,
241
250
}
242
- iterator ! { impl RingBufRevIterator -> & ' self T , get_ref, -1 }
251
+ iterator ! { impl RingBufIterator -> & ' self T , get_ref}
252
+ iterator_rev ! { impl RingBufIterator -> & ' self T , get_ref}
243
253
244
254
/// RingBuf mutable iterator
245
255
pub struct RingBufMutIterator < ' self , T > {
246
256
priv lo: uint ,
247
257
priv nelts : uint ,
248
258
priv index : uint ,
259
+ priv rindex : uint ,
249
260
priv elts : & ' self mut [ Option < T > ] ,
250
261
}
251
- iterator ! { impl RingBufMutIterator -> & ' self mut T , get_mut_ref, 1 }
252
-
253
- /// RingBuf mutable reverse iterator
254
- pub struct RingBufMutRevIterator < ' self , T > {
255
- priv lo: uint ,
256
- priv nelts : uint ,
257
- priv index : uint ,
258
- priv elts : & ' self mut [ Option < T > ] ,
259
- }
260
- iterator ! { impl RingBufMutRevIterator -> & ' self mut T , get_mut_ref, -1 }
262
+ iterator ! { impl RingBufMutIterator -> & ' self mut T , get_mut_ref}
263
+ iterator_rev ! { impl RingBufMutIterator -> & ' self mut T , get_mut_ref}
261
264
262
265
/// Grow is only called on full elts, so nelts is also len(elts), unlike
263
266
/// elsewhere.
0 commit comments