10#ifndef EIGEN_CXX11META_H
11#define EIGEN_CXX11META_H
14#include "EmulateArray.h"
16#include "CXX11Workarounds.h"
28template<
typename... tt>
29struct type_list {
constexpr static int count =
sizeof...(tt); };
31template<
typename t,
typename... tt>
32struct type_list<t, tt...> {
constexpr static int count =
sizeof...(tt) + 1;
typedef t first_type; };
34template<
typename T, T... nn>
35struct numeric_list {
constexpr static std::size_t count =
sizeof...(nn); };
37template<
typename T, T n, T... nn>
38struct numeric_list<T, n, nn...> {
static const std::size_t count =
sizeof...(nn) + 1;
const static T first_value = n; };
40#ifndef EIGEN_PARSED_BY_DOXYGEN
51template<
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...> {};
52template<
typename T, T start, T... ii>
struct gen_numeric_list<T, 0, start, ii...> {
typedef numeric_list<T, ii...> type; };
54template<
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> {};
55template<
typename T, T start, T... ii>
struct gen_numeric_list_reversed<T, 0, start, ii...> {
typedef numeric_list<T, ii...> type; };
57template<
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...> {};
58template<
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; };
60template<
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...> {};
61template<
typename T, T V, T... nn>
struct gen_numeric_list_repeated<T, 0, V, nn...> {
typedef numeric_list<T, nn...> type; };
65template<
class a,
class b>
struct concat;
67template<
typename... as,
typename... bs>
struct concat<type_list<as...>, type_list<bs...>> {
typedef type_list<as..., bs...> type; };
68template<
typename T, T... as, T... bs>
struct concat<numeric_list<T, as...>, numeric_list<T, bs...> > {
typedef numeric_list<T, as..., bs...> type; };
70template<
typename... p>
struct mconcat;
71template<
typename a>
struct mconcat<a> {
typedef a type; };
72template<
typename a,
typename b>
struct mconcat<a, b> : concat<a, b> {};
73template<
typename a,
typename b,
typename... cs>
struct mconcat<a, b, cs...> : concat<a, typename mconcat<b, cs...>::type> {};
77template<
int n,
typename x>
struct take;
78template<
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> {};
79template<
int n>
struct take<n, type_list<>> {
typedef type_list<> type; };
80template<
typename a,
typename... as>
struct take<0, type_list<a, as...>> {
typedef type_list<> type; };
81template<>
struct take<0, type_list<>> {
typedef type_list<> type; };
83template<
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> {};
84template<
typename T,
int n>
struct take<n, numeric_list<T>> {
typedef numeric_list<T> type; };
85template<
typename T, T a, T... as>
struct take<0, numeric_list<T, a, as...>> {
typedef numeric_list<T> type; };
86template<
typename T>
struct take<0, numeric_list<T>> {
typedef numeric_list<T> type; };
88template<
typename T,
int n, T... ii>
struct h_skip_helper_numeric;
89template<
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...> {};
90template<
typename T, T i, T... ii>
struct h_skip_helper_numeric<T, 0, i, ii...> {
typedef numeric_list<T, i, ii...> type; };
91template<
typename T,
int n>
struct h_skip_helper_numeric<T, n> {
typedef numeric_list<T> type; };
92template<
typename T>
struct h_skip_helper_numeric<T, 0> {
typedef numeric_list<T> type; };
94template<
int n,
typename... tt>
struct h_skip_helper_type;
95template<
int n,
typename t,
typename... tt>
struct h_skip_helper_type<n, t, tt...> : h_skip_helper_type<n-1, tt...> {};
96template<
typename t,
typename... tt>
struct h_skip_helper_type<0, t, tt...> {
typedef type_list<t, tt...> type; };
97template<
int n>
struct h_skip_helper_type<n> {
typedef type_list<> type; };
98template<>
struct h_skip_helper_type<0> {
typedef type_list<> type; };
103 template<
typename T, T... ii>
104 constexpr static EIGEN_STRONG_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(); }
105 template<
typename... tt>
106 constexpr static EIGEN_STRONG_INLINE
typename h_skip_helper_type<n, tt...>::type helper(type_list<tt...>) {
return typename h_skip_helper_type<n, tt...>::type(); }
109template<
int n,
typename a>
struct skip {
typedef decltype(h_skip<n>::helper(a())) type; };
111template<
int start,
int count,
typename a>
struct slice : take<count, typename skip<start, a>::type> {};
115template<
int n,
typename x>
struct get;
117template<
int n,
typename a,
typename... as>
struct get<n, type_list<a, as...>> : get<n-1, type_list<as...>> {};
118template<
typename a,
typename... as>
struct get<0, type_list<a, as...>> {
typedef a type; };
120template<
typename T,
int n, T a, T... as>
struct get<n, numeric_list<T, a, as...>> : get<n-1, numeric_list<T, as...>> {};
121template<
typename T, T a, T... as>
struct get<0, numeric_list<T, a, as...>> {
constexpr static T value = a; };
123template<std::size_t n,
typename T, T a, T... as>
constexpr T array_get(
const numeric_list<T, a, as...>&) {
124 return get<(int)n, numeric_list<T, a, as...>>::value;
129template<
typename T, T dummy,
typename t>
struct id_numeric {
typedef t type; };
130template<
typename dummy,
typename t>
struct id_type {
typedef t type; };
134template<
typename a,
typename b>
struct is_same_gf : is_same<a, b> {
constexpr static int global_flags = 0; };
140 template<
typename,
typename>
class op,
141 typename additional_param,
144struct h_apply_op_helper {
typedef type_list<typename op<values, additional_param>::type...> type; };
146 template<
typename,
typename>
class op,
147 typename additional_param,
150struct h_apply_op_helper<true, op, additional_param, values...> {
typedef type_list<typename op<additional_param, values>::type...> type; };
154 template<
typename,
typename>
class op,
155 typename additional_param
159 template<
typename... values>
160 constexpr static typename h_apply_op_helper<from_left, op, additional_param, values...>::type helper(type_list<values...>)
161 {
return typename h_apply_op_helper<from_left, op, additional_param, values...>::type(); }
165 template<
typename,
typename>
class op,
166 typename additional_param,
169struct apply_op_from_left {
typedef decltype(h_apply_op<true, op, additional_param>::helper(a())) type; };
172 template<
typename,
typename>
class op,
173 typename additional_param,
176struct apply_op_from_right {
typedef decltype(h_apply_op<false, op, additional_param>::helper(a())) type; };
181 template<
typename,
typename>
class test,
182 typename check_against,
184 bool last_check_positive =
false
186struct contained_in_list;
189 template<
typename,
typename>
class test,
190 typename check_against,
193struct contained_in_list<test, check_against, h_list, true>
195 constexpr static bool value =
true;
199 template<
typename,
typename>
class test,
200 typename check_against,
204struct 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> {};
207 template<
typename,
typename>
class test,
208 typename check_against
209 EIGEN_TPL_PP_SPEC_HACK_DEFC(
typename, empty)
211struct contained_in_list<test, check_against, type_list<EIGEN_TPL_PP_SPEC_HACK_USE(empty)>, false> {
constexpr static bool value =
false; };
216 template<
typename,
typename>
class test,
217 typename check_against,
219 int default_flags = 0,
220 bool last_check_positive =
false,
221 int last_check_flags = default_flags
223struct contained_in_list_gf;
226 template<
typename,
typename>
class test,
227 typename check_against,
232struct contained_in_list_gf<test, check_against, h_list, default_flags, true, last_check_flags>
234 constexpr static bool value =
true;
235 constexpr static int global_flags = last_check_flags;
239 template<
typename,
typename>
class test,
240 typename check_against,
246struct 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> {};
249 template<
typename,
typename>
class test,
250 typename check_against
251 EIGEN_TPL_PP_SPEC_HACK_DEFC(
typename, empty),
255struct 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; };
266>
struct reduce<Reducer>
268 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE
int run() {
return Reducer::Identity; }
274>
struct reduce<Reducer, A>
276 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE A run(A a) {
return a; }
283>
struct reduce<Reducer, A, Ts...>
285 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE
auto run(A a, Ts... ts) ->
decltype(Reducer::run(a, reduce<Reducer, Ts...>::run(ts...))) {
286 return Reducer::run(a, reduce<Reducer, Ts...>::run(ts...));
293 template<
typename A,
typename B> EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a + b) {
return a + b; }
294 static constexpr int Identity = 0;
297 template<
typename A,
typename B> EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a * b) {
return a * b; }
298 static constexpr int Identity = 1;
301struct logical_and_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a && b) {
return a && b; } };
302struct logical_or_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a || b) {
return a || b; } };
304struct equal_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a == b) {
return a == b; } };
305struct not_equal_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a != b) {
return a != b; } };
306struct lesser_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a < b) {
return a < b; } };
307struct lesser_equal_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a <= b) {
return a <= b; } };
308struct greater_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a > b) {
return a > b; } };
309struct greater_equal_op {
template<
typename A,
typename B>
constexpr static EIGEN_STRONG_INLINE
auto run(A a, B b) ->
decltype(a >= b) {
return a >= b; } };
313struct not_op {
template<
typename A>
constexpr static EIGEN_STRONG_INLINE
auto run(A a) ->
decltype(!a) {
return !a; } };
314struct negation_op {
template<
typename A>
constexpr static EIGEN_STRONG_INLINE
auto run(A a) ->
decltype(-a) {
return -a; } };
315struct greater_equal_zero_op {
template<
typename A>
constexpr static EIGEN_STRONG_INLINE
auto run(A a) ->
decltype(a >= 0) {
return a >= 0; } };
323template<
typename... Ts>
324EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
decltype(reduce<product_op, Ts...>::run((*((Ts*)0))...)) arg_prod(Ts... ts)
326 return reduce<product_op, Ts...>::run(ts...);
329template<
typename... Ts>
330constexpr EIGEN_STRONG_INLINE
decltype(reduce<sum_op, Ts...>::run((*((Ts*)0))...)) arg_sum(Ts... ts)
332 return reduce<sum_op, Ts...>::run(ts...);
337template<
typename Array,
int... n>
338constexpr EIGEN_STRONG_INLINE Array h_array_reverse(Array arr, numeric_list<int, n...>)
340 return {{array_get<
sizeof...(n) - n - 1>(arr)...}};
343template<
typename T, std::
size_t N>
344constexpr EIGEN_STRONG_INLINE array<T, N> array_reverse(array<T, N> arr)
346 return h_array_reverse(arr,
typename gen_numeric_list<int, N>::type());
357template<
typename Reducer,
typename T, std::size_t N, std::size_t n = N - 1>
358struct h_array_reduce {
359 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE
auto run(array<T, N> arr, T identity) ->
decltype(Reducer::run(h_array_reduce<Reducer, T, N, n - 1>::run(arr, identity), array_get<n>(arr)))
361 return Reducer::run(h_array_reduce<Reducer, T, N, n - 1>::run(arr, identity), array_get<n>(arr));
365template<
typename Reducer,
typename T, std::
size_t N>
366struct h_array_reduce<Reducer, T, N, 0>
368 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE T run(
const array<T, N>& arr, T)
370 return array_get<0>(arr);
374template<
typename Reducer,
typename T>
375struct h_array_reduce<Reducer, T, 0>
377 EIGEN_DEVICE_FUNC
constexpr static EIGEN_STRONG_INLINE T run(
const array<T, 0>&, T identity)
383template<
typename Reducer,
typename T, std::
size_t N>
384EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
auto array_reduce(
const array<T, N>& arr, T identity) ->
decltype(h_array_reduce<Reducer, T, N>::run(arr, identity))
386 return h_array_reduce<Reducer, T, N>::run(arr, identity);
391template<
typename T, std::
size_t N>
392EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
auto array_sum(
const array<T, N>& arr) ->
decltype(array_reduce<sum_op, T, N>(arr,
static_cast<T
>(0)))
394 return array_reduce<sum_op, T, N>(arr,
static_cast<T
>(0));
397template<
typename T, std::
size_t N>
398EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
auto array_prod(
const array<T, N>& arr) ->
decltype(array_reduce<product_op, T, N>(arr,
static_cast<T
>(1)))
400 return array_reduce<product_op, T, N>(arr,
static_cast<T
>(1));
404EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE t array_prod(
const std::vector<t>& a) {
405 eigen_assert(a.size() > 0);
407 for (
size_t i = 0; i < a.size(); ++i) { prod *= a[i]; }
413template<
typename Op,
typename A,
typename B, std::size_t N,
int... n>
414constexpr EIGEN_STRONG_INLINE array<
decltype(Op::run(A(), B())),N> h_array_zip(array<A, N> a, array<B, N> b, numeric_list<int, n...>)
416 return array<
decltype(Op::run(A(), B())),N>{{ Op::run(array_get<n>(a), array_get<n>(b))... }};
419template<
typename Op,
typename A,
typename B, std::
size_t N>
420constexpr EIGEN_STRONG_INLINE array<
decltype(Op::run(A(), B())),N> array_zip(array<A, N> a, array<B, N> b)
422 return h_array_zip<Op>(a, b,
typename gen_numeric_list<int, N>::type());
427template<
typename Reducer,
typename Op,
typename A,
typename B, std::size_t N,
int... n>
428constexpr EIGEN_STRONG_INLINE
auto h_array_zip_and_reduce(array<A, N> a, 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))...))
430 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))...);
433template<
typename Reducer,
typename Op,
typename A,
typename B, std::
size_t N>
434constexpr EIGEN_STRONG_INLINE
auto array_zip_and_reduce(array<A, N> a, array<B, N> b) ->
decltype(h_array_zip_and_reduce<Reducer, Op, A, B, N>(a, b,
typename gen_numeric_list<int, N>::type()))
436 return h_array_zip_and_reduce<Reducer, Op, A, B, N>(a, b,
typename gen_numeric_list<int, N>::type());
441template<
typename Op,
typename A, std::size_t N,
int... n>
442constexpr EIGEN_STRONG_INLINE array<
decltype(Op::run(A())),N> h_array_apply(array<A, N> a, numeric_list<int, n...>)
444 return array<
decltype(Op::run(A())),N>{{ Op::run(array_get<n>(a))... }};
447template<
typename Op,
typename A, std::
size_t N>
448constexpr EIGEN_STRONG_INLINE array<
decltype(Op::run(A())),N> array_apply(array<A, N> a)
450 return h_array_apply<Op>(a,
typename gen_numeric_list<int, N>::type());
455template<
typename Reducer,
typename Op,
typename A, std::size_t N,
int... n>
456constexpr EIGEN_STRONG_INLINE
auto h_array_apply_and_reduce(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))...))
458 return reduce<Reducer,
typename id_numeric<int,n,
decltype(Op::run(A()))>::type...>::run(Op::run(array_get<n>(arr))...);
461template<
typename Reducer,
typename Op,
typename A, std::
size_t N>
462constexpr EIGEN_STRONG_INLINE
auto array_apply_and_reduce(array<A, N> a) ->
decltype(h_array_apply_and_reduce<Reducer, Op, A, N>(a,
typename gen_numeric_list<int, N>::type()))
464 return h_array_apply_and_reduce<Reducer, Op, A, N>(a,
typename gen_numeric_list<int, N>::type());
475 template<
typename t,
int... ii>
476 constexpr static EIGEN_STRONG_INLINE array<t, n> run(t v, numeric_list<int, ii...>)
478 return {{
typename id_numeric<int, ii, t>::type(v)... }};
482template<
int n,
typename t>
483constexpr array<t, n> repeat(t v) {
return h_repeat<n>::run(v,
typename gen_numeric_list<int, n>::type()); }
486template<
class InstType,
typename ArrType, std::size_t N,
bool Reverse,
typename... Ps>
487struct h_instantiate_by_c_array;
489template<
class InstType,
typename ArrType, std::size_t N,
typename... Ps>
490struct h_instantiate_by_c_array<InstType, ArrType, N, false, Ps...>
492 static InstType run(ArrType* arr, Ps... args)
494 return h_instantiate_by_c_array<InstType, ArrType, N - 1, false, Ps..., ArrType>::run(arr + 1, args..., arr[0]);
498template<
class InstType,
typename ArrType, std::size_t N,
typename... Ps>
499struct h_instantiate_by_c_array<InstType, ArrType, N, true, Ps...>
501 static InstType run(ArrType* arr, Ps... args)
503 return h_instantiate_by_c_array<InstType, ArrType, N - 1, false, ArrType, Ps...>::run(arr + 1, arr[0], args...);
507template<
class InstType,
typename ArrType,
typename... Ps>
508struct h_instantiate_by_c_array<InstType, ArrType, 0, false, Ps...>
510 static InstType run(ArrType* arr, Ps... args)
513 return InstType(args...);
517template<
class InstType,
typename ArrType,
typename... Ps>
518struct h_instantiate_by_c_array<InstType, ArrType, 0, true, Ps...>
520 static InstType run(ArrType* arr, Ps... args)
523 return InstType(args...);
527template<
class InstType,
typename ArrType, std::
size_t N,
bool Reverse = false>
528InstType instantiate_by_c_array(ArrType* arr)
530 return h_instantiate_by_c_array<InstType, ArrType, N, Reverse>::run(arr);
Namespace containing all symbols from the Eigen library.