ホーム > libalgo

Scanner

概要

現在デフォルトの IO は使いづらく,こういうのが無いとやってられない.chokudai 師匠の Scanner を参考に実装.

実装

#[allow(dead_code)]
fn cin() -> Scanner<std::io::Stdin> {
    Scanner::new(std::io::stdin())
}

#[allow(dead_code)]
pub struct Scanner<T> {
    buf: Vec<u8>,
    len: usize,
    idx: usize,
    reader: T,
}

#[allow(dead_code)]
impl<Reader: std::io::Read> Scanner<Reader> {
    fn new(r: Reader) -> Scanner<Reader> {
        Scanner {
            buf: vec![0; 8192],
            len: 0,
            idx: 0,
            reader: r,
        }
    }

    fn next<T: std::str::FromStr>(&mut self) -> T {
        self.wrapped::<T>().unwrap()
    }

    fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.next()).collect()
    }

    fn mat<T: std::str::FromStr>(&mut self, r: usize, c: usize) -> Vec<Vec<T>> {
        (0..r).map(|_| self.vec(c)).collect()
    }

    fn vec_char(&mut self) -> Vec<char> {
        self.next_token().take().unwrap().chars().collect()
    }

    fn mat_char(&mut self, r: usize) -> Vec<Vec<char>> {
        (0..r).map(|_| self.vec_char()).collect()
    }

    fn wrapped<T: std::str::FromStr>(&mut self) -> Option<T> {
        self.next_token().and_then(|s| s.parse::<_>().ok())
    }

    fn next_token(&mut self) -> Option<String> {
        let mut res = String::with_capacity(16);
        while let Some(c) = self.get_u8() {
            let d = c as char;
            if !d.is_whitespace() {
                res.push(d);
            } else if res.len() != 0 {
                self.unget_u8(c);
                break;
            }
        }
        if res.len() == 0 { None } else { Some(res) }
    }

    fn next_line(&mut self) -> String {
        self.next_line_wrapped().unwrap()
    }

    fn next_line_wrapped(&mut self) -> Option<String> {
        let c = self.get_u8();
        if c.is_none() {
            return None;
        }
        let mut line = String::with_capacity(20);
        line.push(c.unwrap() as char);
        loop {
            let c = self.get_u8();
            if c.is_none() || c.unwrap() == b'\n' {
                // コメントはC++等での仕様
                // if c.is_some() {
                //     self.unget_u8(b'\n');
                // }
                return Some(line);
            }
            line.push(c.unwrap() as char);
        }
    }

    fn has_next(&mut self) -> bool {
        loop {
            let c = self.get_u8();
            if c.is_none() {
                return false;
            }
            let c = c.unwrap();
            if !(c as char).is_whitespace() {
                self.unget_u8(c);
                return true;
            }
        }
    }

    fn get_u8(&mut self) -> Option<u8> {
        if self.idx == self.len {
            match self.reader.read(&mut self.buf[..]) {
                Ok(l) if l > 0 => {
                    self.idx = 0;
                    self.len = l;
                }
                _ => return None,
            }
        }
        self.idx += 1;
        Some(self.buf[self.idx - 1])
    }

    fn unget_u8(&mut self, c: u8) {
        self.idx = self.idx - 1;
        self.buf[self.idx] = c;
    }
}