Skip to content

Commit

Permalink
refactor: split fastio
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Feb 22, 2025
1 parent 8a6bea5 commit ac099b9
Show file tree
Hide file tree
Showing 422 changed files with 1,095 additions and 752 deletions.
8 changes: 4 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ notebook:
- ios_container: 容器 IO
code_ext: hpp
test_ext: cpp
- fastio: 快速 IO
- fastin: 快速读入
code_ext: hpp
test_ext: cpp
- fastout: 快速输出
code_ext: hpp
test_ext: cpp
ds:
Expand Down Expand Up @@ -1374,9 +1377,6 @@ notebook:
- strhex2uint_si64: 16 进制字符串转 u32
code_ext: hpp
test_ext: cpp
- u32tostr: u32 转字符串
code_ext: hpp
test_ext: cpp
- u32tostrhex: u32 转 16 进制字符串
code_ext: hpp
test_ext: cpp
Expand Down
64 changes: 0 additions & 64 deletions src/code/fast/u32tostr.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/code/game/npuzzle_data.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TIFALIBS_GAME_NPUZZLE_DATA
#define TIFALIBS_GAME_NPUZZLE_DATA

#include "../util/util.hpp"
#include "../util/traits.hpp"

namespace tifa_libs::game {

Expand Down Expand Up @@ -57,7 +57,7 @@ class NPuzzleData {
swap(node[pre], node[p0]);
}
CEXP auto operator<=>(NPuzzleData CR r) CNE { return node <=> r.node; }
friend std::istream &operator>>(std::istream &is, NPuzzleData &np) NE {
friend auto &operator>>(tifa_libs::istream_c auto &is, NPuzzleData &np) NE {
for (auto &i : np.node) is >> i;
np.p0 = u32(std::ranges::find(np.node, 0) - np.node.begin());
flt_ (u32, p, 0, (u32)np.node.size())
Expand Down
4 changes: 2 additions & 2 deletions src/code/geo2d/circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ struct circle {
CEXP circle(point<FP> CR c, FP r) NE : o(c), r(r) {}
CEXP circle(FP c_x, FP c_y, FP r_) NE : o(c_x, c_y), r(r_) {}

friend std::istream &operator>>(std::istream &is, circle &c) NE { return is >> c.o >> c.r; }
friend std::ostream &operator<<(std::ostream &os, circle CR c) NE { return os << c.o << ' ' << c.r; }
friend auto &operator>>(tifa_libs::istream_c auto &is, circle &c) NE { return is >> c.o >> c.r; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, circle CR c) NE { return os << c.o << ' ' << c.r; }
friend CEXP bool operator==(circle CR l, circle CR r) NE { return l.o == r.o && l.r == r.r; }
friend CEXP bool operator<(circle CR l, circle CR r) NE { return l.o == r.o ? l.r < r.r : l.o < r.o; }

Expand Down
4 changes: 2 additions & 2 deletions src/code/geo2d/cvh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ struct cvh : public polygon<FP> {
if (!inited) strict ? init<true>() : init<false>();
}

friend std::istream &operator>>(std::istream &is, cvh &ch) NE {
friend auto &operator>>(tifa_libs::istream_c auto &is, cvh &ch) NE {
for (auto &i : ch.vs) is >> i;
return is;
}
friend std::ostream &operator<<(std::ostream &os, cvh<FP> CR ch) NE {
friend auto &operator<<(tifa_libs::ostream_c auto &os, cvh<FP> CR ch) NE {
if (ch.vs.empty()) return os;
for (auto it = ch.vs.begin(); it != ch.vs.end() - 1; ++it) os << *it << ' ';
return os << ch.vs.back();
Expand Down
4 changes: 2 additions & 2 deletions src/code/geo2d/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct point {
FP x, y;
CEXPE point(FP x = {}, FP y = {}) NE : x{x}, y{y} {}

friend std::istream &operator>>(std::istream &is, point &p) NE { return is >> p.x >> p.y; }
friend std::ostream &operator<<(std::ostream &os, point CR p) NE { return os << p.x << ' ' << p.y; }
friend auto &operator>>(tifa_libs::istream_c auto &is, point &p) NE { return is >> p.x >> p.y; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, point CR p) NE { return os << p.x << ' ' << p.y; }
// s + (t - s) * r
template <std::floating_point T>
friend CEXP point lerp(point CR s, point CR t, T r) NE { return s + (t - s) * r; }
Expand Down
4 changes: 2 additions & 2 deletions src/code/geo2d/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct polygon {
CEXP polygon(itl<point<FP>> vs_) NE : vs(vs_) {}
CEXP polygon(spn<point<FP>> vs_) NE : vs(vs_.begin(), vs_.end()) {}

friend std::istream &operator>>(std::istream &is, polygon &p) NE {
friend auto &operator>>(tifa_libs::istream_c auto &is, polygon &p) NE {
for (auto &i : p.vs) is >> i;
return is;
}
friend std::ostream &operator<<(std::ostream &os, polygon CR p) NE {
friend auto &operator<<(tifa_libs::ostream_c auto &os, polygon CR p) NE {
if (p.vs.empty()) return os;
for (auto it = p.vs.begin(); it != p.vs.end() - 1; ++it) os << *it << ' ';
return os << p.vs.back();
Expand Down
4 changes: 2 additions & 2 deletions src/code/geo2d/triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ struct triangle {
CEXP triangle(point<FP> CR a, point<FP> CR b, point<FP> CR c) NE : A(a), B(b), C(c) {}
CEXP triangle(FP a_x, FP a_y, FP b_x, FP b_y, FP c_x, FP c_y) NE : A(a_x, a_y), B(b_x, b_y), C(c_x, c_y) {}

friend std::istream &operator>>(std::istream &is, triangle &t) NE { return is >> t.A >> t.B >> t.C; }
friend std::ostream &operator<<(std::ostream &os, triangle CR t) NE { return os << t.A << ' ' << t.B << ' ' << t.C; }
friend auto &operator>>(tifa_libs::istream_c auto &is, triangle &t) NE { return is >> t.A >> t.B >> t.C; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, triangle CR t) NE { return os << t.A << ' ' << t.B << ' ' << t.C; }
friend CEXP bool operator==(triangle CR l, triangle CR r) NE { return l.A == r.A && l.B == r.B && l.C == r.C; }
// (a, b, c)
CEXP pt3<FP> edges() CNE { return {dist_PP(B, C), dist_PP(C, A), dist_PP(A, B)}; }
Expand Down
4 changes: 2 additions & 2 deletions src/code/geo3d/line3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ struct line3d {
point3d<FP> l, r;
CEXP line3d(point3d<FP> CR s, point3d<FP> CR t) NE : l(s), r(t) {}

friend std::istream &operator>>(std::istream &is, line3d &l) NE { return is >> l.l >> l.r; }
friend std::ostream &operator<<(std::ostream &os, line3d CR l) NE { return os << l.l << ' ' << l.r; }
friend auto &operator>>(tifa_libs::istream_c auto &is, line3d &l) NE { return is >> l.l >> l.r; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, line3d CR l) NE { return os << l.l << ' ' << l.r; }
};

} // namespace tifa_libs::geo
Expand Down
2 changes: 1 addition & 1 deletion src/code/geo3d/planev.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct planev {
point3d<FP> const *u, *v, *w;
CEXP planev(point3d<FP> CR a, point3d<FP> CR b, point3d<FP> CR c) NE : u(&a), v(&b), w(&c) {}

friend std::ostream &operator<<(std::ostream &os, planev CR pl) NE { return os << *pl.u << ' ' << *pl.v << ' ' << *pl.w; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, planev CR pl) NE { return os << *pl.u << ' ' << *pl.v << ' ' << *pl.w; }
CEXP point3d<FP> normal() CNE { return cross(*u, *v, *w); }
CEXP FP area2() CNE { return normal().norm(); }
CEXP FP area() CNE { return area2() * (FP).5; }
Expand Down
6 changes: 3 additions & 3 deletions src/code/geo3d/point3d.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TIFALIBS_GEO3D_POINT3D
#define TIFALIBS_GEO3D_POINT3D

#include "../util/util.hpp"
#include "../util/traits.hpp"

namespace tifa_libs::geo {

Expand All @@ -11,8 +11,8 @@ struct point3d {
FP x, y, z;
CEXPE point3d(FP x = FP{}, FP y = FP{}, FP z = FP{}) NE : x(x), y(y), z(z) {}

friend std::istream &operator>>(std::istream &is, point3d &p) NE { return is >> p.x >> p.y >> p.z; }
friend std::ostream &operator<<(std::ostream &os, point3d CR p) NE { return os << p.x << ' ' << p.y << ' ' << p.z; }
friend auto &operator>>(tifa_libs::istream_c auto &is, point3d &p) NE { return is >> p.x >> p.y >> p.z; }
friend auto &operator<<(tifa_libs::ostream_c auto &os, point3d CR p) NE { return os << p.x << ' ' << p.y << ' ' << p.z; }
// s + (t - s) * r
template <std::floating_point T>
friend CEXP point3d lerp(point3d CR s, point3d CR t, T r) NE { return s + (t - s) * r; }
Expand Down
125 changes: 125 additions & 0 deletions src/code/io/fastin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef TIFALIBS_IO_FASTIN
#define TIFALIBS_IO_FASTIN

#include "../util/traits.hpp"
#ifdef __linux__
#include <sys/mman.h>
#include <sys/stat.h>
#endif

namespace tifa_libs {

//! UB if EOF occured during reading
class fastin {
CEXP static u32 BUF = 0x200005;
FILE *f_ = nullptr;
#ifdef __linux__
char *bg, *ed, *p;
struct stat Fl;

template <bool ignore_space = true>
void read_str(strn &n) NE {
char *l = p, *r;
if CEXP (ignore_space) {
while (!isgraph(*l++));
r = l--;
while (isgraph(*r++));
} else {
while (!isprint(*l++));
r = l--;
while (isprint(*r++));
}
n.assign(l, p = r);
}

public:
fastin(FILE *f = stdin) NE { assert(f), rebind(f); }
~fastin() NE { rebind(); }
void rebind(FILE *f = nullptr) NE {
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
if (!f_) munmap(bg, Fl.st_size + 1);
#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
if (!f) return;
auto fd = fileno(f_ = f);
fstat(fd, &Fl);
p = (bg = (char *)mmap(nullptr, Fl.st_size + 4, PROT_READ, MAP_PRIVATE, fd, 0));
ed = bg + Fl.st_size;
madvise(bg, Fl.st_size + 4, MADV_SEQUENTIAL);
}
bool iseof() NE { return p == ed; }
#else
char buf[BUF], *ed, *p;

template <bool ignore_space>
void read_str(strn &n) NE {
n.clear(), n.reserve(24);
if CEXP (ignore_space) {
n.push_back(skip_cntrls().get());
while (isgraph(get())) n.push_back(peek());
} else {
n.push_back(skip_cntrl().get());
while (isprint(get())) n.push_back(peek());
}
}

public:
fastin(FILE *f = stdin) NE { rebind(f); }
void rebind(FILE *f) NE { f_ = f, p = ed = buf; }
bool iseof() NE {
if (p == ed) [[unlikely]]
ed = (p = buf) + fread(buf, 1, BUF, f_);
return p == ed;
}
#endif
char peek() NE {
if (iseof()) [[unlikely]]
return EOF;
return *p;
}
char get_unchk() NE { return *p++; }
char get() NE {
if (iseof()) [[unlikely]]
return EOF;
return get_unchk();
}
#define SKIP(name, pred) \
fastin &skip_##name() NE { \
if (pred(peek())) get_unchk(); \
return *this; \
}
SKIP(cntrl, iscntrl)
SKIP(cntrls, !isgraph)
SKIP(ndigit, !isdigit)
#undef SKIP
template <class T>
requires(std::integral<T> && !char_c<T>)
fastin &operator>>(T &n) NE {
n = 0;
bool is_neg = false;
if CEXP (std::signed_integral<T>) {
while (!isdigit(peek())) is_neg |= peek() == '-', get_unchk();
} else skip_ndigit();
while (isdigit(peek())) (n *= 10) += peek() & 15, get_unchk();
if CEXP (sint_c<T>)
if (is_neg) n = -n;
return *this;
}
fastin &operator>>(std::floating_point auto &n) NE {
static strn s;
(*this >> s), std::from_chars(s.begin().base(), s.end().base(), n);
return *this;
}
//! ignore cntrl and space
fastin &operator>>(char_c auto &n) NE { return (n = skip_cntrls().get()), *this; }
fastin &operator>>(strn &n) NE { return read_str<true>(n), *this; }
fastin &getline(strn &n) NE { return read_str<false>(n), *this; }
//! NOT ignore cntrl and space
fastin &strict_read(char_c auto &n) NE { return (n = get()), *this; }
fastin &operator>>(fastin &(*func)(fastin &)) NE { return func(*this); }
};
inline fastin fin;
inline fastin &ws(fastin &f) NE { return f.skip_cntrls(); }

} // namespace tifa_libs

#endif
Loading

0 comments on commit ac099b9

Please sign in to comment.