5 #include "../Io/FileTools.h" 6 #include "../Text/StringTokenizer.h" 7 #include "../Text/TextTools.h" 25 for (
size_t i = 0; i < nCol; i++)
27 data_[i].resize(nRow);
41 nCol_(colNames.size()),
42 data_(colNames.size()),
46 for (
size_t i = 0; i <
nCol_; i++)
48 data_[i].resize(nRow);
56 nCol_(colNames.size()),
57 data_(colNames.size()),
101 if (colIndex >=
nCol_)
103 if (rowIndex >=
data_[colIndex].size())
105 return data_[colIndex][rowIndex];
110 if (colIndex >=
nCol_)
112 if (rowIndex >=
data_[colIndex].size())
114 return data_[colIndex][rowIndex];
129 return (*
this)(rowIndex, colIndex);
147 return (*
this)(rowIndex, colIndex);
161 if (colIndex >=
nCol_)
166 return (*
this)(rowIndex, colIndex);
178 if (colIndex >=
nCol_)
183 return (*
this)(rowIndex, colIndex);
200 if (rowIndex >=
data_[colIndex].size())
202 return (*
this)(rowIndex, colIndex);
217 if (rowIndex >=
data_[colIndex].size())
219 return (*
this)(rowIndex, colIndex);
237 if (rowNames.size() !=
nRow_)
281 if (colNames.size() !=
nCol_)
330 return data_[colIndex];
345 return data_[colIndex];
357 for (
size_t i = 0; i <
colNames_.size(); i++)
369 data_.erase(
data_.begin() +
static_cast<ptrdiff_t
>(index));
382 data_.erase(
data_.begin() +
static_cast<ptrdiff_t
>(colIndex));
396 if (newColumn.size() !=
nRow_)
398 data_.push_back(newColumn);
411 if (newColumn.size() !=
nRow_)
416 data_.push_back(newColumn);
429 for (
size_t i = 0; i <
nCol_; i++)
431 row.push_back(
data_[i][index]);
444 for (
size_t i = 0; i <
nCol_; i++)
446 row.push_back(
data_[i][rowIndex]);
460 for (
size_t i = 0; i <
rowNames_.size(); i++)
470 for (
size_t j = 0; j <
nCol_; j++)
472 vector<string>* column = &
data_[j];
473 if (index >= column->size())
475 column->erase(column->begin() +
static_cast<ptrdiff_t
>(index));
489 for (
size_t j = 0; j <
nCol_; j++)
491 vector<string>* column = &
data_[j];
492 column->erase(column->begin() +
static_cast<ptrdiff_t
>(rowIndex));
507 if (newRow.size() !=
nCol_)
509 for (
size_t j = 0; j <
nCol_; j++)
511 data_[j].push_back(newRow[j]);
518 if (rowIndex >=
nRow_)
520 if (newRow.size() !=
nCol_)
523 for (
size_t j = 0; j <
nCol_; j++)
525 data_[j][rowIndex] = newRow[j];
538 if (newRow.size() !=
nCol_)
543 for (
size_t j = 0; j <
nCol_; j++)
545 data_[j].push_back(newRow[j]);
554 unique_ptr<DataTable>
DataTable::read(istream& in,
const string& sep,
bool header,
int rowNames)
562 size_t nCol = row1.size();
564 unique_ptr<DataTable> dt;
565 if (row1.size() == row2.size())
567 dt = make_unique<DataTable>(nCol);
579 else if (row1.size() == row2.size() - 1)
581 dt = make_unique<DataTable>(nCol);
583 string rowName = *row2.begin();
584 dt->
addRow(rowName, vector<string>(row2.begin() + 1, row2.end()));
588 throw DimensionException(
"DataTable::read(...). Row 2 has not the correct number of columns.", row2.size(), nCol);
597 string rowName = *st.
getTokens().begin();
612 if (static_cast<size_t>(rowNames) >= nCol)
613 throw IndexOutOfBoundsException(
"DataTable::read(...). Invalid column specified for row names.", static_cast<size_t>(rowNames), 0, nCol - 1);
614 vector<string> col = dt->
getColumn(static_cast<size_t>(rowNames));
635 for (
size_t i = 1; i < n; i++)
637 out << sep << colNames[i];
647 for (
size_t j = 1; j < n; j++)
649 out << sep << data(i, j);
666 for (
size_t i = 1; i < n; i++)
668 out << sep << colNames[i];
678 for (
size_t j = 1; j < n; j++)
680 out << sep << data(i, j);
std::vector< std::string > rowNames_
std::vector< std::string > getColumnNames() const
Get the column names of this table.
std::string getRowName(size_t index) const
Get a given row name.
const T * getElement() const
Exception thrown when a given column name is not found is a DataTable object.
Exception thrown when attempting to duplicate a row name.
Exception thrown when trying to retrieve a row by its name and no row names have been specified...
Exception thrown when a given element was not found in the vector.
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns...
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
std::vector< std::vector< std::string > > data_
std::vector< std::string > getRow(size_t index) const
Exception thrown when a given row name is not found is a DataTable object.
std::vector< std::string > & getColumn(size_t index)
std::string getColumnName(size_t index) const
Get a given column name.
size_t getNumberOfRows() const
Excpetion thrown when attempting to duplicate a column name.
Exception thrown when trying to retrieve a column by its name and no column names have been specified...
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
std::string & operator()(size_t rowIndex, size_t colIndex)
void setRowName(size_t rowIndex, const std::string &rowName)
Set the row name of a row with a given index.
void addRow(const std::vector< std::string > &newRow)
Add a new row.
const std::deque< std::string > & getTokens() const
Retrieve all tokens.
virtual OutputStream & endLine()=0
static std::unique_ptr< DataTable > read(std::istream &in, const std::string &sep="\, bool header=true, int rowNames=-1)
Read a table form a stream in CSV-like format.
bool hasColumnNames() const
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
void deleteColumn(size_t index)
Delete the given column.
void setRow(const size_t rowIndex, const std::vector< std::string > &newRow)
Sets an existing with a given index.
Index out of bounds exception class.
Exception thrown when a given name is not found is a DataTable object.
void deleteRow(size_t index)
Delete the given row.
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces...
Exception thrown when a dimension problem occured.
General exception class dealing with column names.
std::vector< std::string > colNames_
General exception class dealing with row names.
size_t getNumberOfColumns() const
DataTable & operator=(const DataTable &table)
static void write(const DataTable &data, std::ostream &out, const std::string &sep="\, bool alignHeaders=false)
Write a DataTable object to stream in CVS-like format.
std::vector< std::string > getRowNames() const
Get the row names of this table.