-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
221 lines (188 loc) · 5.64 KB
/
Copy pathUtils.cpp
File metadata and controls
221 lines (188 loc) · 5.64 KB
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
37
38
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// Created by francesca on 05/10/20.
//
//TODO adjust all
//#include <fstream>
//#include <iostream>
//#include <sstream>
//#include "Utils.h"
//using namespace std;
//
//std::vector<Point> CSVReader(const std::string &filename) {
//
// fstream fin; // File pointer
// fin.open(filename, ios::in); // Open an the csv file
//
// int rollnum, roll2, count = 0;
// // Read the Data from the file
// // as String Vector
// vector<string> row;
// string line, word, temp;
//
// while (fin >> temp) {
//
// row.clear();
//
// // read an entire row and
// // store it in a string variable 'line'
// getline(fin, line);
//
// // used for breaking words
// stringstream s(line);
//
// // read every column data of a row and
// // store it in a string variable, 'word'
// while (getline(s, word, ', ')) {
//
// // add all the column data
// // of a row to a vector
// row.push_back(word);
// }
//
// // convert string to integer for comparision
// roll2 = stoi(row[0]);
//
// // Compare the roll number
// if (roll2 == rollnum) {
//
// // Print the found data
// count = 1;
// cout << "Details of Roll " << row[0] << " : \n";
// cout << "Name: " << row[1] << "\n";
// cout << "Maths: " << row[2] << "\n";
// cout << "Physics: " << row[3] << "\n";
// cout << "Chemistry: " << row[4] << "\n";
// cout << "Biology: " << row[5] << "\n";
// break;
// }
// }
//
//
// return std::vector<Point>();
//}
//
//
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <utility> // std::pair
#include <stdexcept> // std::runtime_error
#include <sstream> // std::stringstream
#include "Point.h"
#include "Utils.h"
std::vector<Point> read_csv(std::string filename){
// Reads a CSV file into a vector of <string, vector<int>> pairs where
// each pair represents <column name, column values>
// Create a vector of <string, int vector> pairs to store the result
std::vector<Point> result;
// Create an input filestream
std::ifstream myFile(filename);
// Make sure the file is open
if(!myFile.is_open()) throw std::runtime_error("Could not open file");
// Helper vars
std::string line, colname;
int val;
// // Read the column names
// if(myFile.good())
// {
// // Extract the first line in the file
// std::getline(myFile, line);
//
// // Create a stringstream from line
// std::stringstream ss(line);
//
// // Extract each column name
// while(std::getline(ss, colname, ',')){
//
// // Initialize and add <colname, int vector> pairs to result
// result.push_back({colname, std::vector<int> {}});
// }
// }
// Read data, line by line
while(std::getline(myFile, line))
{
// Create a stringstream of the current line
std::stringstream ss(line);
// Keep track of the current column index
int colIdx = 0;
// Extract each integer
while(ss >> val){
// Add the current integer to the 'colIdx' column's values vector
// result.push_back(val);
// If the next token is a comma, ignore it and move on
if(ss.peek() == ',') ss.ignore();
// Increment the column index
colIdx++;
}
}
// Close file
myFile.close();
return result;
}
std::vector<Point> getPointsFromCsv(std::string& fileName)
{
std::vector<Point> points;
std::ifstream data(fileName);
std::string line;
while (getline(data, line)) {
std::stringstream lineStream(line);
std::string cell;
std::vector<float> point;
while (std::getline(lineStream, cell, ','))
point.push_back(stod(cell));
points.emplace_back(Point(point));
}
return points;
}
void savePointsToCsv(const std::vector<Point> points, std::string filename, int verbose){
if (verbose > 0)
std::cout << "savePointsToCsv" << std::endl;
std::ofstream myFile(filename);
// Send data to the stream
for(auto & point : points)
{
for(int coord_idx = 0; coord_idx < point.getValues().size(); coord_idx++)
{
myFile << point.getValues()[coord_idx];
if(coord_idx != point.getValues().size() - 1)
myFile << ",";
}
myFile << "\n";
}
// Close the file
myFile.close();
}
std::vector<std::string> getPathTokens(std::string s, const std::string delimiter){
std::vector<std::string> tokens;
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
tokens.push_back(token);
s.erase(0, pos + delimiter.length());
}
tokens.push_back(s);
// for(auto & t : tokens){
// std::cout << t << std::endl;
// }
return tokens;
}
void saveResultsToCsv(std::vector<Result> results_time, std::string filename){
// Save time results for experiments to file
std::ofstream myFile(filename);
// Send data to the stream
for(auto experiment : results_time){
myFile << experiment.opeMP_par << ",";
myFile << experiment.omp_static << ",";
myFile << experiment.n_threads << ",";
myFile << experiment.num_points << ",";
myFile << experiment.bandwidth << ",";
myFile << experiment.ms_iterations << ",";
myFile << experiment.runs << ",";
myFile << experiment.time << "\n";
}
// Close the file
myFile.close();
}