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)
108 "Shape element count exceeds the maximum representable size.");
146 std::vector<value_type> stride_values(
rank());
147 stride_values[
rank() - 1] = 1;
152 stride_values[i] > std::numeric_limits<value_type>::max() / dims_[i])
155 "Stride value exceeds the maximum representable size.");
158 stride_values[i - 1] = stride_values[i] * dims_[i];
161 return Shape(stride_values);
189 return dims_[stratax::indexing::normalize_index(index,
rank())];
197 [[nodiscard]]
bool empty() const noexcept
199 return dims_.empty();
210 if (
rank() != other.rank())
216 if (dims_[i] != other.dims_[i])
227 return dims_.begin();
237 return dims_.cbegin();
247 return dims_.rbegin();
252 return dims_.crbegin();
262 return dims_.crend();
272 dims_.swap(other.dims_);
288inline std::ostream& operator<<(std::ostream& os,
const Shape& shape)
302 if (shape.rank() == 1)
Fixed-size owner of aligned, contiguous element storage.
const_pointer const_iterator
Read-only contiguous random-access iterator type.
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only iterator that traverses elements in reverse 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.
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.