Source code for titan_pylib.others.fast_io

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