Changed print output to line-by-line (can be re-set to debug print output with feature flag.)

Fortune for reverse's current commit: Middle blessing − 中吉
master
Avril 3 years ago
commit 47df7a1e91
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -0,0 +1 @@
/target

7
Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "reverse"
version = "0.2.0"

@ -0,0 +1,24 @@
[package]
name = "reverse"
version = "0.2.0"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["output-lines"]
# Output as lines instead of `["2", "1", "0"]`
output-lines = []
# Print each string escaped
output-quoted = []
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip=true
[dependencies]

@ -0,0 +1,5 @@
reverse:
RUSTFLAGS="-C target-cpu=native" cargo build --release
strip target/release/$@

@ -0,0 +1,44 @@
//#[inline]
fn reverse<T>(slice: &mut [T])
{
match slice {
[ref mut a, ref mut rest @ .., ref mut b] => {
std::mem::swap(a, b);
reverse(rest)
},
[] | [_] => (),
}
}
/// This actually works! And is so easily parallelisable with something like rayon, or asyncing by spawning/creating the tail-call into a new task, then either waiting them concurrently or in parallen (spawn or created future from just calling the function without awaiting it)
#[allow(dead_code)]
fn binsearch<'a, V: ?Sized, T: PartialEq<V> + 'a>(slice: &'a [T], find: &V) -> Option<&'a T>
{
match slice {
[ref a, pivot @ .., ref b] => {
match (a==find, b==find) {
(true, _) => Some(a),
(_, true) => Some(b),
_ => binsearch(pivot, find),
}
},
[ref a] if a == find => Some(a),
_ => None,
}
}
fn main() {
let mut args: Vec<String> = std::env::args().skip(1).collect();
reverse(&mut args[..]);
//eprintln!("{:?}", binsearch(&args[..], "1")); // It works!
#[cfg(feature="output-lines")]
{
for x in args.iter() {
#[cfg(feature="output-quoted")] println!("{:?}", x);
#[cfg(not(feature="output-quoted"))] println!("{}", x);
}
}
#[cfg(not(feature="output-lines"))]
println!("{:?}", args);
}
Loading…
Cancel
Save