Stratax 0.3.1
Loading...
Searching...
No Matches
stratax::core::Shape Class Reference

Stores the dimensions of a multidimensional array. More...

#include <Shape.hpp>

Public Types

using value_type = std::size_t
 Type used to represent each dimension.
 
using size_type = std::size_t
 Unsigned type used for ranks and dimension indices.
 
using difference_type = std::ptrdiff_t
 Signed type used for checked indices and iterator distances.
 
using const_reference = const value_type &
 Read-only reference to a dimension.
 
using const_iterator = Buffer< value_type >::const_iterator
 Read-only contiguous iterator over dimensions.
 
using const_reverse_iterator = Buffer< value_type >::const_reverse_iterator
 Read-only iterator over dimensions in reverse order.
 

Public Member Functions

 Shape () noexcept=default
 Constructs an empty, rank-zero shape.
 
 Shape (std::initializer_list< value_type > dims)
 Constructs a shape from dimensions in outermost-to-innermost order.
 
 Shape (const std::vector< value_type > &dims)
 Constructs a shape by copying a vector of dimensions.
 
size_type elements () const
 Computes the total number of elements described by the shape.
 
size_type rank () const noexcept
 Returns the number of dimensions.
 
Shape strides () const
 Computes canonical row-major strides for this shape.
 
const_reference operator[] (size_type index) const noexcept
 Returns a dimension without bounds checking.
 
const_reference at (difference_type index) const
 Returns a dimension using checked, Python-style indexing.
 
bool empty () const noexcept
 Reports whether the shape has rank zero.
 
bool operator== (const Shape &other) const noexcept
 Compares two shapes dimension by dimension.
 
const_iterator begin () const noexcept
 Returns a const iterator to the first dimension.
 
const_iterator end () const noexcept
 Returns a const iterator past the final dimension.
 
const_iterator cbegin () const noexcept
 Returns a const iterator to the first dimension.
 
const_iterator cend () const noexcept
 Returns a const iterator past the final dimension.
 
const_reverse_iterator rbegin () const noexcept
 Returns a const reverse iterator to the final dimension.
 
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the final dimension.
 
const_reverse_iterator rend () const noexcept
 Returns the past-the-end reverse iterator.
 
const_reverse_iterator crend () const noexcept
 Returns the past-the-end const reverse iterator.
 
void swap (Shape &other) noexcept
 Exchanges dimension storage with another shape.
 

Detailed Description

Stores the dimensions of a multidimensional array.

Dimensions are stored in contiguous, read-only order. The number of stored dimensions is the shape's rank, and elements() returns their product when that product can be represented by std::size_t.

Example
const stratax::core::Shape shape{2, 3, 4};
shape.rank(); // 3
shape.elements(); // 24
shape.at(-1); // 4
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:121

Definition at line 32 of file Shape.hpp.

Member Typedef Documentation

◆ const_iterator

Read-only contiguous iterator over dimensions.

Definition at line 47 of file Shape.hpp.

◆ const_reference

Read-only reference to a dimension.

Definition at line 45 of file Shape.hpp.

◆ const_reverse_iterator

Read-only iterator over dimensions in reverse order.

Definition at line 49 of file Shape.hpp.

◆ difference_type

using stratax::core::Shape::difference_type = std::ptrdiff_t

Signed type used for checked indices and iterator distances.

Definition at line 43 of file Shape.hpp.

◆ size_type

using stratax::core::Shape::size_type = std::size_t

Unsigned type used for ranks and dimension indices.

Definition at line 41 of file Shape.hpp.

◆ value_type

using stratax::core::Shape::value_type = std::size_t

Type used to represent each dimension.

Definition at line 39 of file Shape.hpp.

Constructor & Destructor Documentation

◆ Shape() [1/3]

stratax::core::Shape::Shape ( )
defaultnoexcept

Constructs an empty, rank-zero shape.

Complexity
O(1).

◆ Shape() [2/3]

stratax::core::Shape::Shape ( std::initializer_list< value_type dims)
inline

Constructs a shape from dimensions in outermost-to-innermost order.

Parameters
dimsDimensions to copy into the shape.
Exceptions
std::bad_allocIf dimension storage cannot be allocated.
Complexity
O(dims.size()).

Definition at line 64 of file Shape.hpp.

◆ Shape() [3/3]

stratax::core::Shape::Shape ( const std::vector< value_type > &  dims)
inline

Constructs a shape by copying a vector of dimensions.

Parameters
dimsDimensions in outermost-to-innermost order.
Exceptions
std::bad_allocIf dimension storage cannot be allocated.
Complexity
O(dims.size()).

Definition at line 75 of file Shape.hpp.

Member Function Documentation

◆ at()

const_reference stratax::core::Shape::at ( difference_type  index) const
inline

Returns a dimension using checked, Python-style indexing.

Negative indices count backward from the final dimension, so at(-1) returns the last dimension.

Parameters
indexDimension index in the range [-rank(), rank()).
Returns
Const reference to the normalized dimension.
Exceptions
Exceptions::IndexErrorIf index is outside the valid range.
Complexity
O(1).

Definition at line 185 of file Shape.hpp.

◆ begin()

const_iterator stratax::core::Shape::begin ( ) const
inlinenoexcept

Returns a const iterator to the first dimension.

Complexity
O(1).

Definition at line 223 of file Shape.hpp.

◆ cbegin()

const_iterator stratax::core::Shape::cbegin ( ) const
inlinenoexcept

Returns a const iterator to the first dimension.

Complexity
O(1).

Definition at line 233 of file Shape.hpp.

◆ cend()

const_iterator stratax::core::Shape::cend ( ) const
inlinenoexcept

Returns a const iterator past the final dimension.

Complexity
O(1).

Definition at line 238 of file Shape.hpp.

◆ crbegin()

const_reverse_iterator stratax::core::Shape::crbegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the final dimension.

Complexity
O(1).

Definition at line 248 of file Shape.hpp.

◆ crend()

const_reverse_iterator stratax::core::Shape::crend ( ) const
inlinenoexcept

Returns the past-the-end const reverse iterator.

Complexity
O(1).

Definition at line 258 of file Shape.hpp.

◆ elements()

size_type stratax::core::Shape::elements ( ) const
inline

Computes the total number of elements described by the shape.

Returns zero for a rank-zero shape or for any shape containing a zero dimension. Multiplication is checked to prevent unsigned overflow.

Returns
Product of all dimensions, or zero for an empty/zero-sized shape.
Exceptions
Exceptions::DimensionErrorIf the product exceeds std::size_t.
Complexity
O(rank()).

Definition at line 91 of file Shape.hpp.

◆ empty()

bool stratax::core::Shape::empty ( ) const
inlinenoexcept

Reports whether the shape has rank zero.

Returns
true if no dimensions are stored; otherwise false.
Complexity
O(1).

Definition at line 195 of file Shape.hpp.

◆ end()

const_iterator stratax::core::Shape::end ( ) const
inlinenoexcept

Returns a const iterator past the final dimension.

Complexity
O(1).

Definition at line 228 of file Shape.hpp.

◆ operator==()

bool stratax::core::Shape::operator== ( const Shape other) const
inlinenoexcept

Compares two shapes dimension by dimension.

Parameters
otherShape to compare with.
Returns
true if both shapes have identical rank and dimensions.
Complexity
O(rank()).

Definition at line 206 of file Shape.hpp.

◆ operator[]()

const_reference stratax::core::Shape::operator[] ( size_type  index) const
inlinenoexcept

Returns a dimension without bounds checking.

Parameters
indexZero-based dimension index.
Returns
Const reference to the dimension at index.
Precondition
index < rank(); otherwise behavior is undefined.
Complexity
O(1).

Definition at line 169 of file Shape.hpp.

◆ rank()

size_type stratax::core::Shape::rank ( ) const
inlinenoexcept

Returns the number of dimensions.

Returns
Number of stored dimension values.
Complexity
O(1).

Definition at line 121 of file Shape.hpp.

◆ rbegin()

const_reverse_iterator stratax::core::Shape::rbegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the final dimension.

Complexity
O(1).

Definition at line 243 of file Shape.hpp.

◆ rend()

const_reverse_iterator stratax::core::Shape::rend ( ) const
inlinenoexcept

Returns the past-the-end reverse iterator.

Complexity
O(1).

Definition at line 253 of file Shape.hpp.

◆ strides()

Shape stratax::core::Shape::strides ( ) const
inline

Computes canonical row-major strides for this shape.

The returned shape has the same rank as this shape. Its final stride is one, and each preceding stride is the product of the dimensions to its right. A rank-zero shape produces a rank-zero stride shape.

Returns
Shape containing the row-major stride for each dimension.
Exceptions
Exceptions::DimensionErrorIf a stride exceeds std::size_t.
std::bad_allocIf storage for the result cannot be allocated.
Complexity
O(rank()).

Definition at line 138 of file Shape.hpp.

◆ swap()

void stratax::core::Shape::swap ( Shape other)
inlinenoexcept

Exchanges dimension storage with another shape.

Parameters
otherShape whose dimensions are exchanged with this shape.
Complexity
O(1).

Definition at line 268 of file Shape.hpp.


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