StaticSize.hpp 7.91 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
#pragma once

#include <array>
#include <tuple>
#include <type_traits>

7
#include <dune/common/typeutilities.hh>
8
9
#include <amdis/common/TypeTraits.hpp>

Praetorius, Simon's avatar
Praetorius, Simon committed
10
#if AMDIS_HAS_MTL
11
12
#include <boost/numeric/mtl/operation/static_size.hpp>
#endif
13

Praetorius, Simon's avatar
Praetorius, Simon committed
14
15
16
17
namespace AMDiS
{
  namespace Impl
  {
18
19
20
    template <class Container>
    struct SizeImpl
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
21
#if AMDIS_HAS_MTL
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      // MTL4: Try if a mtl::static_size is specialized for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<6>)
        -> decltype(std::integral_constant<std::size_t,(mtl::static_num_rows<T>::value * mtl::static_num_cols<T>::value)>{})
      {
        return {};
      }
#endif

      // Eigen: Try if a static SizeAtCompileTime constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<5>)
        -> decltype(std::integral_constant<std::size_t,T::SizeAtCompileTime>{})
      {
        return {};
      }

      // Try if tuple_size is implemented for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<4>)
        -> decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>{})
      {
        return {};
      }

      // Try if a static dimension constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<3>)
        -> decltype(std::integral_constant<std::size_t,T::dimension>{})
      {
        return {};
      }

      // Try if a static rows and cols constants are specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<2>)
        -> decltype(std::integral_constant<std::size_t,(T::rows * T::cols)>{})
      {
        return {};
      }

      // Try if there's a static constexpr size()
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<1>)
        -> decltype(std::integral_constant<std::size_t,T::size()>{})
      {
        return {};
      }

      // Arithmetic types have size 1 otherwise size is 0
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<0>)
74
        -> decltype(std::integral_constant<std::size_t, (std::is_arithmetic_v<T> ? 1 : 0)>{})
75
76
77
78
79
80
81
82
83
      {
        return {};
      }

      static constexpr auto eval(Container const& container)
      {
        return eval(container, Dune::PriorityTag<42>{});
      }
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
84
85
86

  } // end namespace Impl

87

88
89
90
91
92
93
94
95
96
97
98
99
  /**
  * \brief Return a static constant size of the container
  *
  * \param container Container whose size is queried
  * \return Size of t
  *
  * If the size of t is known at compile time the size is
  * returned as std::integral_constant<std::size_t, size>.
  * Otherwise 0 is returned by default, or 1 for arithmetic types.
  */
  template <class Container>
  constexpr auto static_size(Container const& container)
100
  {
101
102
    return Impl::SizeImpl<Container>::eval(container);
  }
103

104
105
106
  template <class Container>
  constexpr std::size_t static_size_v
    = decltype(static_size(std::declval<remove_cvref_t<Container>>()))::value;
107

108
109
110
111
112
  template <class T, std::size_t S0, std::size_t S1 = static_size_v<T>>
  struct CheckSize
  {
    static_assert(S0 == S1, "Non-matching size");
  };
113
114


115
116
117
118
119
  namespace Impl
  {
    template <class Matrix>
    struct NumRowsImpl
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
120
#if AMDIS_HAS_MTL
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      // MTL4: Try if a mtl::static_num_rows is specialized for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<4>)
        -> decltype(std::integral_constant<std::size_t,mtl::static_num_rows<T>::value>{})
      {
        return {};
      }
#endif

      // Eigen: Try if a static RowsAtCompileTime constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<3>)
        -> decltype(std::integral_constant<std::size_t,T::RowsAtCompileTime>{})
      {
        return {};
      }

      // Try if a static rows constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<2>)
        -> decltype(std::integral_constant<std::size_t,T::rows>{})
      {
        return {};
      }

      // Try if there's a static constexpr N()
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<1>)
        -> decltype(std::integral_constant<std::size_t,T::N()>{})
      {
        return {};
      }

      // Fallback-size is 1 for arithmetic types or 0
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<0>)
157
        -> decltype(std::integral_constant<std::size_t, (std::is_arithmetic_v<T> ? 1 : 0)>{})
158
159
160
161
162
163
164
165
166
      {
        return {};
      }

      static constexpr auto eval(Matrix const& matrix)
      {
        return eval(matrix, Dune::PriorityTag<42>{});
      }
    };
167

168
169
170
  } // end namespace Impl


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /**
  * \brief Return a static constant rows of the matrix
  *
  * \param container Matrix whose number of rows is queried
  * \return Number of rows of matrix
  *
  * If the size of t is known at compile time the number of rows is
  * returned as std::integral_constant<std::size_t, size>.
  * Otherwise 0 is returned by default, or 1 for arithmetic types.
  */
  template <class Matrix>
  constexpr auto static_num_rows(Matrix const& matrix)
  {
    return Impl::NumRowsImpl<Matrix>::eval(matrix);
  }

  template <class Matrix>
  constexpr std::size_t static_num_rows_v
    = decltype(static_num_rows(std::declval<remove_cvref_t<Matrix>>()))::value;
190

191
192
193
194
195
  template <class T, std::size_t S0, std::size_t S1 = static_num_rows<T>>
  struct CheckNumRows
  {
    static_assert(S0 == S1, "Non-matching num rows");
  };
196
197
198

  namespace Impl
  {
199
200
201
    template <class Matrix>
    struct NumColsImpl
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
202
#if AMDIS_HAS_MTL
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
      // MTL4: Try if a mtl::static_num_cols is specialized for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<4>)
        -> decltype(std::integral_constant<std::size_t,mtl::static_num_cols<T>::value>{})
      {
        return {};
      }
#endif

      // Eigen: Try if a static ColsAtCompileTime constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<3>)
        -> decltype(std::integral_constant<std::size_t,T::ColsAtCompileTime>{})
      {
        return {};
      }

      // Try if a static cols constant is specified for class
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<2>)
        -> decltype(std::integral_constant<std::size_t,T::cols>{})
      {
        return {};
      }

      // Try if there's a static constexpr M()
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<1>)
        -> decltype(std::integral_constant<std::size_t,T::M()>{})
      {
        return {};
      }

      // Fallback-size is 0
      template <class T>
      static constexpr auto eval(T const&, Dune::PriorityTag<0>)
239
        -> decltype(std::integral_constant<std::size_t, (std::is_arithmetic_v<T> ? 1 : 0)>{})
240
241
242
243
244
245
246
247
248
      {
        return {};
      }

      static constexpr auto eval(Matrix const& matrix)
      {
        return eval(matrix, Dune::PriorityTag<42>{});
      }
    };
249

250
  } // end namespace Impl
251

252

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  /**
  * \brief Return a static constant columns of the matrix
  *
  * \param container Matrix whose number of columns is queried
  * \return Number of columns of matrix
  *
  * If the size of matrix is known at compile time the number if cols is
  * returned as std::integral_constant<std::size_t, size>.
  * Otherwise 0 is returned by default, or 1 for arithmetic types.
  */
  template <class Matrix>
  constexpr auto static_num_cols(Matrix const& matrix)
  {
    return Impl::NumColsImpl<Matrix>::eval(matrix);
  }
268

269
270
271
  template <class Matrix>
  constexpr std::size_t static_num_cols_v
    = decltype(static_num_cols(std::declval<remove_cvref_t<Matrix>>()))::value;
Praetorius, Simon's avatar
Praetorius, Simon committed
272

273
274
275
276
277
  template <class T, std::size_t S0, std::size_t S1 = static_num_cols<T>>
  struct CheckNumCols
  {
    static_assert(S0 == S1, "Non-matching num rows");
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
278
279

} // end namespace AMDiS