Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::Strides Class Reference

Represents the memory strides associated with a tensor shape. More...

#include <strides.hpp>

Public Types

using rank_type = uint8_t
 Type used for rank (number of dimensions).
 
using size_type = int64_t
 Type used for size and shape dimensions.
 

Public Member Functions

constexpr Strides () noexcept=default
 Default constructor (rank 0).
 
template<Integral... Sizes>
constexpr Strides (Sizes... sizes)
 Constructs strides from variadic size arguments.
 
template<Iterator Iterator>
constexpr Strides (Iterator begin, Iterator end)
 Constructs strides from a pair of iterators.
 
constexpr Strides (const Shape &shape)
 Constructs strides from a shape assuming row-major layout.
 
constexpr size_typeaddress () noexcept
 Returns a pointer to the underlying data (non-const).
 
constexpr size_type const * address () const noexcept
 Returns a pointer to the underlying data (const).
 
constexpr auto rank () const noexcept
 Returns the number of dimensions (rank).
 
constexpr auto front () const
 Returns the first stride value.
 
constexpr auto back () const
 Returns the last stride value.
 
template<Integral Index>
constexpr auto const & operator[] (Index index) const
 Accesses a stride value by index (const).
 
template<Integral Index>
constexpr auto & operator[] (Index index)
 Accesses a stride value by index (non-const).
 
constexpr void expand (size_type size)
 Expands the strides's last dimension with a given size.
 
Iterators


constexpr auto begin ()
 
constexpr auto end ()
 
constexpr auto begin () const
 
constexpr auto end () const
 
constexpr auto cbegin () const
 
constexpr auto cend () const
 

Static Public Attributes

static constexpr uint8_t limit = 8
 

Detailed Description

Represents the memory strides associated with a tensor shape.

Strides describes how many elements must be skipped in memory to move between elements along each tensor dimension. It is a key part of defining how multidimensional data is stored and accessed in memory.

A Strides object can be constructed manually (via variadic arguments or iterators), or automatically from a Shape (default row-major/contiguous layout).

Maximum rank is limited to Strides::limit = 8 for constexpr compatibility and static layout. This limit may be removed in C++26 when std::vector become constexpr friendly.

Example (Contiguous Layout):

using namespace tannic;
Shape shape({2, 3, 4});
Strides strides(shape);
std::cout << strides; // Output: Strides(12, 4, 1)
Represents the shape (dimensions) of an tensor-like expression.
Definition: shape.hpp:79
Represents the memory strides associated with a tensor shape.
Definition: strides.hpp:87
Definition: buffer.hpp:41

Example (Custom Strides):

Strides strides(6, 2, 1); // Custom strides for a specific memory layout

Member Typedef Documentation

◆ rank_type

using tannic::Strides::rank_type = uint8_t

Type used for rank (number of dimensions).

◆ size_type

using tannic::Strides::size_type = int64_t

Type used for size and shape dimensions.

Constructor & Destructor Documentation

◆ Strides() [1/4]

constexpr tannic::Strides::Strides ( )
constexprdefaultnoexcept

Default constructor (rank 0).

◆ Strides() [2/4]

template<Integral... Sizes>
constexpr tannic::Strides::Strides ( Sizes...  sizes)
inlineconstexpr

Constructs strides from variadic size arguments.

Template Parameters
SizesVariadic list of integral stride values.
Parameters
sizesStrides for each dimension.
Note
Asserts if the number of dimensions (rank) exceeds limit.

Example:

constexpr tannic::Strides s(6, 2, 1);
std::cout << s; // Output: Strides(6, 2, 1)

◆ Strides() [3/4]

template<Iterator Iterator>
constexpr tannic::Strides::Strides ( Iterator  begin,
Iterator  end 
)
inlineconstexpr

Constructs strides from a pair of iterators.

Template Parameters
IteratorType of the iterator (must satisfy the Iterator concept).
Parameters
beginIterator to the beginning of stride values.
endIterator to the end of stride values.
Note
Asserts if the number of dimensions exceeds limit.

Example:

std::vector<std::size_t> values = {12, 4, 1};
tannic::Strides s(values.begin(), values.end());

◆ Strides() [4/4]

constexpr tannic::Strides::Strides ( const Shape shape)
inlineconstexpr

Constructs strides from a shape assuming row-major layout.

Parameters
shapeShape from which to compute contiguous strides.

Example:

constexpr tannic::Shape shape(2, 3, 4);
constexpr tannic::Strides s(shape);
std::cout << s; // Output: Strides(12, 4, 1)

Member Function Documentation

◆ address() [1/2]

constexpr size_type const * tannic::Strides::address ( ) const
inlineconstexprnoexcept

Returns a pointer to the underlying data (const).

Returns
Pointer to beginning of stride values.

◆ address() [2/2]

constexpr size_type * tannic::Strides::address ( )
inlineconstexprnoexcept

Returns a pointer to the underlying data (non-const).

Returns
Pointer to beginning of stride values.

◆ back()

constexpr auto tannic::Strides::back ( ) const
inlineconstexpr

Returns the last stride value.

Returns
Stride of the last dimension.

◆ begin() [1/2]

constexpr auto tannic::Strides::begin ( )
inlineconstexpr

◆ begin() [2/2]

constexpr auto tannic::Strides::begin ( ) const
inlineconstexpr

◆ cbegin()

constexpr auto tannic::Strides::cbegin ( ) const
inlineconstexpr

◆ cend()

constexpr auto tannic::Strides::cend ( ) const
inlineconstexpr

◆ end() [1/2]

constexpr auto tannic::Strides::end ( )
inlineconstexpr

◆ end() [2/2]

constexpr auto tannic::Strides::end ( ) const
inlineconstexpr

◆ expand()

constexpr void tannic::Strides::expand ( size_type  size)
inlineconstexpr

Expands the strides's last dimension with a given size.

Increments the rank by one.

Parameters
sizeThe size of the dimension that will be added to the back of the strides.

◆ front()

constexpr auto tannic::Strides::front ( ) const
inlineconstexpr

Returns the first stride value.

Returns
Stride of the first dimension.

◆ operator[]() [1/2]

template<Integral Index>
constexpr auto & tannic::Strides::operator[] ( Index  index)
inlineconstexpr

Accesses a stride value by index (non-const).

Template Parameters
IndexIntegral index type.
Parameters
indexIndex of the stride to access (supports negative indexing).
Returns
Reference to the stride value at the given index.

◆ operator[]() [2/2]

template<Integral Index>
constexpr auto const & tannic::Strides::operator[] ( Index  index) const
inlineconstexpr

Accesses a stride value by index (const).

Template Parameters
IndexIntegral index type.
Parameters
indexIndex of the stride to access (supports negative indexing).
Returns
Value of the stride at the given index.

◆ rank()

constexpr auto tannic::Strides::rank ( ) const
inlineconstexprnoexcept

Returns the number of dimensions (rank).

Returns
Rank of the strides.

Member Data Documentation

◆ limit

constexpr uint8_t tannic::Strides::limit = 8
staticconstexpr

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