12
12
13
13
use std:: uint;
14
14
use std:: vec;
15
- use std:: cast:: transmute;
16
15
use std:: iterator:: FromIterator ;
17
16
18
17
static INITIAL_CAPACITY : uint = 32 u; // 2^5
@@ -92,13 +91,9 @@ impl<T> Deque<T> {
92
91
result
93
92
}
94
93
95
- /// Return index in underlying vec for element index
94
+ /// Return index in underlying vec for a given logical element index
96
95
fn raw_index ( & self , idx : uint ) -> uint {
97
- if self . lo >= self . elts . len ( ) - idx {
98
- ( self . lo + idx) - self . elts . len ( )
99
- } else {
100
- ( self . lo + idx)
101
- }
96
+ raw_index ( self . lo , self . elts . len ( ) , idx)
102
97
}
103
98
104
99
/// Remove and return the last element in the deque
@@ -159,83 +154,79 @@ impl<T> Deque<T> {
159
154
160
155
/// Front-to-back iterator.
161
156
pub fn iter < ' a > ( & ' a self ) -> DequeIterator < ' a , T > {
162
- DequeIterator { idx : self . lo , nelts : self . nelts , used : 0 , vec : self . elts }
157
+ DequeIterator { index : 0 , nelts : self . nelts , elts : self . elts , lo : self . lo }
163
158
}
164
159
165
160
/// Front-to-back iterator which returns mutable values.
166
161
pub fn mut_iter < ' a > ( & ' a mut self ) -> DequeMutIterator < ' a , T > {
167
- DequeMutIterator { idx : self . lo , nelts : self . nelts , used : 0 , vec : self . elts }
162
+ DequeMutIterator { index : 0 , nelts : self . nelts , elts : self . elts , lo : self . lo }
168
163
}
169
164
170
165
/// Back-to-front iterator.
171
166
pub fn rev_iter < ' a > ( & ' a self ) -> DequeRevIterator < ' a , T > {
172
- DequeRevIterator { idx : self . raw_index ( self . nelts -1 ) , nelts : self . nelts , used : 0 , vec : self . elts }
167
+ DequeRevIterator { index : self . nelts -1 , nelts : self . nelts , elts : self . elts ,
168
+ lo : self . lo }
173
169
}
174
170
175
171
/// Back-to-front iterator which returns mutable values.
176
172
pub fn mut_rev_iter < ' a > ( & ' a mut self ) -> DequeMutRevIterator < ' a , T > {
177
- DequeMutRevIterator { idx : self . raw_index ( self . nelts -1 ) , nelts : self . nelts , used : 0 , vec : self . elts }
173
+ DequeMutRevIterator { index : self . nelts -1 , nelts : self . nelts , elts : self . elts ,
174
+ lo : self . lo }
178
175
}
179
176
}
180
177
181
178
macro_rules! iterator {
182
- ( impl $name: ident -> $elem: ty, $step: expr) => {
179
+ ( impl $name: ident -> $elem: ty, $getter : ident , $ step: expr) => {
183
180
impl <' self , T > Iterator <$elem> for $name<' self , T > {
184
181
#[ inline]
185
182
fn next( & mut self ) -> Option <$elem> {
186
- if self . used >= self . nelts {
183
+ if self . nelts == 0 {
187
184
return None ;
188
185
}
189
- let ret = unsafe {
190
- match self . vec[ self . idx % self . vec. len( ) ] {
191
- Some ( ref e) => Some ( transmute( e) ) ,
192
- None => None
193
- }
194
- } ;
195
- self . idx += $step;
196
- self . used += 1 ;
197
- ret
186
+ let raw_index = raw_index( self . lo, self . elts. len( ) , self . index) ;
187
+ self . index += $step;
188
+ self . nelts -= 1 ;
189
+ Some ( self . elts[ raw_index] . $getter ( ) )
198
190
}
199
191
}
200
192
}
201
193
}
202
194
203
195
/// Deque iterator
204
196
pub struct DequeIterator < ' self , T > {
205
- priv idx : uint ,
197
+ priv lo : uint ,
206
198
priv nelts : uint ,
207
- priv used : uint ,
208
- priv vec : & ' self [ Option < T > ]
199
+ priv index : uint ,
200
+ priv elts : & ' self [ Option < T > ] ,
209
201
}
210
- iterator ! { impl DequeIterator -> & ' self T , 1 }
202
+ iterator ! { impl DequeIterator -> & ' self T , get_ref , 1 }
211
203
212
204
/// Deque reverse iterator
213
205
pub struct DequeRevIterator < ' self , T > {
214
- priv idx : uint ,
206
+ priv lo : uint ,
215
207
priv nelts : uint ,
216
- priv used : uint ,
217
- priv vec : & ' self [ Option < T > ]
208
+ priv index : uint ,
209
+ priv elts : & ' self [ Option < T > ] ,
218
210
}
219
- iterator ! { impl DequeRevIterator -> & ' self T , -1 }
211
+ iterator ! { impl DequeRevIterator -> & ' self T , get_ref , -1 }
220
212
221
213
/// Deque mutable iterator
222
214
pub struct DequeMutIterator < ' self , T > {
223
- priv idx : uint ,
215
+ priv lo : uint ,
224
216
priv nelts : uint ,
225
- priv used : uint ,
226
- priv vec: & ' self mut [ Option < T > ]
227
-
217
+ priv index : uint ,
218
+ priv elts : & ' self mut [ Option < T > ] ,
228
219
}
229
- iterator ! { impl DequeMutIterator -> & ' self mut T , 1 }
220
+ iterator ! { impl DequeMutIterator -> & ' self mut T , get_mut_ref , 1 }
230
221
231
222
/// Deque mutable reverse iterator
232
223
pub struct DequeMutRevIterator < ' self , T > {
233
- priv idx : uint ,
224
+ priv lo : uint ,
234
225
priv nelts : uint ,
235
- priv used : uint ,
236
- priv vec : & ' self mut [ Option < T > ]
226
+ priv index : uint ,
227
+ priv elts : & ' self mut [ Option < T > ] ,
237
228
}
238
- iterator ! { impl DequeMutRevIterator -> & ' self mut T , -1 }
229
+ iterator ! { impl DequeMutRevIterator -> & ' self mut T , get_mut_ref , -1 }
239
230
240
231
/// Grow is only called on full elts, so nelts is also len(elts), unlike
241
232
/// elsewhere.
@@ -258,6 +249,15 @@ fn get<'r, T>(elts: &'r [Option<T>], i: uint) -> &'r T {
258
249
match elts[ i] { Some ( ref t) => t, _ => fail ! ( ) }
259
250
}
260
251
252
+ /// Return index in underlying vec for a given logical element index
253
+ fn raw_index ( lo : uint , len : uint , index : uint ) -> uint {
254
+ if lo >= len - index {
255
+ lo + index - len
256
+ } else {
257
+ lo + index
258
+ }
259
+ }
260
+
261
261
impl < A , T : Iterator < A > > FromIterator < A , T > for Deque < A > {
262
262
fn from_iterator ( iterator : & mut T ) -> Deque < A > {
263
263
let mut deq = Deque :: new ( ) ;
0 commit comments