Simplify API by removing allCols/allRows and reusing rowwise/colwise to define iterators over rows/columns

This commit is contained in:
Gael Guennebaud 2018-10-05 23:11:21 +02:00
parent 91613bf2c2
commit d92f004ab7
6 changed files with 50 additions and 51 deletions

View File

@ -597,10 +597,6 @@ template<typename Derived> class DenseBase
inline iterator end();
inline const_iterator end() const;
inline const_iterator cend() const;
inline SubVectorsProxy<Derived,Vertical> allCols();
inline SubVectorsProxy<const Derived,Vertical> allCols() const;
inline SubVectorsProxy<Derived,Horizontal> allRows();
inline SubVectorsProxy<const Derived,Horizontal> allRows() const;
#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase
#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL

View File

@ -228,39 +228,4 @@ inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cend() co
return const_iterator(derived(), size());
}
template<typename XprType, DirectionType Direction>
class SubVectorsProxy
{
public:
typedef internal::subvector_stl_iterator<XprType, Direction> iterator;
typedef internal::subvector_stl_iterator<const XprType, Direction> const_iterator;
SubVectorsProxy(XprType& xpr) : m_xpr(xpr) {}
iterator begin() const { return iterator (m_xpr, 0); }
const_iterator cbegin() const { return const_iterator(m_xpr, 0); }
iterator end() const { return iterator (m_xpr, m_xpr.template subVectors<Direction>()); }
const_iterator cend() const { return const_iterator(m_xpr, m_xpr.template subVectors<Direction>()); }
protected:
XprType& m_xpr;
};
template<typename Derived>
SubVectorsProxy<Derived,Vertical> DenseBase<Derived>::allCols()
{ return SubVectorsProxy<Derived,Vertical>(derived()); }
template<typename Derived>
SubVectorsProxy<const Derived,Vertical> DenseBase<Derived>::allCols() const
{ return SubVectorsProxy<const Derived,Vertical>(derived()); }
template<typename Derived>
SubVectorsProxy<Derived,Horizontal> DenseBase<Derived>::allRows()
{ return SubVectorsProxy<Derived,Horizontal>(derived()); }
template<typename Derived>
SubVectorsProxy<const Derived,Horizontal> DenseBase<Derived>::allRows() const
{ return SubVectorsProxy<const Derived,Horizontal>(derived()); }
} // namespace Eigen

View File

@ -241,6 +241,32 @@ template<typename ExpressionType, int Direction> class VectorwiseOp
EIGEN_DEVICE_FUNC
inline const ExpressionType& _expression() const { return m_matrix; }
#ifdef EIGEN_PARSED_BY_DOXYGEN
/** STL-like \link https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator RandomAccessIterator \endlink
* iterator type over the columns of rows as returned by the begin() and end() methods.
*/
random_access_iterator_type iterator;
/** This is the const version of iterator (aka read-only) */
random_access_iterator_type const_iterator;
#else
typedef internal::subvector_stl_iterator<ExpressionType, DirectionType(Direction)> iterator;
typedef internal::subvector_stl_iterator<const ExpressionType, DirectionType(Direction)> const_iterator;
#endif
/** returns an iterator to the first row (rowwise) or column (colwise) of the nested expression.
* \sa end(), cbegin()
*/
iterator begin() const { return iterator (m_matrix, 0); }
/** const version of begin() */
const_iterator cbegin() const { return const_iterator(m_matrix, 0); }
/** returns an iterator to the row (resp. column) following the last row (resp. column) of the nested expression
* \sa begin(), cend()
*/
iterator end() const { return iterator (m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()); }
/** const version of end() */
const_iterator cend() const { return const_iterator(m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()); }
/** \returns a row or column vector expression of \c *this reduxed by \a func
*
* The template parameter \a BinaryOp is the type of the functor

View File

@ -133,11 +133,11 @@ template<typename ExpressionType> class ArrayWrapper;
template<typename ExpressionType> class MatrixWrapper;
template<typename Derived> class SolverBase;
template<typename XprType> class InnerIterator;
template<typename XprType, DirectionType Direction> class SubVectorsProxy;
namespace internal {
template<typename XprType> class generic_randaccess_stl_iterator;
template<typename XprType> class pointer_based_stl_iterator;
template<typename XprType, DirectionType Direction> class subvector_stl_iterator;
template<typename DecompositionType> struct kernel_retval_base;
template<typename DecompositionType> struct kernel_retval;
template<typename DecompositionType> struct image_retval_base;

View File

@ -0,0 +1,12 @@
Matrix3i m = Matrix3i::Random();
cout << "Here is the initial matrix m:" << endl << m << endl;
int i = -1;
for(auto c: m.colwise()) {
c *= i;
++i;
}
cout << "Here is the matrix m after the for-range-loop:" << endl << m << endl;
auto cols = m.colwise();
auto it = std::find_if(cols.cbegin(), cols.cend(),
[](Matrix3i::ConstColXpr x) { return x.squaredNorm() == 0; });
cout << "The first empty column is: " << distance(cols.cbegin(),it) << endl;

View File

@ -209,11 +209,11 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
if(rows>=3) {
VERIFY_IS_EQUAL((v.begin()+rows/2)[1], v(rows/2+1));
VERIFY_IS_EQUAL((A.allRows().begin()+rows/2)[1], A.row(rows/2+1));
VERIFY_IS_EQUAL((A.rowwise().begin()+rows/2)[1], A.row(rows/2+1));
}
if(cols>=3) {
VERIFY_IS_EQUAL((A.allCols().begin()+cols/2)[1], A.col(cols/2+1));
VERIFY_IS_EQUAL((A.colwise().begin()+cols/2)[1], A.col(cols/2+1));
}
// check std::sort
@ -310,12 +310,12 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
// check rows/cols iterators with range-for loops
{
j = 0;
for(auto c : A.allCols()) { VERIFY_IS_APPROX(c.sum(), A.col(j).sum()); ++j; }
for(auto c : A.colwise()) { VERIFY_IS_APPROX(c.sum(), A.col(j).sum()); ++j; }
j = 0;
for(auto c : B.allCols()) { VERIFY_IS_APPROX(c.sum(), B.col(j).sum()); ++j; }
for(auto c : B.colwise()) { VERIFY_IS_APPROX(c.sum(), B.col(j).sum()); ++j; }
j = 0;
for(auto c : B.allCols()) {
for(auto c : B.colwise()) {
i = 0;
for(auto& x : c) {
VERIFY_IS_EQUAL(x, B(i,j));
@ -328,9 +328,9 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
B.setRandom();
i = 0;
for(auto r : A.allRows()) { VERIFY_IS_APPROX(r.sum(), A.row(i).sum()); ++i; }
for(auto r : A.rowwise()) { VERIFY_IS_APPROX(r.sum(), A.row(i).sum()); ++i; }
i = 0;
for(auto r : B.allRows()) { VERIFY_IS_APPROX(r.sum(), B.row(i).sum()); ++i; }
for(auto r : B.rowwise()) { VERIFY_IS_APPROX(r.sum(), B.row(i).sum()); ++i; }
}
@ -338,21 +338,21 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
{
RowVectorType row = RowVectorType::Random(cols);
A.rowwise() = row;
VERIFY( std::all_of(A.allRows().begin(), A.allRows().end(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.norm(),row.norm()); }) );
VERIFY( std::all_of(A.rowwise().begin(), A.rowwise().end(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.norm(),row.norm()); }) );
VectorType col = VectorType::Random(rows);
A.colwise() = col;
VERIFY( std::all_of(A.allCols().begin(), A.allCols().end(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.norm(),col.norm()); }) );
VERIFY( std::all_of(A.colwise().begin(), A.colwise().end(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.norm(),col.norm()); }) );
i = internal::random<Index>(0,A.rows()-1);
A.setRandom();
A.row(i).setZero();
VERIFY_IS_EQUAL( std::find_if(A.allRows().begin(), A.allRows().end(), [](typename ColMatrixType::RowXpr x) { return x.norm() == Scalar(0); })-A.allRows().begin(), i );
VERIFY_IS_EQUAL( std::find_if(A.rowwise().begin(), A.rowwise().end(), [](typename ColMatrixType::RowXpr x) { return x.norm() == Scalar(0); })-A.rowwise().begin(), i );
j = internal::random<Index>(0,A.cols()-1);
A.setRandom();
A.col(j).setZero();
VERIFY_IS_EQUAL( std::find_if(A.allCols().begin(), A.allCols().end(), [](typename ColMatrixType::ColXpr x) { return x.norm() == Scalar(0); })-A.allCols().begin(), j );
VERIFY_IS_EQUAL( std::find_if(A.colwise().begin(), A.colwise().end(), [](typename ColMatrixType::ColXpr x) { return x.norm() == Scalar(0); })-A.colwise().begin(), j );
}
#endif