SparseMatrix NeighborhoodBuilder::buildMatrix_(std::vector<T>& entries, const DenseMatrix& mat) const { // Make sure, that there are no duplicate entries std::sort(entries.begin(), entries.begin() + k_ * mat.rows(), triple_less); auto new_end = std::unique(entries.begin(), entries.begin() + k_ * mat.rows(), triple_equal); // An iterator for inserting the transposed entries // This will also serve as the new end pointer of the // entries array. auto new_it = new_end; // Symmetrize the matrix for(auto old_it = entries.begin(); old_it != new_end; ++old_it, ++new_it) { *new_it = T(old_it->col(), old_it->row(), old_it->value()); } // Build the temporary matrix for the results SparseMatrix result(mat.rowNames(), mat.rowNames()); // Fill the matrix and convert to CCS // new_it points to the end of valid matrix entries. result.matrix().setFromTriplets(entries.begin(), new_it); result.matrix().makeCompressed(); return result; }