soi_io/
lib.rs

1use std::fmt::{Debug, Display};
2use std::io;
3use std::str::FromStr;
4use text_io::read;
5
6/// Reads a single line and converts it to a vector of type T
7/// # Example
8/// ```
9/// use soi_io::read_vec;
10/// let v: Vec<usize> = read_vec();
11/// ```
12/// # Panics
13/// Panics if the input is not a valid vector of type T
14pub fn read_vec<T>() -> Vec<T> where <T as FromStr>::Err: Debug, T: FromStr {
15    let mut input = String::new();
16    io::stdin().read_line(&mut input).unwrap();
17    input
18        .trim()
19        .split(' ')
20        .map(|x| x.to_string())
21        // delete all empty strings
22        .filter(|x| !x.is_empty())
23        .collect::<Vec<String>>()
24        .iter()
25        .filter(|x| *x != " ")
26        .map(|x| match x.parse::<T>() {
27            Ok(x) => x,
28            Err(e) => panic!("Error parsing input: {:?}", e),
29        })
30        .collect::<Vec<T>>()
31}
32
33/// Calls the read macro from text_io
34pub fn read<T>() -> T where <T as FromStr>::Err: Debug, T: FromStr + Display {
35    read!()
36}
37
38/// Reads a single line and converts it to a vector of type T
39/// # Example
40/// ```
41/// use soi_io::read_vec_len;
42/// let v: Vec<usize> = read_vec_len(3);
43/// ```
44/// # Panics
45/// Panics if the input is not a valid vector of type T
46pub fn read_vec_len<T>(len: usize) -> Vec<T> where <T as FromStr>::Err: Debug, T: FromStr + Display {
47    let mut v = Vec::with_capacity(len);
48    for _ in 0..len {
49        let a = read!();
50        v.push(a);
51    }
52    v
53}
54/// Prints the result of a test case in the format:
55/// Case #<case>: <result>
56#[macro_export]
57macro_rules! print_test_case {
58    ($case:expr, $($arg:tt)*) => {
59        println!("Case #{}: {}", $case, format_args!("{}", $($arg)*));
60    };
61}