fast_io

ソースコード

from titan_pylib.others.fast_io import FastIO

view on github

展開済みコード

 1# from titan_pylib.others.fast_io import FastIO
 2import io, os
 3
 4
 5class FastIO:
 6
 7    # https://recruit.gmo.jp/engineer/jisedai/blog/python_standard_io_with_bytesio/
 8
 9    input_buf = io.BytesIO()
10    output_buf = io.BytesIO()
11    num_line = 0
12    BUFSIZE = (1 << 18) - 1
13
14    @classmethod
15    def input(cls):
16        pointer = cls.input_buf.tell()
17        while cls.num_line == 0:
18            byte_data = os.read(0, cls.BUFSIZE)
19            cls.num_line = byte_data.count(b"\n") + (not byte_data)
20            cls.input_buf.seek(0, 2)
21            cls.input_buf.write(byte_data)
22        cls.input_buf.seek(pointer)
23        cls.num_line -= 1
24        return cls.input_buf.readline().decode().rstrip()
25
26    @classmethod
27    def buf_input(cls):
28        pointer = cls.input_buf.tell()
29        while cls.num_line == 0:
30            byte_data = os.read(0, cls.BUFSIZE)
31            cls.num_line = byte_data.count(b"\n") + (not byte_data)
32            cls.input_buf.seek(0, 2)
33            cls.input_buf.write(byte_data)
34        cls.input_buf.seek(pointer)
35        cls.num_line -= 1
36        return cls.input_buf.readline()
37
38    @classmethod
39    def write(cls, *args, sep=" ", end="\n", flush=False):
40        for i in range(len(args) - 1):
41            cls.output_buf.write(str(args[i]).encode())
42            cls.output_buf.write(sep.encode())
43        if args:
44            cls.output_buf.write(str(args[-1]).encode())
45        cls.output_buf.write(end.encode())
46        if flush:
47            cls.flush()
48
49    @classmethod
50    def flush(cls):
51        os.write(1, cls.output_buf.getvalue())
52        cls.output_buf.truncate(0)
53        cls.output_buf.seek(0)
54
55
56buf_input = FastIO.buf_input
57input = FastIO.input
58write = FastIO.write
59flush = FastIO.flush

仕様

class FastIO[source]

Bases: object

BUFSIZE = 262143
classmethod buf_input()[source]
classmethod flush()[source]
classmethod input()[source]
input_buf = <_io.BytesIO object>
num_line = 0
output_buf = <_io.BytesIO object>
classmethod write(*args, sep=' ', end='\n', flush=False)[source]
buf_input()
flush()
input()
write(*args, sep=' ', end='\n', flush=False)