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 
11 #ifndef EIGEN_INTEGRAL_CONSTANT_H
12 #define EIGEN_INTEGRAL_CONSTANT_H
13 
14 #include "../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template<int N> class FixedInt;
21 template<int N> class VariableAndFixedInt;
22 
51 template<int N> class FixedInt
52 {
53 public:
54  static const int value = N;
55  EIGEN_CONSTEXPR operator int() const { return value; }
56 
58  FixedInt() = default;
59 
61  FixedInt(std::integral_constant<int,N>) {}
62 
64  FixedInt( VariableAndFixedInt<N> other) {
65  #ifndef EIGEN_INTERNAL_DEBUGGING
66  EIGEN_UNUSED_VARIABLE(other);
67  #endif
68  eigen_internal_assert(int(other)==N);
69  }
70 
72  FixedInt<-N> operator-() const { return FixedInt<-N>(); }
73 
74  template<int M>
76  FixedInt<N+M> operator+( FixedInt<M>) const { return FixedInt<N+M>(); }
77 
78  template<int M>
80  FixedInt<N-M> operator-( FixedInt<M>) const { return FixedInt<N-M>(); }
81 
82  template<int M>
84  FixedInt<N*M> operator*( FixedInt<M>) const { return FixedInt<N*M>(); }
85 
86  template<int M>
88  FixedInt<N/M> operator/( FixedInt<M>) const { return FixedInt<N/M>(); }
89 
90  template<int M>
92  FixedInt<N%M> operator%( FixedInt<M>) const { return FixedInt<N%M>(); }
93 
94  template<int M>
96  FixedInt<N|M> operator|( FixedInt<M>) const { return FixedInt<N|M>(); }
97 
98  template<int M>
100  FixedInt<N&M> operator&( FixedInt<M>) const { return FixedInt<N&M>(); }
101 
102  // Needed in C++14 to allow fix<N>():
103  EIGEN_CONSTEXPR FixedInt operator() () const { return *this; }
104 
105  VariableAndFixedInt<N> operator() (int val) const { return VariableAndFixedInt<N>(val); }
106 };
107 
137 template<int N> class VariableAndFixedInt
138 {
139 public:
140  static const int value = N;
141  operator int() const { return m_value; }
142  VariableAndFixedInt(int val) { m_value = val; }
143 protected:
144  int m_value;
145 };
146 
147 template<typename T, int Default=Dynamic> struct get_fixed_value {
148  static const int value = Default;
149 };
150 
151 template<int N,int Default> struct get_fixed_value<FixedInt<N>,Default> {
152  static const int value = N;
153 };
154 
155 template<int N,int Default> struct get_fixed_value<VariableAndFixedInt<N>,Default> {
156  static const int value = N ;
157 };
158 
159 template<typename T, int N, int Default>
160 struct get_fixed_value<variable_if_dynamic<T,N>,Default> {
161  static const int value = N;
162 };
163 
164 template<typename T> EIGEN_DEVICE_FUNC Index get_runtime_value(const T &x) { return x; }
165 
166 // Cleanup integer/FixedInt/VariableAndFixedInt/etc types:
167 
168 // By default, no cleanup:
169 template<typename T, int DynamicKey=Dynamic, typename EnableIf=void> struct cleanup_index_type { typedef T type; };
170 
171 // Convert any integral type (e.g., short, int, unsigned int, etc.) to Eigen::Index
172 template<typename T, int DynamicKey> struct cleanup_index_type<T,DynamicKey,std::enable_if_t<internal::is_integral<T>::value>> { typedef Index type; };
173 
174 // If VariableAndFixedInt does not match DynamicKey, then we turn it to a pure compile-time value:
175 template<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):
177 template<int DynamicKey> struct cleanup_index_type<VariableAndFixedInt<DynamicKey>, DynamicKey> { typedef Index type; };
178 
179 template<int N, int DynamicKey> struct cleanup_index_type<std::integral_constant<int,N>, DynamicKey> { typedef FixedInt<N> type; };
180 
181 } // end namespace internal
182 
183 #ifndef EIGEN_PARSED_BY_DOXYGEN
184 
185 template<int N>
186 constexpr internal::FixedInt<N> fix{};
187 
188 #else // EIGEN_PARSED_BY_DOXYGEN
189 
214 template<int N>
215 static const auto fix();
216 
245 template<int N>
246 static const auto fix(int val);
247 
248 #endif // EIGEN_PARSED_BY_DOXYGEN
249 
250 } // end namespace Eigen
251 
252 #endif // EIGEN_INTEGRAL_CONSTANT_H
const CwiseBinaryOp< internal::scalar_bitwise_or_op< Scalar >, const Derived, const OtherDerived > operator|(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
const CwiseBinaryOp< internal::scalar_bitwise_and_op< Scalar >, const Derived, const OtherDerived > operator&(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
Matrix4Xd M
IndexedView_or_Block operator()(const RowIndices &rowIndices, const ColIndices &colIndices)
#define eigen_internal_assert(x)
Definition: Macros.h:908
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:957
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
static const auto fix()
bfloat16 operator/(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:275
Index get_runtime_value(const T &x)
: InteropHeaders
Definition: Core:139
const CwiseBinaryOp< internal::scalar_difference_op< typename DenseDerived::Scalar, typename SparseDerived::Scalar >, const DenseDerived, const SparseDerived > operator-(const MatrixBase< DenseDerived > &a, const SparseMatrixBase< SparseDerived > &b)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
@ Default
Definition: Constants.h:364
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
const CwiseBinaryOp< internal::scalar_sum_op< typename DenseDerived::Scalar, typename SparseDerived::Scalar >, const DenseDerived, const SparseDerived > operator+(const MatrixBase< DenseDerived > &a, const SparseMatrixBase< SparseDerived > &b)
Definition: BFloat16.h:222