diff options
author | Gael Guennebaud <g.gael@free.fr> | 2017-01-16 16:17:01 +0100 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2017-01-16 16:17:01 +0100 |
commit | a9232af845702d62d81c49b4d92b39f29410cea0 (patch) | |
tree | 6efb7ecec7c49880a16d2ab31599d9c0dba3de7f /Eigen/src/Core | |
parent | 6e97698161275db750868afb99f405cdb849f412 (diff) |
Introduce a variable_or_fixed<N> proxy returned by fix<N>(val) to pass both a compile-time and runtime fallback value in case N means "runtime".
This mechanism is used by the seq/seqN functions. The proxy object is immediately converted to pure compile-time (as fix<N>) or pure runtime (i.e., an Index) to avoid redundant template instantiations.
Diffstat (limited to 'Eigen/src/Core')
-rw-r--r-- | Eigen/src/Core/ArithmeticSequence.h | 56 | ||||
-rw-r--r-- | Eigen/src/Core/util/IntegralConstant.h | 69 |
2 files changed, 103 insertions, 22 deletions
diff --git a/Eigen/src/Core/ArithmeticSequence.h b/Eigen/src/Core/ArithmeticSequence.h index afb014ac5..bafc00f14 100644 --- a/Eigen/src/Core/ArithmeticSequence.h +++ b/Eigen/src/Core/ArithmeticSequence.h @@ -119,10 +119,26 @@ protected: namespace internal { -template<typename T, typename EnableIf=void> struct cleanup_seq_type { typedef T type; }; -template<typename T> struct cleanup_seq_type<T,typename internal::enable_if<internal::is_integral<T>::value>::type> { typedef Index type; }; -template<int N> struct cleanup_seq_type<fix_t<N> > { typedef fix_t<N> type; }; -template<int N> struct cleanup_seq_type<fix_t<N> (*)() > { typedef fix_t<N> type; }; +// Cleanup return types: + +// By default, no change: +template<typename T, int DynamicKey=Dynamic, typename EnableIf=void> struct cleanup_seq_type { typedef T type; }; + +// Convert short, int, unsigned int, etc. to Eigen::Index +template<typename T, int DynamicKey> struct cleanup_seq_type<T,DynamicKey,typename internal::enable_if<internal::is_integral<T>::value>::type> { typedef Index type; }; + +// In c++98/c++11, fix<N> is a pointer to function that we better cleanup to a true fix_t<N>: +template<int N, int DynamicKey> struct cleanup_seq_type<fix_t<N> (*)(), DynamicKey> { typedef fix_t<N> type; }; + +// If variable_or_fixed does not match DynamicKey, then we turn it to a pure compile-time value: +template<int N, int DynamicKey> struct cleanup_seq_type<variable_or_fixed<N>, DynamicKey> { typedef fix_t<N> type; }; +// If variable_or_fixed matches DynamicKey, then we turn it to a pure runtime-value (aka Index): +template<int DynamicKey> struct cleanup_seq_type<variable_or_fixed<DynamicKey>, DynamicKey> { typedef Index type; }; + +// Helper to cleanup the type of the increment: +template<typename T> struct cleanup_seq_incr { + typedef typename cleanup_seq_type<T,DynamicIndex>::type type; +}; } @@ -130,9 +146,9 @@ template<int N> struct cleanup_seq_type<fix_t<N> (*)() > { typedef fix_t<N> type * * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */ template<typename FirstType,typename SizeType,typename IncrType> -ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,typename internal::cleanup_seq_type<SizeType>::type,typename internal::cleanup_seq_type<IncrType>::type > +ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,typename internal::cleanup_seq_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type > seqN(FirstType first, SizeType size, IncrType incr) { - return ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,typename internal::cleanup_seq_type<SizeType>::type,typename internal::cleanup_seq_type<IncrType>::type>(first,size,incr); + return ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,typename internal::cleanup_seq_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type>(first,size,incr); } /** \returns an ArithemeticSequence starting at \a first, of length \a size, and unit increment @@ -181,10 +197,10 @@ auto seq(FirstType f, LastType l) -> decltype(seqN(f,(l-f+fix<1>()))) template<typename FirstType,typename LastType, typename IncrType> auto seq(FirstType f, LastType l, IncrType incr) - -> decltype(seqN(f, (l-f+typename internal::cleanup_seq_type<IncrType>::type(incr)) - / typename internal::cleanup_seq_type<IncrType>::type(incr),typename internal::cleanup_seq_type<IncrType>::type(incr))) + -> decltype(seqN(f, (l-f+typename internal::cleanup_seq_incr<IncrType>::type(incr)) + / typename internal::cleanup_seq_incr<IncrType>::type(incr),typename internal::cleanup_seq_incr<IncrType>::type(incr))) { - typedef typename internal::cleanup_seq_type<IncrType>::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f,(l-f+CleanedIncrType(incr))/CleanedIncrType(incr),CleanedIncrType(incr)); } #else @@ -225,10 +241,10 @@ seq(const Symbolic::BaseExpr<FirstTypeDerived> &f, const Symbolic::BaseExpr<Last template<typename FirstType,typename LastType, typename IncrType> typename internal::enable_if<!(Symbolic::is_symbolic<FirstType>::value || Symbolic::is_symbolic<LastType>::value), - ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,Index,typename internal::cleanup_seq_type<IncrType>::type> >::type + ArithemeticSequence<typename internal::cleanup_seq_type<FirstType>::type,Index,typename internal::cleanup_seq_incr<IncrType>::type> >::type seq(FirstType f, LastType l, IncrType incr) { - typedef typename internal::cleanup_seq_type<IncrType>::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f,(l-f+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -239,10 +255,10 @@ typename internal::enable_if<!Symbolic::is_symbolic<LastType>::value, Symbolic::ValueExpr>, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type<IncrType>::type> >::type + typename internal::cleanup_seq_incr<IncrType>::type> >::type seq(const Symbolic::BaseExpr<FirstTypeDerived> &f, LastType l, IncrType incr) { - typedef typename internal::cleanup_seq_type<IncrType>::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f.derived(),(l-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -252,10 +268,10 @@ typename internal::enable_if<!Symbolic::is_symbolic<FirstType>::value, Symbolic::QuotientExpr<Symbolic::AddExpr<Symbolic::AddExpr<LastTypeDerived,Symbolic::ValueExpr>, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type<IncrType>::type> >::type + typename internal::cleanup_seq_incr<IncrType>::type> >::type seq(FirstType f, const Symbolic::BaseExpr<LastTypeDerived> &l, IncrType incr) { - typedef typename internal::cleanup_seq_type<IncrType>::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f,(l.derived()-f+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -265,10 +281,10 @@ ArithemeticSequence<FirstTypeDerived, Symbolic::NegateExpr<FirstTypeDerived> >, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type<IncrType>::type> + typename internal::cleanup_seq_incr<IncrType>::type> seq(const Symbolic::BaseExpr<FirstTypeDerived> &f, const Symbolic::BaseExpr<LastTypeDerived> &l, IncrType incr) { - typedef typename internal::cleanup_seq_type<IncrType>::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f.derived(),(l.derived()-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } #endif @@ -394,13 +410,13 @@ seq(FirstType f, LastType l) { template<typename FirstType,typename LastType,typename IncrType> ArithemeticSequenceProxyWithBounds< typename internal::cleanup_seq_type<FirstType>::type, typename internal::cleanup_seq_type<LastType>::type, - typename internal::cleanup_seq_type<IncrType>::type > + typename internal::cleanup_seq_incr<IncrType>::type > seq(FirstType f, LastType l, IncrType s) { return ArithemeticSequenceProxyWithBounds<typename internal::cleanup_seq_type<FirstType>::type, typename internal::cleanup_seq_type<LastType>::type, - typename internal::cleanup_seq_type<IncrType>::type> - (f,l,typename internal::cleanup_seq_type<IncrType>::type(s)); + typename internal::cleanup_seq_incr<IncrType>::type> + (f,l,typename internal::cleanup_seq_incr<IncrType>::type(s)); } } diff --git a/Eigen/src/Core/util/IntegralConstant.h b/Eigen/src/Core/util/IntegralConstant.h index a4394c464..dc8c8413d 100644 --- a/Eigen/src/Core/util/IntegralConstant.h +++ b/Eigen/src/Core/util/IntegralConstant.h @@ -15,13 +15,35 @@ namespace Eigen { namespace internal { +template<int N> struct fix_t; +template<int N> class variable_or_fixed; + template<int N> struct fix_t { static const int value = N; operator int() const { return value; } - fix_t (fix_t<N> (*)() ) {} fix_t() {} + fix_t(variable_or_fixed<N> other) { + EIGEN_ONLY_USED_FOR_DEBUG(other); + eigen_internal_assert(int(other)==N); + } + +#if EIGEN_HAS_CXX14 // Needed in C++14 to allow fix<N>(): fix_t operator() () const { return *this; } + + variable_or_fixed<N> operator() (int val) const { return variable_or_fixed<N>(val); } +#else + fix_t (fix_t<N> (*)() ) {} +#endif +}; + +template<int N> class variable_or_fixed { +public: + static const int value = N; + operator int() const { return m_value; } + variable_or_fixed(int val) { m_value = val; } +protected: + int m_value; }; template<typename T, int Default=Dynamic> struct get_compile_time { @@ -32,6 +54,10 @@ template<int N,int Default> struct get_compile_time<fix_t<N>,Default> { enum { value = N }; }; +template<int N,int Default> struct get_compile_time<variable_or_fixed<N>,Default> { + enum { value = N }; +}; + template<typename T, int N, int Default> struct get_compile_time<variable_if_dynamic<T,N>,Default> { enum { value = N }; @@ -45,12 +71,17 @@ template<int N> struct is_compile_time<fix_t<N> > { enum { value = true }; }; #ifndef EIGEN_PARSED_BY_DOXYGEN -#if __cplusplus > 201103L +#if EIGEN_HAS_CXX14 template<int N> static const internal::fix_t<N> fix{}; #else template<int N> inline internal::fix_t<N> fix() { return internal::fix_t<N>(); } + +// The generic typename T is mandatory. Otherwise, a code like fix<N> could refer to either the function above or this next overload. +// This way a code like fix<N> can only refer to the previous function. +template<int N,typename T> +inline internal::variable_or_fixed<N> fix(T val) { return internal::variable_or_fixed<N>(val); } #endif #else // EIGEN_PARSED_BY_DOXYGEN @@ -67,6 +98,8 @@ inline internal::fix_t<N> fix() { return internal::fix_t<N>(); } * seqN(10,fix<4>,fix<-3>) // <=> [10 7 4 1] * \endcode * + * See also the function fix(int) to pass both a compile-time and runtime value. + * * In c++14, it is implemented as: * \code * template<int N> static const internal::fix_t<N> fix{}; @@ -82,10 +115,42 @@ inline internal::fix_t<N> fix() { return internal::fix_t<N>(); } * Here internal::fix_t<N> is thus a pointer to function. * * If for some reason you want a true object in c++98 then you can write: \code fix<N>() \endcode which is also valid in c++14. + * + * \sa fix(int), seq, seqN */ template<int N> static const auto fix; +/** \fn fix(int) + * \ingroup Core_Module + * + * This function returns an object embedding both a compile-time integer \c N, and a runtime value \a val. + * + * \tparam N the compile-time integer value + * \param val the runtime integer value + * + * This function is a more general version of the \ref fix identifier/function that can be used in template code + * where the compile-time value coudl turned out to actually mean "undefined at compile-time". For positive integers + * such as a size of a dimension, this case is identified by Eigen::Dynamic, whereas runtime signed integers (e.g., an increment/stride) are identified + * as Eigen::DynamicIndex. + * + * A typical use case would be: + * \code + * template<typename Derived> void foo(const MatrixBase<Derived> &mat) { + * const int N = Derived::RowsAtCompileTime==Dynamic ? Dynamic : Derived::RowsAtCompileTime/2; + * const int n = mat.rows()/2; + * ... mat( seqN(0,fix<N>(n) ) ...; + * } + * \endcode + * In this example, the function Eigen::seqN knows that the second argument is expected to be a size. + * If the passed compile-time value N equals Eigen::Dynamic, then the proxy object returned by fix will be dissmissed, and converted to an Eigen::Index of value \c n. + * Otherwise, the runtime-value \c n will be dissmissed, and the returned ArithmeticSequence will be of the exact same type as <tt> seqN(0,fix<N>) </tt>. + * + * \sa fix, seqN, class ArithmeticSequence + */ +template<int N> +static const auto fix(int val); + #endif // EIGEN_PARSED_BY_DOXYGEN } // end namespace Eigen |