Last active
September 8, 2018 11:43
-
-
Save qryxip/196790d77ce2ea87f501fab4b4a16c79 to your computer and use it in GitHub Desktop.
Rustで競プロをやるための標準入力取得法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_imports)] | |
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))] | |
#[macro_use] | |
mod _input { | |
use std::fmt; | |
use std::io::{self, Read}; | |
use std::str::{self, FromStr}; | |
macro_rules! input { | |
($($name:ident : $t:tt), *,) => { | |
input!($($name: $t),*) | |
}; | |
($($name:ident : $t:tt), *) => { | |
let ($($name, )*) = { | |
let mut _scanned = ::_input::Scanned::new(::std::io::stdin(), 1024); | |
$(let $name = _read_value!(_scanned, $t);)* | |
($($name, )*) | |
}; | |
$(let _ = &$name;)* | |
}; | |
} | |
macro_rules! _read_value { | |
($scanned:expr, ($($t:tt), *)) => { | |
($(_read_value!($scanned, $t)), *) | |
}; | |
($scanned:expr, [$t:tt; $n:expr]) => { | |
(0..$n).map(|_| _read_value!($scanned, $t)).collect::<Vec<_>>() | |
}; | |
($scanned:expr, _bytes) => { | |
_read_value!($scanned, String).into_bytes() | |
}; | |
($scanned:expr, _index) => { | |
_read_value!($scanned, usize) - 1 | |
}; | |
($scanned:expr, $t:ty) => { | |
$scanned.next::<$t>() | |
}; | |
} | |
#[derive(Default)] | |
pub struct Scanned { | |
buf: Vec<u8>, | |
pos: usize, | |
} | |
impl Scanned { | |
pub fn new<R: Read>(mut input: R, estimated: usize) -> Self { | |
let mut buf = Vec::with_capacity(estimated); | |
let _ = io::copy(&mut input, &mut buf).unwrap(); | |
// if buf.last() != Some(&b'\n') { | |
// buf.push(b'\n'); | |
// } | |
// assert!(buf.iter().all(|&b| b <= 0x7f)); | |
Scanned { buf: buf, pos: 0 } | |
} | |
#[inline] | |
pub fn next<T: FromStr>(&mut self) -> T | |
where | |
T::Err: fmt::Debug, | |
{ | |
let mut start = None; | |
loop { | |
match (self.buf[self.pos], start.is_some()) { | |
(b' ', true) | (b'\n', true) => break, | |
(_, true) | (b' ', false) | (b'\n', false) => self.pos += 1, | |
(_, false) => start = Some(self.pos), | |
} | |
} | |
let target = &self.buf[start.unwrap()..self.pos]; | |
unsafe { str::from_utf8_unchecked(target) }.parse().unwrap() | |
} | |
} | |
} | |
#[allow(dead_code)] | |
mod output { | |
use std::io::{self, BufWriter, StdoutLock, Write as _Write}; | |
pub fn with_stdout<F: FnMut(&mut BufWriter<StdoutLock>)>(mut f: F) { | |
let stdout = io::stdout(); | |
let mut stdout = BufWriter::new(stdout.lock()); | |
f(&mut stdout); | |
stdout.flush().unwrap(); | |
} | |
} | |
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; | |
use std::io::Write; | |
use std::ops::{ | |
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, | |
Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign, | |
}; | |
use std::str::{self, FromStr}; | |
use std::{char, cmp, f32, f64, fmt, i16, i32, i64, i8, thread, u16, u32, u64, u8}; | |
fn main() { | |
input! { | |
n: usize, | |
v: [u64; n], | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_imports)] | |
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))] | |
#[macro_use] | |
mod _input { | |
use std::fmt; | |
use std::io::BufRead; | |
use std::str::{self, FromStr}; | |
macro_rules! input { | |
($($name:ident : $t:tt), *,) => { | |
input!($($name: $t),*) | |
}; | |
($($name:ident : $t:tt), *) => { | |
let ($($name, )*) = { | |
let _stdin = ::std::io::stdin(); | |
// ::_input::SCANNER.with(|_scanner| { | |
// let mut _scanner = _scanner.borrow_mut(); | |
// $( | |
// let $name = _read_value!(_scanner, $t); | |
// )* | |
// ($($name, )*) | |
// }) | |
let mut _scanner = ::_input::Scanner::new( | |
::std::io::BufReader::new(_stdin.lock()), | |
1024, | |
); | |
$(let $name = _read_value!(_scanner, $t);)* | |
($($name, )*) | |
}; | |
$(let _ = &$name;)* | |
}; | |
} | |
macro_rules! _read_value { | |
($scanner:expr, ($($t:tt), *)) => { | |
($(_read_value!($scanner, $t)), *) | |
}; | |
($scanner:expr, [$t:tt; $n:expr]) => { | |
(0..$n).map(|_| _read_value!($scanner, $t)).collect::<Vec<_>>() | |
}; | |
($scanner:expr, _bytes) => { | |
_read_value!($scanner, String).into_bytes() | |
}; | |
($scanner:expr, _index) => { | |
_read_value!($scanner, usize) - 1 | |
}; | |
($scanner:expr, $t:ty) => { | |
$scanner.next::<$t>() | |
}; | |
} | |
// use std::cell::RefCell; | |
// use std::io::{self, BufReader, Stdin}; | |
// thread_local! { | |
// pub static SCANNER: RefCell<Scanner<BufReader<Stdin>>> = { | |
// RefCell::new(Scanner::new(BufReader::new(io::stdin()), 0)) | |
// }; | |
// } | |
pub struct Scanner<R: BufRead> { | |
rdr: R, | |
buf: Vec<u8>, | |
pos: usize, | |
} | |
impl<R: BufRead> Scanner<R> { | |
pub fn new(rdr: R, capacity: usize) -> Self { | |
Scanner { | |
rdr: rdr, | |
buf: Vec::with_capacity(capacity), | |
pos: 0, | |
} | |
} | |
#[inline] | |
pub fn next<T: FromStr>(&mut self) -> T | |
where | |
T::Err: fmt::Debug, | |
{ | |
if self.buf.is_empty() { | |
self.read_next_line(); | |
} | |
let mut start = None; | |
loop { | |
match (self.buf[self.pos], start.is_some()) { | |
// (c, _) if (c > 0x7f) => panic!("non-ASCII characer"), | |
(b' ', true) | (b'\n', true) => break, | |
(_, true) | (b' ', false) => self.pos += 1, | |
(b'\n', false) => self.read_next_line(), | |
(_, false) => start = Some(self.pos), | |
} | |
} | |
let target = &self.buf[start.unwrap()..self.pos]; | |
unsafe { str::from_utf8_unchecked(target) }.parse().unwrap() | |
} | |
#[inline] | |
fn read_next_line(&mut self) { | |
self.pos = 0; | |
self.buf.clear(); | |
if self.rdr.read_until(b'\n', &mut self.buf).unwrap() == 0 { | |
panic!("reached EOF"); | |
} | |
// if self.buf.last() != Some(&b'\n') { | |
// self.buf.push(b'\n'); | |
// } | |
} | |
} | |
} | |
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; | |
use std::io::Write; | |
use std::ops::{ | |
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, | |
Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign, | |
}; | |
use std::str::{self, FromStr}; | |
use std::{char, cmp, f32, f64, fmt, i16, i32, i64, i8, thread, u16, u32, u64, u8}; | |
fn main() { | |
input! { | |
n: usize, | |
v: [u64; n], | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment