Skip to content

Commit 05b7a19

Browse files
authored
Add iterable::Reverse. (#41)
1 parent 082e281 commit 05b7a19

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/iterable/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
//! Essentially, it provides a set of handy utilities as a wrapper around
55
//! iterators.
66
7+
mod rev;
8+
pub use rev::Reverse;
9+
710
/// The trait [`Iterable`] represents a streamable object that can produce
811
/// an arbitrary number of streams of length [`Iterable::len`](Iterable::len).
912
///
1013
/// An Iterable is pretty much like an [`IntoIterator`] that can be copied over
11-
/// and over, and has an hint of the length. Copies are meant to be shared across threads safely.
14+
/// and over, and has an hint of the length. Copies are meant to be shared
15+
/// across threads safely.
1216
///
1317
/// # Examples
1418
///
@@ -41,8 +45,8 @@
4145
/// [`Borrow`](std::borrow::Borrow) in order to avoid copying the contents of
4246
/// the iterator..
4347
///
44-
/// The `Iter` associated type has a lifetime that is independent from that of the
45-
/// [`Iterable`] object. This means that implicitly a copy of the relevant
48+
/// The `Iter` associated type has a lifetime that is independent from that of
49+
/// the [`Iterable`] object. This means that implicitly a copy of the relevant
4650
/// contents of the object will happen whenever
4751
/// [`Iterable::iter`](crate::iterable::Iterable::iter) is called. This might
4852
/// change in the future as associated type constructors

src/iterable/rev.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use super::Iterable;
2+
use crate::iter::Rev;
3+
4+
/// Stream that goes over an `[ExactSizeIterator]` in reverse order.
5+
///
6+
/// This stream allows to switch fast from little endian ordering used in
7+
/// time-efficient algorithms, e.g. in slices `&[T]` into big endia ordering
8+
/// (used in space-efficient algorithms.
9+
///
10+
/// # Examples
11+
/// ```
12+
/// use ark_std::iterable::{Iterable, Reverse};
13+
///
14+
/// let le_v = &[1, 2, 3];
15+
/// let be_v = Reverse(le_v);
16+
/// let mut be_v_iter = be_v.iter();
17+
/// assert_eq!(be_v_iter.next(), Some(&3));
18+
/// assert_eq!(be_v_iter.next(), Some(&2));
19+
/// assert_eq!(be_v_iter.next(), Some(&1));
20+
/// ```
21+
#[derive(Clone, Copy)]
22+
pub struct Reverse<I>(pub I)
23+
where
24+
I: Iterable,
25+
I::Iter: DoubleEndedIterator;
26+
27+
impl<I> Iterable for Reverse<I>
28+
where
29+
I: Iterable,
30+
I::Iter: DoubleEndedIterator,
31+
{
32+
type Item = I::Item;
33+
type Iter = Rev<I::Iter>;
34+
35+
#[inline]
36+
fn iter(&self) -> Self::Iter {
37+
self.0.iter().rev()
38+
}
39+
40+
#[inline]
41+
fn len(&self) -> usize {
42+
self.0.len()
43+
}
44+
}

0 commit comments

Comments
 (0)