From 5c889fed0d633d9301a6ab4097bc2cec7e6cbccf Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 11 Feb 2021 23:57:03 +0000 Subject: [PATCH] rust version start --- shuffle3rs/Cargo.lock | 83 +++++++++++++++++++++++++++++++++++++++ shuffle3rs/Cargo.toml | 10 +++++ shuffle3rs/src/ext.rs | 34 ++++++++++++++++ shuffle3rs/src/main.rs | 9 +++++ shuffle3rs/src/shuffle.rs | 42 ++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 shuffle3rs/Cargo.lock create mode 100644 shuffle3rs/Cargo.toml create mode 100644 shuffle3rs/src/ext.rs create mode 100644 shuffle3rs/src/main.rs create mode 100644 shuffle3rs/src/shuffle.rs diff --git a/shuffle3rs/Cargo.lock b/shuffle3rs/Cargo.lock new file mode 100644 index 0000000..4ccc27e --- /dev/null +++ b/shuffle3rs/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "shuffle3rs" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/shuffle3rs/Cargo.toml b/shuffle3rs/Cargo.toml new file mode 100644 index 0000000..8e725ee --- /dev/null +++ b/shuffle3rs/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "shuffle3rs" +version = "0.1.0" +authors = ["Avril "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/shuffle3rs/src/ext.rs b/shuffle3rs/src/ext.rs new file mode 100644 index 0000000..00a40d3 --- /dev/null +++ b/shuffle3rs/src/ext.rs @@ -0,0 +1,34 @@ +use rand::Rng; +use crate::shuffle; + +pub trait SliceElementExt +{ + fn random_element(&self, rng: &mut R) -> &T; + fn random_element_mut(&mut self, rng: &mut R) -> &mut T; +} + +pub trait ShuffleExt +{ + fn shuffle(&mut self, rng: &mut R); + fn unshuffle(&mut self, rng: &mut R); +} + +impl SliceElementExt for [T] +{ + #[inline] fn random_element(&self, rng: &mut R) -> &T { + shuffle::element_in(self, rng) + } + #[inline] fn random_element_mut(&mut self, rng: &mut R) -> &mut T { + shuffle::element_in_mut(self, rng) + } +} + +impl ShuffleExt for [T] +{ + #[inline] fn shuffle(&mut self, rng: &mut R) { + shuffle::shuffle(self, rng) + } + #[inline] fn unshuffle(&mut self, rng: &mut R) { + todo!() + } +} diff --git a/shuffle3rs/src/main.rs b/shuffle3rs/src/main.rs new file mode 100644 index 0000000..b4d26cb --- /dev/null +++ b/shuffle3rs/src/main.rs @@ -0,0 +1,9 @@ + +#![allow(dead_code)] + +#[macro_use] mod ext; use ext::*; +mod shuffle; + +fn main() { + println!("Hello, world!"); +} diff --git a/shuffle3rs/src/shuffle.rs b/shuffle3rs/src/shuffle.rs new file mode 100644 index 0000000..b15b491 --- /dev/null +++ b/shuffle3rs/src/shuffle.rs @@ -0,0 +1,42 @@ +use rand::Rng; +use std::mem::swap; + + +/// Get a random element in this slice. +pub fn element_in<'a, T, R: Rng + ?Sized>(slice: &'a (impl AsRef<[T]> + ?Sized), with: &mut R) -> &'a T +{ + let slice = slice.as_ref(); + &slice[with.gen_range(0..slice.len())] +} + +/// Get a random element in this slice. +pub fn element_in_mut<'a, T, R: Rng + ?Sized>(slice: &'a mut (impl AsMut<[T]> + ?Sized), with: &mut R) -> &'a mut T +{ + inside(slice.as_mut(), with) +} + + +#[inline(always)] fn inside<'a, T, R: Rng + ?Sized>(slice: &'a mut [T], with: &mut R) -> &'a mut T +{ + &mut slice[with.gen_range(0..slice.len())] +} + +fn shuffle_slice(slice: &mut [T], with: &mut R) +{ + match slice + { + &mut [] | &mut [_] => {}, + &mut [ref mut inner @ .. , ref mut b] => { + swap(b, inside(inner, with)); + shuffle(inner, with); + }, + } +} + +//TODO: unshuffle + +/// Shuffle this slice with this `Rng`. +#[inline(always)] pub fn shuffle(mut slice: impl AsMut<[T]>, with: &mut R) +{ + shuffle_slice(slice.as_mut(), with) +}