-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
77 lines (75 loc) · 1.76 KB
/
parser.cpp
File metadata and controls
77 lines (75 loc) · 1.76 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
class InputReader {
public:
InputReader() {
in = stdin;
cursor = 0;
fread(buffer, 1, SIZE, in);
}
InputReader(const char *input_file) {
in = fopen(input_file, "r");
cursor = 0;
fread(buffer, 1, SIZE, in);
}
template <typename T>
InputReader& operator>>(T &nr) {
while (!isdigit(buffer[cursor]))
advance();
nr = 0;
while (isdigit(buffer[cursor])) {
nr *= 10;
nr += buffer[cursor] - '0';
advance();
}
return (*this);
}
private:
FILE *in;
static const int SIZE = (1 << 17);
char buffer[SIZE];
int cursor;
void advance() {
++ cursor;
if (cursor == SIZE) {
cursor = 0;
fread(buffer, 1, SIZE, in);
}
}
};
class OutputWriter {
public:
OutputWriter() {
out = stdout;
cursor = 0;
}
OutputWriter(const char *output_file) {
out = fopen(output_file, "w");
cursor = 0;
}
~OutputWriter() { flush(); }
OutputWriter& operator<<(int nr) {
char digits[10];
int cnt = 0;
do {
digits[cnt ++] = (nr % 10 + '0');
nr /= 10;
} while (nr);
for (int i = cnt - 1; i >= 0; -- i)
(*this) << digits[i];
return (*this);
}
OutputWriter& operator<<(const char &ch) {
if (cursor == SIZE)
flush();
buffer[cursor ++] = ch;
return (*this);
}
void flush() {
fwrite(buffer, 1, cursor, out);
cursor = 0;
}
private:
FILE *out;
static const int SIZE = (1 << 17);
char buffer[SIZE];
int cursor;
};