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❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3 +/- ##
==========================================
+ Coverage 97.92% 97.97% +0.04%
==========================================
Files 3 3
Lines 1206 1382 +176
==========================================
+ Hits 1181 1354 +173
- Misses 25 28 +3
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.
…instead of bool IsConst Co-authored-by: pingelit <22936321+pingelit@users.noreply.github.com>
pingelit
left a comment
There was a problem hiding this comment.
I think it would be best if the iterator were random access or at least bidirectional. Also, test that the iterator is correctly implemented through a static_assert.
…gory tests Co-authored-by: pingelit <22936321+pingelit@users.noreply.github.com>
Done in c5bf0a1. |
pingelit
left a comment
There was a problem hiding this comment.
Great, this address all my ideas.
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 random access iterator template in thedetailnamespace, tracking a flat linear index 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 headeriterator_categoryisstd::random_access_iterator_tag; full random access interface:operator--(pre/post),operator+=/-=,operator+/-,operator[],operator</>/<=/>=, and iterator differenceoperator-iteratortoconst_iteratoriterator/const_iteratortype aliases innd_spanpointing todetail::nd_iteratorbegin()/end()/cbegin()/cend()with stride-aware iterator versions;end()/cend()passsize()so the end iterator has a well-defined flat index for arithmetic#include <iterator>for iterator support typestests/test_nd_span.cppstatic_assertchecks verifying thatnd_span<int>::iteratorandnd_span<int>::const_iteratorhaveiterator_category == std::random_access_iterator_tag"nd_span - Stride-aware iteration"test case covering:operator[],operator+/-,operator+=/-=, decrement, iterator difference, ordering)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.