5#ifndef GKO_PUBLIC_CORE_BASE_DIM_HPP_
6#define GKO_PUBLIC_CORE_BASE_DIM_HPP_
11#include <ginkgo/core/base/types.hpp>
25template <
size_type Dimensionality,
typename DimensionType =
size_type>
27 static constexpr size_type dimensionality = Dimensionality;
28 friend struct dim<dimensionality + 1>;
30 using dimension_type = DimensionType;
35 constexpr GKO_ATTRIBUTES
dim() :
dim{dimension_type{}} {}
42 explicit constexpr GKO_ATTRIBUTES
dim(
const dimension_type& size)
43 : first_{size}, rest_{size}
59 template <
typename... Rest, std::enable_if_t<
sizeof...(Rest) ==
60 Dimensionality - 1>* =
nullptr>
61 constexpr GKO_ATTRIBUTES
dim(
const dimension_type& first,
63 : first_{first}, rest_{
static_cast<dimension_type
>(rest)...}
77 constexpr GKO_ATTRIBUTES
const dimension_type&
operator[](
78 const size_type& dimension)
const noexcept
80 return GKO_ASSERT(dimension < dimensionality),
81 dimension == 0 ? first_ : rest_[dimension - 1];
90 return GKO_ASSERT(dimension < dimensionality),
91 dimension == 0 ? first_ : rest_[dimension - 1];
105 explicit constexpr GKO_ATTRIBUTES
operator bool()
const
107 return static_cast<bool>(first_) &&
static_cast<bool>(rest_);
120 return x.first_ == y.first_ && x.rest_ == y.rest_;
146 return dim(x.first_ * y.first_, x.rest_ * y.rest_);
166 void inline print_to(std::ostream& os)
const
168 os << first_ <<
", ";
173 constexpr GKO_ATTRIBUTES
dim(
const dimension_type first,
175 : first_{first}, rest_{rest}
178 dimension_type first_;
179 dim<dimensionality - 1, dimension_type> rest_;
184template <
typename DimensionType>
185struct dim<1u, DimensionType> {
186 static constexpr size_type dimensionality = 1u;
187 friend struct dim<2>;
189 using dimension_type = DimensionType;
191 constexpr GKO_ATTRIBUTES dim(
const dimension_type& size = dimension_type{})
195 constexpr GKO_ATTRIBUTES
const dimension_type& operator[](
196 const size_type& dimension)
const noexcept
198 return GKO_ASSERT(dimension == 0), first_;
201 GKO_ATTRIBUTES dimension_type& operator[](
const size_type& dimension)
203 return GKO_ASSERT(dimension == 0), first_;
206 explicit constexpr GKO_ATTRIBUTES
operator bool()
const
208 return static_cast<bool>(first_);
211 friend constexpr GKO_ATTRIBUTES
bool operator==(
const dim& x,
const dim& y)
213 return x.first_ == y.first_;
216 friend constexpr GKO_ATTRIBUTES
bool operator!=(
const dim& x,
const dim& y)
221 friend constexpr GKO_ATTRIBUTES dim operator*(
const dim& x,
const dim& y)
223 return dim(x.first_ * y.first_);
226 friend std::ostream& operator<<(std::ostream& os,
const dim& x)
235 void inline print_to(std::ostream& os)
const { os << first_; }
237 dimension_type first_;
250template <
typename DimensionType>
254 return {dimensions[1], dimensions[0]};
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition batch_dim.hpp:119
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
constexpr dim()
Creates a dimension object with all dimensions set to zero.
Definition dim.hpp:35
friend constexpr dim operator*(const dim &x, const dim &y)
Multiplies two dim objects.
Definition dim.hpp:144
friend constexpr bool operator!=(const dim &x, const dim &y)
Checks if two dim objects are not equal.
Definition dim.hpp:131
friend constexpr bool operator==(const dim &x, const dim &y)
Checks if two dim objects are equal.
Definition dim.hpp:118
friend std::ostream & operator<<(std::ostream &os, const dim &x)
A stream operator overload for dim.
Definition dim.hpp:157
constexpr const dimension_type & operator[](const size_type &dimension) const noexcept
Returns the requested dimension.
Definition dim.hpp:77
dimension_type & operator[](const size_type &dimension) noexcept
Definition dim.hpp:87
constexpr dim(const dimension_type &size)
Creates a dimension object with all dimensions set to the same value.
Definition dim.hpp:42
constexpr dim(const dimension_type &first, const Rest &... rest)
Creates a dimension object with the specified dimensions.
Definition dim.hpp:61