@@ -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