通用模板

· · 个人记录

前言

把以后可能会用到的通用的高弹性(?)模板放上来。

我是重工业选手,习惯写长板子。如果需要可以自取。

正题

快读快写

目前暂不支持输出浮点数,弹性也不是很高,并且不支持除 freopen() 外的文件输入输出和交互(即无法 flush)。

代码:

namespace read_write { char c; }
namespace read {
using namespace read_write;
const size_t READ_MAXN = 1 << 14;
const size_t READ_ARR_SIZE = READ_MAXN;
char inbuf[READ_ARR_SIZE];
char *curinpos = inbuf + READ_MAXN;
char *end_inbuf = inbuf + READ_MAXN;
inline char gc() {
    if (__builtin_expect(curinpos == inbuf + READ_MAXN, 0)) {
        int siz = fread(inbuf, 1, READ_MAXN, stdin);
        if (__builtin_expect(siz < READ_MAXN, 0)) memset(inbuf + siz, EOF, READ_MAXN - siz);
        curinpos = inbuf;
    }
    return *curinpos++;
}
inline short readshort() {
    short eax(0), flg(0);
    c = gc();
    while (c < 48) flg |= (c == '-'), c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return flg ? -eax : eax;
}
inline unsigned short readushort() {
    unsigned short eax(0);
    c = gc();
    while (c < 48) c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return eax;
}
inline int readint() {
    int eax(0), flg(0);
    c = gc();
    while (c < 48) flg |= (c == '-'), c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return flg ? -eax : eax;
}
inline unsigned readuint() {
    unsigned eax(0);
    c = gc();
    while (c < 48) c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return eax;
}
inline long readlong() {
    long eax(0), flg(0);
    c = gc();
    while (c < 48) flg |= (c == '-'), c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return flg ? -eax : eax;
}
inline unsigned long readulong() {
    unsigned long eax(0);
    c = gc();
    while (c < 48) c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return eax;
}
inline long long readllong() {
    long long eax(0), flg(0);
    c = gc();
    while (c < 48) flg |= (c == '-'), c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return flg ? -eax : eax;
}
inline unsigned long long readullong() {
    unsigned long long eax(0);
    c = gc();
    while (c < 48) c = gc();
    while (c > 47) eax = (eax << 3) + (eax << 1) + (c & 15), c = gc();
    return eax;
}
inline float readfloat() {
    bool flg = false, decimal = false;
    float eax(0);
    c = gc();
    while (c < 48) {
        flg |= (c == '-');
        if (c == '.') goto read_decimal;
        c = gc();
    }
    if (c == '.') goto read_decimal;
    while (c > 47 && c < 58) eax = eax * 10 + (c & 15), c = gc();
    if (c != '.') return flg ? -eax : eax;
read_decimal:
    decimal = true;
    float unit = 0.1;
    c = gc();
    while (c > 47 && c < 58) {
        eax += unit * (c & 15);
        unit *= 0.1;
        c = gc();
    }
    if (c == 'F' || c == 'f') c = gc();
    return flg ? -eax : eax;
}
inline double readdouble() {
    bool flg = false, decimal = false;
    float eax(0);
    c = gc();
    while (c < 48) {
        flg |= (c == '-');
        if (c == '.') goto read_decimal;
        c = gc();
    }
    if (c == '.') goto read_decimal;
    while (c > 47 && c < 58) eax = eax * 10 + (c & 15), c = gc();
    if (c != '.') return flg ? -eax : eax;
read_decimal:
    decimal = true;
    double unit = 0.1;
    c = gc();
    while (c > 47 && c < 58) {
        eax += unit * (c & 15);
        unit *= 0.1;
        c = gc();
    }
    return flg ? -eax : eax;
}
inline long double readldouble() {
    bool flg = false, decimal = false;
    long double eax(0);
    c = gc();
    while (c < 48) {
        flg |= (c == '-');
        if (c == '.') goto read_decimal;
        c = gc();
    }
    if (c == '.') goto read_decimal;
    while (c > 47 && c < 58) eax = eax * 10 + (c & 15), c = gc();
    if (c != '.') return flg ? -eax : eax;
read_decimal:
    decimal = true;
    long double unit = 0.1;
    c = gc();
    while (c > 47 && c < 58) {
        eax += unit * (c & 15);
        unit *= 0.1;
        c = gc();
    }
    if (c == 'L' || c == 'l') c = gc();
    return flg ? -eax : eax;
}
}
namespace write {
using namespace read_write;
const size_t WRITE_MAXN = 1 << 14;
const size_t WRITE_ARR_SIZE = WRITE_MAXN;
char outbuf[WRITE_ARR_SIZE];
char *curoutpos = outbuf;
char *end_outbuf = outbuf + WRITE_MAXN;
inline void pc(char x) {
    if (__builtin_expect(curoutpos == outbuf + WRITE_MAXN, 0)) {
        fwrite(outbuf, 1, WRITE_MAXN, stdout);
        curoutpos = outbuf;
    }
    *curoutpos++ = x;
}
template <typename _Tp, typename std::enable_if<std::is_integral<_Tp>::value && std::is_unsigned<_Tp>::value, bool>::type = true>
inline void put(const _Tp& val) {
    _Tp x(val);
    static char buf[std::numeric_limits<_Tp>::digits * 2136 / 643 + 5];
    static int curpos;
    curpos = 0;
    do {
        buf[curpos++] = x % 10;
        x /= 10;
    } while (x);
    while (curpos--) pc(buf[curpos] | 48);
}
template <typename _Tp, typename std::enable_if<std::is_integral<_Tp>::value && std::is_signed<_Tp>::value, bool>::type = true>
inline void put(const _Tp& val) {
    _Tp x(val);
    static char buf[std::numeric_limits<_Tp>::digits * 2136 / 643 + 5];
    static int curpos;
    curpos = 0;
    if (x < 0) {
        pc('-');
        x = -x;
    }
    do {
        buf[curpos++] = x % 10;
        x /= 10;
    } while (x);
    while (curpos--) pc(buf[curpos] | 48);
}
struct _AutoFlush {
    ~_AutoFlush() { fwrite(outbuf, 1, curoutpos - outbuf, stdout); }
} __flush_helper;
inline void putshort(short x) { put(x); }
inline void putushort(unsigned short x) { put(x); }
inline void putint(int x) { put(x); }
inline void putuint(unsigned x) { put(x); }
inline void putlong(long x) { put(x); }
inline void putulong(unsigned long x) { put(x); }
inline void putllong(long long x) { put(x); }
inline void putullong(unsigned long long x) { put(x); }
}
using read::readshort;
using read::readushort;
using read::readint;
using read::readuint;
using read::readlong;
using read::readulong;
using read::readllong;
using read::readullong;
using read::readfloat;
using read::readdouble;
using read::readldouble;
using write::putshort;
using write::putushort;
using write::putint;
using write::putuint;
using write::putlong;
using write::putulong;
using write::putllong;
using write::putullong;
using write::put;
#define getchar read::gc
#define putchar write::pc

随机数

目前只支持非负整型。

代码:

template <typename _Tp, typename std::enable_if<std::is_integral<_Tp>::value && std::is_unsigned<_Tp>::value, bool>::type = true>
struct my_rand {
    typedef _Tp result_type;
    constexpr _Tp min() const { return std::numeric_limits<_Tp>::min(); }
    constexpr _Tp max() const { return std::numeric_limits<_Tp>::max(); }
    result_type _M_seed;
    inline result_type operator()(result_type) const;
    inline result_type operator()();
    inline void seed(result_type);
    inline my_rand(result_type x = (result_type)5489u) : _M_seed(x) {}
};
template <>
inline unsigned int my_rand<unsigned int>::
operator()(unsigned int x) const {
    float tmp = x;
    return *(unsigned int*)&tmp;
}
template <>
inline unsigned int my_rand<unsigned int>::
operator()() { return _M_seed += operator()(_M_seed); }
template <>
inline unsigned long long my_rand<unsigned long long>::
operator()(unsigned long long x) const {
    double tmp = x;
    return *(unsigned long long*)&tmp;
}
template <>
inline unsigned long long my_rand<unsigned long long>::
operator()() { return _M_seed += operator()(_M_seed); }