Fix nd_span iterators to respect strides of non-contiguous views#3
Fix nd_span iterators to respect strides of non-contiguous views#3
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3 +/- ##
=======================================
Coverage 97.92% 97.92%
=======================================
Files 3 3
Lines 1206 1206
=======================================
Hits 1181 1181
Misses 25 25
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: pingelit <22936321+pingelit@users.noreply.github.com>
Co-authored-by: pingelit <22936321+pingelit@users.noreply.github.com>
pingelit
left a comment
There was a problem hiding this comment.
This looks good, some minor changes to the iterator class would be nice to encourage reuse.
include/nd_array/nd_array.hpp
Outdated
| [[nodiscard]] pointer begin( ) noexcept { return m_data; } | ||
| /// \brief Stride-aware forward iterator for nd_span | ||
| /// \tparam IsConst Whether this is a const iterator | ||
| template<bool IsConst> |
There was a problem hiding this comment.
I think moving the iterator to the detail namespace would make sense here. That way, it could be reused more easily.
There was a problem hiding this comment.
Done in 29f04dd — the iterator is now detail::nd_iterator<ElementType, MaxRank> in the detail namespace, and nd_span exposes it via using iterator = detail::nd_iterator<Ty, MaxRank> and using const_iterator = detail::nd_iterator<const Ty, MaxRank>.
include/nd_array/nd_array.hpp
Outdated
| using iterator = basic_iterator<false>; ///< Mutable stride-aware iterator | ||
| using const_iterator = basic_iterator<true>; ///< Const stride-aware iterator |
There was a problem hiding this comment.
I think I prefer if the tempate argument would just be the underlying type, either const or non-const, not a bool.
There was a problem hiding this comment.
Done in 29f04dd — the template parameter is now the element type directly (template<typename ElementType, size_t MaxRank>), so nd_iterator<Ty, MaxRank> is the mutable iterator and nd_iterator<const Ty, MaxRank> is the const iterator. The bool IsConst + std::conditional_t machinery is gone.
…instead of bool IsConst Co-authored-by: pingelit <22936321+pingelit@users.noreply.github.com>
nd_span::begin()/end()returned raw pointers, iterating linearly over raw memory regardless of strides. This made range-based for loops and STL algorithms produce wrong results for subspans and transposed views (any view with non-unit or non-contiguous strides).Changes
include/nd_array/nd_array.hppdetail::nd_iterator<ElementType, MaxRank>stride-aware forward iterator template in thedetailnamespace, tracking multi-dimensional indices and computing element addresses asdata + Σ(indices[i] × strides[i])Tyfor mutable,const Tyfor const) rather than abool IsConstflag, making the iterator reusable by other classes in the same headeriteratortoconst_iteratoriterator/const_iteratortype aliases innd_spanpointing todetail::nd_iteratorbegin()/end()/cbegin()/cend()with stride-aware iterator versions#include <iterator>forstd::forward_iterator_tagtests/test_nd_span.cppAdded
"nd_span - Stride-aware iteration"test case covering:Example
Original prompt
nd_spaniterators do not consider the strides of subspans #2💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.