Stratax 0.2.0
Loading...
Searching...
No Matches
stratax::container::Tensor< T > Class Template Reference

Stores an N-dimensional Stratax array in contiguous memory. More...

#include <Tensor.hpp>

Public Types

using value_type = T
 Element type stored by the tensor.
using iterator = typename core::Buffer<T>::iterator
 Mutable iterator over tensor elements.
using const_iterator = typename core::Buffer<T>::const_iterator
 Const iterator over tensor elements.
using reverse_iterator = typename core::Buffer<T>::reverse_iterator
 Mutable reverse iterator over tensor elements.
using const_reverse_iterator = typename core::Buffer<T>::const_reverse_iterator
 Const reverse iterator over tensor elements.
template<typename U>
using rebind = Tensor<U>
 Rebinds the tensor container to another element type.

Public Member Functions

 Tensor () noexcept=default
 Creates an empty tensor with rank 0.
 Tensor (const core::Shape &shape)
 Creates a tensor with default-initialized storage for a shape.
 Tensor (const core::Shape &shape, const T &value)
 Creates a tensor from a shape and fills it with a value.
 Tensor (const Tensor &)=default
 Creates a copy of another tensor.
 Tensor (Tensor &&) noexcept=default
 Transfers ownership from another tensor.
Tensoroperator= (const Tensor &)=default
 Replaces this tensor with a copy of another tensor.
Tensoroperator= (Tensor &&) noexcept=default
 Replaces this tensor by taking ownership from another tensor.
 ~Tensor ()=default
 Destroys the tensor.
std::size_t size () const noexcept
 Returns the total number of elements in the tensor.
bool empty () const noexcept
 Returns whether the tensor contains no elements.
std::size_t rank () const noexcept
 Returns the tensor rank.
const core::Shapeshape () const noexcept
 Returns the tensor shape.
const core::Stridesstrides () const noexcept
 Returns the tensor strides.
T & operator() (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator() (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
T & operator[] (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator[] (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
template<typename... Rest>
T & operator() (std::size_t first, std::size_t second, Rest... rest)
 Returns an element by multidimensional index with bounds checking.
template<typename... Rest>
const T & operator() (std::size_t first, std::size_t second, Rest... rest) const
 Returns an element by multidimensional index with bounds checking.
T & operator() (const std::vector< std::size_t > &indices)
 Returns an element by multidimensional index from a vector with bounds checking.
const T & operator() (const std::vector< std::size_t > &indices) const
 Returns an element by multidimensional index from a vector with bounds checking.
T & at (std::ptrdiff_t index)
 Returns an element by rank-1 flat index with bounds checking.
const T & at (std::ptrdiff_t index) const
 Returns an element by rank-1 flat index with bounds checking.
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
T & at (std::ptrdiff_t first, std::ptrdiff_t second, Rest... rest)
 Returns an element by multi-index with bounds checking.
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
const T & at (std::ptrdiff_t first, std::ptrdiff_t second, Rest... rest) const
 Returns an element by multi-index with bounds checking.
T & front ()
 Returns the first element.
const T & front () const
 Returns the first element as a const reference.
T & back ()
 Returns the last element.
const T & back () const
 Returns the last element as a const reference.
T * data () noexcept
 Returns the raw data pointer.
const T * data () const noexcept
 Returns the raw data pointer as a const pointer.
iterator begin () noexcept
 Returns an iterator to the first element.
const_iterator begin () const noexcept
 Returns a const iterator to the first element.
const_iterator cbegin () const noexcept
 Returns a const iterator to the first element.
iterator end () noexcept
 Returns an iterator one past the last element.
const_iterator end () const noexcept
 Returns a const iterator one past the last element.
const_iterator cend () const noexcept
 Returns a const iterator one past the last element.
reverse_iterator rbegin () noexcept
 Returns a reverse iterator to the last element.
const_reverse_iterator rbegin () const noexcept
 Returns a const reverse iterator to the last element.
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the last element.
reverse_iterator rend () noexcept
 Returns a reverse iterator before the first element.
const_reverse_iterator rend () const noexcept
 Returns a const reverse iterator before the first element.
const_reverse_iterator crend () const noexcept
 Returns a const reverse iterator before the first element.
void fill (const T &value)
 Fills every element with the same value.
void swap (Tensor &other) noexcept
 Swaps the contents of two tensors.

Detailed Description

template<typename T>
requires Numeric<T>
class stratax::container::Tensor< T >

Stores an N-dimensional Stratax array in contiguous memory.

Tensor owns element storage and shape metadata for generic multi-dimensional array operations.

Template Parameters
TNumeric element type.

Definition at line 30 of file Tensor.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename T>
using stratax::container::Tensor< T >::const_iterator = typename core::Buffer<T>::const_iterator

Const iterator over tensor elements.

Definition at line 45 of file Tensor.hpp.

◆ const_reverse_iterator

template<typename T>
using stratax::container::Tensor< T >::const_reverse_iterator = typename core::Buffer<T>::const_reverse_iterator

Const reverse iterator over tensor elements.

Definition at line 51 of file Tensor.hpp.

◆ iterator

template<typename T>
using stratax::container::Tensor< T >::iterator = typename core::Buffer<T>::iterator

Mutable iterator over tensor elements.

Definition at line 42 of file Tensor.hpp.

◆ rebind

template<typename T>
template<typename U>
using stratax::container::Tensor< T >::rebind = Tensor<U>

Rebinds the tensor container to another element type.

Definition at line 55 of file Tensor.hpp.

◆ reverse_iterator

template<typename T>
using stratax::container::Tensor< T >::reverse_iterator = typename core::Buffer<T>::reverse_iterator

Mutable reverse iterator over tensor elements.

Definition at line 48 of file Tensor.hpp.

◆ value_type

template<typename T>
using stratax::container::Tensor< T >::value_type = T

Element type stored by the tensor.

Definition at line 39 of file Tensor.hpp.

Constructor & Destructor Documentation

◆ Tensor() [1/3]

template<typename T>
stratax::container::Tensor< T >::Tensor ( )
defaultnoexcept

Creates an empty tensor with rank 0.

The tensor has no dimensions, no strides, and owns no elements.

◆ Tensor() [2/3]

template<typename T>
stratax::container::Tensor< T >::Tensor ( const core::Shape & shape)
inlineexplicit

Creates a tensor with default-initialized storage for a shape.

The tensor rank and strides are derived directly from shape.

Parameters
shapeSource shape.

Definition at line 73 of file Tensor.hpp.

◆ Tensor() [3/3]

template<typename T>
stratax::container::Tensor< T >::Tensor ( const core::Shape & shape,
const T & value )
inline

Creates a tensor from a shape and fills it with a value.

Parameters
shapeSource shape.
valueValue copied into every element.

Definition at line 87 of file Tensor.hpp.

Member Function Documentation

◆ at() [1/4]

template<typename T>
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
T & stratax::container::Tensor< T >::at ( std::ptrdiff_t first,
std::ptrdiff_t second,
Rest... rest )
inline

Returns an element by multi-index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index rank or bounds are invalid.

Definition at line 346 of file Tensor.hpp.

◆ at() [2/4]

template<typename T>
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
const T & stratax::container::Tensor< T >::at ( std::ptrdiff_t first,
std::ptrdiff_t second,
Rest... rest ) const
inline

Returns an element by multi-index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index rank or bounds are invalid.

Definition at line 395 of file Tensor.hpp.

◆ at() [3/4]

template<typename T>
T & stratax::container::Tensor< T >::at ( std::ptrdiff_t index)
inline

Returns an element by rank-1 flat index with bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index is out of bounds.

Definition at line 311 of file Tensor.hpp.

◆ at() [4/4]

template<typename T>
const T & stratax::container::Tensor< T >::at ( std::ptrdiff_t index) const
inline

Returns an element by rank-1 flat index with bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf the index is out of bounds.

Definition at line 326 of file Tensor.hpp.

◆ back() [1/2]

template<typename T>
T & stratax::container::Tensor< T >::back ( )
inline

Returns the last element.

Returns
Mutable reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 459 of file Tensor.hpp.

◆ back() [2/2]

template<typename T>
const T & stratax::container::Tensor< T >::back ( ) const
inline

Returns the last element as a const reference.

Returns
Const reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 470 of file Tensor.hpp.

◆ begin() [1/2]

template<typename T>
const_iterator stratax::container::Tensor< T >::begin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the tensor.

Definition at line 510 of file Tensor.hpp.

◆ begin() [2/2]

template<typename T>
iterator stratax::container::Tensor< T >::begin ( )
inlinenodiscardnoexcept

Returns an iterator to the first element.

Returns
Iterator to the beginning of the tensor.

Definition at line 500 of file Tensor.hpp.

◆ cbegin()

template<typename T>
const_iterator stratax::container::Tensor< T >::cbegin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the tensor.

Definition at line 520 of file Tensor.hpp.

◆ cend()

template<typename T>
const_iterator stratax::container::Tensor< T >::cend ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the tensor.

Definition at line 550 of file Tensor.hpp.

◆ crbegin()

template<typename T>
const_reverse_iterator stratax::container::Tensor< T >::crbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 580 of file Tensor.hpp.

◆ crend()

template<typename T>
const_reverse_iterator stratax::container::Tensor< T >::crend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 610 of file Tensor.hpp.

◆ data() [1/2]

template<typename T>
const T * stratax::container::Tensor< T >::data ( ) const
inlinenodiscardnoexcept

Returns the raw data pointer as a const pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 490 of file Tensor.hpp.

◆ data() [2/2]

template<typename T>
T * stratax::container::Tensor< T >::data ( )
inlinenodiscardnoexcept

Returns the raw data pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 480 of file Tensor.hpp.

◆ empty()

template<typename T>
bool stratax::container::Tensor< T >::empty ( ) const
inlinenodiscardnoexcept

Returns whether the tensor contains no elements.

Returns
true when the tensor size is zero.

Definition at line 139 of file Tensor.hpp.

◆ end() [1/2]

template<typename T>
const_iterator stratax::container::Tensor< T >::end ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the tensor.

Definition at line 540 of file Tensor.hpp.

◆ end() [2/2]

template<typename T>
iterator stratax::container::Tensor< T >::end ( )
inlinenodiscardnoexcept

Returns an iterator one past the last element.

Returns
Iterator to the end of the tensor.

Definition at line 530 of file Tensor.hpp.

◆ fill()

template<typename T>
void stratax::container::Tensor< T >::fill ( const T & value)
inline

Fills every element with the same value.

Parameters
valueValue to assign to each element.

Definition at line 620 of file Tensor.hpp.

◆ front() [1/2]

template<typename T>
T & stratax::container::Tensor< T >::front ( )
inline

Returns the first element.

Returns
Mutable reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 437 of file Tensor.hpp.

◆ front() [2/2]

template<typename T>
const T & stratax::container::Tensor< T >::front ( ) const
inline

Returns the first element as a const reference.

Returns
Const reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the tensor is empty.

Definition at line 448 of file Tensor.hpp.

◆ operator()() [1/6]

template<typename T>
T & stratax::container::Tensor< T >::operator() ( const std::vector< std::size_t > & indices)
inline

Returns an element by multidimensional index from a vector with bounds checking.

Parameters
indicesVector of index components, one per dimension.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the vector size does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 284 of file Tensor.hpp.

◆ operator()() [2/6]

template<typename T>
const T & stratax::container::Tensor< T >::operator() ( const std::vector< std::size_t > & indices) const
inline

Returns an element by multidimensional index from a vector with bounds checking.

Parameters
indicesVector of index components, one per dimension.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the vector size does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 298 of file Tensor.hpp.

◆ operator()() [3/6]

template<typename T>
template<typename... Rest>
T & stratax::container::Tensor< T >::operator() ( std::size_t first,
std::size_t second,
Rest... rest )
inline

Returns an element by multidimensional index with bounds checking.

At least two index components are required; use the flat overload for rank-1 or flat storage access.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the number of indices does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 241 of file Tensor.hpp.

◆ operator()() [4/6]

template<typename T>
template<typename... Rest>
const T & stratax::container::Tensor< T >::operator() ( std::size_t first,
std::size_t second,
Rest... rest ) const
inline

Returns an element by multidimensional index with bounds checking.

Parameters
firstFirst index component.
secondSecond index component.
restRemaining index components.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::DimensionErrorIf the number of indices does not match the tensor rank.
Exceptions::IndexErrorIf any index component is out of bounds or the offset overflows.

Definition at line 264 of file Tensor.hpp.

◆ operator()() [5/6]

template<typename T>
const T & stratax::container::Tensor< T >::operator() ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 195 of file Tensor.hpp.

◆ operator()() [6/6]

template<typename T>
T & stratax::container::Tensor< T >::operator() ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 182 of file Tensor.hpp.

◆ operator[]() [1/2]

template<typename T>
const T & stratax::container::Tensor< T >::operator[] ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 221 of file Tensor.hpp.

◆ operator[]() [2/2]

template<typename T>
T & stratax::container::Tensor< T >::operator[] ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 208 of file Tensor.hpp.

◆ rank()

template<typename T>
std::size_t stratax::container::Tensor< T >::rank ( ) const
inlinenodiscardnoexcept

Returns the tensor rank.

Returns
Number of dimensions described by the tensor shape.

Definition at line 149 of file Tensor.hpp.

◆ rbegin() [1/2]

template<typename T>
const_reverse_iterator stratax::container::Tensor< T >::rbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 570 of file Tensor.hpp.

◆ rbegin() [2/2]

template<typename T>
reverse_iterator stratax::container::Tensor< T >::rbegin ( )
inlinenodiscardnoexcept

Returns a reverse iterator to the last element.

Returns
Reverse iterator starting at the last element.

Definition at line 560 of file Tensor.hpp.

◆ rend() [1/2]

template<typename T>
const_reverse_iterator stratax::container::Tensor< T >::rend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 600 of file Tensor.hpp.

◆ rend() [2/2]

template<typename T>
reverse_iterator stratax::container::Tensor< T >::rend ( )
inlinenodiscardnoexcept

Returns a reverse iterator before the first element.

Returns
Reverse iterator representing the end sentinel.

Definition at line 590 of file Tensor.hpp.

◆ shape()

template<typename T>
const core::Shape & stratax::container::Tensor< T >::shape ( ) const
inlinenoexcept

Returns the tensor shape.

Returns
Shape describing the tensor.

Definition at line 159 of file Tensor.hpp.

◆ size()

template<typename T>
std::size_t stratax::container::Tensor< T >::size ( ) const
inlinenodiscardnoexcept

Returns the total number of elements in the tensor.

Returns
Number of stored elements.

Definition at line 129 of file Tensor.hpp.

◆ strides()

template<typename T>
const core::Strides & stratax::container::Tensor< T >::strides ( ) const
inlinenoexcept

Returns the tensor strides.

Returns
Row-major strides describing the tensor layout.

Definition at line 169 of file Tensor.hpp.

◆ swap()

template<typename T>
void stratax::container::Tensor< T >::swap ( Tensor< T > & other)
inlinenoexcept

Swaps the contents of two tensors.

Parameters
otherTensor to exchange state with.

Definition at line 630 of file Tensor.hpp.


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