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 struct cleanup_seq_type { typedef T type; }; -template struct cleanup_seq_type::value>::type> { typedef Index type; }; -template struct cleanup_seq_type > { typedef fix_t type; }; -template struct cleanup_seq_type (*)() > { typedef fix_t type; }; +// Cleanup return types: + +// By default, no change: +template struct cleanup_seq_type { typedef T type; }; + +// Convert short, int, unsigned int, etc. to Eigen::Index +template struct cleanup_seq_type::value>::type> { typedef Index type; }; + +// In c++98/c++11, fix is a pointer to function that we better cleanup to a true fix_t: +template struct cleanup_seq_type (*)(), DynamicKey> { typedef fix_t type; }; + +// If variable_or_fixed does not match DynamicKey, then we turn it to a pure compile-time value: +template struct cleanup_seq_type, DynamicKey> { typedef fix_t type; }; +// If variable_or_fixed matches DynamicKey, then we turn it to a pure runtime-value (aka Index): +template struct cleanup_seq_type, DynamicKey> { typedef Index type; }; + +// Helper to cleanup the type of the increment: +template struct cleanup_seq_incr { + typedef typename cleanup_seq_type::type type; +}; } @@ -130,9 +146,9 @@ template struct cleanup_seq_type (*)() > { typedef fix_t type * * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */ template -ArithemeticSequence::type,typename internal::cleanup_seq_type::type,typename internal::cleanup_seq_type::type > +ArithemeticSequence::type,typename internal::cleanup_seq_type::type,typename internal::cleanup_seq_incr::type > seqN(FirstType first, SizeType size, IncrType incr) { - return ArithemeticSequence::type,typename internal::cleanup_seq_type::type,typename internal::cleanup_seq_type::type>(first,size,incr); + return ArithemeticSequence::type,typename internal::cleanup_seq_type::type,typename internal::cleanup_seq_incr::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 auto seq(FirstType f, LastType l, IncrType incr) - -> decltype(seqN(f, (l-f+typename internal::cleanup_seq_type::type(incr)) - / typename internal::cleanup_seq_type::type(incr),typename internal::cleanup_seq_type::type(incr))) + -> decltype(seqN(f, (l-f+typename internal::cleanup_seq_incr::type(incr)) + / typename internal::cleanup_seq_incr::type(incr),typename internal::cleanup_seq_incr::type(incr))) { - typedef typename internal::cleanup_seq_type::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr::type CleanedIncrType; return seqN(f,(l-f+CleanedIncrType(incr))/CleanedIncrType(incr),CleanedIncrType(incr)); } #else @@ -225,10 +241,10 @@ seq(const Symbolic::BaseExpr &f, const Symbolic::BaseExpr typename internal::enable_if::value || Symbolic::is_symbolic::value), - ArithemeticSequence::type,Index,typename internal::cleanup_seq_type::type> >::type + ArithemeticSequence::type,Index,typename internal::cleanup_seq_incr::type> >::type seq(FirstType f, LastType l, IncrType incr) { - typedef typename internal::cleanup_seq_type::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr::type CleanedIncrType; return seqN(f,(l-f+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -239,10 +255,10 @@ typename internal::enable_if::value, Symbolic::ValueExpr>, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type::type> >::type + typename internal::cleanup_seq_incr::type> >::type seq(const Symbolic::BaseExpr &f, LastType l, IncrType incr) { - typedef typename internal::cleanup_seq_type::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr::type CleanedIncrType; return seqN(f.derived(),(l-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -252,10 +268,10 @@ typename internal::enable_if::value, Symbolic::QuotientExpr, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type::type> >::type + typename internal::cleanup_seq_incr::type> >::type seq(FirstType f, const Symbolic::BaseExpr &l, IncrType incr) { - typedef typename internal::cleanup_seq_type::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr::type CleanedIncrType; return seqN(f,(l.derived()-f+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } @@ -265,10 +281,10 @@ ArithemeticSequence >, Symbolic::ValueExpr>, Symbolic::ValueExpr>, - typename internal::cleanup_seq_type::type> + typename internal::cleanup_seq_incr::type> seq(const Symbolic::BaseExpr &f, const Symbolic::BaseExpr &l, IncrType incr) { - typedef typename internal::cleanup_seq_type::type CleanedIncrType; + typedef typename internal::cleanup_seq_incr::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 ArithemeticSequenceProxyWithBounds< typename internal::cleanup_seq_type::type, typename internal::cleanup_seq_type::type, - typename internal::cleanup_seq_type::type > + typename internal::cleanup_seq_incr::type > seq(FirstType f, LastType l, IncrType s) { return ArithemeticSequenceProxyWithBounds::type, typename internal::cleanup_seq_type::type, - typename internal::cleanup_seq_type::type> - (f,l,typename internal::cleanup_seq_type::type(s)); + typename internal::cleanup_seq_incr::type> + (f,l,typename internal::cleanup_seq_incr::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 struct fix_t; +template class variable_or_fixed; + template struct fix_t { static const int value = N; operator int() const { return value; } - fix_t (fix_t (*)() ) {} fix_t() {} + fix_t(variable_or_fixed other) { + EIGEN_ONLY_USED_FOR_DEBUG(other); + eigen_internal_assert(int(other)==N); + } + +#if EIGEN_HAS_CXX14 // Needed in C++14 to allow fix(): fix_t operator() () const { return *this; } + + variable_or_fixed operator() (int val) const { return variable_or_fixed(val); } +#else + fix_t (fix_t (*)() ) {} +#endif +}; + +template 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 struct get_compile_time { @@ -32,6 +54,10 @@ template struct get_compile_time,Default> { enum { value = N }; }; +template struct get_compile_time,Default> { + enum { value = N }; +}; + template struct get_compile_time,Default> { enum { value = N }; @@ -45,12 +71,17 @@ template struct is_compile_time > { enum { value = true }; }; #ifndef EIGEN_PARSED_BY_DOXYGEN -#if __cplusplus > 201103L +#if EIGEN_HAS_CXX14 template static const internal::fix_t fix{}; #else template inline internal::fix_t fix() { return internal::fix_t(); } + +// The generic typename T is mandatory. Otherwise, a code like fix could refer to either the function above or this next overload. +// This way a code like fix can only refer to the previous function. +template +inline internal::variable_or_fixed fix(T val) { return internal::variable_or_fixed(val); } #endif #else // EIGEN_PARSED_BY_DOXYGEN @@ -67,6 +98,8 @@ inline internal::fix_t fix() { return internal::fix_t(); } * 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 static const internal::fix_t fix{}; @@ -82,10 +115,42 @@ inline internal::fix_t fix() { return internal::fix_t(); } * Here internal::fix_t is thus a pointer to function. * * If for some reason you want a true object in c++98 then you can write: \code fix() \endcode which is also valid in c++14. + * + * \sa fix(int), seq, seqN */ template 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 void foo(const MatrixBase &mat) { + * const int N = Derived::RowsAtCompileTime==Dynamic ? Dynamic : Derived::RowsAtCompileTime/2; + * const int n = mat.rows()/2; + * ... mat( seqN(0,fix(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 seqN(0,fix) . + * + * \sa fix, seqN, class ArithmeticSequence + */ +template +static const auto fix(int val); + #endif // EIGEN_PARSED_BY_DOXYGEN } // end namespace Eigen diff --git a/test/indexed_view.cpp b/test/indexed_view.cpp index e2c0e886d..472268010 100644 --- a/test/indexed_view.cpp +++ b/test/indexed_view.cpp @@ -8,7 +8,12 @@ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #ifdef EIGEN_TEST_PART_2 -// Make sure we also check c++98 implementation +// Make sure we also check c++11 max implementation +#define EIGEN_MAX_CPP_VER 11 +#endif + +#ifdef EIGEN_TEST_PART_3 +// Make sure we also check c++98 max implementation #define EIGEN_MAX_CPP_VER 03 #endif @@ -49,6 +54,13 @@ is_same_type(const T1& a, const T2& b) return (a == b).all(); } +template +typename internal::enable_if::value,bool>::type +is_same_seq_type(const T1& a, const T2& b) +{ + return a.size() == b.size() && a.first()==b.first() && Index(a.incrObject())==Index(b.incrObject()); +} + void check_indexed_view() { using Eigen::placeholders::all; @@ -157,6 +169,23 @@ void check_indexed_view() VERIFY_IS_EQUAL( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2); VERIFY_IS_EQUAL( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , 3*4); + VERIFY_IS_EQUAL( (A(seqN(2,fix<5>), seqN(1,fix<3>))).RowsAtCompileTime, 5); + VERIFY_IS_EQUAL( (A(seqN(2,fix<5>), seqN(1,fix<3>))).ColsAtCompileTime, 3); + VERIFY_IS_EQUAL( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).RowsAtCompileTime, 5); + VERIFY_IS_EQUAL( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).ColsAtCompileTime, 3); + VERIFY_IS_EQUAL( (A(seqN(2,fix(5)), seqN(1,fix(3)))).RowsAtCompileTime, Dynamic); + VERIFY_IS_EQUAL( (A(seqN(2,fix(5)), seqN(1,fix(3)))).ColsAtCompileTime, Dynamic); + VERIFY_IS_EQUAL( (A(seqN(2,fix(5)), seqN(1,fix(3)))).rows(), 5); + VERIFY_IS_EQUAL( (A(seqN(2,fix(5)), seqN(1,fix(3)))).cols(), 3); + + VERIFY( is_same_seq_type( seqN(2,5,fix<-1>), seqN(2,5,fix<-1>(-1)) ) ); + VERIFY( is_same_seq_type( seqN(2,5), seqN(2,5,fix<1>(1)) ) ); + VERIFY( is_same_seq_type( seqN(2,5,3), seqN(2,5,fix(3)) ) ); + VERIFY( is_same_seq_type( seq(2,7,fix<3>), seqN(2,2,fix<3>) ) ); + VERIFY( is_same_seq_type( seqN(2,fix(5),3), seqN(2,5,fix(3)) ) ); + VERIFY( is_same_seq_type( seqN(2,fix<5>(5),fix<-2>), seqN(2,fix<5>,fix<-2>()) ) ); + + VERIFY( (A(seqN(2,fix<5>), 5)).RowsAtCompileTime == 5); VERIFY( (A(4, all)).ColsAtCompileTime == Dynamic); VERIFY( (A(4, all)).RowsAtCompileTime == 1);