lcls2
C++ API: public headers of xtcdata, psalg, psdaq and psana
Loading...
Searching...
No Matches
psalg::NDArray< T > Class Template Reference

XtcData::Array<T> subclass that keeps its own copy of the shape (set_shape() asserts ndim < MAXNDIM) and either points at an external buffer or owns a heap buffer, deleted in the destructor. More...

#include <NDArray.hh>

Inherits XtcData::Array< T >.

Public Types

enum  { MAXNDIM = XtcData::MaxRank }
 Size of the internal shape array. More...
 
typedef psalg::types::shape_t shape_t
 Alias for psalg::types::shape_t (uint32_t).
 
typedef psalg::types::size_t size_t
 Alias for psalg::types::size_t (uint32_t).
 
typedef XtcData::Array< T > base
 Alias for the base class XtcData::Array<T>.
 
using NON_CONST_T = typename remove_const< T >::type
 T with const removed; element type of the owned buffer.
 

Public Member Functions

 NDArray (const shape_t *sh, const size_t ndim, void *buf_ext=0)
 Set the shape from sh and ndim, then use buf_ext as the data buffer without copying; if buf_ext is null, allocate an owned buffer of size() elements.
 
 NDArray (const shape_t *sh, const size_t ndim, const void *buf_ext)
 Set the shape from sh and ndim, then point at the const buffer buf_ext without copying (nothing is allocated if it is null).
 
 NDArray ()
 Empty array: no shape (rank 0) and no data.
 
 ~NDArray ()
 Delete the owned buffer, if any.
 
 NDArray (const base &o)
 Copy the shape of the Array o and deep-copy its size() elements into an owned buffer.
 
 NDArray (const NDArray< T > &o)
 Copy the shape of o and deep-copy its size() elements into an owned buffer.
 
NDArray< T > & operator= (const NDArray< T > &o)
 Unless o is this object, copy o's base members, then its shape and a deep copy of its data into an owned buffer.
 
void set_shape (const shape_t *shape=NULL, const size_t ndim=0)
 Copy ndim entries of shape (if it is not null) into the internal shape array and set the rank; asserts ndim < MAXNDIM.
 
void set_shape_string (const std::string &str)
 Converts string like "(5920, 388)" to array for _shape[]={5920, 388}.
 
void reshape (const shape_t *shape, const size_t ndim)
 Same as set_shape(shape, ndim).
 
T * data ()
 Return the data pointer.
 
const T * const_data () const
 Return the data pointer as const T*.
 
shape_t * shape () const
 Return the shape pointer.
 
size_t ndim () const
 Return the rank.
 
size_t rank () const
 Return the rank (same as ndim()).
 
size_t size () const
 Return the product of the first ndim() shape entries (1 for rank 0).
 
void set_ndarray (base &a)
 Copy the shape of a and point at its data without copying (an owned buffer is released).
 
void set_ndarray (NDArray< T > &a)
 Copy the shape of a and point at its data without copying (an owned buffer is released).
 
void set_const_data_buffer (const void *buf_ext=0)
 CONST !!! *buf sets pointer to external data buffer WARNING shape needs to be set first, othervice size() is undefined!
 
void set_data_buffer (void *buf_ext=0)
 sets pointer to data WARNING shape needs to be set first, othervice size() is undefined!
 
void set_data_copy (const void *buf=0)
 copy content of external buffer in object memory buffer WARNING shape needs to be set first, othervice size() is undefined!
 
void reserve_data_buffer (const size_t size)
 Reserves internal data buffer for array of requested size.
 
std::string string_ndarray (const size_t max_nvalues=5)
 Return "typeid=... ndim=... size=... shape=(...) data=..." listing at most max_nvalues values, followed by "..." if there are more; an empty string for rank 0.
 
- Public Member Functions inherited from XtcData::Array< T >
 Array (void *data, uint32_t *shape, uint32_t rank)
 Wrap data (reinterpreted as T*), shape and rank; nothing is copied.
 
 Array ()
 Construct an empty view: null data and shape pointers, rank 0.
 
 Array (const Array &a)
 Copy the data pointer, shape pointer and rank of a (shallow copy).
 
Array & operator= (const Array &o)
 Copy the data pointer, shape pointer and rank of o (shallow); self-assignment does nothing.
 
T & operator() (unsigned i)
 Return a reference to element i.
 
T & operator() (unsigned i, unsigned j)
 Return a reference to element (i, j) at offset i*shape[1]+j.
 
T & operator() (unsigned i, unsigned j, unsigned k)
 Return a reference to element (i, j, k) at the row-major offset.
 
T & operator() (unsigned i, unsigned j, unsigned k, unsigned l)
 Return a reference to element (i, j, k, l) at the row-major offset.
 
T & operator() (unsigned i, unsigned j, unsigned k, unsigned l, unsigned m)
 Return a reference to element (i, j, k, l, m) at the row-major offset.
 
uint32_t rank () const
 Return the rank.
 
uint32_t * shape () const
 Return the shape pointer (not a copy).
 
T * data ()
 Return the data pointer.
 
const T * const_data () const
 Return the data pointer as const T*.
 
uint64_t num_elem () const
 Return the product of shape[0] .
 
void shape (uint32_t a, uint32_t b=0, uint32_t c=0, uint32_t d=0, uint32_t e=0)
 Write a, b, c, d, e into shape[0..4] (always five entries).
 
void set_rank (uint32_t rank)
 Set the rank.
 
void set_shape (uint32_t *shape)
 Set the shape pointer.
 
void set_data (void *data)
 Set the data pointer (reinterpreted as T*).
 

Public Attributes

bool T_IS_CONST = std::is_const<T>::value
 True if T is const. It is a non-static member, so every object stores it.
 

Friends

std::ostream & operator<< (std::ostream &os, const NDArray< T > &o)
 Write the same description as string_ndarray() with at most 4 values to os (nothing for rank 0) and return os.
 

Additional Inherited Members

- Protected Attributes inherited from XtcData::Array< T >
uint32_t * _shape
 
T * _data
 
uint32_t _rank
 

Detailed Description

template<typename T>
class psalg::NDArray< T >

XtcData::Array<T> subclass that keeps its own copy of the shape (set_shape() asserts ndim < MAXNDIM) and either points at an external buffer or owns a heap buffer, deleted in the destructor.

Copy construction and assignment deep-copy the data into an owned buffer.

Member Typedef Documentation

◆ base

Alias for the base class XtcData::Array<T>.

◆ NON_CONST_T

template<typename T >
using psalg::NDArray< T >::NON_CONST_T = typename remove_const<T>::type

T with const removed; element type of the owned buffer.

◆ shape_t

Alias for psalg::types::shape_t (uint32_t).

◆ size_t

Alias for psalg::types::size_t (uint32_t).

Member Enumeration Documentation

◆ anonymous enum

template<typename T >
anonymous enum

Size of the internal shape array.

Enumerator
MAXNDIM 

Equal to XtcData::MaxRank (5); set_shape() asserts ndim < MAXNDIM.

Constructor & Destructor Documentation

◆ NDArray() [1/5]

template<typename T >
psalg::NDArray< T >::NDArray ( const shape_t *  sh,
const size_t  ndim,
void *  buf_ext = 0 
)
inline

Set the shape from sh and ndim, then use buf_ext as the data buffer without copying; if buf_ext is null, allocate an owned buffer of size() elements.

◆ NDArray() [2/5]

template<typename T >
psalg::NDArray< T >::NDArray ( const shape_t *  sh,
const size_t  ndim,
const void *  buf_ext 
)
inline

Set the shape from sh and ndim, then point at the const buffer buf_ext without copying (nothing is allocated if it is null).

◆ NDArray() [3/5]

template<typename T >
psalg::NDArray< T >::NDArray ( )
inline

Empty array: no shape (rank 0) and no data.

◆ ~NDArray()

template<typename T >
psalg::NDArray< T >::~NDArray ( )
inline

Delete the owned buffer, if any.

◆ NDArray() [4/5]

template<typename T >
psalg::NDArray< T >::NDArray ( const base &  o)
inline

Copy the shape of the Array o and deep-copy its size() elements into an owned buffer.

◆ NDArray() [5/5]

template<typename T >
psalg::NDArray< T >::NDArray ( const NDArray< T > &  o)
inline

Copy the shape of o and deep-copy its size() elements into an owned buffer.

Member Function Documentation

◆ const_data()

template<typename T >
const T * psalg::NDArray< T >::const_data ( ) const
inline

Return the data pointer as const T*.

◆ data()

template<typename T >
T * psalg::NDArray< T >::data ( )
inline

Return the data pointer.

◆ ndim()

template<typename T >
size_t psalg::NDArray< T >::ndim ( ) const
inline

Return the rank.

◆ operator=()

template<typename T >
NDArray< T > & psalg::NDArray< T >::operator= ( const NDArray< T > &  o)
inline

Unless o is this object, copy o's base members, then its shape and a deep copy of its data into an owned buffer.

Returns
*this.

◆ rank()

template<typename T >
size_t psalg::NDArray< T >::rank ( ) const
inline

Return the rank (same as ndim()).

◆ reserve_data_buffer()

template<typename T >
void psalg::NDArray< T >::reserve_data_buffer ( const size_t  size)
inline

Reserves internal data buffer for array of requested size.

For externally defined data buffer do not do anything, othervise reserves memory on heap. size is a number of values

◆ reshape()

template<typename T >
void psalg::NDArray< T >::reshape ( const shape_t *  shape,
const size_t  ndim 
)
inline

Same as set_shape(shape, ndim).

◆ set_const_data_buffer()

template<typename T >
void psalg::NDArray< T >::set_const_data_buffer ( const void *  buf_ext = 0)
inline

CONST !!! *buf sets pointer to external data buffer WARNING shape needs to be set first, othervice size() is undefined!

◆ set_data_buffer()

template<typename T >
void psalg::NDArray< T >::set_data_buffer ( void *  buf_ext = 0)
inline

sets pointer to data WARNING shape needs to be set first, othervice size() is undefined!

◆ set_data_copy()

template<typename T >
void psalg::NDArray< T >::set_data_copy ( const void *  buf = 0)
inline

copy content of external buffer in object memory buffer WARNING shape needs to be set first, othervice size() is undefined!

◆ set_ndarray() [1/2]

template<typename T >
void psalg::NDArray< T >::set_ndarray ( base &  a)
inline

Copy the shape of a and point at its data without copying (an owned buffer is released).

If a has no data, an owned buffer of size() elements is allocated instead.

◆ set_ndarray() [2/2]

template<typename T >
void psalg::NDArray< T >::set_ndarray ( NDArray< T > &  a)
inline

Copy the shape of a and point at its data without copying (an owned buffer is released).

If a has no data, an owned buffer of size() elements is allocated instead.

◆ set_shape()

template<typename T >
void psalg::NDArray< T >::set_shape ( const shape_t *  shape = NULL,
const size_t  ndim = 0 
)
inline

Copy ndim entries of shape (if it is not null) into the internal shape array and set the rank; asserts ndim < MAXNDIM.

The data buffer is not changed.

◆ set_shape_string()

template<typename T >
void psalg::NDArray< T >::set_shape_string ( const std::string &  str)
inline

Converts string like "(5920, 388)" to array for _shape[]={5920, 388}.

◆ shape()

template<typename T >
shape_t * psalg::NDArray< T >::shape ( ) const
inline

Return the shape pointer.

◆ size()

template<typename T >
size_t psalg::NDArray< T >::size ( ) const
inline

Return the product of the first ndim() shape entries (1 for rank 0).

◆ string_ndarray()

template<typename T >
std::string psalg::NDArray< T >::string_ndarray ( const size_t  max_nvalues = 5)
inline

Return "typeid=... ndim=... size=... shape=(...) data=..." listing at most max_nvalues values, followed by "..." if there are more; an empty string for rank 0.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream &  os,
const NDArray< T > &  o 
)
friend

Write the same description as string_ndarray() with at most 4 values to os (nothing for rank 0) and return os.

Member Data Documentation

◆ T_IS_CONST

template<typename T >
bool psalg::NDArray< T >::T_IS_CONST = std::is_const<T>::value

True if T is const. It is a non-static member, so every object stores it.


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