Alexandria  2.18
Please provide a description of the project.
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Euclid::NdArray::NdArray< T > Class Template Reference

#include <NdArray.h>

Collaboration diagram for Euclid::NdArray::NdArray< T >:
[legend]

Classes

struct  ContainerInterface
 
struct  ContainerWrapper
 
class  Iterator
 

Public Types

typedef NdArray< T > self_type
 
typedef Iterator< true > const_iterator
 
typedef Iterator< false > iterator
 

Public Member Functions

virtual ~NdArray ()=default
 
 NdArray (const std::vector< size_t > &shape_)
 
template<template< class... > class Container = std::vector>
 NdArray (const std::vector< size_t > &shape_, const Container< T > &data)
 
template<template< class... > class Container = std::vector>
 NdArray (const std::vector< size_t > &shape_, Container< T > &&data)
 
template<typename Iterator >
 NdArray (const std::vector< size_t > &shape_, Iterator begin, Iterator end)
 
template<typename... Args>
 NdArray (const std::vector< size_t > &shape_, const std::vector< std::string > &attr_names, Args &&... args)
 
 NdArray (const std::initializer_list< size_t > &shape_)
 
 NdArray (const self_type &)=default
 
 NdArray (self_type &&)=default
 
NdArrayoperator= (const NdArray &)=default
 
NdArray copy () const
 
const std::vector< size_t > shape () const
 
self_typereshape (const std::vector< size_t > new_shape)
 
template<typename... D>
self_typereshape (size_t i, D... rest)
 
T & at (const std::vector< size_t > &coords)
 
const T & at (const std::vector< size_t > &coords) const
 
T & at (const std::vector< size_t > &coords, const std::string &attr)
 
const T & at (const std::vector< size_t > &coords, const std::string &attr) const
 
template<typename... D>
T & at (size_t i, D... rest)
 
template<typename... D>
const T & at (size_t i, D... rest) const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
size_t size () const
 
bool operator== (const self_type &b) const
 
bool operator!= (const self_type &b) const
 
self_typeconcatenate (const self_type &other)
 
const std::vector< std::string > & attributes () const
 

Private Member Functions

 NdArray (const self_type *other)
 
size_t get_offset (const std::vector< size_t > &coords) const
 
size_t get_offset (std::vector< size_t > coords, const std::string &attr) const
 
void update_strides ()
 
template<typename... D>
T & at_helper (std::vector< size_t > &acc, size_t i, D... rest)
 
T & at_helper (std::vector< size_t > &acc)
 
T & at_helper (std::vector< size_t > &acc, const std::string &attr)
 
template<typename... D>
const T & at_helper (std::vector< size_t > &acc, size_t i, D... rest) const
 
const T & at_helper (std::vector< size_t > &acc) const
 
template<typename... D>
self_typereshape_helper (std::vector< size_t > &acc, size_t i, D... rest)
 
self_typereshape_helper (std::vector< size_t > &acc)
 

Private Attributes

std::vector< size_t > m_shape
 
std::vector< size_t > m_stride_size
 
std::vector< std::stringm_attr_names
 
size_t m_size
 
std::shared_ptr< ContainerInterfacem_container
 

Detailed Description

template<typename T>
class Euclid::NdArray::NdArray< T >

Stores a multidimensional array in a contiguous piece of memory in row-major order

Template Parameters
TData type
ContainerWhich container to use, by default std::vector

Definition at line 46 of file NdArray.h.

Member Typedef Documentation

◆ const_iterator

template<typename T >
typedef Iterator<true> Euclid::NdArray::NdArray< T >::const_iterator

Definition at line 173 of file NdArray.h.

◆ iterator

template<typename T >
typedef Iterator<false> Euclid::NdArray::NdArray< T >::iterator

Definition at line 174 of file NdArray.h.

◆ self_type

template<typename T >
typedef NdArray<T> Euclid::NdArray::NdArray< T >::self_type

Definition at line 54 of file NdArray.h.

Constructor & Destructor Documentation

◆ ~NdArray()

template<typename T >
virtual Euclid::NdArray::NdArray< T >::~NdArray ( )
virtualdefault

Destructor.

◆ NdArray() [1/9]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( const std::vector< size_t > &  shape_)
explicit

Constructs a default-initialized matrix with the given shape.

Parameters
shape_The shape of the matrix. The number of elements in shape corresponds to the number of dimensions, the values to each dimension size.

◆ NdArray() [2/9]

template<typename T >
template<template< class... > class Container = std::vector>
Euclid::NdArray::NdArray< T >::NdArray ( const std::vector< size_t > &  shape_,
const Container< T > &  data 
)

Constructs a matrix and initialize it with the given data.

Parameters
shape_The shape of the matrix. The number of elements in shape corresponds to the number of dimensions, the values to each dimension size.
dataThe initial data. It must match exactly the matrix size (shape[0]*shape[1]...*shape[n]).
Exceptions
std::invalid_argumentIf the data size does not corresponds to the matrix size.

◆ NdArray() [3/9]

template<typename T >
template<template< class... > class Container = std::vector>
Euclid::NdArray::NdArray< T >::NdArray ( const std::vector< size_t > &  shape_,
Container< T > &&  data 
)

Constructs a matrix and initialize it with the given data.

Template Parameters
ContainerOwns the memory used by the NdArray. It must expose the methods size() and data().
Parameters
shape_The shape of the matrix. The number of elements in shape corresponds to the number of dimensions, the values to each dimension size.
dataThe initial data. It must match exactly the matrix size (shape[0]*shape[1]...*shape[n]). The NdArray will move the data into its internal storage! This avoids a copy, but remember to not use data after this call.
Exceptions
std::invalid_argumentIf the data size does not corresponds to the matrix size.

◆ NdArray() [4/9]

template<typename T >
template<typename Iterator >
Euclid::NdArray::NdArray< T >::NdArray ( const std::vector< size_t > &  shape_,
Iterator  begin,
Iterator  end 
)

Constructs a matrix and initialize it with from the given iterators

Parameters
shape_The shape of the matrix. The number of elements in shape corresponds to the number of dimensions, the values to each dimension size.
beginThe beginning of the data
endThe end of the data
Exceptions
std::invalid_argumentIf the data size does not corresponds to the matrix size.

◆ NdArray() [5/9]

template<typename T >
template<typename... Args>
Euclid::NdArray::NdArray< T >::NdArray ( const std::vector< size_t > &  shape_,
const std::vector< std::string > &  attr_names,
Args &&...  args 
)

Constructs a matrix, giving a name to each of the items on the last dimension

Parameters
attr_namesNames for the dimensions of the last axis
shape_Shape for the matrix
Note
Unlike numpy, attr_names is treated strictly as an alias, so NdArray<float>({20}, {"X", "Y"}) has a shape of (20, 2)

◆ NdArray() [6/9]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( const std::initializer_list< size_t > &  shape_)
inline

Constructs a default-initialized matrix with the given shape (as an initializer list).

Parameters
shape_The shape of the matrix. The number of elements in shape corresponds to the number of dimensions, the values to each dimension size.

Definition at line 253 of file NdArray.h.

◆ NdArray() [7/9]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( const self_type )
default

Copy constructor

Note
The underlying data is not copied, but shared

◆ NdArray() [8/9]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( self_type &&  )
default

Move constructor

◆ NdArray() [9/9]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( const self_type other)
explicitprivate

Private constructor used for deep copies

Member Function Documentation

◆ at() [1/6]

template<typename T >
T& Euclid::NdArray::NdArray< T >::at ( const std::vector< size_t > &  coords)

Gets a reference to the value stored at the given coordinates.

Parameters
coordsElements coordinates.
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.

◆ at() [2/6]

template<typename T >
const T& Euclid::NdArray::NdArray< T >::at ( const std::vector< size_t > &  coords) const

Gets a constant reference to the value stored at the given coordinates

Parameters
coordsElements coordinates.
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.

◆ at() [3/6]

template<typename T >
T& Euclid::NdArray::NdArray< T >::at ( const std::vector< size_t > &  coords,
const std::string attr 
)

Gets a reference to the value stored at the given coordinates.

Parameters
coordsElements coordinates, except last one
attrAttribute name used to determine the last coordinate
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.

◆ at() [4/6]

template<typename T >
const T& Euclid::NdArray::NdArray< T >::at ( const std::vector< size_t > &  coords,
const std::string attr 
) const

Gets a constant reference to the value stored at the given coordinates.

Parameters
coordsElements coordinates, except last one
attrAttribute name used to determine the last coordinate
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.

◆ at() [5/6]

template<typename T >
template<typename... D>
T& Euclid::NdArray::NdArray< T >::at ( size_t  i,
D...  rest 
)

Gets a reference to the value stored at the given coordinates.

Parameters
coordsElements coordinates.
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.
Note
This is a convenience function that allows access without requiring a vector when the number of dimensions is known in advance (i.e. at(x, y, z) instead of at(std::vector<size_t>{x, y, z})).

◆ at() [6/6]

template<typename T >
template<typename... D>
const T& Euclid::NdArray::NdArray< T >::at ( size_t  i,
D...  rest 
) const

Gets a constant reference to the value stored at the given coordinates.

Parameters
coordsElements coordinates.
Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.
Note
This is a convenience function that allows access without requiring a vector when the number of dimensions is known in advance (i.e. at(x, y, z) instead of at(std::vector<size_t>{x, y, z})).

◆ at_helper() [1/5]

template<typename T >
T& Euclid::NdArray::NdArray< T >::at_helper ( std::vector< size_t > &  acc)
private

Helper to expand at with a variable number of arguments (base case)

◆ at_helper() [2/5]

template<typename T >
const T& Euclid::NdArray::NdArray< T >::at_helper ( std::vector< size_t > &  acc) const
private

Helper to expand constant at with a variable number of arguments (base case)

◆ at_helper() [3/5]

template<typename T >
T& Euclid::NdArray::NdArray< T >::at_helper ( std::vector< size_t > &  acc,
const std::string attr 
)
private

Helper to expand at with a variable number of arguments, being the last an attribute name

◆ at_helper() [4/5]

template<typename T >
template<typename... D>
T& Euclid::NdArray::NdArray< T >::at_helper ( std::vector< size_t > &  acc,
size_t  i,
D...  rest 
)
private

Helper to expand at with a variable number of arguments

◆ at_helper() [5/5]

template<typename T >
template<typename... D>
const T& Euclid::NdArray::NdArray< T >::at_helper ( std::vector< size_t > &  acc,
size_t  i,
D...  rest 
) const
private

Helper to expand constant at with a variable number of arguments

◆ attributes()

template<typename T >
const std::vector<std::string>& Euclid::NdArray::NdArray< T >::attributes ( ) const
Returns
Attribute names

◆ begin() [1/2]

template<typename T >
iterator Euclid::NdArray::NdArray< T >::begin ( )
Returns
An iterator pointing to the first element (which corresponds to the one with the coordinates set to 0).

◆ begin() [2/2]

template<typename T >
const_iterator Euclid::NdArray::NdArray< T >::begin ( ) const
Returns
A constant iterator pointing to the first element (which corresponds to the one with the coordinates set to 0).

◆ concatenate()

template<typename T >
self_type& Euclid::NdArray::NdArray< T >::concatenate ( const self_type other)

Concatenate to this array another one along the first axis

Returns
*this

◆ copy()

template<typename T >
NdArray Euclid::NdArray::NdArray< T >::copy ( ) const
inline

Create a copy of the NdArray

Definition at line 277 of file NdArray.h.

◆ end() [1/2]

template<typename T >
iterator Euclid::NdArray::NdArray< T >::end ( )
Returns
An iterator pointing just after the last element (which correspond to the one with the coordinates set to (shape[0]-1, shape[1]-1, ... shape[n]-1).

◆ end() [2/2]

template<typename T >
const_iterator Euclid::NdArray::NdArray< T >::end ( ) const
Returns
A constant iterator pointing just after the last element (which correspond to the one with the coordinates set to (shape[0]-1, shape[1]-1, ... shape[n]-1).

◆ get_offset() [1/2]

template<typename T >
size_t Euclid::NdArray::NdArray< T >::get_offset ( const std::vector< size_t > &  coords) const
private

Gets the total offset for the given coordinates.

Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds.

◆ get_offset() [2/2]

template<typename T >
size_t Euclid::NdArray::NdArray< T >::get_offset ( std::vector< size_t >  coords,
const std::string attr 
) const
private

Gets the total offset for the given coordinates.

Exceptions
std::out_of_rangeIf the number of coordinates is invalid, or any of them is out of bounds, or the attribute does not exist.

◆ operator!=()

template<typename T >
bool Euclid::NdArray::NdArray< T >::operator!= ( const self_type b) const

Two NdArrays are not equal if their shapes or their content are not equal

◆ operator=()

template<typename T >
NdArray& Euclid::NdArray::NdArray< T >::operator= ( const NdArray< T > &  )
default

Assignment

Note
The underlying data is not copied, but shared

◆ operator==()

template<typename T >
bool Euclid::NdArray::NdArray< T >::operator== ( const self_type b) const

Two NdArrays are equal if their shapes and their content are equal

◆ reshape() [1/2]

template<typename T >
self_type& Euclid::NdArray::NdArray< T >::reshape ( const std::vector< size_t >  new_shape)

Reshape the NdArray.

Note
This modifies the object
Parameters
new_shapeA vector with as many elements as number of dimensions, containing the size of each one.
Exceptions
std::range_errorIf the new shape does not match the number of elements already contained within the NdArray.
Returns
*this
Exceptions
std::invalid_argumentIf the array has attribute names

◆ reshape() [2/2]

template<typename T >
template<typename... D>
self_type& Euclid::NdArray::NdArray< T >::reshape ( size_t  i,
D...  rest 
)

Reshape the NdArray.

Note
This modifies the object
Parameters
new_shapeA vector with as many elements as number of dimensions, containing the size of each one.
Exceptions
std::range_errorIf the new shape does not match the number of elements already contained within the NdArray.
Returns
*this

◆ reshape_helper() [1/2]

template<typename T >
self_type& Euclid::NdArray::NdArray< T >::reshape_helper ( std::vector< size_t > &  acc)
private

◆ reshape_helper() [2/2]

template<typename T >
template<typename... D>
self_type& Euclid::NdArray::NdArray< T >::reshape_helper ( std::vector< size_t > &  acc,
size_t  i,
D...  rest 
)
private

◆ shape()

template<typename T >
const std::vector<size_t> Euclid::NdArray::NdArray< T >::shape ( ) const
inline

Gets the shape of the matrix.

Returns
A vector with as many elements as number of dimensions, containing the size of each one.

Definition at line 286 of file NdArray.h.

References Euclid::NdArray::NdArray< T >::m_shape.

Referenced by Euclid::Table::ndArraySize(), and Euclid::NdArray::NdArray< T >::ContainerWrapper< Container >::resize().

◆ size()

template<typename T >
size_t Euclid::NdArray::NdArray< T >::size ( ) const

Size of the underlying container

◆ update_strides()

template<typename T >
void Euclid::NdArray::NdArray< T >::update_strides ( )
private

Compute the stride size for each dimension

Member Data Documentation

◆ m_attr_names

template<typename T >
std::vector<std::string> Euclid::NdArray::NdArray< T >::m_attr_names
private

Definition at line 436 of file NdArray.h.

◆ m_container

template<typename T >
std::shared_ptr<ContainerInterface> Euclid::NdArray::NdArray< T >::m_container
private

Definition at line 515 of file NdArray.h.

◆ m_shape

template<typename T >
std::vector<size_t> Euclid::NdArray::NdArray< T >::m_shape
private

Definition at line 435 of file NdArray.h.

Referenced by Euclid::NdArray::NdArray< T >::shape().

◆ m_size

template<typename T >
size_t Euclid::NdArray::NdArray< T >::m_size
private

Definition at line 437 of file NdArray.h.

◆ m_stride_size

template<typename T >
std::vector<size_t> Euclid::NdArray::NdArray< T >::m_stride_size
private

Definition at line 435 of file NdArray.h.


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