11#ifndef EIGEN_TENSOR_TENSOR_INDEX_LIST_H
12#define EIGEN_TENSOR_TENSOR_INDEX_LIST_H
15#include "./InternalHeaderCheck.h"
21 static constexpr Index value = n;
22 EIGEN_DEVICE_FUNC
constexpr operator Index()
const {
return n; }
23 EIGEN_DEVICE_FUNC
void set(Index val) {
24 EIGEN_ONLY_USED_FOR_DEBUG(val);
25 eigen_assert(val == n);
31template <Index f, Index s>
32struct type2indexpair {
33 static constexpr Index first = f;
34 static constexpr Index second = s;
36 constexpr EIGEN_DEVICE_FUNC
operator IndexPair<Index>()
const {
return IndexPair<Index>(f, s); }
38 EIGEN_DEVICE_FUNC
void set(
const IndexPair<Index>& val) {
39 EIGEN_ONLY_USED_FOR_DEBUG(val);
40 eigen_assert(val.first == f);
41 eigen_assert(val.second == s);
45#if EIGEN_COMP_CXXVER < 17
47constexpr Index type2index<n>::value;
48template <Index f, Index s>
49constexpr Index type2indexpair<f, s>::first;
50template <Index f, Index s>
51constexpr Index type2indexpair<f, s>::second;
57 enum { IsComplex = 0, RequireInitialization =
false, ReadCost = 1, AddCost = 1, MulCost = 1 };
59 EIGEN_DEVICE_FUNC
static constexpr EIGEN_STRONG_INLINE Real epsilon() {
return 0; }
60 EIGEN_DEVICE_FUNC
static constexpr EIGEN_STRONG_INLINE Real dummy_precision() {
return 0; }
61 EIGEN_DEVICE_FUNC
static constexpr EIGEN_STRONG_INLINE Real highest() {
return n; }
62 EIGEN_DEVICE_FUNC
static constexpr EIGEN_STRONG_INLINE Real lowest() {
return n; }
67EIGEN_DEVICE_FUNC
void update_value(T& val, Index new_val) {
68 val = internal::convert_index<T>(new_val);
71EIGEN_DEVICE_FUNC
void update_value(type2index<n>& val, Index new_val) {
76EIGEN_DEVICE_FUNC
void update_value(T& val, IndexPair<Index> new_val) {
79template <Index f, Index s>
80EIGEN_DEVICE_FUNC
void update_value(type2indexpair<f, s>& val, IndexPair<Index> new_val) {
85struct is_compile_time_constant_impl : std::false_type {};
87template <Index f, Index s>
88struct is_compile_time_constant_impl<type2indexpair<f, s>> : std::true_type {};
91struct is_compile_time_constant_impl<type2index<idx>> : std::true_type {};
94using is_compile_time_constant = is_compile_time_constant_impl<std::remove_cv_t<std::remove_reference_t<T>>>;
96template <
typename... T>
99template <
typename T,
typename... O>
100struct IndexTuple<T, O...> {
101 EIGEN_DEVICE_FUNC
constexpr IndexTuple() =
default;
102 EIGEN_DEVICE_FUNC
constexpr IndexTuple(
const T& v,
const O... o) : head(v), others(o...) {}
104 static constexpr int count = 1 +
sizeof...(O);
106 IndexTuple<O...> others{};
108 using Other = IndexTuple<O...>;
112struct IndexTuple<T> {
113 EIGEN_DEVICE_FUNC
constexpr IndexTuple() =
default;
114 EIGEN_DEVICE_FUNC
constexpr IndexTuple(
const T& v) : head(v) {}
116 constexpr static int count = 1;
121template <
int N,
typename... T>
122struct IndexTupleExtractor;
124template <
int N,
typename T,
typename... O>
125struct IndexTupleExtractor<N, T, O...> {
126 using ValType =
typename IndexTupleExtractor<N - 1, O...>::ValType;
128 EIGEN_DEVICE_FUNC
static constexpr ValType& get_val(IndexTuple<T, O...>& val) {
129 return IndexTupleExtractor<N - 1, O...>::get_val(val.others);
132 EIGEN_DEVICE_FUNC
static constexpr const ValType& get_val(
const IndexTuple<T, O...>& val) {
133 return IndexTupleExtractor<N - 1, O...>::get_val(val.others);
135 template <
typename V>
136 EIGEN_DEVICE_FUNC
static void set_val(IndexTuple<T, O...>& val, V& new_val) {
137 IndexTupleExtractor<N - 1, O...>::set_val(val.others, new_val);
141template <
typename T,
typename... O>
142struct IndexTupleExtractor<0, T, O...> {
145 EIGEN_DEVICE_FUNC
static constexpr ValType& get_val(IndexTuple<T, O...>& val) {
return val.head; }
146 EIGEN_DEVICE_FUNC
static constexpr const ValType& get_val(
const IndexTuple<T, O...>& val) {
return val.head; }
147 template <
typename V>
148 EIGEN_DEVICE_FUNC
static void set_val(IndexTuple<T, O...>& val, V& new_val) {
153template <
int N,
typename T,
typename... O>
154EIGEN_DEVICE_FUNC
constexpr typename IndexTupleExtractor<N, T, O...>::ValType& array_get(IndexTuple<T, O...>& tuple) {
155 return IndexTupleExtractor<N, T, O...>::get_val(tuple);
157template <
int N,
typename T,
typename... O>
158EIGEN_DEVICE_FUNC
constexpr const typename IndexTupleExtractor<N, T, O...>::ValType& array_get(
159 const IndexTuple<T, O...>& tuple) {
160 return IndexTupleExtractor<N, T, O...>::get_val(tuple);
162template <
typename T,
typename... O>
163struct array_size<IndexTuple<T, O...>> {
164 static constexpr size_t value = IndexTuple<T, O...>::count;
166template <
typename T,
typename... O>
167struct array_size<const IndexTuple<T, O...>> {
168 static constexpr size_t value = IndexTuple<T, O...>::count;
171template <Index Idx,
typename ValueT>
173 template <
typename... T>
174 EIGEN_DEVICE_FUNC
static constexpr ValueT get(
const Index i,
const IndexTuple<T...>& t) {
175 return i == Idx ? array_get<Idx>(t) : tuple_coeff<Idx - 1, ValueT>::get(i, t);
177 template <
typename... T>
178 EIGEN_DEVICE_FUNC
static void set(
const Index i, IndexTuple<T...>& t,
const ValueT& value) {
180 update_value(array_get<Idx>(t), value);
182 tuple_coeff<Idx - 1, ValueT>::set(i, t, value);
186 template <
typename... T>
187 EIGEN_DEVICE_FUNC
static constexpr bool value_known_statically(
const Index i,
const IndexTuple<T...>& t) {
188 return ((i == Idx) && is_compile_time_constant<
typename IndexTupleExtractor<Idx, T...>::ValType>::value) ||
189 tuple_coeff<Idx - 1, ValueT>::value_known_statically(i, t);
192 template <
typename... T>
193 EIGEN_DEVICE_FUNC
static constexpr bool values_up_to_known_statically(
const IndexTuple<T...>& t) {
194 return is_compile_time_constant<
typename IndexTupleExtractor<Idx, T...>::ValType>::value &&
195 tuple_coeff<Idx - 1, ValueT>::values_up_to_known_statically(t);
198 template <
typename... T>
199 EIGEN_DEVICE_FUNC
static constexpr bool values_up_to_statically_known_to_increase(
const IndexTuple<T...>& t) {
200 return is_compile_time_constant<
typename IndexTupleExtractor<Idx, T...>::ValType>::value &&
201 is_compile_time_constant<
typename IndexTupleExtractor<Idx - 1, T...>::ValType>::value &&
202 array_get<Idx>(t) > array_get<Idx - 1>(t) &&
203 tuple_coeff<Idx - 1, ValueT>::values_up_to_statically_known_to_increase(t);
207template <
typename ValueT>
208struct tuple_coeff<0, ValueT> {
209 template <
typename... T>
210 EIGEN_DEVICE_FUNC
static constexpr ValueT get(
const Index ,
const IndexTuple<T...>& t) {
211 return array_get<0>(t);
213 template <
typename... T>
214 EIGEN_DEVICE_FUNC
static void set(
const Index i, IndexTuple<T...>& t,
const ValueT value) {
215 EIGEN_ONLY_USED_FOR_DEBUG(i);
216 eigen_assert(i == 0);
217 update_value(array_get<0>(t), value);
219 template <
typename... T>
220 EIGEN_DEVICE_FUNC
static constexpr bool value_known_statically(
const Index i,
const IndexTuple<T...>&) {
221 return is_compile_time_constant<
typename IndexTupleExtractor<0, T...>::ValType>::value && (i == 0);
224 template <
typename... T>
225 EIGEN_DEVICE_FUNC
static constexpr bool values_up_to_known_statically(
const IndexTuple<T...>&) {
226 return is_compile_time_constant<
typename IndexTupleExtractor<0, T...>::ValType>::value;
229 template <
typename... T>
230 EIGEN_DEVICE_FUNC
static constexpr bool values_up_to_statically_known_to_increase(
const IndexTuple<T...>&) {
252template <
typename FirstType,
typename... OtherTypes>
253struct IndexList : internal::IndexTuple<FirstType, OtherTypes...> {
254 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr Index operator[](
const Index i)
const {
255 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
256 Index>::get(i, *
this);
258 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr Index get(
const Index i)
const {
259 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
260 Index>::get(i, *
this);
262 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
void set(
const Index i,
const Index value) {
263 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
264 Index>::set(i, *
this, value);
267 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr std::size_t size()
const {
return 1 +
sizeof...(OtherTypes); }
269 EIGEN_DEVICE_FUNC
constexpr IndexList(
const internal::IndexTuple<FirstType, OtherTypes...>& other)
270 : internal::IndexTuple<FirstType, OtherTypes...>(other) {}
271 EIGEN_DEVICE_FUNC
constexpr IndexList(FirstType& first, OtherTypes... other)
272 : internal::IndexTuple<FirstType, OtherTypes...>(first, other...) {}
273 EIGEN_DEVICE_FUNC
constexpr IndexList() =
default;
275 EIGEN_DEVICE_FUNC
constexpr bool value_known_statically(
const Index i)
const {
276 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
277 Index>::value_known_statically(i, *
this);
279 EIGEN_DEVICE_FUNC
constexpr bool all_values_known_statically()
const {
280 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
281 Index>::values_up_to_known_statically(*
this);
284 EIGEN_DEVICE_FUNC
constexpr bool values_statically_known_to_increase()
const {
285 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
286 Index>::values_up_to_statically_known_to_increase(*
this);
290template <
typename FirstType,
typename... OtherTypes>
291std::ostream& operator<<(std::ostream& os,
const IndexList<FirstType, OtherTypes...>& dims) {
293 for (
size_t i = 0; i < 1 +
sizeof...(OtherTypes); ++i) {
294 if (i > 0) os <<
", ";
301template <
typename FirstType,
typename... OtherTypes>
302constexpr IndexList<FirstType, OtherTypes...> make_index_list(FirstType val1, OtherTypes... other_vals) {
303 return IndexList<FirstType, OtherTypes...>(val1, other_vals...);
306template <
typename FirstType,
typename... OtherTypes>
307struct IndexPairList : internal::IndexTuple<FirstType, OtherTypes...> {
308 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr IndexPair<Index> operator[](
const Index i)
const {
309 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
310 IndexPair<Index>>::get(i, *
this);
312 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
void set(
const Index i,
const IndexPair<Index> value) {
313 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
314 IndexPair<Index>>::set(i, *
this, value);
317 EIGEN_DEVICE_FUNC
constexpr IndexPairList(
const internal::IndexTuple<FirstType, OtherTypes...>& other)
318 : internal::IndexTuple<FirstType, OtherTypes...>(other) {}
319 EIGEN_DEVICE_FUNC
constexpr IndexPairList() =
default;
321 EIGEN_DEVICE_FUNC
constexpr bool value_known_statically(
const Index i)
const {
322 return internal::tuple_coeff<internal::array_size<internal::IndexTuple<FirstType, OtherTypes...>>::value - 1,
323 Index>::value_known_statically(i, *
this);
329template <
typename FirstType,
typename... OtherTypes>
330EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index array_prod(
const IndexList<FirstType, OtherTypes...>& sizes) {
333 for (
size_t i = 0; i < array_size<IndexList<FirstType, OtherTypes...>>::value; ++i) {
339template <
typename FirstType,
typename... OtherTypes>
340struct array_size<IndexList<FirstType, OtherTypes...>> {
341 static constexpr size_t value = array_size<IndexTuple<FirstType, OtherTypes...>>::value;
343template <
typename FirstType,
typename... OtherTypes>
344struct array_size<const IndexList<FirstType, OtherTypes...>> {
345 static constexpr size_t value = array_size<IndexTuple<FirstType, OtherTypes...>>::value;
348template <
typename FirstType,
typename... OtherTypes>
349struct array_size<IndexPairList<FirstType, OtherTypes...>> {
350 static constexpr size_t value = 1 +
sizeof...(OtherTypes);
352template <
typename FirstType,
typename... OtherTypes>
353struct array_size<const IndexPairList<FirstType, OtherTypes...>> {
354 static constexpr size_t value = 1 +
sizeof...(OtherTypes);
357template <Index N,
typename FirstType,
typename... OtherTypes>
358EIGEN_DEVICE_FUNC
constexpr Index array_get(IndexList<FirstType, OtherTypes...>& a) {
359 return IndexTupleExtractor<N, FirstType, OtherTypes...>::get_val(a);
361template <Index N,
typename FirstType,
typename... OtherTypes>
362EIGEN_DEVICE_FUNC
constexpr Index array_get(
const IndexList<FirstType, OtherTypes...>& a) {
363 return IndexTupleExtractor<N, FirstType, OtherTypes...>::get_val(a);
367struct index_known_statically_impl {
368 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index) {
return false; }
371template <
typename FirstType,
typename... OtherTypes>
372struct index_known_statically_impl<IndexList<FirstType, OtherTypes...>> {
373 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i) {
374 return IndexList<FirstType, OtherTypes...>().value_known_statically(i);
379struct all_indices_known_statically_impl {
380 static constexpr bool run() {
return false; }
383template <
typename FirstType,
typename... OtherTypes>
384struct all_indices_known_statically_impl<IndexList<FirstType, OtherTypes...>> {
385 EIGEN_DEVICE_FUNC
static constexpr bool run() {
386 return IndexList<FirstType, OtherTypes...>().all_values_known_statically();
391struct indices_statically_known_to_increase_impl {
392 EIGEN_DEVICE_FUNC
static constexpr bool run() {
return false; }
395template <
typename FirstType,
typename... OtherTypes>
396struct indices_statically_known_to_increase_impl<IndexList<FirstType, OtherTypes...>> {
397 EIGEN_DEVICE_FUNC
static constexpr bool run() {
398 return Eigen::IndexList<FirstType, OtherTypes...>().values_statically_known_to_increase();
402template <
typename Tx>
403struct index_statically_eq_impl {
404 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
407template <
typename FirstType,
typename... OtherTypes>
408struct index_statically_eq_impl<IndexList<FirstType, OtherTypes...>> {
409 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
410 return IndexList<FirstType, OtherTypes...>().value_known_statically(i) &&
411 (IndexList<FirstType, OtherTypes...>().get(i) == value);
416struct index_statically_ne_impl {
417 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
420template <
typename FirstType,
typename... OtherTypes>
421struct index_statically_ne_impl<IndexList<FirstType, OtherTypes...>> {
422 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
423 return IndexList<FirstType, OtherTypes...>().value_known_statically(i) &&
424 (IndexList<FirstType, OtherTypes...>().get(i) != value);
429struct index_statically_gt_impl {
430 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
433template <
typename FirstType,
typename... OtherTypes>
434struct index_statically_gt_impl<IndexList<FirstType, OtherTypes...>> {
435 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
436 return IndexList<FirstType, OtherTypes...>().value_known_statically(i) &&
437 (IndexList<FirstType, OtherTypes...>().get(i) > value);
442struct index_statically_lt_impl {
443 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
446template <
typename FirstType,
typename... OtherTypes>
447struct index_statically_lt_impl<IndexList<FirstType, OtherTypes...>> {
448 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
449 return IndexList<FirstType, OtherTypes...>().value_known_statically(i) &&
450 (IndexList<FirstType, OtherTypes...>().get(i) < value);
454template <
typename Tx>
455struct index_pair_first_statically_eq_impl {
456 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
459template <
typename FirstType,
typename... OtherTypes>
460struct index_pair_first_statically_eq_impl<IndexPairList<FirstType, OtherTypes...>> {
461 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
462 return IndexPairList<FirstType, OtherTypes...>().value_known_statically(i) &&
463 (IndexPairList<FirstType, OtherTypes...>().operator[](i).first == value);
467template <
typename Tx>
468struct index_pair_second_statically_eq_impl {
469 EIGEN_DEVICE_FUNC
static constexpr bool run(Index, Index) {
return false; }
472template <
typename FirstType,
typename... OtherTypes>
473struct index_pair_second_statically_eq_impl<IndexPairList<FirstType, OtherTypes...>> {
474 EIGEN_DEVICE_FUNC
static constexpr bool run(
const Index i,
const Index value) {
475 return IndexPairList<FirstType, OtherTypes...>().value_known_statically(i) &&
476 (IndexPairList<FirstType, OtherTypes...>().operator[](i).second == value);
486static EIGEN_DEVICE_FUNC
constexpr bool index_known_statically(Index i) {
487 return index_known_statically_impl<std::remove_cv_t<T>>::run(i);
491static EIGEN_DEVICE_FUNC
constexpr bool all_indices_known_statically() {
492 return all_indices_known_statically_impl<std::remove_cv_t<T>>::run();
496static EIGEN_DEVICE_FUNC
constexpr bool indices_statically_known_to_increase() {
497 return indices_statically_known_to_increase_impl<std::remove_cv_t<T>>::run();
501static EIGEN_DEVICE_FUNC
constexpr bool index_statically_eq(Index i, Index value) {
502 return index_statically_eq_impl<std::remove_cv_t<T>>::run(i, value);
506static EIGEN_DEVICE_FUNC
constexpr bool index_statically_ne(Index i, Index value) {
507 return index_statically_ne_impl<std::remove_cv_t<T>>::run(i, value);
511static EIGEN_DEVICE_FUNC
constexpr bool index_statically_gt(Index i, Index value) {
512 return index_statically_gt_impl<std::remove_cv_t<T>>::run(i, value);
516static EIGEN_DEVICE_FUNC
constexpr bool index_statically_lt(Index i, Index value) {
517 return index_statically_lt_impl<std::remove_cv_t<T>>::run(i, value);
521static EIGEN_DEVICE_FUNC
constexpr bool index_pair_first_statically_eq(Index i, Index value) {
522 return index_pair_first_statically_eq_impl<std::remove_cv_t<T>>::run(i, value);
526static EIGEN_DEVICE_FUNC
constexpr bool index_pair_second_statically_eq(Index i, Index value) {
527 return index_pair_second_statically_eq_impl<std::remove_cv_t<T>>::run(i, value);
Namespace containing all symbols from the Eigen library.