Skip to content

Commit 09b281b

Browse files
committed
Add template for fastIO
1 parent 8a78ee4 commit 09b281b

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

fastio.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using std::string;
2+
3+
static struct IO {
4+
char tmp[1 << 10];
5+
6+
// fast input routines
7+
char cur;
8+
9+
//#define nextChar() (cur = getc_unlocked(stdin))
10+
//#define peekChar() (cur)
11+
inline char nextChar() { return cur = getc_unlocked(stdin); }
12+
inline char peekChar() { return cur; }
13+
14+
inline operator bool() { return peekChar(); }
15+
inline static bool isBlank(char c) { return (c < '-' && c); }
16+
inline bool skipBlanks() { while (isBlank(nextChar())); return peekChar() != 0; }
17+
18+
inline IO& operator >> (char & c) { c = nextChar(); return *this; }
19+
20+
inline IO& operator >> (char * buf) {
21+
if (skipBlanks()) {
22+
if (peekChar()) {
23+
*(buf++) = peekChar();
24+
while (!isBlank(nextChar())) *(buf++) = peekChar();
25+
} *(buf++) = 0; } return *this; }
26+
27+
inline IO& operator >> (string & s) {
28+
if (skipBlanks()) { s.clear(); s += peekChar();
29+
while (!isBlank(nextChar())) s += peekChar(); }
30+
return *this; }
31+
32+
inline IO& operator >> (double & d) { if ((*this) >> tmp) sscanf(tmp, "%lf", &d); return *this; }
33+
34+
#define defineInFor(intType) \
35+
inline IO& operator >>(intType & n) { \
36+
if (skipBlanks()) { \
37+
int sign = +1; \
38+
if (peekChar() == '-') { \
39+
sign = -1; \
40+
n = nextChar() - '0'; \
41+
} else \
42+
n = peekChar() - '0'; \
43+
while (!isBlank(nextChar())) { \
44+
n += n + (n << 3) + peekChar() - 48; \
45+
} \
46+
n *= sign; \
47+
} \
48+
return *this; \
49+
}
50+
51+
defineInFor(int)
52+
defineInFor(unsigned int)
53+
defineInFor(long long)
54+
55+
// fast output routines
56+
57+
//#define putChar(c) putc_unlocked((c), stdout)
58+
inline void putChar(char c) { putc_unlocked(c, stdout); }
59+
inline IO& operator << (char c) { putChar(c); return *this; }
60+
inline IO& operator << (const char * s) { while (*s) putChar(*s++); return *this; }
61+
62+
inline IO& operator << (const string & s) { for (int i = 0; i < (int)s.size(); ++i) putChar(s[i]); return *this; }
63+
64+
char * toString(double d) { sprintf(tmp, "%lf%c", d, '\0'); return tmp; }
65+
inline IO& operator << (double d) { return (*this) << toString(d); }
66+
67+
68+
#define defineOutFor(intType) \
69+
inline char * toString(intType n) { \
70+
char * p = (tmp + 30); \
71+
if (n) { \
72+
bool isNeg = 0; \
73+
if (n < 0) isNeg = 1, n = -n; \
74+
while (n) \
75+
*--p = (n % 10) + '0', n /= 10; \
76+
if (isNeg) *--p = '-'; \
77+
} else *--p = '0'; \
78+
return p; \
79+
} \
80+
inline IO& operator << (intType n) { return (*this) << toString(n); }
81+
82+
defineOutFor(int)
83+
defineOutFor(long long)
84+
85+
#define endl ('\n')
86+
#define cout __io__
87+
#define cin __io__
88+
} __io__;

0 commit comments

Comments
 (0)