CMU15-445_lab0 C++ primer

1. build the environment

官方使用的环境是ubuntu, 在非ubuntu的发行版上,我们可以使用docker去进行处理
在我们的项目的目录下

1
2
systemctl start docker 
sudo docker build . -t bustu

然后你就可以去喝咖啡了,这个一般会持续很长的时间

2. start code

1
2
docker create -t -i --name bustub -v $(pwd):/bustub bustub bash # 和本机的目录进行同步 
docker start -a -i bustub # 启动镜像

下面就可以在本机上编写代码,在docker中进行编译了

代码部分就是对matrix类的处理,实现里面的一些方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//===----------------------------------------------------------------------===//
//
// BusTub
//
// p0_starter.h
//
// Identification: src/include/primer/p0_starter.h
//
// Copyright (c) 2015-2020, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include <memory>
#include <stdexcept>
#include <vector>
#include <string.h>
#include "common/exception.h"

namespace bustub {

/**
* The Matrix type defines a common
* interface for matrix operations.
*/
template <typename T>
class Matrix {
protected:
/**
* TODO(P0): Add implementation
*
* Construct a new Matrix instance.
* @param rows The number of rows
* @param cols The number of columns
*
*/
Matrix(int rows, int cols) {
rows_ = rows;
cols_ = cols;
linear_ = new T[rows*cols];
memset(linear_, 0, sizeof(T)*(rows*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_;

public:
/** @return The number of rows in the matrix */
virtual auto GetRowCount() const -> int = 0;

/** @return The number of columns in the matrix */
virtual auto GetColumnCount() const -> int = 0;

/**
* Get the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @return The (i,j)th matrix element
* @throws OUT_OF_RANGE if either index is out of range
*/
virtual auto GetElement(int i, int j) const -> T = 0;

/**
* Set the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @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;

/**
* Fill the elements of the matrix from `source`.
*
* Throw OUT_OF_RANGE in the event that `source`
* does not contain the required number of elements.
*
* @param source The source container
* @throws OUT_OF_RANGE if `source` is incorrect size
*/
virtual void FillFrom(const std::vector<T> &source) = 0;

/**
* Destroy a matrix instance.
* TODO(P0): Add implementation
*/
virtual ~Matrix() {
free(linear_);
}
};

/**
* The RowMatrix type is a concrete matrix implementation.
* It implements the interface defined by the Matrix type.
*/
template <typename T>
class RowMatrix : public Matrix<T> {
public:
/**
* TODO(P0): Add implementation
*
* Construct a new RowMatrix instance.
* @param rows The number of rows
* @param cols The number of columns
*/
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;
}
}

/**
* TODO(P0): Add implementation
* @return The number of rows in the matrix
*/
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 this->cols_; }

/**
* TODO(P0): Add implementation
*
* Get the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @return The (i,j)th matrix element
* @throws OUT_OF_RANGE if either index is out of range
*/
auto GetElement(int i, int j) const -> T override {
if (i < 0 || j < 0 || i >= GetRowCount() || j >= GetColumnCount())
throw Exception(ExceptionType::OUT_OF_RANGE, "source does not contain the required number of elements");
else
return data_[i][j];
}

/**
* Set the (i,j)th matrix element.
*
* Throw OUT_OF_RANGE if either index is out of range.
*
* @param i The row index
* @param j The column index
* @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 {
if (i < 0 || j < 0 || i >= GetRowCount() || j >= GetColumnCount())
throw Exception(ExceptionType::OUT_OF_RANGE, "source does not contain the required number of elements");
data_[i][j] = val;
}

/**
* TODO(P0): Add implementation
*
* Fill the elements of the matrix from `source`.
*
* Throw OUT_OF_RANGE in the event that `source`
* does not contain the required number of elements.
*
* @param source The source container
* @throws OUT_OF_RANGE if `source` is incorrect size
*/
void FillFrom(const std::vector<T> &source) override {
if (static_cast<int>(source.size()) != GetColumnCount()*GetRowCount()) {
throw Exception(ExceptionType::OUT_OF_RANGE, "size error");
return;
}
int cnt = 0;
for (int i = 0; i < GetRowCount(); i++)
for (int j = 0; j < GetColumnCount(); j++) {
data_[i][j] = source[cnt++];
}
}

/**
* TODO(P0): Add implementation
*
* Destroy a RowMatrix instance.
*/
~RowMatrix() override {
free(data_);
}

private:
/**
* A 2D array containing the elements of the matrix in row-major format.
*
* TODO(P0):
* - Allocate the array of row pointers in the constructor.
* - Use these pointers to point to corresponding elements of the `linear` array.
* - Don't forget to deallocate the array in the destructor.
*/
T **data_;
};

/**
* The RowMatrixOperations class defines operations
* that may be performed on instances of `RowMatrix`.
*/
template <typename T>
class RowMatrixOperations {
public:
/**
* Compute (`matrixA` + `matrixB`) and return the result.
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @return The result of matrix addition
*/
static auto Add(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) -> std::unique_ptr<RowMatrix<T>> {
// TODO(P0): Add implementation
if (matrixA->GetColumnCount() != matrixB->GetColumnCount() || matrixA->GetRowCount() != matrixB->GetRowCount())
return std::unique_ptr<RowMatrix<T>>(nullptr);
int row = matrixA->GetRowCount();
int col = matrixA->GetColumnCount();
std::unique_ptr<RowMatrix<T>>ptr = std::make_unique<RowMatrix<T>>(row, col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) {
ptr->SetElement(i, j, matrixA->GetElement(i, j) + matrixB->GetElement(i, j));
}
return ptr;
}

/**
* Compute the matrix multiplication (`matrixA` * `matrixB` and return the result.
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @return The result of matrix multiplication
*/
static auto Multiply(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) -> std::unique_ptr<RowMatrix<T>> {
// TODO(P0): Add implementation
if (matrixA->GetColumnCount() != matrixB->GetRowCount())
return std::unique_ptr<RowMatrix<T>>(nullptr);
int row = matrixA->GetRowCount();
int col = matrixB->GetColumnCount();
std::unique_ptr<RowMatrix<T>>ptr = std::make_unique<RowMatrix<T>>(row, col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) {
int temp = 0;
for (int z = 0; z < matrixA->GetColumnCount();z++) {
temp += matrixA->GetElement(i, z) * matrixB->GetElement(z, j);
}
ptr->SetElement(i, j, temp);
}
return ptr;
}

/**
* Simplified General Matrix Multiply operation. Compute (`matrixA` * `matrixB` + `matrixC`).
* Return `nullptr` if dimensions mismatch for input matrices.
* @param matrixA Input matrix
* @param matrixB Input matrix
* @param matrixC Input matrix
* @return The result of general matrix multiply
*/
static auto GEMM(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB, const RowMatrix<T> *matrixC)
-> std::unique_ptr<RowMatrix<T>> {
// TODO(P0): Add implementation
if (matrixA->GetColumnCount() != matrixB->GetRowCount())
return std::unique_ptr<RowMatrix<T>>(nullptr);
if (matrixA->GetRowCount() != matrixC->GetRowCount() || matrixB->GetColumnCount() != matrixC->GetRowCount())
return std::unique_ptr<RowMatrix<T>>(nullptr);
auto mul = Multiply(matrixA, matrixB);
const RowMatrix<T>*p1 = mul.get();
return Add(p1, matrixC);
}
};
} // namespace bustub

3. test

将测试文件中的DISABLE删除就可以进行评分。