Skip to content

Make contiguous 1d arrays/views model std::ranges::contiguous_range#137

Open
Wentzell wants to merge 1 commit into
unstablefrom
contiguous_range_1d
Open

Make contiguous 1d arrays/views model std::ranges::contiguous_range#137
Wentzell wants to merge 1 commit into
unstablefrom
contiguous_range_1d

Conversation

@Wentzell

@Wentzell Wentzell commented Jun 17, 2026

Copy link
Copy Markdown
Member

This is a first draft to make 1-D contiguous nda types compliant with std::ranges::contiguous_range

Turn the rank-1 nda::array_iterator into a std::contiguous_iterator when
it traverses a contiguous, unit-stride chunk of memory, so that contiguous
1d arrays/views (array<T,1>, vector<T>, array_contiguous_view<T,1>) satisfy
std::ranges::contiguous_range and interoperate with the std::ranges
algorithms.

iterators.hpp:
- Add a `bool Contiguous` template parameter to array_iterator (defaulted to
  false). The rank-1 specialization exposes iterator_concept =
  contiguous_iterator_tag when Contiguous, else random_access_iterator_tag.
- value_type is now cv-unqualified (std::remove_cv_t<T>), operator-> returns a
  real pointer (T*), and operator[] is const. The latter two also make the
  strided rank-1 iterator a genuine std::random_access_iterator.

_impl_basic_array_view_common.hpp:
- Drive the flag with _is_contiguous_1d = (Rank == 1) and has_contiguous(...).
  The Rank == 1 restriction is essential: a multi-dimensional contiguous array
  collapses to a rank-1 iterator internally, but must not advertise as a 1d
  contiguous range or nda::get_rank (which tests contiguous_range first) would
  report 1 for it.

Since 1d arrays now model contiguous_range, guard the foreign-range overloads
with `not MemoryArray<R>` to avoid ambiguity / wrong-branch selection:
- operator==(array, range) / operator==(range, array) in basic_functions.hpp
- the contiguous_range CTAD deduction guide in basic_array_view.hpp
- the diag() contiguous_range branch in matrix_functions.hpp

Add test/c++/nda_ranges.cpp covering the new concept guarantees, the
deliberate exclusion of general/strided views and multi-dimensional arrays,
and that array equality and std::ranges algorithms still work.

Assisted-by: Claude <noreply@anthropic.com>
@Wentzell Wentzell requested a review from Thoemi09 June 17, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant