-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_reader.cpp
271 lines (227 loc) · 8.04 KB
/
input_reader.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "input_reader.h"
#include "stat_reader.h"
#include <algorithm>
#include <unordered_map>
using namespace std;
namespace transport_catalogue {
namespace query {
inline std::vector<std::string_view> Split(std::string_view str, char delim) {
std::vector<std::string_view> result;
int64_t pos = 0;
const int64_t pos_end = str.npos;
while (true) {
int64_t delimiter = str.find(delim, pos);
result.push_back(delimiter == pos_end ? str.substr(pos) : str.substr(pos, delimiter - pos));
if (delimiter == pos_end) {
break;
} else {
pos = delimiter + 1;
}
}
return result;
}
pair<string_view, string_view> Command::ParseCoordinates(string_view latitude, string_view longitude) {
while (longitude.front() == ' ') {
longitude.remove_prefix(1);
}
while (latitude.front() == ' ') {
latitude.remove_prefix(1);
}
while (longitude.back() == ' ') {
longitude.remove_suffix(1);
}
while (latitude.back() == ' ') {
latitude.remove_suffix(1);
}
return make_pair(latitude, longitude);
}
vector<pair<string_view, string_view>> Command::ParseDistances(vector<string_view> vec_input) {
vector<pair<string_view, string_view>> result;
size_t i = 2;
while (i < vec_input.size()) {
while (vec_input[i].front() == ' ') {
vec_input[i].remove_prefix(1);
}
auto pos_m = vec_input[i].find('m');
string_view dist, stop;
dist = vec_input[i].substr(0, pos_m);
auto pos_t = vec_input[i].find('t');
pos_t += 2;
stop = vec_input[i].substr(pos_t, vec_input[i].length());
while (stop.front() == ' ') {
stop.remove_prefix(1);
}
while (stop.back() == ' ') {
stop.remove_suffix(1);
}
result.push_back({dist, stop});
++i;
}
return result;
}
vector<string_view> Command::ParseBuses(vector<string_view> vec_input) {
vector<string_view> result;
vector<string_view> parsed_buses;
if (vec_input.size() > 3) {
if (desc_command.find('-') != string::npos) {
route_type = RouteType::Direct;
parsed_buses = Split(desc_command, '-');
}
if (desc_command.find('>') != string::npos) {
route_type = RouteType::Round;
parsed_buses = Split(desc_command, '>');
}
}
for (size_t i = 0; i < parsed_buses.size(); ++i) {
while (parsed_buses[i].front() == ' ') {
parsed_buses[i].remove_prefix(1);
}
while (parsed_buses[i].back() == ' ') {
parsed_buses[i].remove_suffix(1);
}
result.push_back(parsed_buses[i]);
}
return result;
}
void Command::ParseCommandString(string input) {
static std::unordered_map<std::string, QueryType> const table = {
{"Stop", QueryType::StopX}, {"Bus", QueryType::BusX}
};
origin_command = move(input);
auto vec_input = Split(origin_command, ' ');
auto pos_start = origin_command.find_first_not_of(' ');
auto pos_end_of_command = origin_command.find(' ', pos_start);
string temp_type = {origin_command.begin() + pos_start, origin_command.begin() + pos_end_of_command};
switch (table.at(temp_type)) {
case QueryType::StopX:
type = QueryType::StopX;
if (auto pos = origin_command.find(':'); pos != string::npos) {
//Stop stop1:
desc_command = origin_command.substr(pos + 2, origin_command.length() - pos - 1);
auto temp = Split(desc_command, ',');
for (size_t i = pos_end_of_command; i < pos; ++i) {
name += origin_command[i];
}
while (name.front() == ' ') {
name.erase(name.begin());
}
coordinates = ParseCoordinates(temp[0], temp[1]);
if (temp.size() > 2) {
distances = ParseDistances(temp);
}
}
else {
//Stop stop1
for (size_t i = pos_end_of_command; i < origin_command.size(); ++i) {
name += origin_command[i];
}
while (name.front() == ' ') {
name.erase(name.begin());
}
while (name.back() == ' ') {
name.erase(name.end() - 1);
}
break;
}
break;
case QueryType::BusX:
type = QueryType::BusX;
if (auto pos = origin_command.find(':'); pos != string::npos) {
desc_command = origin_command.substr(pos + 2, origin_command.length() - pos - 1);
//Bus bus1:
for (size_t i = pos_end_of_command; i < pos; ++i) {
name += origin_command[i];
}
while (name.front() == ' ') {
name.erase(name.begin());
}
route = ParseBuses(vec_input);
}
else {
//Bus bus1
for (size_t i = pos_end_of_command; i < origin_command.size(); ++i) {
name += origin_command[i];
}
while (name.front() == ' ') {
name.erase(name.begin());
}
while (name.back() == ' ') {
name.erase(name.end() - 1);
}
break;
}
break;
}
}
void InputReader::ParseInput() {
int query_count;
cin >> query_count;
cin.ignore();
string command;
for (int i = 0; i < query_count; ++i) {
getline(cin, command);
Command cur_command;
cur_command.ParseCommandString(move(command));
commands_.push_back(move(cur_command));
}
}
void InputReader::Load(TransportCatalogue& tc) {
//firstly with description
auto it_desc = partition(commands_.begin(), commands_.end(), [](Command com) {
return !com.desc_command.empty();
});
//firstly stop queries
auto it_stops = partition(commands_.begin(), it_desc, [](Command com) {
return com.type == QueryType::StopX;
});
//Stop X:
for (auto cur_it = commands_.begin(); cur_it != it_stops; ++cur_it) {
InputReader::LoadCommand(tc, *cur_it, 0);
}
//set stop distances
for (auto cur_it = commands_.begin(); cur_it != it_stops; ++cur_it) {
InputReader::LoadCommand(tc, *cur_it, 1);
}
//Bus X:
for (auto cur_it = it_stops; cur_it != it_desc; ++cur_it) {
InputReader::LoadCommand(tc, *cur_it, 0);
}
//last queries (output)
for (auto cur_it = it_desc; cur_it != commands_.end(); ++cur_it) {
InputReader::LoadCommand(tc, *cur_it, 0);
}
}
void InputReader::LoadCommand(TransportCatalogue& tc, Command com, bool dist) {
switch (com.type) {
case QueryType::StopX:
if (com.coordinates != pair<string_view, string_view>()) {
string lat = {com.coordinates.first.begin(), com.coordinates.first.end()};
string lon = {com.coordinates.second.begin(), com.coordinates.second.end()};
if (dist == false) {
tc.AddStop(com.name, stod(lat), stod(lon));
}
else {
if (!com.distances.empty()) {
for (auto& [dist, stop] : com.distances) {
string dist_str = {dist.begin(), dist.end()};
tc.SetStopDistance(com.name, stoull(dist_str), stop);
}
}
}
}
else {
output::OutputStopAbout(tc, com.name);
}
break;
case QueryType::BusX:
if (!com.route.empty()) {
tc.AddRoute(com.name, com.route_type, com.route);
}
else {
output::OutputRouteAbout(tc, com.name);
}
break;
}
}
}//namespace query
}//namespace transport_catalogue