16
16
use std:: num;
17
17
use std:: uint;
18
18
use std:: vec;
19
- use std:: iterator:: { FromIterator , Invert } ;
19
+ use std:: iterator:: { FromIterator , Invert , RandomAccessIterator } ;
20
20
21
21
use container:: Deque ;
22
22
@@ -176,8 +176,7 @@ impl<T> RingBuf<T> {
176
176
177
177
/// Front-to-back iterator.
178
178
pub fn iter<' a>( & ' a self ) -> RingBufIterator <' a, T > {
179
- RingBufIterator { index : 0 , rindex : self . nelts - 1 ,
180
- nelts : self . nelts , elts : self . elts , lo : self . lo }
179
+ RingBufIterator { index: 0 , rindex: self . nelts, lo: self . lo, elts: self . elts}
181
180
}
182
181
183
182
/// Back-to-front iterator.
@@ -187,8 +186,7 @@ impl<T> RingBuf<T> {
187
186
188
187
/// Front-to-back iterator which returns mutable values.
189
188
pub fn mut_iter<' a>( & ' a mut self ) -> RingBufMutIterator <' a, T > {
190
- RingBufMutIterator { index : 0 , rindex : self . nelts - 1 ,
191
- nelts : self . nelts , elts : self . elts , lo : self . lo }
189
+ RingBufMutIterator { index: 0 , rindex: self . nelts, lo: self . lo, elts: self . elts}
192
190
}
193
191
194
192
/// Back-to-front iterator which returns mutable values.
@@ -202,18 +200,18 @@ macro_rules! iterator {
202
200
impl <' self , T > Iterator <$elem> for $name<' self , T > {
203
201
#[ inline]
204
202
fn next( & mut self ) -> Option <$elem> {
205
- if self . nelts == 0 {
203
+ if self . index == self . rindex {
206
204
return None ;
207
205
}
208
206
let raw_index = raw_index( self . lo, self . elts. len( ) , self . index) ;
209
207
self . index += 1 ;
210
- self . nelts -= 1 ;
211
- Some ( self . elts[ raw_index] . $getter ( ) )
208
+ Some ( self . elts[ raw_index] . $getter ( ) )
212
209
}
213
210
214
211
#[ inline]
215
212
fn size_hint( & self ) -> ( uint, Option <uint>) {
216
- ( self . nelts, Some ( self . nelts) )
213
+ let len = self . rindex - self . index;
214
+ ( len, Some ( len) )
217
215
}
218
216
}
219
217
}
@@ -224,33 +222,46 @@ macro_rules! iterator_rev {
224
222
impl <' self , T > DoubleEndedIterator <$elem> for $name<' self , T > {
225
223
#[ inline]
226
224
fn next_back( & mut self ) -> Option <$elem> {
227
- if self . nelts == 0 {
225
+ if self . index == self . rindex {
228
226
return None ;
229
227
}
230
- let raw_index = raw_index( self . lo, self . elts. len( ) , self . rindex) ;
231
228
self . rindex -= 1 ;
232
- self . nelts -= 1 ;
233
- Some ( self . elts[ raw_index] . $getter ( ) )
229
+ let raw_index = raw_index ( self . lo , self . elts . len ( ) , self . rindex ) ;
230
+ Some ( self . elts[ raw_index] . $getter ( ) )
234
231
}
235
232
}
236
233
}
237
234
}
238
235
236
+
239
237
/// RingBuf iterator
240
238
pub struct RingBufIterator < ' self , T > {
241
239
priv lo: uint ,
242
- priv nelts : uint ,
243
240
priv index : uint ,
244
241
priv rindex : uint ,
245
242
priv elts : & ' self [ Option < T > ] ,
246
243
}
247
244
iterator ! { impl RingBufIterator -> & ' self T , get_ref}
248
245
iterator_rev ! { impl RingBufIterator -> & ' self T , get_ref}
249
246
247
+ impl < ' self , T > RandomAccessIterator < & ' self T > for RingBufIterator < ' self , T > {
248
+ #[ inline]
249
+ fn indexable ( & self ) -> uint { self . rindex - self . index }
250
+
251
+ #[ inline]
252
+ fn idx ( & self , j : uint ) -> Option < & ' self T > {
253
+ if j >= self . indexable ( ) {
254
+ None
255
+ } else {
256
+ let raw_index = raw_index ( self . lo , self . elts . len ( ) , self . index + j) ;
257
+ Some ( self . elts [ raw_index] . get_ref ( ) )
258
+ }
259
+ }
260
+ }
261
+
250
262
/// RingBuf mutable iterator
251
263
pub struct RingBufMutIterator < ' self , T > {
252
264
priv lo: uint ,
253
- priv nelts : uint ,
254
265
priv index : uint ,
255
266
priv rindex : uint ,
256
267
priv elts : & ' self mut [ Option < T > ] ,
0 commit comments