Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
dim.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_DIM_HPP_
34#define GKO_PUBLIC_CORE_BASE_DIM_HPP_
35
36
37#include <iostream>
38
39
40#include <ginkgo/core/base/types.hpp>
41
42
43namespace gko {
44
45
54template <size_type Dimensionality, typename DimensionType = size_type>
55struct dim {
56 static constexpr size_type dimensionality = Dimensionality;
57 friend struct dim<dimensionality + 1>;
58
59 using dimension_type = DimensionType;
60
66 constexpr GKO_ATTRIBUTES dim(const dimension_type& size = dimension_type{})
67 : first_{size}, rest_{size}
68 {}
69
83 template <typename... Rest>
84 constexpr GKO_ATTRIBUTES dim(const dimension_type& first,
85 const Rest&... rest)
86 : first_{first}, rest_{static_cast<dimension_type>(rest)...}
87 {}
88
100 constexpr GKO_ATTRIBUTES const dimension_type& operator[](
101 const size_type& dimension) const noexcept
102 {
103 return GKO_ASSERT(dimension < dimensionality),
104 dimension == 0 ? first_ : rest_[dimension - 1];
105 }
106
110 GKO_ATTRIBUTES dimension_type& operator[](
111 const size_type& dimension) noexcept
112 {
113 return GKO_ASSERT(dimension < dimensionality),
114 dimension == 0 ? first_ : rest_[dimension - 1];
115 }
116
128 explicit constexpr GKO_ATTRIBUTES operator bool() const
129 {
130 return static_cast<bool>(first_) && static_cast<bool>(rest_);
131 }
132
141 friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
142 {
143 return x.first_ == y.first_ && x.rest_ == y.rest_;
144 }
145
154 friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
155 {
156 return dim(x.first_ * y.first_, x.rest_ * y.rest_);
157 }
158
167 friend std::ostream& operator<<(std::ostream& os, const dim& x)
168 {
169 os << "(";
170 x.print_to(os);
171 os << ")";
172 return os;
173 }
174
175private:
176 void inline print_to(std::ostream& os) const
177 {
178 os << first_ << ", ";
179 rest_.print_to(os);
180 }
181
182
183 constexpr GKO_ATTRIBUTES dim(const dimension_type first,
185 : first_{first}, rest_{rest}
186 {}
187
188 dimension_type first_;
189 dim<dimensionality - 1, dimension_type> rest_;
190};
191
192
193// base case for dim recursive template
194template <typename DimensionType>
195struct dim<1u, DimensionType> {
196 static constexpr size_type dimensionality = 1u;
197 friend struct dim<2>;
198
199 using dimension_type = DimensionType;
200
201 constexpr GKO_ATTRIBUTES dim(const dimension_type& size = dimension_type{})
202 : first_{size}
203 {}
204
205 constexpr GKO_ATTRIBUTES const dimension_type& operator[](
206 const size_type& dimension) const noexcept
207 {
208 return GKO_ASSERT(dimension == 0), first_;
209 }
210
211 GKO_ATTRIBUTES dimension_type& operator[](const size_type& dimension)
212 {
213 return GKO_ASSERT(dimension == 0), first_;
214 }
215
216 explicit constexpr GKO_ATTRIBUTES operator bool() const
217 {
218 return static_cast<bool>(first_);
219 }
220
221 friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
222 {
223 return x.first_ == y.first_;
224 }
225
226 friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
227 {
228 return dim(x.first_ * y.first_);
229 }
230
231 friend std::ostream& operator<<(std::ostream& os, const dim& x)
232 {
233 os << "(";
234 x.print_to(os);
235 os << ")";
236 return os;
237 }
238
239private:
240 void inline print_to(std::ostream& os) const { os << first_; }
241
242 dimension_type first_;
243};
244
245
257template <size_type Dimensionality, typename DimensionType>
258constexpr GKO_ATTRIBUTES GKO_INLINE bool operator!=(
261{
262 return !(x == y);
263}
264
265
275template <typename DimensionType>
276constexpr GKO_ATTRIBUTES GKO_INLINE dim<2, DimensionType> transpose(
277 const dim<2, DimensionType>& dimensions) noexcept
278{
279 return {dimensions[1], dimensions[0]};
280}
281
282
283} // namespace gko
284
285
286#endif // GKO_PUBLIC_CORE_BASE_DIM_HPP_
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
constexpr bool operator!=(const dim< Dimensionality, DimensionType > &x, const dim< Dimensionality, DimensionType > &y)
Checks if two dim objects are different.
Definition dim.hpp:258
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
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:148
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
constexpr dim(const dimension_type &first, const Rest &... rest)
Creates a dimension object with the specified dimensions.
Definition dim.hpp:84
friend constexpr dim operator*(const dim &x, const dim &y)
Multiplies two dim objects.
Definition dim.hpp:154
friend constexpr bool operator==(const dim &x, const dim &y)
Checks if two dim objects are equal.
Definition dim.hpp:141
friend std::ostream & operator<<(std::ostream &os, const dim &x)
A stream operator overload for dim.
Definition dim.hpp:167
constexpr dim(const dimension_type &size=dimension_type{})
Creates a dimension object with all dimensions set to the same value.
Definition dim.hpp:66
constexpr const dimension_type & operator[](const size_type &dimension) const noexcept
Returns the requested dimension.
Definition dim.hpp:100
dimension_type & operator[](const size_type &dimension) noexcept
Definition dim.hpp:110