Optimizations
This commit is contained in:
parent
5fddebd1bc
commit
e0303ff369
@ -42,9 +42,9 @@ Matrix2::Matrix2() {}
|
||||
Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) {
|
||||
this->nValues = nRows * nCols;
|
||||
if (this->nValues == 0)
|
||||
data = nullptr;
|
||||
this->data = nullptr;
|
||||
else {
|
||||
this->data = new float[nValues]();
|
||||
this->data = new float[this->nValues];
|
||||
this->externalData = false;
|
||||
}
|
||||
}
|
||||
@ -55,15 +55,41 @@ Matrix2::Matrix2(float* data, int nRows, int nCols)
|
||||
this->externalData = true;
|
||||
}
|
||||
|
||||
Matrix2::Matrix2(const Matrix2& m)
|
||||
: nRows(m.nRows), nCols(m.nCols), nValues(m.nValues) {
|
||||
if (this->nValues == 0)
|
||||
this->data = nullptr;
|
||||
else {
|
||||
this->data = new float[this->nValues];
|
||||
std::copy(m.data, m.data + nValues, this->data);
|
||||
}
|
||||
}
|
||||
|
||||
Matrix2& Matrix2::operator=(const Matrix2& m) {
|
||||
if (this != &m) {
|
||||
delete[] this->data; // Free the current memory
|
||||
|
||||
this->nRows = m.nRows;
|
||||
this->nCols = m.nCols;
|
||||
this->nValues = m.nValues;
|
||||
if (this->nValues == 0)
|
||||
this->data = nullptr;
|
||||
else {
|
||||
this->data = new float[this->nValues];
|
||||
std::copy(m.data, m.data + this->nValues, this->data);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Matrix2::~Matrix2() {
|
||||
if (data != nullptr && !this->externalData)
|
||||
if (!this->externalData)
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
Matrix2 Matrix2::Clone() const {
|
||||
Matrix2 r = Matrix2(this->nRows, this->nCols);
|
||||
for (int ix = 0; ix < this->nValues; ix++)
|
||||
r.data[ix] = this->data[ix];
|
||||
std::copy(this->data, this->data + this->nValues, r.data);
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -154,6 +180,12 @@ Matrix2 LinearAlgebra::Matrix2::operator+(const Matrix2& v) const {
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix2 Matrix2::operator+=(const Matrix2& v) {
|
||||
for (int ix = 0; ix < this->nValues; ix++)
|
||||
this->data[ix] += v.data[ix];
|
||||
return *this;
|
||||
}
|
||||
|
||||
Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const {
|
||||
Matrix2 r = Matrix2(this->nRows, B.nCols);
|
||||
|
||||
|
@ -33,6 +33,8 @@ class Matrix2 {
|
||||
Matrix2();
|
||||
Matrix2(int nRows, int nCols);
|
||||
Matrix2(float* data, int nRows, int nCols);
|
||||
Matrix2(const Matrix2& m);
|
||||
Matrix2& operator=(const Matrix2& other);
|
||||
|
||||
~Matrix2();
|
||||
|
||||
@ -55,6 +57,7 @@ class Matrix2 {
|
||||
/// @param m The matrix to add to this matrix
|
||||
/// @return The result of the addition
|
||||
Matrix2 operator+(const Matrix2& v) const;
|
||||
Matrix2 operator+=(const Matrix2& v);
|
||||
|
||||
Matrix2 operator*(const Matrix2& m) const;
|
||||
friend Matrix2 operator*(const Matrix2& m, float f) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user