$darkmode
Eigen  5.0.1-dev
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 
10 #ifndef EIGEN_INTEGRAL_CONSTANT_H
11 #define EIGEN_INTEGRAL_CONSTANT_H
12 
13 // IWYU pragma: private
14 #include "../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <int N>
21 class FixedInt;
22 template <int N>
23 class VariableAndFixedInt;
24 
54 template <int N>
55 class 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 
143 template <int N>
144 class 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 
154 template <typename T, int Default = Dynamic>
155 struct get_fixed_value {
156  static const int value = Default;
157 };
158 
159 template <int N, int Default>
160 struct get_fixed_value<FixedInt<N>, Default> {
161  static const int value = N;
162 };
163 
164 template <int N, int Default>
165 struct get_fixed_value<VariableAndFixedInt<N>, Default> {
166  static const int value = N;
167 };
168 
169 template <typename T, int N, int Default>
170 struct get_fixed_value<variable_if_dynamic<T, N>, Default> {
171  static const int value = N;
172 };
173 
174 template <typename T>
175 EIGEN_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:
182 template <typename T, int DynamicKey = Dynamic, typename EnableIf = void>
183 struct cleanup_index_type {
184  typedef T type;
185 };
186 
187 // Convert any integral type (e.g., short, int, unsigned int, etc.) to Eigen::Index
188 template <typename T, int DynamicKey>
189 struct 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:
194 template <int N, int DynamicKey>
195 struct 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):
199 template <int DynamicKey>
200 struct cleanup_index_type<VariableAndFixedInt<DynamicKey>, DynamicKey> {
201  typedef Index type;
202 };
203 
204 template <int N, int DynamicKey>
205 struct 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 
213 template <int N>
214 constexpr internal::FixedInt<N> fix{};
215 
216 #else // EIGEN_PARSED_BY_DOXYGEN
217 
240 template <int N>
241 static const auto fix();
242 
272 template <int N>
273 static const auto fix(int val);
274 
275 #endif // EIGEN_PARSED_BY_DOXYGEN
276 
277 } // end namespace Eigen
278 
279 #endif // EIGEN_INTEGRAL_CONSTANT_H
const Product< MatrixDerived, PermutationDerived, DefaultProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:474
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Definition: BFloat16.h:231
static const auto fix()
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82