Sparse move

This commit is contained in:
Charles Schlosser 2024-03-27 17:44:50 +00:00 committed by Antonio Sánchez
parent c8d368bdaf
commit 7b5d32b7c9
4 changed files with 84 additions and 6 deletions

View File

@ -788,8 +788,11 @@ class SparseMatrix : public SparseCompressedBase<SparseMatrix<Scalar_, Options_,
Base::operator=(other);
}
inline SparseMatrix(SparseMatrix&& other)
: Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
/** Move constructor */
inline SparseMatrix(SparseMatrix&& other) : SparseMatrix() { this->swap(other); }
template <typename OtherDerived>
inline SparseMatrix(SparseCompressedBase<OtherDerived>&& other) : SparseMatrix() {
*this = other.derived().markAsRValue();
}
@ -857,7 +860,10 @@ class SparseMatrix : public SparseCompressedBase<SparseMatrix<Scalar_, Options_,
return *this;
}
inline SparseMatrix& operator=(SparseMatrix&& other) { return *this = other.derived().markAsRValue(); }
inline SparseMatrix& operator=(SparseMatrix&& other) {
this->swap(other);
return *this;
}
#ifndef EIGEN_PARSED_BY_DOXYGEN
template <typename OtherDerived>
@ -872,6 +878,12 @@ class SparseMatrix : public SparseCompressedBase<SparseMatrix<Scalar_, Options_,
template <typename OtherDerived>
EIGEN_DONT_INLINE SparseMatrix& operator=(const SparseMatrixBase<OtherDerived>& other);
template <typename OtherDerived>
inline SparseMatrix& operator=(SparseCompressedBase<OtherDerived>&& other) {
*this = other.derived().markAsRValue();
return *this;
}
#ifndef EIGEN_NO_IO
friend std::ostream& operator<<(std::ostream& s, const SparseMatrix& m) {
EIGEN_DBG_SPARSE(

View File

@ -304,6 +304,24 @@ class SparseVector : public SparseCompressedBase<SparseVector<Scalar_, Options_,
return *this;
}
inline SparseVector(SparseVector&& other) : SparseVector() { this->swap(other); }
template <typename OtherDerived>
inline SparseVector(SparseCompressedBase<OtherDerived>&& other) : SparseVector() {
*this = other.derived().markAsRValue();
}
inline SparseVector& operator=(SparseVector&& other) {
this->swap(other);
return *this;
}
template <typename OtherDerived>
inline SparseVector& operator=(SparseCompressedBase<OtherDerived>&& other) {
*this = other.derived().markAsRValue();
return *this;
}
#ifndef EIGEN_PARSED_BY_DOXYGEN
template <typename Lhs, typename Rhs>
inline SparseVector& operator=(const SparseSparseProduct<Lhs, Rhs>& product) {

View File

@ -39,7 +39,7 @@ void sparse_basic(const SparseMatrixType& ref) {
typedef Matrix<Scalar, Dynamic, Dynamic> DenseMatrix;
typedef Matrix<Scalar, Dynamic, 1> DenseVector;
typedef Matrix<Scalar, Dynamic, Dynamic, SparseMatrixType::IsRowMajor ? RowMajor : ColMajor> CompatibleDenseMatrix;
Scalar eps = 1e-6;
Scalar eps = Scalar(1e-6);
Scalar s1 = internal::random<Scalar>();
{
@ -948,6 +948,27 @@ void sparse_basic(const SparseMatrixType& ref) {
SparseMatrixType m2(rows, 0);
m2.reserve(ArrayXi::Constant(m2.outerSize(), 1));
}
// test move
{
using TransposedType = SparseMatrix<Scalar, SparseMatrixType::IsRowMajor ? ColMajor : RowMajor,
typename SparseMatrixType::StorageIndex>;
DenseMatrix refMat1 = DenseMatrix::Random(rows, cols);
SparseMatrixType m1(rows, cols);
initSparse<Scalar>(density, refMat1, m1);
// test move ctor
SparseMatrixType m2(std::move(m1));
VERIFY_IS_APPROX(m2, refMat1);
// test move assignment
m1 = std::move(m2);
VERIFY_IS_APPROX(m1, refMat1);
// test move ctor (SparseMatrixBase)
TransposedType m3(std::move(m1.transpose()));
VERIFY_IS_APPROX(m3, refMat1.transpose());
// test move assignment (SparseMatrixBase)
m2 = std::move(m3.transpose());
VERIFY_IS_APPROX(m2, refMat1);
}
}
template <typename SparseMatrixType>
@ -994,7 +1015,7 @@ EIGEN_DECLARE_TEST(sparse_basic) {
g_dense_op_sparse_count = 0; // Suppresses compiler warning.
for (int i = 0; i < g_repeat; i++) {
int r = Eigen::internal::random<int>(1, 200), c = Eigen::internal::random<int>(1, 200);
if (Eigen::internal::random<int>(0, 4) == 0) {
if (Eigen::internal::random<int>(0, 3) == 0) {
r = c; // check square matrices in 25% of tries
}
EIGEN_UNUSED_VARIABLE(r + c);
@ -1011,7 +1032,7 @@ EIGEN_DECLARE_TEST(sparse_basic) {
r = Eigen::internal::random<int>(1, 100);
c = Eigen::internal::random<int>(1, 100);
if (Eigen::internal::random<int>(0, 4) == 0) {
if (Eigen::internal::random<int>(0, 3) == 0) {
r = c; // check square matrices in 25% of tries
}

View File

@ -108,6 +108,33 @@ void sparse_vector(int rows, int cols) {
VERIFY_IS_APPROX(refV3 = v1.transpose(), v1.toDense());
VERIFY_IS_APPROX(DenseVector(v1), v1.toDense());
// test move
{
SparseVectorType v3(std::move(v1));
VERIFY_IS_APPROX(v3, refV1);
v1 = v3;
}
{
SparseVectorType v3;
v3 = std::move(v1);
VERIFY_IS_APPROX(v3, refV1);
v1 = v3;
}
{
SparseVectorType v3(std::move(mv1));
VERIFY_IS_APPROX(v3, refV1);
mv1 = v3;
}
{
SparseVectorType v3;
v3 = std::move(mv1);
VERIFY_IS_APPROX(v3, refV1);
mv1 = v3;
}
// test conservative resize
{
std::vector<StorageIndex> inc;