diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-11-12 11:05:54 -0800 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-11-12 11:05:54 -0800 |
commit | 7a1316fcc564b709b361592c6897591d9747c401 (patch) | |
tree | 395a506d3e3ec1c6ed865c1b092f300923ae1f48 /unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h | |
parent | 737d237722c887d5e0841fd5b89e5ef02054a095 (diff) |
Fixed compilation error with xcode.
Diffstat (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h | 184 |
1 files changed, 139 insertions, 45 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h b/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h index 78e1d2bd1..dcd2464f1 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorIndexList.h @@ -77,38 +77,128 @@ struct is_compile_time_constant<const type2index<idx>& > { static constexpr bool value = true; }; + + + +template<typename... T> +struct IndexTuple; + +template<typename T, typename... O> +struct IndexTuple<T, O...> { + constexpr IndexTuple() : head(), others() { } + constexpr IndexTuple(const T& v, const O... o) : head(v), others(o...) { } + + constexpr static int count = 1 + sizeof...(O); + T head; + IndexTuple<O...> others; + typedef T Head; + typedef IndexTuple<O...> Other; +}; + +template<typename T> + struct IndexTuple<T> { + constexpr IndexTuple() : head() { } + constexpr IndexTuple(const T& v) : head(v) { } + + constexpr static int count = 1; + T head; + typedef T Head; +}; + + +template<int N, typename... T> +struct IndexTupleExtractor; + +template<int N, typename T, typename... O> +struct IndexTupleExtractor<N, T, O...> { + + typedef typename IndexTupleExtractor<N-1, O...>::ValType ValType; + + static constexpr ValType& get_val(IndexTuple<T, O...>& val) { + return IndexTupleExtractor<N-1, O...>::get_val(val.others); + } + + static constexpr const ValType& get_val(const IndexTuple<T, O...>& val) { + return IndexTupleExtractor<N-1, O...>::get_val(val.others); + } + template <typename V> + static void set_val(IndexTuple<T, O...>& val, V& new_val) { + IndexTupleExtractor<N-1, O...>::set_val(val.others, new_val); + } + +}; + + template<typename T, typename... O> + struct IndexTupleExtractor<0, T, O...> { + + typedef T ValType; + + static constexpr ValType& get_val(IndexTuple<T, O...>& val) { + return val.head; + } + static constexpr const ValType& get_val(const IndexTuple<T, O...>& val) { + return val.head; + } + template <typename V> + static void set_val(IndexTuple<T, O...>& val, V& new_val) { + val.head = new_val; + } +}; + + + +template <int N, typename T, typename... O> +constexpr typename IndexTupleExtractor<N, T, O...>::ValType& array_get(IndexTuple<T, O...>& tuple) { + return IndexTupleExtractor<N, T, O...>::get_val(tuple); +} +template <int N, typename T, typename... O> +constexpr const typename IndexTupleExtractor<N, T, O...>::ValType& array_get(const IndexTuple<T, O...>& tuple) { + return IndexTupleExtractor<N, T, O...>::get_val(tuple); +} +template <typename T, typename... O> + struct array_size<IndexTuple<T, O...> > { + static const size_t value = IndexTuple<T, O...>::count; +}; +template <typename T, typename... O> + struct array_size<const IndexTuple<T, O...> > { + static const size_t value = IndexTuple<T, O...>::count; +}; + + + + template <DenseIndex Idx> struct tuple_coeff { template <typename... T> - static constexpr DenseIndex get(const DenseIndex i, const std::tuple<T...>& t) { - return std::get<Idx>(t) * (i == Idx) + tuple_coeff<Idx-1>::get(i, t) * (i != Idx); + static constexpr DenseIndex get(const DenseIndex i, const IndexTuple<T...>& t) { + return array_get<Idx>(t) * (i == Idx) + tuple_coeff<Idx-1>::get(i, t) * (i != Idx); } template <typename... T> - static void set(const DenseIndex i, std::tuple<T...>& t, const DenseIndex value) { + static void set(const DenseIndex i, IndexTuple<T...>& t, const DenseIndex value) { if (i == Idx) { - update_value(std::get<Idx>(t), value); + update_value(array_get<Idx>(t), value); } else { tuple_coeff<Idx-1>::set(i, t, value); } } template <typename... T> - static constexpr bool value_known_statically(const DenseIndex i, const std::tuple<T...>& t) { - return ((i == Idx) & is_compile_time_constant<typename std::tuple_element<Idx, std::tuple<T...> >::type>::value) || + static constexpr bool value_known_statically(const DenseIndex i, const IndexTuple<T...>& t) { + return ((i == Idx) & is_compile_time_constant<typename IndexTupleExtractor<Idx, T...>::ValType>::value) || tuple_coeff<Idx-1>::value_known_statically(i, t); } template <typename... T> - static constexpr bool values_up_to_known_statically(const std::tuple<T...>& t) { - return is_compile_time_constant<typename std::tuple_element<Idx, std::tuple<T...> >::type>::value && + static constexpr bool values_up_to_known_statically(const IndexTuple<T...>& t) { + return is_compile_time_constant<typename IndexTupleExtractor<Idx, T...>::ValType>::value && tuple_coeff<Idx-1>::values_up_to_known_statically(t); } template <typename... T> - static constexpr bool values_up_to_statically_known_to_increase(const std::tuple<T...>& t) { - return is_compile_time_constant<typename std::tuple_element<Idx, std::tuple<T...> >::type>::value && - is_compile_time_constant<typename std::tuple_element<Idx-1, std::tuple<T...> >::type>::value && - std::get<Idx>(t) > std::get<Idx-1>(t) && + static constexpr bool values_up_to_statically_known_to_increase(const IndexTuple<T...>& t) { + return is_compile_time_constant<typename IndexTupleExtractor<Idx, T...>::ValType>::value && + is_compile_time_constant<typename IndexTupleExtractor<Idx, T...>::ValType>::value && + array_get<Idx>(t) > array_get<Idx-1>(t) && tuple_coeff<Idx-1>::values_up_to_statically_known_to_increase(t); } }; @@ -116,62 +206,66 @@ struct tuple_coeff { template <> struct tuple_coeff<0> { template <typename... T> - static constexpr DenseIndex get(const DenseIndex i, const std::tuple<T...>& t) { + static constexpr DenseIndex get(const DenseIndex i, const IndexTuple<T...>& t) { // eigen_assert (i == 0); // gcc fails to compile assertions in constexpr - return std::get<0>(t) * (i == 0); + return array_get<0>(t) * (i == 0); } template <typename... T> - static void set(const DenseIndex i, std::tuple<T...>& t, const DenseIndex value) { + static void set(const DenseIndex i, IndexTuple<T...>& t, const DenseIndex value) { eigen_assert (i == 0); - update_value(std::get<0>(t), value); + update_value(array_get<0>(t), value); } template <typename... T> - static constexpr bool value_known_statically(const DenseIndex i, const std::tuple<T...>&) { - // eigen_assert (i == 0); // gcc fails to compile assertions in constexpr - return is_compile_time_constant<typename std::tuple_element<0, std::tuple<T...> >::type>::value & (i == 0); + static constexpr bool value_known_statically(const DenseIndex i, const IndexTuple<T...>&) { + return is_compile_time_constant<typename IndexTupleExtractor<0, T...>::ValType>::value & (i == 0); } template <typename... T> - static constexpr bool values_up_to_known_statically(const std::tuple<T...>&) { - return is_compile_time_constant<typename std::tuple_element<0, std::tuple<T...> >::type>::value; + static constexpr bool values_up_to_known_statically(const IndexTuple<T...>&) { + return is_compile_time_constant<typename IndexTupleExtractor<0, T...>::ValType>::value; } template <typename... T> - static constexpr bool values_up_to_statically_known_to_increase(const std::tuple<T...>&) { + static constexpr bool values_up_to_statically_known_to_increase(const IndexTuple<T...>&) { return true; } }; } // namespace internal + template<typename FirstType, typename... OtherTypes> -struct IndexList : std::tuple<FirstType, OtherTypes...> { + struct IndexList : internal::IndexTuple<FirstType, OtherTypes...> { EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr DenseIndex operator[] (const DenseIndex i) const { - return internal::tuple_coeff<std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value-1>::get(i, *this); + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::get(i, *this); + } + EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr DenseIndex get(const DenseIndex i) const { + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::get(i, *this); } EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC void set(const DenseIndex i, const DenseIndex value) { - return internal::tuple_coeff<std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value-1>::set(i, *this, value); + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::set(i, *this, value); } - constexpr IndexList(const std::tuple<FirstType, OtherTypes...>& other) : std::tuple<FirstType, OtherTypes...>(other) { } - constexpr IndexList() : std::tuple<FirstType, OtherTypes...>() { } + constexpr IndexList(const internal::IndexTuple<FirstType, OtherTypes...>& other) : internal::IndexTuple<FirstType, OtherTypes...>(other) { } + constexpr IndexList(FirstType& first, OtherTypes... other) : internal::IndexTuple<FirstType, OtherTypes...>(first, other...) { } + constexpr IndexList() : internal::IndexTuple<FirstType, OtherTypes...>() { } constexpr bool value_known_statically(const DenseIndex i) const { - return internal::tuple_coeff<std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value-1>::value_known_statically(i, *this); + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::value_known_statically(i, *this); } constexpr bool all_values_known_statically() const { - return internal::tuple_coeff<std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value-1>::values_up_to_known_statically(*this); + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::values_up_to_known_statically(*this); } constexpr bool values_statically_known_to_increase() const { - return internal::tuple_coeff<std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value-1>::values_up_to_statically_known_to_increase(*this); + return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...> >::value-1>::values_up_to_statically_known_to_increase(*this); } }; template<typename FirstType, typename... OtherTypes> constexpr IndexList<FirstType, OtherTypes...> make_index_list(FirstType val1, OtherTypes... other_vals) { - return std::make_tuple(val1, other_vals...); + return IndexList<FirstType, OtherTypes...>(val1, other_vals...); } @@ -186,17 +280,17 @@ template<typename FirstType, typename... OtherTypes> size_t array_prod(const Ind } template<typename FirstType, typename... OtherTypes> struct array_size<IndexList<FirstType, OtherTypes...> > { - static const size_t value = std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value; + static const size_t value = array_size<IndexTuple<FirstType, OtherTypes...> >::value; }; template<typename FirstType, typename... OtherTypes> struct array_size<const IndexList<FirstType, OtherTypes...> > { - static const size_t value = std::tuple_size<std::tuple<FirstType, OtherTypes...> >::value; + static const size_t value = array_size<IndexTuple<FirstType, OtherTypes...> >::value; }; -template<DenseIndex n, typename FirstType, typename... OtherTypes> constexpr DenseIndex array_get(IndexList<FirstType, OtherTypes...>& a) { - return std::get<n>(a); +template<DenseIndex N, typename FirstType, typename... OtherTypes> constexpr DenseIndex array_get(IndexList<FirstType, OtherTypes...>& a) { + return IndexTupleExtractor<N, FirstType, OtherTypes...>::get_val(a); } -template<DenseIndex n, typename FirstType, typename... OtherTypes> constexpr DenseIndex array_get(const IndexList<FirstType, OtherTypes...>& a) { - return std::get<n>(a); +template<DenseIndex N, typename FirstType, typename... OtherTypes> constexpr DenseIndex array_get(const IndexList<FirstType, OtherTypes...>& a) { + return IndexTupleExtractor<N, FirstType, OtherTypes...>::get_val(a); } template <typename T> @@ -273,7 +367,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_eq<IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] == value); + (IndexList<FirstType, OtherTypes...>().get(i) == value); } }; @@ -281,7 +375,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_eq<const IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] == value); + (IndexList<FirstType, OtherTypes...>().get(i) == value); } }; @@ -296,7 +390,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_ne<IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] != value); + (IndexList<FirstType, OtherTypes...>().get(i) != value); } }; @@ -304,7 +398,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_ne<const IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] != value); + (IndexList<FirstType, OtherTypes...>().get(i) != value); } }; @@ -320,7 +414,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_gt<IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] > value); + (IndexList<FirstType, OtherTypes...>().get(i) > value); } }; @@ -328,7 +422,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_gt<const IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] > value); + (IndexList<FirstType, OtherTypes...>().get(i) > value); } }; @@ -343,7 +437,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_lt<IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] < value); + (IndexList<FirstType, OtherTypes...>().get(i) < value); } }; @@ -351,7 +445,7 @@ template <typename FirstType, typename... OtherTypes> struct index_statically_lt<const IndexList<FirstType, OtherTypes...> > { constexpr bool operator() (const DenseIndex i, const DenseIndex value) const { return IndexList<FirstType, OtherTypes...>().value_known_statically(i) & - (IndexList<FirstType, OtherTypes...>()[i] < value); + (IndexList<FirstType, OtherTypes...>().get(i) < value); } }; |