Skip to content

Commit 35314c9

Browse files
jedestepemberian
authored andcommitted
Moving implementation details to a macro
1 parent 096fb79 commit 35314c9

File tree

1 file changed

+28
-67
lines changed

1 file changed

+28
-67
lines changed

src/libextra/deque.rs

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::prelude::*;
1414
use core::uint;
1515
use core::util::replace;
1616
use core::vec;
17+
use core::cast::transmute;
1718

1819
static initial_capacity: uint = 32u; // 2^5
1920

@@ -174,13 +175,36 @@ impl<T> Deque<T> {
174175
}
175176
}
176177

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+
177200
/// Deque iterator
178201
pub struct DequeIterator<'self, T> {
179202
priv idx: uint,
180203
priv nelts: uint,
181204
priv used: uint,
182205
priv vec: &'self [Option<T>]
183206
}
207+
iterator!{impl DequeIterator -> &'self T, 1}
184208

185209
/// Deque reverse iterator
186210
pub struct DequeRevIterator<'self, T> {
@@ -189,6 +213,8 @@ pub struct DequeRevIterator<'self, T> {
189213
priv used: uint,
190214
priv vec: &'self [Option<T>]
191215
}
216+
iterator!{impl DequeRevIterator -> &'self T, -1}
217+
192218
/// Deque mutable iterator
193219
pub struct DequeMutIterator<'self, T> {
194220
priv idx: uint,
@@ -197,6 +223,7 @@ pub struct DequeMutIterator<'self, T> {
197223
priv vec: &'self mut [Option<T>]
198224

199225
}
226+
iterator!{impl DequeMutIterator -> &'self mut T, 1}
200227

201228
/// Deque mutable reverse iterator
202229
pub struct DequeMutRevIterator<'self, T> {
@@ -205,73 +232,7 @@ pub struct DequeMutRevIterator<'self, T> {
205232
priv used: uint,
206233
priv vec: &'self mut [Option<T>]
207234
}
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}
275236

276237
/// Grow is only called on full elts, so nelts is also len(elts), unlike
277238
/// elsewhere.

0 commit comments

Comments
 (0)