Matrix(int rows, int cols) { rows_ = rows; cols_ = cols; int len = rows * cols; linear_ = new T[len]; memset(linear_, 0, sizeof(T) * len); }
TODO 2 Destroy a matrix instance
1
virtual ~Matrix() { delete[] linear_; }
TODO 3 Construct a new RowMatrix instance
1 2 3 4 5 6
RowMatrix(int rows, int cols) : Matrix<T>(rows, cols) { data_ = new T *[rows]; for (int i = 0; i < rows; ++i) { data_[i] = this->linear_ + i * cols; } }
T GetElement(int i, int j)constoverride{ if (i < 0 || i >= this->rows_ || j < 0 || j >= this->cols_) { throwException(ExceptionType::OUT_OF_RANGE, "Void Get Element: index is out of range."); } return data_[i][j]; }
voidSetElement(int i, int j, T val)override{ if (i < 0 || i >= this->rows_ || j < 0 || j >= this->cols_) { throwException(ExceptionType::OUT_OF_RANGE, "Void Set Element: index out of range"); } data_[i][j] = val; }
TODO 5 Fill from source container
1 2 3 4 5 6 7 8 9
voidFillFrom(const std::vector<T> &source)override{ int size = source.size(); if (size != this->rows_ * this->cols_) { throwException(ExceptionType::OUT_OF_RANGE, "source does not contain the required number of elements"); } for (int i = 0; i < size; ++i) { this->linear_[i] = source[i]; } }
int rows = matrixA->GetRowCount(); int cols = matrixA->GetColumnCount(); std::unique_ptr<RowMatrix<T>> ret = std::make_unique<RowMatrix<T>>(rows, cols);
for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { ret->SetElement(i, j, matrixA->GetElement(i, j) + matrixB->GetElement(i, j)); } } return ret; }