Table
Data / CompositeDescription
Stores data in rows and columns like a spreadsheet. Load with loadTable(). Column names are set by the header row.
Syntax
Table* t = loadTable("data.csv", "header")
Parameters
None
Returns
TableMethods
| addColumn(std::string name) | Adds a column |
| addRow() | Adds a new row, returns reference |
| getRowCount() | Returns number of rows |
| getColumnCount() | Returns number of columns |
| getString(int row, int col) | Gets a cell value as string |
| getString(int row, std::string col) | Gets a cell value by column name |
| getInt(int row, int col) | Gets a cell value as int |
| getFloat(int row, int col) | Gets a cell value as float |
| setString(int row, int col, std::string v) | Sets a cell value |
| setInt(int row, int col, int v) | Sets a cell value as int |
| setFloat(int row, int col, float v) | Sets a cell value as float |
| removeRow(int i) | Removes a row |
| clearRows() | Removes all rows |
| getColumnIndex(std::string name) | Returns index of a column by name |
Related
Under the Hood
From Processing.h:
class Table {
public:
std::vector<std::string> columns;
std::vector<std::vector<std::string>> rows;
Table() = default;
void addColumn(const std::string& name) { columns.push_back(name); }
int getColumnCount() const { return (int)columns.size(); }
int getRowCount() const { return (int)rows.size(); }
std::string getColumnTitle(int i) const { return i<(int)columns.size()?columns[i]:""; }
int getColumnIndex(const std::string& n) const {
for (int i=0;i<(int)columns.size();i++) if(columns[i]==n) return i;
return -1;
}
std::vector<std::string>& addRow() { rows.push_back(std::vector<std::string>(columns.size())); return rows.back(); }
std::string getString(int row, int col) const { return row<(int)rows.size()&&col<(int)rows[row].size()?rows[row][col]:""; }
std::string getString(int row, const std::string& col) const { return getString(row,getColumnIndex(col)); }
int getInt(int row, int col) const { auto s=getString(row,col); return s.empty()?0:std::stoi(s); }
int getInt(int row, const std::string& col) const { return getInt(row,getColumnIndex(col)); }
float getFloat(int row, int col) const { auto s=getString(row,col); return s.empty()?0:std::stof(s); }
float getFloat(int row, const std::string& col) const { return getFloat(row,getColumnIndex(col)); }
void setString(int row, int col, const std::string& v) { if(row<(int)rows.size()&&col<(int)rows[row].size()) rows[row][col]=v; }
void setString(int row, const std::string& col, const std::string& v) { setString(row,getColumnIndex(col),v); }
void setInt(int row, int col, int v) { setString(row,col,std::to_string(v)); }
void setFloat(int row, int col, float v) { setString(row,col,std::to_string(v)); }
std::vector<int> findRowsWithValue(const std::string& col, const std::string& val) const {
std::vector<int> r; int c=getColumnIndex(col);
for (int i=0;i<(int)rows.size();i++) if(getString(i,c)==val) r.push_back(i);
return r;
}
int findFirstRowWithValue(const std::string& col, const std::string& val) const {
auto r=findRowsWithValue(col,val); return r.empty()?-1:r[0];
}
void removeRow(int i) { if(i<(int)rows.size()) rows.erase(rows.begin()+i); }
void clearRows() { rows.clear(); }
};