diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..608b1d449c8c6b055b67282a57f153e2b714712d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +build/ \ No newline at end of file diff --git a/hw1/README.md b/hw1/README.md index a85973693da4dde9b6ff7a8224da8b061c5f3095..35af13767e56e63b786f68e468c12f45530b8f26 100644 --- a/hw1/README.md +++ b/hw1/README.md @@ -19,9 +19,6 @@ 请在你们的代码中补充必要的注释,同时不要写不必要的注释。 -同时,我提供的测试不够完全:也就是就算通过了测试也无法保证代码的正确性, -可自行补充测试用例。 - ## 编译和测试 编译: ``` @@ -36,11 +33,11 @@ make mkdir build cd build make matrix_test -./test/matrix_test +./test/matrix_text ``` 你应该会发现运行结果中会显示有`DISABLE`的字样,这是因为有测试样例被禁用了。(这起始是一个好的开发习惯,不需要某个测试样例时,将它禁用,缩短测试时间)。 那该怎么解决呢? 打开`test/matrix_test.cpp`,你应该不难发现有几个函数长得像这样: -`TEST(StarterTest, DISABLED_InitializationTest) {`,可以看到,第二个参数开头带有`DISABLED_`,将它删掉,就可以解除禁用。 +`TEST(StarterTest, DISABLED_InitializationTest) {`,可以看到,第二个参数开头带有`DISABLED_`,将它删掉,就可以解除禁用。 \ No newline at end of file diff --git a/hw1/src/include/matrix.h b/hw1/src/include/matrix.h index 479ce80d72ff4c6b2787fd29aa5c30f30da42433..5994e0622f0a9b3cf5adbd6356f54d067881a909 100644 --- a/hw1/src/include/matrix.h +++ b/hw1/src/include/matrix.h @@ -16,11 +16,11 @@ #include #include #include -#include "common/exception.h" +#include "common/exception.h" namespace bustub { -/** +/** * The Matrix type defines a common * interface for matrix operations. */ @@ -35,23 +35,29 @@ class Matrix { * @param cols The number of columns * */ - Matrix(int rows, int cols) {} /** The number of rows in the matrix */ int rows_; /** The number of columns in the matrix */ int cols_; - - /** + /** * TODO(P0): Allocate the array in the constructor. * TODO(P0): Deallocate the array in the destructor. * A flattened array containing the elements of the matrix. */ T *linear_; + Matrix(int rows, int cols) { + rows_=rows; + cols_=cols; + linear_=(T*)malloc(sizeof(T)*rows*cols+1); + } + + + public: /** @return The number of rows in the matrix */ - virtual auto GetRowCount() const -> int = 0; + virtual auto GetRowCount()const -> int = 0; /** @return The number of columns in the matrix */ virtual auto GetColumnCount() const -> int = 0; @@ -78,7 +84,7 @@ class Matrix { * @param val The value to insert * @throws OUT_OF_RANGE if either index is out of range */ - virtual void SetElement(int i, int j, T val) = 0; + virtual void SetElement(int i, int j, T val)= 0; /** * Fill the elements of the matrix from `source`. @@ -89,7 +95,7 @@ class Matrix { * @param source The source container * @throws OUT_OF_RANGE if `source` is incorrect size */ - virtual void FillFrom(const std::vector &source) = 0; + virtual void FillFrom(const std::vector &source)= 0; /** * Destroy a matrix instance. @@ -112,19 +118,28 @@ class RowMatrix : public Matrix { * @param rows The number of rows * @param cols The number of columns */ - RowMatrix(int rows, int cols) : Matrix(rows, cols) {} + RowMatrix(int rows, int cols) : Matrix(rows, cols) { + + data_=new T* [rows]; + for(int i=0;i int override { return 0; } + auto GetRowCount() const -> int override { + return this->rows_; + } /** * TODO(P0): Add implementation * @return The number of columns in the matrix */ - auto GetColumnCount() const -> int override { return 0; } + auto GetColumnCount() const -> int override { + return this->cols_; + } /** * TODO(P0): Add implementation @@ -139,7 +154,13 @@ class RowMatrix : public Matrix { * @throws OUT_OF_RANGE if either index is out of range */ auto GetElement(int i, int j) const -> T override { - throw NotImplementedException{"RowMatrix::GetElement() not implemented."}; + if(i>=this->rows_||i<0||j>=this->cols_||j<0) + throw bustub::Exception(bustub::ExceptionType::OUT_OF_RANGE, + "Out of range"); + + return data_[i][j]; + + //throw NotImplementedException{"RowMatrix::GetElement() not implemented."}; } /** @@ -152,7 +173,14 @@ class RowMatrix : public Matrix { * @param val The value to insert * @throws OUT_OF_RANGE if either index is out of range */ - void SetElement(int i, int j, T val) override {} + void SetElement(int i, int j, T val) override { + + if(i>=this->rows_||i<0||j>=this->cols_||j<0) + throw bustub::Exception(bustub::ExceptionType::OUT_OF_RANGE, + "Out of range"); + + data_[i][j]=val; + } /** * TODO(P0): Add implementation @@ -166,7 +194,20 @@ class RowMatrix : public Matrix { * @throws OUT_OF_RANGE if `source` is incorrect size */ void FillFrom(const std::vector &source) override { - throw NotImplementedException{"RowMatrix::FillFrom() not implemented."}; + typename std::vector::const_iterator iter=source.begin(); + + for(int i=0;irows_;i++) + for(int j=0;jcols_;j++){ + data_[i][j]=*iter; + iter++; + } + if(iter!=source.end()){ + throw bustub::Exception + (bustub::ExceptionType::OUT_OF_RANGE,"Out of range"); + } + + + //throw NotImplementedException{"RowMatrix::FillFrom() not implemented."}; } /** @@ -203,8 +244,20 @@ class RowMatrixOperations { * @return The result of matrix addition */ static auto Add(const RowMatrix *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { + + auto cols=matrixA->GetColumnCount(); + auto rows=matrixB->GetRowCount(); + int val=0; + RowMatrix *p=new RowMatrix(rows,cols); + for(int i=0;iGetElement(i,j)+matrixB->GetElement(i,j); + p->SetElement(i,j,val); + } + // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); + //return std::unique_ptr>(nullptr); + return std::unique_ptr>(p); } /** @@ -215,8 +268,23 @@ class RowMatrixOperations { * @return The result of matrix multiplication */ static auto Multiply(const RowMatrix *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { + + auto rows=matrixA->GetRowCount(); + auto cols=matrixB->GetColumnCount(); + auto r=matrixA->GetColumnCount(); + int val; + RowMatrix *p=new RowMatrix(rows,cols); + for(int i=0;iGetElement(i,k))*(matrixB->GetElement(k,j)); + p->SetElement(i,j,val); + } + return std::unique_ptr>(p); + // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); + //return std::unique_ptr>(nullptr); } /** @@ -229,8 +297,21 @@ class RowMatrixOperations { */ static auto GEMM(const RowMatrix *matrixA, const RowMatrix *matrixB, const RowMatrix *matrixC) -> std::unique_ptr> { + int rows=matrixC->GetRowCount(); + int cols=matrixC->GetColumnCount(); + RowMatrix matrixr(rows,cols); + RowMatrix *p=&matrixr; + + matrixr=Multiply(matrixA,matrixB); + p=Add(p,matrixC); + + return std::unique_ptr>(p); + + // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); + //return std::unique_ptr>(nullptr); } }; } // namespace bustub + + diff --git a/hw1/test/matrix_test.cpp b/hw1/test/matrix_test.cpp index 7b3853bd4b85ae6b51f2d0e3fd9151580dba8247..453c7718646d639eccc60f64a9fa48d24812bb41 100644 --- a/hw1/test/matrix_test.cpp +++ b/hw1/test/matrix_test.cpp @@ -43,7 +43,7 @@ TEST(StarterTest, SampleTest) { } /** Test that matrix initialization works as expected */ -TEST(StarterTest, DISABLED_InitializationTest) { +TEST(StarterTest, /*DISABLED_*/InitializationTest) { auto matrix = std::make_unique>(2, 2); // Source contains too few elements @@ -69,7 +69,7 @@ TEST(StarterTest, DISABLED_InitializationTest) { } } -TEST(StarterTest, DISABLED_ElementAccessTest) { +TEST(StarterTest,/*DISABLED_*/ElementAccessTest) { auto matrix = std::make_unique>(2, 2); std::vector source(4); @@ -116,7 +116,7 @@ TEST(StarterTest, DISABLED_ElementAccessTest) { } /** Test that matrix addition works as expected */ -TEST(StarterTest, DISABLED_AdditionTest) { +TEST(StarterTest, /*DISABLED_*/AdditionTest) { auto matrix0 = std::make_unique>(3, 3); const std::vector source0{1, 4, 2, 5, 2, -1, 0, 3, 1}; @@ -156,7 +156,7 @@ TEST(StarterTest, DISABLED_AdditionTest) { } /** Test that matrix multiplication works as expected */ -TEST(StarterTest, DISABLED_MultiplicationTest) { +TEST(StarterTest, /*DISABLED_*/MultiplicationTest) { const std::vector source0{1, 2, 3, 4, 5, 6}; auto matrix0 = std::make_unique>(2, 3); matrix0->FillFrom(source0);