Skip to content

Commit 5cb455d

Browse files
Muthserabluss
authored andcommitted
implement DoubleEndedIterator for 1d lanes
This is especially useful when one wants to iterate over rows or columns of a 2d array in reverse.
1 parent cd0a956 commit 5cb455d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/iterators/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,18 @@ where
732732
}
733733
}
734734

735+
impl<'a, A, D> DoubleEndedIterator for LanesIter<'a, A, D>
736+
where
737+
D: Dimension,
738+
Baseiter<A, D>: DoubleEndedIterator<Item = *mut A>,
739+
{
740+
fn next_back(&mut self) -> Option<Self::Item> {
741+
self.iter.next_back().map(|ptr| unsafe {
742+
ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix))
743+
})
744+
}
745+
}
746+
735747
// NOTE: LanesIterMut is a mutable iterator and must not expose aliasing
736748
// pointers. Due to this we use an empty slice for the raw data (it's unused
737749
// anyway).

tests/iterators.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ fn double_ended() {
3939
assert_equal(aview1(&[1, 2, 3]).into_iter().rev(), [1, 2, 3].iter().rev());
4040
}
4141

42+
#[test]
43+
fn double_ended_rows() {
44+
let a = ArcArray::from_iter(0..8).reshape((4, 2));
45+
let mut row_it = a.rows().into_iter();
46+
assert_equal(row_it.next_back().unwrap(), &[6, 7]);
47+
assert_equal(row_it.next().unwrap(), &[0, 1]);
48+
assert_equal(row_it.next_back().unwrap(), &[4, 5]);
49+
assert_equal(row_it.next_back().unwrap(), &[2, 3]);
50+
assert!(row_it.next().is_none());
51+
assert!(row_it.next_back().is_none());
52+
53+
for (row, check) in a.rows().into_iter().rev().zip(&[[6, 7], [4, 5], [2, 3], [0, 1]]) {
54+
assert_equal(row, check);
55+
}
56+
}
57+
4258
#[test]
4359
fn iter_size_hint() {
4460
// Check that the size hint is correctly computed

0 commit comments

Comments
 (0)