18namespace stratax::core {
69 template<
bool IsConst>
73 using view_type = std::conditional_t<IsConst, const ArrayView, ArrayView>;
75 view_type* view_ =
nullptr;
88 using reference = std::conditional_t<IsConst, const_reference, ArrayView::reference>;
94 : view_(view), index_(index)
98 template<
bool OtherConst>
99 requires (IsConst && !OtherConst)
101 : view_(other.view_), index_(other.index_)
160 return lhs.index_ <=> rhs.index_;
188 if (shape_.
rank() != strides_.
rank())
193 if (data_ ==
nullptr)
200 const auto extent = shape_[dim] - 1;
201 const auto limit = std::numeric_limits<size_type>::max();
202 if (extent != 0 && strides_[dim] > (limit - maximum_offset) / extent)
206 maximum_offset += extent * strides_[dim];
218 std::is_const_v<element_type> &&
219 !std::is_const_v<U> &&
220 std::same_as<value_type, std::remove_const_t<U>>
223 : data_(other.
data()),
224 shape_(other.
shape()),
253 return shape_.
rank();
294 return data_[flat_offset(index)];
300 return data_[flat_offset(index)];
324template<
typename... Rest>
325requires ((std::is_integral_v<Rest>) && ...)
328 constexpr size_type count =
sizeof...(Rest) + 1;
333 "The number of indices must match the view rank.");
336 const std::array<size_type, count> indices{
341 return data_[indexing::offset(strides_, indices)];
352template<
typename... Rest>
353requires ((std::is_integral_v<Rest>) && ...)
356 constexpr size_type count =
sizeof...(Rest) + 1;
361 "The number of indices must match the view rank.");
364 const std::array<size_type, count> indices{
369 return data_[indexing::offset(strides_, indices)];
378 index = indexing::normalize_index(index,
size());
379 return (*
this)[
static_cast<size_type>(index)];
388 index = indexing::normalize_index(index,
size());
389 return (*
this)[
static_cast<size_type>(index)];
399 return data_[checked_offset(indices)];
409 return data_[checked_offset(indices)];
420 const size_type coordinate = index % shape_[dim];
421 index /= shape_[dim];
422 offset += coordinate * strides_[dim];
429 const std::vector<difference_type>& indices)
const
431 if (indices.size() !=
rank())
434 "The number of indices must match the view rank.");
447 indexing::normalize_index(
Stratax exception types derived from std::runtime_error.
Unchecked offsets from element strides.
std::size_t offset(const core::Shape &strides, const IndexContainer &indices)
Computes an unchecked flat offset from strides and normalized indices.
Immutable dimension metadata and checked row-major strides.
Invalid dimension or overflow in dimension, element-count, or stride arithmetic.
Index or slice cannot be used for the requested access.
Number of axes or supplied multidimensional indices does not match the required rank.
Invalid argument value, such as an invalid shift count or null nonempty view.
Random-access iterator over logical flat indices of a view.
ArrayView::value_type value_type
Unqualified element type.
friend bool operator==(const basic_iterator &, const basic_iterator &)=default
Compares both the view identity and logical position.
std::random_access_iterator_tag iterator_category
Legacy random-access iterator category tag.
friend basic_iterator operator+(basic_iterator iterator, difference_type offset)
Returns an iterator advanced by a signed offset.
basic_iterator & operator--()
Moves back one position and returns this iterator.
friend auto operator<=>(const basic_iterator &lhs, const basic_iterator &rhs)
Orders logical positions within the same view.
basic_iterator(const basic_iterator< OtherConst > &other) noexcept
Converts a mutable iterator to a read-only iterator.
basic_iterator & operator+=(difference_type offset)
Advances by a signed offset and returns this iterator.
basic_iterator operator++(int)
Advances one position and returns the previous iterator.
friend basic_iterator operator+(difference_type offset, basic_iterator iterator)
Returns an iterator advanced by a signed offset.
std::conditional_t< IsConst, const_reference, ArrayView::reference > reference
Element reference with the iterator constness.
basic_iterator operator--(int)
Moves back one position and returns the previous iterator.
friend difference_type operator-(const basic_iterator &lhs, const basic_iterator &rhs)
Returns the signed logical distance between iterators into the same view.
reference operator*() const
Returns the element at the current logical position.
basic_iterator & operator-=(difference_type offset)
Moves back by a signed offset and returns this iterator.
reference operator[](difference_type offset) const
Returns the element at a relative logical offset.
std::ptrdiff_t difference_type
Signed iterator-distance type.
basic_iterator & operator++()
Advances one position and returns this iterator.
std::random_access_iterator_tag iterator_concept
C++20 random-access iterator concept tag.
basic_iterator()=default
Constructs a singular iterator.
basic_iterator(view_type *view, size_type index) noexcept
Constructs an iterator at a logical flat index, including the end position.
friend basic_iterator operator-(basic_iterator iterator, difference_type offset)
Returns an iterator moved back by a signed offset.
Non-owning view of elements with unsigned element strides.
const Shape & strides() const noexcept
Stored element strides.
ArrayView< const value_type > const_view
View type exposing read-only elements.
reference at(difference_type index)
Checked logical flat access with negative indices.
element_type * pointer
Element pointer with the constness of T.
const_iterator end() const noexcept
Past-the-end iterator.
T element_type
Element type including any const qualifier.
const_iterator begin() const noexcept
First logical-element iterator.
const_reference operator()(size_type first, Rest... rest) const
Accesses an element using multidimensional indices with rank checking only.
const_reference operator[](size_type index) const
Unchecked logical flat access.
ArrayView(const ArrayView< U > &other)
Creates a read-only alias of a mutable view, copying its metadata.
const Shape & shape() const noexcept
Logical extents.
const_pointer data() const noexcept
Read-only base pointer; empty views may have non-null pointers.
std::size_t size_type
Unsigned extent and offset type.
iterator end() noexcept
Past-the-end iterator.
const_iterator cend() const noexcept
Past-the-end iterator.
reference operator()(size_type first, Rest... rest)
Accesses an element using multidimensional indices with rank checking only.
element_type & reference
Element reference, mutable only when T is mutable.
std::remove_const_t< element_type > value_type
Unqualified element type.
size_type size() const noexcept
Number of logical elements, zero for rank zero.
const_reference at(difference_type index) const
Checked logical flat access with negative indices.
basic_iterator< true > const_iterator
Read-only random-access iterator in logical order.
reference operator[](size_type index)
Unchecked logical flat access.
ArrayView(pointer data, const Shape &shape, const Shape &strides)
Constructs a view after validating shape and stride metadata.
reference at(const std::vector< difference_type > &indices)
Checked multidimensional access with negative indices.
bool empty() const noexcept
Whether there are no logical elements.
const value_type & const_reference
Read-only element reference.
const value_type * const_pointer
Read-only element pointer.
size_type ndim() const noexcept
Alias for rank().
std::ptrdiff_t difference_type
Signed index and iterator-distance type.
const_view as_const() const
Read-only alias with copied metadata.
pointer data() noexcept
Base storage pointer; empty views may have non-null pointers.
const_reference at(const std::vector< difference_type > &indices) const
Checked multidimensional access with negative indices.
iterator begin() noexcept
First logical-element iterator.
basic_iterator< false > iterator
Mutable-view random-access iterator in logical order.
const_iterator cbegin() const noexcept
First logical-element iterator.
size_type rank() const noexcept
Number of logical axes.
Stores the dimensions of a multidimensional array.
size_type elements() const
Computes the total number of elements described by the shape.
size_type rank() const noexcept
Returns the number of dimensions.