Skip to content

Commit 98c1654

Browse files
committed
auto merge of #7808 : blake2-ppc/rust/ringbuf, r=thestinger
2 parents e844b52 + 3385e79 commit 98c1654

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/libextra/ringbuf.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::num;
1717
use std::util;
1818
use std::uint;
1919
use std::vec;
20-
use std::iterator::FromIterator;
20+
use std::iterator::{FromIterator, InvertIterator};
2121

2222
use container::Deque;
2323

@@ -180,37 +180,37 @@ impl<T> RingBuf<T> {
180180

181181
/// Front-to-back iterator.
182182
pub fn iter<'a>(&'a self) -> RingBufIterator<'a, T> {
183-
RingBufIterator{index: 0, nelts: self.nelts, elts: self.elts, lo: self.lo}
183+
RingBufIterator{index: 0, rindex: self.nelts - 1,
184+
nelts: self.nelts, elts: self.elts, lo: self.lo}
184185
}
185186

186-
/// Front-to-back iterator which returns mutable values.
187-
pub fn mut_iter<'a>(&'a mut self) -> RingBufMutIterator<'a, T> {
188-
RingBufMutIterator{index: 0, nelts: self.nelts, elts: self.elts, lo: self.lo}
187+
/// Back-to-front iterator.
188+
pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, RingBufIterator<'a, T>> {
189+
self.iter().invert()
189190
}
190191

191-
/// Back-to-front iterator.
192-
pub fn rev_iter<'a>(&'a self) -> RingBufRevIterator<'a, T> {
193-
RingBufRevIterator{index: self.nelts-1, nelts: self.nelts, elts: self.elts,
194-
lo: self.lo}
192+
/// Front-to-back iterator which returns mutable values.
193+
pub fn mut_iter<'a>(&'a mut self) -> RingBufMutIterator<'a, T> {
194+
RingBufMutIterator{index: 0, rindex: self.nelts - 1,
195+
nelts: self.nelts, elts: self.elts, lo: self.lo}
195196
}
196197

197198
/// Back-to-front iterator which returns mutable values.
198-
pub fn mut_rev_iter<'a>(&'a mut self) -> RingBufMutRevIterator<'a, T> {
199-
RingBufMutRevIterator{index: self.nelts-1, nelts: self.nelts, elts: self.elts,
200-
lo: self.lo}
199+
pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T, RingBufMutIterator<'a, T>> {
200+
self.mut_iter().invert()
201201
}
202202
}
203203

204204
macro_rules! iterator {
205-
(impl $name:ident -> $elem:ty, $getter:ident, $step:expr) => {
205+
(impl $name:ident -> $elem:ty, $getter:ident) => {
206206
impl<'self, T> Iterator<$elem> for $name<'self, T> {
207207
#[inline]
208208
fn next(&mut self) -> Option<$elem> {
209209
if self.nelts == 0 {
210210
return None;
211211
}
212212
let raw_index = raw_index(self.lo, self.elts.len(), self.index);
213-
self.index += $step;
213+
self.index += 1;
214214
self.nelts -= 1;
215215
Some(self.elts[raw_index]. $getter ())
216216
}
@@ -223,41 +223,44 @@ macro_rules! iterator {
223223
}
224224
}
225225

226-
/// RingBuf iterator
227-
pub struct RingBufIterator<'self, T> {
228-
priv lo: uint,
229-
priv nelts: uint,
230-
priv index: uint,
231-
priv elts: &'self [Option<T>],
226+
macro_rules! iterator_rev {
227+
(impl $name:ident -> $elem:ty, $getter:ident) => {
228+
impl<'self, T> DoubleEndedIterator<$elem> for $name<'self, T> {
229+
#[inline]
230+
fn next_back(&mut self) -> Option<$elem> {
231+
if self.nelts == 0 {
232+
return None;
233+
}
234+
let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
235+
self.rindex -= 1;
236+
self.nelts -= 1;
237+
Some(self.elts[raw_index]. $getter ())
238+
}
239+
}
240+
}
232241
}
233-
iterator!{impl RingBufIterator -> &'self T, get_ref, 1}
234242

235-
/// RingBuf reverse iterator
236-
pub struct RingBufRevIterator<'self, T> {
243+
/// RingBuf iterator
244+
pub struct RingBufIterator<'self, T> {
237245
priv lo: uint,
238246
priv nelts: uint,
239247
priv index: uint,
248+
priv rindex: uint,
240249
priv elts: &'self [Option<T>],
241250
}
242-
iterator!{impl RingBufRevIterator -> &'self T, get_ref, -1}
251+
iterator!{impl RingBufIterator -> &'self T, get_ref}
252+
iterator_rev!{impl RingBufIterator -> &'self T, get_ref}
243253

244254
/// RingBuf mutable iterator
245255
pub struct RingBufMutIterator<'self, T> {
246256
priv lo: uint,
247257
priv nelts: uint,
248258
priv index: uint,
259+
priv rindex: uint,
249260
priv elts: &'self mut [Option<T>],
250261
}
251-
iterator!{impl RingBufMutIterator -> &'self mut T, get_mut_ref, 1}
252-
253-
/// RingBuf mutable reverse iterator
254-
pub struct RingBufMutRevIterator<'self, T> {
255-
priv lo: uint,
256-
priv nelts: uint,
257-
priv index: uint,
258-
priv elts: &'self mut [Option<T>],
259-
}
260-
iterator!{impl RingBufMutRevIterator -> &'self mut T, get_mut_ref, -1}
262+
iterator!{impl RingBufMutIterator -> &'self mut T, get_mut_ref}
263+
iterator_rev!{impl RingBufMutIterator -> &'self mut T, get_mut_ref}
261264

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

0 commit comments

Comments
 (0)