41 #ifndef BPP_NUMERIC_TABLE_H
42 #define BPP_NUMERIC_TABLE_H
45 #include "../Clonable.h"
71 std::vector< std::vector<T> >
data_;
83 Table(
size_t nRow,
size_t nCol) :
90 for (
size_t i = 0; i < nCol; i++)
92 data_[i].resize(nRow);
103 Table(
const std::vector<std::string>& colNames) :
105 nCol_(colNames.size()),
106 data_(colNames.size()),
122 Table(
const std::vector<std::vector<T> >& vt) :
123 nRow_(vt.size() == 0 ? 0 : vt[0].size()),
150 nRow_ = vt[0].size();
168 const std::vector< std::vector<T> >&
getData()
const
182 if (colIndex >=
nCol_)
184 if (rowIndex >=
data_[colIndex].size())
186 return data_[colIndex][rowIndex];
197 if (colIndex >=
nCol_)
199 if (rowIndex >=
data_[colIndex].size())
201 return data_[colIndex][rowIndex];
212 T&
operator()(
const std::string& rowName,
const std::string& colName)
222 return (*
this)(rowIndex, colIndex);
239 const T&
operator()(
const std::string& rowName,
const std::string& colName)
const
249 return (*
this)(rowIndex, colIndex);
270 if (colIndex >=
nCol_)
275 return (*
this)(rowIndex, colIndex);
292 const T&
operator()(
const std::string& rowName,
size_t colIndex)
const
296 if (colIndex >=
nCol_)
301 return (*
this)(rowIndex, colIndex);
325 if (rowIndex >=
data_[colIndex].size())
327 return (*
this)(rowIndex, colIndex);
344 const T&
operator()(
size_t rowIndex,
const std::string& colName)
const
351 if (rowIndex >=
data_[colIndex].size())
353 return (*
this)(rowIndex, colIndex);
383 if (colNames.size() !=
nCol_)
471 return data_[colIndex];
485 const std::vector<T>&
getColumn(
const std::string& colName)
const
492 return data_[colIndex];
511 for (
size_t i = 0; i <
colNames_.size(); i++)
550 data_.erase(
data_.begin() + (
size_t)(colIndex));
569 void addColumn(
const std::vector<T>& newColumn,
int pos = -1)
571 if (pos > (
int)
nCol_)
578 if (newColumn.size() !=
nRow_)
595 void addColumn(
const std::string& colName,
const std::vector<T>& newColumn,
int pos = -1)
597 if (pos > (
int)
nCol_)
605 if (newColumn.size() !=
nRow_)
624 void addColumn(std::string& st,
const std::string& sep =
"\t",
int pos = -1,
int rowCol = -1)
626 if (pos > (
int)
nCol_)
634 if (row.size() !=
nRow_ + (rowCol >= 0) ? 1 : 0)
637 std::vector<T> newColumn;
639 for (
size_t i = 0; i < row.size(); i++)
641 if ((
int)i == rowCol)
643 std::string colName = row[i];
651 std::stringstream ss(row[i]);
654 newColumn.push_back(t);
671 void setColumn(
const std::vector<T>& newColumn,
size_t pos)
676 if (newColumn.size() !=
nRow_)
679 data_[pos] = newColumn;
692 void setColumn(
const std::string& colName,
const std::vector<T>& newColumn,
size_t pos)
700 if (newColumn.size() !=
nRow_)
708 data_[pos] = newColumn;
737 if (rowNames.size() !=
nRow_)
773 bool hasRow(
const std::string& rowName)
const
777 for (
size_t i = 0; i <
rowNames_.size(); i++)
814 std::vector<T>
getRow(
size_t index)
const
819 for (
size_t i = 0; i <
nCol_; i++)
821 row.push_back(
data_[i][index]);
833 std::vector<T>
getRow(
const std::string& rowName)
const
841 for (
size_t i = 0; i <
nCol_; i++)
843 row.push_back(
data_[i][rowIndex]);
862 for (
size_t j = 0; j <
nCol_; j++)
864 std::vector<T>* column = &
data_[j];
865 if (index >= column->size())
867 column->erase(column->begin() + index);
883 for (
size_t j = 0; j <
nCol_; j++)
885 std::vector<T>* column = &
data_[j];
886 if (index >= column->size())
888 column->erase(column->begin() + (
long)index, column->begin() +
long(index + len));
910 for (
size_t j = 0; j <
nCol_; j++)
912 std::vector<T>* column = &
data_[j];
913 column->erase(column->begin() + rowIndex);
931 void addRow(
const std::vector<T>& newRow,
int pos = -1)
933 if (pos > (
int)
nRow_)
941 if (newRow.size() !=
nCol_)
943 for (
size_t j = 0; j <
nCol_; j++)
945 data_[j].insert(
data_[j].begin() + (
size_t)pos, newRow[j]);
957 void addRow(
const std::string& rowName,
const std::vector<T>& newRow,
int pos = -1)
959 if (pos > (
int)
nRow_)
967 if (newRow.size() !=
nCol_)
972 for (
size_t j = 0; j <
nCol_; j++)
974 data_[j].insert(
data_[j].begin() + (
size_t)pos, newRow[j]);
988 void addRow(std::string& st,
const std::string& sep =
"\t",
int pos = -1,
int rowCol = -1)
990 if (pos > (
int)
nRow_)
998 if (row.size() !=
nCol_ + (rowCol >= 0) ? 1 : 0)
1003 for (
size_t i = 0; i < row.size(); i++)
1005 if ((
int)i == rowCol)
1007 std::string rowName = row[i];
1015 std::stringstream ss(row[i]);
1018 data_[id].insert(
data_[
id].begin() + (
long)pos, t);
1046 static Table<T>*
read(std::istream& in,
bool byRow,
const std::string& sep =
"\t",
bool header =
true,
int names = -1)
1051 size_t nCol = row1.size();
1072 dt =
new Table<T>(0, nCol - (names >= 0 ? 1 : 0));
1073 dt->
addRow(firstLine, sep, names);
1077 dt =
new Table<T>(nCol - (names >= 0 ? 1 : 0), 0);
1088 dt->
addRow(line, sep, names);
1107 static void write(
const Table& data, std::ostream& out,
bool byRow,
const std::string& sep =
"\t",
bool alignHeaders =
false)
1120 if (alignHeaders && frontNames)
1123 for (
size_t i = 1; i < n; i++)
1125 out << sep << names[i];
1132 for (
size_t i = 0; i < m; i++)
1139 out << (byRow ? data(i, 0) : data(0, i));
1140 for (
size_t j = 1; j < n; j++)
1142 out << sep << (byRow ? data(i, j) : data(j, i));
1161 if (alignHeaders && frontNames)
1164 for (
size_t i = 1; i < n; i++)
1166 out << sep << names[i];
1173 for (
size_t i = 0; i < m; i++)
1178 out << (byRow ? data(i, 0) : data(0, i));
1179 for (
size_t j = 1; j < n; j++)
1181 out << sep << (byRow ? data(i, j) : data(j, i));
Number exception: integers.
The Clonable interface (allow an object to be cloned).
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.
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns.
const T & operator()(size_t rowIndex, const std::string &colName) const
void addColumn(const std::string &colName, const std::vector< T > &newColumn, int pos=-1)
Add a new column.
const T & operator()(size_t rowIndex, size_t colIndex) const
void deleteRows(size_t index, size_t len)
Delete the given rows.
std::vector< std::string > rowNames_
std::vector< T > getRow(size_t index) const
size_t getNumberOfColumns() const
std::vector< T > & getColumn(size_t index)
T & operator()(const std::string &rowName, size_t colIndex)
std::string getColumnName(size_t index) const
Get a given column name.
void deleteRow(size_t index)
Delete the given row.
const std::vector< std::string > & getColumnNames() const
Get the column names of this table.
Table * clone() const
Create a copy of this object and send a pointer to it.
Table & operator=(const std::vector< std::vector< T > > &vt)
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
std::vector< T > & getColumn(const std::string &colName)
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
Table(const std::vector< std::string > &colNames)
Build a new void Table object with named columns.
void deleteRow(const std::string &rowName)
Delete the given row.
void deleteColumn(size_t index)
Delete the given column.
T & operator()(size_t rowIndex, size_t colIndex)
void setColumn(const std::vector< T > &newColumn, size_t pos)
Set a new column.
std::vector< std::string > colNames_
const std::vector< std::vector< T > > & getData() const
const std::vector< T > & getColumn(size_t index) const
Table(const std::vector< std::vector< T > > &vt)
bool hasColumnNames() const
std::string getRowName(size_t index) const
Get a given row name.
Table(const Table &table)
std::vector< T > getRow(const std::string &rowName) const
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
const std::vector< T > & getColumn(const std::string &colName) const
const T & operator()(const std::string &rowName, size_t colIndex) const
std::vector< std::string > & getColumnNames()
std::vector< std::vector< T > > data_
void addRow(const std::string &rowName, const std::vector< T > &newRow, int pos=-1)
Add a new row at a given position.
void deleteColumn(const std::string &colName)
Delete the given column.
std::vector< std::string > getRowNames()
Get the row names of this table.
static Table< T > * read(std::istream &in, bool byRow, const std::string &sep="\t", bool header=true, int names=-1)
Read a table form a stream in CSV-like.
void setColumn(const std::string &colName, const std::vector< T > &newColumn, size_t pos)
Set a new column.
void addColumn(const std::vector< T > &newColumn, int pos=-1)
Add a new column.
static void write(const Table &data, std::ostream &out, bool byRow, const std::string &sep="\t", bool alignHeaders=false)
Write a Table object to stream in CVS-like format.
void addRow(std::string &st, const std::string &sep="\t", int pos=-1, int rowCol=-1)
Add a new row.
Table & operator=(const Table &table)
static void write(const Table &data, bpp::OutputStream &out, bool byRow, const std::string &sep="\t", bool alignHeaders=false)
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
size_t getNumberOfRows() const
T & operator()(const std::string &rowName, const std::string &colName)
const T & operator()(const std::string &rowName, const std::string &colName) const
T & operator()(size_t rowIndex, const std::string &colName)
Table(size_t nRow, size_t nCol)
Build a new void Table object with nRow rows and nCol columns.
void addRow(const std::vector< T > &newRow, int pos=-1)
Add a new row.
const std::vector< std::string > & getRowNames() const
void addColumn(std::string &st, const std::string &sep="\t", int pos=-1, int rowCol=-1)
Add a new column.
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.