1#ifndef WXM_MULTI_ITERATOR_H
2#define WXM_MULTI_ITERATOR_H
18 std::array<size_t, max_dims> idcs;
20 std::array<size_t, max_dims> shape_;
36 template<
class... Idcs,
37 class =
typename std::enable_if<
40 : idcs{0}, shape_{static_cast<size_t>(lengths)...}, ndims_(sizeof...(Idcs))
43 sizeof...(Idcs) < max_dims,
44 "Number of dimensions for the multi-iterator cannot exceed max_dims");
46 for (
size_t i = 0; i < ndims_; ++i)
60 std::copy(cont.begin(), cont.end(), shape_.begin());
62 for (
size_t i = 0; i < ndims_; ++i)
72 const std::array<size_t, max_dims>&
indices()
const
105 const std::array<size_t, max_dims>&
shape()
const
117 for (
size_t i = ndims_ - 1; i > 0; --i)
119 if (idcs[i] >= shape_[i])
136 idcs[ndims_ - 1] += val;
137 bool no_carry =
false;
138 for (
size_t i = ndims_ - 1; i > 0 && !no_carry; --i)
141 while (idcs[i] >= shape_[i])
143 idcs[i] -= shape_[i];
159 for (
size_t i = dim + 1; i < ndims_; ++i)
165 for (
size_t i = dim; i > 0; --i)
167 if (idcs[i] >= shape_[i])
185 for (
size_t i = dim + 1; i < ndims_; ++i)
191 bool no_carry =
false;
192 for (
size_t i = dim; i > 0 && !no_carry; --i)
195 while (idcs[i] >= shape_[i])
197 idcs[i] -= shape_[i];
A multi-dimensional iterator Stores the index in each dimension, as well as a global flattened index.
Definition: multi_iterator.h:17
multi_iterator & advance(size_t dim)
Increment the multi iterator in the given dimension.
Definition: multi_iterator.h:154
multi_iterator & operator+=(size_t val)
Increment the multi iterator by val.
Definition: multi_iterator.h:133
multi_iterator & advance(size_t dim, size_t val)
Increment the multi iterator by val in the given dimension.
Definition: multi_iterator.h:181
const std::array< size_t, max_dims > & shape() const
Gets the multi-dimensional indices.
Definition: multi_iterator.h:105
multi_iterator(Idcs... lengths)
Construct a multi-iterator from a list of lengths.
Definition: multi_iterator.h:39
size_t ndims() const
number of dimensions
Definition: multi_iterator.h:96
multi_iterator & operator++()
Increment the multi iterator.
Definition: multi_iterator.h:113
multi_iterator(const C &cont)
Construct a multi-iterator from a container which contains the lengths.
Definition: multi_iterator.h:57
size_t size() const
Total number of elements.
Definition: multi_iterator.h:88
size_t index() const
Returns the flattened global index.
Definition: multi_iterator.h:80
const std::array< size_t, max_dims > & indices() const
Gets the multi-dimensional indices.
Definition: multi_iterator.h:72
Base namespace for everything not included in the global namespace.
Definition: field_source.h:8