@@ -14,6 +14,7 @@ use core::prelude::*;
14
14
use core:: uint;
15
15
use core:: util:: replace;
16
16
use core:: vec;
17
+ use core:: cast:: transmute;
17
18
18
19
static initial_capacity: uint = 32 u; // 2^5
19
20
@@ -174,13 +175,36 @@ impl<T> Deque<T> {
174
175
}
175
176
}
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
+
177
200
/// Deque iterator
178
201
pub struct DequeIterator < ' self , T > {
179
202
priv idx: uint ,
180
203
priv nelts : uint ,
181
204
priv used : uint ,
182
205
priv vec: & ' self [ Option < T > ]
183
206
}
207
+ iterator ! { impl DequeIterator -> & ' self T , 1 }
184
208
185
209
/// Deque reverse iterator
186
210
pub struct DequeRevIterator < ' self , T > {
@@ -189,6 +213,8 @@ pub struct DequeRevIterator<'self, T> {
189
213
priv used : uint ,
190
214
priv vec: & ' self [ Option < T > ]
191
215
}
216
+ iterator ! { impl DequeRevIterator -> & ' self T , -1 }
217
+
192
218
/// Deque mutable iterator
193
219
pub struct DequeMutIterator < ' self , T > {
194
220
priv idx: uint ,
@@ -197,6 +223,7 @@ pub struct DequeMutIterator<'self, T> {
197
223
priv vec: & ' self mut [ Option < T > ]
198
224
199
225
}
226
+ iterator ! { impl DequeMutIterator -> & ' self mut T , 1 }
200
227
201
228
/// Deque mutable reverse iterator
202
229
pub struct DequeMutRevIterator < ' self , T > {
@@ -205,73 +232,7 @@ pub struct DequeMutRevIterator<'self, T> {
205
232
priv used : uint ,
206
233
priv vec: & ' self mut [ Option < T > ]
207
234
}
208
-
209
- /// Iterator visiting elements of the deque from front to back
210
- impl < ' self , T > Iterator < & ' self T > for DequeIterator < ' self , T > {
211
- fn next ( & mut self ) -> Option < & ' self T > {
212
- if self . used >= self . nelts {
213
- return None ;
214
- }
215
- let ret = match self . vec [ self . idx % self . vec . len ( ) ] {
216
- Some ( ref e) => Some ( e) ,
217
- None => None
218
- } ;
219
- self . idx += 1 ;
220
- self . used += 1 ;
221
- ret
222
- }
223
- }
224
-
225
- /// Iterator visiting elements of the deque mutably from front to back
226
- impl < ' self , T > Iterator < & ' self mut T > for DequeMutIterator < ' self , T > {
227
- fn next ( & mut self ) -> Option < & ' self mut T > {
228
- if self . used >= self . nelts {
229
- return None ;
230
- }
231
- let ret = match self . vec [ self . idx % self . vec . len ( ) ] {
232
- Some ( ref mut e) => Some ( e) ,
233
- None => None
234
- } ;
235
- self . idx += 1 ;
236
- self . used += 1 ;
237
- ret
238
- }
239
- }
240
-
241
- /// Iterator visiting elements of the deque from back to front
242
- impl < ' self , T > Iterator < & ' self T > for DequeRevIterator < ' self , T > {
243
- #[ inline]
244
- fn next ( & mut self ) -> Option < & ' self T > {
245
- if self . used >= self . nelts {
246
- return None ;
247
- }
248
- let ret = match self . vec [ self . idx % self . vec . len ( ) ] {
249
- Some ( ref e) => Some ( e) ,
250
- None => None
251
- } ;
252
- self . idx -= 1 ;
253
- self . used += 1 ;
254
- ret
255
-
256
- }
257
- }
258
-
259
- /// Iterator visiting elements of the deque mutably from back to front
260
- impl < ' self , T > Iterator < & ' self mut T > for DequeMutRevIterator < ' self , T > {
261
- #[ inline]
262
- fn next ( & mut self ) -> Option < & ' self mut T > {
263
- if self . used >= self . nelts {
264
- return None ;
265
- }
266
- let ret = match self . vec [ self . idx % self . vec . len ( ) ] {
267
- Some ( ref mut e) => Some ( e) ,
268
- None => None
269
- } ;
270
- self . idx -= 1 ;
271
- self . used += 1 ;
272
- ret
273
- }
274
- }
235
+ iterator ! { impl DequeMutRevIterator -> & ' self mut T , -1 }
275
236
276
237
/// Grow is only called on full elts, so nelts is also len(elts), unlike
277
238
/// elsewhere.
0 commit comments