Eigen  5.0.1-dev+master
 
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
112#if EIGEN_COMP_CXXVER < 17
113template <int N>
114constexpr int FixedInt<N>::value;
115#endif
116
147template <int N>
148class VariableAndFixedInt {
149 public:
150 static const int value = N;
151 constexpr operator int() const { return m_value; }
152 constexpr VariableAndFixedInt(int val) : m_value(val) {}
153
154 protected:
155 int m_value;
156};
157
158template <typename T, int Default = Dynamic>
159struct get_fixed_value {
160 static const int value = Default;
161};
162
163template <int N, int Default>
164struct get_fixed_value<FixedInt<N>, Default> {
165 static const int value = N;
166};
167
168template <int N, int Default>
169struct get_fixed_value<VariableAndFixedInt<N>, Default> {
170 static const int value = N;
171};
172
173template <typename T, int N, int Default>
174struct get_fixed_value<variable_if_dynamic<T, N>, Default> {
175 static const int value = N;
176};
177
178template <typename T>
179EIGEN_DEVICE_FUNC constexpr Index get_runtime_value(const T &x) {
180 return x;
181}
182
183// Cleanup integer/FixedInt/VariableAndFixedInt/etc types:
184
185// By default, no cleanup:
186template <typename T, int DynamicKey = Dynamic, typename EnableIf = void>
187struct cleanup_index_type {
188 using type = T;
189};
190
191// Convert any integral type (e.g., short, int, unsigned int, etc.) to Eigen::Index
192template <typename T, int DynamicKey>
193struct cleanup_index_type<T, DynamicKey, std::enable_if_t<std::is_integral<T>::value>> {
194 using type = Index;
195};
196
197// If VariableAndFixedInt does not match DynamicKey, then we turn it to a pure compile-time value:
198template <int N, int DynamicKey>
199struct cleanup_index_type<VariableAndFixedInt<N>, DynamicKey> {
200 using type = FixedInt<N>;
201};
202// If VariableAndFixedInt matches DynamicKey, then we turn it to a pure runtime-value (aka Index):
203template <int DynamicKey>
204struct cleanup_index_type<VariableAndFixedInt<DynamicKey>, DynamicKey> {
205 using type = Index;
206};
207
208template <int N, int DynamicKey>
209struct cleanup_index_type<std::integral_constant<int, N>, DynamicKey> {
210 using type = FixedInt<N>;
211};
212
213} // end namespace internal
214
215#ifndef EIGEN_PARSED_BY_DOXYGEN
216
217template <int N>
218constexpr internal::FixedInt<N> fix{};
219
220#else // EIGEN_PARSED_BY_DOXYGEN
221
244template <int N>
245static const auto fix();
246
276template <int N>
277static const auto fix(int val);
278
279#endif // EIGEN_PARSED_BY_DOXYGEN
280
281} // end namespace Eigen
282
283#endif // EIGEN_INTEGRAL_CONSTANT_H
static const auto fix()