-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_table.cc
308 lines (286 loc) · 8.07 KB
/
db_table.cc
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// MP4 miller olsonn!
#include "db_table.hpp"
#include <stdexcept>
#include "iomanip"
const unsigned int kRowGrowthRate = 2;
// helper function for AddColumn:
void DbTable::Resize() {
for (auto& [id, row] : rows_) {
void** newrow = new void*[static_cast<unsigned long>(row_col_capacity_) *
kRowGrowthRate];
for (unsigned int i = 0; i < row_col_capacity_; i++) {
newrow[i] = row[i];
}
delete[] row;
row = newrow;
}
row_col_capacity_ *= kRowGrowthRate;
}
void DbTable::AddColumn(const std::pair<std::string, DataType>& col_desc) {
if (col_descs_.size() == row_col_capacity_) {
Resize();
}
col_descs_.push_back(col_desc);
DataType type = col_desc.second;
for (auto& [id, row] : rows_) {
if (type == DataType::kDouble) {
row[col_descs_.size() - 1] = static_cast<void*>(new double{0.0});
} else if (type == DataType::kString) {
row[col_descs_.size() - 1] = static_cast<void*>(new std::string{""});
} else if (type == DataType::kInt) {
row[col_descs_.size() - 1] = static_cast<void*>(new int{0});
}
}
}
void DbTable::DeleteColumnByIdx(unsigned int col_idx) {
if (col_idx >= col_descs_.size()) {
throw std::out_of_range("");
}
if (col_descs_.size() == 1 && !rows_.empty()) {
throw std::runtime_error("");
}
for (auto& [id, row] : rows_) {
DataType type = col_descs_[col_idx].second;
if (type == DataType::kString) {
delete static_cast<std::string*>(row[col_idx]);
}
if (type == DataType::kDouble) {
delete static_cast<double*>(row[col_idx]);
}
if (type == DataType::kInt) {
delete static_cast<int*>(row[col_idx]);
}
for (unsigned int i = col_idx + 1; i < col_descs_.size(); i++) {
row[i - 1] = row[i];
}
}
col_descs_.erase(col_descs_.begin() + col_idx);
}
void DbTable::AddRow(const std::initializer_list<std::string>& col_data) {
if (col_data.size() != col_descs_.size()) {
throw std::invalid_argument("");
}
rows_[next_unique_id_] = new void*[row_col_capacity_];
int i = 0;
for (const auto& data : col_data) {
DataType type = col_descs_.at(i).second;
if (type == DataType::kString) {
rows_[next_unique_id_][i] = static_cast<void*>(new std::string{data});
}
if (type == DataType::kDouble) {
rows_[next_unique_id_][i] =
static_cast<void*>(new double{std::stod(data)});
}
if (type == DataType::kInt) {
rows_[next_unique_id_][i] = static_cast<void*>(new int{std::stoi(data)});
}
i++;
}
next_unique_id_++;
}
void DbTable::DeleteRowById(unsigned int id) {
if (rows_.find(id) == rows_.end()) {
throw std::out_of_range("");
}
void** row_to_delete = rows_[id];
for (unsigned int i = 0; i < col_descs_.size(); i++) {
DataType type = col_descs_[i].second;
switch (type) {
case DataType::kString:
delete static_cast<std::string*>(row_to_delete[i]);
break;
case DataType::kDouble:
delete static_cast<double*>(row_to_delete[i]);
break;
case DataType::kInt:
delete static_cast<int*>(row_to_delete[i]);
break;
}
}
delete[] row_to_delete;
rows_.erase(id);
}
void DbTable::PrintColumn(std::ostream& os, const DbTable& table) const {
for (size_t i = 0; i < table.col_descs_.size(); i++) {
os << table.col_descs_[i].first << "(";
switch (table.col_descs_[i].second) {
case DataType::kString:
os << "std::string";
break;
case DataType::kDouble:
os << "double";
break;
case DataType::kInt:
os << "int";
break;
}
os << ")";
if (i != table.col_descs_.size() - 1) {
os << ", ";
}
}
os << std::endl;
}
void DbTable::PrintRow(std::ostream& os,
void** /*row*/,
const DbTable& table) const {
for (const auto& [id, row] : table.rows_) {
for (unsigned int i = 0; i < table.col_descs_.size(); i++) {
switch (table.col_descs_[i].second) {
case DataType::kString:
os << *(static_cast<std::string*>(row[i]));
break;
case DataType::kDouble:
os << *(static_cast<double*>(row[i]));
break;
case DataType::kInt:
os << *(static_cast<int*>(row[i]));
break;
}
if (i != table.col_descs_.size() - 1) {
os << ", ";
}
}
os << std::endl;
}
}
std::ostream& operator<<(std::ostream& os, const DbTable& table) {
for (const auto& col_desc : table.col_descs_) {
os << col_desc.first << "(";
switch (col_desc.second) {
case DataType::kString:
os << "std::string";
break;
case DataType::kDouble:
os << "double";
break;
case DataType::kInt:
os << "int";
break;
}
os << ")";
if (&col_desc != &table.col_descs_.back()) {
os << ", ";
}
}
for (const auto& [id, row] : table.rows_) {
for (unsigned int i = 0; i < table.col_descs_.size(); i++) {
switch (table.col_descs_[i].second) {
case DataType::kString:
os << *(static_cast<std::string*>(row[i]));
break;
case DataType::kDouble:
os << *(static_cast<double*>(row[i]));
break;
case DataType::kInt:
os << *(static_cast<int*>(row[i]));
break;
}
if (i != table.col_descs_.size() - 1) {
os << ", ";
}
}
os << std::endl;
}
return os;
}
DbTable::DbTable(const DbTable& rhs) {
next_unique_id_ = rhs.next_unique_id_;
row_col_capacity_ = rhs.row_col_capacity_;
col_descs_ = rhs.col_descs_;
for (const auto& [id, row] : rhs.rows_) {
void** new_row = new void*[row_col_capacity_];
for (unsigned int i = 0; i < col_descs_.size(); i++) {
DataType type = col_descs_[i].second;
switch (type) {
case DataType::kDouble:
new_row[i] = new double{*static_cast<double*>(row[i])};
break;
case DataType::kString:
new_row[i] = new std::string{*static_cast<std::string*>(row[i])};
break;
case DataType::kInt:
new_row[i] = new int{*static_cast<int*>(row[i])};
break;
}
}
rows_[id] = new_row;
}
}
// helper
void DbTable::DeleteRow(void** row) {
for (unsigned int i = 0; i < col_descs_.size(); i++) {
DataType type = col_descs_[i].second;
switch (type) {
case DataType::kString:
delete static_cast<std::string*>(row[i]);
break;
case DataType::kDouble:
delete static_cast<double*>(row[i]);
break;
case DataType::kInt:
delete static_cast<int*>(row[i]);
break;
}
}
delete[] row;
}
// helper
void** DbTable::CopyRow(void** src_row) {
void** new_row = new void*[row_col_capacity_];
for (unsigned int i = 0; i < col_descs_.size(); i++) {
DataType type = col_descs_[i].second;
switch (type) {
case DataType::kDouble:
new_row[i] = new double{*static_cast<double*>(src_row[i])};
break;
case DataType::kString:
new_row[i] = new std::string{*static_cast<std::string*>(src_row[i])};
break;
case DataType::kInt:
new_row[i] = new int{*static_cast<int*>(src_row[i])};
break;
}
}
return new_row;
}
DbTable& DbTable::operator=(const DbTable& rhs) {
if (this != &rhs) {
for (auto& [row_id, row] : rows_) {
DeleteRow(row);
}
rows_.clear();
next_unique_id_ = rhs.next_unique_id_;
row_col_capacity_ = rhs.row_col_capacity_;
col_descs_ = rhs.col_descs_;
for (const auto& [id, row] : rhs.rows_) {
rows_[id] = CopyRow(row);
}
}
return *this;
}
DbTable::~DbTable() {
for (auto& [row_id, row] : rows_) {
for (unsigned int i = 0; i < col_descs_.size(); i++) {
DataType type = col_descs_[i].second;
if (type == DataType::kInt) {
delete static_cast<int*>(row[i]);
}
if (type == DataType::kString) {
delete static_cast<std::string*>(row[i]);
}
if (type == DataType::kDouble) {
delete static_cast<double*>(row[i]);
}
}
delete[] row;
row = nullptr;
}
next_unique_id_ = 0;
row_col_capacity_ = 2;
}
// LAST Function
// std::ostream& operator<<(std::ostream& os, const DbTable& table) {
// if (table.rows_.empty()) return os;
// return os;
//}