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