From db9a9a12ba25353c94417dba24fab35cee16bde1 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Fri, 9 Nov 2018 16:49:19 +0100 Subject: bug #1619: make const and non-const iterators compatible --- Eigen/src/Core/StlIterators.h | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'Eigen/src/Core/StlIterators.h') 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 class pointer_based_stl_iterator { enum { is_lvalue = internal::is_lvalue::value }; + typedef pointer_based_stl_iterator::type > non_const_iterator; + typedef pointer_based_stl_iterator::type > const_iterator; + typedef typename internal::conditional::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::type pointer; typedef typename internal::conditional::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: -- cgit v1.2.3