11#ifndef WEILYCODER_FAST_IO_HPP
22#define WEILYCODER_FAST_IO_HPP
33
4+ /* *
5+ * @file fast-io.hpp
6+ * @brief Fast Input/Output Utilities
7+ */
8+
49#include < cstddef>
510#include < cstdint>
611#include < cstdio>
1419
1520namespace weilycoder {
1621#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
22+ /* *
23+ * @brief Fast Reader using Memory-Mapped I/O
24+ */
1725struct FastReadMMap {
1826 size_t file_size;
1927 char *data, *pos;
2028
2129 FastReadMMap (const FastReadMMap &) = delete ;
2230 FastReadMMap &operator =(const FastReadMMap &) = delete ;
2331
32+ /* *
33+ * @brief Constructs a FastReadMMap object and memory-maps stdin.
34+ * If mapping fails, sets file_size to max size_t.
35+ */
2436 FastReadMMap () {
2537 struct stat st;
2638
@@ -35,19 +47,34 @@ struct FastReadMMap {
3547 pos = data;
3648 }
3749
50+ /* *
51+ * @brief Destructor that unmaps the memory-mapped file.
52+ */
3853 ~FastReadMMap () {
3954 if (file_size != std::numeric_limits<size_t >::max ())
4055 munmap (data, file_size);
4156 }
4257
58+ /* *
59+ * @brief Gets the next character from the memory-mapped input.
60+ * @return The next character, or EOF if the end of the file is reached.
61+ */
4362 inline char getchar () {
4463 return static_cast <size_t >(pos - data) < file_size ? *pos++ : EOF;
4564 }
4665
66+ /* *
67+ * @brief Alias for getchar().
68+ * @return The next character from input.
69+ */
4770 inline char operator ()() { return getchar (); }
4871};
4972#endif
5073
74+ /* *
75+ * @brief Fast Reader using fread
76+ * @tparam buffer_size Size of the internal buffer (default: 1 MB)
77+ */
5178template <size_t buffer_size = 1 << 20 > struct FastReadFRead {
5279 char buf[buffer_size], *pos = buf, *end = buf;
5380
@@ -56,20 +83,39 @@ template <size_t buffer_size = 1 << 20> struct FastReadFRead {
5683 FastReadFRead (const FastReadFRead &) = delete ;
5784 FastReadFRead &operator =(const FastReadFRead &) = delete ;
5885
86+ /* *
87+ * @brief Gets the next character from input using buffered fread.
88+ * @return The next character, or EOF if the end of the file is reached.
89+ */
5990 inline char getchar () {
6091 return pos == end && (end = (pos = buf) + fread (buf, 1 , buffer_size, stdin),
6192 pos == end)
6293 ? EOF
6394 : *pos++;
6495 }
6596
97+ /* *
98+ * @brief Clears the internal buffer.
99+ */
66100 inline void clear () { pos = end = buf; }
67101
102+ /* *
103+ * @brief Reopens the input stream by clearing the buffer
104+ * and seeking to the beginning.
105+ */
68106 inline void reopen () { clear (), fseek (stdin, 0 , SEEK_SET); }
69107
108+ /* *
109+ * @brief Alias for getchar().
110+ * @return The next character from input.
111+ */
70112 inline char operator ()() { return getchar (); }
71113};
72114
115+ /* *
116+ * @brief Fast Writer using fwrite
117+ * @tparam buffer_size Size of the internal buffer (default: 1 MB)
118+ */
73119template <size_t buffer_size = 1 << 20 > struct FastWriteFWrite {
74120 char buf[buffer_size], *pos = buf;
75121
@@ -79,12 +125,20 @@ template <size_t buffer_size = 1 << 20> struct FastWriteFWrite {
79125 FastWriteFWrite (const FastWriteFWrite &) = delete ;
80126 FastWriteFWrite &operator =(const FastWriteFWrite &) = delete ;
81127
128+ /* *
129+ * @brief Puts a character into the output buffer.
130+ * Flushes the buffer if it is full.
131+ * @param c Character to put into the buffer.
132+ */
82133 inline void putchar (char c) {
83134 if (pos - buf == buffer_size)
84135 flush ();
85136 *pos++ = c;
86137 }
87138
139+ /* *
140+ * @brief Flushes the output buffer to stdout.
141+ */
88142 inline void flush () {
89143 size_t write_size = pos - buf;
90144 if (write_size) {
@@ -93,9 +147,19 @@ template <size_t buffer_size = 1 << 20> struct FastWriteFWrite {
93147 }
94148 }
95149
150+ /* *
151+ * @brief Alias for putchar().
152+ * @param c Character to put into the buffer.
153+ */
96154 inline void operator ()(char c) { putchar (c); }
97155};
98156
157+ /* *
158+ * @brief Fast Input/Output handler combining a Reader and a Writer.
159+ * @tparam Reader Type of the input reader.
160+ * @tparam Writer Type of the output writer.
161+ * @tparam debug If true, uses standard I/O functions for debugging.
162+ */
99163template <typename Reader, typename Writer, bool debug = false > struct FastIO {
100164 Reader reader;
101165 Writer writer;
@@ -105,24 +169,45 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
105169 FastIO (const FastIO &) = delete ;
106170 FastIO &operator =(const FastIO &) = delete ;
107171
172+ /* *
173+ * @brief Gets the next character from input.
174+ * @return The next character from input.
175+ */
108176 inline char getchar () {
109177 if constexpr (debug)
110178 return std::getchar ();
111179 else
112180 return reader.getchar ();
113181 }
114182
183+ /* *
184+ * @brief Puts a character into output.
185+ * @param c Character to put into output.
186+ */
115187 inline void putchar (char c) {
116188 if constexpr (debug)
117189 std::putchar (c), std::fflush (stdout);
118190 else
119191 writer.putchar (c);
120192 }
121193
194+ /* *
195+ * @brief Flushes the output buffer.
196+ */
122197 inline void flush () { writer.flush (); }
123198
124- int32_t abs (int32_t x) { return x >= 0 ? x : -x; }
125-
199+ /* *
200+ * @brief Computes the absolute value of a 32-bit integer.
201+ * @param x The integer to compute the absolute value for.
202+ * @return The absolute value of x.
203+ */
204+ static int32_t abs (int32_t x) { return x >= 0 ? x : -x; }
205+
206+ /* *
207+ * @brief Reads an unsigned integer of type utype from input.
208+ * @tparam utype Unsigned integer type to read.
209+ * @return The read unsigned integer.
210+ */
126211 template <typename utype> inline utype _read_u () {
127212 char c;
128213 do
@@ -135,6 +220,13 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
135220 return x;
136221 }
137222
223+ /* *
224+ * @brief Reads a signed integer of type itype from input.
225+ * @tparam itype Signed integer type to read.
226+ * @tparam utype Unsigned integer type used for intermediate calculations,
227+ * should be the corresponding unsigned type of itype.
228+ * @return The read signed integer.
229+ */
138230 template <typename itype, typename utype> inline itype _read_i () {
139231 char c;
140232 bool neg = false ;
@@ -149,6 +241,12 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
149241 return static_cast <itype>(neg ? -x : x);
150242 }
151243
244+ /* *
245+ * @brief Writes an unsigned integer of type utype to output.
246+ * @tparam utype Unsigned integer type to write.
247+ * @tparam bufsize Size of the internal buffer used for conversion.
248+ * @param x The unsigned integer to write.
249+ */
152250 template <typename utype, size_t bufsize> inline void _write_u (utype x) {
153251 static char buf[bufsize];
154252 size_t len = 0 ;
@@ -159,6 +257,12 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
159257 putchar (buf[i]);
160258 }
161259
260+ /* *
261+ * @brief Writes a signed integer of type itype to output.
262+ * @tparam itype Signed integer type to write.
263+ * @tparam bufsize Size of the internal buffer used for conversion.
264+ * @param x The signed integer to write.
265+ */
162266 template <typename itype, size_t bufsize> inline void _write_i (itype x) {
163267 bool neg = x < 0 ;
164268 static char buf[bufsize];
@@ -172,22 +276,76 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
172276 putchar (buf[i]);
173277 }
174278
279+ /* *
280+ * @brief Reads a signed 32-bit integer from input.
281+ * @return The read signed 32-bit integer.
282+ */
175283 inline int32_t read_i32 () { return _read_i<int32_t , uint32_t >(); }
284+
285+ /* *
286+ * @brief Reads an unsigned 32-bit integer from input.
287+ * @return The read unsigned 32-bit integer.
288+ */
176289 inline uint32_t read_u32 () { return _read_u<uint32_t >(); }
177290
291+ /* *
292+ * @brief Writes a signed 32-bit integer to output.
293+ * @param x The signed 32-bit integer to write.
294+ */
178295 inline void write_i32 (int32_t x) { _write_i<int32_t , 10 >(x); }
296+
297+ /* *
298+ * @brief Writes an unsigned 32-bit integer to output.
299+ * @param x The unsigned 32-bit integer to write.
300+ */
179301 inline void write_u32 (uint32_t x) { _write_u<uint32_t , 10 >(x); }
180302
303+ /* *
304+ * @brief Reads a signed 64-bit integer from input.
305+ * @return The read signed 64-bit integer.
306+ */
181307 inline int64_t read_i64 () { return _read_i<int64_t , uint64_t >(); }
308+
309+ /* *
310+ * @brief Reads an unsigned 64-bit integer from input.
311+ * @return The read unsigned 64-bit integer.
312+ */
182313 inline uint64_t read_u64 () { return _read_u<uint64_t >(); }
183314
315+ /* *
316+ * @brief Writes a signed 64-bit integer to output.
317+ * @param x The signed 64-bit integer to write.
318+ */
184319 inline void write_i64 (int64_t x) { _write_i<int64_t , 20 >(x); }
320+
321+ /* *
322+ * @brief Writes an unsigned 64-bit integer to output.
323+ * @param x The unsigned 64-bit integer to write.
324+ */
185325 inline void write_u64 (uint64_t x) { _write_u<uint64_t , 20 >(x); }
186326
327+ /* *
328+ * @brief Reads a signed 128-bit integer from input.
329+ * @return The read signed 128-bit integer.
330+ */
187331 inline __int128 read_i128 () { return _read_i<__int128, unsigned __int128>(); }
332+
333+ /* *
334+ * @brief Reads an unsigned 128-bit integer from input.
335+ * @return The read unsigned 128-bit integer.
336+ */
188337 inline unsigned __int128 read_u128 () { return _read_u<unsigned __int128>(); }
189338
339+ /* *
340+ * @brief Writes a signed 128-bit integer to output.
341+ * @param x The signed 128-bit integer to write.
342+ */
190343 inline void write_i128 (__int128 x) { _write_i<__int128, 40 >(x); }
344+
345+ /* *
346+ * @brief Writes an unsigned 128-bit integer to output.
347+ * @param x The unsigned 128-bit integer to write.
348+ */
191349 inline void write_u128 (unsigned __int128 x) {
192350 _write_u<unsigned __int128, 40 >(x);
193351 }
0 commit comments