CXX11Meta.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11META_H
11 #define EIGEN_CXX11META_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
23 template<typename... tt>
24 struct type_list { constexpr static int count = sizeof...(tt); };
25 
26 template<typename t, typename... tt>
27 struct type_list<t, tt...> { constexpr static int count = sizeof...(tt) + 1; typedef t first_type; };
28 
29 template<typename T, T... nn>
30 struct numeric_list { constexpr static std::size_t count = sizeof...(nn); };
31 
32 template<typename T, T n, T... nn>
33 struct numeric_list<T, n, nn...> { constexpr static std::size_t count = sizeof...(nn) + 1; constexpr static T first_value = n; };
34 
35 /* numeric list constructors
36  *
37  * equivalencies:
38  * constructor result
39  * typename gen_numeric_list<int, 5>::type numeric_list<int, 0,1,2,3,4>
40  * typename gen_numeric_list_reversed<int, 5>::type numeric_list<int, 4,3,2,1,0>
41  * typename gen_numeric_list_swapped_pair<int, 5,1,2>::type numeric_list<int, 0,2,1,3,4>
42  * typename gen_numeric_list_repeated<int, 0, 5>::type numeric_list<int, 0,0,0,0,0>
43  */
44 
45 template<typename T, std::size_t n, T start = 0, T... ii> struct gen_numeric_list : gen_numeric_list<T, n-1, start, start + n-1, ii...> {};
46 template<typename T, T start, T... ii> struct gen_numeric_list<T, 0, start, ii...> { typedef numeric_list<T, ii...> type; };
47 
48 template<typename T, std::size_t n, T start = 0, T... ii> struct gen_numeric_list_reversed : gen_numeric_list_reversed<T, n-1, start, ii..., start + n-1> {};
49 template<typename T, T start, T... ii> struct gen_numeric_list_reversed<T, 0, start, ii...> { typedef numeric_list<T, ii...> type; };
50 
51 template<typename T, std::size_t n, T a, T b, T start = 0, T... ii> struct gen_numeric_list_swapped_pair : gen_numeric_list_swapped_pair<T, n-1, a, b, start, (start + n-1) == a ? b : ((start + n-1) == b ? a : (start + n-1)), ii...> {};
52 template<typename T, T a, T b, T start, T... ii> struct gen_numeric_list_swapped_pair<T, 0, a, b, start, ii...> { typedef numeric_list<T, ii...> type; };
53 
54 template<typename T, std::size_t n, T V, T... nn> struct gen_numeric_list_repeated : gen_numeric_list_repeated<T, n-1, V, V, nn...> {};
55 template<typename T, T V, T... nn> struct gen_numeric_list_repeated<T, 0, V, nn...> { typedef numeric_list<T, nn...> type; };
56 
57 /* list manipulation: concatenate */
58 
59 template<class a, class b> struct concat;
60 
61 template<typename... as, typename... bs> struct concat<type_list<as...>, type_list<bs...>> { typedef type_list<as..., bs...> type; };
62 template<typename T, T... as, T... bs> struct concat<numeric_list<T, as...>, numeric_list<T, bs...> > { typedef numeric_list<T, as..., bs...> type; };
63 
64 template<typename... p> struct mconcat;
65 template<typename a> struct mconcat<a> { typedef a type; };
66 template<typename a, typename b> struct mconcat<a, b> : concat<a, b> {};
67 template<typename a, typename b, typename... cs> struct mconcat<a, b, cs...> : concat<a, typename mconcat<b, cs...>::type> {};
68 
69 /* list manipulation: extract slices */
70 
71 template<int n, typename x> struct take;
72 template<int n, typename a, typename... as> struct take<n, type_list<a, as...>> : concat<type_list<a>, typename take<n-1, type_list<as...>>::type> {};
73 template<int n> struct take<n, type_list<>> { typedef type_list<> type; };
74 template<typename a, typename... as> struct take<0, type_list<a, as...>> { typedef type_list<> type; };
75 template<> struct take<0, type_list<>> { typedef type_list<> type; };
76 
77 template<typename T, int n, T a, T... as> struct take<n, numeric_list<T, a, as...>> : concat<numeric_list<T, a>, typename take<n-1, numeric_list<T, as...>>::type> {};
78 template<typename T, int n> struct take<n, numeric_list<T>> { typedef numeric_list<T> type; };
79 template<typename T, T a, T... as> struct take<0, numeric_list<T, a, as...>> { typedef numeric_list<T> type; };
80 template<typename T> struct take<0, numeric_list<T>> { typedef numeric_list<T> type; };
81 
82 template<typename T, int n, T... ii> struct h_skip_helper_numeric;
83 template<typename T, int n, T i, T... ii> struct h_skip_helper_numeric<T, n, i, ii...> : h_skip_helper_numeric<T, n-1, ii...> {};
84 template<typename T, T i, T... ii> struct h_skip_helper_numeric<T, 0, i, ii...> { typedef numeric_list<T, i, ii...> type; };
85 template<typename T, int n> struct h_skip_helper_numeric<T, n> { typedef numeric_list<T> type; };
86 template<typename T> struct h_skip_helper_numeric<T, 0> { typedef numeric_list<T> type; };
87 
88 template<int n, typename... tt> struct h_skip_helper_type;
89 template<int n, typename t, typename... tt> struct h_skip_helper_type<n, t, tt...> : h_skip_helper_type<n-1, tt...> {};
90 template<typename t, typename... tt> struct h_skip_helper_type<0, t, tt...> { typedef type_list<t, tt...> type; };
91 template<int n> struct h_skip_helper_type<n> { typedef type_list<> type; };
92 template<> struct h_skip_helper_type<0> { typedef type_list<> type; };
93 
94 template<int n>
95 struct h_skip {
96  template<typename T, T... ii>
97  constexpr static inline typename h_skip_helper_numeric<T, n, ii...>::type helper(numeric_list<T, ii...>) { return typename h_skip_helper_numeric<T, n, ii...>::type(); }
98  template<typename... tt>
99  constexpr static inline typename h_skip_helper_type<n, tt...>::type helper(type_list<tt...>) { return typename h_skip_helper_type<n, tt...>::type(); }
100 };
101 
102 template<int n, typename a> struct skip { typedef decltype(h_skip<n>::helper(a())) type; };
103 
104 template<int start, int count, typename a> struct slice : take<count, typename skip<start, a>::type> {};
105 
106 /* list manipulation: retrieve single element from list */
107 
108 template<int n, typename x> struct get;
109 
110 template<int n, typename a, typename... as> struct get<n, type_list<a, as...>> : get<n-1, type_list<as...>> {};
111 template<typename a, typename... as> struct get<0, type_list<a, as...>> { typedef a type; };
112 template<int n EIGEN_TPL_PP_SPEC_HACK_DEFC(typename, as)> struct get<n, type_list<EIGEN_TPL_PP_SPEC_HACK_USE(as)>> { static_assert((n - n) < 0, "meta-template get: The element to extract from a list must be smaller than the size of the list."); };
113 
114 template<typename T, int n, T a, T... as> struct get<n, numeric_list<T, a, as...>> : get<n-1, numeric_list<T, as...>> {};
115 template<typename T, T a, T... as> struct get<0, numeric_list<T, a, as...>> { constexpr static int value = a; };
116 template<typename T, int n EIGEN_TPL_PP_SPEC_HACK_DEFC(T, as)> struct get<n, numeric_list<T EIGEN_TPL_PP_SPEC_HACK_USEC(as)>> { static_assert((n - n) < 0, "meta-template get: The element to extract from a list must be smaller than the size of the list."); };
117 
118 /* always get type, regardless of dummy; good for parameter pack expansion */
119 
120 template<typename T, T dummy, typename t> struct id_numeric { typedef t type; };
121 template<typename dummy, typename t> struct id_type { typedef t type; };
122 
123 /* equality checking, flagged version */
124 
125 template<typename a, typename b> struct is_same_gf : is_same<a, b> { constexpr static int global_flags = 0; };
126 
127 /* apply_op to list */
128 
129 template<
130  bool from_left, // false
131  template<typename, typename> class op,
132  typename additional_param,
133  typename... values
134 >
135 struct h_apply_op_helper { typedef type_list<typename op<values, additional_param>::type...> type; };
136 template<
137  template<typename, typename> class op,
138  typename additional_param,
139  typename... values
140 >
141 struct h_apply_op_helper<true, op, additional_param, values...> { typedef type_list<typename op<additional_param, values>::type...> type; };
142 
143 template<
144  bool from_left,
145  template<typename, typename> class op,
146  typename additional_param
147 >
148 struct h_apply_op
149 {
150  template<typename... values>
151  constexpr static typename h_apply_op_helper<from_left, op, additional_param, values...>::type helper(type_list<values...>)
152  { return typename h_apply_op_helper<from_left, op, additional_param, values...>::type(); }
153 };
154 
155 template<
156  template<typename, typename> class op,
157  typename additional_param,
158  typename a
159 >
160 struct apply_op_from_left { typedef decltype(h_apply_op<true, op, additional_param>::helper(a())) type; };
161 
162 template<
163  template<typename, typename> class op,
164  typename additional_param,
165  typename a
166 >
167 struct apply_op_from_right { typedef decltype(h_apply_op<false, op, additional_param>::helper(a())) type; };
168 
169 /* see if an element is in a list */
170 
171 template<
172  template<typename, typename> class test,
173  typename check_against,
174  typename h_list,
175  bool last_check_positive = false
176 >
177 struct contained_in_list;
178 
179 template<
180  template<typename, typename> class test,
181  typename check_against,
182  typename h_list
183 >
184 struct contained_in_list<test, check_against, h_list, true>
185 {
186  constexpr static bool value = true;
187 };
188 
189 template<
190  template<typename, typename> class test,
191  typename check_against,
192  typename a,
193  typename... as
194 >
195 struct contained_in_list<test, check_against, type_list<a, as...>, false> : contained_in_list<test, check_against, type_list<as...>, test<check_against, a>::value> {};
196 
197 template<
198  template<typename, typename> class test,
199  typename check_against
200  EIGEN_TPL_PP_SPEC_HACK_DEFC(typename, empty)
201 >
202 struct contained_in_list<test, check_against, type_list<EIGEN_TPL_PP_SPEC_HACK_USE(empty)>, false> { constexpr static bool value = false; };
203 
204 /* see if an element is in a list and check for global flags */
205 
206 template<
207  template<typename, typename> class test,
208  typename check_against,
209  typename h_list,
210  int default_flags = 0,
211  bool last_check_positive = false,
212  int last_check_flags = default_flags
213 >
214 struct contained_in_list_gf;
215 
216 template<
217  template<typename, typename> class test,
218  typename check_against,
219  typename h_list,
220  int default_flags,
221  int last_check_flags
222 >
223 struct contained_in_list_gf<test, check_against, h_list, default_flags, true, last_check_flags>
224 {
225  constexpr static bool value = true;
226  constexpr static int global_flags = last_check_flags;
227 };
228 
229 template<
230  template<typename, typename> class test,
231  typename check_against,
232  typename a,
233  typename... as,
234  int default_flags,
235  int last_check_flags
236 >
237 struct contained_in_list_gf<test, check_against, type_list<a, as...>, default_flags, false, last_check_flags> : contained_in_list_gf<test, check_against, type_list<as...>, default_flags, test<check_against, a>::value, test<check_against, a>::global_flags> {};
238 
239 template<
240  template<typename, typename> class test,
241  typename check_against
242  EIGEN_TPL_PP_SPEC_HACK_DEFC(typename, empty),
243  int default_flags,
244  int last_check_flags
245 >
246 struct contained_in_list_gf<test, check_against, type_list<EIGEN_TPL_PP_SPEC_HACK_USE(empty)>, default_flags, false, last_check_flags> { constexpr static bool value = false; constexpr static int global_flags = default_flags; };
247 
248 /* generic reductions */
249 
250 template<
251  typename Reducer,
252  typename... Ts
253 > struct reduce;
254 
255 template<
256  typename Reducer,
257  typename A,
258  typename... Ts
259 > struct reduce<Reducer, A, Ts...>
260 {
261  constexpr static inline A run(A a, Ts...) { return a; }
262 };
263 
264 template<
265  typename Reducer,
266  typename A,
267  typename B,
268  typename... Ts
269 > struct reduce<Reducer, A, B, Ts...>
270 {
271  constexpr static inline auto run(A a, B b, Ts... ts) -> decltype(Reducer::run(a, reduce<Reducer, B, Ts...>::run(b, ts...))) {
272  return Reducer::run(a, reduce<Reducer, B, Ts...>::run(b, ts...));
273  }
274 };
275 
276 /* generic binary operations */
277 
278 struct sum_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a + b) { return a + b; } };
279 struct product_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a * b) { return a * b; } };
280 
281 struct logical_and_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a && b) { return a && b; } };
282 struct logical_or_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a || b) { return a || b; } };
283 
284 struct equal_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a == b) { return a == b; } };
285 struct not_equal_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a != b) { return a != b; } };
286 struct lesser_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a < b) { return a < b; } };
287 struct lesser_equal_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a <= b) { return a <= b; } };
288 struct greater_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a > b) { return a > b; } };
289 struct greater_equal_op { template<typename A, typename B> constexpr static inline auto run(A a, B b) -> decltype(a >= b) { return a >= b; } };
290 
291 /* generic unary operations */
292 
293 struct not_op { template<typename A> constexpr static inline auto run(A a) -> decltype(!a) { return !a; } };
294 struct negation_op { template<typename A> constexpr static inline auto run(A a) -> decltype(-a) { return -a; } };
295 struct greater_equal_zero_op { template<typename A> constexpr static inline auto run(A a) -> decltype(a >= 0) { return a >= 0; } };
296 
297 
298 /* reductions for lists */
299 
300 // using auto -> return value spec makes ICC 13.0 and 13.1 crash here, so we have to hack it
301 // together in front... (13.0 doesn't work with array_prod/array_reduce/... anyway, but 13.1
302 // does...
303 template<typename... Ts>
304 constexpr inline decltype(reduce<product_op, Ts...>::run((*((Ts*)0))...)) arg_prod(Ts... ts)
305 {
306  return reduce<product_op, Ts...>::run(ts...);
307 }
308 
309 template<typename... Ts>
310 constexpr inline decltype(reduce<sum_op, Ts...>::run((*((Ts*)0))...)) arg_sum(Ts... ts)
311 {
312  return reduce<sum_op, Ts...>::run(ts...);
313 }
314 
315 /* reverse arrays */
316 
317 template<typename Array, int... n>
318 constexpr inline Array h_array_reverse(Array arr, numeric_list<int, n...>)
319 {
320  return {{array_get<sizeof...(n) - n - 1>(arr)...}};
321 }
322 
323 template<typename T, std::size_t N>
324 constexpr inline std::array<T, N> array_reverse(std::array<T, N> arr)
325 {
326  return h_array_reverse(arr, typename gen_numeric_list<int, N>::type());
327 }
328 
329 /* generic array reductions */
330 
331 // can't reuse standard reduce() interface above because Intel's Compiler
332 // *really* doesn't like it, so we just reimplement the stuff
333 // (start from N - 1 and work down to 0 because specialization for
334 // n == N - 1 also doesn't work in Intel's compiler, so it goes into
335 // an infinite loop)
336 template<typename Reducer, typename T, std::size_t N, std::size_t n = N - 1>
337 struct h_array_reduce {
338  constexpr static inline auto run(std::array<T, N> arr) -> decltype(Reducer::run(h_array_reduce<Reducer, T, N, n - 1>::run(arr), array_get<n>(arr)))
339  {
340  return Reducer::run(h_array_reduce<Reducer, T, N, n - 1>::run(arr), array_get<n>(arr));
341  }
342 };
343 
344 template<typename Reducer, typename T, std::size_t N>
345 struct h_array_reduce<Reducer, T, N, 0>
346 {
347  constexpr static inline T run(std::array<T, N> arr)
348  {
349  return array_get<0>(arr);
350  }
351 };
352 
353 template<typename Reducer, typename T, std::size_t N>
354 constexpr inline auto array_reduce(std::array<T, N> arr) -> decltype(h_array_reduce<Reducer, T, N>::run(arr))
355 {
356  return h_array_reduce<Reducer, T, N>::run(arr);
357 }
358 
359 /* standard array reductions */
360 
361 template<typename T, std::size_t N>
362 constexpr inline auto array_sum(std::array<T, N> arr) -> decltype(array_reduce<sum_op, T, N>(arr))
363 {
364  return array_reduce<sum_op, T, N>(arr);
365 }
366 
367 template<typename T, std::size_t N>
368 constexpr inline auto array_prod(std::array<T, N> arr) -> decltype(array_reduce<product_op, T, N>(arr))
369 {
370  return array_reduce<product_op, T, N>(arr);
371 }
372 
373 template<typename t>
374 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE t array_prod(const std::vector<t>& a) {
375  eigen_assert(a.size() > 0);
376  t prod = 1;
377  for (size_t i = 0; i < a.size(); ++i) { prod *= a[i]; }
378  return prod;
379 }
380 
381 /* zip an array */
382 
383 template<typename Op, typename A, typename B, std::size_t N, int... n>
384 constexpr inline std::array<decltype(Op::run(A(), B())),N> h_array_zip(std::array<A, N> a, std::array<B, N> b, numeric_list<int, n...>)
385 {
386  return std::array<decltype(Op::run(A(), B())),N>{{ Op::run(array_get<n>(a), array_get<n>(b))... }};
387 }
388 
389 template<typename Op, typename A, typename B, std::size_t N>
390 constexpr inline std::array<decltype(Op::run(A(), B())),N> array_zip(std::array<A, N> a, std::array<B, N> b)
391 {
392  return h_array_zip<Op>(a, b, typename gen_numeric_list<int, N>::type());
393 }
394 
395 /* zip an array and reduce the result */
396 
397 template<typename Reducer, typename Op, typename A, typename B, std::size_t N, int... n>
398 constexpr inline auto h_array_zip_and_reduce(std::array<A, N> a, std::array<B, N> b, numeric_list<int, n...>) -> decltype(reduce<Reducer, typename id_numeric<int,n,decltype(Op::run(A(), B()))>::type...>::run(Op::run(array_get<n>(a), array_get<n>(b))...))
399 {
400  return reduce<Reducer, typename id_numeric<int,n,decltype(Op::run(A(), B()))>::type...>::run(Op::run(array_get<n>(a), array_get<n>(b))...);
401 }
402 
403 template<typename Reducer, typename Op, typename A, typename B, std::size_t N>
404 constexpr inline auto array_zip_and_reduce(std::array<A, N> a, std::array<B, N> b) -> decltype(h_array_zip_and_reduce<Reducer, Op, A, B, N>(a, b, typename gen_numeric_list<int, N>::type()))
405 {
406  return h_array_zip_and_reduce<Reducer, Op, A, B, N>(a, b, typename gen_numeric_list<int, N>::type());
407 }
408 
409 /* apply stuff to an array */
410 
411 template<typename Op, typename A, std::size_t N, int... n>
412 constexpr inline std::array<decltype(Op::run(A())),N> h_array_apply(std::array<A, N> a, numeric_list<int, n...>)
413 {
414  return std::array<decltype(Op::run(A())),N>{{ Op::run(array_get<n>(a))... }};
415 }
416 
417 template<typename Op, typename A, std::size_t N>
418 constexpr inline std::array<decltype(Op::run(A())),N> array_apply(std::array<A, N> a)
419 {
420  return h_array_apply<Op>(a, typename gen_numeric_list<int, N>::type());
421 }
422 
423 /* apply stuff to an array and reduce */
424 
425 template<typename Reducer, typename Op, typename A, std::size_t N, int... n>
426 constexpr inline auto h_array_apply_and_reduce(std::array<A, N> arr, numeric_list<int, n...>) -> decltype(reduce<Reducer, typename id_numeric<int,n,decltype(Op::run(A()))>::type...>::run(Op::run(array_get<n>(arr))...))
427 {
428  return reduce<Reducer, typename id_numeric<int,n,decltype(Op::run(A()))>::type...>::run(Op::run(array_get<n>(arr))...);
429 }
430 
431 template<typename Reducer, typename Op, typename A, std::size_t N>
432 constexpr inline auto array_apply_and_reduce(std::array<A, N> a) -> decltype(h_array_apply_and_reduce<Reducer, Op, A, N>(a, typename gen_numeric_list<int, N>::type()))
433 {
434  return h_array_apply_and_reduce<Reducer, Op, A, N>(a, typename gen_numeric_list<int, N>::type());
435 }
436 
437 /* repeat a value n times (and make an array out of it
438  * usage:
439  * std::array<int, 16> = repeat<16>(42);
440  */
441 
442 template<int n>
443 struct h_repeat
444 {
445  template<typename t, int... ii>
446  constexpr static inline std::array<t, n> run(t v, numeric_list<int, ii...>)
447  {
448  return {{ typename id_numeric<int, ii, t>::type(v)... }};
449  }
450 };
451 
452 template<int n, typename t>
453 constexpr std::array<t, n> repeat(t v) { return h_repeat<n>::run(v, typename gen_numeric_list<int, n>::type()); }
454 
455 /* instantiate a class by a C-style array */
456 template<class InstType, typename ArrType, std::size_t N, bool Reverse, typename... Ps>
457 struct h_instantiate_by_c_array;
458 
459 template<class InstType, typename ArrType, std::size_t N, typename... Ps>
460 struct h_instantiate_by_c_array<InstType, ArrType, N, false, Ps...>
461 {
462  static InstType run(ArrType* arr, Ps... args)
463  {
464  return h_instantiate_by_c_array<InstType, ArrType, N - 1, false, Ps..., ArrType>::run(arr + 1, args..., arr[0]);
465  }
466 };
467 
468 template<class InstType, typename ArrType, std::size_t N, typename... Ps>
469 struct h_instantiate_by_c_array<InstType, ArrType, N, true, Ps...>
470 {
471  static InstType run(ArrType* arr, Ps... args)
472  {
473  return h_instantiate_by_c_array<InstType, ArrType, N - 1, false, ArrType, Ps...>::run(arr + 1, arr[0], args...);
474  }
475 };
476 
477 template<class InstType, typename ArrType, typename... Ps>
478 struct h_instantiate_by_c_array<InstType, ArrType, 0, false, Ps...>
479 {
480  static InstType run(ArrType* arr, Ps... args)
481  {
482  (void)arr;
483  return InstType(args...);
484  }
485 };
486 
487 template<class InstType, typename ArrType, typename... Ps>
488 struct h_instantiate_by_c_array<InstType, ArrType, 0, true, Ps...>
489 {
490  static InstType run(ArrType* arr, Ps... args)
491  {
492  (void)arr;
493  return InstType(args...);
494  }
495 };
496 
497 template<class InstType, typename ArrType, std::size_t N, bool Reverse = false>
498 InstType instantiate_by_c_array(ArrType* arr)
499 {
500  return h_instantiate_by_c_array<InstType, ArrType, N, Reverse>::run(arr);
501 }
502 
503 } // end namespace internal
504 
505 } // end namespace Eigen
506 
507 #endif // EIGEN_CXX11META_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13