Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement DoubleEndedIterator for 1d LanesIter #1237

Merged
merged 1 commit into from
Mar 10, 2024

Conversation

Muthsera
Copy link
Contributor

In a project I wanted to iterate over the rows and columns of a 2d array in reverse but while rows and columns exist, the resulting LanesIter does not implement DoubleEndedIterator as of yet. It is possible to use indexing via row with reversed indices but that is less ergonomic.

This PR fixes this at least for 1d LanesIter and now allows one to do:

let a = ArcArray::from_iter(0..8).reshape((4, 2));
for (row, check) in a.rows().into_iter().rev().zip(&[[6, 7], [4, 5], [2, 3], [0, 1]]) {
    assert_equal(row, check);
}

The implementation just delegates to the underlying 1d Baseiter which implements DoubleEndedIterator.
The trait is not implemented for higher dimensions however, which would need some additional work.

@@ -726,6 +726,18 @@ where
}
}

impl<'a, A, D> DoubleEndedIterator for LanesIter<'a, A, D>
where
D: Dimension,
Copy link
Member

@bluss bluss Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected this to be limited to the case where D=Ix1. Just like how .iter()'s Iter works. I think it's better that way, then the Baseiter does not leak out (it's in the where clause, that's a leak of internals into public API.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that, and also applied the same change to LanesIterMut - it and LanesIter must of course change together.

@bluss bluss force-pushed the rev branch 2 times, most recently from 5cb455d to 0ec6ede Compare March 10, 2024 12:59
@bluss bluss added this to the 0.16.0 milestone Mar 10, 2024
@bluss bluss enabled auto-merge March 10, 2024 13:00
@bluss bluss added this pull request to the merge queue Mar 10, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 10, 2024
This is especially useful when one wants to iterate over rows or
columns of a 2d array in reverse.

Co-authored-by: Ulrik Sverdrup <bluss@users.noreply.github.com>
@bluss bluss enabled auto-merge March 10, 2024 13:30
@bluss bluss added this pull request to the merge queue Mar 10, 2024
Merged via the queue into rust-ndarray:master with commit 1e0102d Mar 10, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants