haar_lib/macros/
io.rs

1//! `get!`, `input!`
2
3/// [`crate::io::fastio::FastIO`]を第一引数に、型を第二引数にとって、入力のパースを行う。
4#[macro_export]
5macro_rules! get {
6    ( $in:expr; [$a:tt $(as $to:ty)*; $num:expr] ) => {
7        (0..$num).map(|_| get!($in; $a $(as $to)*)).collect::<Vec<_>>()
8    };
9
10    ( $in:expr; ($($type:tt $(as $to:ty)*),*) ) => {
11        ($(get!($in; $type $(as $to)*)),*)
12    };
13
14    ( $in:expr; i8 ) => { $in.read_i64() as i8 };
15    ( $in:expr; i16 ) => { $in.read_i64() as i16 };
16    ( $in:expr; i32 ) => { $in.read_i64() as i32 };
17    ( $in:expr; i64 ) => { $in.read_i64() };
18    ( $in:expr; isize ) => { $in.read_i64() as isize };
19
20    ( $in:expr; u8 ) => { $in.read_u64() as u8 };
21    ( $in:expr; u16 ) => { $in.read_u64() as u16 };
22    ( $in:expr; u32 ) => { $in.read_u64() as u32 };
23    ( $in:expr; u64 ) => { $in.read_u64() };
24    ( $in:expr; usize ) => { $in.read_u64() as usize };
25
26    ( $in:expr; char) => { $in.read_char() };
27
28    ( $in:expr; [u8]) => { $in.read_bytes() };
29    ( $in:expr; [char] ) => { $in.read_chars() };
30    ( $in:expr; String ) => { $in.read_string() };
31
32    ( $in:expr; $from:tt as $to:ty ) => { <$to>::from(get!($in; $from)) };
33}
34
35/// [`crate::io::fastio::FastIO`]を第一引数にとり、第二引数以降に`変数名: 型`を連ねる。
36#[macro_export]
37macro_rules! input {
38    ( @inner $in:expr, $name:pat, $type:tt ) => {
39        let $name = get!($in; $type);
40    };
41    ( @inner $in:expr, $name:pat, $type:tt as $to:ty ) => {
42        let $name = get!($in; $type as $to);
43    };
44
45    ( $in:expr; $( $names:pat = $type:tt $(as $to:ty)? ),* ) => {
46        $(input!(@inner $in, $names, $type $(as $to)?);)*
47    }
48}
49
50/// [`crate::io::fastio::FastIO`]を第一引数にとり、第二引数以降を空白区切りで出力する。
51#[macro_export]
52macro_rules! output {
53    ( @one $io:expr, $a:expr ) => {
54        $io.write($a);
55    };
56
57    ( $io:expr; $a:expr, $($rest:expr),* ) => {
58        output!(@one $io, $a);
59        $(
60            $io.write(" ");
61            $io.write($rest);
62        )*
63        $io.writeln("");
64    };
65}