36 using Extent = std::vector<IndexType>;
38 using Index = std::vector<IndexType>;
52 throw std::invalid_argument(
"Extents of wrong dimension");
59 template <
typename... Es>
64 throw std::invalid_argument(
"Extents of wrong dimension");
70 template <
typename InputIt>
75 throw std::invalid_argument(
"Extents of wrong dimension");
78 auto size = std::accumulate(
80 std::multiplies<IndexType>());
83 throw std::invalid_argument(
84 "Array extent does not match size of input data");
97 if (e.size() !=
dim_) {
98 throw std::invalid_argument(
"Extents of wrong dimension");
106 template <
typename... Es>
119 auto size() const -> std::
size_t {
return data_.size(); }
127 if (index.size() !=
dim_) {
128 throw std::invalid_argument(
"Index of wrong dimension");
136 if (index.size() !=
dim_) {
137 throw std::invalid_argument(
"Index of wrong dimension");
143 template <
typename... Is>
150 template <
typename... Is>
151 auto operator()(Is... indices)
const ->
const T&
159 auto offset = std::accumulate(
161 std::multiplies<IndexType>());
163 auto b = std::next(
data_.begin(), index * offset);
164 auto e = std::next(
data_.begin(), (index + 1) * offset);
180 auto data() ->
typename Container::value_type* {
return data_.data(); }
226 }
else if (dim > a.
dim_) {
227 throw std::invalid_argument(
"Dimension higher than that of array");
232 auto flattened = std::accumulate(
234 IndexType(1), std::multiplies<IndexType>());
235 newExtent.push_back(flattened);
238 newExtent.end(), std::next(a.
extents_.begin(), a.
dim_ - dim + 1),
256 auto size = std::accumulate(
258 std::multiplies<IndexType>());
261 throw std::range_error(
"Array extent is zero");
271 for (std::size_t it = 0; it <
extents_.size(); it++) {
273 auto offset = std::accumulate(
275 std::multiplies<IndexType>());
276 idx += i[it] * offset;
Dynamically-allocated N-Dimensional Array.
auto as_vector() -> Container
Return copy of raw data.
NDArray(std::size_t n)
Default constructor.
auto end() const -> const_iterator
Return an iterator that points to the past-the-end element in the array.
typename Container::const_iterator const_iterator
auto index_to_data_index_(Index i) const -> IndexType
auto data() -> typename Container::value_type *
Get a pointer to the start of the underlying data.
auto extents() const -> Extent
Get the extent (size) of the array's dimensions.
auto front() -> T &
Return a reference to the first element in the array.
auto front() const -> const T &
Return a reference to the first element in the array.
auto operator()(Index index) -> T &
Per-element access.
auto size() const -> std::size_t
Get the total number of elements in the array.
void setExtents(Extent e)
Set the extent of the array's dimensions.
NDArray(std::size_t n, Extent e)
Constructor with dimensions.
std::vector< IndexType > Index
std::vector< IndexType > Extent
typename Container::iterator iterator
auto back() -> T &
Return a reference to the last element in the array.
auto back() const -> const T &
Return a reference to the last element in the array.
std::vector< T > Container
auto begin() -> iterator
Return an iterator that points to the first element in the array.
auto dims() const -> std::size_t
Get the number of dimensions of the array.
auto begin() const -> const_iterator
Return an iterator that points to the first element in the array.
NDArray(std::size_t n, Extent e, InputIt first, InputIt last)
Constructor with range initialization.
auto end() -> iterator
Return an iterator that points to the past-the-end element in the array.
auto slice(IndexType index) -> NDArray
Get slice of array by dropping highest dimension.
static void Flatten(NDArray &a, std::size_t dim)
Flatten an array by dropping a dimension and appending the data to the next highest dimension.
typename Container::size_type IndexType
Volume Cartographer library