commit 47df7a1e91d9936149ec5f3a3a3a5658f88de54a Author: Avril Date: Mon Apr 4 18:51:41 2022 +0100 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 − 中吉 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..41c1ff6 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..49ec163 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "reverse" +version = "0.2.0" +authors = ["Avril "] +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] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..35b76c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + + +reverse: + RUSTFLAGS="-C target-cpu=native" cargo build --release + strip target/release/$@ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..28b2b09 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,44 @@ + +//#[inline] +fn reverse(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 + '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 = 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); +}