-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
334 lines (279 loc) · 10.3 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <limits>
#include <functional>
#include <locale>
// Если перенесимость значения не имеет, можно взять uint64_t
using count_t = unsigned long long;
/** ДКА для разбора URL'ов */
class URLFiniteStateMachine {
public:
URLFiniteStateMachine() :
currentState(State::init)
{};
/** Анализирует символ, переводя автомат в соответствующее состояние */
void consume(char ch) {
switch(currentState) {
case State::init:
if('h' == ch) {
currentState = State::prefix_h;
} else {
currentState = State::error;
}
break;
case State::prefix_h:
if('t' == ch) {
currentState = State::prefix_t_1;
} else {
currentState = State::error;
}
break;
case State::prefix_t_1:
if('t' == ch) {
currentState = State::prefix_t_2;
} else {
currentState = State::error;
}
break;
case State::prefix_t_2:
if('p' == ch) {
currentState = State::prefix_p;
} else {
currentState = State::error;
}
break;
case State::prefix_p:
if('s' == ch) {
currentState = State::prefix_s;
} else if(':' == ch) {
currentState = State::prefix_colon;
} else {
currentState = State::error;
}
break;
case State::prefix_s:
if(':' == ch) {
currentState = State::prefix_colon;
} else {
currentState = State::error;
}
break;
case State::prefix_colon:
if('/' == ch) {
currentState = State::prefix_slash_1;
} else {
currentState = State::error;
}
break;
case State::prefix_slash_1:
if('/' == ch) {
currentState = State::prefis_slash_2;
} else {
currentState = State::error;
}
break;
case State::prefis_slash_2:
if(isDomainContent(ch)) {
currentState = State::domain_content;
addToDomain(ch);
} else {
currentState = State::error;
}
break;
case State::domain_content:
if(isDomainContent(ch)) {
currentState = State::domain_content;
addToDomain(ch);
} else if('/' == ch) {
currentState = State::path_slash;
path.push_back(ch);
} else {
currentState = State::success;
path.push_back('/');
}
break;
case State::path_slash:
if(isPathContent(ch)) {
currentState = State::path_content;
path.push_back(ch);
} else {
currentState = State::success;
}
break;
case State::path_content:
if(isPathContent(ch)) {
currentState = State::path_content;
path.push_back(ch);
} else {
currentState = State::success;
}
break;
case State::error:
throw std::invalid_argument("FSM already in error state");
case State::success:
throw std::invalid_argument("FSM already in success state");
}
}
void addToDomain(char ch) {
// Домены не регистрозависимы
domain.push_back(std::tolower(ch, std::locale()));
}
std::string&& takeDomain() {
return std::move(domain);
}
std::string&& takePath() {
return std::move(path);
}
bool isSuccess() const {
return State::success == currentState;
}
bool isError() const {
return State::error == currentState;
}
private:
/** Состояния автомата */
enum class State {
init,
prefix_h,
prefix_t_1,
prefix_t_2,
prefix_p,
prefix_s,
prefix_colon,
prefix_slash_1,
prefis_slash_2,
domain_content,
path_slash,
path_content,
error,
success
};
bool isDomainContent(char ch) {
return isAlpha(ch) || isNumeric(ch) || '.' == ch || '-' == ch;
}
bool isPathContent(char ch) {
return isAlpha(ch) || isNumeric(ch) || '.' == ch || ',' == ch || '/' == ch || '+' == ch || '_' == ch;
}
bool isAlpha(char ch) {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z');
}
bool isNumeric(char ch) {
return '0' <= ch && ch <= '9';
}
/** Текущее состояние автомата */
State currentState;
std::string domain;
std::string path;
};
/** Разбирает URL'ы (их домены и пути) из потока символов, ведя подсчет встреченных URL'ов */
class URLParser {
public:
/** Число встреченных неуникальных URL'ов */
count_t urlCounter;
/** Число встереченных доменов */
std::map<std::string, count_t> countByDomain;
/** Число встреченных путей */
std::map<std::string, count_t> countByPath;
URLParser() :
urlCounter(0),
countByDomain(),
countByPath(),
parsers()
{};
/**
* Обработать следующий символ разбираемой последовательности.
*
* @param ch Следующий символ в разбираемой последовательности.
* @return Домены, разбор которых был полностью завершен на этом шаге.
*/
void consume(char ch) {
// Новый символ может быть началом нового URL'а
parsers.push_back(URLFiniteStateMachine());
for(auto iter = parsers.begin(); iter != parsers.end(); ) {
iter->consume(ch);
if(iter->isSuccess()) {
++urlCounter;
countByDomain[iter->takeDomain()] += 1;
countByPath[iter->takePath()] += 1;
iter = parsers.erase(iter);
} else if(iter->isError()) {
iter = parsers.erase(iter);
} else {
++iter;
}
}
}
private:
/** Автоматы, в данный момент не в конечном состоянии */
std::vector<URLFiniteStateMachine> parsers;
};
void printStats(std::ofstream& out, const std::string& header, const count_t topCount,
std::map<std::string, count_t>& countByValue);
std::multimap<count_t, std::string, std::greater<count_t>> revertIndex(std::map<std::string, count_t>& countByStr);
int main(int argc, char** argv) {
if(argc != 3 && argc != 5) {
std::cerr << "Wrong count of arguments: " << argc << std::endl;
return EXIT_FAILURE;
}
count_t topCount = std::numeric_limits<count_t>::max();
if(argc == 5) {
if(argv[1] != std::string("-n")) {
std::cerr << "Invalid flag " << argv[1] << std::endl;
return EXIT_FAILURE;
}
try {
topCount = std::stoull(argv[2]);
} catch(const std::exception& exc) {
std::cerr << "Invalid count argument " << argv[2] << std::endl;
return EXIT_FAILURE;
}
}
std::ifstream in(argv[argc - 2]);
if(!in) {
std::cerr << "File \"" << argv[argc - 2] << "\" not found" << std::endl;
return EXIT_FAILURE;
}
std::ofstream out(argv[argc - 1]);
if(!out) {
std::cerr << "Can't open \"" << argv[argc - 1] << "\" file for output" << std::endl;
return EXIT_FAILURE;
}
URLParser urlParser;
char ch;
while(in.get(ch)) {
urlParser.consume(ch);
}
// Завершаем автоматы, находящиеся в корректном незавершенном состоянии разбора, когда поток уже вычитан
urlParser.consume('\n');
out << "total urls " << urlParser.urlCounter << ", domains " << urlParser.countByDomain.size()
<< ", paths " << urlParser.countByPath.size() << std::endl;
printStats(out, "domains", topCount, urlParser.countByDomain);
printStats(out, "paths", topCount, urlParser.countByPath);
return EXIT_SUCCESS;
}
void printStats(std::ofstream& out, const std::string& header, const count_t topCount,
std::map<std::string, count_t>& countByValue)
{
std::multimap<count_t, std::string, std::greater<count_t>> valuesByCount = revertIndex(countByValue);
int current = 1;
out << std::endl << "top " << header << std::endl;
for(auto iter = valuesByCount.cbegin(); iter != valuesByCount.cend(); ++iter) {
if(current > topCount) {
break;
}
out << iter->first << " " << iter->second << std::endl;
++current;
}
valuesByCount.clear();
}
std::multimap<count_t, std::string, std::greater<count_t>> revertIndex(std::map<std::string, count_t>& countByStr) {
std::multimap<count_t, std::string, std::greater<count_t>> result;
for(auto iter = countByStr.cbegin(); iter != countByStr.cend(); ) {
// multimap сохраняет порядок вставки значений по ключю с C++11, поэтому сохраняется лексикографический порядок
result.emplace(iter->second, iter->first);
iter = countByStr.erase(iter);
}
return result;
}