9
9
// except according to those terms.
10
10
11
11
//! A double-ended queue implemented as a circular buffer
12
-
13
12
use core:: prelude:: * ;
14
13
15
14
use core:: uint;
16
15
use core:: util:: replace;
17
16
use core:: vec;
17
+ use core:: cast:: transmute;
18
18
19
19
static initial_capacity: uint = 32 u; // 2^5
20
20
@@ -153,7 +153,86 @@ impl<T> Deque<T> {
153
153
pub fn reserve_at_least ( & mut self , n : uint ) {
154
154
vec:: reserve_at_least ( & mut self . elts , n) ;
155
155
}
156
+
157
+ /// Front-to-back iterator.
158
+ pub fn iter < ' a > ( & ' a self ) -> DequeIterator < ' a , T > {
159
+ DequeIterator { idx : self . lo , nelts : self . nelts , used : 0 , vec : self . elts }
160
+ }
161
+
162
+ /// Front-to-back iterator which returns mutable values.
163
+ pub fn mut_iter < ' a > ( & ' a mut self ) -> DequeMutIterator < ' a , T > {
164
+ DequeMutIterator { idx : self . lo , nelts : self . nelts , used : 0 , vec : self . elts }
165
+ }
166
+
167
+ /// Back-to-front iterator.
168
+ pub fn rev_iter < ' a > ( & ' a self ) -> DequeRevIterator < ' a , T > {
169
+ DequeRevIterator { idx : self . hi - 1 u, nelts : self . nelts , used : 0 , vec : self . elts }
170
+ }
171
+
172
+ /// Back-to-front iterator which returns mutable values.
173
+ pub fn mut_rev_iter < ' a > ( & ' a mut self ) -> DequeMutRevIterator < ' a , T > {
174
+ DequeMutRevIterator { idx : self . hi - 1 u, nelts : self . nelts , used : 0 , vec : self . elts }
175
+ }
176
+ }
177
+
178
+ macro_rules! iterator {
179
+ ( impl $name: ident -> $elem: ty, $step: expr) => {
180
+ impl <' self , T > Iterator <$elem> for $name<' self , T > {
181
+ #[ inline]
182
+ fn next( & mut self ) -> Option <$elem> {
183
+ if self . used >= self . nelts {
184
+ return None ;
185
+ }
186
+ let ret = unsafe {
187
+ match self . vec[ self . idx % self . vec. len( ) ] {
188
+ Some ( ref e) => Some ( transmute( e) ) ,
189
+ None => None
190
+ }
191
+ } ;
192
+ self . idx += $step;
193
+ self . used += 1 ;
194
+ ret
195
+ }
196
+ }
197
+ }
198
+ }
199
+
200
+ /// Deque iterator
201
+ pub struct DequeIterator < ' self , T > {
202
+ priv idx: uint ,
203
+ priv nelts : uint ,
204
+ priv used : uint ,
205
+ priv vec: & ' self [ Option < T > ]
156
206
}
207
+ iterator ! { impl DequeIterator -> & ' self T , 1 }
208
+
209
+ /// Deque reverse iterator
210
+ pub struct DequeRevIterator < ' self , T > {
211
+ priv idx: uint ,
212
+ priv nelts : uint ,
213
+ priv used : uint ,
214
+ priv vec: & ' self [ Option < T > ]
215
+ }
216
+ iterator ! { impl DequeRevIterator -> & ' self T , -1 }
217
+
218
+ /// Deque mutable iterator
219
+ pub struct DequeMutIterator < ' self , T > {
220
+ priv idx: uint ,
221
+ priv nelts : uint ,
222
+ priv used : uint ,
223
+ priv vec: & ' self mut [ Option < T > ]
224
+
225
+ }
226
+ iterator ! { impl DequeMutIterator -> & ' self mut T , 1 }
227
+
228
+ /// Deque mutable reverse iterator
229
+ pub struct DequeMutRevIterator < ' self , T > {
230
+ priv idx: uint ,
231
+ priv nelts : uint ,
232
+ priv used : uint ,
233
+ priv vec: & ' self mut [ Option < T > ]
234
+ }
235
+ iterator ! { impl DequeMutRevIterator -> & ' self mut T , -1 }
157
236
158
237
/// Grow is only called on full elts, so nelts is also len(elts), unlike
159
238
/// elsewhere.
@@ -178,6 +257,7 @@ mod tests {
178
257
use core:: cmp:: Eq ;
179
258
use core:: kinds:: Copy ;
180
259
use core:: vec:: capacity;
260
+ use core;
181
261
182
262
#[ test]
183
263
fn test_simple ( ) {
@@ -318,8 +398,7 @@ mod tests {
318
398
319
399
#[ test]
320
400
fn test_param_taggy ( ) {
321
- test_parameterized :: < Taggy > ( One ( 1 ) , Two ( 1 , 2 ) , Three ( 1 , 2 , 3 ) ,
322
- Two ( 17 , 42 ) ) ;
401
+ test_parameterized :: < Taggy > ( One ( 1 ) , Two ( 1 , 2 ) , Three ( 1 , 2 , 3 ) , Two ( 17 , 42 ) ) ;
323
402
}
324
403
325
404
#[ test]
@@ -382,4 +461,31 @@ mod tests {
382
461
assert_eq ! ( capacity( & mut d. elts) , 64 ) ;
383
462
}
384
463
464
+ #[ test]
465
+ fn test_iter ( ) {
466
+ let mut d = Deque :: new ( ) ;
467
+ for core:: int:: range( 0 , 5 ) |i| {
468
+ d. add_back ( i) ;
469
+ }
470
+ assert_eq ! ( d. iter( ) . collect:: <~[ & int] >( ) , ~[ & 0 , & 1 , & 2 , & 3 , & 4 ] ) ;
471
+
472
+ for core:: int:: range( 6 , 9 ) |i| {
473
+ d. add_front ( i) ;
474
+ }
475
+ assert_eq ! ( d. iter( ) . collect:: <~[ & int] >( ) , ~[ & 8 , & 7 , & 6 , & 0 , & 1 , & 2 , & 3 , & 4 ] ) ;
476
+ }
477
+
478
+ #[ test]
479
+ fn test_rev_iter ( ) {
480
+ let mut d = Deque :: new ( ) ;
481
+ for core:: int:: range( 0 , 5 ) |i| {
482
+ d. add_back ( i) ;
483
+ }
484
+ assert_eq ! ( d. rev_iter( ) . collect:: <~[ & int] >( ) , ~[ & 4 , & 3 , & 2 , & 1 , & 0 ] ) ;
485
+
486
+ for core:: int:: range( 6 , 9 ) |i| {
487
+ d. add_front ( i) ;
488
+ }
489
+ assert_eq ! ( d. rev_iter( ) . collect:: <~[ & int] >( ) , ~[ & 4 , & 3 , & 2 , & 1 , & 0 , & 6 , & 7 , & 8 ] ) ;
490
+ }
385
491
}
0 commit comments