Skip to content

Commit d266b4c

Browse files
committed
Enhanced fast-io.hpp
1 parent 24d99a8 commit d266b4c

1 file changed

Lines changed: 42 additions & 17 deletions

File tree

src/alfred/config/fast-io.hpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,78 @@
66

77
class FastIO {
88
private:
9-
int siz = 100000;
9+
int chunk = 1 << 18;
1010
char *buf, *p1, *p2;
11+
char *obuf, *op;
12+
13+
inline void flush_output(void) {
14+
fwrite(obuf, 1, len, stdout), op = obuf;
15+
}
1116

1217
public:
1318
inline char nc(void) {
14-
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, siz, stdin), p1 == p2) ? EOF : *p1++;
19+
if (p1 == p2) {
20+
p2 = (p1 = buf) + fread(buf, 1, chunk, stdin);
21+
if (p1 == p2) return EOF;
22+
} else return *p1++;
23+
}
24+
inline void pc(char c) {
25+
if (op == obuf + chunk) flush_output();
26+
*op++ = c;
27+
}
28+
FastIO(void) : chunk(1 << 18) {
29+
p1 = p2 = buf = (char *)(malloc(chunk));
30+
op = obuf = (char *)(malloc(chunk));
31+
}
32+
FastIO(int _chunk) : chunk(_chunk) {
33+
p1 = p2 = buf = (char *)(malloc(chunk));
34+
op = obuf = (char *)(malloc(chunk));
1535
}
16-
inline void pc(char c) { putchar(c); }
17-
FastIO(void) { buf = (char *)malloc(siz); }
18-
FastIO(int n) : siz(n) { buf = (char *)malloc(siz); } // n characters.
19-
~FastIO(void) { free(buf); }
20-
inline void resize(int n) {
21-
siz = n;
22-
buf = (char *)realloc(buf, siz);
36+
~FastIO(void) {
37+
flush_output();
38+
free(buf), free(obuf);
2339
}
40+
inline void flush(void) { flush_output(); }
2441
} __buf;
2542

26-
inline void reset_size(int siz) {
27-
__buf.resize(siz);
28-
}
43+
inline void pc(char c) { __buf.pc(c); }
2944

3045
template <class T>
3146
inline void fast_read(T &x) {
3247
x = 0;
48+
static bool neg = false;
3349
static char c = __buf.nc();
34-
while (c < '0' || c > '9') c = __buf.nc();
50+
while (c < '0' || c > '9') {
51+
neg = (c == '-'), c = __buf.nc();
52+
}
3553
while (c >= '0' && c <= '9') {
3654
x = (x << 1) + (x << 3) + (c ^ 48), c = __buf.nc();
3755
}
56+
if (neg) x = -x;
3857
}
3958
template <class T>
4059
inline void write(T x) {
41-
if (x < 0) putchar('-'), x = -x;
60+
if (x < 0) {
61+
__buf.pc('-'), x = -x;
62+
}
4263
if (x > 9) write(x / 10);
43-
putchar((x % 10) ^ 48);
64+
__buf.pc((x % 10) ^ 48);
4465
}
4566
template <int mod>
4667
inline void write(ModInt<mod> x) {
4768
write(x.x);
4869
}
70+
template <typename T, typename... V>
71+
inline void write(T x, V... v) {
72+
write(x), __buf.pc(' '), write(v...);
73+
}
4974
template <class T>
5075
inline void writeln(T x) {
51-
write(x), puts("");
76+
write(x), __buf.pc('\n');
5277
}
5378
template <typename T, typename... V>
5479
inline void writeln(T x, V... v) {
55-
write(x), putchar(' '), writeln(v...);
80+
write(x), __buf.pc(' '), writeln(v...);
5681
}
5782
template <typename T, typename... V>
5883
inline void fast_read(T &t, V &...v) {

0 commit comments

Comments
 (0)