Blame view

stim/parser/table.h 2.98 KB
735a2a24   David Mayerich   started testing o...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #ifndef STIM_CSV_H
  #define STIM_CSV_H
  
  #include <vector>
  #include <sstream>
  #include <fstream>
  
  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<std::string>() );	//push an empty row
  		}
  
  		template<typename T>
  		T operator<<(T i){
  			std::stringstream ss;
d4aa4cf7   David Mayerich   fixed the in-memo...
37
38
  			if(std::is_fundamental<T>())						//if the data type is numeric
  				ss<<std::setprecision(std::numeric_limits<T>::digits10 + 3);	//set the precision to account for all digits
735a2a24   David Mayerich   started testing o...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  			ss<<i;
  			TABLE[y].push_back(ss.str());
  			x++;
  			return i;
  		}
  
  		std::string str(){
  			std::stringstream ss;
  			for(size_t yi = 0; yi < TABLE.size(); yi++){
  				for(size_t xi = 0; xi < TABLE[yi].size(); xi++){
  					if(xi != 0) ss<<", ";
  					ss<<TABLE[yi][xi];
  				}
  				ss<<std::endl;
  			}
  			return ss.str();
  		}
  
  		void save_ascii(std::string filename, char col_delim = ',', char row_delim = '\n'){
  			ofstream outfile(filename);
  			for(size_t yi = 0; yi < TABLE.size(); yi++){
  				if(yi != 0 && TABLE[yi].size() > 0) outfile<<row_delim;
  				for(size_t xi = 0; xi < TABLE[yi].size(); xi++){
  					if(xi != 0) outfile<<col_delim;
  					outfile<<TABLE[yi][xi];
  				}
  			}
  		}
  
  		void read_ascii(std::string filename, char col_delim = ',', char row_delim = '\n'){
  			ifstream infile(filename);						//open an ascii file for reading
  			TABLE.clear();									//empty out the table
  
  			std::string line;
  			while(!infile.eof()){
  				std::getline(infile, line, row_delim);		//read a line from the file using the row delimiter
  				size_t pos = 0;
  				std::string token;
  				std::vector< std::string > 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<typename T>
  		std::vector< std::vector<T> > get_vector(){
  			std::vector< std::vector<T> > 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
  		}
  
  	};
  };
  
  
  #endif