一个有趣的IO板子

encore

2019-07-01 14:18:02

Personal

```cpp namespace input{ constexpr StandardType1 _k_max_input_size = 1 << 21; char buf[_k_max_input_size], *p1 = buf, *p2 = buf; inline StandardType1 getc(void) { return __builtin_expect(p1 == p2, 0) && (p2 = (p1 = buf) + fread(buf, 1, _k_max_input_size, fin), __builtin_expect(p1 == p2, 0)) ? EOF : *p1++; } StandardType1 redi(void) { StandardType1 x = 0; int f = 0; char ch = getc(); while (!std::isdigit(ch)) { if (ch == '-') f = 1; ch = getc(); } while (std::isdigit(ch)) { x = x * 10 + ch - 48; ch = getc(); } return f ? -x : x; } __attribute__((__fastcall__)) StandardType1 redi(StandardType1 &x) { x = 0; int f = 0; char ch = getc(); while (!std::isdigit(ch)) { if (ch == '-') f = 1; ch = getc(); } while (std::isdigit(ch)) { x = x * 10 + ch - 48; ch = getc(); } return f ? -x : x; } template<typename ...Args> __attribute__((__fastcall__)) StandardType1 redi(StandardType1 &x, Args &...args) { return redi(x), redi(args...); } } namespace output{ constexpr StandardType1 _k_max_output_size = 1 << 21; char buf[_k_max_output_size], a[20]; int p, p2= -1; inline __attribute__((destructor)) void flush(void) { fwrite (buf, 1, p2 + 1, fout), p2 = -1; } inline void putch(const char c) { if (__builtin_expect(p2 >= _k_max_output_size - 1, 0)) flush(); buf[++p2] = c; } inline void print(StandardType1 x) { if (p2 > _k_max_output_size >> 1) flush(); if (x < 0) buf[++p2] = 45, x = -x; while(a[++p] = x % 10 + 48, x /= 10); while(buf[++p2] = a[p], --p); } inline void print(const char c, StandardType1 x) { if (p2 > _k_max_output_size >> 1) flush(); if (x < 0) buf[++p2] = 45, x = -x; while(a[++p] = x % 10 + 48, x /= 10); while(buf[++p2] = a[p], --p); buf[++p2] = c; } inline void print_str(const char *_str) { while(putch(*_str), *(++_str)); } inline void print_str(std::string x) { for (char i:x) putch(i); } __attribute__((__fastcall__)) __attribute__((format(printf, 1, 2))) void print(const char *__format, ...) { va_list ap; va_start(ap, __format); std::string ti; bool flag = false; while (*__format) { if (flag) { if (*__format == 'd') ti.append("d"), flag = false; else if (*__format == 'l') ti.append("l"); else if (*__format == '%' && ti.length() == 2) putch('%'), ti.clear(), flag = false; else putch('%'), print_str(ti), putch(*__format), ti.clear(), flag = false; } else { if (*__format == '%') flag = true; else putch(*__format); } if (ti == "d") print(va_arg(ap, int)), ti.clear(); else if (ti == "lld") print(va_arg(ap, long long)), ti.clear(); ++__format; } va_end(ap); } } using input::getc; using input::redi; using output::print; using output::print_str; using output::putch; ``` print用法类似printf,其他不解释了。效率应该还不错,不过以有趣为主。