diff --git a/Eigen/src/SparseCore/SparseCompressedBase.h b/Eigen/src/SparseCore/SparseCompressedBase.h index 9f2243796..b94079b8f 100644 --- a/Eigen/src/SparseCore/SparseCompressedBase.h +++ b/Eigen/src/SparseCore/SparseCompressedBase.h @@ -284,6 +284,13 @@ class SparseCompressedBase::InnerIterator { inline operator bool() const { return (m_id < m_end); } + // Position-based equality (bug #1192 — without these, == falls back to bool conversion). + inline bool operator==(const InnerIterator& other) const { + eigen_assert(m_values == other.m_values && "comparing iterators from different sources"); + return m_outer.value() == other.m_outer.value() && m_id == other.m_id; + } + inline bool operator!=(const InnerIterator& other) const { return !(*this == other); } + protected: const Scalar* m_values; const StorageIndex* m_indices; @@ -351,6 +358,12 @@ class SparseCompressedBase::ReverseInnerIterator { inline operator bool() const { return (m_id > m_start); } + inline bool operator==(const ReverseInnerIterator& other) const { + eigen_assert(m_values == other.m_values && "comparing iterators from different sources"); + return m_outer.value() == other.m_outer.value() && m_id == other.m_id; + } + inline bool operator!=(const ReverseInnerIterator& other) const { return !(*this == other); } + protected: const Scalar* m_values; const StorageIndex* m_indices; diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 94f5717a4..f3f0a1878 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -981,6 +981,32 @@ void sparse_basic(const SparseMatrixType& ref) { iters[1] = IteratorType(m2, m2.outerSize() - 1); } + // test InnerIterator equality (bug #1192) + { + typedef typename SparseMatrixType::InnerIterator IteratorType; + SparseMatrixType m2(rows, cols); + DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); + initSparse(density, refMat2, m2); + Index outer_with_two = -1; + for (Index o = 0; o < m2.outerSize(); ++o) + if (m2.innerVector(o).nonZeros() >= 2) { + outer_with_two = o; + break; + } + if (outer_with_two >= 0) { + IteratorType a(m2, outer_with_two), b(m2, outer_with_two); + VERIFY(a == b); + ++b; + VERIFY(a != b); + VERIFY(!(a == b)); + ++a; + VERIFY(a == b); + while (a) ++a; + while (b) ++b; + VERIFY(a == b); + } + } + // test reserve with empty rows/columns { SparseMatrixType m1(0, cols);