Skip to content

Commit fcf7ad3

Browse files
committed
feat: Enhance FastIO with inline functions for improved performance and readability
1 parent bc1d7d9 commit fcf7ad3

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

test/many_aplusb.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../weilycoder/fast-io.hpp"
44
using namespace weilycoder;
55

6-
static FastIODefault<> io;
6+
static FastIOStd<> io;
77

88
int main() {
99
size_t t = io.read_u64();

test/many_aplusb_debug.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../weilycoder/fast-io.hpp"
44
using namespace weilycoder;
55

6-
static FastIODefault<true> io;
6+
static FastIOStd<true> io;
77

88
int main() {
99
size_t t = io.read_u64();

weilycoder/fast-io.hpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,59 @@ struct FastReadMMap {
3636
munmap(data, file_size);
3737
}
3838

39-
char getchar() {
39+
inline char getchar() {
4040
return static_cast<size_t>(pos - data) < file_size ? *pos++ : EOF;
4141
}
4242

43-
char operator()() { return getchar(); }
43+
inline char operator()() { return getchar(); }
44+
};
45+
46+
template <size_t buffer_size = 1 << 20> struct FastReadFRead {
47+
char buf[buffer_size], *pos = buf, *end = buf;
48+
49+
FastReadFRead() = default;
50+
51+
FastReadFRead(const FastReadFRead &) = delete;
52+
FastReadFRead &operator=(const FastReadFRead &) = delete;
53+
54+
inline char getchar() {
55+
return pos == end && (end = (pos = buf) + fread(buf, 1, buffer_size, stdin),
56+
pos == end)
57+
? EOF
58+
: *pos++;
59+
}
60+
61+
inline void clear() { pos = end = buf; }
62+
63+
inline void reopen() { clear(), fseek(stdin, 0, SEEK_SET); }
64+
65+
inline char operator()() { return getchar(); }
4466
};
4567

4668
template <size_t buffer_size = 1 << 20> struct FastWriteFWrite {
47-
char buffer[buffer_size], *pos = buffer;
69+
char buf[buffer_size], *pos = buf;
4870

4971
FastWriteFWrite() = default;
5072
~FastWriteFWrite() { flush(); }
5173

5274
FastWriteFWrite(const FastWriteFWrite &) = delete;
5375
FastWriteFWrite &operator=(const FastWriteFWrite &) = delete;
5476

55-
void putchar(char c) {
56-
if (pos - buffer == buffer_size)
77+
inline void putchar(char c) {
78+
if (pos - buf == buffer_size)
5779
flush();
5880
*pos++ = c;
5981
}
6082

61-
void flush() {
62-
size_t write_size = pos - buffer;
83+
inline void flush() {
84+
size_t write_size = pos - buf;
6385
if (write_size) {
64-
fwrite(buffer, 1, write_size, stdout);
65-
pos = buffer;
86+
fwrite(buf, 1, write_size, stdout);
87+
pos = buf;
6688
}
6789
}
6890

69-
void operator()(char c) { putchar(c); }
91+
inline void operator()(char c) { putchar(c); }
7092
};
7193

7294
template <typename Reader, typename Writer, bool debug = false> struct FastIO {
@@ -78,23 +100,23 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
78100
FastIO(const FastIO &) = delete;
79101
FastIO &operator=(const FastIO &) = delete;
80102

81-
char getchar() {
103+
inline char getchar() {
82104
if constexpr (debug)
83105
return std::getchar();
84106
else
85107
return reader.getchar();
86108
}
87109

88-
void putchar(char c) {
110+
inline void putchar(char c) {
89111
if constexpr (debug)
90112
std::putchar(c), std::fflush(stdout);
91113
else
92114
writer.putchar(c);
93115
}
94116

95-
void flush() { writer.flush(); }
117+
inline void flush() { writer.flush(); }
96118

97-
uint64_t read_u64() {
119+
inline uint64_t read_u64() {
98120
char c;
99121
do
100122
c = getchar();
@@ -106,7 +128,7 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
106128
return x;
107129
}
108130

109-
void write_u64(uint64_t x) {
131+
inline void write_u64(uint64_t x) {
110132
static char buf[20];
111133
size_t len = 0;
112134
do
@@ -116,11 +138,14 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
116138
putchar(buf[i]);
117139
}
118140

119-
void write_u64_line(uint64_t x) { write_u64(x), putchar('\n'); }
141+
inline void write_u64_line(uint64_t x) { write_u64(x), putchar('\n'); }
120142
};
121143

122144
template <bool debug = false>
123-
using FastIODefault = FastIO<FastReadMMap, FastWriteFWrite<>, debug>;
145+
using FastIOStd = FastIO<FastReadMMap, FastWriteFWrite<>, debug>;
146+
147+
template <bool debug = false>
148+
using FastIOFile = FastIO<FastReadFRead<>, FastWriteFWrite<>, debug>;
124149
} // namespace weilycoder
125150

126151
#endif

0 commit comments

Comments
 (0)