Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
dim.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_DIM_HPP_
6 #define GKO_PUBLIC_CORE_BASE_DIM_HPP_
7 
8 
9 #include <iostream>
10 
11 
12 #include <ginkgo/core/base/types.hpp>
13 
14 
15 namespace gko {
16 
17 
26 template <size_type Dimensionality, typename DimensionType = size_type>
27 struct dim {
28  static constexpr size_type dimensionality = Dimensionality;
29  friend struct dim<dimensionality + 1>;
30 
31  using dimension_type = DimensionType;
32 
36  constexpr GKO_ATTRIBUTES dim() : dim{dimension_type{}} {}
37 
43  explicit constexpr GKO_ATTRIBUTES dim(const dimension_type& size)
44  : first_{size}, rest_{size}
45  {}
46 
60  template <typename... Rest, std::enable_if_t<sizeof...(Rest) ==
61  Dimensionality - 1>* = nullptr>
62  constexpr GKO_ATTRIBUTES dim(const dimension_type& first,
63  const Rest&... rest)
64  : first_{first}, rest_{static_cast<dimension_type>(rest)...}
65  {}
66 
78  constexpr GKO_ATTRIBUTES const dimension_type& operator[](
79  const size_type& dimension) const noexcept
80  {
81  return GKO_ASSERT(dimension < dimensionality),
82  dimension == 0 ? first_ : rest_[dimension - 1];
83  }
84 
88  GKO_ATTRIBUTES dimension_type& operator[](
89  const size_type& dimension) noexcept
90  {
91  return GKO_ASSERT(dimension < dimensionality),
92  dimension == 0 ? first_ : rest_[dimension - 1];
93  }
94 
106  explicit constexpr GKO_ATTRIBUTES operator bool() const
107  {
108  return static_cast<bool>(first_) && static_cast<bool>(rest_);
109  }
110 
119  friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
120  {
121  return x.first_ == y.first_ && x.rest_ == y.rest_;
122  }
123 
132  friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
133  {
134  return !(x == y);
135  }
136 
145  friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
146  {
147  return dim(x.first_ * y.first_, x.rest_ * y.rest_);
148  }
149 
158  friend std::ostream& operator<<(std::ostream& os, const dim& x)
159  {
160  os << "(";
161  x.print_to(os);
162  os << ")";
163  return os;
164  }
165 
166 private:
167  void inline print_to(std::ostream& os) const
168  {
169  os << first_ << ", ";
170  rest_.print_to(os);
171  }
172 
173 
174  constexpr GKO_ATTRIBUTES dim(const dimension_type first,
175  dim<dimensionality - 1> rest)
176  : first_{first}, rest_{rest}
177  {}
178 
179  dimension_type first_;
180  dim<dimensionality - 1, dimension_type> rest_;
181 };
182 
183 
184 // base case for dim recursive template
185 template <typename DimensionType>
187  static constexpr size_type dimensionality = 1u;
188  friend struct dim<2>;
189 
190  using dimension_type = DimensionType;
191 
192  constexpr GKO_ATTRIBUTES dim(const dimension_type& size = dimension_type{})
193  : first_{size}
194  {}
195 
196  constexpr GKO_ATTRIBUTES const dimension_type& operator[](
197  const size_type& dimension) const noexcept
198  {
199  return GKO_ASSERT(dimension == 0), first_;
200  }
201 
202  GKO_ATTRIBUTES dimension_type& operator[](const size_type& dimension)
203  {
204  return GKO_ASSERT(dimension == 0), first_;
205  }
206 
207  explicit constexpr GKO_ATTRIBUTES operator bool() const
208  {
209  return static_cast<bool>(first_);
210  }
211 
212  friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
213  {
214  return x.first_ == y.first_;
215  }
216 
217  friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
218  {
219  return !(x == y);
220  }
221 
222  friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
223  {
224  return dim(x.first_ * y.first_);
225  }
226 
227  friend std::ostream& operator<<(std::ostream& os, const dim& x)
228  {
229  os << "(";
230  x.print_to(os);
231  os << ")";
232  return os;
233  }
234 
235 private:
236  void inline print_to(std::ostream& os) const { os << first_; }
237 
238  dimension_type first_;
239 };
240 
241 
251 template <typename DimensionType>
252 constexpr GKO_ATTRIBUTES GKO_INLINE dim<2, DimensionType> transpose(
253  const dim<2, DimensionType>& dimensions) noexcept
254 {
255  return {dimensions[1], dimensions[0]};
256 }
257 
258 
259 } // namespace gko
260 
261 
262 #endif // GKO_PUBLIC_CORE_BASE_DIM_HPP_
gko::dim::operator==
constexpr friend bool operator==(const dim &x, const dim &y)
Checks if two dim objects are equal.
Definition: dim.hpp:119
gko::dim::operator*
constexpr friend dim operator*(const dim &x, const dim &y)
Multiplies two dim objects.
Definition: dim.hpp:145
gko::dim< 1u, DimensionType >
Definition: dim.hpp:186
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::dim::operator<<
friend std::ostream & operator<<(std::ostream &os, const dim &x)
A stream operator overload for dim.
Definition: dim.hpp:158
gko::dim::operator[]
constexpr const dimension_type & operator[](const size_type &dimension) const noexcept
Returns the requested dimension.
Definition: dim.hpp:78
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::dim::operator[]
dimension_type & operator[](const size_type &dimension) noexcept
Definition: dim.hpp:88
gko::dim
A type representing the dimensions of a multidimensional object.
Definition: dim.hpp:27
gko::dim::operator!=
constexpr friend bool operator!=(const dim &x, const dim &y)
Checks if two dim objects are not equal.
Definition: dim.hpp:132
gko::dim::dim
constexpr dim(const dimension_type &first, const Rest &... rest)
Creates a dimension object with the specified dimensions.
Definition: dim.hpp:62
gko::transpose
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:120
gko::dim::dim
constexpr dim()
Creates a dimension object with all dimensions set to zero.
Definition: dim.hpp:36
gko::dim::dim
constexpr dim(const dimension_type &size)
Creates a dimension object with all dimensions set to the same value.
Definition: dim.hpp:43