Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
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 >:
Collaboration graph
[legend]

Classes

struct  ContainerInterface
 
struct  ContainerWrapper
 
struct  Details
 
class  Iterator
 

Public Types

using self_type = NdArray< T >
 
using const_iterator = Iterator< true >
 
using iterator = Iterator< false >
 

Public Member Functions

 ~NdArray ()=default
 
 NdArray (std::vector< size_t > shape_)
 
template<template< class... > class Container = std::vector>
 NdArray (std::vector< size_t > shape_, const Container< T > &data)
 
template<template< class... > class Container = std::vector>
 NdArray (std::vector< size_t > shape_, Container< T > &&data)
 
template<template< class... > class Container = std::vector>
 NdArray (std::vector< size_t > shape_, std::vector< size_t > strides_, Container< T > &&data)
 
template<typename Iterator >
 NdArray (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 &)
 
 NdArray (self_type &&) noexcept=default
 
NdArrayoperator= (const NdArray &)
 
NdArray copy () const
 
const std::vector< size_t > & shape () const
 
size_t shape (std::size_t i) const
 
const std::vector< std::size_t > & strides () const
 
std::size_t strides (std::size_t i) const
 
self_typereshape (const std::vector< size_t > &new_shape)
 
template<typename... D>
self_typereshape (size_t i, D... rest)
 
const T & front () const
 
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)
 
self_type slice (size_t i)
 
const self_type slice (size_t i) const
 
self_type rslice (size_t i)
 
const self_type rslice (size_t i) const
 
void next_slice (void)
 
const std::vector< std::string > & attributes () const
 

Private Member Functions

 NdArray (const self_type *other)
 
 NdArray (std::shared_ptr< ContainerInterface > container, size_t offset, std::vector< size_t > shape, std::vector< size_t > stride, std::vector< std::string > attr_names)
 
size_t get_offset (const std::vector< size_t > &coords) const
 
size_t get_attr_offset (const std::string &attr) const
 
void update_strides ()
 
template<typename... D>
T & at_helper (size_t offset_acc, size_t axis, size_t i, D... rest)
 
T & at_helper (size_t offset_acc, size_t axis)
 
T & at_helper (size_t offset_acc, size_t axis, const std::string &attr)
 
template<typename... D>
const T & at_helper (size_t offset_acc, size_t axis, size_t i, D... rest) const
 
const T & at_helper (size_t offset_acc, size_t axis) const
 
const T & at_helper (size_t offset_acc, size_t axis, const std::string &attr) 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::unique_ptr< Detailsm_details_ptr
 

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 >
using Euclid::NdArray::NdArray< T >::const_iterator = Iterator<true>

Definition at line 185 of file NdArray.h.

◆ iterator

template<typename T >
using Euclid::NdArray::NdArray< T >::iterator = Iterator<false>

Definition at line 186 of file NdArray.h.

◆ self_type

template<typename T >
using Euclid::NdArray::NdArray< T >::self_type = NdArray<T>

Definition at line 54 of file NdArray.h.

Constructor & Destructor Documentation

◆ ~NdArray()

template<typename T >
Euclid::NdArray::NdArray< T >::~NdArray ( )
default

Destructor.

◆ NdArray() [1/11]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( 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/11]

template<typename T >
template<template< class... > class Container = std::vector>
Euclid::NdArray::NdArray< T >::NdArray ( 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/11]

template<typename T >
template<template< class... > class Container = std::vector>
Euclid::NdArray::NdArray< T >::NdArray ( 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(), resize() 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/11]

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

Construct a matrix with a custom container and strides.

Template Parameters
ContainerOwns the memory used by the NdArray. It must expose the methods size(), resize() 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.
strides_The number of bytes we need to jump between adjacent positions on a given dimension
dataThe initial data. 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::runtime_errorIf the data is not in C order.
std::invalid_argumentIf the shape and strides dimension do not match.

◆ NdArray() [5/11]

template<typename T >
template<typename Iterator >
Euclid::NdArray::NdArray< T >::NdArray ( 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() [6/11]

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() [7/11]

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

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 286 of file NdArray.h.

◆ NdArray() [8/11]

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

Copy constructor

Note
The underlying data is not copied, but shared

◆ NdArray() [9/11]

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

Move constructor

◆ NdArray() [10/11]

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

Private constructor used for deep copies

◆ NdArray() [11/11]

template<typename T >
Euclid::NdArray::NdArray< T >::NdArray ( std::shared_ptr< ContainerInterface container,
size_t  offset,
std::vector< size_t >  shape,
std::vector< size_t >  stride,
std::vector< std::string attr_names 
)
private

Private constructor used for slices

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.
Warning
This method does not test for bounds, but it uses asserts to catch bad accesses on debug builds.
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.
Warning
This method does not test for bounds, but it uses asserts to catch bad accesses on debug builds.
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/6]

template<typename T >
T & Euclid::NdArray::NdArray< T >::at_helper ( size_t  offset_acc,
size_t  axis 
)
private

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

◆ at_helper() [2/6]

template<typename T >
const T & Euclid::NdArray::NdArray< T >::at_helper ( size_t  offset_acc,
size_t  axis 
) const
private

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

◆ at_helper() [3/6]

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

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

◆ at_helper() [4/6]

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

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

◆ at_helper() [5/6]

template<typename T >
template<typename... D>
T & Euclid::NdArray::NdArray< T >::at_helper ( size_t  offset_acc,
size_t  axis,
size_t  i,
D...  rest 
)
private

Helper to expand at with a variable number of arguments

◆ at_helper() [6/6]

template<typename T >
template<typename... D>
const T & Euclid::NdArray::NdArray< T >::at_helper ( size_t  offset_acc,
size_t  axis,
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 310 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).

◆ front()

template<typename T >
const T & Euclid::NdArray::NdArray< T >::front ( ) const
Returns
A reference to the front element

◆ get_attr_offset()

template<typename T >
size_t Euclid::NdArray::NdArray< T >::get_attr_offset ( const std::string attr) const
private

Get the offset for the given attribute name

Exceptions
std::out_of_rangeIf the attribute is unknown.

◆ get_offset()

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.

◆ next_slice()

template<typename T >
void Euclid::NdArray::NdArray< T >::next_slice ( void  )

◆ 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 > &  )

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

◆ rslice() [1/2]

template<typename T >
self_type Euclid::NdArray::NdArray< T >::rslice ( size_t  i)

Return a slice of the array cutting along the last axis

◆ rslice() [2/2]

template<typename T >
const self_type Euclid::NdArray::NdArray< T >::rslice ( size_t  i) const

Return a slice of the array cutting along the last axis

◆ shape() [1/2]

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 319 of file NdArray.h.

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

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

◆ shape() [2/2]

template<typename T >
size_t Euclid::NdArray::NdArray< T >::shape ( std::size_t  i) const
inline
Parameters
i
Returns
The size of the axis i

Definition at line 327 of file NdArray.h.

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

◆ size()

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

Size of the underlying container

◆ slice() [1/2]

template<typename T >
self_type Euclid::NdArray::NdArray< T >::slice ( size_t  i)

Return a slice of the array cutting along the first axis

◆ slice() [2/2]

template<typename T >
const self_type Euclid::NdArray::NdArray< T >::slice ( size_t  i) const

Return a slice of the array cutting along the first axis

◆ strides() [1/2]

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

Definition at line 331 of file NdArray.h.

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

◆ strides() [2/2]

template<typename T >
std::size_t Euclid::NdArray::NdArray< T >::strides ( std::size_t  i) const
inline

Definition at line 335 of file NdArray.h.

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

◆ update_strides()

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

Compute the stride size for each dimension

Member Data Documentation

◆ m_details_ptr

template<typename T >
std::unique_ptr<Details> Euclid::NdArray::NdArray< T >::m_details_ptr
private

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