24 void validate_dimensions()
const
26 for (std::size_t dim : dims_)
30 "Shape dimensions cannot be negative.");
55 requires (sizeof...(Dims) > 0)
57 : dims_{
static_cast<std::size_t
>(dims)...}
59 validate_dimensions();
84 validate_dimensions();
105 : dims_(std::move(dims))
107 validate_dimensions();
117 : dims_(std::move(dims))
144 std::size_t prod = 1;
145 for (std::size_t dim : dims_)
157 [[nodiscard]] std::size_t
rank()
const
182 [[nodiscard]]
bool empty() const noexcept
184 return dims_.empty();
196 if (
rank() != other.rank())
200 for (std::size_t i = 0; i <
rank(); ++i)
202 if (dims_[i] != other.dims_[i])
219 return !(*
this == other);
229 return dims_.begin();
249 return dims_.begin();
269 dims_.swap(other.dims_);
287 for (std::size_t dim : shape)
296 if (shape.
rank() == 1)
Shared runtime validation helpers.
Owns contiguous dynamically allocated storage.
Stores a list of dimension lengths for an array shape.
~Shape()=default
Destroys the shape.
std::size_t elements() const
Returns the total number of elements described by the shape.
Shape(Buffer< std::size_t > &&dims, allow_zero_t allow_zero)
Creates a shape by taking ownership of dimensions without rejecting zero dimensions.
auto begin() const noexcept
Returns a const iterator to the first stored dimension.
bool operator!=(const Shape &other) const noexcept
Returns whether two shapes differ in rank or dimension values.
const std::size_t & operator()(std::size_t index) const
Returns the length of a specific dimension.
bool empty() const noexcept
Returns whether the shape has no dimensions.
auto end() const noexcept
Returns a const iterator one past the last stored dimension.
bool operator==(const Shape &other) const noexcept
Compares two shapes for exact rank and dimension equality.
void swap(Shape &other) noexcept
Swaps the stored dimensions with another shape.
auto end() noexcept
Returns an iterator one past the last stored dimension.
Shape() noexcept=default
Creates an empty shape.
std::size_t rank() const
Returns the number of stored dimensions.
Shape(const Buffer< std::size_t > &dims)
Creates a shape by copying dimension lengths from a buffer.
auto begin() noexcept
Returns an iterator to the first stored dimension.
static constexpr allow_zero_t allow_zero
Shape(Buffer< std::size_t > &&dims)
Creates a shape by taking ownership of dimension lengths from a buffer.
Shape(std::initializer_list< std::size_t > list, allow_zero_t allow_zero)
Creates a shape from dimension lengths without rejecting zero dimensions.
Shape(const Buffer< std::size_t > &dims, allow_zero_t allow_zero)
Creates a shape by copying dimension lengths without rejecting zero dimensions.
Matches signed and unsigned integral types, excluding character-like types.
std::size_t checked_multiply(std::size_t lhs, std::size_t rhs, const char *message)
Multiplies two sizes while checking for overflow.
void require_index(std::size_t index, std::size_t size, const char *message)
Requires an index to be less than a size.
std::size_t nonnegative_shape_dimension(long long value, const char *message)
Converts a signed shape dimension to std::size_t after rejecting negatives.
std::ostream & operator<<(std::ostream &os, const Shape &shape)
Writes a shape to a stream in tuple-like form.