-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparse.hpp
171 lines (163 loc) · 5.35 KB
/
parse.hpp
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
#include <vector>
#include <map>
using std::cerr;
using std::endl;
using std::vector;
using std::map;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ostringstream;
ifstream& Getline(ifstream& ifs, string& line) {
getline(ifs,line);
if (!line.empty()) {
while (isspace(line.at(line.size()-1))) {
line = line.substr(0,line.size()-1);
}
}
return ifs;
}
vector<string> split(string to_split) {
vector<string> split_string;
unsigned index_start;
for (unsigned i = 0;i < to_split.length();i++) {
index_start = i;
while(i < to_split.length() && !isspace(to_split.at(i))) {
i++;
}
split_string.push_back(to_split.substr(index_start,i - index_start));
}
return split_string;
}
vector<vector<string>> get_input(string input_file) {
vector<vector<string>> input;
ifstream file(input_file);
if(file.is_open()) {
string line;
while(Getline(file,line)) {
vector<string> words = split(line);
if(words.size() > 1) {
for(unsigned i=1; i<words.size(); i++) {
if(words[i] != "0" && words[i] != "1" && words[i] != "x" && words[i] != "X") {
cerr << "Problem with this input line: " << line << endl;
cerr << "Problem is with word: \"" << words[i] << "\" at position " << i << endl;
return vector<vector<string>>();
}
}
} else {
cerr << "No Actuation Data Found, Please Check input file" << endl;
return vector<vector<string>>();
}
input.push_back(words);
}
file.close();
} else {
cerr << "Input File Not Found" << endl;
return vector<vector<string>>();
}
return input;
}
map<string,vector<string>> parse_edge_list(string input_file) {
map<string,vector<string>> input_graph;
ifstream file(input_file);
if(file.is_open()) {
string line;
int vertices = -1;
int flag = 0;
while(!flag && Getline(file,line)) {
while(line.size() == 0) {
Getline(file,line);
}
vector<string> words = split(line);
if(words.size() != 0) {
if(words[0] == "p") {
vertices = stoi(words[2]);
flag = 1;
}
}
}
if(!flag || vertices == -3) {
cerr << "File is missing parameter line before edge list" << endl;
cerr << "Should be: \"p edge <number of vertices> <number of edges>\"" << endl;
return map<string,vector<string>>();
}
for(int i=0; i<vertices; i++) {
string pre = "v";
string temp;
std::ostringstream convert;
convert << (i+1);
temp = convert.str();
pre.append(temp);
vector<string> base;
input_graph[pre] = base;
}
while(Getline(file,line))
{
vector<string> words = split(line);
if(words[0] == "e") {
string arg1 = "v";
arg1.append(words[1]);
string arg2 = "v";
arg2.append(words[2]);
vector<string> base;
vector<string> base2;
input_graph[arg1].push_back(arg2);
input_graph[arg2].push_back(arg1);
}
}
} else {
cerr << "Input File Not Found" << endl;
return map<string,vector<string>>();
}
return input_graph;
}
//Used to parse test inputs where the first line is the number of
//vertices, and the next lines are the edge matrix
map<string,vector<string>> parse_edge_matrix(string input_file) {
map<string,vector<string>> input_graph;
string pre = "v";
ifstream file(input_file);
if(file.is_open()) {
string line;
Getline(file,line);
int n = stoi(line);
int i = 0;
while(Getline(file,line)) {
i += 1;
vector<string> words = split(line);
if((int)words.size() != n) {
cerr << "Invalid Input, line " << i << " is not the correct length (" << words.size() << "," << n << "): " << line << endl;
for (unsigned i = 0;i < words.size();i++) {
cerr << "\t" << words.at(i) << endl;
}
return map<string,vector<string>>();
}
vector<string> edges;
for(int j = 0; j < n; j++) {
if(words[j] == "1") {
pre = "v";
string temp;
ostringstream convert;
convert << (j+1);
temp = convert.str();
edges.push_back(pre.append(temp));
}
}
pre = "v";
string temp;
ostringstream convert;
convert << i;
temp = convert.str();
input_graph[pre.append(temp)] = edges;
}
if(i != n) {
cerr << "Input is not the right length" << endl;
return map<string,vector<string>>();
}
file.close();
} else {
cerr << "Input File Not Found" << endl;
return map<string,vector<string>>();
}
return input_graph;
}