48#include <initializer_list>
81 static constexpr uint8_t
limit = 8;
90 constexpr Shape() noexcept = default;
106 assert(shape.size() <
limit &&
"Rank limit exceeded");
107 rank_ =
static_cast<size_type>(shape.size());
109 for (
auto size : shape) {
110 sizes_[dimension++] = size;
113 throw Exception(
"Shape rank limit exceeded");
134 : sizes_{static_cast<
size_type>(sizes)...}
135 , rank_(sizeof...(sizes)) {
137 throw Exception(
"Shape rank limit exceeded");
146 template<Iterable Sizes>
149 for (
auto size : sizes) {
150 sizes_[dimension++] =
static_cast<size_type>(size);
154 throw Exception(
"Shape rank limit exceeded");
175 template<Iterator Iterator>
179 sizes_[dimension++] =
static_cast<size_type>(*iterator);
183 throw Exception(
"Shape rank limit exceeded");
192 return sizes_.data();
200 return sizes_.data();
216 return sizes_.begin();
220 return sizes_.begin() + rank_;
224 return sizes_.begin();
227 constexpr auto end()
const {
228 return sizes_.begin() + rank_;
232 return sizes_.cbegin();
236 return sizes_.cbegin() + rank_;
244 constexpr auto front() const noexcept {
245 return sizes_.front();
255 throw Exception(
"Cannot call back() on an empty Shape");
256 return sizes_[rank_ - 1];
265 template<Integral Index>
276 template<Integral Index>
287 if (rank_ + 1 >
limit)
288 throw Exception(
"Shape rank limit exceeded");
289 sizes_[rank_] = size;
296 std::array<size_type, limit> sizes_{};
305constexpr bool operator==(
Shape const& first,
Shape const& second) {
306 if (first.
rank() != second.
rank())
return false;
308 if (first[dimension] != second[dimension])
return false;
316 os << static_cast<unsigned int>(shape[dimension]);
317 if (dimension + 1 < shape.
rank()) {
A simple generic exception type for the Tannic Tensor Library.
Definition: exceptions.hpp:44
Represents the shape (dimensions) of an tensor-like expression.
Definition: shape.hpp:79
constexpr auto end() const
Definition: shape.hpp:227
constexpr rank_type rank() const noexcept
Returns the number of dimensions (rank).
Definition: shape.hpp:207
constexpr auto begin()
Definition: shape.hpp:215
constexpr Shape(Sizes... sizes)
Constructs a shape from a list of size arguments.
Definition: shape.hpp:133
size_t size_type
Type used for size and shape dimensions.
Definition: shape.hpp:85
constexpr Shape(Iterator begin, Iterator end)
Constructs a shape from a pair of iterators.
Definition: shape.hpp:176
constexpr auto cbegin() const
Definition: shape.hpp:231
constexpr auto cend() const
Definition: shape.hpp:235
constexpr auto const & operator[](Index index) const
Accesses a dimension by index (const).
Definition: shape.hpp:266
constexpr void expand(size_type size)
Expands the shape's last dimension with a given size.
Definition: shape.hpp:286
constexpr size_type * address() noexcept
Returns a pointer to the internal size data (non-const).
Definition: shape.hpp:191
constexpr auto end()
Definition: shape.hpp:219
constexpr Shape() noexcept=default
Default constructor.
constexpr auto front() const noexcept
Returns the first dimension size.
Definition: shape.hpp:244
constexpr auto begin() const
Definition: shape.hpp:223
constexpr auto & operator[](Index index)
Accesses a dimension by index (non-const).
Definition: shape.hpp:277
constexpr auto back() const
Returns the last dimension size.
Definition: shape.hpp:253
constexpr Shape(Sizes &&sizes)
Constructs a shape from any iterable container of sizes.
Definition: shape.hpp:147
uint8_t rank_type
Type used for rank (number of dimensions).
Definition: shape.hpp:84
static constexpr uint8_t limit
Definition: shape.hpp:81
constexpr size_type const * address() const noexcept
Returns a pointer to the internal size data (const).
Definition: shape.hpp:199
Requires a type to be an integral type (e.g., int, std::size_t).
Definition: concepts.hpp:147
Requires a type to satisfy the C++20 std::input_iterator concept.
Definition: concepts.hpp:140
constexpr Index normalize(Index index, Size bound)
Normalize a possibly-negative index into the valid range [0, bound).
Definition: indexing.hpp:87
Definition: buffer.hpp:41
std::ostream & operator<<(std::ostream &os, Shape const &shape)
Definition: shape.hpp:313