This repository has been archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparser.cc
249 lines (209 loc) · 5.4 KB
/
parser.cc
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* @file parser.cc
* @author Adam Jez ([email protected])
* @author Roman Blanco ([email protected])
* @date 7.12.2014
* @brief Metody pro tridy, ktere parsuji zdrojove
* soubory a vraci je ve vektoru
*/
#include "parser.h"
/**
* Funkce vrati vektor hodnot v tretim parametru
* @param vec prohledavany seznam
* @param second cislo, ktere se v seznamu hleda
* @param out vektor hodnot, ktery byli v paru s hledanou hodnotou
*/
void getFirstBySecond(vector<pair<int, int>> *vec, int second, vector<int> *out)
{
out->clear();
for(auto conn = vec->begin(); conn != vec->end(); ++conn)
{
if(conn->second == second)
out->push_back(conn->first);
}
}
/**
* Funkce vrati vektor hodnot v tretim parametru
* @param vec prohledavany seznam
* @param first cislo, ktere se v seznamu hleda
* @param out vektor hodnot, ktery byli v paru s hledanou hodnotou
*/
void getSecondByFirst(vector<pair<int, int>> *vec, int first, vector<int> *out)
{
out->clear();
for(auto conn = vec->begin(); conn != vec->end(); ++conn)
{
if(conn->first == first)
out->push_back(conn->second);
}
}
/**
* Funkce vrati mozne nasledovniky/predchazejici zadane hodnoty
* @param vec prohledavany seznam
* @param item cislo, ktere se v seznamu hleda
* @param out vektor hodnot, ktery byli v paru s hledanou hodnotou
*/
void getNext(vector<pair<int, int>> *vec, int item, vector<vector<pair<int, int>>> *out)
{
for(auto conn = vec->begin(); conn != vec->end(); ++conn)
{
if(conn->first == item || conn->second == item)
{
vector<pair<int, int>> v {make_pair(conn->first, conn->second)};
out->push_back(v);
}
}
}
/**
* Vrati nasledujici hodnotu ze seznamu po hodnote from
* @param vec prohledavany seznam
* @param from cislo od ktereho se hleda nasledujici
* @param to cislo, ke kteremu se chceme dostat
* @return vrati hodnotu nasledujiciho prvku, kdyz nenajde, vrati -1
*/
int findNext(vector<pair<int, int>> *vec, int from, int to)
{
vector<vector<pair<int, int>>> result;
getNext(vec, from, &result);
/*for(auto path = result.begin(); path != result.end(); ++path)
{
cout << path->front().first << " " << path->front().second << endl;
}*/
u_int size;
bool change;
while(true)
{
change = false;
for(auto path = result.begin(); path != result.end(); ++path)
{
size = path->size();
auto find = path->back();
if(find.first == to || find.second == to)
{
// Nasli jsme cestu, vratime id dalsiho prvku
auto item = path->front();
//cout << "Nasli jsme: " << item.first << " " << item.second << endl;
if(item.first == from)
return item.second;
else
return item.first;
}
//cout << "posledni: " << find.first << " " << find.second << endl;
for(auto conn = vec->begin(); conn != vec->end(); ++conn)
{
if(from < to && conn->first == find.second && conn->second != find.first)
{
// Nasli jsem cestu
path->push_back(make_pair(conn->first, conn->second));
}
if(from > to && conn->second == find.first && conn->first != find.second)
{
// Nasli jsem cestu
path->push_back(make_pair(conn->first, conn->second));
}
}
if(size != path->size())
change = true;
}
if(!change)
break;
}
return -1;
}
/**
* Funkce projde zdrojovy soubor ve formatu: "X Y\n", kde X a Y jsou cisla spojeni
* @return vrati seznam paru spojeni
*/
vector<pair<int, int>> ConnectionParser::Run()
{
int x,y;
vector<pair<int, int>> result;
_readFile = new ifstream(_fileName);
while(_readFile->good())
{
(*_readFile) >> x >> ws >> y >> ws;
result.push_back(make_pair(x, y));
}
_readFile->close();
return result;
}
/**
* Funkce projde zdrojovy soubor ve formatu: "A STR X Y Z\n", kde X, Y a Z
* jsou cisla spojene s pridavanym objektem, STR je nazev objektu a A je cislo typu objektu
* @return vrati seznam objektu (most, reka, pristav, apod.)
*/
vector<WaterItem *> WaterItemsParser::Run()
{
int type;
string name;
float x, y, z;
WaterItem *item;
vector<WaterItem *> result;
_readFile = new ifstream(_fileName);
while(_readFile->good())
{
(*_readFile) >> type >> ws >> name >> ws >> x >> ws >> y >> z >> ws;
switch(type)
{
case tunnel:
{
Tunnel * tun = new Tunnel(name, x);
item = (WaterItem*)tun;
break;
}
case chamber:
{
Chamber * cham = new Chamber(name, x);
item = (WaterItem*)cham;
break;
}
case port:
{
Port * port = new Port(x);
item = (WaterItem*)port;
break;
}
case channel:
{
Channel * chan = new Channel(x);
item = (WaterItem*)chan;
break;
}
case river:
{
River * riv = new River(x, (bool)y);
item = (WaterItem*)riv;
break;
}
case bridge:
{
Bridge * brid = new Bridge(name, x);
item = (WaterItem*)brid;
break;
}
default:
;
}
result.push_back(item);
}
_readFile->close();
return result;
}
/**
* Funkce projde zdrojovy soubor ve formatu: "X Y A\n", kde X, Y jsou id objektu a A je pocet tisic tun
* @return vrati seznam dvojitych paru, obsahujici id objektu od do a pocet tisic tun mezi nimi za rok
*/
vector<pair<pair<int, int>, float>> TrafficParser::Run()
{
int id1, id2;
float x;
vector<pair<pair<int, int>, float>> result;
_readFile = new ifstream(_fileName);
while(_readFile->good())
{
(*_readFile) >> id1 >> ws >> id2 >> ws >> x >> ws;
result.push_back(make_pair(make_pair(id1, id2),x));
}
_readFile->close();
return result;
}