// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2013 Christian Seiler // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #ifndef EIGEN_CXX11META_H #define EIGEN_CXX11META_H #include #include "EmulateArray.h" // Emulate the cxx11 functionality that we need if the compiler doesn't support it. // Visual studio 2015 doesn't advertise itself as cxx11 compliant, although it // supports enough of the standard for our needs #if __cplusplus > 199711L || EIGEN_COMP_MSVC >= 1900 #include "CXX11Workarounds.h" namespace Eigen { namespace internal { /** \internal * \file CXX11/util/CXX11Meta.h * This file contains generic metaprogramming classes which are not specifically related to Eigen. * This file expands upon Core/util/Meta.h and adds support for C++11 specific features. */ template struct type_list { constexpr static int count = sizeof...(tt); }; template struct type_list { constexpr static int count = sizeof...(tt) + 1; typedef t first_type; }; template struct numeric_list { constexpr static std::size_t count = sizeof...(nn); }; template struct numeric_list { constexpr static std::size_t count = sizeof...(nn) + 1; constexpr static T first_value = n; }; /* numeric list constructors * * equivalencies: * constructor result * typename gen_numeric_list::type numeric_list * typename gen_numeric_list_reversed::type numeric_list * typename gen_numeric_list_swapped_pair::type numeric_list * typename gen_numeric_list_repeated::type numeric_list */ template struct gen_numeric_list : gen_numeric_list {}; template struct gen_numeric_list { typedef numeric_list type; }; template struct gen_numeric_list_reversed : gen_numeric_list_reversed {}; template struct gen_numeric_list_reversed { typedef numeric_list type; }; template struct gen_numeric_list_swapped_pair : gen_numeric_list_swapped_pair {}; template struct gen_numeric_list_swapped_pair { typedef numeric_list type; }; template struct gen_numeric_list_repeated : gen_numeric_list_repeated {}; template struct gen_numeric_list_repeated { typedef numeric_list type; }; /* list manipulation: concatenate */ template struct concat; template struct concat, type_list> { typedef type_list type; }; template struct concat, numeric_list > { typedef numeric_list type; }; template struct mconcat; template struct mconcat { typedef a type; }; template struct mconcat : concat {}; template struct mconcat : concat::type> {}; /* list manipulation: extract slices */ template struct take; template struct take> : concat, typename take>::type> {}; template struct take> { typedef type_list<> type; }; template struct take<0, type_list> { typedef type_list<> type; }; template<> struct take<0, type_list<>> { typedef type_list<> type; }; template struct take> : concat, typename take>::type> {}; template struct take> { typedef numeric_list type; }; template struct take<0, numeric_list> { typedef numeric_list type; }; template struct take<0, numeric_list> { typedef numeric_list type; }; template struct h_skip_helper_numeric; template struct h_skip_helper_numeric : h_skip_helper_numeric {}; template struct h_skip_helper_numeric { typedef numeric_list type; }; template struct h_skip_helper_numeric { typedef numeric_list type; }; template struct h_skip_helper_numeric { typedef numeric_list type; }; template struct h_skip_helper_type; template struct h_skip_helper_type : h_skip_helper_type {}; template struct h_skip_helper_type<0, t, tt...> { typedef type_list type; }; template struct h_skip_helper_type { typedef type_list<> type; }; template<> struct h_skip_helper_type<0> { typedef type_list<> type; }; template struct h_skip { template constexpr static inline typename h_skip_helper_numeric::type helper(numeric_list) { return typename h_skip_helper_numeric::type(); } template constexpr static inline typename h_skip_helper_type::type helper(type_list) { return typename h_skip_helper_type::type(); } }; template struct skip { typedef decltype(h_skip::helper(a())) type; }; template struct slice : take::type> {}; /* list manipulation: retrieve single element from list */ template struct get; template struct get> : get> {}; template struct get<0, type_list> { typedef a type; }; template struct get> : get> {}; template struct get<0, numeric_list> { constexpr static T value = a; }; /* always get type, regardless of dummy; good for parameter pack expansion */ template struct id_numeric { typedef t type; }; template struct id_type { typedef t type; }; /* equality checking, flagged version */ template struct is_same_gf : is_same { constexpr static int global_flags = 0; }; /* apply_op to list */ template< bool from_left, // false template class op, typename additional_param, typename... values > struct h_apply_op_helper { typedef type_list::type...> type; }; template< template class op, typename additional_param, typename... values > struct h_apply_op_helper { typedef type_list::type...> type; }; template< bool from_left, template class op, typename additional_param > struct h_apply_op { template constexpr static typename h_apply_op_helper::type helper(type_list) { return typename h_apply_op_helper::type(); } }; template< template class op, typename additional_param, typename a > struct apply_op_from_left { typedef decltype(h_apply_op::helper(a())) type; }; template< template class op, typename additional_param, typename a > struct apply_op_from_right { typedef decltype(h_apply_op::helper(a())) type; }; /* see if an element is in a list */ template< template class test, typename check_against, typename h_list, bool last_check_positive = false > struct contained_in_list; template< template class test, typename check_against, typename h_list > struct contained_in_list { constexpr static bool value = true; }; template< template class test, typename check_against, typename a, typename... as > struct contained_in_list, false> : contained_in_list, test::value> {}; template< template class test, typename check_against EIGEN_TPL_PP_SPEC_HACK_DEFC(typename, empty) > struct contained_in_list, false> { constexpr static bool value = false; }; /* see if an element is in a list and check for global flags */ template< template class test, typename check_against, typename h_list, int default_flags = 0, bool last_check_positive = false, int last_check_flags = default_flags > struct contained_in_list_gf; template< template class test, typename check_against, typename h_list, int default_flags, int last_check_flags > struct contained_in_list_gf { constexpr static bool value = true; constexpr static int global_flags = last_check_flags; }; template< template class test, typename check_against, typename a, typename... as, int default_flags, int last_check_flags > struct contained_in_list_gf, default_flags, false, last_check_flags> : contained_in_list_gf, default_flags, test::value, test::global_flags> {}; template< template class test, typename check_against EIGEN_TPL_PP_SPEC_HACK_DEFC(typename, empty), int default_flags, int last_check_flags > struct contained_in_list_gf, default_flags, false, last_check_flags> { constexpr static bool value = false; constexpr static int global_flags = default_flags; }; /* generic reductions */ template< typename Reducer, typename... Ts > struct reduce; template< typename Reducer > struct reduce { constexpr static inline int run() { return Reducer::Identity; } }; template< typename Reducer, typename A > struct reduce { constexpr static inline A run(A a) { return a; } }; template< typename Reducer, typename A, typename... Ts > struct reduce { constexpr static inline auto run(A a, Ts... ts) -> decltype(Reducer::run(a, reduce::run(ts...))) { return Reducer::run(a, reduce::run(ts...)); } }; /* generic binary operations */ struct sum_op { template EIGEN_DEVICE_FUNC constexpr static inline auto run(A a, B b) -> decltype(a + b) { return a + b; } static constexpr int Identity = 0; }; struct product_op { template EIGEN_DEVICE_FUNC constexpr static inline auto run(A a, B b) -> decltype(a * b) { return a * b; } static constexpr int Identity = 1; }; struct logical_and_op { template constexpr static inline auto run(A a, B b) -> decltype(a && b) { return a && b; } }; struct logical_or_op { template constexpr static inline auto run(A a, B b) -> decltype(a || b) { return a || b; } }; struct equal_op { template constexpr static inline auto run(A a, B b) -> decltype(a == b) { return a == b; } }; struct not_equal_op { template constexpr static inline auto run(A a, B b) -> decltype(a != b) { return a != b; } }; struct lesser_op { template constexpr static inline auto run(A a, B b) -> decltype(a < b) { return a < b; } }; struct lesser_equal_op { template constexpr static inline auto run(A a, B b) -> decltype(a <= b) { return a <= b; } }; struct greater_op { template constexpr static inline auto run(A a, B b) -> decltype(a > b) { return a > b; } }; struct greater_equal_op { template constexpr static inline auto run(A a, B b) -> decltype(a >= b) { return a >= b; } }; /* generic unary operations */ struct not_op { template constexpr static inline auto run(A a) -> decltype(!a) { return !a; } }; struct negation_op { template constexpr static inline auto run(A a) -> decltype(-a) { return -a; } }; struct greater_equal_zero_op { template constexpr static inline auto run(A a) -> decltype(a >= 0) { return a >= 0; } }; /* reductions for lists */ // using auto -> return value spec makes ICC 13.0 and 13.1 crash here, so we have to hack it // together in front... (13.0 doesn't work with array_prod/array_reduce/... anyway, but 13.1 // does... template constexpr inline decltype(reduce::run((*((Ts*)0))...)) arg_prod(Ts... ts) { return reduce::run(ts...); } template constexpr inline decltype(reduce::run((*((Ts*)0))...)) arg_sum(Ts... ts) { return reduce::run(ts...); } /* reverse arrays */ template constexpr inline Array h_array_reverse(Array arr, numeric_list) { return {{array_get(arr)...}}; } template constexpr inline array array_reverse(array arr) { return h_array_reverse(arr, typename gen_numeric_list::type()); } /* generic array reductions */ // can't reuse standard reduce() interface above because Intel's Compiler // *really* doesn't like it, so we just reimplement the stuff // (start from N - 1 and work down to 0 because specialization for // n == N - 1 also doesn't work in Intel's compiler, so it goes into // an infinite loop) template struct h_array_reduce { EIGEN_DEVICE_FUNC constexpr static inline auto run(array arr, T identity) -> decltype(Reducer::run(h_array_reduce::run(arr, identity), array_get(arr))) { return Reducer::run(h_array_reduce::run(arr, identity), array_get(arr)); } }; template struct h_array_reduce { EIGEN_DEVICE_FUNC constexpr static inline T run(const array& arr, T) { return array_get<0>(arr); } }; template struct h_array_reduce { EIGEN_DEVICE_FUNC constexpr static inline T run(const array&, T identity) { return identity; } }; template EIGEN_DEVICE_FUNC constexpr inline auto array_reduce(const array& arr, T identity) -> decltype(h_array_reduce::run(arr, identity)) { return h_array_reduce::run(arr, identity); } /* standard array reductions */ template EIGEN_DEVICE_FUNC constexpr inline auto array_sum(const array& arr) -> decltype(array_reduce(arr, static_cast(0))) { return array_reduce(arr, static_cast(0)); } template EIGEN_DEVICE_FUNC constexpr inline auto array_prod(const array& arr) -> decltype(array_reduce(arr, static_cast(1))) { return array_reduce(arr, static_cast(1)); } template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE t array_prod(const std::vector& a) { eigen_assert(a.size() > 0); t prod = 1; for (size_t i = 0; i < a.size(); ++i) { prod *= a[i]; } return prod; } /* zip an array */ template constexpr inline array h_array_zip(array a, array b, numeric_list) { return array{{ Op::run(array_get(a), array_get(b))... }}; } template constexpr inline array array_zip(array a, array b) { return h_array_zip(a, b, typename gen_numeric_list::type()); } /* zip an array and reduce the result */ template constexpr inline auto h_array_zip_and_reduce(array a, array b, numeric_list) -> decltype(reduce::type...>::run(Op::run(array_get(a), array_get(b))...)) { return reduce::type...>::run(Op::run(array_get(a), array_get(b))...); } template constexpr inline auto array_zip_and_reduce(array a, array b) -> decltype(h_array_zip_and_reduce(a, b, typename gen_numeric_list::type())) { return h_array_zip_and_reduce(a, b, typename gen_numeric_list::type()); } /* apply stuff to an array */ template constexpr inline array h_array_apply(array a, numeric_list) { return array{{ Op::run(array_get(a))... }}; } template constexpr inline array array_apply(array a) { return h_array_apply(a, typename gen_numeric_list::type()); } /* apply stuff to an array and reduce */ template constexpr inline auto h_array_apply_and_reduce(array arr, numeric_list) -> decltype(reduce::type...>::run(Op::run(array_get(arr))...)) { return reduce::type...>::run(Op::run(array_get(arr))...); } template constexpr inline auto array_apply_and_reduce(array a) -> decltype(h_array_apply_and_reduce(a, typename gen_numeric_list::type())) { return h_array_apply_and_reduce(a, typename gen_numeric_list::type()); } /* repeat a value n times (and make an array out of it * usage: * array = repeat<16>(42); */ template struct h_repeat { template constexpr static inline array run(t v, numeric_list) { return {{ typename id_numeric::type(v)... }}; } }; template constexpr array repeat(t v) { return h_repeat::run(v, typename gen_numeric_list::type()); } /* instantiate a class by a C-style array */ template struct h_instantiate_by_c_array; template struct h_instantiate_by_c_array { static InstType run(ArrType* arr, Ps... args) { return h_instantiate_by_c_array::run(arr + 1, args..., arr[0]); } }; template struct h_instantiate_by_c_array { static InstType run(ArrType* arr, Ps... args) { return h_instantiate_by_c_array::run(arr + 1, arr[0], args...); } }; template struct h_instantiate_by_c_array { static InstType run(ArrType* arr, Ps... args) { (void)arr; return InstType(args...); } }; template struct h_instantiate_by_c_array { static InstType run(ArrType* arr, Ps... args) { (void)arr; return InstType(args...); } }; template InstType instantiate_by_c_array(ArrType* arr) { return h_instantiate_by_c_array::run(arr); } } // end namespace internal } // end namespace Eigen #else // Non C++11, fallback to emulation mode #include "EmulateCXX11Meta.h" #endif #endif // EIGEN_CXX11META_H