Eigen  5.0.1
 
Loading...
Searching...
No Matches
IntegralConstant.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
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_INTEGRAL_CONSTANT_H
12#define EIGEN_INTEGRAL_CONSTANT_H
13
14// IWYU pragma: private
15#include "../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <int N>
22class FixedInt;
23template <int N>
24class VariableAndFixedInt;
25
55template <int N>
56class FixedInt {
57 public:
58 static constexpr int value = N;
59 constexpr operator int() const { return N; }
60
61 constexpr FixedInt() = default;
62 constexpr FixedInt(std::integral_constant<int, N>) {}
63
64 constexpr FixedInt(VariableAndFixedInt<N> other) {
65 EIGEN_UNUSED_VARIABLE(other);
66 eigen_internal_assert(int(other) == N);
67 }
68
69 constexpr FixedInt<-N> operator-() const { return FixedInt<-N>(); }
70
71 template <int M>
72 constexpr FixedInt<N + M> operator+(FixedInt<M>) const {
73 return FixedInt<N + M>();
74 }
75
76 template <int M>
77 constexpr FixedInt<N - M> operator-(FixedInt<M>) const {
78 return FixedInt<N - M>();
79 }
80
81 template <int M>
82 constexpr FixedInt<N * M> operator*(FixedInt<M>) const {
83 return FixedInt<N * M>();
84 }
85
86 template <int M>
87 constexpr FixedInt<N / M> operator/(FixedInt<M>) const {
88 return FixedInt<N / M>();
89 }
90
91 template <int M>
92 constexpr FixedInt<N % M> operator%(FixedInt<M>) const {
93 return FixedInt<N % M>();
94 }
95
96 template <int M>
97 constexpr FixedInt<N | M> operator|(FixedInt<M>) const {
98 return FixedInt<N | M>();
99 }
100
101 template <int M>
102 constexpr FixedInt<N & M> operator&(FixedInt<M>) const {
103 return FixedInt<N & M>();
104 }
105
106 // Needed in C++14 to allow fix<N>():
107 constexpr FixedInt operator()() const { return *this; }
108
109 constexpr VariableAndFixedInt<N> operator()(int val) const { return VariableAndFixedInt<N>(val); }
110};
111
142template <int N>
143class VariableAndFixedInt {
144 public:
145 static const int value = N;
146 constexpr operator int() const { return m_value; }
147 constexpr VariableAndFixedInt(int val) : m_value(val) {}
148
149 protected:
150 int m_value;
151};
152
153template <typename T, int Default = Dynamic>
154struct get_fixed_value {
155 static const int value = Default;
156};
157
158template <int N, int Default>
159struct get_fixed_value<FixedInt<N>, Default> {
160 static const int value = N;
161};
162
163template <int N, int Default>
164struct get_fixed_value<VariableAndFixedInt<N>, Default> {
165 static const int value = N;
166};
167
168template <typename T, int N, int Default>
169struct get_fixed_value<variable_if_dynamic<T, N>, Default> {
170 static const int value = N;
171};
172
173template <typename T>
174EIGEN_DEVICE_FUNC constexpr Index get_runtime_value(const T &x) {
175 return x;
176}
177
178// Cleanup integer/FixedInt/VariableAndFixedInt/etc types:
179
180// By default, no cleanup:
181template <typename T, int DynamicKey = Dynamic, typename EnableIf = void>
182struct cleanup_index_type {
183 using type = T;
184};
185
186// Convert any integral type (e.g., short, int, unsigned int, etc.) to Eigen::Index
187template <typename T, int DynamicKey>
188struct cleanup_index_type<T, DynamicKey, std::enable_if_t<std::is_integral<T>::value>> {
189 using type = Index;
190};
191
192// If VariableAndFixedInt does not match DynamicKey, then we turn it to a pure compile-time value:
193template <int N, int DynamicKey>
194struct cleanup_index_type<VariableAndFixedInt<N>, DynamicKey> {
195 using type = FixedInt<N>;
196};
197// If VariableAndFixedInt matches DynamicKey, then we turn it to a pure runtime-value (aka Index):
198template <int DynamicKey>
199struct cleanup_index_type<VariableAndFixedInt<DynamicKey>, DynamicKey> {
200 using type = Index;
201};
202
203template <int N, int DynamicKey>
204struct cleanup_index_type<std::integral_constant<int, N>, DynamicKey> {
205 using type = FixedInt<N>;
206};
207
208} // end namespace internal
209
210#ifndef EIGEN_PARSED_BY_DOXYGEN
211
212template <int N>
213constexpr internal::FixedInt<N> fix{};
214
215#else // EIGEN_PARSED_BY_DOXYGEN
216
239template <int N>
240static const auto fix();
241
271template <int N>
272static const auto fix(int val);
273
274#endif // EIGEN_PARSED_BY_DOXYGEN
275
276} // end namespace Eigen
277
278#endif // EIGEN_INTEGRAL_CONSTANT_H
static const auto fix()