24Shape::Shape(
const std::initializer_list<Dim> &dims) : _dims{dims} {}
26Shape::Shape(
const std::vector<Dim> &shape_vec) : _dims{shape_vec} {}
28Shape::Shape(
const std::initializer_list<uint32_t> &shape_vec)
30 for (
const auto &dim : shape_vec)
32 if (dim >= std::numeric_limits<int32_t>::max())
34 std::out_of_range(
"Provided dimension: " + std::to_string(dim) +
" is out of range");
36 _dims.emplace_back(
Dim{
static_cast<int32_t
>(dim)});
40Shape Shape::scalar() {
return Shape{std::initializer_list<Dim>{}}; }
42size_t Shape::rank()
const {
return _dims.size(); }
44Dim Shape::operator[](
const size_t &axis)
const
48 throw std::invalid_argument(
"You cannot gather dimension from a scalar");
50 if (axis >
rank() - 1)
52 throw std::out_of_range(
"Axis=" + std::to_string(axis) +
53 " is out of range of shape's rank: " + std::to_string(
rank()));
58bool Shape::is_scalar()
const {
return _dims.empty(); }
60bool Shape::is_dynamic()
const
66 return std::any_of(std::begin(_dims), std::end(_dims),
70bool Shape::operator==(
const Shape &rhs)
const
76 for (
size_t axis = 0; axis <
rank(); ++axis)
78 if (_dims[axis].value() != rhs[axis].value())
94 for (
int i = 0; i < shape.
rank() - 1; ++i)
96 os << shape[i].value() <<
", ";
98 os << shape[shape.
rank() - 1].value() <<
"]";
bool is_dynamic() const
Returns true if the dimension is dynamic. Otherwise, return false.
bool is_scalar() const
Returns true if the shape is a scalar. Otherwise, return false.
size_t rank() const
Returns number of dimensions in the shape.
std::ostream & operator<<(std::ostream &os, const Shape &shape)
Print the shape in format [1, 2, 3].