From b9d81c915009e08a2397a2fc2d36a15d16b3b32f Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 6 Oct 2015 13:29:41 +0200 Subject: [PATCH] Add a functor to setFromTriplets to handle duplicated entries --- Eigen/src/SparseCore/SparseMatrix.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h index c8c31fd83..22a6bd803 100644 --- a/Eigen/src/SparseCore/SparseMatrix.h +++ b/Eigen/src/SparseCore/SparseMatrix.h @@ -437,6 +437,10 @@ class SparseMatrix template void setFromTriplets(const InputIterators& begin, const InputIterators& end); + template + void setFromTriplets(const InputIterators& begin, const InputIterators& end); + + template void sumupDuplicates(); //--- @@ -889,7 +893,7 @@ private: namespace internal { -template +template void set_from_triplets(const InputIterator& begin, const InputIterator& end, SparseMatrixType& mat, int Options = 0) { EIGEN_UNUSED_VARIABLE(Options); @@ -915,7 +919,7 @@ void set_from_triplets(const InputIterator& begin, const InputIterator& end, Spa trMat.insertBackUncompressed(it->row(),it->col()) = it->value(); // pass 3: - trMat.sumupDuplicates(); + trMat.template sumupDuplicates(); } // pass 4: transposed copy -> implicit sorting @@ -966,11 +970,24 @@ template template void SparseMatrix::setFromTriplets(const InputIterators& begin, const InputIterators& end) { - internal::set_from_triplets(begin, end, *this); + internal::set_from_triplets, internal::scalar_sum_op >(begin, end, *this); +} + +/** The same as setFromTriplets but when duplicates are met the functor \a DupFunctor is applied: + * \code + * value = DupFunctor()(OldValue, NewValue) + * \endcode + */ +template +template +void SparseMatrix::setFromTriplets(const InputIterators& begin, const InputIterators& end) +{ + internal::set_from_triplets, DupFunctor>(begin, end, *this); } /** \internal */ template +template void SparseMatrix::sumupDuplicates() { eigen_assert(!isCompressed()); @@ -989,7 +1006,7 @@ void SparseMatrix::sumupDuplicates() if(wi(i)>=start) { // we already meet this entry => accumulate it - m_data.value(wi(i)) += m_data.value(k); + m_data.value(wi(i)) = DupFunctor()(m_data.value(wi(i)), m_data.value(k)); } else {