#ifndef STIM_TABLE_H #define STIM_TABLE_H #include #include #include #include namespace stim{ class table{ std::vector< std::vector< std::string > > TABLE; size_t x; //current x and y positions for adding elements to the table size_t y; void init(){ x = y = 0; TABLE.resize(1); //create a single row TABLE[0].clear(); //make sure that the row is empty } public: table(){ init(); } void new_column(){ x = 0; //reset to the first position of the column y++; //increment the current row position TABLE.push_back( std::vector() ); //push an empty row } template T operator<<(T i){ std::stringstream ss; if(std::is_fundamental()) //if the data type is numeric ss<::digits10 + 3); //set the precision to account for all digits ss< 0) outfile< row; while ((pos = line.find(col_delim)) != std::string::npos) { token = line.substr(0, pos); row.push_back(token); line.erase(0, pos + 1); } token = line.substr(0, std::string::npos); //read the last column row.push_back(token); //push it into the table TABLE.push_back(row); //add an empty vector to the table } } /// Returns a vector representation of the table by casting to the specified template template std::vector< std::vector > get_vector(){ std::vector< std::vector > result; result.resize(TABLE.size()); //initialize the first dimension of the returned table for(size_t i = 0; i < TABLE.size(); i++){ //for each row result[i].resize(TABLE[i].size()); //initialize the number of columns in this row for(size_t j = 0; j < TABLE[i].size(); j++){ //for each column std::stringstream ss(TABLE[i][j]); ss >> result[i][j]; } } return result; //return the casted table } size_t rows(){ return TABLE.size(); } size_t cols(size_t ri){ return TABLE[ri].size(); } //returns the maximum number of columns in the table size_t cols(){ size_t max_c = 0; for(size_t ri = 0; ri < rows(); ri++){ max_c = std::max(max_c, cols(ri)); } return max_c; } std::string operator()(size_t ri, size_t ci){ return TABLE[ri][ci]; } }; }; #endif