From 73682f37004008ae5e45e87eff856f754ad32087 Mon Sep 17 00:00:00 2001 From: David Cosgrove Date: Sun, 1 Mar 2026 06:18:20 +0000 Subject: [PATCH] Add explicit operator= and copy c'tors. (#9133) Co-authored-by: David Cosgrove --- Code/Geometry/Transform3D.h | 6 ++++++ Code/Geometry/catch_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ Code/Numerics/Matrix.h | 17 +++++++++++------ Code/Numerics/SquareMatrix.h | 8 +++++--- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/Code/Geometry/Transform3D.h b/Code/Geometry/Transform3D.h index c6acee8b9..4617c0ca9 100644 --- a/Code/Geometry/Transform3D.h +++ b/Code/Geometry/Transform3D.h @@ -44,6 +44,12 @@ class RDKIT_RDGEOMETRYLIB_EXPORT Transform3D d_data[id] = 1.0; } } + Transform3D(const Transform3D &t) = default; + Transform3D(Transform3D &&t) = default; + ~Transform3D() = default; + + Transform3D &operator=(const Transform3D &t) = default; + Transform3D &operator=(Transform3D &&t) = default; void setToIdentity(); diff --git a/Code/Geometry/catch_tests.cpp b/Code/Geometry/catch_tests.cpp index 871799d91..c39c09a49 100644 --- a/Code/Geometry/catch_tests.cpp +++ b/Code/Geometry/catch_tests.cpp @@ -9,6 +9,7 @@ // #include #include +#include #include TEST_CASE("construct Point2D from Point3D", "[point]") { @@ -142,4 +143,37 @@ TEST_CASE("compareParams") { CHECK(!grd.compareParams(grd2)); CHECK(!grd2.compareParams(grd)); } +} + +TEST_CASE("Test copy c'tor and operator=") { + RDGeom::Transform3D t1; + for (unsigned int i = 0; i < 4; ++i) { + for (unsigned int j = 0; j < 4; ++j) { + t1.setValUnchecked(i, j, i * j); + } + } + + RDGeom::Transform3D t2(t1); + for (unsigned int i = 0; i < 4; ++i) { + for (unsigned int j = 0; j < 4; ++j) { + CHECK(t1.getValUnchecked(i, j) == t2.getValUnchecked(i, j)); + } + } + + RDGeom::Transform3D t3; + for (unsigned int i = 0; i < 4; ++i) { + for (unsigned int j = 0; j < 4; ++j) { + if (i == j) { + CHECK(t3.getValUnchecked(i, j) == 1.0); + } else { + CHECK(t3.getValUnchecked(i, j) == 0.0); + } + } + } + t3 = t2; + for (unsigned int i = 0; i < 4; ++i) { + for (unsigned int j = 0; j < 4; ++j) { + CHECK(t3.getValUnchecked(i, j) == t2.getValUnchecked(i, j)); + } + } } \ No newline at end of file diff --git a/Code/Numerics/Matrix.h b/Code/Numerics/Matrix.h index 603ec5287..ea06d1fa3 100644 --- a/Code/Numerics/Matrix.h +++ b/Code/Numerics/Matrix.h @@ -71,9 +71,17 @@ class Matrix { d_dataSize * sizeof(TYPE)); d_data.reset(data); } - + Matrix(Matrix &&other) = default; virtual ~Matrix() {} + Matrix &operator=(const Matrix &other) { + if (this == &other) { + return *this; + } + return assign(other); + } + Matrix &operator=(Matrix &&other) = default; + //! returns the number of rows inline unsigned int numRows() const { return d_nRows; } @@ -159,7 +167,7 @@ class Matrix { } //! Matrix addition. - /*! Perform a element by element addition of other Matrix to this Matrix + /*! Perform an element by element addition of other Matrix to this Matrix */ virtual Matrix &operator+=(const Matrix &other) { PRECONDITION(d_nRows == other.numRows(), @@ -246,9 +254,6 @@ class Matrix { unsigned int d_nCols{0}; unsigned int d_dataSize{0}; DATA_SPTR d_data; - - private: - Matrix &operator=(const Matrix &other); }; //! Matrix multiplication @@ -276,7 +281,7 @@ Matrix &multiply(const Matrix &A, const Matrix &B, CHECK_INVARIANT(aRows == cRows, "Size mismatch during multiplication"); CHECK_INVARIANT(bCols == cCols, "Size mismatch during multiplication"); - // we have the sizes check do do the multiplication + // we have the sizes check so do the multiplication TYPE *cData = C.getData(); const TYPE *bData = B.getData(); const TYPE *aData = A.getData(); diff --git a/Code/Numerics/SquareMatrix.h b/Code/Numerics/SquareMatrix.h index 259bc1ee8..dbe5ed53d 100644 --- a/Code/Numerics/SquareMatrix.h +++ b/Code/Numerics/SquareMatrix.h @@ -27,9 +27,11 @@ class SquareMatrix : public Matrix { SquareMatrix(unsigned int N, typename Matrix::DATA_SPTR data) : Matrix(N, N, data) {} - // inline unsigned int size() const { - // return d_nRows; - // } + SquareMatrix(const SquareMatrix &B) = default; + SquareMatrix(SquareMatrix &&B) = default; + SquareMatrix &operator=(const SquareMatrix &B) = default; + SquareMatrix &operator=(SquareMatrix &&B) = default; + ~SquareMatrix() = default; SquareMatrix &operator*=(TYPE scale) override { Matrix::operator*=(scale);