Skip to content

Commit 75015c3

Browse files
author
blake2-ppc
committed
deque: Change iterators to use the same index calculation as Deque
The previous implementation of reverse iterators used modulus (%) of negative indices, which did work but was fragile due to dependency on the divisor.
1 parent f88d532 commit 75015c3

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

src/libextra/deque.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use std::uint;
1414
use std::vec;
15-
use std::cast::transmute;
1615
use std::iterator::FromIterator;
1716

1817
static INITIAL_CAPACITY: uint = 32u; // 2^5
@@ -92,13 +91,9 @@ impl<T> Deque<T> {
9291
result
9392
}
9493

95-
/// Return index in underlying vec for element index
94+
/// Return index in underlying vec for a given logical element index
9695
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)
10297
}
10398

10499
/// Remove and return the last element in the deque
@@ -159,83 +154,79 @@ impl<T> Deque<T> {
159154

160155
/// Front-to-back iterator.
161156
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}
163158
}
164159

165160
/// Front-to-back iterator which returns mutable values.
166161
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}
168163
}
169164

170165
/// Back-to-front iterator.
171166
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}
173169
}
174170

175171
/// Back-to-front iterator which returns mutable values.
176172
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}
178175
}
179176
}
180177

181178
macro_rules! iterator {
182-
(impl $name:ident -> $elem:ty, $step:expr) => {
179+
(impl $name:ident -> $elem:ty, $getter:ident, $step:expr) => {
183180
impl<'self, T> Iterator<$elem> for $name<'self, T> {
184181
#[inline]
185182
fn next(&mut self) -> Option<$elem> {
186-
if self.used >= self.nelts {
183+
if self.nelts == 0 {
187184
return None;
188185
}
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 ())
198190
}
199191
}
200192
}
201193
}
202194

203195
/// Deque iterator
204196
pub struct DequeIterator<'self, T> {
205-
priv idx: uint,
197+
priv lo: uint,
206198
priv nelts: uint,
207-
priv used: uint,
208-
priv vec: &'self [Option<T>]
199+
priv index: uint,
200+
priv elts: &'self [Option<T>],
209201
}
210-
iterator!{impl DequeIterator -> &'self T, 1}
202+
iterator!{impl DequeIterator -> &'self T, get_ref, 1}
211203

212204
/// Deque reverse iterator
213205
pub struct DequeRevIterator<'self, T> {
214-
priv idx: uint,
206+
priv lo: uint,
215207
priv nelts: uint,
216-
priv used: uint,
217-
priv vec: &'self [Option<T>]
208+
priv index: uint,
209+
priv elts: &'self [Option<T>],
218210
}
219-
iterator!{impl DequeRevIterator -> &'self T, -1}
211+
iterator!{impl DequeRevIterator -> &'self T, get_ref, -1}
220212

221213
/// Deque mutable iterator
222214
pub struct DequeMutIterator<'self, T> {
223-
priv idx: uint,
215+
priv lo: uint,
224216
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>],
228219
}
229-
iterator!{impl DequeMutIterator -> &'self mut T, 1}
220+
iterator!{impl DequeMutIterator -> &'self mut T, get_mut_ref, 1}
230221

231222
/// Deque mutable reverse iterator
232223
pub struct DequeMutRevIterator<'self, T> {
233-
priv idx: uint,
224+
priv lo: uint,
234225
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>],
237228
}
238-
iterator!{impl DequeMutRevIterator -> &'self mut T, -1}
229+
iterator!{impl DequeMutRevIterator -> &'self mut T, get_mut_ref, -1}
239230

240231
/// Grow is only called on full elts, so nelts is also len(elts), unlike
241232
/// elsewhere.
@@ -258,6 +249,15 @@ fn get<'r, T>(elts: &'r [Option<T>], i: uint) -> &'r T {
258249
match elts[i] { Some(ref t) => t, _ => fail!() }
259250
}
260251

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+
261261
impl<A, T: Iterator<A>> FromIterator<A, T> for Deque<A> {
262262
fn from_iterator(iterator: &mut T) -> Deque<A> {
263263
let mut deq = Deque::new();

0 commit comments

Comments
 (0)