mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-13 04:09:10 +08:00
Move Eigen::all,last,lastp1,lastN to Eigen::placeholders::.
These names are so common, IMO they should not exist directly in the `Eigen::` namespace. This prevents us from using the `last` or `all` names for any parameters or local variables, otherwise spitting out warnings about shadowing or hiding the global values. Many external projects (and our own examples) also heavily use ``` using namespace Eigen; ``` which means these conflict with external libraries as well, e.g. `std::fill(first,last,value)`. It seems originally these were placed in a separate namespace `Eigen::placeholders`, which has since been deprecated. I propose to un-deprecate this, and restore the original locations. These symbols are also imported into `Eigen::indexing`, which additionally imports `fix` and `seq`. An alternative is to remove the `placeholders` namespace and stick with `indexing`. NOTE: this is an API-breaking change. Fixes #2321.
This commit is contained in:
parent
44da7a3b9d
commit
5dac0b53c9
@ -74,7 +74,7 @@ template<typename T> struct cleanup_seq_incr {
|
|||||||
typedef typename cleanup_index_type<T,DynamicIndex>::type type;
|
typedef typename cleanup_index_type<T,DynamicIndex>::type type;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace internal
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------
|
||||||
// seq(first,last,incr) and seqN(first,size,incr)
|
// seq(first,last,incr) and seqN(first,size,incr)
|
||||||
@ -321,6 +321,7 @@ seq(const symbolic::BaseExpr<FirstTypeDerived> &f, const symbolic::BaseExpr<Last
|
|||||||
|
|
||||||
#endif // EIGEN_PARSED_BY_DOXYGEN
|
#endif // EIGEN_PARSED_BY_DOXYGEN
|
||||||
|
|
||||||
|
namespace placeholders {
|
||||||
|
|
||||||
#if EIGEN_HAS_CXX11 || defined(EIGEN_PARSED_BY_DOXYGEN)
|
#if EIGEN_HAS_CXX11 || defined(EIGEN_PARSED_BY_DOXYGEN)
|
||||||
/** \cpp11
|
/** \cpp11
|
||||||
@ -331,9 +332,9 @@ seq(const symbolic::BaseExpr<FirstTypeDerived> &f, const symbolic::BaseExpr<Last
|
|||||||
* \sa lastN(SizeType), seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
|
* \sa lastN(SizeType), seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
|
||||||
template<typename SizeType,typename IncrType>
|
template<typename SizeType,typename IncrType>
|
||||||
auto lastN(SizeType size, IncrType incr)
|
auto lastN(SizeType size, IncrType incr)
|
||||||
-> decltype(seqN(Eigen::last-(size-fix<1>())*incr, size, incr))
|
-> decltype(seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr))
|
||||||
{
|
{
|
||||||
return seqN(Eigen::last-(size-fix<1>())*incr, size, incr);
|
return seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \cpp11
|
/** \cpp11
|
||||||
@ -344,12 +345,14 @@ auto lastN(SizeType size, IncrType incr)
|
|||||||
* \sa lastN(SizeType,IncrType, seqN(FirstType,SizeType), seq(FirstType,LastType) */
|
* \sa lastN(SizeType,IncrType, seqN(FirstType,SizeType), seq(FirstType,LastType) */
|
||||||
template<typename SizeType>
|
template<typename SizeType>
|
||||||
auto lastN(SizeType size)
|
auto lastN(SizeType size)
|
||||||
-> decltype(seqN(Eigen::last+fix<1>()-size, size))
|
-> decltype(seqN(Eigen::placeholders::last+fix<1>()-size, size))
|
||||||
{
|
{
|
||||||
return seqN(Eigen::last+fix<1>()-size, size);
|
return seqN(Eigen::placeholders::last+fix<1>()-size, size);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
} // namespace placeholders
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
// Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
|
// Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
|
||||||
@ -389,25 +392,25 @@ struct get_compile_time_incr<ArithmeticSequence<FirstType,SizeType,IncrType> > {
|
|||||||
* \code using namespace Eigen::indexing; \endcode
|
* \code using namespace Eigen::indexing; \endcode
|
||||||
* is equivalent to:
|
* is equivalent to:
|
||||||
* \code
|
* \code
|
||||||
using Eigen::all;
|
using Eigen::fix;
|
||||||
using Eigen::seq;
|
using Eigen::seq;
|
||||||
using Eigen::seqN;
|
using Eigen::seqN;
|
||||||
using Eigen::lastN; // c++11 only
|
using Eigen::placeholders::all;
|
||||||
using Eigen::last;
|
using Eigen::placeholders::last;
|
||||||
using Eigen::lastp1;
|
using Eigen::placeholders::lastN; // c++11 only
|
||||||
using Eigen::fix;
|
using Eigen::placeholders::lastp1;
|
||||||
\endcode
|
\endcode
|
||||||
*/
|
*/
|
||||||
namespace indexing {
|
namespace indexing {
|
||||||
using Eigen::all;
|
using Eigen::fix;
|
||||||
using Eigen::seq;
|
using Eigen::seq;
|
||||||
using Eigen::seqN;
|
using Eigen::seqN;
|
||||||
|
using Eigen::placeholders::all;
|
||||||
|
using Eigen::placeholders::last;
|
||||||
#if EIGEN_HAS_CXX11
|
#if EIGEN_HAS_CXX11
|
||||||
using Eigen::lastN;
|
using Eigen::placeholders::lastN;
|
||||||
#endif
|
#endif
|
||||||
using Eigen::last;
|
using Eigen::placeholders::lastp1;
|
||||||
using Eigen::lastp1;
|
|
||||||
using Eigen::fix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace Eigen
|
} // end namespace Eigen
|
||||||
|
@ -98,7 +98,7 @@ class IndexedViewImpl;
|
|||||||
* - decltype(ArrayXi::LinSpaced(...))
|
* - decltype(ArrayXi::LinSpaced(...))
|
||||||
* - Any view/expressions of the previous types
|
* - Any view/expressions of the previous types
|
||||||
* - Eigen::ArithmeticSequence
|
* - Eigen::ArithmeticSequence
|
||||||
* - Eigen::internal::AllRange (helper for Eigen::all)
|
* - Eigen::internal::AllRange (helper for Eigen::placeholders::all)
|
||||||
* - Eigen::internal::SingleRange (helper for single index)
|
* - Eigen::internal::SingleRange (helper for single index)
|
||||||
* - etc.
|
* - etc.
|
||||||
*
|
*
|
||||||
|
@ -17,7 +17,11 @@ namespace Eigen {
|
|||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
struct symbolic_last_tag {};
|
struct symbolic_last_tag {};
|
||||||
}
|
} // namespace internal
|
||||||
|
|
||||||
|
namespace placeholders {
|
||||||
|
|
||||||
|
typedef symbolic::SymbolExpr<internal::symbolic_last_tag> last_t;
|
||||||
|
|
||||||
/** \var last
|
/** \var last
|
||||||
* \ingroup Core_Module
|
* \ingroup Core_Module
|
||||||
@ -30,38 +34,20 @@ struct symbolic_last_tag {};
|
|||||||
* A typical usage example would be:
|
* A typical usage example would be:
|
||||||
* \code
|
* \code
|
||||||
* using namespace Eigen;
|
* using namespace Eigen;
|
||||||
* using Eigen::last;
|
* using Eigen::placeholders::last;
|
||||||
* VectorXd v(n);
|
* VectorXd v(n);
|
||||||
* v(seq(2,last-2)).setOnes();
|
* v(seq(2,last-2)).setOnes();
|
||||||
* \endcode
|
* \endcode
|
||||||
*
|
*
|
||||||
* \sa end
|
* \sa end
|
||||||
*/
|
*/
|
||||||
static const symbolic::SymbolExpr<internal::symbolic_last_tag> last; // PLEASE use Eigen::last instead of Eigen::placeholders::last
|
static const last_t last;
|
||||||
|
|
||||||
/** \var lastp1
|
} // namespace placeholders
|
||||||
* \ingroup Core_Module
|
|
||||||
*
|
|
||||||
* Can be used as a parameter to Eigen::seq and Eigen::seqN functions to symbolically
|
|
||||||
* reference the last+1 element/row/columns of the underlying vector or matrix once
|
|
||||||
* passed to DenseBase::operator()(const RowIndices&, const ColIndices&).
|
|
||||||
*
|
|
||||||
* This symbolic placeholder supports standard arithmetic operations.
|
|
||||||
* It is essentially an alias to last+fix<1>.
|
|
||||||
*
|
|
||||||
* \sa last
|
|
||||||
*/
|
|
||||||
#ifdef EIGEN_PARSED_BY_DOXYGEN
|
|
||||||
static const auto lastp1 = last+fix<1>;
|
|
||||||
#else
|
|
||||||
// Using a FixedExpr<1> expression is important here to make sure the compiler
|
|
||||||
// can fully optimize the computation starting indices with zero overhead.
|
|
||||||
static const symbolic::AddExpr<symbolic::SymbolExpr<internal::symbolic_last_tag>,symbolic::ValueExpr<Eigen::internal::FixedInt<1> > > lastp1(last+fix<1>());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
// Replace symbolic last/end "keywords" by their true runtime value
|
// Replace symbolic last/end "keywords" by their true runtime value
|
||||||
inline Index eval_expr_given_size(Index x, Index /* size */) { return x; }
|
inline Index eval_expr_given_size(Index x, Index /* size */) { return x; }
|
||||||
|
|
||||||
template<int N>
|
template<int N>
|
||||||
@ -70,7 +56,7 @@ FixedInt<N> eval_expr_given_size(FixedInt<N> x, Index /*size*/) { return x; }
|
|||||||
template<typename Derived>
|
template<typename Derived>
|
||||||
Index eval_expr_given_size(const symbolic::BaseExpr<Derived> &x, Index size)
|
Index eval_expr_given_size(const symbolic::BaseExpr<Derived> &x, Index size)
|
||||||
{
|
{
|
||||||
return x.derived().eval(last=size-1);
|
return x.derived().eval(Eigen::placeholders::last=size-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract increment/step at compile time
|
// Extract increment/step at compile time
|
||||||
@ -165,23 +151,44 @@ template<int Size> struct get_compile_time_incr<AllRange<Size> > {
|
|||||||
|
|
||||||
} // end namespace internal
|
} // end namespace internal
|
||||||
|
|
||||||
|
namespace placeholders {
|
||||||
|
|
||||||
|
typedef symbolic::AddExpr<symbolic::SymbolExpr<internal::symbolic_last_tag>,symbolic::ValueExpr<Eigen::internal::FixedInt<1> > > lastp1_t;
|
||||||
|
typedef Eigen::internal::all_t all_t;
|
||||||
|
|
||||||
|
/** \var lastp1
|
||||||
|
* \ingroup Core_Module
|
||||||
|
*
|
||||||
|
* Can be used as a parameter to Eigen::seq and Eigen::seqN functions to symbolically
|
||||||
|
* reference the last+1 element/row/columns of the underlying vector or matrix once
|
||||||
|
* passed to DenseBase::operator()(const RowIndices&, const ColIndices&).
|
||||||
|
*
|
||||||
|
* This symbolic placeholder supports standard arithmetic operations.
|
||||||
|
* It is essentially an alias to last+fix<1>.
|
||||||
|
*
|
||||||
|
* \sa last
|
||||||
|
*/
|
||||||
|
#ifdef EIGEN_PARSED_BY_DOXYGEN
|
||||||
|
static const auto lastp1 = last+fix<1>;
|
||||||
|
#else
|
||||||
|
// Using a FixedExpr<1> expression is important here to make sure the compiler
|
||||||
|
// can fully optimize the computation starting indices with zero overhead.
|
||||||
|
static const lastp1_t lastp1(last+fix<1>());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** \var end
|
||||||
|
* \ingroup Core_Module
|
||||||
|
* \sa lastp1
|
||||||
|
*/
|
||||||
|
static const lastp1_t end = lastp1;
|
||||||
|
|
||||||
/** \var all
|
/** \var all
|
||||||
* \ingroup Core_Module
|
* \ingroup Core_Module
|
||||||
* Can be used as a parameter to DenseBase::operator()(const RowIndices&, const ColIndices&) to index all rows or columns
|
* Can be used as a parameter to DenseBase::operator()(const RowIndices&, const ColIndices&) to index all rows or columns
|
||||||
*/
|
*/
|
||||||
static const Eigen::internal::all_t all; // PLEASE use Eigen::all instead of Eigen::placeholders::all
|
static const Eigen::internal::all_t all;
|
||||||
|
|
||||||
|
} // namespace placeholders
|
||||||
namespace placeholders {
|
|
||||||
typedef symbolic::SymbolExpr<internal::symbolic_last_tag> last_t;
|
|
||||||
typedef symbolic::AddExpr<symbolic::SymbolExpr<internal::symbolic_last_tag>,symbolic::ValueExpr<Eigen::internal::FixedInt<1> > > end_t;
|
|
||||||
typedef Eigen::internal::all_t all_t;
|
|
||||||
|
|
||||||
EIGEN_DEPRECATED static const all_t all = Eigen::all; // PLEASE use Eigen::all instead of Eigen::placeholders::all
|
|
||||||
EIGEN_DEPRECATED static const last_t last = Eigen::last; // PLEASE use Eigen::last instead of Eigen::placeholders::last
|
|
||||||
EIGEN_DEPRECATED static const end_t end = Eigen::lastp1; // PLEASE use Eigen::lastp1 instead of Eigen::placeholders::end
|
|
||||||
}
|
|
||||||
|
|
||||||
} // end namespace Eigen
|
} // end namespace Eigen
|
||||||
|
|
||||||
|
@ -37,7 +37,9 @@ namespace Eigen {
|
|||||||
* std::cout << expr98.eval(x=6) << "\n";
|
* std::cout << expr98.eval(x=6) << "\n";
|
||||||
* \endcode
|
* \endcode
|
||||||
*
|
*
|
||||||
* It is currently only used internally to define and manipulate the Eigen::last and Eigen::lastp1 symbols in Eigen::seq and Eigen::seqN.
|
* It is currently only used internally to define and manipulate the
|
||||||
|
* Eigen::placeholders::last and Eigen::placeholders::lastp1 symbols in
|
||||||
|
* Eigen::seq and Eigen::seqN.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
namespace symbolic {
|
namespace symbolic {
|
||||||
|
@ -218,7 +218,7 @@ operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST
|
|||||||
*
|
*
|
||||||
* Each parameter must either be:
|
* Each parameter must either be:
|
||||||
* - An integer indexing a single row or column
|
* - An integer indexing a single row or column
|
||||||
* - Eigen::all indexing the full set of respective rows or columns in increasing order
|
* - Eigen::placeholders::all indexing the full set of respective rows or columns in increasing order
|
||||||
* - An ArithmeticSequence as returned by the Eigen::seq and Eigen::seqN functions
|
* - An ArithmeticSequence as returned by the Eigen::seq and Eigen::seqN functions
|
||||||
* - Any %Eigen's vector/array of integers or expressions
|
* - Any %Eigen's vector/array of integers or expressions
|
||||||
* - Plain C arrays: \c int[N]
|
* - Plain C arrays: \c int[N]
|
||||||
@ -235,7 +235,7 @@ operator()(const IndicesT (&indices)[IndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST
|
|||||||
* method will returns a Block object after extraction of the relevant information from the passed arguments. This is the case
|
* method will returns a Block object after extraction of the relevant information from the passed arguments. This is the case
|
||||||
* when all arguments are either:
|
* when all arguments are either:
|
||||||
* - An integer
|
* - An integer
|
||||||
* - Eigen::all
|
* - Eigen::placeholders::all
|
||||||
* - An ArithmeticSequence with compile-time increment strictly equal to 1, as returned by Eigen::seq(a,b), and Eigen::seqN(a,N).
|
* - An ArithmeticSequence with compile-time increment strictly equal to 1, as returned by Eigen::seq(a,b), and Eigen::seqN(a,N).
|
||||||
*
|
*
|
||||||
* Otherwise a more general IndexedView<Derived,RowIndices',ColIndices'> object will be returned, after conversion of the inputs
|
* Otherwise a more general IndexedView<Derived,RowIndices',ColIndices'> object will be returned, after conversion of the inputs
|
||||||
|
@ -15,7 +15,7 @@ All the aforementioned operations are handled through the generic DenseBase::ope
|
|||||||
Each argument can be:
|
Each argument can be:
|
||||||
- An integer indexing a single row or column, including symbolic indices.
|
- An integer indexing a single row or column, including symbolic indices.
|
||||||
- The symbol Eigen::all representing the whole set of respective rows or columns in increasing order.
|
- The symbol Eigen::all representing the whole set of respective rows or columns in increasing order.
|
||||||
- An ArithmeticSequence as constructed by the Eigen::seq, Eigen::seqN, or Eigen::lastN functions.
|
- An ArithmeticSequence as constructed by the Eigen::seq, Eigen::seqN, or Eigen::placeholders::lastN functions.
|
||||||
- Any 1D vector/array of integers including %Eigen's vector/array, expressions, std::vector, std::array, as well as plain C arrays: `int[N]`.
|
- Any 1D vector/array of integers including %Eigen's vector/array, expressions, std::vector, std::array, as well as plain C arrays: `int[N]`.
|
||||||
|
|
||||||
More generally, it can accepts any object exposing the following two member functions:
|
More generally, it can accepts any object exposing the following two member functions:
|
||||||
@ -114,7 +114,8 @@ Here are some examples for a 2D array/matrix \c A and a 1D array/vector \c v.
|
|||||||
|
|
||||||
As seen in the last exemple, referencing the <i> last n </i> elements (or rows/columns) is a bit cumbersome to write.
|
As seen in the last exemple, referencing the <i> last n </i> elements (or rows/columns) is a bit cumbersome to write.
|
||||||
This becomes even more tricky and error prone with a non-default increment.
|
This becomes even more tricky and error prone with a non-default increment.
|
||||||
Here comes \link Eigen::lastN(SizeType) Eigen::lastN(size) \endlink, and \link Eigen::lastN(SizeType,IncrType) Eigen::lastN(size,incr) \endlink:
|
Here comes \link Eigen::placeholders::lastN(SizeType) Eigen::placeholders::lastN(size) \endlink, and
|
||||||
|
\link Eigen::placeholders::lastN(SizeType,IncrType) Eigen::placeholders::lastN(size,incr) \endlink:
|
||||||
|
|
||||||
<table class="manual">
|
<table class="manual">
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -36,7 +36,12 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
using Eigen::placeholders::all;
|
||||||
|
using Eigen::placeholders::last;
|
||||||
|
using Eigen::placeholders::lastp1;
|
||||||
|
|
||||||
#if EIGEN_HAS_CXX11
|
#if EIGEN_HAS_CXX11
|
||||||
|
using Eigen::placeholders::lastN;
|
||||||
#include <array>
|
#include <array>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -220,7 +225,7 @@ void check_indexed_view()
|
|||||||
VERIFY( is_same_seq_type( seqN(2,fix<5>(5),fix<-2>), seqN(2,fix<5>,fix<-2>()) ) );
|
VERIFY( is_same_seq_type( seqN(2,fix<5>(5),fix<-2>), seqN(2,fix<5>,fix<-2>()) ) );
|
||||||
|
|
||||||
VERIFY( is_same_seq_type( seq(2,fix<5>), seqN(2,4) ) );
|
VERIFY( is_same_seq_type( seq(2,fix<5>), seqN(2,4) ) );
|
||||||
#if EIGEN_HAS_CXX11
|
#if EIGEN_HAS_CXX11
|
||||||
VERIFY( is_same_seq_type( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
|
VERIFY( is_same_seq_type( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
|
||||||
VERIFY( is_same_seq( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
|
VERIFY( is_same_seq( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
|
||||||
VERIFY( is_same_seq( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
|
VERIFY( is_same_seq( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
|
||||||
|
@ -10,6 +10,9 @@
|
|||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
using Eigen::placeholders::last;
|
||||||
|
using Eigen::placeholders::all;
|
||||||
|
|
||||||
template<typename T1,typename T2>
|
template<typename T1,typename T2>
|
||||||
typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
|
typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
|
||||||
is_same_eq(const T1& a, const T2& b)
|
is_same_eq(const T1& a, const T2& b)
|
||||||
|
@ -133,6 +133,7 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
|
|||||||
ColMatrixType A = ColMatrixType::Random(rows,cols);
|
ColMatrixType A = ColMatrixType::Random(rows,cols);
|
||||||
const ColMatrixType& cA(A);
|
const ColMatrixType& cA(A);
|
||||||
RowMatrixType B = RowMatrixType::Random(rows,cols);
|
RowMatrixType B = RowMatrixType::Random(rows,cols);
|
||||||
|
using Eigen::placeholders::last;
|
||||||
|
|
||||||
Index i, j;
|
Index i, j;
|
||||||
|
|
||||||
|
@ -19,6 +19,10 @@
|
|||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
|
using Eigen::placeholders::last;
|
||||||
|
using Eigen::placeholders::lastp1;
|
||||||
|
using Eigen::placeholders::all;
|
||||||
|
|
||||||
template<typename T1,typename T2>
|
template<typename T1,typename T2>
|
||||||
bool is_same_symb(const T1& a, const T2& b, Index size)
|
bool is_same_symb(const T1& a, const T2& b, Index size)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user