Skip to content

Commit f686213

Browse files
author
blake2-ppc
committed
extra: Implement RandomAccessIterator for RingBuf
1 parent 2f10d1e commit f686213

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/libextra/ringbuf.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use std::num;
1717
use std::uint;
1818
use std::vec;
19-
use std::iterator::{FromIterator, Invert};
19+
use std::iterator::{FromIterator, Invert, RandomAccessIterator};
2020

2121
use container::Deque;
2222

@@ -176,8 +176,7 @@ impl<T> RingBuf<T> {
176176

177177
/// Front-to-back iterator.
178178
pub fn iter<'a>(&'a self) -> RingBufIterator<'a, T> {
179-
RingBufIterator{index: 0, rindex: self.nelts - 1,
180-
nelts: self.nelts, elts: self.elts, lo: self.lo}
179+
RingBufIterator{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts}
181180
}
182181

183182
/// Back-to-front iterator.
@@ -187,8 +186,7 @@ impl<T> RingBuf<T> {
187186

188187
/// Front-to-back iterator which returns mutable values.
189188
pub fn mut_iter<'a>(&'a mut self) -> RingBufMutIterator<'a, T> {
190-
RingBufMutIterator{index: 0, rindex: self.nelts - 1,
191-
nelts: self.nelts, elts: self.elts, lo: self.lo}
189+
RingBufMutIterator{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts}
192190
}
193191

194192
/// Back-to-front iterator which returns mutable values.
@@ -202,18 +200,18 @@ macro_rules! iterator {
202200
impl<'self, T> Iterator<$elem> for $name<'self, T> {
203201
#[inline]
204202
fn next(&mut self) -> Option<$elem> {
205-
if self.nelts == 0 {
203+
if self.index == self.rindex {
206204
return None;
207205
}
208206
let raw_index = raw_index(self.lo, self.elts.len(), self.index);
209207
self.index += 1;
210-
self.nelts -= 1;
211-
Some(self.elts[raw_index]. $getter ())
208+
Some(self.elts[raw_index] . $getter ())
212209
}
213210

214211
#[inline]
215212
fn size_hint(&self) -> (uint, Option<uint>) {
216-
(self.nelts, Some(self.nelts))
213+
let len = self.rindex - self.index;
214+
(len, Some(len))
217215
}
218216
}
219217
}
@@ -224,33 +222,46 @@ macro_rules! iterator_rev {
224222
impl<'self, T> DoubleEndedIterator<$elem> for $name<'self, T> {
225223
#[inline]
226224
fn next_back(&mut self) -> Option<$elem> {
227-
if self.nelts == 0 {
225+
if self.index == self.rindex {
228226
return None;
229227
}
230-
let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
231228
self.rindex -= 1;
232-
self.nelts -= 1;
233-
Some(self.elts[raw_index]. $getter ())
229+
let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
230+
Some(self.elts[raw_index] . $getter ())
234231
}
235232
}
236233
}
237234
}
238235

236+
239237
/// RingBuf iterator
240238
pub struct RingBufIterator<'self, T> {
241239
priv lo: uint,
242-
priv nelts: uint,
243240
priv index: uint,
244241
priv rindex: uint,
245242
priv elts: &'self [Option<T>],
246243
}
247244
iterator!{impl RingBufIterator -> &'self T, get_ref}
248245
iterator_rev!{impl RingBufIterator -> &'self T, get_ref}
249246

247+
impl<'self, T> RandomAccessIterator<&'self T> for RingBufIterator<'self, T> {
248+
#[inline]
249+
fn indexable(&self) -> uint { self.rindex - self.index }
250+
251+
#[inline]
252+
fn idx(&self, j: uint) -> Option<&'self T> {
253+
if j >= self.indexable() {
254+
None
255+
} else {
256+
let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
257+
Some(self.elts[raw_index].get_ref())
258+
}
259+
}
260+
}
261+
250262
/// RingBuf mutable iterator
251263
pub struct RingBufMutIterator<'self, T> {
252264
priv lo: uint,
253-
priv nelts: uint,
254265
priv index: uint,
255266
priv rindex: uint,
256267
priv elts: &'self mut [Option<T>],

0 commit comments

Comments
 (0)