Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.0
 
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
10
11#ifndef EIGEN_INTEGRAL_CONSTANT_H
12#define EIGEN_INTEGRAL_CONSTANT_H
13
14namespace Eigen {
15
16namespace internal {
17
18template<int N> class FixedInt;
19template<int N> class VariableAndFixedInt;
20
51template<int N> class FixedInt
52{
53public:
54 static const int value = N;
55 EIGEN_CONSTEXPR operator int() const { return value; }
56 FixedInt() {}
57 FixedInt( VariableAndFixedInt<N> other) {
58 #ifndef EIGEN_INTERNAL_DEBUGGING
59 EIGEN_UNUSED_VARIABLE(other);
60 #endif
61 eigen_internal_assert(int(other)==N);
62 }
63
64 FixedInt<-N> operator-() const { return FixedInt<-N>(); }
65 template<int M>
66 FixedInt<N+M> operator+( FixedInt<M>) const { return FixedInt<N+M>(); }
67 template<int M>
68 FixedInt<N-M> operator-( FixedInt<M>) const { return FixedInt<N-M>(); }
69 template<int M>
70 FixedInt<N*M> operator*( FixedInt<M>) const { return FixedInt<N*M>(); }
71 template<int M>
72 FixedInt<N/M> operator/( FixedInt<M>) const { return FixedInt<N/M>(); }
73 template<int M>
74 FixedInt<N%M> operator%( FixedInt<M>) const { return FixedInt<N%M>(); }
75 template<int M>
76 FixedInt<N|M> operator|( FixedInt<M>) const { return FixedInt<N|M>(); }
77 template<int M>
78 FixedInt<N&M> operator&( FixedInt<M>) const { return FixedInt<N&M>(); }
79
80#if EIGEN_HAS_CXX14_VARIABLE_TEMPLATES
81 // Needed in C++14 to allow fix<N>():
82 FixedInt operator() () const { return *this; }
83
84 VariableAndFixedInt<N> operator() (int val) const { return VariableAndFixedInt<N>(val); }
85#else
86 FixedInt ( FixedInt<N> (*)() ) {}
87#endif
88
89#if EIGEN_HAS_CXX11
90 FixedInt(std::integral_constant<int,N>) {}
91#endif
92};
93
123template<int N> class VariableAndFixedInt
124{
125public:
126 static const int value = N;
127 operator int() const { return m_value; }
128 VariableAndFixedInt(int val) { m_value = val; }
129protected:
130 int m_value;
131};
132
133template<typename T, int Default=Dynamic> struct get_fixed_value {
134 static const int value = Default;
135};
136
137template<int N,int Default> struct get_fixed_value<FixedInt<N>,Default> {
138 static const int value = N;
139};
140
141#if !EIGEN_HAS_CXX14
142template<int N,int Default> struct get_fixed_value<FixedInt<N> (*)(),Default> {
143 static const int value = N;
144};
145#endif
146
147template<int N,int Default> struct get_fixed_value<VariableAndFixedInt<N>,Default> {
148 static const int value = N ;
149};
150
151template<typename T, int N, int Default>
152struct get_fixed_value<variable_if_dynamic<T,N>,Default> {
153 static const int value = N;
154};
155
156template<typename T> EIGEN_DEVICE_FUNC Index get_runtime_value(const T &x) { return x; }
157#if !EIGEN_HAS_CXX14
158template<int N> EIGEN_DEVICE_FUNC Index get_runtime_value(FixedInt<N> (*)()) { return N; }
159#endif
160
161// Cleanup integer/FixedInt/VariableAndFixedInt/etc types:
162
163// By default, no cleanup:
164template<typename T, int DynamicKey=Dynamic, typename EnableIf=void> struct cleanup_index_type { typedef T type; };
165
166// Convert any integral type (e.g., short, int, unsigned int, etc.) to Eigen::Index
167template<typename T, int DynamicKey> struct cleanup_index_type<T,DynamicKey,typename internal::enable_if<internal::is_integral<T>::value>::type> { typedef Index type; };
168
169#if !EIGEN_HAS_CXX14
170// In c++98/c++11, fix<N> is a pointer to function that we better cleanup to a true FixedInt<N>:
171template<int N, int DynamicKey> struct cleanup_index_type<FixedInt<N> (*)(), DynamicKey> { typedef FixedInt<N> type; };
172#endif
173
174// If VariableAndFixedInt does not match DynamicKey, then we turn it to a pure compile-time value:
175template<int N, int DynamicKey> struct cleanup_index_type<VariableAndFixedInt<N>, DynamicKey> { typedef FixedInt<N> type; };
176// If VariableAndFixedInt matches DynamicKey, then we turn it to a pure runtime-value (aka Index):
177template<int DynamicKey> struct cleanup_index_type<VariableAndFixedInt<DynamicKey>, DynamicKey> { typedef Index type; };
178
179#if EIGEN_HAS_CXX11
180template<int N, int DynamicKey> struct cleanup_index_type<std::integral_constant<int,N>, DynamicKey> { typedef FixedInt<N> type; };
181#endif
182
183} // end namespace internal
184
185#ifndef EIGEN_PARSED_BY_DOXYGEN
186
187#if EIGEN_HAS_CXX14_VARIABLE_TEMPLATES
188template<int N>
189static const internal::FixedInt<N> fix{};
190#else
191template<int N>
192inline internal::FixedInt<N> fix() { return internal::FixedInt<N>(); }
193
194// The generic typename T is mandatory. Otherwise, a code like fix<N> could refer to either the function above or this next overload.
195// This way a code like fix<N> can only refer to the previous function.
196template<int N,typename T>
197inline internal::VariableAndFixedInt<N> fix(T val) { return internal::VariableAndFixedInt<N>(internal::convert_index<int>(val)); }
198#endif
199
200#else // EIGEN_PARSED_BY_DOXYGEN
201
234template<int N>
235static const auto fix();
236
265template<int N>
266static const auto fix(int val);
267
268#endif // EIGEN_PARSED_BY_DOXYGEN
269
270} // end namespace Eigen
271
272#endif // EIGEN_INTEGRAL_CONSTANT_H
static const auto fix()
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74