23#ifndef VCL_SPACE_CORE_ARRAY_H
24#define VCL_SPACE_CORE_ARRAY_H
26#include <vclib/concepts/ranges/range.h>
27#include <vclib/concepts/space/array.h>
28#include <vclib/io/serialization.h>
29#include <vclib/misc/nested_initializer_lists.h>
30#include <vclib/types.h>
62template<
class T, u
int N>
65 static_assert(N > 0,
"Array dimension must be > 0.");
68 std::array<std::size_t, N> mSizes;
101 static constexpr uint
DIM = N;
108 Array() : mVec(0) { mSizes.fill(0); }
123 template<
typename...
Sizes>
126 std::size_t
args[N] = {
static_cast<std::size_t
>(s)...};
128 for (uint
i = 0;
i < N;
i++) {
159 initializeNestedLists(
values);
167 bool empty()
const {
return mVec.empty(); }
176 std::size_t
size(std::size_t dim)
const
252 template<
typename... I>
255 std::size_t
args[N] = {
static_cast<std::size_t
>(indices)...};
256 return mVec[getIndex(
args)];
271 template<
typename... I>
273 requires (
sizeof...(indices) == N)
275 std::size_t
args[N] = {
static_cast<std::size_t
>(indices)...};
276 return mVec[getIndex(
args)];
307 template<
typename... I>
310 return const_cast<Pointer>(std::as_const(*this).data(indices...));
341 template<
typename... I>
344 constexpr std::size_t
n =
sizeof...(indices);
345 if constexpr (
n == 0) {
349 std::size_t
args[] = {
static_cast<std::size_t
>(indices)...};
353 for (
i = 1;
i <
n;
i++) {
381 std::vector<T>&&
stdVector() && {
return std::move(mVec); }
397 void fill(
const T& t) { std::fill(mVec.begin(), mVec.end(), t); }
418 for (
auto it = std::ranges::begin(
r);
419 it != std::ranges::end(
r) &&
i < mVec.size();
434 template<
typename...
Sizes>
437 std::size_t
args[N] = {
static_cast<std::size_t
>(s)...};
439 for (uint
i = 0;
i < N;
i++) {
459 template<
typename...
Sizes>
462 std::size_t
newSizes[N] = {
static_cast<std::size_t
>(s)...};
464 for (uint
i = 0;
i < N;
i++)
468 for (std::size_t
i = 0;
i < mVec.size();
i++) {
469 std::array<std::size_t, N> indices = reverseIndex(
i);
471 for (std::size_t
j = 0;
j < N;
j++)
472 if (indices[
j] >=
newSizes[
j] || indices[
j] >= mSizes[
j])
479 for (uint
i = 0;
i < mSizes.size();
i++) {
492 for (uint
i = 0;
i < N;
i++)
524 for (uint
i = 0;
i < mSizes.size() - 1;
i++) {
525 sub.mSizes[
i] = mSizes[
i + 1];
528 sub.mVec = std::vector<T>(
529 mVec.begin() +
r *
size, mVec.begin() + (
r + 1) *
size);
533 void serialize(std::ostream&
os)
const
535 vcl::serialize(
os, mSizes);
536 vcl::serialize(
os, mVec);
539 void deserialize(std::istream& is)
541 vcl::deserialize(is, mSizes);
542 vcl::deserialize(is, mVec);
578 std::size_t getIndex(
const std::size_t indices[])
const
580 std::size_t
ind = indices[0];
581 assert(indices[0] < mSizes[0]);
582 for (uint
i = 1;
i < N;
i++) {
590 std::array<std::size_t, N> reverseIndex(uint index)
592 std::array<std::size_t, N> indices;
593 for (
long int i = N - 1; i >= 0; i--) {
594 indices[i] = index % mSizes[i];
600 static std::size_t getIndex(
601 const std::size_t indices[],
602 const std::size_t sizes[])
604 std::size_t ind = indices[0];
605 assert(indices[0] < sizes[0]);
606 for (uint i = 1; i < N; i++) {
607 assert(indices[i] < sizes[i]);
614 void initializeNestedLists(NestedInitializerLists<T, N> values)
616 std::list<std::size_t> szs =
620 size_t totalSize = 1;
621 for (std::size_t s : szs) {
625 mVec.resize(totalSize);
627 typename std::vector<T>::iterator iterator = mVec.begin();
630 [&iterator](T value) {
631 *(iterator++) = value;
637template<
typename Scalar>
638std::ostream& operator<<(std::ostream& out,
const Array<Scalar, 2>& a)
640 for (uint i = 0; i < a.sizeX(); i++) {
641 for (uint j = 0; j < a.sizeY(); j++) {
642 out << std::setw(4) << a(i, j) <<
" ";
661template<
typename Scalar>
674template<
typename Scalar>
687template<
typename Scalar>
The Array class is a dynamically allocated N-dimensional array stored in RowWise mode.
Definition array.h:64
std::vector< T > stdVector() &
Returns a std::vector containing the elements of the array in row-major order.
Definition array.h:372
Array()
Default constructor for the Array class.
Definition array.h:108
std::vector< T >::const_pointer ConstPointer
A const pointer to the type of the elements stored in the array.
Definition array.h:89
ConstIterator end() const
Returns a const iterator to the end of the array.
Definition array.h:571
std::size_t sizeZ() const
Returns the size of the Z dimension of the array.
Definition array.h:228
Reference operator()(I... indices)
Operator () that allows to access one element of the array. It can be used as left or right value.
Definition array.h:253
ConstReference operator()(I... indices) const
Operator () that allows to access one element of the array. It can be used only as right value.
Definition array.h:272
Iterator end()
Returns an iterator to the end of the array.
Definition array.h:557
std::size_t size(std::size_t dim) const
Returns the size of the given dimension.
Definition array.h:176
ValueType Scalar
Same of ValueType, just for compatibility with Eigen Matrices.
Definition array.h:76
Pointer data(I... indices)
Allows to get the data of the Array, through a pointer to the first element.
Definition array.h:308
Iterator begin()
Returns an iterator to the beginning of the array.
Definition array.h:550
std::vector< T >::const_reference ConstReference
A const reference to the type of the elements stored in the array.
Definition array.h:81
Array< T, N - 1 > subArray(uint r) const
Creates a new subArray of dimension N-1, starting from the given index at its first dimension.
Definition array.h:519
void resize(Sizes... s)
Allows to resize the Array, not conserving the values of the previous array.
Definition array.h:435
void fill(const T &t)
Fills the entire Array with the value t.
Definition array.h:397
std::size_t cols() const
Returns the number of columns of a 2-dimensional array.
Definition array.h:198
static constexpr uint DIM
The number of dimensions of the array.
Definition array.h:101
std::vector< T > && stdVector() &&
Returns a std::vector containing the elements of the array in row-major order.
Definition array.h:381
std::size_t rows() const
Returns the number of rows of a 2-dimensional array.
Definition array.h:189
std::vector< T >::iterator Iterator
An iterator to the elements of the array.
Definition array.h:95
std::vector< T >::value_type ValueType
The type of the elements stored in the array.
Definition array.h:73
void fill(Rng &&r)
Fills the entire Array with the values contained in the range r, in row-major order.
Definition array.h:415
std::size_t sizeW() const
Returns the size of the W dimension of the array.
Definition array.h:238
std::size_t sizeY() const
Returns the size of the Y dimension of the array.
Definition array.h:218
std::vector< T >::pointer Pointer
A pointer to the type of the elements stored in the array.
Definition array.h:92
bool empty() const
Checks whether the array is empty.
Definition array.h:167
void conservativeResize(Sizes... s)
Allows to resize the Array, conserving the values of the previous array.
Definition array.h:460
std::size_t sizeX() const
Returns the size of the X dimension of the array.
Definition array.h:208
ConstPointer data(I... indices) const
Allows to get the data of the Array, through a pointer to the first element.
Definition array.h:342
Array(NestedInitializerLists< T, N > values)
Creates and initializes an N-dimensional array. Sizes are given by the maximum size of the initialize...
Definition array.h:157
std::vector< T >::const_iterator ConstIterator
A const iterator to the elements of the array.
Definition array.h:98
Array(Sizes... s)
Constructor for the Array class that creates an N-dimensional array with the given sizes....
Definition array.h:124
ConstIterator begin() const
Returns a const iterator to the beginning of the array.
Definition array.h:564
void clear()
Clear the entire array, setting every dimension to size 0.
Definition array.h:489
std::vector< T >::reference Reference
A reference to the type of the elements stored in the array.
Definition array.h:84
const std::vector< T > & stdVector() const &
Returns a std::vector containing the elements of the array in row-major order.
Definition array.h:390
static std::list< size_t > maxDimensionsLevels(NestedInitializerLists< T, L > values)
Returns a list containing the maximum size of elements for every dimension.
Definition nested_initializer_lists.h:103
static void processElements(NestedInitializerLists< T, L > values, T_Function function)
Applies the lambda function passed as parameter to all the elements of the NestedInitializerLists.
Definition nested_initializer_lists.h:155
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43