1use std::{
2 io::stdin,
3 str::FromStr,
4 sync::{Mutex, MutexGuard},
5};
6
7type Buffer = Vec<String>;
8
9pub trait FromBuf: Sized {
16 type Err;
17
18 fn from_buf(b: &mut Buffer) -> Result<Self, Self::Err>;
29}
30
31impl<T: FromStr> FromBuf for T {
32 type Err = T::Err;
33
34 fn from_buf(b: &mut Buffer) -> Result<Self, Self::Err> {
35 T::from_str(b.remove(0).as_str())
36 }
37}
38
39pub struct InputStream {
41 buf_mutex: Mutex<Buffer>,
42}
43
44impl InputStream {
45 const fn new() -> Self {
46 Self {
47 buf_mutex: Mutex::new(Buffer::new()),
48 }
49 }
50
51 fn get_buf(&self) -> MutexGuard<Vec<String>> {
52 let mut buf = self.buf_mutex.lock().unwrap();
53 if buf.is_empty() {
54 let mut temp_str = String::new();
55 stdin().read_line(&mut temp_str).unwrap();
56 buf.extend(
57 temp_str
58 .trim()
59 .split_ascii_whitespace()
60 .map(|s| String::from_str(s).unwrap()),
61 );
62 }
63 buf
64 }
65
66 pub fn read<T: FromBuf + Default>(&self) -> T {
70 T::from_buf(&mut self.get_buf()).unwrap_or_default()
71 }
72
73 pub fn read_str(&self) -> String {
75 self.get_buf().remove(0)
76 }
77
78 pub fn read_char(&self) -> char {
80 let mut buf = self.get_buf();
81 let c = buf[0].remove(0);
82 if buf[0].is_empty() {
83 buf.remove(0);
84 }
85 c
86 }
87}
88
89#[allow(non_upper_case_globals)]
93pub static rin: InputStream = InputStream::new();
94
95#[macro_export]
115macro_rules! input {
116 (mut $name:ident : $type:ty) => {
117 let mut $name: $type = rinput::rin.read();
118 };
119
120 ($name:ident : $type:ty) => {
121 let $name: $type = rinput::rin.read();
122 };
123
124 (mut $name:ident : $type:ty, $($tail:tt)+) => {
125 input!(mut $name:$type);
126 input!($($tail)+);
127 };
128
129 ($name:ident : $type:ty, $($tail:tt)+) => {
130 input!($name:$type);
131 input!($($tail)+);
132 };
133}