IntegralConstant.h
Go to the documentation of this file.
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>
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>
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>
94  return FixedInt<N % M>();
95  }
96 
97  template <int M>
99  return FixedInt<N | M>();
100  }
101 
102  template <int M>
104  return FixedInt<N & M>();
105  }
106 
107  // Needed in C++14 to allow fix<N>():
108  constexpr FixedInt operator()() const { return *this; }
109 
111 };
112 
143 template <int N>
145  public:
146  static const int value = N;
147  operator int() const { return m_value; }
149 
150  protected:
151  int m_value;
152 };
153 
154 template <typename T, int Default = Dynamic>
156  static const int value = Default;
157 };
158 
159 template <int N, int Default>
161  static const int value = N;
162 };
163 
164 template <int N, int Default>
166  static const int value = N;
167 };
168 
169 template <typename T, int N, int Default>
171  static const int value = N;
172 };
173 
174 template <typename T>
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>
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>
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>
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
#define eigen_internal_assert(x)
Definition: Macros.h:916
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Definition: IntegralConstant.h:23
constexpr FixedInt<-N > operator-() const
Definition: IntegralConstant.h:70
constexpr FixedInt< N/M > operator/(FixedInt< M >) const
Definition: IntegralConstant.h:88
constexpr VariableAndFixedInt< N > operator()(int val) const
Definition: IntegralConstant.h:110
constexpr FixedInt< N - M > operator-(FixedInt< M >) const
Definition: IntegralConstant.h:78
static constexpr int value
Definition: IntegralConstant.h:57
constexpr FixedInt(VariableAndFixedInt< N > other)
Definition: IntegralConstant.h:63
constexpr FixedInt< N &M > operator&(FixedInt< M >) const
Definition: IntegralConstant.h:103
constexpr FixedInt< N|M > operator|(FixedInt< M >) const
Definition: IntegralConstant.h:98
constexpr FixedInt< N+M > operator+(FixedInt< M >) const
Definition: IntegralConstant.h:73
constexpr FixedInt< N *M > operator*(FixedInt< M >) const
Definition: IntegralConstant.h:83
constexpr FixedInt()=default
constexpr FixedInt(std::integral_constant< int, N >)
Definition: IntegralConstant.h:61
constexpr FixedInt operator()() const
Definition: IntegralConstant.h:108
constexpr FixedInt< N % M > operator%(FixedInt< M >) const
Definition: IntegralConstant.h:93
Definition: IntegralConstant.h:144
static const int value
Definition: IntegralConstant.h:146
int m_value
Definition: IntegralConstant.h:151
VariableAndFixedInt(int val)
Definition: IntegralConstant.h:148
Definition: XprHelper.h:154
@ N
Definition: constructor.cpp:22
return int(ret)+1
EIGEN_DEVICE_FUNC Index get_runtime_value(const T &x)
Definition: IntegralConstant.h:175
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
@ Default
Definition: Constants.h:361
constexpr internal::FixedInt< N > fix
Definition: IntegralConstant.h:214
val
Definition: calibrate.py:119
Definition: Eigen_Colamd.h:49
list x
Definition: plotDoE.py:28
Definition: IntegralConstant.h:183
T type
Definition: IntegralConstant.h:184
Definition: IntegralConstant.h:155
static const int value
Definition: IntegralConstant.h:156