Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
dim.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_BASE_DIM_HPP_
34 #define GKO_CORE_BASE_DIM_HPP_
35 
36 
37 #include <ginkgo/core/base/types.hpp>
38 #include <ginkgo/core/base/utils.hpp>
39 
40 
41 namespace gko {
42 
43 
52 template <size_type Dimensionality, typename DimensionType = size_type>
53 struct dim {
54  static constexpr size_type dimensionality = Dimensionality;
55 
56  using dimension_type = DimensionType;
57 
63  constexpr GKO_ATTRIBUTES dim(const dimension_type &size = dimension_type{})
64  : first_{size}, rest_{size}
65  {}
66 
80  template <typename... Rest>
81  constexpr GKO_ATTRIBUTES dim(const dimension_type &first,
82  const Rest &... rest)
83  : first_{first}, rest_{static_cast<dimension_type>(rest)...}
84  {}
85 
97  constexpr GKO_ATTRIBUTES const dimension_type &operator[](
98  const size_type &dimension) const noexcept
99  {
100  return GKO_ASSERT(dimension < dimensionality), *(&first_ + dimension);
101  }
102 
106  GKO_ATTRIBUTES dimension_type &operator[](
107  const size_type &dimension) noexcept
108  {
109  return GKO_ASSERT(dimension < dimensionality), *(&first_ + dimension);
110  }
111 
120  constexpr GKO_ATTRIBUTES operator bool() const
121  {
122  return static_cast<bool>(first_) && static_cast<bool>(rest_);
123  }
124 
133  friend constexpr GKO_ATTRIBUTES bool operator==(const dim &x, const dim &y)
134  {
135  return x.first_ == y.first_ && x.rest_ == y.rest_;
136  }
137 
146  friend constexpr GKO_ATTRIBUTES dim operator*(const dim &x, const dim &y)
147  {
148  return dim(x.first_ * y.first_, x.rest_ * y.rest_);
149  }
150 
151 private:
152  constexpr GKO_ATTRIBUTES dim(const dimension_type first,
154  : first_{first}, rest_{rest}
155  {}
156 
157  dimension_type first_;
158  dim<dimensionality - 1, dimension_type> rest_;
159 };
160 
161 
162 // base case for dim recursive template
163 template <typename DimensionType>
164 struct dim<1u, DimensionType> {
165  static constexpr size_type dimensionality = 1u;
166 
167  using dimension_type = DimensionType;
168 
169  constexpr GKO_ATTRIBUTES dim(const dimension_type &size = dimension_type{})
170  : first_{size}
171  {}
172 
173  constexpr GKO_ATTRIBUTES const dimension_type &operator[](
174  const size_type &dimension) const noexcept
175  {
176  return *(&first_ + dimension);
177  }
178 
179  GKO_ATTRIBUTES dimension_type &operator[](const size_type &dimension)
180  {
181  return *(&first_ + dimension);
182  }
183 
184  constexpr GKO_ATTRIBUTES operator bool() const
185  {
186  return static_cast<bool>(first_);
187  }
188 
189  friend constexpr GKO_ATTRIBUTES bool operator==(const dim &x, const dim &y)
190  {
191  return x.first_ == y.first_;
192  }
193 
194  friend constexpr GKO_ATTRIBUTES dim operator*(const dim &x, const dim &y)
195  {
196  return dim(x.first_ * y.first_);
197  }
198 
199 private:
200  dimension_type first_;
201 };
202 
203 
215 template <size_type Dimensionality, typename DimensionType>
216 constexpr GKO_ATTRIBUTES GKO_INLINE bool operator!=(
219 {
220  return !(x == y);
221 }
222 
223 
233 template <typename DimensionType>
234 constexpr GKO_ATTRIBUTES GKO_INLINE dim<2, DimensionType> transpose(
235  const dim<2, DimensionType> &dimensions) noexcept
236 {
237  return {dimensions[1], dimensions[0]};
238 }
239 
240 
241 } // namespace gko
242 
243 
244 #endif // GKO_CORE_BASE_DIM_HPP_
A type representing the dimensions of a multidimensional object.
Definition: dim.hpp:53
friend constexpr bool operator==(const dim &x, const dim &y)
Checks if two dim objects are equal.
Definition: dim.hpp:133
constexpr const dimension_type & operator[](const size_type &dimension) const noexcept
Returns the requested dimension.
Definition: dim.hpp:97
constexpr bool operator!=(const dim< Dimensionality, DimensionType > &x, const dim< Dimensionality, DimensionType > &y)
Checks if two dim objects are different.
Definition: dim.hpp:216
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
constexpr dim(const dimension_type &first, const Rest &... rest)
Creates a dimension object with the specified dimensions.
Definition: dim.hpp:81
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
dimension_type & operator[](const size_type &dimension) noexcept
Definition: dim.hpp:106
friend constexpr dim operator*(const dim &x, const dim &y)
Multiplies two dim objects.
Definition: dim.hpp:146
constexpr dim(const dimension_type &size=dimension_type{})
Creates a dimension object with all dimensions set to the same value.
Definition: dim.hpp:63
constexpr dim< 2, DimensionType > transpose(const dim< 2, DimensionType > &dimensions) noexcept
Returns a dim<2> object with its dimensions swapped.
Definition: dim.hpp:234