-
Notifications
You must be signed in to change notification settings - Fork 60
Add SimpleArray::as_span() and as_mdspan<N>()
#777
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,11 +38,14 @@ | |
| #include <modmesh/toggle/RadixTree.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <concepts> | ||
| #include <format> | ||
| #include <functional> | ||
| #include <limits> | ||
| #include <mdspan> | ||
| #include <numeric> | ||
| #include <span> | ||
| #include <stdexcept> | ||
|
|
||
| #ifdef _MSC_VER | ||
|
|
@@ -1946,6 +1949,57 @@ class SimpleArray | |
| template <typename... Args> | ||
| value_type * vptr(Args... args) { return m_body + buffer_offset(m_stride, args...); } | ||
|
|
||
| std::span<value_type> as_span() | ||
| { | ||
| if (!is_c_contiguous()) | ||
| { | ||
| throw std::runtime_error("SimpleArray::as_span: array is not C-contiguous"); | ||
| } | ||
| return std::span<value_type>(data(), size()); | ||
| } | ||
| std::span<value_type const> as_span() const | ||
| { | ||
| if (!is_c_contiguous()) | ||
| { | ||
| throw std::runtime_error("SimpleArray::as_span: array is not C-contiguous"); | ||
| } | ||
| return std::span<value_type const>(data(), size()); | ||
| } | ||
|
|
||
| template <size_t N> | ||
| std::mdspan<value_type, std::dextents<size_t, N>> as_mdspan() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multi-dimensional view. |
||
| { | ||
| if (ndim() != N) | ||
| { | ||
| throw std::out_of_range( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runtime shape check. |
||
| std::format("SimpleArray::as_mdspan: rank {} does not match ndim() {}", N, ndim())); | ||
| } | ||
| if (!is_c_contiguous()) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw if the buffer is not C-contiguous. |
||
| { | ||
| throw std::runtime_error("SimpleArray::as_mdspan: array is not C-contiguous"); | ||
| } | ||
| std::array<size_t, N> exts; | ||
| for (size_t i = 0; i < N; ++i) { exts[i] = shape(i); } | ||
|
Comment on lines
+1975
to
+1982
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may check whether SimpleArray is contiguous. The size of shape may not equal to the size of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Added above. |
||
| return std::mdspan<value_type, std::dextents<size_t, N>>(data(), exts); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| } | ||
|
|
||
| template <size_t N> | ||
| std::mdspan<value_type const, std::dextents<size_t, N>> as_mdspan() const | ||
| { | ||
| if (ndim() != N) | ||
| { | ||
| throw std::out_of_range( | ||
| std::format("SimpleArray::as_mdspan: rank {} does not match ndim() {}", N, ndim())); | ||
| } | ||
| if (!is_c_contiguous()) | ||
| { | ||
| throw std::runtime_error("SimpleArray::as_mdspan: array is not C-contiguous"); | ||
| } | ||
| std::array<size_t, N> exts; | ||
| for (size_t i = 0; i < N; ++i) { exts[i] = shape(i); } | ||
| return std::mdspan<value_type const, std::dextents<size_t, N>>(data(), exts); | ||
| } | ||
|
|
||
| /* Backdoor */ | ||
| value_type const & data(size_t it) const { return data()[it]; } | ||
| value_type & data(size_t it) { return data()[it]; } | ||
|
|
@@ -2423,4 +2477,4 @@ class SimpleArrayPlex | |
|
|
||
| } /* end namespace modmesh */ | ||
|
|
||
| /* vim: set et ts=4 sw=4: */ | ||
| // vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GCC 14 and 15 with the ubuntu package did not have
mdspan.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although we may face CI error when ubuntu PPA dies again, it seems that GCC 16 is necessary.