Skip to content

Commit b2c717b

Browse files
authored
Copy constructor (#44)
* Adding copy constructor to DataTable * removing new keyword
1 parent 714769f commit b2c717b

2 files changed

Lines changed: 64 additions & 13 deletions

File tree

include/DataTable.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ namespace Data {
8686
virtual bool fromSQL(const std::string &dbfile,
8787
const std::string &tablename) = 0;
8888
virtual std::vector<std::vector<std::string>> getData() const = 0;
89+
virtual std::map<std::string, std::vector<std::string>>
90+
getDataAsMap() const = 0;
8991
virtual std::shared_ptr<IDataTable> getRow(int idx) const = 0;
9092
virtual std::vector<std::string>
9193
getColumn(std::string columnName) const = 0;
94+
virtual bool checkColumnExists(std::string columnName) const = 0;
9295
virtual std::vector<std::string> getColumnNames() const = 0;
9396
virtual std::shared_ptr<IDataTable>
9497
selectColumns(std::vector<std::string> columnNames) const = 0;
@@ -188,9 +191,12 @@ namespace Data {
188191

189192
void columnErrorCheck(std::string const columnName) const {
190193
if (this->data.find(columnName) == this->data.end()) {
191-
std::string message =
192-
"Invalid Column Name of " + columnName + " for DataTable.";
193-
throw new std::logic_error(message);
194+
std::string message = "Invalid Column Name of " + columnName +
195+
" for DataTable. Columns are: ";
196+
for (auto kv : this->data) {
197+
message += (kv.first + " ");
198+
}
199+
throw std::logic_error(message);
194200
}
195201
}
196202

@@ -200,7 +206,7 @@ namespace Data {
200206
std::to_string(idx) +
201207
" for DataTable with " +
202208
std::to_string(this->nrows()) + " rows.";
203-
throw new std::logic_error(message);
209+
throw std::logic_error(message);
204210
}
205211
}
206212

@@ -225,6 +231,8 @@ namespace Data {
225231
/// @brief Default constructor. Sets everything to default
226232
DataTable(){};
227233

234+
DataTable(const DataTable &dt);
235+
228236
/// @brief Constructor used to load a file to a DataTable
229237
/// @param filename file path as a string
230238
/// @param hasHeaders flag to read headers. False if no headers in file
@@ -282,6 +290,11 @@ namespace Data {
282290
return dat;
283291
}
284292

293+
std::map<std::string, std::vector<std::string>>
294+
getDataAsMap() const override {
295+
return this->data;
296+
}
297+
285298
/// @brief Return a row of data
286299
/// @param idx The index of the row of data looked for
287300
/// @return std::shared_ptr<IDataTable> object with the single row of
@@ -294,6 +307,8 @@ namespace Data {
294307
std::vector<std::string>
295308
getColumn(std::string columnName) const override;
296309

310+
bool checkColumnExists(std::string columnName) const override;
311+
297312
std::vector<std::string> getColumnNames() const override;
298313

299314
/// @brief Return a set of columns of data as a new

src/DataTable.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ namespace Data {
1818

1919
DataTable::DataTable(const std::string &filename, bool hasHeaders,
2020
char delim) {
21-
this->fromCSV(filename, hasHeaders, delim);
21+
if (!this->fromCSV(filename, hasHeaders, delim)) {
22+
throw std::invalid_argument("File " + filename +
23+
" could not be found!");
24+
}
2225
}
2326

2427
DataTable::DataTable(const std::string &dbfile,
2528
const std::string &tablename) {
26-
throw new std::logic_error("Not Implemented Yet");
29+
throw std::logic_error("Not Implemented Yet");
2730
}
2831

2932
DataTable::DataTable(std::map<std::string, std::vector<std::string>> data,
@@ -40,6 +43,22 @@ namespace Data {
4043
}
4144
}
4245

46+
DataTable::DataTable(const DataTable &dt) {
47+
std::map<std::string, std::vector<std::string>> data =
48+
dt.getDataAsMap();
49+
DataTableShape shape = dt.getShape();
50+
std::vector<std::string> headOrder = dt.getHeaders();
51+
this->data = data;
52+
this->shape = shape;
53+
if (headOrder.empty() || headOrder.size() != data.size()) {
54+
for (auto kv : this->data) {
55+
this->headerOrder.push_back(kv.first);
56+
}
57+
} else {
58+
this->headerOrder = headOrder;
59+
}
60+
}
61+
4362
std::vector<std::string> DataTable::loadRows(std::ifstream &csvStream) {
4463
std::vector<std::string> contents = {};
4564
std::string line;
@@ -73,6 +92,8 @@ namespace Data {
7392
}
7493
} else {
7594
for (std::string header : firstLine) {
95+
header.erase(std::remove(header.begin(), header.end(), '\"'),
96+
header.end());
7697
this->data[header] = {};
7798
this->headerOrder.push_back(header);
7899
}
@@ -93,7 +114,7 @@ namespace Data {
93114

94115
bool DataTable::fromSQL(const std::string &dbfile,
95116
const std::string &tablename) {
96-
throw new std::logic_error("Not Implemented Yet");
117+
throw std::logic_error("Not Implemented Yet");
97118
}
98119

99120
void DataTable::toCSV(const std::string &filename) const {
@@ -152,6 +173,16 @@ namespace Data {
152173
return this->data.at(columnName);
153174
}
154175

176+
bool DataTable::checkColumnExists(std::string columnName) const {
177+
std::vector<std::string> headers = this->getHeaders();
178+
for (std::string header : headers) {
179+
if (header.compare(columnName) == 0) {
180+
return true;
181+
}
182+
}
183+
return false;
184+
}
185+
155186
std::shared_ptr<IDataTable>
156187
DataTable::selectColumns(std::vector<std::string> columnNames) const {
157188
if (columnNames.empty()) {
@@ -295,7 +326,7 @@ namespace Data {
295326

296327
// throw error on invalid 1-1 join columns
297328
if (tableOneColumnNames.size() != tableTwoColumnNames.size()) {
298-
throw new std::logic_error("Joining columns are not one-to-one.");
329+
throw std::logic_error("Joining columns are not one-to-one.");
299330
}
300331

301332
// return nothing if the column names are empty
@@ -316,7 +347,7 @@ namespace Data {
316347
// Should never be true if the getColumn fails and the ColumnNames
317348
// Params are not empty
318349
if (t1Columns.empty() || t2Columns.empty()) {
319-
throw new std::logic_error("Joining columns not found in tables.");
350+
throw std::logic_error("Joining columns not found in tables.");
320351
}
321352

322353
std::vector<std::vector<int>> indices;
@@ -377,21 +408,21 @@ namespace Data {
377408
DataTable::leftJoin(std::shared_ptr<IDataTable> const tableTwo,
378409
std::string tableOneColumnName,
379410
std::string tableTwoColumnName) const {
380-
throw new std::logic_error("Not Implemented Yet");
411+
throw std::logic_error("Not Implemented Yet");
381412
}
382413

383414
std::shared_ptr<IDataTable>
384415
DataTable::rightJoin(std::shared_ptr<IDataTable> const tableTwo,
385416
std::string tableOneColumnName,
386417
std::string tableTwoColumnName) const {
387-
throw new std::logic_error("Not Implemented Yet");
418+
throw std::logic_error("Not Implemented Yet");
388419
}
389420

390421
std::shared_ptr<IDataTable>
391422
DataTable::outerJoin(std::shared_ptr<IDataTable> const tableTwo,
392423
std::string tableOneColumnName,
393424
std::string tableTwoColumnName) const {
394-
throw new std::logic_error("Not Implemented Yet");
425+
throw std::logic_error("Not Implemented Yet");
395426
}
396427

397428
void DataTable::dropColumns(std::vector<std::string> columnNames) {
@@ -401,7 +432,12 @@ namespace Data {
401432
}
402433

403434
void DataTable::dropColumn(std::string column) {
404-
columnErrorCheck(column);
435+
try {
436+
columnErrorCheck(column);
437+
} catch (std::logic_error ex) {
438+
throw ex;
439+
}
440+
405441
this->data.erase(column);
406442
for (std::vector<std::string>::iterator it = this->headerOrder.begin();
407443
it != this->headerOrder.end(); ++it) {

0 commit comments

Comments
 (0)