Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorIndexList.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_INDEX_LIST_H
12#define EIGEN_TENSOR_TENSOR_INDEX_LIST_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <Index n>
20struct type2index {
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);
26 }
27};
28
29// This can be used with IndexPairList to get compile-time constant pairs,
30// such as IndexPairList<type2indexpair<1,2>, type2indexpair<3,4>>().
31template <Index f, Index s>
32struct type2indexpair {
33 static constexpr Index first = f;
34 static constexpr Index second = s;
35
36 constexpr EIGEN_DEVICE_FUNC operator IndexPair<Index>() const { return IndexPair<Index>(f, s); }
37
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);
42 }
43};
44
45#if EIGEN_COMP_CXXVER < 17
46template <Index n>
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;
52#endif
53
54template <Index n>
55struct NumTraits<type2index<n>> {
56 using Real = Index;
57 enum { IsComplex = 0, RequireInitialization = false, ReadCost = 1, AddCost = 1, MulCost = 1 };
58
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; }
63};
64
65namespace internal {
66template <typename T>
67EIGEN_DEVICE_FUNC void update_value(T& val, Index new_val) {
68 val = internal::convert_index<T>(new_val);
69}
70template <Index n>
71EIGEN_DEVICE_FUNC void update_value(type2index<n>& val, Index new_val) {
72 val.set(new_val);
73}
74
75template <typename T>
76EIGEN_DEVICE_FUNC void update_value(T& val, IndexPair<Index> new_val) {
77 val = new_val;
78}
79template <Index f, Index s>
80EIGEN_DEVICE_FUNC void update_value(type2indexpair<f, s>& val, IndexPair<Index> new_val) {
81 val.set(new_val);
82}
83
84template <typename T>
85struct is_compile_time_constant_impl : std::false_type {};
86
87template <Index f, Index s>
88struct is_compile_time_constant_impl<type2indexpair<f, s>> : std::true_type {};
89
90template <Index idx>
91struct is_compile_time_constant_impl<type2index<idx>> : std::true_type {};
92
93template <typename T>
94using is_compile_time_constant = is_compile_time_constant_impl<std::remove_cv_t<std::remove_reference_t<T>>>;
95
96template <typename... T>
97struct IndexTuple;
98
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...) {}
103
104 static constexpr int count = 1 + sizeof...(O);
105 T head{};
106 IndexTuple<O...> others{};
107 using Head = T;
108 using Other = IndexTuple<O...>;
109};
110
111template <typename T>
112struct IndexTuple<T> {
113 EIGEN_DEVICE_FUNC constexpr IndexTuple() = default;
114 EIGEN_DEVICE_FUNC constexpr IndexTuple(const T& v) : head(v) {}
115
116 constexpr static int count = 1;
117 T head{};
118 using Head = T;
119};
120
121template <int N, typename... T>
122struct IndexTupleExtractor;
123
124template <int N, typename T, typename... O>
125struct IndexTupleExtractor<N, T, O...> {
126 using ValType = typename IndexTupleExtractor<N - 1, O...>::ValType;
127
128 EIGEN_DEVICE_FUNC static constexpr ValType& get_val(IndexTuple<T, O...>& val) {
129 return IndexTupleExtractor<N - 1, O...>::get_val(val.others);
130 }
131
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);
134 }
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);
138 }
139};
140
141template <typename T, typename... O>
142struct IndexTupleExtractor<0, T, O...> {
143 using ValType = T;
144
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) {
149 val.head = new_val;
150 }
151};
152
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);
156}
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);
161}
162template <typename T, typename... O>
163struct array_size<IndexTuple<T, O...>> {
164 static constexpr size_t value = IndexTuple<T, O...>::count;
165};
166template <typename T, typename... O>
167struct array_size<const IndexTuple<T, O...>> {
168 static constexpr size_t value = IndexTuple<T, O...>::count;
169};
170
171template <Index Idx, typename ValueT>
172struct tuple_coeff {
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);
176 }
177 template <typename... T>
178 EIGEN_DEVICE_FUNC static void set(const Index i, IndexTuple<T...>& t, const ValueT& value) {
179 if (i == Idx) {
180 update_value(array_get<Idx>(t), value);
181 } else {
182 tuple_coeff<Idx - 1, ValueT>::set(i, t, value);
183 }
184 }
185
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);
190 }
191
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);
196 }
197
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);
204 }
205};
206
207template <typename ValueT>
208struct tuple_coeff<0, ValueT> {
209 template <typename... T>
210 EIGEN_DEVICE_FUNC static constexpr ValueT get(const Index /*i*/, const IndexTuple<T...>& t) {
211 return array_get<0>(t);
212 }
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);
218 }
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);
222 }
223
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;
227 }
228
229 template <typename... T>
230 EIGEN_DEVICE_FUNC static constexpr bool values_up_to_statically_known_to_increase(const IndexTuple<T...>&) {
231 return true;
232 }
233};
234} // namespace internal
235
251
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);
257 }
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);
261 }
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);
265 }
266
267 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr std::size_t size() const { return 1 + sizeof...(OtherTypes); }
268
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;
274
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);
278 }
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);
282 }
283
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);
287 }
288};
289
290template <typename FirstType, typename... OtherTypes>
291std::ostream& operator<<(std::ostream& os, const IndexList<FirstType, OtherTypes...>& dims) {
292 os << "[";
293 for (size_t i = 0; i < 1 + sizeof...(OtherTypes); ++i) {
294 if (i > 0) os << ", ";
295 os << dims[i];
296 }
297 os << "]";
298 return os;
299}
300
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...);
304}
305
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);
311 }
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);
315 }
316
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;
320
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);
324 }
325};
326
327namespace internal {
328
329template <typename FirstType, typename... OtherTypes>
330EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index array_prod(const IndexList<FirstType, OtherTypes...>& sizes) {
331 Index result = 1;
332 EIGEN_UNROLL_LOOP
333 for (size_t i = 0; i < array_size<IndexList<FirstType, OtherTypes...>>::value; ++i) {
334 result *= sizes[i];
335 }
336 return result;
337}
338
339template <typename FirstType, typename... OtherTypes>
340struct array_size<IndexList<FirstType, OtherTypes...>> {
341 static constexpr size_t value = array_size<IndexTuple<FirstType, OtherTypes...>>::value;
342};
343template <typename FirstType, typename... OtherTypes>
344struct array_size<const IndexList<FirstType, OtherTypes...>> {
345 static constexpr size_t value = array_size<IndexTuple<FirstType, OtherTypes...>>::value;
346};
347
348template <typename FirstType, typename... OtherTypes>
349struct array_size<IndexPairList<FirstType, OtherTypes...>> {
350 static constexpr size_t value = 1 + sizeof...(OtherTypes);
351};
352template <typename FirstType, typename... OtherTypes>
353struct array_size<const IndexPairList<FirstType, OtherTypes...>> {
354 static constexpr size_t value = 1 + sizeof...(OtherTypes);
355};
356
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);
360}
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);
364}
365
366template <typename T>
367struct index_known_statically_impl {
368 EIGEN_DEVICE_FUNC static constexpr bool run(const Index) { return false; }
369};
370
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);
375 }
376};
377
378template <typename T>
379struct all_indices_known_statically_impl {
380 static constexpr bool run() { return false; }
381};
382
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();
387 }
388};
389
390template <typename T>
391struct indices_statically_known_to_increase_impl {
392 EIGEN_DEVICE_FUNC static constexpr bool run() { return false; }
393};
394
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();
399 }
400};
401
402template <typename Tx>
403struct index_statically_eq_impl {
404 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
405};
406
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);
412 }
413};
414
415template <typename T>
416struct index_statically_ne_impl {
417 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
418};
419
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);
425 }
426};
427
428template <typename T>
429struct index_statically_gt_impl {
430 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
431};
432
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);
438 }
439};
440
441template <typename T>
442struct index_statically_lt_impl {
443 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
444};
445
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);
451 }
452};
453
454template <typename Tx>
455struct index_pair_first_statically_eq_impl {
456 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
457};
458
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);
464 }
465};
466
467template <typename Tx>
468struct index_pair_second_statically_eq_impl {
469 EIGEN_DEVICE_FUNC static constexpr bool run(Index, Index) { return false; }
470};
471
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);
477 }
478};
479
480} // end namespace internal
481} // end namespace Eigen
482
483namespace Eigen {
484namespace internal {
485template <typename T>
486static EIGEN_DEVICE_FUNC constexpr bool index_known_statically(Index i) {
487 return index_known_statically_impl<std::remove_cv_t<T>>::run(i);
488}
489
490template <typename T>
491static EIGEN_DEVICE_FUNC constexpr bool all_indices_known_statically() {
492 return all_indices_known_statically_impl<std::remove_cv_t<T>>::run();
493}
494
495template <typename T>
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();
498}
499
500template <typename T>
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);
503}
504
505template <typename T>
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);
508}
509
510template <typename T>
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);
513}
514
515template <typename T>
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);
518}
519
520template <typename T>
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);
523}
524
525template <typename T>
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);
528}
529
530} // end namespace internal
531} // end namespace Eigen
532
533#endif // EIGEN_TENSOR_TENSOR_INDEX_LIST_H
Namespace containing all symbols from the Eigen library.