-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_parser.cpp
More file actions
140 lines (124 loc) · 3.16 KB
/
input_parser.cpp
File metadata and controls
140 lines (124 loc) · 3.16 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
#include "input_parser.h"
void parseGraph(istream& in, Instance* instance) {
string token;
while (true) {
in >> token;
if (not in) break;
if (token == "E") {
int a, b, w;
in >> a >> b >> w;
instance->graph.createEdge(a - 1, b - 1, w);
} else if (token == "Nodes") {
int n;
in >> n;
for (int i = 0; i < n; i++) {
instance->graph.createNode(false);
}
} else if (token == "Edges") {
int m;
in >> m;
} else if (token == "END") {
break;
} else {
cerr << "Input parsing failed!" << endl;
exit(EXIT_FAILURE);
}
}
};
void parseTerminals(istream& in, Instance* instance) {
string token;
while (true) {
in >> token;
if (not in) break;
if (token == "T") {
int id;
in >> id;
//Warning: The section containing the graph must come before the
//section containing the terminals.
instance->graph.nodes[id - 1].terminal = true;
} else if(token == "Terminals") {
int k;
in >> k;
} else if (token == "END") {
break;
} else {
cerr << "Input parsing failed!" << endl;
exit(EXIT_FAILURE);
}
}
};
void parseTreeDecomposition(istream& in, Instance* instance, string x) {
string skip;
if (x == "") {
in >> skip;
} else {
skip = x;
}
if (skip == "c") {
getline(cin, skip);
in >> skip;
}
in >> skip;
int num_bags;
int tw;
int n;
in >> num_bags >> tw >> n;
instance->td.bags.resize(num_bags);
instance->td.tree.resize(num_bags);
getline(in, skip);
for (int i = 0; i < num_bags; i++) {
string line;
getline(in, line);
stringstream line_in(line);
line_in >> skip;
int id;
line_in >> id;
id--;
while (true) {
int node;
line_in >> node;
if (not line_in) break;
instance->td.bags[id].insert(node - 1);
}
}
for (int i = 0; i < num_bags - 1; i++) {
int a, b;
in >> a >> b;
a--;
b--;
instance->td.tree[a].push_back(b);
instance->td.tree[b].push_back(a);
}
if (x == "") in >> skip;
};
Instance* parseInput(istream& in) {
Instance* instance = new Instance();
string token;
while (true) {
in >> token;
if (not in) break;
if (token == "SECTION") {
string type;
in >> type;
if (type == "Graph") {
parseGraph(in, instance);
} else if (type == "Terminals") {
parseTerminals(in, instance);
} else if(type == "Tree") {
in >> type;
parseTreeDecomposition(in, instance, "");
} else {
cerr << "Input parsing failed! type2: " << type << endl;
exit(EXIT_FAILURE);
}
} else if(token == "EOF") {
break;
} else if (token == "c" or token == "s") {
parseTreeDecomposition(in, instance, token);
} else {
cerr << "Input parsing failed! type: " << token << endl;
exit(EXIT_FAILURE);
}
}
return instance;
}