-
Notifications
You must be signed in to change notification settings - Fork 69
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
How to "flip" mdspan using submdspan (or any other utility)? #352
Comments
No this is not currently supported and was not brought up as a requirement in the last 9 years ... I.e. like the default accessor but just store the total offset, and then have an access function which does:
|
As Christian explained, you can get the desired effect by defining a "reverse accessor."
The front matter of the proposal explains the authors' design intent. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0009r18.html The authors considered and rejected Python-style "negative indices mean counting from the back." It complicates indexing math by adding branches and/or modular arithmetic, both of which may affect performance. One could wrap mdspan to get this effect. |
Do you mean that flipping an mdspan in general has never come up, or specifically doing it via submdspan? |
Indexing using a negative index is not the same as having a negative stride. Giving certain indexing values special meaning (like the python "index from the back") obviously adds a lot of potential runtime overhead. On the other hand, negative strides together with the exact same (mathematical) mapping rules of the mdspan/submdspan create no overhead during access of the span. The constructors of those types could become slightly more expensive, depending on their exact specification though. In my experience (mostly with numpy), views with negative strides have come up as useful every once in a while. Thus, I would welcome additions to submdspan to make them compatible with negative strides. That is, if they really aren't; I actually have no experience with std::submdspan yet. |
I've been experimenting with mdspan for a few days, and one thing remains unclear to me: Is it possible to use submdspan to create an mdspan that "views" the original data in reverse order?
For example, consider a span with 9 rows and 10 columns:
This represents the following table:
Is there a way to call the submdspan function to get the same data but with the rows flipped?
I tried to achieve this by passing a negative stride into strided_slice, but it returned incorrect results (with negative extents):
At this point, I think I do not understand how this is supposed to be done. As far as I understand, the original intention of this proposal was to have a tool similar to indexing in NumPy, MATLAB, or Fortran, which can handle this task easily.
The text was updated successfully, but these errors were encountered: