5#include <initializer_list>
10#include <stratax/core/Buffer.hpp>
11#include <stratax/exceptions/Exceptions.hpp>
12#include <stratax/indexing/Normalize.hpp>
14namespace stratax::core {
75 Shape(
const std::vector<value_type>& dims)
78 std::copy(dims.begin(), dims.end(), dims_.
begin());
105 if (prod > std::numeric_limits<size_type>::max() / dim)
145 std::vector<value_type> stride_values(
rank());
146 stride_values[
rank() - 1] = 1;
151 stride_values[i] > std::numeric_limits<value_type>::max() / dims_[i])
156 stride_values[i - 1] = stride_values[i] * dims_[i];
159 return Shape(stride_values);
187 return dims_[stratax::indexing::normalize_index(index,
rank())];
195 [[nodiscard]]
bool empty() const noexcept
197 return dims_.
empty();
208 if (
rank() != other.rank())
214 if (dims_[i] != other.dims_[i])
225 return dims_.
begin();
260 return dims_.
crend();
270 dims_.
swap(other.dims_);
286inline std::ostream& operator<<(std::ostream& os,
const Shape& shape)
300 if (shape.rank() == 1)
Fixed-size owner of aligned, contiguous element storage.
const_pointer const_iterator
Read-only contiguous random-access iterator type.
reverse_iterator rend() noexcept
Returns the past-the-end iterator for mutable reverse traversal.
void swap(Buffer &other) noexcept
Swaps storage and size with another buffer.
size_type size() const noexcept
Returns the number of stored elements.
const_reverse_iterator crend() const noexcept
Returns the past-the-end iterator for const reverse traversal.
const_iterator cend() const noexcept
Returns a const iterator one past the final element.
bool empty() const noexcept
Returns whether the buffer has no elements.
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final element.
reverse_iterator rbegin() noexcept
Returns a mutable reverse iterator to the final element.
iterator begin() noexcept
Returns a mutable iterator to the first element.
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only iterator that traverses elements in reverse order.
iterator end() noexcept
Returns a mutable iterator one past the final element.
Stores the dimensions of a multidimensional array.
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.
std::size_t size_type
Unsigned type used for ranks and dimension indices.
size_type rank() const noexcept
Returns the number of dimensions.
const_reference at(difference_type index) const
Returns a dimension using checked, Python-style indexing.
const_iterator cend() const noexcept
Returns a const iterator past the final dimension.
bool empty() const noexcept
Reports whether the shape has rank zero.
const value_type & const_reference
Read-only reference to a dimension.
bool operator==(const Shape &other) const noexcept
Compares two shapes dimension by dimension.
void swap(Shape &other) noexcept
Exchanges dimension storage with another shape.
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final dimension.
const_iterator begin() const noexcept
Returns a const iterator to the first dimension.
Shape strides() const
Computes canonical row-major strides for this shape.
const_iterator end() const noexcept
Returns a const iterator past the final dimension.
Shape() noexcept=default
Constructs an empty, rank-zero shape.
const_reference operator[](size_type index) const noexcept
Returns a dimension without bounds checking.
Buffer< value_type >::const_reverse_iterator const_reverse_iterator
Read-only iterator over dimensions in reverse order.
const_reverse_iterator crend() const noexcept
Returns the past-the-end const reverse iterator.
std::size_t value_type
Type used to represent each dimension.
const_iterator cbegin() const noexcept
Returns a const iterator to the first dimension.
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the final dimension.
Buffer< value_type >::const_iterator const_iterator
Read-only contiguous iterator over dimensions.
const_reverse_iterator rend() const noexcept
Returns the past-the-end reverse iterator.
std::ptrdiff_t difference_type
Signed type used for checked indices and iterator distances.