42 #include "../Io/FileTools.h"
43 #include "../Text/StringTokenizer.h"
44 #include "../Text/TextTools.h"
60 for (
size_t i = 0; i < nCol; i++)
62 data_[i].resize(nRow);
76 nCol_(colNames.size()),
77 data_(colNames.size()),
81 for (
size_t i = 0; i <
nCol_; i++)
83 data_[i].resize(nRow);
91 nCol_(colNames.size()),
92 data_(colNames.size()),
147 if (colIndex >=
nCol_)
149 if (rowIndex >=
data_[colIndex].size())
151 return data_[colIndex][rowIndex];
156 if (colIndex >=
nCol_)
158 if (rowIndex >=
data_[colIndex].size())
160 return data_[colIndex][rowIndex];
175 return (*
this)(rowIndex, colIndex);
193 return (*
this)(rowIndex, colIndex);
207 if (colIndex >=
nCol_)
212 return (*
this)(rowIndex, colIndex);
224 if (colIndex >=
nCol_)
229 return (*
this)(rowIndex, colIndex);
246 if (rowIndex >=
data_[colIndex].size())
248 return (*
this)(rowIndex, colIndex);
263 if (rowIndex >=
data_[colIndex].size())
265 return (*
this)(rowIndex, colIndex);
283 if (rowNames.size() !=
nRow_)
289 rowNames_ =
new vector<string>(rowNames.begin(), rowNames.end());
303 (*rowNames_)[rowId] = rowName;
329 if (colNames.size() !=
nCol_)
335 colNames_ =
new vector<string>(colNames.begin(), colNames.end());
380 return data_[colIndex];
395 return data_[colIndex];
407 for (
size_t i = 0; i <
colNames_->size(); i++)
419 data_.erase(
data_.begin() +
static_cast<ptrdiff_t
>(index));
432 data_.erase(
data_.begin() +
static_cast<ptrdiff_t
>(colIndex));
446 if (newColumn.size() !=
nRow_)
448 data_.push_back(newColumn);
461 if (newColumn.size() !=
nRow_)
466 data_.push_back(newColumn);
479 for (
size_t i = 0; i <
nCol_; i++)
481 row.push_back(
data_[i][index]);
494 for (
size_t i = 0; i <
nCol_; i++)
496 row.push_back(
data_[i][rowIndex]);
510 for (
size_t i = 0; i <
rowNames_->size(); i++)
520 for (
size_t j = 0; j <
nCol_; j++)
522 vector<string>* column = &
data_[j];
523 if (index >= column->size())
525 column->erase(column->begin() +
static_cast<ptrdiff_t
>(index));
539 for (
size_t j = 0; j <
nCol_; j++)
541 vector<string>* column = &
data_[j];
542 column->erase(column->begin() +
static_cast<ptrdiff_t
>(rowIndex));
557 if (newRow.size() !=
nCol_)
559 for (
size_t j = 0; j <
nCol_; j++)
561 data_[j].push_back(newRow[j]);
568 if (rowIndex >=
nRow_)
570 if (newRow.size() !=
nCol_)
573 for (
size_t j = 0; j <
nCol_; j++)
575 data_[j][rowIndex] = newRow[j];
588 if (newRow.size() !=
nCol_)
593 for (
size_t j = 0; j <
nCol_; j++)
595 data_[j].push_back(newRow[j]);
612 size_t nCol = row1.size();
615 if (row1.size() == row2.size())
629 else if (row1.size() == row2.size() - 1)
633 string rowName = *row2.begin();
634 dt->
addRow(rowName, vector<string>(row2.begin() + 1, row2.end()));
638 throw DimensionException(
"DataTable::read(...). Row 2 has not the correct number of columns.", row2.size(), nCol);
647 string rowName = *st.
getTokens().begin();
662 if (
static_cast<size_t>(rowNames) >= nCol)
663 throw IndexOutOfBoundsException(
"DataTable::read(...). Invalid column specified for row names.",
static_cast<size_t>(rowNames), 0, nCol - 1);
664 vector<string> col = dt->
getColumn(
static_cast<size_t>(rowNames));
685 for (
size_t i = 1; i < n; i++)
687 out << sep << colNames[i];
697 for (
size_t j = 1; j < n; j++)
699 out << sep << data(i, j);
716 for (
size_t i = 1; i < n; i++)
718 out << sep << colNames[i];
728 for (
size_t j = 1; j < n; j++)
730 out << sep << data(i, j);
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns.
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
std::vector< std::string > * rowNames_
void deleteRow(size_t index)
Delete the given row.
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
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.
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
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.
std::vector< std::string > * colNames_
std::vector< std::vector< std::string > > data_
std::vector< std::string > getRowNames() const
Get the row names of this table.
size_t getNumberOfColumns() const
std::string getColumnName(size_t index) const
Get a given column name.
bool hasColumnNames() const
std::vector< std::string > getColumnNames() const
Get the column names of this table.
std::vector< std::string > getRow(size_t index) const
std::vector< std::string > & getColumn(size_t index)
static DataTable * read(std::istream &in, const std::string &sep="\t", bool header=true, int rowNames=-1)
Read a table form a stream in CSV-like format.
static void write(const DataTable &data, std::ostream &out, const std::string &sep="\t", bool alignHeaders=false)
Write a DataTable object to stream in CVS-like format.
DataTable & operator=(const DataTable &table)
void addRow(const std::vector< std::string > &newRow)
Add a new row.
std::string getRowName(size_t index) const
Get a given row name.
size_t getNumberOfRows() const
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
Exception thrown when a dimension problem occured.
Excpetion thrown when attempting to duplicate a column name.
Exception thrown when attempting to duplicate a row name.
Exception thrown when a given element was not found in the vector.
const T * getElement() const
Index out of bounds exception class.
Exception thrown when trying to retrieve a column by its name and no column names have been specified...
Exception thrown when trying to retrieve a row by its name and no row names have been specified.
virtual OutputStream & endLine()=0
const std::deque< std::string > & getTokens() const
Retrieve all tokens.
Exception thrown when a given column name is not found is a DataTable object.
General exception class dealing with column names.
Exception thrown when a given name is not found is a DataTable object.
Exception thrown when a given row name is not found is a DataTable object.
General exception class dealing with row names.
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.