Visual Computing Library
|
The Array class is a dynamically allocated N-dimensional array stored in RowWise mode. More...
#include <vclib/space/core/array.h>
Public Types | |
using | ValueType = std::vector< T >::value_type |
The type of the elements stored in the array. | |
using | Scalar = ValueType |
Same of ValueType, just for compatibility with Eigen Matrices. | |
using | ConstReference = std::vector< T >::const_reference |
A const reference to the type of the elements stored in the array. | |
using | Reference = std::vector< T >::reference |
A reference to the type of the elements stored in the array. | |
using | ConstPointer = std::vector< T >::const_pointer |
A const pointer to the type of the elements stored in the array. | |
using | Pointer = std::vector< T >::pointer |
A pointer to the type of the elements stored in the array. | |
using | Iterator = std::vector< T >::iterator |
An iterator to the elements of the array. | |
using | ConstIterator = std::vector< T >::const_iterator |
A const iterator to the elements of the array. | |
Public Member Functions | |
Array () | |
Default constructor for the Array class. | |
template<typename... Sizes> requires (sizeof...(s) == N) | |
Array (Sizes... s) | |
Constructor for the Array class that creates an N-dimensional array with the given sizes. All its elements are initialized to 0. | |
Array (NestedInitializerLists< T, N > values) | |
Creates and initializes an N-dimensional array. Sizes are given by the maximum size of the initializer lists for every dimension, and missing values are automatically set to zero. | |
bool | empty () const |
Checks whether the array is empty. | |
std::size_t | size (std::size_t dim) const |
Returns the size of the given dimension. | |
std::size_t | rows () const |
Returns the number of rows of a 2-dimensional array. | |
std::size_t | cols () const |
Returns the number of columns of a 2-dimensional array. | |
std::size_t | sizeX () const |
Returns the size of the X dimension of the array. | |
std::size_t | sizeY () const |
Returns the size of the Y dimension of the array. | |
std::size_t | sizeZ () const |
Returns the size of the Z dimension of the array. | |
std::size_t | sizeW () const |
Returns the size of the W dimension of the array. | |
template<typename... I> requires (sizeof...(indices) == N) | |
Reference | operator() (I... indices) |
Operator () that allows to access one element of the array. It can be used as left or right value. | |
template<typename... I> requires (sizeof...(indices) == N) | |
ConstReference | operator() (I... indices) const |
Operator () that allows to access one element of the array. It can be used only as right value. | |
template<typename... I> requires (sizeof...(indices) < N) | |
Pointer | data (I... indices) |
Allows to get the data of the Array, through a pointer to the first element. | |
template<typename... I> requires (sizeof...(indices) < N) | |
ConstPointer | data (I... indices) const |
Allows to get the data of the Array, through a pointer to the first element. | |
std::vector< T > | stdVector () & |
Returns a std::vector containing the elements of the array in row-major order. | |
std::vector< T > && | stdVector () && |
Returns a std::vector containing the elements of the array in row-major order. | |
const std::vector< T > & | stdVector () const & |
Returns a std::vector containing the elements of the array in row-major order. | |
void | fill (const T &t) |
Fills the entire Array with the value t. | |
template<Range Rng> | |
void | fill (Rng &&r) |
Fills the entire Array with the values contained in the range r, in row-major order. | |
template<typename... Sizes> requires (sizeof...(s) == N) | |
void | resize (Sizes... s) |
Allows to resize the Array, not conserving the values of the previous array. | |
template<typename... Sizes> requires (sizeof...(s) == N) | |
void | conservativeResize (Sizes... s) |
Allows to resize the Array, conserving the values of the previous array. | |
void | clear () |
Clear the entire array, setting every dimension to size 0. | |
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. | |
void | serialize (std::ostream &os) const |
void | deserialize (std::istream &is) |
Iterator | begin () |
Returns an iterator to the beginning of the array. | |
Iterator | end () |
Returns an iterator to the end of the array. | |
ConstIterator | begin () const |
Returns a const iterator to the beginning of the array. | |
ConstIterator | end () const |
Returns a const iterator to the end of the array. | |
Static Public Attributes | |
static constexpr uint | DIM = N |
The number of dimensions of the array. | |
Private Member Functions | |
std::size_t | getIndex (const std::size_t indices[]) const |
std::array< std::size_t, N > | reverseIndex (uint index) |
void | initializeNestedLists (NestedInitializerLists< T, N > values) |
Static Private Member Functions | |
static std::size_t | getIndex (const std::size_t indices[], const std::size_t sizes[]) |
Private Attributes | |
friend | Array< T, N+1 > |
std::array< std::size_t, N > | mSizes |
std::vector< T > | mVec |
Friends | |
template<typename S > | |
std::ostream & | operator<< (std::ostream &out, const Array< S, 2 > &a) |
The Array class is a dynamically allocated N-dimensional array stored in RowWise mode.
This class is suitable for storing multi-dimensional data that has a fixed size, as it provides efficient access to its elements. All the elements of the array are stored contiguously in memory, so the access to them using the ()
operator is faster than accessing them through nested vectors.
The size of the array is specified at compile time via the template parameter N
, which indicates the number of dimensions of the array. The size of each dimension can be specified at runtime either on initialization or by calling the resize
or conservativeResize
member functions.
For one-dimensional arrays, it is recommended to use std::vector
or std::array
, as they are more efficient.
T | The type of the elements stored in the array. |
N | The number of dimensions of the array. |
|
inline |
Default constructor for the Array class.
Creates an N-dimensional array with size 0 for every dimension.
|
inline |
Constructor for the Array class that creates an N-dimensional array with the given sizes. All its elements are initialized to 0.
Sizes | Variadic parameter pack containing N sizes, one for every dimension of the array. |
[in] | s | Variadic parameter pack containing N sizes, one for every dimension of the array. |
|
inline |
Creates and initializes an N-dimensional array. Sizes are given by the maximum size of the initializer lists for every dimension, and missing values are automatically set to zero.
Example code:
This code initializes a 3x4 2D array with the following values:
[in] | values | The nested initializer lists of values. |
|
inline |
Returns an iterator to the beginning of the array.
|
inline |
Returns a const iterator to the beginning of the array.
|
inline |
Returns the number of columns of a 2-dimensional array.
|
inline |
Allows to resize the Array, conserving the values of the previous array.
The new array will have the specified sizes if possible, but if any dimension is smaller than the previous size, then the values in those dimensions will be conserved.
Sizes | Types of the arguments representing the new sizes of the Array. |
[in] | s | N elements representing the new sizes of the Array. |
|
inline |
Allows to get the data of the Array, through a pointer to the first element.
The function also allows to get the pointer of a specific position in the array.
Example:
I | Types of the indices used to access a subarray of the array. |
[in] | indices | A number of indices that is less than the number of dimensions of the array. |
|
inline |
Allows to get the data of the Array, through a pointer to the first element.
The function also allows to get the pointer of a specific position in the array.
Example:
I | Types of the indices used to access a subarray of the array. |
[in] | indices | A number of indices that is less than the number of dimensions of the array. |
|
inline |
Checks whether the array is empty.
true
if the array is empty, false
otherwise.
|
inline |
Returns an iterator to the end of the array.
|
inline |
Returns a const iterator to the end of the array.
|
inline |
Fills the entire Array with the value t.
[in] | t | The value to fill the array with. |
Fills the entire Array with the values contained in the range r, in row-major order.
If the size of the container is greater than the total size of the array, the remaining elements of the container will be ignored. If the size of the container is less than the total size of the array, the remaining values in the array will be left unchanged.
Rng | Type of the range of values to fill the array with. It must satisfy the Range concept. |
|
inline |
Operator () that allows to access one element of the array. It can be used as left or right value.
I | Types of the indices used to access the element of the array. |
[in] | indices | N indices that allow to access an element of the array. A number of indices not equal to N will generate a compilation error. |
|
inline |
Operator () that allows to access one element of the array. It can be used only as right value.
I | Types of the indices used to access the element of the array. |
[in] | indices | N indices that allow to access an element of the array. A number of indices not equal to N will generate a compilation error. |
|
inline |
|
inline |
Returns the number of rows of a 2-dimensional array.
|
inline |
Returns the size of the given dimension.
[in] | dim | The dimension to query the size of. |
|
inline |
Returns the size of the W dimension of the array.
|
inline |
Returns the size of the X dimension of the array.
|
inline |
Returns the size of the Y dimension of the array.
|
inline |
Returns the size of the Z dimension of the array.
|
inline |
Returns a std::vector containing the elements of the array in row-major order.
|
inline |
Returns a std::vector containing the elements of the array in row-major order.
|
inline |
Returns a std::vector containing the elements of the array in row-major order.
|
inline |
Creates a new subArray of dimension N-1, starting from the given index at its first dimension.
The new subArray has the same type as the original array, but one dimension less, and contains the elements of the original array starting from the given index at its first dimension.
Example:
r | Index at the first dimension to start the sub-array. |