Visual Computing Library
Loading...
Searching...
No Matches
vcl::Array< T, N > Class Template Reference

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)
 

Detailed Description

template<class T, uint N>
class vcl::Array< T, N >

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.

Template Parameters
TThe type of the elements stored in the array.
NThe number of dimensions of the array.

Constructor & Destructor Documentation

◆ Array() [1/3]

template<class T , uint N>
vcl::Array< T, N >::Array ( )
inline

Default constructor for the Array class.

Creates an N-dimensional array with size 0 for every dimension.

◆ Array() [2/3]

template<class T , uint N>
template<typename... Sizes>
requires (sizeof...(s) == N)
vcl::Array< T, N >::Array ( Sizes...  s)
inline

Constructor for the Array class that creates an N-dimensional array with the given sizes. All its elements are initialized to 0.

Template Parameters
SizesVariadic parameter pack containing N sizes, one for every dimension of the array.
Parameters
[in]sVariadic parameter pack containing N sizes, one for every dimension of the array.
Note
The number of arguments must match the number of dimensions of the array.

◆ Array() [3/3]

template<class T , uint N>
vcl::Array< T, N >::Array ( NestedInitializerLists< T, N >  values)
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:

Array<int, 2> array = { {1,2,3,4}, {5}, {9, 10} };
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43

This code initializes a 3x4 2D array with the following values:

1 2 3 4
5 0 0 0
9 10 0 0
Parameters
[in]valuesThe nested initializer lists of values.
Note
The number of levels of the nested initializer lists must correspond to the number of dimensions of the array.

Member Function Documentation

◆ begin() [1/2]

template<class T , uint N>
Iterator vcl::Array< T, N >::begin ( )
inline

Returns an iterator to the beginning of the array.

Returns
Iterator to the beginning of the array.

◆ begin() [2/2]

template<class T , uint N>
ConstIterator vcl::Array< T, N >::begin ( ) const
inline

Returns a const iterator to the beginning of the array.

Returns
Const iterator to the beginning of the array.

◆ cols()

template<class T , uint N>
std::size_t vcl::Array< T, N >::cols ( ) const
inline

Returns the number of columns of a 2-dimensional array.

Returns
The number of columns of the array.
Note
This function can only be called for 2-dimensional arrays.

◆ conservativeResize()

template<class T , uint N>
template<typename... Sizes>
requires (sizeof...(s) == N)
void vcl::Array< T, N >::conservativeResize ( Sizes...  s)
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.

Template Parameters
SizesTypes of the arguments representing the new sizes of the Array.
Parameters
[in]sN elements representing the new sizes of the Array.

◆ data() [1/2]

template<class T , uint N>
template<typename... I>
requires (sizeof...(indices) < N)
Pointer vcl::Array< T, N >::data ( I...  indices)
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:

Array<int, 3> array(10, 13, 4);
//...
int* carray = array.data(3); // carray will point to the element in
// position (3, 0, 0).
carray = array.data(5, 2); // carray will point to the element in
// position (5, 2, 0).
carray = array.data(); // carray will point to the element in position
// (0, 0, 0).
Template Parameters
ITypes of the indices used to access a subarray of the array.
Parameters
[in]indicesA number of indices that is less than the number of dimensions of the array.
Returns
A pointer to the requested subarray.

◆ data() [2/2]

template<class T , uint N>
template<typename... I>
requires (sizeof...(indices) < N)
ConstPointer vcl::Array< T, N >::data ( I...  indices) const
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:

Array<int, 3> array(10, 13, 4);
//...
const int* carray = array.data(3); // carray will point to the element in
// position (3, 0, 0).
carray = array.data(5, 2); // carray will point to the element in
// position (5, 2, 0).
carray = array.data(); // carray will point to the element in position
// (0, 0, 0).
Template Parameters
ITypes of the indices used to access a subarray of the array.
Parameters
[in]indicesA number of indices that is less than the number of dimensions of the array.
Returns
A const pointer to the requested subarray.

◆ empty()

template<class T , uint N>
bool vcl::Array< T, N >::empty ( ) const
inline

Checks whether the array is empty.

Returns
true if the array is empty, false otherwise.

◆ end() [1/2]

template<class T , uint N>
Iterator vcl::Array< T, N >::end ( )
inline

Returns an iterator to the end of the array.

Returns
Iterator to the end of the array.

◆ end() [2/2]

template<class T , uint N>
ConstIterator vcl::Array< T, N >::end ( ) const
inline

Returns a const iterator to the end of the array.

Returns
Const iterator to the end of the array.

◆ fill() [1/2]

template<class T , uint N>
void vcl::Array< T, N >::fill ( const T &  t)
inline

Fills the entire Array with the value t.

Parameters
[in]tThe value to fill the array with.

◆ fill() [2/2]

template<class T , uint N>
template<Range Rng>
void vcl::Array< T, N >::fill ( Rng &&  r)
inline

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.

Template Parameters
RngType of the range of values to fill the array with. It must satisfy the Range concept.
Parameters
[in]rA range of the same type as the array. The range must have begin() and end() members provided.

◆ operator()() [1/2]

template<class T , uint N>
template<typename... I>
requires (sizeof...(indices) == N)
Reference vcl::Array< T, N >::operator() ( I...  indices)
inline

Operator () that allows to access one element of the array. It can be used as left or right value.

Template Parameters
ITypes of the indices used to access the element of the array.
Parameters
[in]indicesN indices that allow to access an element of the array. A number of indices not equal to N will generate a compilation error.
Returns
A reference to the element of the array.

◆ operator()() [2/2]

template<class T , uint N>
template<typename... I>
requires (sizeof...(indices) == N)
ConstReference vcl::Array< T, N >::operator() ( I...  indices) const
inline

Operator () that allows to access one element of the array. It can be used only as right value.

Template Parameters
ITypes of the indices used to access the element of the array.
Parameters
[in]indicesN indices that allow to access an element of the array. A number of indices not equal to N will generate a compilation error.
Returns
A const reference to the element of the array.

◆ resize()

template<class T , uint N>
template<typename... Sizes>
requires (sizeof...(s) == N)
void vcl::Array< T, N >::resize ( Sizes...  s)
inline

Allows to resize the Array, not conserving the values of the previous array.

Template Parameters
SizesTypes of the arguments representing the new sizes of the Array.
Parameters
[in]sN elements representing the new sizes of the Array.

◆ rows()

template<class T , uint N>
std::size_t vcl::Array< T, N >::rows ( ) const
inline

Returns the number of rows of a 2-dimensional array.

Returns
The number of rows of the array.
Note
This function can only be called for 2-dimensional arrays.

◆ size()

template<class T , uint N>
std::size_t vcl::Array< T, N >::size ( std::size_t  dim) const
inline

Returns the size of the given dimension.

Parameters
[in]dimThe dimension to query the size of.
Returns
The size of the given dimension.

◆ sizeW()

template<class T , uint N>
std::size_t vcl::Array< T, N >::sizeW ( ) const
inline

Returns the size of the W dimension of the array.

Returns
The size of the W dimension of the array.
Note
This function can only be called for arrays with at least four dimensions.

◆ sizeX()

template<class T , uint N>
std::size_t vcl::Array< T, N >::sizeX ( ) const
inline

Returns the size of the X dimension of the array.

Returns
The size of the X dimension of the array.
Note
This function can only be called for arrays with at least one dimension.

◆ sizeY()

template<class T , uint N>
std::size_t vcl::Array< T, N >::sizeY ( ) const
inline

Returns the size of the Y dimension of the array.

Returns
The size of the Y dimension of the array.
Note
This function can only be called for arrays with at least two dimensions.

◆ sizeZ()

template<class T , uint N>
std::size_t vcl::Array< T, N >::sizeZ ( ) const
inline

Returns the size of the Z dimension of the array.

Returns
The size of the Z dimension of the array.
Note
This function can only be called for arrays with at least three dimensions.

◆ stdVector() [1/3]

template<class T , uint N>
std::vector< T > vcl::Array< T, N >::stdVector ( ) &
inline

Returns a std::vector containing the elements of the array in row-major order.

Returns
A std::vector containing the elements of the array in row-major order.

◆ stdVector() [2/3]

template<class T , uint N>
std::vector< T > && vcl::Array< T, N >::stdVector ( ) &&
inline

Returns a std::vector containing the elements of the array in row-major order.

Returns
A std::vector containing the elements of the array in row-major order.

◆ stdVector() [3/3]

template<class T , uint N>
const std::vector< T > & vcl::Array< T, N >::stdVector ( ) const &
inline

Returns a std::vector containing the elements of the array in row-major order.

Returns
A const reference to a std::vector containing the elements of the array in row-major order.

◆ subArray()

template<class T , uint N>
Array< T, N - 1 > vcl::Array< T, N >::subArray ( uint  r) const
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:

Array<int, 2> sa = a.subArray(1);
// sa is a 2x6 2D Array, containing the elements at the second "row" of
// Array a.
Parameters
rIndex at the first dimension to start the sub-array.
Returns
The sub-array of dimension N-1.
Precondition
N > 1
r < size(0)

The documentation for this class was generated from the following file: