-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast IO.cpp
More file actions
128 lines (97 loc) · 3.08 KB
/
fast IO.cpp
File metadata and controls
128 lines (97 loc) · 3.08 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
// template by neal
namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
size_t input_pos = 0, input_len = 0;
char output_buffer[BUFFER_SIZE];
int output_pos = 0;
char number_buffer[100];
uint8_t lookup[100];
void _update_input_buffer() {
input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
input_pos = 0;
if (input_len == 0)
input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
if (input_pos >= input_len)
_update_input_buffer();
return input_buffer[advance ? input_pos++ : input_pos];
}
inline bool isspace(char c) {
return (unsigned char) (c - '\t') < 5 || c == ' ';
}
inline void read_char(char &c) {
while (isspace(next_char(false)))
next_char();
c = next_char();
}
template<typename T>
inline void read_int(T &number) {
bool negative = false;
number = 0;
while (!isdigit(next_char(false)))
if (next_char() == '-')
negative = true;
do {
number = 10 * number + (next_char() - '0');
} while (isdigit(next_char(false)));
if (negative)
number = -number;
}
template<typename T, typename... Args>
inline void read_int(T &number, Args &... args) {
read_int(number);
read_int(args...);
}
inline void read_str(string &str) {
while (isspace(next_char(false)))
next_char();
str.clear();
do {
str += next_char();
} while (!isspace(next_char(false)));
}
void _flush_output() {
fwrite(output_buffer, sizeof(char), output_pos, stdout);
output_pos = 0;
}
inline void write_char(char c) {
if (output_pos == BUFFER_SIZE)
_flush_output();
output_buffer[output_pos++] = c;
}
template<typename T>
inline void write_int(T number, char after = '\0') {
if (number < 0) {
write_char('-');
number = -number;
}
int length = 0;
while (number >= 10) {
uint8_t lookup_value = lookup[number % 100];
number /= 100;
number_buffer[length++] = char((lookup_value & 15) + '0');
number_buffer[length++] = char((lookup_value >> 4) + '0');
}
if (number != 0 || length == 0)
write_char(char(number + '0'));
for (int i = length - 1; i >= 0; i--)
write_char(number_buffer[i]);
if (after)
write_char(after);
}
inline void write_str(const string &str, char after = '\0') {
for (char c : str)
write_char(c);
if (after)
write_char(after);
}
void init() {
// Make sure _flush_output() is called at the end of the program.
bool exit_success = atexit(_flush_output) == 0;
assert(exit_success);
for (int i = 0; i < 100; i++)
lookup[i] = uint8_t((i / 10 << 4) + i % 10);
}
}