From f1083cfa2712e2fe70e056ac25f897e8d1656df9 Mon Sep 17 00:00:00 2001 From: Jali Date: Thu, 26 Dec 2019 23:22:32 +0100 Subject: [PATCH] Added sorting code --- .gitignore | 2 + Cargo.lock | 185 ++++++++++++++++++++++++++++++++ Cargo.toml | 11 ++ src/env/commandlinearguments.rs | 63 +++++++++++ src/env/mod.rs | 9 ++ src/main.rs | 19 ++++ src/sort/ballpit.rs | 90 ++++++++++++++++ src/sort/buckets.rs | 54 ++++++++++ src/sort/mod.rs | 12 +++ 9 files changed, 445 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/env/commandlinearguments.rs create mode 100644 src/env/mod.rs create mode 100644 src/main.rs create mode 100644 src/sort/ballpit.rs create mode 100644 src/sort/buckets.rs create mode 100644 src/sort/mod.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b7bf873 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,185 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ballpit" +version = "0.1.0" +dependencies = [ + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "c2-chacha" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "getrandom" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.66" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ppv-lite86" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_chacha" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wasi" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +"checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407" +"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" +"checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +"checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" +"checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" +"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" +"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1667e93 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ballpit" +version = "0.1.0" +authors = ["Jali "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2.33.0" +rand = "0.7" diff --git a/src/env/commandlinearguments.rs b/src/env/commandlinearguments.rs new file mode 100644 index 0000000..fd4ef6e --- /dev/null +++ b/src/env/commandlinearguments.rs @@ -0,0 +1,63 @@ +//! File: env/commandlinearguments.rs +//! Created On: Thu 26 Dec 2019 09:01:57 PM CET +//! Last Modfied: Thu 26 Dec 2019 09:01:57 PM CET +//! Author: Jali +//! Description: Defines the command line arguments that were passed + +use clap::ArgMatches; +use std::str::FromStr; + +/** + * # Summary + * Stores the command line arguments from our + * service. + */ +pub struct CommandLineArguments { + pub no_of_balls: u32, +} + +impl CommandLineArguments { + /** + * # Summary + * Creates a new instance of a CommandLineArguments object either from + * the command line or the default configuration + * + * # Returns + * A new ```CommandLineArguments``` object + * + * # Examples + * + * ```rust + * let cmdargs = CommandLineArguments::new(); + * ``` + */ + pub fn new() -> CommandLineArguments { + let matches = CommandLineArguments::parse_command_line(); + + CommandLineArguments { + no_of_balls: FromStr::from_str(matches.value_of("NOOFBALLS").unwrap_or("1000")) + .unwrap(), + } + } + + /** + * # Summary + * Parses the command line and returns all matches that fit valid options. + * + * # Generics + * ## Lifetimeparm: 'a + * Assures the same lifetime to the return value than the command line. + * + * # Returns + * An *ArgMatches* struct, that contrains all matched arguments. + */ + fn parse_command_line<'a>() -> ArgMatches<'a> { + clap_app!(myapp => + (version: "1.0.360.1") + (author: "Jali ") + (about: "A naïve algorithm to sort balls in a ballpit by colour") + (@arg NOOFBALLS: -n --noofballs +takes_value "Specifies the number of balls in the ballpit") + ) + .get_matches() + } +} diff --git a/src/env/mod.rs b/src/env/mod.rs new file mode 100644 index 0000000..ea73177 --- /dev/null +++ b/src/env/mod.rs @@ -0,0 +1,9 @@ +//! File: env/mod.rs +//! Created On: Thu 26 Dec 2019 09:05:17 PM CET +//! Last Modfied: Thu 26 Dec 2019 09:05:17 PM CET +//! Author: Jali +//! Description: Defines the command line arguments that were passed +mod commandlinearguments; + +// publish the CommandLineArgument struct +pub use self::commandlinearguments::CommandLineArguments; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..96adcfc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,19 @@ +//! File: main.rs +//! Created On: Thu 26 Dec 2019 09:11:26 PM CET +//! Last Modified: Thu 26 Dec 2019 11:20:32 PM CET +//! Description: A naïve algorithm to sort balls in a ball pit. +#[macro_use] +extern crate clap; +extern crate rand; + +mod env; +mod sort; + +fn main() { + let args = env::CommandLineArguments::new(); + + let mut buckets = sort::Buckets::new(); + buckets.sort(args.no_of_balls); + + println!("The pit is empty"); +} diff --git a/src/sort/ballpit.rs b/src/sort/ballpit.rs new file mode 100644 index 0000000..477492c --- /dev/null +++ b/src/sort/ballpit.rs @@ -0,0 +1,90 @@ +//! File: sort/ballpit.rs +//! Author: Jali +//! Create On: Thu 26 Dec 2019 09:55:45 PM CET +//! Last Modified: Thu 26 Dec 2019 10:52:30 PM CET +//! Description: Simulates a ballpit with a given number of balls. +//! The ballpit creates each ball randomly, when a ball is picked, +//! and reduces the counter by one. + +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; +use std::fmt; + +/** + * Defines the kinds of balls, that are available + */ +#[derive(Debug)] +pub enum Colours { + Red, + Green, + Yellow, + Blue, +} + +// implements the distribution class for ```Colours``` +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Colours { + match rng.gen_range(0, 4) { + 0 => Colours::Red, + 1 => Colours::Green, + 2 => Colours::Yellow, + _ => Colours::Blue, + } + } +} + +// implements Display for Colours +impl fmt::Display for Colours { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Colours::Red => write!(f, "Red"), + Colours::Green => write!(f, "Green"), + Colours::Yellow => write!(f, "Yellow"), + Colours::Blue => write!(f, "Blue"), + } + } +} + +pub struct Ballpit { + remaining: u32, +} + +impl Ballpit { + /** + * # Summary + * Creates a new ballpit. + * + * # Parameters + * ## no_of_balls + * The inital number of balls in the pit + * + * # Returns + * A new ```Ballpit``` object + */ + pub fn new(no_of_balls: u32) -> Ballpit { + Ballpit { + remaining: no_of_balls, + } + } + + /** + * # Summary + * Picks a ball from the pit, and returns it. + * The call to this function will decrease the number of + * balls in the pit, for if a ball it picked, it will be gone. + * + * # Returns + * Returns an Option value. The value contains the colour of + * the ball, or ```None```, if the pit is empty. + */ + pub fn pick(&mut self) -> Option { + if self.remaining == 0 { + return None; + } + + self.remaining -= 1; + Some(rand::random()) + } +} diff --git a/src/sort/buckets.rs b/src/sort/buckets.rs new file mode 100644 index 0000000..0c5a33d --- /dev/null +++ b/src/sort/buckets.rs @@ -0,0 +1,54 @@ +//! File: sort/buckets.rs +//! Author: Jali +//! Created On: Thu 26 Dec 2019 10:59:40 PM CET +//! Last Modified: Thu 26 Dec 2019 11:20:06 PM CET +//! Description: Sorts the balls picked into buckets. + +#[path = "./ballpit.rs"] +mod ballpit; +use ballpit::Ballpit; +use ballpit::Colours; + +pub struct Buckets { + pub red: u32, + pub green: u32, + pub yellow: u32, + pub blue: u32, +} + +impl Buckets { + /** + * # Summary + * Creates a new buckets collection + * + * # Returns + * A new ```Buckets``` object. + */ + pub fn new() -> Buckets { + Buckets { + red: 0, + green: 0, + yellow: 0, + blue: 0, + } + } + + pub fn sort(&mut self, no_of_balls: u32) { + let mut pit = Ballpit::new(no_of_balls); + while let Some(ball) = pit.pick() { + println!("A {} ball was picked!", ball); + match ball { + Colours::Red => self.red += 1, + Colours::Green => self.green += 1, + Colours::Yellow => self.yellow += 1, + Colours::Blue => self.blue += 1, + } + } + + println!("All {} balls where picked from the pit", no_of_balls); + println!( + "Of these where: {} red, {} green, {} yellow and {} blue", + self.red, self.green, self.yellow, self.blue + ); + } +} diff --git a/src/sort/mod.rs b/src/sort/mod.rs new file mode 100644 index 0000000..2470412 --- /dev/null +++ b/src/sort/mod.rs @@ -0,0 +1,12 @@ +//! File: sort/mod.rs +//! Created On: Thu 26 Dec 2019 10:31:54 PM CET +//! Last Modfied: Thu 26 Dec 2019 10:31:54 PM CET +//! Author: Jali +//! Description: Defines the command line arguments that were passed +mod ballpit; +mod buckets; + +// export the ballpit module +pub use self::ballpit::Ballpit; +pub use self::ballpit::Colours; +pub use self::buckets::Buckets;