bpp-core  2.2.0
DataTable.cpp
Go to the documentation of this file.
1 //
2 // File: DataTable.cpp
3 // Created by: Julien Dutheil
4 // Created on: Aug 2005
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "DataTable.h"
41 #include "VectorTools.h"
42 #include "../Io/FileTools.h"
43 #include "../Text/TextTools.h"
44 #include "../Text/StringTokenizer.h"
45 
46 using namespace bpp;
47 using namespace std;
48 
49 /******************************************************************************/
50 
51 DataTable::DataTable(size_t nRow, size_t nCol) :
52  nRow_(nRow),
53  nCol_(nCol),
54  data_(nCol),
55  rowNames_(0),
56  colNames_(0)
57 {
58  for (size_t i = 0; i < nCol; i++)
59  {
60  data_[i].resize(nRow);
61  }
62 }
63 
64 DataTable::DataTable(size_t nCol) :
65  nRow_(0),
66  nCol_(nCol),
67  data_(nCol),
68  rowNames_(0),
69  colNames_(0)
70 {}
71 
72 DataTable::DataTable(const std::vector<std::string>& colNames) throw (DuplicatedTableColumnNameException) :
73  nRow_(0),
74  nCol_(colNames.size()),
75  data_(colNames.size()),
76  rowNames_(0),
77  colNames_(0)
78 
79 {
80  setColumnNames(colNames); // May throw an exception.
81 }
82 
84  nRow_(table.nRow_),
85  nCol_(table.nCol_),
86  data_(table.data_),
87  rowNames_(0),
88  colNames_(0)
89 {
90  if (table.rowNames_)
91  rowNames_ = new vector<string>(*table.rowNames_);
92  if (table.colNames_)
93  colNames_ = new vector<string>(*table.colNames_);
94 }
95 
97 {
98  nRow_ = table.nRow_;
99  nCol_ = table.nCol_;
100  data_ = table.data_;
101  if (rowNames_)
102  delete rowNames_;
103  if (colNames_)
104  delete colNames_;
105  rowNames_ = 0;
106  colNames_ = 0;
107  if (table.rowNames_)
108  rowNames_ = new vector<string>(*table.rowNames_);
109  if (table.colNames_)
110  colNames_ = new vector<string>(*table.colNames_);
111  return *this;
112 }
113 
114 /******************************************************************************/
115 
117 {
118  if (rowNames_ != NULL)
119  delete rowNames_;
120  if (colNames_ != NULL)
121  delete colNames_;
122 }
123 
124 /******************************************************************************/
125 /* Cell access */
126 /******************************************************************************/
127 
128 string& DataTable::operator()(size_t rowIndex, size_t colIndex) throw (IndexOutOfBoundsException)
129 {
130  if (colIndex >= nCol_)
131  throw IndexOutOfBoundsException("DataTable::operator(size_t, size_t).", colIndex, 0, nCol_ - 1);
132  if (rowIndex >= data_[colIndex].size())
133  throw IndexOutOfBoundsException("DataTable::operator(size_t, size_t).", rowIndex, 0, data_[colIndex].size() - 1);
134  return data_[colIndex][rowIndex];
135 }
136 
137 const string& DataTable::operator()(size_t rowIndex, size_t colIndex) const throw (IndexOutOfBoundsException)
138 {
139  if (colIndex >= nCol_)
140  throw IndexOutOfBoundsException("DataTable::operator(size_t, size_t).", colIndex, 0, nCol_ - 1);
141  if (rowIndex >= data_[colIndex].size())
142  throw IndexOutOfBoundsException("DataTable::operator(size_t, size_t).", rowIndex, 0, data_[colIndex].size() - 1);
143  return data_[colIndex][rowIndex];
144 }
145 
146 /******************************************************************************/
147 
148 string& DataTable::operator()(const string& rowName, const string& colName)
150 {
151  if (rowNames_ == NULL)
152  throw NoTableRowNamesException("DataTable::operator(const string &, const string &).");
153  if (colNames_ == NULL)
154  throw NoTableColumnNamesException("DataTable::operator(const string &, const string &).");
155  try
156  {
157  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
158  size_t colIndex = VectorTools::which(*colNames_, colName);
159  return (*this)(rowIndex, colIndex);
160  }
162  {
163  throw TableNameNotFoundException("DataTable::operator(const string &, const string &).", *ex.getElement());
164  }
165 }
166 
167 const string& DataTable::operator()(const string& rowName, const string& colName) const
169 {
170  if (rowNames_ == NULL)
171  throw NoTableRowNamesException("DataTable::operator(const string &, const string &).");
172  if (colNames_ == NULL)
173  throw NoTableColumnNamesException("DataTable::operator(const string &, const string &).");
174  try
175  {
176  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
177  size_t colIndex = VectorTools::which(*colNames_, colName);
178  return (*this)(rowIndex, colIndex);
179  }
181  {
182  throw TableNameNotFoundException("DataTable::operator(const string &, const string &).", *ex.getElement());
183  }
184 }
185 
186 /******************************************************************************/
187 
188 string& DataTable::operator()(const string& rowName, size_t colIndex)
190 {
191  if (rowNames_ == NULL)
192  throw NoTableRowNamesException("DataTable::operator(const string &, size_t).");
193  if (colIndex >= nCol_)
194  throw IndexOutOfBoundsException("DataTable::operator(const string &, size_t).", colIndex, 0, nCol_ - 1);
195  try
196  {
197  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
198  return (*this)(rowIndex, colIndex);
199  }
201  {
202  throw TableNameNotFoundException("DataTable::operator(const string &, size_t).", *ex.getElement());
203  }
204 }
205 
206 const string& DataTable::operator()(const string& rowName, size_t colIndex) const
208 {
209  if (rowNames_ == NULL)
210  throw NoTableRowNamesException("DataTable::operator(const string &, size_t).");
211  if (colIndex >= nCol_)
212  throw IndexOutOfBoundsException("DataTable::operator(const string &, size_t).", colIndex, 0, nCol_ - 1);
213  try
214  {
215  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
216  return (*this)(rowIndex, colIndex);
217  }
219  {
220  throw TableNameNotFoundException("DataTable::operator(const string &, size_t).", *ex.getElement());
221  }
222 }
223 
224 /******************************************************************************/
225 
226 string& DataTable::operator()(size_t rowIndex, const string& colName)
228 {
229  if (colNames_ == NULL)
230  throw NoTableColumnNamesException("DataTable::operator(size_t, const string &).");
231  try
232  {
233  size_t colIndex = VectorTools::which(*colNames_, colName);
234  if (rowIndex >= data_[colIndex].size())
235  throw IndexOutOfBoundsException("DataTable::operator(size_t, const string &).", rowIndex, 0, data_[colIndex].size() - 1);
236  return (*this)(rowIndex, colIndex);
237  }
239  {
240  throw TableNameNotFoundException("DataTable::operator(const string &, const string &).", *ex.getElement());
241  }
242 }
243 
244 const string& DataTable::operator()(size_t rowIndex, const string& colName) const
246 {
247  if (colNames_ == NULL)
248  throw NoTableColumnNamesException("DataTable::operator(size_t, const string &).");
249  try
250  {
251  size_t colIndex = VectorTools::which(*colNames_, colName);
252  if (rowIndex >= data_[colIndex].size())
253  throw IndexOutOfBoundsException("DataTable::operator(size_t, const string &).", rowIndex, 0, data_[colIndex].size() - 1);
254  return (*this)(rowIndex, colIndex);
255  }
257  {
258  throw TableNameNotFoundException("DataTable::operator(const string &, const string &).", *ex.getElement());
259  }
260 }
261 
262 /******************************************************************************/
263 /* Work with names */
264 /******************************************************************************/
265 
266 void DataTable::setRowNames(const vector<string>& rowNames)
268 {
269  if (!VectorTools::isUnique(rowNames))
270  {
271  throw DuplicatedTableRowNameException("DataTable::setRowNames(...). Row names must be unique.");
272  }
273  if (rowNames.size() != nRow_)
274  throw DimensionException("DataTable::setRowNames.", rowNames.size(), nRow_);
275  else
276  {
277  if (rowNames_ != NULL)
278  delete rowNames_;
279  rowNames_ = new vector<string>(rowNames.begin(), rowNames.end());
280  }
281 }
282 
283 vector<string> DataTable::getRowNames() const throw (NoTableRowNamesException)
284 {
285  if (rowNames_ == NULL)
286  throw NoTableRowNamesException("DataTable::getRowNames().");
287  return *rowNames_;
288 }
289 
291 {
292  if (rowNames_ == NULL)
293  throw NoTableRowNamesException("DataTable::getRowName(size_t).");
294  if (index >= nRow_)
295  throw IndexOutOfBoundsException("DataTable::getRowName(size_t).", index, 0, nRow_ - 1);
296  return (*rowNames_)[index];
297 }
298 
299 /******************************************************************************/
300 
301 void DataTable::setColumnNames(const vector<string>& colNames)
303 {
304  if (!VectorTools::isUnique(colNames))
305  throw DuplicatedTableColumnNameException("DataTable::setColumnNames(...). Column names must be unique.");
306  if (colNames.size() != nCol_)
307  throw DimensionException("DataTable::setColumnNames.", colNames.size(), nCol_);
308  else
309  {
310  if (colNames_ != NULL)
311  delete colNames_;
312  colNames_ = new vector<string>(colNames.begin(), colNames.end());
313  }
314 }
315 
317 {
318  if (colNames_ == NULL)
319  throw NoTableColumnNamesException("DataTable::getColumnNames().");
320  return *colNames_;
321 }
322 
324 {
325  if (colNames_ == NULL)
326  throw NoTableColumnNamesException("DataTable::getColumnName(size_t).");
327  if (index >= nCol_)
328  throw IndexOutOfBoundsException("DataTable::getColumnName(size_t).", index, 0, nCol_ - 1);
329  return (*colNames_)[index];
330 }
331 
332 /******************************************************************************/
333 /* Work on columns */
334 /******************************************************************************/
335 
336 vector<string>& DataTable::getColumn(size_t index)
338 {
339  if (index >= nCol_)
340  throw IndexOutOfBoundsException("DataTable::getColumn(size_t).", index, 0, nCol_ - 1);
341  return data_[index];
342 }
343 
344 const vector<string>& DataTable::getColumn(size_t index) const
346 {
347  if (index >= nCol_)
348  throw IndexOutOfBoundsException("DataTable::getColumn(size_t).", index, 0, nCol_ - 1);
349  return data_[index];
350 }
351 
352 vector<string>& DataTable::getColumn(const string& colName)
354 {
355  if (colNames_ == NULL)
356  throw NoTableColumnNamesException("DataTable::getColumn(const string &).");
357  try
358  {
359  size_t colIndex = VectorTools::which(*colNames_, colName);
360  return data_[colIndex];
361  }
363  {
364  throw TableColumnNameNotFoundException("DataTable::getColumn(const string &).", colName);
365  }
366 }
367 
368 const vector<string>& DataTable::getColumn(const string& colName) const
370 {
371  if (colNames_ == NULL)
372  throw NoTableColumnNamesException("DataTable::getColumn(const string &).");
373  try
374  {
375  size_t colIndex = VectorTools::which(*colNames_, colName);
376  return data_[colIndex];
377  }
379  {
380  throw TableColumnNameNotFoundException("DataTable::getColumn(const string &).", colName);
381  }
382 }
383 
384 bool DataTable::hasColumn(const string& colName) const
385 {
386  if (colNames_ == NULL)
387  return false;
388  for (size_t i = 0; i < colNames_->size(); i++)
389  {
390  if ((*colNames_)[i] == colName)
391  return true;
392  }
393  return false;
394 }
395 
396 void DataTable::deleteColumn(size_t index)
398 {
399  if (index >= nCol_)
400  throw IndexOutOfBoundsException("DataTable::deleteColumn(size_t).", index, 0, nCol_ - 1);
401  data_.erase(data_.begin() + static_cast<ptrdiff_t>(index));
402  if (colNames_)
403  colNames_->erase(colNames_->begin() + static_cast<ptrdiff_t>(index));
404  nCol_--;
405 }
406 
407 void DataTable::deleteColumn(const string& colName)
409 {
410  if (!colNames_)
411  throw NoTableColumnNamesException("DataTable::deleteColumn(const string &).");
412  try
413  {
414  size_t colIndex = VectorTools::which(*colNames_, colName);
415  data_.erase(data_.begin() + static_cast<ptrdiff_t>(colIndex));
416  colNames_->erase(colNames_->begin() + static_cast<ptrdiff_t>(colIndex));
417  nCol_--;
418  }
420  {
421  throw TableColumnNameNotFoundException("DataTable::deleteColumn(const string &).", colName);
422  }
423 }
424 
425 void DataTable::addColumn(const vector<string>& newColumn)
427 {
428  if (colNames_)
429  throw TableColumnNamesException("DataTable::addColumn. Table has column names.");
430  if (newColumn.size() != nRow_)
431  throw DimensionException("DataTable::addColumn.", newColumn.size(), nRow_);
432  data_.push_back(newColumn);
433  nCol_++;
434 }
435 
436 void DataTable::addColumn(const string& colName, const vector<string>& newColumn)
438 {
439  if (!colNames_)
440  {
441  if (nCol_ == 0)
442  colNames_ = new vector<string>();
443  else
444  throw NoTableColumnNamesException("DataTable::addColumn. Table has column names.");
445  }
446  if (newColumn.size() != nRow_)
447  throw DimensionException("DataTable::addColumn.", newColumn.size(), nRow_);
448  if (nCol_ > 0 && find(colNames_->begin(), colNames_->end(), colName) != colNames_->end())
449  throw DuplicatedTableColumnNameException("DataTable::addColumn(const string &, const vector<string> &). Column names must be unique.");
450  colNames_->push_back(colName);
451  data_.push_back(newColumn);
452  nCol_++;
453 }
454 
455 /******************************************************************************/
456 /* Work on rows */
457 /******************************************************************************/
458 
459 vector<string> DataTable::getRow(size_t index) const
461 {
462  if (index >= nRow_)
463  throw IndexOutOfBoundsException("DataTable::getRow(size_t).", index, 0, nRow_ - 1);
464  vector<string> row;
465  for (size_t i = 0; i < nCol_; i++)
466  {
467  row.push_back(data_[i][index]);
468  }
469  return row;
470 }
471 
472 vector<string> DataTable::getRow(const string& rowName) const
474 {
475  if (!rowNames_)
476  throw NoTableRowNamesException("DataTable::getRow(const string &).");
477  try
478  {
479  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
480  vector<string> row;
481  for (size_t i = 0; i < nCol_; i++)
482  {
483  row.push_back(data_[i][rowIndex]);
484  }
485  return row;
486  }
488  {
489  throw TableRowNameNotFoundException("DataTable::getRow(const string &).", rowName);
490  }
491 }
492 
493 bool DataTable::hasRow(const string& rowName) const
494 {
495  if (rowNames_ == NULL)
496  return false;
497  for (size_t i = 0; i < rowNames_->size(); i++)
498  {
499  if ((*rowNames_)[i] == rowName)
500  return true;
501  }
502  return false;
503 }
504 
505 void DataTable::deleteRow(size_t index)
507 {
508  for (size_t j = 0; j < nCol_; j++)
509  {
510  vector<string>* column = &data_[j];
511  if (index >= column->size())
512  throw IndexOutOfBoundsException("DataTable::deleteRow(size_t).", index, 0, column->size() - 1);
513  column->erase(column->begin() + static_cast<ptrdiff_t>(index));
514  }
515  if (rowNames_)
516  rowNames_->erase(rowNames_->begin() + static_cast<ptrdiff_t>(index));
517  nRow_--;
518 }
519 
520 void DataTable::deleteRow(const string& rowName)
522 {
523  if (!rowNames_)
524  throw NoTableRowNamesException("DataTable::deleteRow(const string &).");
525  try
526  {
527  size_t rowIndex = VectorTools::which(*rowNames_, rowName);
528  for (size_t j = 0; j < nCol_; j++)
529  {
530  vector<string>* column = &data_[j];
531  column->erase(column->begin() + static_cast<ptrdiff_t>(rowIndex));
532  }
533  rowNames_->erase(rowNames_->begin() + static_cast<ptrdiff_t>(rowIndex));
534  nRow_--;
535  }
537  {
538  throw TableRowNameNotFoundException("DataTable::deleteRow(const string &).", rowName);
539  }
540 }
541 
542 void DataTable::addRow(const vector<string>& newRow)
544 {
545  if (rowNames_)
546  throw TableRowNamesException("DataTable::addRow. Table has row names.");
547  if (newRow.size() != nCol_)
548  throw DimensionException("DataTable::addRow.", newRow.size(), nCol_);
549  for (size_t j = 0; j < nCol_; j++)
550  {
551  data_[j].push_back(newRow[j]);
552  }
553  nRow_++;
554 }
555 
556 void DataTable::addRow(const string& rowName, const vector<string>& newRow)
558 {
559  if (!rowNames_)
560  {
561  if (nRow_ == 0)
562  rowNames_ = new vector<string>();
563  else
564  throw NoTableRowNamesException("DataTable::addRow. Table has row names.");
565  }
566  if (newRow.size() != nCol_)
567  throw DimensionException("DataTable::addRow.", newRow.size(), nCol_);
568  if (nRow_ > 0 && find(rowNames_->begin(), rowNames_->end(), rowName) != rowNames_->end())
569  throw DuplicatedTableRowNameException("DataTable::addRow(const string &, const vector<string> &). Row names must be unique.");
570  rowNames_->push_back(rowName);
571  for (size_t j = 0; j < nCol_; j++)
572  {
573  data_[j].push_back(newRow[j]);
574  }
575  nRow_++;
576 }
577 
578 /******************************************************************************/
579 /* Read from a CSV file */
580 /******************************************************************************/
581 
582 DataTable* DataTable::read(istream& in, const string& sep, bool header, int rowNames)
584 {
585  string firstLine = FileTools::getNextLine(in);
586  StringTokenizer st1(firstLine, sep, false, true);
587  vector<string> row1(st1.getTokens().begin(), st1.getTokens().end());
588  string secondLine = FileTools::getNextLine(in);
589  StringTokenizer st2(secondLine, sep, false, true);
590  vector<string> row2(st2.getTokens().begin(), st2.getTokens().end());
591  size_t nCol = row1.size();
592  bool hasRowNames;
593  DataTable* dt;
594  if (row1.size() == row2.size())
595  {
596  dt = new DataTable(nCol);
597  if (header)
598  { // Use first line as header.
599  dt->setColumnNames(row1);
600  }
601  else
602  {
603  dt->addRow(row1);
604  }
605  dt->addRow(row2);
606  hasRowNames = false;
607  }
608  else if (row1.size() == row2.size() - 1)
609  {
610  dt = new DataTable(nCol);
611  dt->setColumnNames(row1);
612  string rowName = *row2.begin();
613  dt->addRow(rowName, vector<string>(row2.begin() + 1, row2.end()));
614  hasRowNames = true;
615  }
616  else
617  throw DimensionException("DataTable::read(...). Row 2 has not the correct number of columns.", row2.size(), nCol);
618 
619  // Now read each line:
620  string line = FileTools::getNextLine(in);
621  while (!TextTools::isEmpty(line))
622  {
623  StringTokenizer st(line, sep, false, true);
624  if (hasRowNames)
625  {
626  string rowName = *st.getTokens().begin();
627  vector<string> row(st.getTokens().begin() + 1, st.getTokens().end());
628  dt->addRow(rowName, row);
629  }
630  else
631  {
632  vector<string> row(st.getTokens().begin(), st.getTokens().end());
633  dt->addRow(row);
634  }
635  line = FileTools::getNextLine(in);
636  }
637 
638  // Row names:
639  if (rowNames > -1)
640  {
641  if (static_cast<size_t>(rowNames) >= nCol)
642  throw IndexOutOfBoundsException("DataTable::read(...). Invalid column specified for row names.", static_cast<size_t>(rowNames), 0, nCol - 1);
643  vector<string> col = dt->getColumn(static_cast<size_t>(rowNames));
644  dt->setRowNames(col);
645  dt->deleteColumn(static_cast<size_t>(rowNames));
646  }
647 
648  return dt;
649 }
650 
651 /******************************************************************************/
652 
653 void DataTable::write(const DataTable& data, ostream& out, const string& sep, bool alignHeaders)
654 {
655  size_t n = data.getNumberOfColumns();
656  if (n == 0)
657  return;
658  if (data.hasColumnNames())
659  { // Write header
660  vector<string> colNames = data.getColumnNames();
661  if (alignHeaders && data.hasRowNames())
662  out << sep;
663  out << colNames[0];
664  for (size_t i = 1; i < n; i++)
665  {
666  out << sep << colNames[i];
667  }
668  out << endl;
669  }
670  // Now write each row:
671  for (size_t i = 0; i < data.getNumberOfRows(); i++)
672  {
673  if (data.hasRowNames())
674  out << data.getRowName(i) << sep;
675  out << data(i, 0);
676  for (size_t j = 1; j < n; j++)
677  {
678  out << sep << data(i, j);
679  }
680  out << endl;
681  }
682 }
683 
684 void DataTable::write(const DataTable& data, bpp::OutputStream& out, const string& sep, bool alignHeaders)
685 {
686  size_t n = data.getNumberOfColumns();
687  if (n == 0)
688  return;
689  if (data.hasColumnNames())
690  { // Write header
691  vector<string> colNames = data.getColumnNames();
692  if (alignHeaders && data.hasRowNames())
693  out << sep;
694  out << colNames[0];
695  for (size_t i = 1; i < n; i++)
696  {
697  out << sep << colNames[i];
698  }
699  out.endLine();
700  }
701  // Now write each row:
702  for (size_t i = 0; i < data.getNumberOfRows(); i++)
703  {
704  if (data.hasRowNames())
705  out << data.getRowName(i) << sep;
706  out << data(i, 0);
707  for (size_t j = 1; j < n; j++)
708  {
709  out << sep << data(i, j);
710  }
711  out.endLine();
712  }
713 }
714 
715 /******************************************************************************/
716 
virtual ~DataTable()
Definition: DataTable.cpp:116
std::vector< std::string > getColumnNames() const
Get the column names of this table.
Definition: DataTable.cpp:316
A tokenizer for strings.
size_t nRow_
Definition: DataTable.h:68
This class allows to perform a correspondence analysis.
std::string getRowName(size_t index) const
Get a given row name.
Definition: DataTable.cpp:290
Exception thrown when a given column name is not found is a DataTable object.
Exception thrown when attempting to duplicate a row name.
std::vector< std::string > * rowNames_
Definition: DataTable.h:70
Exception thrown when trying to retrieve a row by its name and no row names have been specified...
STL namespace.
Exception thrown when a given element was not found in the vector.
This class corresponds to a &#39;dataset&#39;, i.e. a table with data by rows and variable by columns...
Definition: DataTable.h:64
static size_t which(const std::vector< T > &v, const T &which)
Send the position of the first occurence of &#39;which&#39;.
Definition: VectorTools.h:434
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
Definition: DataTable.cpp:266
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
Definition: DataTable.cpp:425
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
Definition: DataTable.cpp:301
std::vector< std::vector< std::string > > data_
Definition: DataTable.h:69
std::vector< std::string > getRow(size_t index) const
Definition: DataTable.cpp:459
virtual const T * getElement() const
Exception thrown when a given row name is not found is a DataTable object.
bool hasRowNames() const
Definition: DataTable.h:351
std::vector< std::string > & getColumn(size_t index)
Definition: DataTable.cpp:336
std::string getColumnName(size_t index) const
Get a given column name.
Definition: DataTable.cpp:323
size_t getNumberOfRows() const
Definition: DataTable.h:311
static bool isEmpty(const std::string &s)
Tell if a string is empty.
Definition: TextTools.cpp:52
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.
Definition: DataTable.cpp:493
std::string & operator()(size_t rowIndex, size_t colIndex)
Definition: DataTable.cpp:128
static std::string getNextLine(std::istream &in)
Get the next non-blanck line of a stream.
Definition: FileTools.cpp:142
std::vector< std::string > * colNames_
Definition: DataTable.h:71
void addRow(const std::vector< std::string > &newRow)
Add a new row.
Definition: DataTable.cpp:542
const std::deque< std::string > & getTokens() const
Retrieve all tokens.
virtual OutputStream & endLine()=0
OutputStream interface.
Definition: OutputStream.h:64
bool hasColumnNames() const
Definition: DataTable.h:227
static bool isUnique(const std::vector< T > &v)
Tell if the std::vector as unique elements.
Definition: VectorTools.h:503
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
Definition: DataTable.cpp:51
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
Definition: DataTable.cpp:384
size_t nCol_
Definition: DataTable.h:68
void deleteColumn(size_t index)
Delete the given column.
Definition: DataTable.cpp:396
static 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.
Definition: DataTable.cpp:582
Index out of bounds exception class.
Definition: Exceptions.h:298
Exception thrown when a given name is not found is a DataTable object.
void deleteRow(size_t index)
Delete the given row.
Definition: DataTable.cpp:505
Exception thrown when a dimension problem occured.
General exception class dealing with column names.
General exception class dealing with row names.
size_t getNumberOfColumns() const
Definition: DataTable.h:197
DataTable & operator=(const DataTable &table)
Definition: DataTable.cpp:96
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.
Definition: DataTable.cpp:653
std::vector< std::string > getRowNames() const
Get the row names of this table.
Definition: DataTable.cpp:283