diff options
-rw-r--r-- | Eigen/src/Core/StlIterators.h | 40 | ||||
-rw-r--r-- | test/stl_iterators.cpp | 58 |
2 files changed, 68 insertions, 30 deletions
diff --git a/Eigen/src/Core/StlIterators.h b/Eigen/src/Core/StlIterators.h index 24eef1269..e39decc85 100644 --- a/Eigen/src/Core/StlIterators.h +++ b/Eigen/src/Core/StlIterators.h @@ -57,6 +57,11 @@ template<typename XprType> class pointer_based_stl_iterator { enum { is_lvalue = internal::is_lvalue<XprType>::value }; + typedef pointer_based_stl_iterator<typename internal::remove_const<XprType>::type > non_const_iterator; + typedef pointer_based_stl_iterator<typename internal::add_const<XprType>::type > const_iterator; + typedef typename internal::conditional<internal::is_const<XprType>::value,non_const_iterator,const_iterator>::type other_iterator; + friend const_iterator; + friend non_const_iterator; public: typedef Index difference_type; typedef typename XprType::Scalar value_type; @@ -64,12 +69,24 @@ 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; + 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(); } + pointer_based_stl_iterator(const non_const_iterator& other) + : m_ptr(other.m_ptr), m_incr(other.m_incr) + {} + + pointer_based_stl_iterator& operator=(const non_const_iterator& other) + { + m_ptr = other.m_ptr; + m_incr.setValue(other.m_incr); + return *this; + } + reference operator*() const { return *m_ptr; } reference operator[](Index i) const { return *(m_ptr+i*m_incr.value()); } pointer operator->() const { return m_ptr; } @@ -92,12 +109,23 @@ public: return (m_ptr - other.m_ptr)/m_incr.value(); } - 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; } + difference_type operator-(const other_iterator& other) const { + return (m_ptr - other.m_ptr)/m_incr.value(); + } + + bool operator==(const pointer_based_stl_iterator& other) const { return m_ptr == other.m_ptr; } + bool operator!=(const pointer_based_stl_iterator& other) const { return m_ptr != other.m_ptr; } + bool operator< (const pointer_based_stl_iterator& other) const { return m_ptr < other.m_ptr; } + bool operator<=(const pointer_based_stl_iterator& other) const { return m_ptr <= other.m_ptr; } + bool operator> (const pointer_based_stl_iterator& other) const { return m_ptr > other.m_ptr; } + bool operator>=(const pointer_based_stl_iterator& other) const { return m_ptr >= other.m_ptr; } + + bool operator==(const other_iterator& other) const { return m_ptr == other.m_ptr; } + bool operator!=(const other_iterator& other) const { return m_ptr != other.m_ptr; } + bool operator< (const other_iterator& other) const { return m_ptr < other.m_ptr; } + bool operator<=(const other_iterator& other) const { return m_ptr <= other.m_ptr; } + bool operator> (const other_iterator& other) const { return m_ptr > other.m_ptr; } + bool operator>=(const other_iterator& other) const { return m_ptr >= other.m_ptr; } protected: diff --git a/test/stl_iterators.cpp b/test/stl_iterators.cpp index ff89ec6ee..8c56db506 100644 --- a/test/stl_iterators.cpp +++ b/test/stl_iterators.cpp @@ -61,32 +61,42 @@ void check_begin_end_for_loop(Xpr xpr) i = 0; for(typename Xpr::const_iterator it = cxpr.begin(); it!=cxpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); } - // Needs to be uncommented while fixing bug 1619 - // i = 0; - // for(typename Xpr::const_iterator it = xpr.begin(); it!=xpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); } + i = 0; + for(typename Xpr::const_iterator it = xpr.begin(); it!=xpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); } + + { + // simple API check + typename Xpr::const_iterator cit; + cit = xpr.begin(); + cit = xpr.cbegin(); + } + + VERIFY( xpr.end() -xpr.begin() == xpr.size() ); + VERIFY( xpr.cend()-xpr.begin() == xpr.size() ); + VERIFY( xpr.end() -xpr.cbegin() == xpr.size() ); + VERIFY( xpr.cend()-xpr.cbegin() == xpr.size() ); if(xpr.size()>0) { VERIFY(xpr.begin() != xpr.end()); VERIFY(xpr.begin() < xpr.end()); VERIFY(xpr.begin() <= xpr.end()); VERIFY(!(xpr.begin() == xpr.end())); - VERIFY(!(xpr.begin() < xpr.end())); - VERIFY(!(xpr.begin() <= xpr.end())); + VERIFY(!(xpr.begin() > xpr.end())); + VERIFY(!(xpr.begin() >= xpr.end())); - // Needs to be uncommented while fixing bug 1619 - // VERIFY(xpr.cbegin() != xpr.end()); - // VERIFY(xpr.cbegin() < xpr.end()); - // VERIFY(xpr.cbegin() <= xpr.end()); - // VERIFY(!(xpr.cbegin() == xpr.end())); - // VERIFY(!(xpr.cbegin() < xpr.end())); - // VERIFY(!(xpr.cbegin() <= xpr.end())); - - // VERIFY(xpr.begin() != xpr.cend()); - // VERIFY(xpr.begin() < xpr.cend()); - // VERIFY(xpr.begin() <= xpr.cend()); - // VERIFY(!(xpr.begin() == xpr.cend())); - // VERIFY(!(xpr.begin() < xpr.cend())); - // VERIFY(!(xpr.begin() <= xpr.cend())); + VERIFY(xpr.cbegin() != xpr.end()); + VERIFY(xpr.cbegin() < xpr.end()); + VERIFY(xpr.cbegin() <= xpr.end()); + VERIFY(!(xpr.cbegin() == xpr.end())); + VERIFY(!(xpr.cbegin() > xpr.end())); + VERIFY(!(xpr.cbegin() >= xpr.end())); + + VERIFY(xpr.begin() != xpr.cend()); + VERIFY(xpr.begin() < xpr.cend()); + VERIFY(xpr.begin() <= xpr.cend()); + VERIFY(!(xpr.begin() == xpr.cend())); + VERIFY(!(xpr.begin() > xpr.cend())); + VERIFY(!(xpr.begin() >= xpr.cend())); } } @@ -419,11 +429,11 @@ void test_stl_iterators(int rows=Rows, int cols=Cols) // a valid type, the first overload is not viable, and the second // overload will be picked. template <class C, - class Iterator = decltype(::std::declval<const C&>().begin()), - class = decltype(::std::declval<const C&>().end()), - class = decltype(++::std::declval<Iterator&>()), - class = decltype(*::std::declval<Iterator>()), - class = typename C::const_iterator> + class Iterator = decltype(::std::declval<const C&>().begin()), + class = decltype(::std::declval<const C&>().end()), + class = decltype(++::std::declval<Iterator&>()), + class = decltype(*::std::declval<Iterator>()), + class = typename C::const_iterator> bool IsContainerType(int /* dummy */) { return true; } template <class C> |