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

Represents the shape (dimensions) of an tensor-like expression. More...

#include <shape.hpp>

Public Types

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

Public Member Functions

constexpr Shape () noexcept=default
 Default constructor.
 
constexpr Shape (std::initializer_list< size_type > shape)
 Constructs a shape from an initializer list of dimension sizes.
 
template<Integral... Sizes>
constexpr Shape (Sizes... sizes)
 Constructs a shape from a list of size arguments.
 
template<Iterable Sizes>
constexpr Shape (Sizes &&sizes)
 Constructs a shape from any iterable container of sizes.
 
template<Iterator Iterator>
constexpr Shape (Iterator begin, Iterator end)
 Constructs a shape from a pair of iterators.
 
constexpr size_typeaddress () noexcept
 Returns a pointer to the internal size data (non-const).
 
constexpr size_type const * address () const noexcept
 Returns a pointer to the internal size data (const).
 
constexpr rank_type rank () const noexcept
 Returns the number of dimensions (rank).
 
constexpr auto front () const noexcept
 Returns the first dimension size.
 
constexpr auto back () const
 Returns the last dimension size.
 
template<Integral Index>
constexpr auto const & operator[] (Index index) const
 Accesses a dimension by index (const).
 
template<Integral Index>
constexpr auto & operator[] (Index index)
 Accesses a dimension by index (non-const).
 
constexpr void expand (size_type size)
 Expands the shape'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 shape (dimensions) of an tensor-like expression.

The Shape class provides a constexpr-friendly abstraction over an expression shape and provides constructors and accessors for tensor shapes, including support for initializer lists, iterators, and iterable containers.

Shapes are limited to a maximum rank of 8 dimensions.

Example:

using namespace tannic;
constexpr Shape s1(2, 3, 4);
constexpr Shape s2(2, 3, 4);
static_assert(s1 == s2);
std::cout << s1 << std::endl; // Shape(2,3,4)
Represents the shape (dimensions) of an tensor-like expression.
Definition: shape.hpp:79
Definition: buffer.hpp:41

Member Typedef Documentation

◆ rank_type

using tannic::Shape::rank_type = uint8_t

Type used for rank (number of dimensions).

◆ size_type

using tannic::Shape::size_type = size_t

Type used for size and shape dimensions.

Constructor & Destructor Documentation

◆ Shape() [1/5]

constexpr tannic::Shape::Shape ( )
constexprdefaultnoexcept

Default constructor.

Initializes a shape of rank 0 and size 1 (shape of scalars).

◆ Shape() [2/5]

constexpr tannic::Shape::Shape ( std::initializer_list< size_type shape)
inlineconstexpr

Constructs a shape from an initializer list of dimension sizes.

Parameters
shapeAn initializer list of dimension sizes.
Note
Asserts if the number of dimensions exceeds limit.

Example:

Shape shape({2, 3, 4});
std::cout << shape; // Output: Shape(2, 3, 4)

◆ Shape() [3/5]

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

Constructs a shape from a list of size arguments.

Template Parameters
SizesVariadic list of integral size arguments.
Parameters
sizesSizes of each dimension.
Note
Asserts if the number of dimensions exceeds limit.

Example:

Shape shape(2, 3, 4);
std::cout << shape; // Output: Shape(2, 3, 4)

◆ Shape() [4/5]

template<Iterable Sizes>
constexpr tannic::Shape::Shape ( Sizes &&  sizes)
inlineconstexpr

Constructs a shape from any iterable container of sizes.

Template Parameters
SizesAn iterable with begin() and end() methods.
Parameters
sizesContainer of dimension sizes.

◆ Shape() [5/5]

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

Constructs a shape from a pair of iterators.

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

Example:

std::vector<std::size_t> dims = {2, 3, 4};
Shape shape(dims.begin(), dims.end());
std::cout << shape; // Output: Shape(2, 3, 4)

Member Function Documentation

◆ address() [1/2]

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

Returns a pointer to the internal size data (const).

Returns
Const pointer to the first element of the sizes array.

◆ address() [2/2]

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

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

Returns
Pointer to the first element of the sizes array.

◆ back()

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

Returns the last dimension size.

Returns
Size of the last dimension.
Note
Asserts if shape is empty.

◆ begin() [1/2]

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

◆ begin() [2/2]

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

◆ cbegin()

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

◆ cend()

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

◆ end() [1/2]

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

◆ end() [2/2]

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

◆ expand()

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

Expands the shape'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 shape.

◆ front()

constexpr auto tannic::Shape::front ( ) const
inlineconstexprnoexcept

Returns the first dimension size.

Returns
Size of the first dimension.

◆ operator[]() [1/2]

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

Accesses a dimension by index (non-const).

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

◆ operator[]() [2/2]

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

Accesses a dimension by index (const).

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

◆ rank()

constexpr rank_type tannic::Shape::rank ( ) const
inlineconstexprnoexcept

Returns the number of dimensions (rank).

Returns
Rank of the shape.

Member Data Documentation

◆ limit

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

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