template<class vec_t = std::vector<double>>
class o2scl::table< vec_t >
Summary
A class to contain and manipulate several equally-sized columns of data. The purpose of this class is to provide a structure which allows one to refer to the columns using a name represented by a string. Thus for a table object named t
with 3 columns (named "colx", "coly" and "colz") and three rows, one could do the following:
t.set("colx",0,1.0);
t.set("colz",1,2.0);
t.set("coly",2,4.0);
cout << t.get("colz",1) << endl;
Note that the rows are numbered starting with 0 instead of starting with 1. To output all the rows of entire column, one can use
for(size_t i=0;i<t.get_nlines();i++) {
cout << i << " " << t.get("colx",i) << endl;
}
To output all the columns of an entire row (in the following example it is the second row), labeled by their column name, one can use:
for(size_t i=0;i<t.get_ncolumns();i++) {
cout << t.get_column_name(i) << " ";
}
cout << endl;
for(size_t i=0;i<t.get_ncolumns();i++) {
cout << t.get(i,1) << " ";
}
cout << endl;
Methods are provided for interpolating columns, sorting columns, finding data points, and several other manipulations of the data.
Column size
The columns grow automatically (similar to the STL <vector>) in reponse to an attempt to call set() for a row that does not presently exist or in a call to line_of_data() when the table is already full. However, this forces memory rearrangments that are expensive, O(R*C). If the user has a good estimate of the number of rows beforehand, it is best to either specify this in the constructor, or in an explicit call to inc_maxlines().
Lookup, differentiation, integration, and interpolation
Lookup, differentiation, integration, and interpolation are automatically implemented using splines from the class interp_vec. A caching mechanism is implemented so that successive interpolations, derivative evaluations or integrations over the same two columns are fast.
Sorting
The columns are automatically sorted by name for speed, the results can be accessed from get_sorted_name(). Individual columns can be sorted (sort_column() ), or the entire table can be sorted by one column (sort_table() ).
Data representation
Each individual column is just a vector object. The columns can be referred to in one of two ways:
- A numerical index from 0 to C-1 (where C is the number of columns). For example, data can be accessed through table::get() and table::set(size_t c, size_t r, double val), or the overloaded [] operator,
table[c][r]
.
- A name of the column which is a string. For example, data can be accessed with table::get(string cname, int r) and table::set(string cname, int r, double val).
The columns are organized in a both a <map> and a <vector> structure so that finding a column by its index, using either of
takes only constant time, and finding a column by its name using either of
const ubvector &
get_column(std::string col)
const;
is O(log(C)). Insertion of a column ( new_column() ) is O(log(C)), but deletion ( delete_column() ) is O(C). Adding a row of data can be either O(1) or O(C), but row insertion and deletion is slow, since the all of the rows must be shifted accordingly.
Because of the structure, this class is not suitable for the matrix manipulation.
Vector types
The type vec_t
can be any vector type with operator[]
, size()
and resize()
methods. HDF5 I/O with vector types other than std::vector<double>
requires a copy. See the the discussion in the sections Tensors and I/O and contiguous storage of the user's guide for more details.
Thread-safety
Generally, the member functions are only thread-safe if they are const
.
I/O and command-line manipulation
When data from an object of type table is output to a file through the hdf_output() function
in o2scl_hdf, the table can be manipulated on the command-line through the acol
utility (see The 'acol' Command-line Utility).
There is an example for the usage of this class given in examples/ex_table.cpp
.
- Idea for Future:
- Create a sort_column_names() or a function to arbitrarily rearrange the columns
- The present structure,
std::map<std::string,col,string_comp>
atree and std::vector<aiter>
alist; could be replaced with std::vector<col>
list and std::map<std::string,int>
tree where the map just stores the index of the the column in the list.
- Rewrite check_synchro into a full is_valid()-like sanity check
Definition at line 49 of file table.h.
|
template<class vec2_t > |
void | insert_table (table< vec2_t > &source, std::string src_index, bool allow_extrap=true, std::string dest_index="") |
| Insert columns from a source table into the new table by interpolation (or extrapolation)
|
|
|
| table (size_t cmaxlines=0) |
| Create a new table with space for nlines<=cmaxlines.
|
|
virtual | ~table () |
| Table destructor.
|
|
| table (const table &t) |
| Copy constructor.
|
|
table & | operator= (const table &t) |
| Copy constructor.
|
|
|
void | set (std::string scol, size_t row, double val) |
| Set row row of column named col to value val . . More...
|
|
void | set (size_t icol, size_t row, double val) |
| Set row row of column number icol to value val . .
|
|
template<class size_vec_t > |
void | set_row (size_t row, size_vec_t &v) |
| Set an entire row of data. More...
|
|
double | get (std::string scol, size_t row) const |
| Get value from row row of column named col . .
|
|
double | get (size_t icol, size_t row) const |
| Get value from row row of column number icol . .
|
|
size_t | get_ncolumns () const |
| Return the number of columns.
|
|
|
size_t | get_nlines () const |
| Return the number of lines.
|
|
void | set_nlines (size_t il) |
| Set the number of lines. More...
|
|
size_t | get_maxlines () |
| Return the maximum number of lines before a reallocation is required.
|
|
template<class resize_vec_t > |
void | get_row (std::string scol, double val, resize_vec_t &row) const |
| Returns a copy of the row with value val in column col . . More...
|
|
template<class resize_vec_t > |
void | get_row (size_t irow, resize_vec_t &row) const |
| Returns a copy of row number irow . . More...
|
|
void | set_nlines_auto (size_t il) |
| Set the number of lines, increasing the size more agressively. More...
|
|
void | inc_maxlines (size_t llines) |
| Manually increase the maximum number of lines.
|
|
void | set_maxlines (size_t llines) |
| Manually set the maximum number of lines. More...
|
|
|
const vec_t & | get_column (std::string scol) const |
| Returns a reference to the column named col . .
|
|
const vec_t & | operator[] (size_t icol) const |
| Returns the column of index icol (const version). . More...
|
|
const vec_t & | operator[] (std::string scol) const |
| Returns the column named scol (const version). . More...
|
|
void | new_column (std::string head) |
| Add a new column owned by the table . More...
|
|
template<class vec2_t > |
int | new_column (std::string name, size_t sz, vec2_t &v) |
| Add a new column by copying data from another vector. More...
|
|
std::string | get_column_name (size_t icol) const |
| Returns the name of column col . More...
|
|
virtual void | swap_column_data (std::string scol, vec_t &v) |
| Swap the data in column scol with that in vector v . More...
|
|
virtual void | rename_column (std::string src, std::string dest) |
| Rename column named src to dest .
|
|
virtual void | delete_column (std::string scol) |
| Delete column named scol . More...
|
|
std::string | get_sorted_name (size_t icol) const |
| Returns the name of column col in sorted order. .
|
|
void | init_column (std::string scol, double val) |
| Initialize all values of column named scol to val . More...
|
|
bool | is_column (std::string scol) const |
| Return true if scol is a column in the current table. More...
|
|
size_t | lookup_column (std::string lname) const |
| Find the index for column named name . More...
|
|
virtual void | copy_column (std::string src, std::string dest) |
| Copy data from column named src to column named dest , creating a new column if necessary .
|
|
template<class resize_vec_t > |
void | column_to_vector (std::string scol, resize_vec_t &v) const |
| Copy a column to a generic vector object. More...
|
|
template<class vec2_t > |
void | copy_to_column (vec2_t &v, std::string scol) |
| Copy to a column from a generic vector object. More...
|
|
template<class vec2_t > |
void | add_col_from_table (table< vec2_t > &source, std::string src_index, std::string src_col, std::string dest_index, std::string dest_col="") |
| Insert a column from a separate table, interpolating it into a new column. More...
|
|
|
void | new_row (size_t n) |
| Insert a row before row n . More...
|
|
void | copy_row (size_t src, size_t dest) |
| Copy the data in row src to row dest .
|
|
void | delete_row (std::string scol, double val) |
| Delete the row with the entry closest to the value val in column scol .
|
|
void | delete_row (size_t irow) |
| Delete the row of index irow .
|
|
void | delete_rows (std::string func) |
| Delete all rows where func evaluates to a number greater than 0.5 . More...
|
|
template<class vec2_t > |
void | copy_rows (std::string func, table< vec2_t > &dest) |
| Copy all rows matching a particular condition to a new table. More...
|
|
void | delete_rows (size_t row_start, size_t row_end) |
| Delete all rows between row_start and row_end . More...
|
|
template<class vec_size_t > |
void | delete_rows (vec_size_t &row_list) |
| Delete all rows in a specified list. More...
|
|
void | delete_idadj_rows () |
| Delete all rows which are identical to adjacent rows. More...
|
|
void | line_of_names (std::string newheads) |
| Read a new set of names from newheads . More...
|
|
template<class vec2_t > |
void | line_of_data (size_t nv, const vec2_t &v) |
| Read a line of data from the first nv entries in a vector and store as a new row in the table. More...
|
|
template<class vec2_t > |
void | line_of_data (const vec2_t &v) |
| Read a line of data and store in a new row of the table. More...
|
|
|
size_t | ordered_lookup (std::string scol, double val) const |
| Look for a value in an ordered column . More...
|
|
size_t | lookup (std::string scol, double val) const |
| Exhaustively search column col for the value val .
|
|
double | lookup_val (std::string scol, double val, std::string scol2) const |
| Search column col for the value val and return value in col2 .
|
|
size_t | lookup (int icol, double val) const |
| Exhaustively search column col for the value val .
|
|
size_t | mlookup (std::string scol, double val, std::vector< size_t > &results, double threshold=0.0) const |
| Exhaustively search column col for many occurences of val .
|
|
|
void | set_interp_type (size_t interp_type) |
| Set the base interpolation objects.
|
|
size_t | get_interp_type () const |
| Get the interpolation type.
|
|
double | interp (std::string sx, double x0, std::string sy) |
| Interpolate value x0 from column named sx into column named sy . More...
|
|
double | interp_const (std::string sx, double x0, std::string sy) const |
| Interpolate value x0 from column named sx into column named sy (const version) More...
|
|
double | interp (size_t ix, double x0, size_t iy) |
| Interpolate value x0 from column with index ix into column with index iy .
|
|
double | interp_const (size_t ix, double x0, size_t iy) const |
| Interpolate value x0 from column with index ix into column with index iy .
|
|
void | deriv (std::string x, std::string y, std::string yp) |
| Make a new column named yp which is the derivative formed from columns named x and y .
|
|
double | deriv (std::string sx, double x0, std::string sy) |
| Compute the first derivative of the function defined by x-values stored in column named sx and y-values stored in column named sy at the value x0 . More...
|
|
double | deriv_const (std::string sx, double x0, std::string sy) const |
| Compute the first derivative of the function defined by x-values stored in column named sx and y-values stored in column named sy at the value x0 (const version) More...
|
|
double | deriv (size_t ix, double x0, size_t iy) |
| Compute the first derivative of the function defined by x-values stored in column with index ix and y-values stored in column with index iy at the value x0 . More...
|
|
double | deriv_const (size_t ix, double x0, size_t iy) const |
| Compute the first derivative of the function defined by x-values stored in column with index ix and y-values stored in column with index iy at the value x0 (const version) More...
|
|
void | deriv2 (std::string x, std::string y, std::string yp) |
| Create a new column named yp which is equal to the second derivative of the function defined by x-values stored in column named x and y-values stored in column named y , i.e. - O(log(C)*R).
|
|
double | deriv2 (std::string sx, double x0, std::string sy) |
| Compute the second derivative of the function defined by x-values stored in column named sx and y-values stored in column named sy at the value x0 . More...
|
|
double | deriv2_const (std::string sx, double x0, std::string sy) const |
| The Compute the second derivative of the function defined by x-values stored in column named sx and y-values stored in column named sy at the value x0 (const version) More...
|
|
double | deriv2 (size_t ix, double x0, size_t iy) |
| Compute the second derivative of the function defined by x-values stored in column with index ix and y-values stored in column with index iy at the value x0 . More...
|
|
double | deriv2_const (size_t ix, double x0, size_t iy) const |
| Compute the second derivative of the function defined by x-values stored in column with index ix and y-values stored in column with index iy at the value x0 (const version) More...
|
|
double | integ (std::string sx, double x1, double x2, std::string sy) |
| Compute the integral of the function defined by x-values stored in column named sx and y-values stored in column named sy between the values x1 and x2 . More...
|
|
double | integ_const (std::string sx, double x1, double x2, std::string sy) const |
| Compute the integral of the function defined by x-values stored in column named sx and y-values stored in column named sy between the values x1 and x2 (const version) More...
|
|
double | integ (size_t ix, double x1, double x2, size_t iy) |
| Compute the integral of the function defined by x-values stored in column with index ix and y-values stored in column with index iy between the values x1 and x2 . More...
|
|
double | integ_const (size_t ix, double x1, double x2, size_t iy) const |
| Compute the integral of the function defined by x-values stored in column with index ix and y-values stored in column with index iy between the values x1 and x2 (const version) More...
|
|
void | integ (std::string x, std::string y, std::string ynew) |
| Create a new column named ynew which is equal to the integral of the function defined by x-values stored in column named x and y-values stored in column named y . More...
|
|
double | max (std::string scol) const |
| Return column maximum. Makes no assumptions about ordering, .
|
|
double | min (std::string scol) const |
| Return column minimum. Makes no assumptions about ordering, .
|
|
|
void | subtable (std::string list, size_t top, size_t bottom, table< vec_t > &tnew) const |
| Make a subtable. More...
|
|
|
void | zero_table () |
| Zero the data entries but keep the column names and nlines fixed.
|
|
void | clear () |
| Clear everything.
|
|
void | clear_table () |
| Clear the table and the column names (but leave constants)
|
|
void | clear_data () |
| Remove all of the data by setting the number of lines to zero. More...
|
|
void | clear_constants () |
| CLear all constants.
|
|
|
void | sort_table (std::string scol) |
| Sort the entire table by the column scol . More...
|
|
void | sort_column (std::string scol) |
| Individually sort the column scol .
|
|
|
virtual void | summary (std::ostream *out, size_t ncol=79) const |
| Output a summary of the information stored. More...
|
|
|
virtual void | add_constant (std::string name, double val) |
| Add a constant, or if the constant already exists, change its value.
|
|
virtual int | set_constant (std::string name, double val, bool err_on_notfound=true) |
| Set a constant equal to a value, but don't add it if not already present. More...
|
|
virtual bool | is_constant (std::string name) const |
| Test if name is a constant.
|
|
virtual double | get_constant (std::string name) const |
| Get a constant.
|
|
virtual size_t | get_nconsts () const |
| Get the number of constants.
|
|
virtual void | get_constant (size_t ix, std::string &name, double &val) const |
| Get a constant by index.
|
|
virtual void | remove_constant (std::string name) |
| Remove a constant.
|
|
|
virtual int | read_generic (std::istream &fin, int verbose=0) |
| Clear the current table and read from a generic data file.
|
|
void | check_synchro () const |
| Return 0 if the tree and list are properly synchronized.
|
|
virtual const char * | type () |
| Return the type, "table" .
|
|
|
void | functions_columns (std::string list) |
| Create new columns or recompute from a list of functions. More...
|
|
void | function_column (std::string function, std::string scol) |
| Make a column from the function specified in function and add it to the table. More...
|
|
template<class resize_vec_t > |
int | function_vector (std::string function, resize_vec_t &vec, bool throw_on_err=true) |
| Compute a column from a function specified in a string. More...
|
|
double | row_function (std::string function, size_t row) const |
| Compute a value by applying a function to a row.
|
|
size_t | function_find_row (std::string function) const |
| Find a row which maximizes a function.
|
|