Skip to content
New issue

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

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

Already on GitHub? # to your account

Add element access via at() to std::mdspan #302

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from

Conversation

stephanlachnit
Copy link
Contributor

Closes #300

Possible implementation for ::at element access to mdspan with boundary checks. The boundary check is implemented with a for loop, as I did not see any other way to achieve this. The error message is inspired by the std::span::at error message from libstdc++ (gcc-mirror/gcc@1fa85dc).

Note that this does not implement ::at element access to mdarray.

@stephanlachnit stephanlachnit changed the title add ::at element access to mdspan Add element access via at() to std::mdspan Nov 29, 2023
@stigrs
Copy link

stigrs commented Jan 24, 2024

Given that SizeTypes can be signed, it should also check for indices[r] < 0.

@stephanlachnit
Copy link
Contributor Author

Given that SizeTypes can be signed, it should also check for indices[r] < 0.

Thanks for this comment! Is this true for std::span as well? At least in libstdc++, they do not check this:

https://github.com/gcc-mirror/gcc/blob/fd7dabc116b9abc40ee6aa25bcc5d240b8cc516a/libstdc%2B%2B-v3/include/std/span#L290-L300

@nmm0
Copy link
Contributor

nmm0 commented Aug 21, 2024

In span, I believe it's always just std::size_t, but mdspan has the size type as a template parameter for the extent and can be signed.

@stephanlachnit
Copy link
Contributor Author

stephanlachnit commented Aug 21, 2024

In span, I believe it's always just std::size_t, but mdspan has the size type as a template parameter for the extent and can be signed.

Ah right, thanks for pointing this out!

Edit: updated the pull request

Signed-off-by: Stephan Lachnit <stephanlachnit@debian.org>
Signed-off-by: Stephan Lachnit <stephanlachnit@debian.org>
Comment on lines +454 to +459
// Check for negative indices
if constexpr(std::is_signed_v<SizeType>) {
if(index < 0) {
return true;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

SizeType doesn't need to be integral here or even less-than comparable with int. mdspan behaves as if all operations on indices happen after conversion to index_type. Thus, should we consider instead converting to index_type first, as we do below?

Suggested change
// Check for negative indices
if constexpr(std::is_signed_v<SizeType>) {
if(index < 0) {
return true;
}
}
// Check for negative indices
if constexpr (std::is_signed_v<index_type>) {
if (static_cast<index_type>(index) < 0) {
return true;
}
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access with bounds checking using ::at
4 participants