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

Arbitrary-rank owning array of numeric values. More...

#include <Tensor.hpp>

Inheritance diagram for stratax::container::Tensor< T >:
Collaboration diagram for stratax::container::Tensor< T >:

Public Types

using value_type = typename core::ArrayBase< T >::value_type
 Stored element type inherited from ArrayBase.
 
using size_type = typename core::ArrayBase< T >::size_type
 Unsigned type used for element counts and normalized indices.
 
using difference_type = typename core::ArrayBase< T >::difference_type
 Signed type used for checked indices and iterator distances.
 
using reference = typename core::ArrayBase< T >::reference
 Mutable element reference type.
 
using const_reference = typename core::ArrayBase< T >::const_reference
 Read-only element reference type.
 
using pointer = typename core::ArrayBase< T >::pointer
 Mutable element pointer type.
 
using const_pointer = typename core::ArrayBase< T >::const_pointer
 Read-only element pointer type.
 
using iterator = typename core::ArrayBase< T >::iterator
 Mutable contiguous random-access iterator type.
 
using const_iterator = typename core::ArrayBase< T >::const_iterator
 Read-only contiguous random-access iterator type.
 
using reverse_iterator = typename core::ArrayBase< T >::reverse_iterator
 Mutable reverse iterator type.
 
using const_reverse_iterator = typename core::ArrayBase< T >::const_reverse_iterator
 Read-only reverse iterator type.
 
- Public Types inherited from stratax::core::ArrayBase< T >
using value_type = typename Buffer< T >::value_type
 Stored element type.
 
using size_type = typename Buffer< value_type >::size_type
 Unsigned type used for element counts and indices.
 
using difference_type = typename Buffer< value_type >::difference_type
 Signed type used for checked indices and iterator distances.
 
using reference = typename Buffer< value_type >::reference
 Mutable element reference type.
 
using const_reference = typename Buffer< value_type >::const_reference
 Read-only element reference type.
 
using pointer = typename Buffer< value_type >::pointer
 Mutable element pointer type.
 
using const_pointer = typename Buffer< value_type >::const_pointer
 Read-only element pointer type.
 
using iterator = typename Buffer< value_type >::iterator
 Mutable contiguous random-access iterator type.
 
using const_iterator = typename Buffer< value_type >::const_iterator
 Read-only contiguous random-access iterator type.
 
using reverse_iterator = typename Buffer< value_type >::reverse_iterator
 Mutable reverse iterator type.
 
using const_reverse_iterator = typename Buffer< value_type >::const_reverse_iterator
 Read-only reverse iterator type.
 

Public Member Functions

 Tensor ()
 Constructs an empty rank-one tensor with shape {0}.
 
 Tensor (const core::Shape &shape)
 Constructs value-initialized storage for an arbitrary shape.
 
 Tensor (const core::Shape &shape, const_reference value)
 Constructs a tensor filled with copies of value.
 
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
reference operator() (size_type first, Rest... rest)
 Returns an element using unchecked variadic indices.
 
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
const_reference operator() (size_type first, Rest... rest) const
 Returns an element using unchecked variadic indices.
 
reference operator() (const std::vector< size_type > &indices)
 Returns an element using unchecked vector-based indices.
 
const_reference operator() (const std::vector< size_type > &indices) const
 Returns an element using unchecked vector-based indices.
 
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
reference at (difference_type first, Rest... rest)
 Returns an element using checked variadic indices.
 
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
const_reference at (difference_type first, Rest... rest) const
 Returns an element using checked variadic indices.
 
reference at (const std::vector< difference_type > &raw_indices)
 Returns an element using checked vector-based indices.
 
const_reference at (const std::vector< difference_type > &raw_indices) const
 Returns an element using checked vector-based indices.
 
void swap (Tensor &other) noexcept
 Exchanges storage and layout metadata with other.
 
- Public Member Functions inherited from stratax::core::ArrayBase< T >
size_type size () const noexcept
 Returns the number of stored elements.
 
bool empty () const noexcept
 Reports whether no elements are stored.
 
size_type rank () const noexcept
 Returns the number of logical dimensions.
 
const Shapeshape () const noexcept
 Returns the logical shape metadata.
 
const Shapestrides () const noexcept
 Returns the row-major stride metadata.
 
pointer data () noexcept
 Returns a pointer to contiguous mutable element storage.
 
const_pointer data () const noexcept
 Returns a pointer to contiguous read-only element storage.
 
reference front ()
 Returns the first element.
 
const_reference front () const
 Returns the first element.
 
reference back ()
 Returns the final element.
 
const_reference back () const
 Returns the final element.
 
reference operator[] (size_type index) noexcept
 Returns an element without bounds checking.
 
const_reference operator[] (size_type index) const noexcept
 Returns an element without bounds checking.
 
reference at (difference_type index)
 Returns an element using checked, Python-style flat indexing.
 
const_reference at (difference_type index) const
 Returns an element using checked, Python-style flat indexing.
 
iterator begin () noexcept
 Returns a mutable 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 a mutable iterator past the final element.
 
const_iterator end () const noexcept
 Returns a const iterator past the final element.
 
const_iterator cend () const noexcept
 Returns a const iterator past the final element.
 
reverse_iterator rbegin () noexcept
 Returns a mutable reverse iterator to the final element.
 
const_reverse_iterator rbegin () const noexcept
 Returns a const reverse iterator to the final element.
 
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the final element.
 
reverse_iterator rend () noexcept
 Returns the past-the-end mutable reverse iterator.
 
const_reverse_iterator rend () const noexcept
 Returns the past-the-end const reverse iterator.
 
const_reverse_iterator crend () const noexcept
 Returns the past-the-end const reverse iterator.
 
void fill (const_reference value)
 Assigns value to every stored element.
 
void swap (ArrayBase &other) noexcept
 Exchanges storage and layout metadata with other.
 

Friends

void swap (Tensor &lhs, Tensor &rhs) noexcept
 Exchanges two tensors using argument-dependent lookup.
 

Additional Inherited Members

- Static Public Member Functions inherited from stratax::core::ArrayBase< T >
static constexpr std::string_view dtype () noexcept
 Returns the canonical name of the stored element dtype.
 
- Protected Member Functions inherited from stratax::core::ArrayBase< T >
 ArrayBase (const Shape &shape)
 Constructs value-initialized storage for shape.
 
 ArrayBase (const Shape &shape, const_reference value)
 Constructs storage filled with value for shape.
 
 ArrayBase (const Shape &shape, Buffer< value_type > &&buffer)
 Adopts an existing buffer for shape.
 
template<typename IndexContainer >
size_type normalized_flat_offset (const IndexContainer &raw_indices) const
 Converts checked signed multidimensional indices to a flat offset.
 

Detailed Description

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

Arbitrary-rank owning array of numeric values.

Forward declaration of the arbitrary-rank dtype array container.

Tensor combines contiguous element storage with a logical Shape and its canonical row-major stride metadata. It supports unchecked multidimensional access with already-normalized unsigned indices and checked multidimensional access with signed, Python-style indices. The flat container interface is inherited from core::ArrayBase.

Template Parameters
TElement type satisfying the DType concept.
Invariants
Example
0.0);
tensor(1, 2, 3) = 7.0; // unchecked multidimensional access
tensor.at(-1, -1, -1); // 7.0; checked negative indices
tensor.at(-1); // checked flat access inherited from ArrayBase
Arbitrary-rank owning array of numeric values.
Definition Tensor.hpp:50
reference at(difference_type first, Rest... rest)
Returns an element using checked variadic indices.
Definition Tensor.hpp:191
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
See also
core::ArrayBase
core::Shape

Definition at line 49 of file Tensor.hpp.

Member Typedef Documentation

◆ const_iterator

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

Read-only contiguous random-access iterator type.

Definition at line 69 of file Tensor.hpp.

◆ const_pointer

template<typename T >
using stratax::container::Tensor< T >::const_pointer = typename core::ArrayBase<T>::const_pointer

Read-only element pointer type.

Definition at line 65 of file Tensor.hpp.

◆ const_reference

template<typename T >
using stratax::container::Tensor< T >::const_reference = typename core::ArrayBase<T>::const_reference

Read-only element reference type.

Definition at line 61 of file Tensor.hpp.

◆ const_reverse_iterator

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

Read-only reverse iterator type.

Definition at line 73 of file Tensor.hpp.

◆ difference_type

template<typename T >
using stratax::container::Tensor< T >::difference_type = typename core::ArrayBase<T>::difference_type

Signed type used for checked indices and iterator distances.

Definition at line 57 of file Tensor.hpp.

◆ iterator

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

Mutable contiguous random-access iterator type.

Definition at line 67 of file Tensor.hpp.

◆ pointer

template<typename T >
using stratax::container::Tensor< T >::pointer = typename core::ArrayBase<T>::pointer

Mutable element pointer type.

Definition at line 63 of file Tensor.hpp.

◆ reference

template<typename T >
using stratax::container::Tensor< T >::reference = typename core::ArrayBase<T>::reference

Mutable element reference type.

Definition at line 59 of file Tensor.hpp.

◆ reverse_iterator

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

Mutable reverse iterator type.

Definition at line 71 of file Tensor.hpp.

◆ size_type

template<typename T >
using stratax::container::Tensor< T >::size_type = typename core::ArrayBase<T>::size_type

Unsigned type used for element counts and normalized indices.

Definition at line 55 of file Tensor.hpp.

◆ value_type

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

Stored element type inherited from ArrayBase.

Definition at line 53 of file Tensor.hpp.

Constructor & Destructor Documentation

◆ Tensor() [1/3]

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

Constructs an empty rank-one tensor with shape {0}.

Complexity
O(1).

Definition at line 87 of file Tensor.hpp.

◆ Tensor() [2/3]

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

Constructs value-initialized storage for an arbitrary shape.

Parameters
shapeLogical dimensions in outermost-to-innermost order.
Exceptions
Exceptions::DimensionErrorIf the element or stride count overflows.
std::bad_allocIf storage allocation fails.
Anyexception thrown while value-initializing a value_type.
Complexity
O(shape.elements() + shape.rank()).

Definition at line 97 of file Tensor.hpp.

◆ Tensor() [3/3]

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

Constructs a tensor filled with copies of value.

Parameters
shapeLogical dimensions in outermost-to-innermost order.
valueValue copied into every element.
Exceptions
Exceptions::DimensionErrorIf the element or stride count overflows.
std::bad_allocIf storage allocation fails.
Anyexception thrown by value_type's copy constructor.
Complexity
O(shape.elements() + shape.rank()).

Definition at line 110 of file Tensor.hpp.

Member Function Documentation

◆ at() [1/4]

template<typename T >
reference stratax::container::Tensor< T >::at ( const std::vector< difference_type > &  raw_indices)
inline

Returns an element using checked vector-based indices.

Parameters
raw_indicesOne signed index per tensor dimension.
Exceptions
Exceptions::IndexErrorIf the number of indices differs from rank() or any component is out of bounds.
Complexity
O(rank()).

Definition at line 233 of file Tensor.hpp.

◆ at() [2/4]

template<typename T >
const_reference stratax::container::Tensor< T >::at ( const std::vector< difference_type > &  raw_indices) const
inline

Returns an element using checked vector-based indices.

Parameters
raw_indicesOne signed index per tensor dimension.
Exceptions
Exceptions::IndexErrorIf the number of indices differs from rank() or any component is out of bounds.
Complexity
O(rank()).

Definition at line 245 of file Tensor.hpp.

◆ at() [3/4]

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

Returns an element using checked variadic indices.

Each signed component is normalized independently. Negative values count backward from the end of their corresponding dimensions.

Template Parameters
RestIntegral types of the remaining index components.
Parameters
firstFirst signed index component.
restRemaining signed index components.
Exceptions
Exceptions::IndexErrorIf the number of components differs from rank() or any component is out of bounds.
Complexity
O(rank()).

Definition at line 191 of file Tensor.hpp.

◆ at() [4/4]

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

Returns an element using checked variadic indices.

Each signed component is normalized independently. Negative values count backward from the end of their corresponding dimensions.

Template Parameters
RestIntegral types of the remaining index components.
Parameters
firstFirst signed index component.
restRemaining signed index components.
Exceptions
Exceptions::IndexErrorIf the number of components differs from rank() or any component is out of bounds.
Complexity
O(rank()).

Definition at line 216 of file Tensor.hpp.

◆ operator()() [1/4]

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

Returns an element using unchecked vector-based indices.

Parameters
indicesOne normalized index per tensor dimension.
Precondition
indices.size() == rank() and every component is within its corresponding dimension.
Complexity
O(rank()).

Definition at line 165 of file Tensor.hpp.

◆ operator()() [2/4]

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

Returns an element using unchecked vector-based indices.

Parameters
indicesOne normalized index per tensor dimension.
Precondition
indices.size() == rank() and every component is within its corresponding dimension.
Complexity
O(rank()).

Definition at line 174 of file Tensor.hpp.

◆ operator()() [3/4]

template<typename T >
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
reference stratax::container::Tensor< T >::operator() ( size_type  first,
Rest...  rest 
)
inline

Returns an element using unchecked variadic indices.

Template Parameters
RestIntegral types of the remaining index components.
Parameters
firstFirst normalized index component.
restRemaining normalized index components.
Precondition
Exactly rank() components are supplied and every component is non-negative and within its corresponding dimension.
Complexity
O(rank()).

Definition at line 126 of file Tensor.hpp.

◆ operator()() [4/4]

template<typename T >
template<typename... Rest>
requires ((std::is_integral_v<Rest>) && ...)
const_reference stratax::container::Tensor< T >::operator() ( size_type  first,
Rest...  rest 
) const
inline

Returns an element using unchecked variadic indices.

Template Parameters
RestIntegral types of the remaining index components.
Parameters
firstFirst normalized index component.
restRemaining normalized index components.
Precondition
Exactly rank() components are supplied and every component is non-negative and within its corresponding dimension.
Complexity
O(rank()).

Definition at line 148 of file Tensor.hpp.

◆ swap()

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

Exchanges storage and layout metadata with other.

Parameters
otherTensor to exchange with this tensor.
Complexity
O(1).

Definition at line 255 of file Tensor.hpp.

Friends And Related Symbol Documentation

◆ swap

template<typename T >
void swap ( Tensor< T > &  lhs,
Tensor< T > &  rhs 
)
friend

Exchanges two tensors using argument-dependent lookup.

Parameters
lhsFirst tensor.
rhsSecond tensor.
Complexity
O(1).

Definition at line 263 of file Tensor.hpp.


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