52 grid_iterator<Rank - 1> it_begin;
55 grid_iterator<Rank - 1> it_end;
58 grid_iterator<Rank - 1> it;
61 using iterator_category = std::forward_iterator_tag;
62 using value_type = long;
63 using difference_type = std::ptrdiff_t;
64 using pointer =
long *;
65 using reference =
long &;
68 grid_iterator() =
default;
72 grid_iterator(
long const *lengths,
long const *strides,
bool at_end)
74 pos(at_end ? lengths[0] : 0),
76 it_begin(lengths + 1, strides + 1, false),
77 it_end(lengths + 1, strides + 1, true),
84 [[nodiscard]]
long operator*()
const {
return offset + *it; }
87 [[nodiscard]]
long operator->()
const {
return operator*(); }
90 [[nodiscard]]
bool operator==(grid_iterator
const &rhs)
const {
return ((rhs.pos == pos) and (rhs.it == it)); }
93 [[nodiscard]]
bool operator!=(grid_iterator
const &rhs)
const {
return not operator==(rhs); }
96 grid_iterator &operator++() {
110 grid_iterator operator++(
int) {
119 class grid_iterator<1> {
130 using iterator_category = std::forward_iterator_tag;
131 using value_type = long;
132 using difference_type = std::ptrdiff_t;
133 using pointer =
long *;
134 using reference =
long &;
137 grid_iterator() =
default;
141 grid_iterator(
long const *lengths,
long const *strides,
bool at_end) : stri(strides[0]), pos(at_end ? lengths[0] : 0), offset(pos * stri) {}
144 [[nodiscard]] std::array<long, 1> indices() {
return {pos}; }
147 [[nodiscard]]
long operator*()
const {
return offset; }
150 [[nodiscard]]
long operator->()
const {
return operator*(); }
153 [[nodiscard]]
bool operator==(grid_iterator
const &rhs)
const {
return (rhs.pos == pos); }
156 [[nodiscard]]
bool operator!=(grid_iterator
const &rhs)
const {
return (rhs.pos != pos); }
159 grid_iterator &operator++() {
166 grid_iterator &operator--() {
173 grid_iterator &operator+=(std::ptrdiff_t n) {
180 [[nodiscard]]
friend grid_iterator
operator+(grid_iterator it, std::ptrdiff_t n) {
return it += n; }
183 [[nodiscard]]
friend std::ptrdiff_t
operator-(grid_iterator
const &lhs, grid_iterator
const &rhs) {
return lhs.pos - rhs.pos; }
187 [[nodiscard]]
friend bool operator<(grid_iterator
const &lhs, grid_iterator
const &rhs) {
return lhs.pos < rhs.pos; }
191 [[nodiscard]]
friend bool operator>(grid_iterator
const &lhs, grid_iterator
const &rhs) {
return lhs.pos > rhs.pos; }
214 template <
int Rank,
typename T,
typename Po
inter>
220 std::array<long, Rank> len;
223 std::array<long, Rank> stri;
226 detail::grid_iterator<Rank> iter;
256 array_iterator(std::array<long, Rank>
const &lengths, std::array<long, Rank>
const &strides, T *start,
bool at_end)
257 : data(start), len(lengths), stri(strides), iter(len.data(), stri.data(), at_end) {}
263 [[nodiscard]]
auto indices() {
return iter.indices(); }
320 template <
typename T,
typename Po
inter>
326 std::array<long, 1> len{};
329 std::array<long, 1> stri{};
332 detail::grid_iterator<1> iter;
362 array_iterator(std::array<long, 1>
const &lengths, std::array<long, 1>
const &strides, T *start,
bool at_end)
363 : data(start), len(lengths), stri(strides), iter(len.data(), stri.data(), at_end) {}
369 [[nodiscard]]
auto indices() {
return iter.indices(); }
375 [[nodiscard]] T &
operator*()
const {
return ((Pointer)data)[*iter]; }
499 [[nodiscard]] T &
operator[](std::ptrdiff_t n) {
return ((Pointer)data)[*(iter + n)]; }
Provides utility functions for std::array.
friend std::ptrdiff_t operator-(array_iterator const &lhs, array_iterator const &rhs)
Binary subtraction of two 1-dimensional array iterators.
friend bool operator>=(array_iterator const &lhs, array_iterator const &rhs)
Greater-than or equal-to comparison operator for two 1-dimensional array iterators.
friend bool operator<=(array_iterator const &lhs, array_iterator const &rhs)
Less-than or equal-to comparison operator for two 1-dimensional array iterators.
array_iterator & operator--()
Prefix decrement operator.
friend array_iterator operator+(std::ptrdiff_t n, array_iterator it)
Binary addition of an integer with an 1-dimensional array iterator.
array_iterator()=default
Default constructor leaves the iterator in an uninitialized state.
friend bool operator>(array_iterator const &lhs, array_iterator const &rhs)
Greater-than comparison operator for two 1-dimensional array iterators.
T & operator[](std::ptrdiff_t n)
Subscript operator.
T & reference
Reference type.
bool operator!=(array_iterator const &rhs) const
Not-equal-to operator.
array_iterator & operator++()
Prefix increment operator.
array_iterator & operator+=(std::ptrdiff_t n)
Compound assignment addition operator increments the iterator a given number of times.
array_iterator operator--(int)
Postfix decrement operator.
T & operator*() const
Dereference operator.
array_iterator operator++(int)
Postfix increment operator.
friend array_iterator operator-(array_iterator it, std::ptrdiff_t n)
Binary subtraction of an 1-dimensional array iterator with an integer.
array_iterator & operator-=(std::ptrdiff_t n)
Compound assignment subtraction operator decrements the iterator a given number of times.
array_iterator(std::array< long, 1 > const &lengths, std::array< long, 1 > const &strides, T *start, bool at_end)
Construct an iterator from the shape and the strides of an array/view, a pointer to its data and a fl...
bool operator==(array_iterator const &rhs) const
Equal-to operator.
friend array_iterator operator+(array_iterator it, std::ptrdiff_t n)
Binary addition of an 1-dimensional array iterator with an integer.
friend bool operator<(array_iterator const &lhs, array_iterator const &rhs)
Less-than comparison operator for two 1-dimensional array iterators.
T & operator->() const
Member access operator.
std::ptrdiff_t difference_type
Difference type.
std::random_access_iterator_tag iterator_category
Iterator category.
auto indices()
Get the current position/index of the iterator.
Iterator for nda::basic_array and nda::basic_array_view types.
value_type & operator->() const
Member access operator.
array_iterator(std::array< long, Rank > const &lengths, std::array< long, Rank > const &strides, T *start, bool at_end)
Construct an iterator from the shape and the strides of an array/view, a pointer to its data and a fl...
std::ptrdiff_t difference_type
Difference type.
array_iterator operator++(int)
Postfix increment operator.
array_iterator()=default
Default constructor leaves the iterator in an uninitialized state.
std::forward_iterator_tag iterator_category
Iterator category.
T & reference
Reference type.
bool operator==(array_iterator const &rhs) const
Equal-to operator.
bool operator!=(array_iterator const &rhs) const
Not-equal-to operator.
value_type & operator*() const
Dereference operator.
auto indices()
Get the current position/multi-dimensional index of the iterator.
array_iterator & operator++()
Prefix increment operator.
Array auto operator+(L &&l, R &&r)
Addition operator for two nda::Array types.
expr_unary<'-', A > operator-(A &&a)
Unary minus operator for nda::Array types.
__inline__ auto operator<(L &&l, R &&r)
Implementation of the lazy binary < operation.
__inline__ auto operator>(L &&l, R &&r)
Implementation of the lazy binary > operation.
constexpr std::array< T, R+1 > front_append(std::array< T, R > const &a, U const &x)
Make a new std::array by prepending one element at the front to an existing std::array.