|
|
@ -17,27 +17,72 @@ fn binsearch<'a, V: ?Sized, T: PartialEq<V> + 'a>(slice: &'a [T], find: &V) -> O
|
|
|
|
{
|
|
|
|
{
|
|
|
|
match slice {
|
|
|
|
match slice {
|
|
|
|
[ref a, pivot @ .., ref b] => {
|
|
|
|
[ref a, pivot @ .., ref b] => {
|
|
|
|
match (a==find, b==find) {
|
|
|
|
match (a==find, b==find) {
|
|
|
|
(true, _) => Some(a),
|
|
|
|
(true, _) => Some(a),
|
|
|
|
(_, true) => Some(b),
|
|
|
|
(_, true) => Some(b),
|
|
|
|
_ => binsearch(pivot, find),
|
|
|
|
_ => binsearch(pivot, find),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[ref a] if a == find => Some(a),
|
|
|
|
[ref a] if a == find => Some(a),
|
|
|
|
_ => None,
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn collect_input() -> Box<dyn Iterator<Item=String> + 'static>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if std::env::args_os().len() <= 1 {
|
|
|
|
|
|
|
|
use std::io::{
|
|
|
|
|
|
|
|
self,
|
|
|
|
|
|
|
|
BufRead,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// No args, collect stdin lines
|
|
|
|
|
|
|
|
Box::new(io::BufReader::new(io::stdin()
|
|
|
|
|
|
|
|
.lock())
|
|
|
|
|
|
|
|
.lines()
|
|
|
|
|
|
|
|
.filter_map(Result::ok))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Has arguments, return them
|
|
|
|
|
|
|
|
Box::new(std::env::args().skip(1))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
|
|
|
fn handle_fmt_err<T>(res: std::io::Result<T>)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#[cfg(not(feature="ignore-output-errors"))]
|
|
|
|
|
|
|
|
res.expect("[!] failed to write");
|
|
|
|
|
|
|
|
let _ = res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
|
let mut args: Vec<String> = std::env::args().skip(1).collect();
|
|
|
|
let mut args: Vec<String> = collect_input().collect();
|
|
|
|
reverse(&mut args[..]);
|
|
|
|
reverse(&mut args[..]);
|
|
|
|
//eprintln!("{:?}", binsearch(&args[..], "1")); // It works!
|
|
|
|
//eprintln!("{:?}", binsearch(&args[..], "1")); // It works!
|
|
|
|
#[cfg(feature="output-lines")]
|
|
|
|
#[cfg(feature="output-lines")]
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
|
|
|
use std::io::{
|
|
|
|
|
|
|
|
Write, BufWriter,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature="buffer-output")]
|
|
|
|
|
|
|
|
let mut out = BufWriter::new(std::io::stdout().lock());
|
|
|
|
|
|
|
|
#[cfg(not(feature="buffer-output"))]
|
|
|
|
|
|
|
|
let mut out = std::io::stdout().lock();
|
|
|
|
for x in args.iter() {
|
|
|
|
for x in args.iter() {
|
|
|
|
#[cfg(feature="output-quoted")] println!("{:?}", x);
|
|
|
|
handle_fmt_err({
|
|
|
|
#[cfg(not(feature="output-quoted"))] println!("{}", x);
|
|
|
|
if cfg!(feature="output-quoted") {
|
|
|
|
|
|
|
|
writeln!(&mut out, "{:?}", x)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
//writeln!(&mut out, "{}", x)
|
|
|
|
|
|
|
|
out.write(x.as_bytes())
|
|
|
|
|
|
|
|
.and_then(|_| out.write(b"\n"))
|
|
|
|
|
|
|
|
.map(|_| {})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#[cfg(feature="buffer-output")]
|
|
|
|
|
|
|
|
handle_fmt_err(out.flush());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature="output-lines"))]
|
|
|
|
#[cfg(not(feature="output-lines"))]
|
|
|
|
println!("{:?}", args);
|
|
|
|
println!("{:?}", args);
|
|
|
|