aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2018-10-03 15:13:15 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2018-10-03 15:13:15 +0200
commit3e64b1fc8661316b20335cd287a726b4b37dff0c (patch)
tree5331e4aead7801d3111030a60dccbf59f12e8cdc
parent8a1e98240e6ac9f3456953b9e0b220c803acf9ed (diff)
Move iterators to internal, improve doc, make unit test c++03 friendly
-rw-r--r--Eigen/src/Core/DenseBase.h18
-rw-r--r--Eigen/src/Core/StlIterators.h165
-rw-r--r--Eigen/src/Core/util/ForwardDeclarations.h4
-rw-r--r--Eigen/src/plugins/ReshapedMethods.h8
-rw-r--r--test/stl_iterators.cpp392
5 files changed, 349 insertions, 238 deletions
diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h
index f69c0b2dc..b00f418c9 100644
--- a/Eigen/src/Core/DenseBase.h
+++ b/Eigen/src/Core/DenseBase.h
@@ -572,15 +572,25 @@ template<typename Derived> class DenseBase
}
EIGEN_DEVICE_FUNC void reverseInPlace();
+ #ifdef EIGEN_PARSED_BY_DOXYGEN
+ /** STL-like \link https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator RandomAccessIterator \endlink
+ * iterator type as returned by the begin() and end() methods.
+ */
+ typedef random_access_iterator_type iterator;
+ /** This is the const version of iterator (aka read-only) */
+ typedef random_access_iterator_type const_iterator;
+ #else
typedef typename internal::conditional< (Flags&DirectAccessBit)==DirectAccessBit,
- PointerBasedStlIterator<Derived>,
- DenseStlIterator<Derived>
+ internal::pointer_based_stl_iterator<Derived>,
+ internal::generic_randaccess_stl_iterator<Derived>
>::type iterator;
typedef typename internal::conditional< (Flags&DirectAccessBit)==DirectAccessBit,
- PointerBasedStlIterator<const Derived>,
- DenseStlIterator<const Derived>
+ internal::pointer_based_stl_iterator<const Derived>,
+ internal::generic_randaccess_stl_iterator<const Derived>
>::type const_iterator;
+ #endif
+
inline iterator begin();
inline const_iterator begin() const;
inline const_iterator cbegin() const;
diff --git a/Eigen/src/Core/StlIterators.h b/Eigen/src/Core/StlIterators.h
index 04d3d3c8d..8713964bd 100644
--- a/Eigen/src/Core/StlIterators.h
+++ b/Eigen/src/Core/StlIterators.h
@@ -9,20 +9,17 @@
namespace Eigen {
+namespace internal {
+
template<typename XprType,typename Derived>
-class IndexedBasedStlIteratorBase
+class indexed_based_stl_iterator_base
{
public:
typedef Index difference_type;
typedef std::random_access_iterator_tag iterator_category;
- IndexedBasedStlIteratorBase() : mp_xpr(0), m_index(0) {}
- IndexedBasedStlIteratorBase(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {}
-
- void swap(IndexedBasedStlIteratorBase& other) {
- std::swap(mp_xpr,other.mp_xpr);
- std::swap(m_index,other.m_index);
- }
+ indexed_based_stl_iterator_base() : mp_xpr(0), m_index(0) {}
+ indexed_based_stl_iterator_base(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {}
Derived& operator++() { ++m_index; return derived(); }
Derived& operator--() { --m_index; return derived(); }
@@ -30,22 +27,22 @@ public:
Derived operator++(int) { Derived prev(derived()); operator++(); return prev;}
Derived operator--(int) { Derived prev(derived()); operator--(); return prev;}
- friend Derived operator+(const IndexedBasedStlIteratorBase& a, Index b) { Derived ret(a.derived()); ret += b; return ret; }
- friend Derived operator-(const IndexedBasedStlIteratorBase& a, Index b) { Derived ret(a.derived()); ret -= b; return ret; }
- friend Derived operator+(Index a, const IndexedBasedStlIteratorBase& b) { Derived ret(b.derived()); ret += a; return ret; }
- friend Derived operator-(Index a, const IndexedBasedStlIteratorBase& b) { Derived ret(b.derived()); ret -= a; return ret; }
+ friend Derived operator+(const indexed_based_stl_iterator_base& a, Index b) { Derived ret(a.derived()); ret += b; return ret; }
+ friend Derived operator-(const indexed_based_stl_iterator_base& a, Index b) { Derived ret(a.derived()); ret -= b; return ret; }
+ friend Derived operator+(Index a, const indexed_based_stl_iterator_base& b) { Derived ret(b.derived()); ret += a; return ret; }
+ friend Derived operator-(Index a, const indexed_based_stl_iterator_base& b) { Derived ret(b.derived()); ret -= a; return ret; }
Derived& operator+=(Index b) { m_index += b; return derived(); }
Derived& operator-=(Index b) { m_index -= b; return derived(); }
- difference_type operator-(const IndexedBasedStlIteratorBase& other) const { eigen_assert(mp_xpr == other.mp_xpr);return m_index - other.m_index; }
+ difference_type operator-(const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr);return m_index - other.m_index; }
- bool operator==(const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
- bool operator!=(const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
- bool operator< (const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
- bool operator<=(const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
- bool operator> (const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
- bool operator>=(const IndexedBasedStlIteratorBase& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
+ bool operator==(const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
+ bool operator!=(const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
+ bool operator< (const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
+ bool operator<=(const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
+ bool operator> (const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
+ bool operator>=(const indexed_based_stl_iterator_base& other) { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
protected:
@@ -57,7 +54,7 @@ protected:
};
template<typename XprType>
-class PointerBasedStlIterator
+class pointer_based_stl_iterator
{
enum { is_lvalue = internal::is_lvalue<XprType>::value };
public:
@@ -67,8 +64,8 @@ public:
typedef typename internal::conditional<bool(is_lvalue), value_type*, const value_type*>::type pointer;
typedef typename internal::conditional<bool(is_lvalue), value_type&, const value_type&>::type reference;
- PointerBasedStlIterator() : m_ptr(0) {}
- PointerBasedStlIterator(XprType& xpr, Index index) : m_incr(xpr.innerStride())
+ pointer_based_stl_iterator() : m_ptr(0) {}
+ pointer_based_stl_iterator(XprType& xpr, Index index) : m_incr(xpr.innerStride())
{
m_ptr = xpr.data() + index * m_incr.value();
}
@@ -77,30 +74,30 @@ public:
reference operator[](Index i) const { return *(m_ptr+i*m_incr.value()); }
pointer operator->() const { return m_ptr; }
- PointerBasedStlIterator& operator++() { m_ptr += m_incr.value(); return *this; }
- PointerBasedStlIterator& operator--() { m_ptr -= m_incr.value(); return *this; }
+ pointer_based_stl_iterator& operator++() { m_ptr += m_incr.value(); return *this; }
+ pointer_based_stl_iterator& operator--() { m_ptr -= m_incr.value(); return *this; }
- PointerBasedStlIterator operator++(int) { PointerBasedStlIterator prev(*this); operator++(); return prev;}
- PointerBasedStlIterator operator--(int) { PointerBasedStlIterator prev(*this); operator--(); return prev;}
+ pointer_based_stl_iterator operator++(int) { pointer_based_stl_iterator prev(*this); operator++(); return prev;}
+ pointer_based_stl_iterator operator--(int) { pointer_based_stl_iterator prev(*this); operator--(); return prev;}
- friend PointerBasedStlIterator operator+(const PointerBasedStlIterator& a, Index b) { PointerBasedStlIterator ret(a); ret += b; return ret; }
- friend PointerBasedStlIterator operator-(const PointerBasedStlIterator& a, Index b) { PointerBasedStlIterator ret(a); ret -= b; return ret; }
- friend PointerBasedStlIterator operator+(Index a, const PointerBasedStlIterator& b) { PointerBasedStlIterator ret(b); ret += a; return ret; }
- friend PointerBasedStlIterator operator-(Index a, const PointerBasedStlIterator& b) { PointerBasedStlIterator ret(b); ret -= a; return ret; }
+ friend pointer_based_stl_iterator operator+(const pointer_based_stl_iterator& a, Index b) { pointer_based_stl_iterator ret(a); ret += b; return ret; }
+ friend pointer_based_stl_iterator operator-(const pointer_based_stl_iterator& a, Index b) { pointer_based_stl_iterator ret(a); ret -= b; return ret; }
+ friend pointer_based_stl_iterator operator+(Index a, const pointer_based_stl_iterator& b) { pointer_based_stl_iterator ret(b); ret += a; return ret; }
+ friend pointer_based_stl_iterator operator-(Index a, const pointer_based_stl_iterator& b) { pointer_based_stl_iterator ret(b); ret -= a; return ret; }
- PointerBasedStlIterator& operator+=(Index b) { m_ptr += b*m_incr.value(); return *this; }
- PointerBasedStlIterator& operator-=(Index b) { m_ptr -= b*m_incr.value(); return *this; }
+ pointer_based_stl_iterator& operator+=(Index b) { m_ptr += b*m_incr.value(); return *this; }
+ pointer_based_stl_iterator& operator-=(Index b) { m_ptr -= b*m_incr.value(); return *this; }
- difference_type operator-(const PointerBasedStlIterator& other) const {
+ difference_type operator-(const pointer_based_stl_iterator& other) const {
return (m_ptr - other.m_ptr)/m_incr.value();
}
- bool operator==(const PointerBasedStlIterator& other) { return m_ptr == other.m_ptr; }
- bool operator!=(const PointerBasedStlIterator& other) { return m_ptr != other.m_ptr; }
- bool operator< (const PointerBasedStlIterator& other) { return m_ptr < other.m_ptr; }
- bool operator<=(const PointerBasedStlIterator& other) { return m_ptr <= other.m_ptr; }
- bool operator> (const PointerBasedStlIterator& other) { return m_ptr > other.m_ptr; }
- bool operator>=(const PointerBasedStlIterator& other) { return m_ptr >= other.m_ptr; }
+ bool operator==(const pointer_based_stl_iterator& other) { return m_ptr == other.m_ptr; }
+ bool operator!=(const pointer_based_stl_iterator& other) { return m_ptr != other.m_ptr; }
+ bool operator< (const pointer_based_stl_iterator& other) { return m_ptr < other.m_ptr; }
+ bool operator<=(const pointer_based_stl_iterator& other) { return m_ptr <= other.m_ptr; }
+ bool operator> (const pointer_based_stl_iterator& other) { return m_ptr > other.m_ptr; }
+ bool operator>=(const pointer_based_stl_iterator& other) { return m_ptr >= other.m_ptr; }
protected:
@@ -109,7 +106,7 @@ protected:
};
template<typename XprType>
-class DenseStlIterator : public IndexedBasedStlIteratorBase<XprType, DenseStlIterator<XprType> >
+class generic_randaccess_stl_iterator : public indexed_based_stl_iterator_base<XprType, generic_randaccess_stl_iterator<XprType> >
{
public:
typedef typename XprType::Scalar value_type;
@@ -121,7 +118,7 @@ protected:
is_lvalue = internal::is_lvalue<XprType>::value
};
- typedef IndexedBasedStlIteratorBase<XprType,DenseStlIterator> Base;
+ typedef indexed_based_stl_iterator_base<XprType,generic_randaccess_stl_iterator> Base;
using Base::m_index;
using Base::mp_xpr;
@@ -135,19 +132,48 @@ public:
typedef typename internal::conditional<bool(is_lvalue), value_type *, const value_type *>::type pointer;
typedef typename internal::conditional<bool(is_lvalue), value_type&, read_only_ref_t>::type reference;
- DenseStlIterator() : Base() {}
- DenseStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
+ generic_randaccess_stl_iterator() : Base() {}
+ generic_randaccess_stl_iterator(XprType& xpr, Index index) : Base(xpr,index) {}
reference operator*() const { return (*mp_xpr)(m_index); }
reference operator[](Index i) const { return (*mp_xpr)(m_index+i); }
pointer operator->() const { return &((*mp_xpr)(m_index)); }
};
-template<typename XprType,typename Derived>
-void swap(IndexedBasedStlIteratorBase<XprType,Derived>& a, IndexedBasedStlIteratorBase<XprType,Derived>& b) {
- a.swap(b);
-}
+template<typename XprType, DirectionType Direction>
+class subvector_stl_iterator : public indexed_based_stl_iterator_base<XprType, subvector_stl_iterator<XprType,Direction> >
+{
+protected:
+
+ enum { is_lvalue = internal::is_lvalue<XprType>::value };
+
+ typedef indexed_based_stl_iterator_base<XprType,subvector_stl_iterator> Base;
+ using Base::m_index;
+ using Base::mp_xpr;
+
+ typedef typename internal::conditional<Direction==Vertical,typename XprType::ColXpr,typename XprType::RowXpr>::type SubVectorType;
+ typedef typename internal::conditional<Direction==Vertical,typename XprType::ConstColXpr,typename XprType::ConstRowXpr>::type ConstSubVectorType;
+
+public:
+ typedef typename internal::conditional<bool(is_lvalue), SubVectorType, ConstSubVectorType>::type value_type;
+ typedef value_type* pointer;
+ typedef value_type reference;
+
+ subvector_stl_iterator() : Base() {}
+ subvector_stl_iterator(XprType& xpr, Index index) : Base(xpr,index) {}
+
+ reference operator*() const { return (*mp_xpr).template subVector<Direction>(m_index); }
+ reference operator[](Index i) const { return (*mp_xpr).template subVector<Direction>(m_index+i); }
+ pointer operator->() const { return &((*mp_xpr).template subVector<Direction>(m_index)); }
+};
+
+} // namespace internal
+
+/** returns an iterator to the first element of the 1D vector or array
+ * \only_for_vectors
+ * \sa end(), cbegin()
+ */
template<typename Derived>
inline typename DenseBase<Derived>::iterator DenseBase<Derived>::begin()
{
@@ -155,12 +181,17 @@ inline typename DenseBase<Derived>::iterator DenseBase<Derived>::begin()
return iterator(derived(), 0);
}
+/** const version of begin() */
template<typename Derived>
inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::begin() const
{
return cbegin();
}
+/** returns a read-only const_iterator to the first element of the 1D vector or array
+ * \only_for_vectors
+ * \sa cend(), begin()
+ */
template<typename Derived>
inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cbegin() const
{
@@ -168,6 +199,10 @@ inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cbegin()
return const_iterator(derived(), 0);
}
+/** returns an iterator to the element following the last element of the 1D vector or array
+ * \only_for_vectors
+ * \sa begin(), cend()
+ */
template<typename Derived>
inline typename DenseBase<Derived>::iterator DenseBase<Derived>::end()
{
@@ -175,12 +210,17 @@ inline typename DenseBase<Derived>::iterator DenseBase<Derived>::end()
return iterator(derived(), size());
}
+/** const version of end() */
template<typename Derived>
inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::end() const
{
return cend();
}
+/** returns a read-only const_iterator to the element following the last element of the 1D vector or array
+ * \only_for_vectors
+ * \sa begin(), cend()
+ */
template<typename Derived>
inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cend() const
{
@@ -189,38 +229,11 @@ inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cend() co
}
template<typename XprType, DirectionType Direction>
-class SubVectorStlIterator : public IndexedBasedStlIteratorBase<XprType, SubVectorStlIterator<XprType,Direction> >
-{
-protected:
-
- enum { is_lvalue = internal::is_lvalue<XprType>::value };
-
- typedef IndexedBasedStlIteratorBase<XprType,SubVectorStlIterator> Base;
- using Base::m_index;
- using Base::mp_xpr;
-
- typedef typename internal::conditional<Direction==Vertical,typename XprType::ColXpr,typename XprType::RowXpr>::type SubVectorType;
- typedef typename internal::conditional<Direction==Vertical,typename XprType::ConstColXpr,typename XprType::ConstRowXpr>::type ConstSubVectorType;
-
-public:
- typedef typename internal::conditional<bool(is_lvalue), SubVectorType, ConstSubVectorType>::type value_type;
- typedef value_type* pointer;
- typedef value_type reference;
-
- SubVectorStlIterator() : Base() {}
- SubVectorStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
-
- reference operator*() const { return (*mp_xpr).template subVector<Direction>(m_index); }
- reference operator[](Index i) const { return (*mp_xpr).template subVector<Direction>(m_index+i); }
- pointer operator->() const { return &((*mp_xpr).template subVector<Direction>(m_index)); }
-};
-
-template<typename XprType, DirectionType Direction>
class SubVectorsProxy
{
public:
- typedef SubVectorStlIterator<XprType, Direction> iterator;
- typedef SubVectorStlIterator<const XprType, Direction> const_iterator;
+ typedef internal::subvector_stl_iterator<XprType, Direction> iterator;
+ typedef internal::subvector_stl_iterator<const XprType, Direction> const_iterator;
SubVectorsProxy(XprType& xpr) : m_xpr(xpr) {}
diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h
index 5fa7e5267..a9332e940 100644
--- a/Eigen/src/Core/util/ForwardDeclarations.h
+++ b/Eigen/src/Core/util/ForwardDeclarations.h
@@ -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> class DenseStlIterator;
-template<typename XprType> class PointerBasedStlIterator;
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 DecompositionType> struct kernel_retval_base;
template<typename DecompositionType> struct kernel_retval;
template<typename DecompositionType> struct image_retval_base;
diff --git a/Eigen/src/plugins/ReshapedMethods.h b/Eigen/src/plugins/ReshapedMethods.h
index ca9251ed2..2466d24cb 100644
--- a/Eigen/src/plugins/ReshapedMethods.h
+++ b/Eigen/src/plugins/ReshapedMethods.h
@@ -43,11 +43,11 @@ reshaped(NRowsType nRows, NColsType nCols) const;
/// \tparam Order specifies whether the coefficients should be processed in column-major-order (ColMajor), in row-major-order (RowMajor),
/// or follows the \em natural order of the nested expression (AutoOrder). The default is ColMajor.
///
-/// This overloads is essentially a shortcut for `A.reshaped<Order>(AutoSize,fix<1>).
+/// This overloads is essentially a shortcut for `A.reshaped<Order>(AutoSize,fix<1>)`.
///
-/// - If Order==ColMajor (the default), then it returns a column-vector from the stacked columns of \c *this.
-/// - If Order==RowMajor, then it returns a column-vector from the stacked rows of \c *this.
-/// - If Order==AutoOrder, then it returns a column-vector with elements stacked following the storage order of \c *this.
+/// - If `Order==ColMajor` (the default), then it returns a column-vector from the stacked columns of \c *this.
+/// - If `Order==RowMajor`, then it returns a column-vector from the stacked rows of \c *this.
+/// - If `Order==AutoOrder`, then it returns a column-vector with elements stacked following the storage order of \c *this.
/// This mode is the recommended one when the particular ordering of the element is not relevant.
///
/// Example:
diff --git a/test/stl_iterators.cpp b/test/stl_iterators.cpp
index 5d501dcd6..2dde0352a 100644
--- a/test/stl_iterators.cpp
+++ b/test/stl_iterators.cpp
@@ -17,20 +17,42 @@ make_reverse_iterator( Iterator i )
return std::reverse_iterator<Iterator>(i);
}
+#if !EIGEN_HAS_CXX11
+template<class ForwardIt>
+ForwardIt is_sorted_until(ForwardIt firstIt, ForwardIt lastIt)
+{
+ if (firstIt != lastIt) {
+ ForwardIt next = firstIt;
+ while (++next != lastIt) {
+ if (*next < *firstIt)
+ return next;
+ firstIt = next;
+ }
+ }
+ return lastIt;
+}
+template<class ForwardIt>
+bool is_sorted(ForwardIt firstIt, ForwardIt lastIt)
+{
+ return ::is_sorted_until(firstIt, lastIt) == lastIt;
+}
+#else
+using std::is_sorted;
+#endif
+
template<typename XprType>
-bool is_PointerBasedStlIterator(const PointerBasedStlIterator<XprType> &) { return true; }
+bool is_pointer_based_stl_iterator(const internal::pointer_based_stl_iterator<XprType> &) { return true; }
template<typename XprType>
-bool is_DenseStlIterator(const DenseStlIterator<XprType> &) { return true; }
+bool is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator<XprType> &) { return true; }
template<typename Scalar, int Rows, int Cols>
-void test_range_for_loop(int rows=Rows, int cols=Cols)
+void test_stl_iterators(int rows=Rows, int cols=Cols)
{
- using std::begin;
- using std::end;
-
typedef Matrix<Scalar,Rows,1> VectorType;
+ #if EIGEN_HAS_CXX11
typedef Matrix<Scalar,1,Cols> RowVectorType;
+ #endif
typedef Matrix<Scalar,Rows,Cols,ColMajor> ColMatrixType;
typedef Matrix<Scalar,Rows,Cols,RowMajor> RowMatrixType;
VectorType v = VectorType::Random(rows);
@@ -41,53 +63,90 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
Index i, j;
- VERIFY( is_PointerBasedStlIterator(v.begin()) );
- VERIFY( is_PointerBasedStlIterator(v.end()) );
- VERIFY( is_PointerBasedStlIterator(cv.begin()) );
- VERIFY( is_PointerBasedStlIterator(cv.end()) );
-
- j = internal::random<Index>(0,A.cols()-1);
- VERIFY( is_PointerBasedStlIterator(A.col(j).begin()) );
- VERIFY( is_PointerBasedStlIterator(A.col(j).end()) );
- VERIFY( is_PointerBasedStlIterator(cA.col(j).begin()) );
- VERIFY( is_PointerBasedStlIterator(cA.col(j).end()) );
-
- i = internal::random<Index>(0,A.rows()-1);
- VERIFY( is_PointerBasedStlIterator(A.row(i).begin()) );
- VERIFY( is_PointerBasedStlIterator(A.row(i).end()) );
- VERIFY( is_PointerBasedStlIterator(cA.row(i).begin()) );
- VERIFY( is_PointerBasedStlIterator(cA.row(i).end()) );
-
- VERIFY( is_PointerBasedStlIterator(A.reshaped().begin()) );
- VERIFY( is_PointerBasedStlIterator(A.reshaped().end()) );
- VERIFY( is_PointerBasedStlIterator(cA.reshaped().begin()) );
- VERIFY( is_PointerBasedStlIterator(cA.reshaped().end()) );
-
- VERIFY( is_PointerBasedStlIterator(B.template reshaped<AutoOrder>().begin()) );
- VERIFY( is_PointerBasedStlIterator(B.template reshaped<AutoOrder>().end()) );
-
- VERIFY( is_DenseStlIterator(A.template reshaped<RowMajor>().begin()) );
- VERIFY( is_DenseStlIterator(A.template reshaped<RowMajor>().end()) );
-
+ // Check we got a fast pointer-based iterator when expected
+ {
+ VERIFY( is_pointer_based_stl_iterator(v.begin()) );
+ VERIFY( is_pointer_based_stl_iterator(v.end()) );
+ VERIFY( is_pointer_based_stl_iterator(cv.begin()) );
+ VERIFY( is_pointer_based_stl_iterator(cv.end()) );
+
+ j = internal::random<Index>(0,A.cols()-1);
+ VERIFY( is_pointer_based_stl_iterator(A.col(j).begin()) );
+ VERIFY( is_pointer_based_stl_iterator(A.col(j).end()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.col(j).begin()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.col(j).end()) );
+
+ i = internal::random<Index>(0,A.rows()-1);
+ VERIFY( is_pointer_based_stl_iterator(A.row(i).begin()) );
+ VERIFY( is_pointer_based_stl_iterator(A.row(i).end()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.row(i).begin()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.row(i).end()) );
+
+ VERIFY( is_pointer_based_stl_iterator(A.reshaped().begin()) );
+ VERIFY( is_pointer_based_stl_iterator(A.reshaped().end()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.reshaped().begin()) );
+ VERIFY( is_pointer_based_stl_iterator(cA.reshaped().end()) );
+
+ VERIFY( is_pointer_based_stl_iterator(B.template reshaped<AutoOrder>().begin()) );
+ VERIFY( is_pointer_based_stl_iterator(B.template reshaped<AutoOrder>().end()) );
+
+ VERIFY( is_generic_randaccess_stl_iterator(A.template reshaped<RowMajor>().begin()) );
+ VERIFY( is_generic_randaccess_stl_iterator(A.template reshaped<RowMajor>().end()) );
+ }
+
#if EIGEN_HAS_CXX11
- i = 0;
- for(auto x : v) { VERIFY_IS_EQUAL(x,v[i++]); }
+ // check swappable
+ {
+ using std::swap;
+ // pointer-based
+ {
+ VectorType v_copy = v;
+ auto a = v.begin();
+ auto b = v.end()-1;
+ swap(a,b);
+ VERIFY_IS_EQUAL(v,v_copy);
+ VERIFY_IS_EQUAL(*b,*v.begin());
+ VERIFY_IS_EQUAL(*b,v(0));
+ VERIFY_IS_EQUAL(*a,v.end()[-1]);
+ VERIFY_IS_EQUAL(*a,v(last));
+ }
- j = internal::random<Index>(0,A.cols()-1);
- i = 0;
- for(auto x : A.col(j)) { VERIFY_IS_EQUAL(x,A(i++,j)); }
+ // generic
+ {
+ RowMatrixType B_copy = B;
+ auto Br = B.reshaped();
+ auto a = Br.begin();
+ auto b = Br.end()-1;
+ swap(a,b);
+ VERIFY_IS_EQUAL(B,B_copy);
+ VERIFY_IS_EQUAL(*b,*Br.begin());
+ VERIFY_IS_EQUAL(*b,Br(0));
+ VERIFY_IS_EQUAL(*a,Br.end()[-1]);
+ VERIFY_IS_EQUAL(*a,Br(last));
+ }
+ }
- i = 0;
- for(auto x : (v+A.col(j))) { VERIFY_IS_APPROX(x,v(i)+A(i,j)); ++i; }
+ // check non-const iterator with for-range loops
+ {
+ i = 0;
+ for(auto x : v) { VERIFY_IS_EQUAL(x,v[i++]); }
- j = 0;
- i = internal::random<Index>(0,A.rows()-1);
- for(auto x : A.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
+ j = internal::random<Index>(0,A.cols()-1);
+ i = 0;
+ for(auto x : A.col(j)) { VERIFY_IS_EQUAL(x,A(i++,j)); }
- i = 0;
- for(auto x : A.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
+ i = 0;
+ for(auto x : (v+A.col(j))) { VERIFY_IS_APPROX(x,v(i)+A(i,j)); ++i; }
- // check const_iterator
+ j = 0;
+ i = internal::random<Index>(0,A.rows()-1);
+ for(auto x : A.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
+
+ i = 0;
+ for(auto x : A.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
+ }
+
+ // same for const_iterator
{
i = 0;
for(auto x : cv) { VERIFY_IS_EQUAL(x,v[i++]); }
@@ -100,49 +159,51 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
for(auto x : cA.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
}
- Matrix<Scalar,Dynamic,Dynamic,ColMajor> Bc = B;
- i = 0;
- for(auto x : B.reshaped()) { VERIFY_IS_EQUAL(x,Bc(i++)); }
-
- VectorType w(v.size());
- i = 0;
- for(auto& x : w) { x = v(i++); }
- VERIFY_IS_EQUAL(v,w);
-
+ // check reshaped() on row-major
{
- j = internal::random<Index>(0,A.cols()-1);
- auto it = A.col(j).begin();
- for(i=0;i<rows;++i) {
- VERIFY_IS_EQUAL(it[i],A(i,j));
- }
+ i = 0;
+ Matrix<Scalar,Dynamic,Dynamic,ColMajor> Bc = B;
+ for(auto x : B.reshaped()) { VERIFY_IS_EQUAL(x,Bc(i++)); }
}
+ // check write access
{
- i = internal::random<Index>(0,A.rows()-1);
- auto it = A.row(i).begin();
- for(j=0;j<cols;++j) { VERIFY_IS_EQUAL(it[j],A(i,j)); }
+ VectorType w(v.size());
+ i = 0;
+ for(auto& x : w) { x = v(i++); }
+ VERIFY_IS_EQUAL(v,w);
}
+ // check for dangling pointers
{
- j = internal::random<Index>(0,A.cols()-1);
- // this would produce a dangling pointer:
- // auto it = (A+2*A).col(j).begin();
- // we need to name the temporary expression:
- auto tmp = (A+2*A).col(j);
- auto it = tmp.begin();
- for(i=0;i<rows;++i) {
- VERIFY_IS_APPROX(it[i],3*A(i,j));
+ // no dangling because pointer-based
+ {
+ j = internal::random<Index>(0,A.cols()-1);
+ auto it = A.col(j).begin();
+ for(i=0;i<rows;++i) {
+ VERIFY_IS_EQUAL(it[i],A(i,j));
+ }
}
- }
-
- // {
- // j = internal::random<Index>(0,A.cols()-1);
- // auto it = (A+2*A).col(j).begin();
- // for(i=0;i<rows;++i) {
- // VERIFY_IS_APPROX(it[i],3*A(i,j));
- // }
- // }
+ // no dangling because pointer-based
+ {
+ i = internal::random<Index>(0,A.rows()-1);
+ auto it = A.row(i).begin();
+ for(j=0;j<cols;++j) { VERIFY_IS_EQUAL(it[j],A(i,j)); }
+ }
+
+ {
+ j = internal::random<Index>(0,A.cols()-1);
+ // this would produce a dangling pointer:
+ // auto it = (A+2*A).col(j).begin();
+ // we need to name the temporary expression:
+ auto tmp = (A+2*A).col(j);
+ auto it = tmp.begin();
+ for(i=0;i<rows;++i) {
+ VERIFY_IS_APPROX(it[i],3*A(i,j));
+ }
+ }
+ }
#endif
if(rows>=3) {
@@ -155,108 +216,125 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
VERIFY_IS_EQUAL((A.allCols().begin()+cols/2)[1], A.col(cols/2+1));
}
- if(rows>=2)
+ // check std::sort
{
- v(1) = v(0)-Scalar(1);
- VERIFY(!std::is_sorted(begin(v),end(v)));
- }
- std::sort(begin(v),end(v));
- VERIFY(std::is_sorted(begin(v),end(v)));
- VERIFY(!std::is_sorted(make_reverse_iterator(end(v)),make_reverse_iterator(begin(v))));
-
- // std::sort with pointer-based iterator and default increment
- {
- j = internal::random<Index>(0,A.cols()-1);
- // std::sort(begin(A.col(j)),end(A.col(j))); // does not compile because this returns const iterators
- typename ColMatrixType::ColXpr Acol = A.col(j);
- std::sort(begin(Acol),end(Acol));
- VERIFY(std::is_sorted(Acol.cbegin(),Acol.cend()));
- A.setRandom();
-
- std::sort(A.col(j).begin(),A.col(j).end());
- VERIFY(std::is_sorted(A.col(j).cbegin(),A.col(j).cend()));
- A.setRandom();
- }
+ // first check that is_sorted returns false when required
+ if(rows>=2)
+ {
+ v(1) = v(0)-Scalar(1);
+ #if EIGEN_HAS_CXX11
+ VERIFY(!is_sorted(std::begin(v),std::end(v)));
+ #else
+ VERIFY(!is_sorted(v.cbegin(),v.cend()));
+ #endif
+ }
- // std::sort with pointer-based iterator and runtime increment
- {
- i = internal::random<Index>(0,A.rows()-1);
- typename ColMatrixType::RowXpr Arow = A.row(i);
- VERIFY_IS_EQUAL( std::distance(begin(Arow),end(Arow)), cols);
- std::sort(begin(Arow),end(Arow));
- VERIFY(std::is_sorted(Arow.cbegin(),Arow.cend()));
- A.setRandom();
+ // on a vector
+ {
+ std::sort(v.begin(),v.end());
+ VERIFY(is_sorted(v.begin(),v.end()));
+ VERIFY(!::is_sorted(make_reverse_iterator(v.end()),make_reverse_iterator(v.begin())));
+ }
- std::sort(A.row(i).begin(),A.row(i).end());
- VERIFY(std::is_sorted(A.row(i).cbegin(),A.row(i).cend()));
- A.setRandom();
- }
+ // on a column of a column-major matrix -> pointer-based iterator and default increment
+ {
+ j = internal::random<Index>(0,A.cols()-1);
+ // std::sort(begin(A.col(j)),end(A.col(j))); // does not compile because this returns const iterators
+ typename ColMatrixType::ColXpr Acol = A.col(j);
+ std::sort(Acol.begin(),Acol.end());
+ VERIFY(is_sorted(Acol.cbegin(),Acol.cend()));
+ A.setRandom();
+
+ std::sort(A.col(j).begin(),A.col(j).end());
+ VERIFY(is_sorted(A.col(j).cbegin(),A.col(j).cend()));
+ A.setRandom();
+ }
- // std::sort with generic iterator
- {
- auto B1 = B.reshaped();
- std::sort(begin(B1),end(B1));
- VERIFY(std::is_sorted(B1.cbegin(),B1.cend()));
- B.setRandom();
+ // on a row of a rowmajor matrix -> pointer-based iterator and runtime increment
+ {
+ i = internal::random<Index>(0,A.rows()-1);
+ typename ColMatrixType::RowXpr Arow = A.row(i);
+ VERIFY_IS_EQUAL( std::distance(Arow.begin(),Arow.end()), cols);
+ std::sort(Arow.begin(),Arow.end());
+ VERIFY(is_sorted(Arow.cbegin(),Arow.cend()));
+ A.setRandom();
+
+ std::sort(A.row(i).begin(),A.row(i).end());
+ VERIFY(is_sorted(A.row(i).cbegin(),A.row(i).cend()));
+ A.setRandom();
+ }
- // assertion because nested expressions are different
- // std::sort(B.reshaped().begin(),B.reshaped().end());
- // VERIFY(std::is_sorted(B.reshaped().cbegin(),B.reshaped().cend()));
- // B.setRandom();
+ // with a generic iterator
+ {
+ Reshaped<RowMatrixType,RowMatrixType::SizeAtCompileTime,1> B1 = B.reshaped();
+ std::sort(B1.begin(),B1.end());
+ VERIFY(is_sorted(B1.cbegin(),B1.cend()));
+ B.setRandom();
+
+ // assertion because nested expressions are different
+ // std::sort(B.reshaped().begin(),B.reshaped().end());
+ // VERIFY(is_sorted(B.reshaped().cbegin(),B.reshaped().cend()));
+ // B.setRandom();
+ }
}
+ // check with partial_sum
{
j = internal::random<Index>(0,A.cols()-1);
typename ColMatrixType::ColXpr Acol = A.col(j);
- std::partial_sum(begin(Acol), end(Acol), begin(v));
+ std::partial_sum(Acol.begin(), Acol.end(), v.begin());
VERIFY_IS_EQUAL(v(seq(1,last)), v(seq(0,last-1))+Acol(seq(1,last)));
// inplace
- std::partial_sum(begin(Acol), end(Acol), begin(Acol));
+ std::partial_sum(Acol.begin(), Acol.end(), Acol.begin());
VERIFY_IS_EQUAL(v, Acol);
}
+ // stress random access as required by std::nth_element
if(rows>=3)
{
- // stress random access
v.setRandom();
VectorType v1 = v;
- std::sort(begin(v1),end(v1));
+ std::sort(v1.begin(),v1.end());
std::nth_element(v.begin(), v.begin()+rows/2, v.end());
VERIFY_IS_APPROX(v1(rows/2), v(rows/2));
v.setRandom();
v1 = v;
- std::sort(begin(v1)+rows/2,end(v1));
+ std::sort(v1.begin()+rows/2,v1.end());
std::nth_element(v.begin()+rows/2, v.begin()+rows/4, v.end());
VERIFY_IS_APPROX(v1(rows/4), v(rows/4));
}
#if EIGEN_HAS_CXX11
- j = 0;
- for(auto c : A.allCols()) { 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; }
+ // 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; }
+ j = 0;
+ for(auto c : B.allCols()) { VERIFY_IS_APPROX(c.sum(), B.col(j).sum()); ++j; }
- j = 0;
- for(auto c : B.allCols()) {
- i = 0;
- for(auto& x : c) {
- VERIFY_IS_EQUAL(x, B(i,j));
- x = A(i,j);
- ++i;
+ j = 0;
+ for(auto c : B.allCols()) {
+ i = 0;
+ for(auto& x : c) {
+ VERIFY_IS_EQUAL(x, B(i,j));
+ x = A(i,j);
+ ++i;
+ }
+ ++j;
}
- ++j;
+ VERIFY_IS_APPROX(A,B);
+ B.setRandom();
+
+ i = 0;
+ for(auto r : A.allRows()) { 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; }
}
- VERIFY_IS_APPROX(A,B);
- B = Bc; // restore B
-
- i = 0;
- for(auto r : A.allRows()) { 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; }
+ // check rows/cols iterators with STL algorithms
{
RowVectorType row = RowVectorType::Random(cols);
A.rowwise() = row;
@@ -265,6 +343,16 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
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()); }) );
+
+ 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 );
+
+ 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 );
}
#endif
@@ -273,9 +361,9 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
EIGEN_DECLARE_TEST(stl_iterators)
{
for(int i = 0; i < g_repeat; i++) {
- CALL_SUBTEST_1(( test_range_for_loop<double,2,3>() ));
- CALL_SUBTEST_1(( test_range_for_loop<float,7,5>() ));
- CALL_SUBTEST_1(( test_range_for_loop<int,Dynamic,Dynamic>(internal::random<int>(5,10), internal::random<int>(5,10)) ));
- CALL_SUBTEST_1(( test_range_for_loop<int,Dynamic,Dynamic>(internal::random<int>(10,200), internal::random<int>(10,200)) ));
+ CALL_SUBTEST_1(( test_stl_iterators<double,2,3>() ));
+ CALL_SUBTEST_1(( test_stl_iterators<float,7,5>() ));
+ CALL_SUBTEST_1(( test_stl_iterators<int,Dynamic,Dynamic>(internal::random<int>(5,10), internal::random<int>(5,10)) ));
+ CALL_SUBTEST_1(( test_stl_iterators<int,Dynamic,Dynamic>(internal::random<int>(10,200), internal::random<int>(10,200)) ));
}
}