libnpy 2.0.0
Lightweight C++ library for reading and writing NPY and NPZ files
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
npy::tensor< T > Class Template Reference

The default tensor class. More...

#include <npy.h>

Public Types

typedef T value_type
 The value type of the tensor.
 
typedef value_typereference
 The reference type of the tensor.
 
typedef const value_typeconst_reference
 The const reference type of the tensor.
 
typedef value_typepointer
 The pointer type of the tensor.
 
typedef const value_typeconst_pointer
 The const pointer type of the tensor.
 

Public Member Functions

 tensor (const std::vector< size_t > &shape)
 Constructor.
 
 tensor (const std::vector< size_t > &shape, bool fortran_order)
 Constructor.
 
 tensor (const tensor< T > &other)
 Copy constructor.
 
 tensor (tensor< T > &&other)
 Move constructor.
 
void save (std::basic_ostream< char > &output, endian_t endianness) const
 Save the tensor to the provided stream.
 
template<typename... Indices>
const T & operator() (Indices... index) const
 Variable parameter index function.
 
const T & operator() (const std::vector< std::size_t > &multi_index) const
 Index function.
 
template<typename... Indices>
T & operator() (Indices... index)
 Variable parameter index function.
 
T & operator() (const std::vector< std::size_t > &multi_index)
 Index function.
 
std::vector< T >::iterator begin ()
 Iterator pointing at the beginning of the tensor in memory.
 
std::vector< T >::const_iterator begin () const
 Iterator pointing at the beginning of the tensor in memory.
 
std::vector< T >::iterator end ()
 Iterator pointing at the end of the tensor in memory.
 
std::vector< T >::const_iterator end () const
 Iterator pointing at the end of the tensor in memory.
 
void set (const std::vector< std::int32_t > &multi_index, const T &value)
 Sets the value at the provided index.
 
const T & get (const std::vector< std::int32_t > &multi_index) const
 Gets the value at the provided index.
 
std::string dtype (endian_t endianness) const
 The data type of the tensor.
 
data_type_t dtype () const
 The data type of the tensor.
 
const std::vector< T > & values () const
 The underlying values buffer.
 
void copy_from (const T *source, size_t nitems)
 Copy values from the source to this tensor.
 
void copy_from (const std::vector< T > &source)
 Copy values from the provided vector.
 
void move_from (std::vector< T > &&source)
 Move values from the provided vector.
 
T * data ()
 A pointer to the start of the underlying values buffer.
 
const T * data () const
 A pointer to the start of the underlying values buffer.
 
size_t size () const
 The number of elements in the tensor.
 
const std::vector< size_t > & shape () const
 The shape of the vector. Each element is the size of the corresponding dimension.
 
size_t shape (int index) const
 Returns the dimensionality of the tensor at the specified index.
 
size_t ndim () const
 The number of dimensions of the tensor.
 
bool fortran_order () const
 Whether the tensor data is stored in FORTRAN, or column-major, order.
 
tensor< T > & operator= (const tensor< T > &other)
 Copy assignment operator.
 
tensor< T > & operator= (tensor< T > &&other)
 Move assignment operator.
 
void save (const std::string &path, endian_t endianness=npy::endian_t::NATIVE)
 Save this tensor to the provided location on disk.
 
template<class INDEX_IT , class SHAPE_IT >
size_t ravel (INDEX_IT index, SHAPE_IT shape) const
 Ravels a multi-index into a single value indexing the buffer.
 
size_t ravel (const std::vector< std::int32_t > &multi_index) const
 Ravels a multi-index into a single value indexing the buffer.
 
size_t ravel (const std::vector< std::size_t > &abs_multi_index) const
 Ravels a multi-index into a single value indexing the buffer.
 
std::string dtype (endian_t endianness) const
 Specialization of dtype for std::wstring tensors.
 

Static Public Member Functions

static tensor< T > from_file (const std::string &path)
 Load a tensor from the specified location on disk.
 
static tensor< T > load (std::basic_istream< char > &input, const header_info &info)
 Load a tensor from the provided stream.
 

Detailed Description

template<typename T>
class npy::tensor< T >

The default tensor class.

This class can be used as a data exchange format for the library, but the methods and classes will also work with your own tensor implementation. The library methods require the following methods to be present in a tensor type:

As long as these are present and have the same semantics, the library should handle them in the same was as this implementation. Only certain type of tensor objects are natively supported (see npy::data_type_t).

Note
This class is not optimized for access speed. It is intended as a simple data exchange format. Once the raw data has been extracted from the NPY or NPZ, it is recommended to convert it to a more efficient format for processing using the data() method.

Member Typedef Documentation

◆ const_pointer

template<typename T >
typedef const value_type* npy::tensor< T >::const_pointer

The const pointer type of the tensor.

◆ const_reference

template<typename T >
typedef const value_type& npy::tensor< T >::const_reference

The const reference type of the tensor.

◆ pointer

template<typename T >
typedef value_type* npy::tensor< T >::pointer

The pointer type of the tensor.

◆ reference

template<typename T >
typedef value_type& npy::tensor< T >::reference

The reference type of the tensor.

◆ value_type

template<typename T >
typedef T npy::tensor< T >::value_type

The value type of the tensor.

Constructor & Destructor Documentation

◆ tensor() [1/4]

template<typename T >
npy::tensor< T >::tensor ( const std::vector< size_t > &  shape)
inline

Constructor.

This will allocate a data buffer of the appropriate size in row-major order.

Parameters
shapethe shape of the tensor

◆ tensor() [2/4]

template<typename T >
npy::tensor< T >::tensor ( const std::vector< size_t > &  shape,
bool  fortran_order 
)
inline

Constructor.

This will allocate a data buffer of the appropriate size.

Parameters
shapethe shape of the tensor
fortran_orderwhether the data is stored in FORTRAN, or column major, order

◆ tensor() [3/4]

template<typename T >
npy::tensor< T >::tensor ( const tensor< T > &  other)
inline

Copy constructor.

◆ tensor() [4/4]

template<typename T >
npy::tensor< T >::tensor ( tensor< T > &&  other)
inline

Move constructor.

Member Function Documentation

◆ begin() [1/2]

template<typename T >
std::vector< T >::iterator npy::tensor< T >::begin ( )
inline

Iterator pointing at the beginning of the tensor in memory.

◆ begin() [2/2]

template<typename T >
std::vector< T >::const_iterator npy::tensor< T >::begin ( ) const
inline

Iterator pointing at the beginning of the tensor in memory.

◆ copy_from() [1/2]

template<typename T >
void npy::tensor< T >::copy_from ( const std::vector< T > &  source)
inline

Copy values from the provided vector.

Parameters
sourcethe source vector. Should have the same size as values.

◆ copy_from() [2/2]

template<typename T >
void npy::tensor< T >::copy_from ( const T *  source,
size_t  nitems 
)
inline

Copy values from the source to this tensor.

Parameters
sourcepointer to the start of the source buffer
nitemsthe number of items to copy. Should be equal to size.

◆ data() [1/2]

template<typename T >
T * npy::tensor< T >::data ( )
inline

A pointer to the start of the underlying values buffer.

◆ data() [2/2]

template<typename T >
const T * npy::tensor< T >::data ( ) const
inline

A pointer to the start of the underlying values buffer.

◆ dtype() [1/3]

template<typename T >
data_type_t npy::tensor< T >::dtype ( ) const
inline

The data type of the tensor.

This is one of the methods required by the library to read and write NPY files.

Returns
the data type of the tensor

◆ dtype() [2/3]

template<typename T >
std::string npy::tensor< T >::dtype ( endian_t  endianness) const
inline

The data type of the tensor.

◆ dtype() [3/3]

std::string npy::tensor< std::wstring >::dtype ( endian_t  endianness) const
inline

Specialization of dtype for std::wstring tensors.

◆ end() [1/2]

template<typename T >
std::vector< T >::iterator npy::tensor< T >::end ( )
inline

Iterator pointing at the end of the tensor in memory.

◆ end() [2/2]

template<typename T >
std::vector< T >::const_iterator npy::tensor< T >::end ( ) const
inline

Iterator pointing at the end of the tensor in memory.

◆ fortran_order()

template<typename T >
bool npy::tensor< T >::fortran_order ( ) const
inline

Whether the tensor data is stored in FORTRAN, or column-major, order.

This is one of the methods required by the library to read and write NPY files.

Returns
whether the tensor is stored in FORTRAN order

◆ from_file()

template<typename T >
static tensor< T > npy::tensor< T >::from_file ( const std::string &  path)
inlinestatic

Load a tensor from the specified location on disk.

◆ get()

template<typename T >
const T & npy::tensor< T >::get ( const std::vector< std::int32_t > &  multi_index) const
inline

Gets the value at the provided index.

Parameters
multi_indexthe index into the tensor
Returns
the value at the provided index

◆ load()

template<typename T >
static tensor< T > npy::tensor< T >::load ( std::basic_istream< char > &  input,
const header_info info 
)
inlinestatic

Load a tensor from the provided stream.

This is one of the methods required by the library to read NPY files. If you implement this in a custom tensor, you will need to populate your internal data structure using the provided stream and header information. The npy::read_values method can be used to read raw data from the stream.

Parameters
inputthe input stream
infothe header information
Returns
an instance of the tensor read from the stream
See also
npy::read_values

◆ move_from()

template<typename T >
void npy::tensor< T >::move_from ( std::vector< T > &&  source)
inline

Move values from the provided vector.

Parameters
sourcethe source vector. Should have the same size as values.

◆ ndim()

template<typename T >
size_t npy::tensor< T >::ndim ( ) const
inline

The number of dimensions of the tensor.

This is one of the methods required by the library to read and write NPY files.

Returns
the number of dimensions

◆ operator()() [1/4]

template<typename T >
T & npy::tensor< T >::operator() ( const std::vector< std::size_t > &  multi_index)
inline

Index function.

Parameters
multi_indexthe index into the tensor
Returns
the value at the provided index

◆ operator()() [2/4]

template<typename T >
const T & npy::tensor< T >::operator() ( const std::vector< std::size_t > &  multi_index) const
inline

Index function.

Parameters
multi_indexthe index into the tensor
Returns
the value at the provided index

◆ operator()() [3/4]

template<typename T >
template<typename... Indices>
T & npy::tensor< T >::operator() ( Indices...  index)
inline

Variable parameter index function.

Parameters
indexan index into the tensor. Can be negative (in which case it will work as in numpy)
Returns
the value at the provided index

◆ operator()() [4/4]

template<typename T >
template<typename... Indices>
const T & npy::tensor< T >::operator() ( Indices...  index) const
inline

Variable parameter index function.

Parameters
indexan index into the tensor. Can be negative (in which case it will work as in numpy)
Returns
the value at the provided index

◆ operator=() [1/2]

template<typename T >
tensor< T > & npy::tensor< T >::operator= ( const tensor< T > &  other)
inline

Copy assignment operator.

◆ operator=() [2/2]

template<typename T >
tensor< T > & npy::tensor< T >::operator= ( tensor< T > &&  other)
inline

Move assignment operator.

◆ ravel() [1/3]

template<typename T >
size_t npy::tensor< T >::ravel ( const std::vector< std::int32_t > &  multi_index) const
inline

Ravels a multi-index into a single value indexing the buffer.

Parameters
multi_indexthe multi-index value
Returns
the single value in the buffer corresponding to the multi-index

◆ ravel() [2/3]

template<typename T >
size_t npy::tensor< T >::ravel ( const std::vector< std::size_t > &  abs_multi_index) const
inline

Ravels a multi-index into a single value indexing the buffer.

Parameters
abs_multi_indexthe multi-index value
Returns
the single value in the buffer corresponding to the multi-index

◆ ravel() [3/3]

template<typename T >
template<class INDEX_IT , class SHAPE_IT >
size_t npy::tensor< T >::ravel ( INDEX_IT  index,
SHAPE_IT  shape 
) const
inline

Ravels a multi-index into a single value indexing the buffer.

Template Parameters
INDEX_ITthe index iterator class
SHAPE_ITthe shape iterator class
Parameters
indexthe multi-index iterator
shapethe shape iterator
Returns
the single value in the buffer corresponding to the multi-index

◆ save() [1/2]

template<typename T >
void npy::tensor< T >::save ( const std::string &  path,
endian_t  endianness = npy::endian_t::NATIVE 
)
inline

Save this tensor to the provided location on disk.

Parameters
patha valid location on disk
endiannessthe endianness to use in writing the tensor

◆ save() [2/2]

template<typename T >
void npy::tensor< T >::save ( std::basic_ostream< char > &  output,
endian_t  endianness 
) const
inline

Save the tensor to the provided stream.

This is one of the methods required by the library to write NPY files. If you implement this in a custom tensor, you will need to write your internal data structure to the provided stream. The npy::write_values method can be used to write raw data to the stream.

Parameters
outputthe output stream
endiannessthe endianness to use in writing the data
See also
npy::write_values

◆ set()

template<typename T >
void npy::tensor< T >::set ( const std::vector< std::int32_t > &  multi_index,
const T &  value 
)
inline

Sets the value at the provided index.

Parameters
multi_indexan index into the tensor
valuethe value to set

◆ shape() [1/2]

template<typename T >
const std::vector< size_t > & npy::tensor< T >::shape ( ) const
inline

The shape of the vector. Each element is the size of the corresponding dimension.

◆ shape() [2/2]

template<typename T >
size_t npy::tensor< T >::shape ( int  index) const
inline

Returns the dimensionality of the tensor at the specified index.

This is one of the methods required by the library to read and write NPY files.

Parameters
indexindex into the shape
Returns
the dimensionality at the index

◆ size()

template<typename T >
size_t npy::tensor< T >::size ( ) const
inline

The number of elements in the tensor.

◆ values()

template<typename T >
const std::vector< T > & npy::tensor< T >::values ( ) const
inline

The underlying values buffer.


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