From 6bf20718502df177e1e1aadc4ee576767ce149fa Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 5 Jan 2021 15:47:12 +0100 Subject: [PATCH] smallvecs --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + src/azul.rs | 65 ++++++++++++++++++++++------------------------------- src/main.rs | 2 +- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e126b0c..1a27bcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,6 +39,7 @@ version = "0.1.0" dependencies = [ "coz", "rand", + "smallvec", ] [[package]] @@ -93,6 +94,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "smallvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a55ca5f3b68e41c979bf8c46a6f1da892ca4db8f94023ce0bd32407573b1ac0" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 41c3a67..a305397 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] rand = "0.8.0" coz = "0.1" +smallvec = "1.6.0" [profile.release] debug = 1 \ No newline at end of file diff --git a/src/azul.rs b/src/azul.rs index 9d4ad56..aeb66f3 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -1,5 +1,6 @@ use std::ops::{Deref, DerefMut}; use rand::prelude::*; +use smallvec::{SmallVec, smallvec}; #[derive(Debug, Clone, Copy, PartialEq)] pub enum Tile { @@ -109,10 +110,10 @@ impl Iterator for GameMoveIter { } #[derive(Debug, Clone)] -struct Bag (Vec); +struct Bag (SmallVec<[Tile; 128]>); impl Default for Bag { fn default() -> Self { - let mut bag = Vec::::with_capacity(100); + let mut bag = SmallVec::<[Tile; 128]>::new(); for _ in 0..20 { bag.push(Tile::Blue); }; @@ -134,37 +135,37 @@ impl Default for Bag { } impl Deref for Bag { - type Target = Vec; + type Target = SmallVec<[Tile; 128]>; - fn deref(&self) -> &Vec { + fn deref(&self) -> &SmallVec<[Tile; 128]> { &self.0 } } impl DerefMut for Bag { - fn deref_mut(&mut self) -> &mut Vec { + fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 128]> { &mut self.0 } } -impl From> for Bag { - fn from(vector: Vec) -> Bag { +impl From> for Bag { + fn from(vector: SmallVec<[Tile; 128]>) -> Bag { Bag(vector) } } #[derive(Default, Debug, Clone)] -struct Factory (Vec); +struct Factory (SmallVec<[Tile; 4]>); impl Deref for Factory { - type Target = Vec; + type Target = SmallVec<[Tile; 4]>; - fn deref(&self) -> &Vec { + fn deref(&self) -> &SmallVec<[Tile; 4]> { &self.0 } } impl DerefMut for Factory { - fn deref_mut(&mut self) -> &mut Vec { + fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 4]> { &mut self.0 } } @@ -172,38 +173,37 @@ impl DerefMut for Factory { #[derive(Debug, Clone)] -struct Market (Vec); - +struct Market (SmallVec<[Tile; 28]>); impl Default for Market { fn default() -> Self { - let mut market = Vec::::with_capacity(20); + let mut market = SmallVec::<[Tile; 28]>::new(); market.push(Tile::Start); Market(market) } } impl Deref for Market { - type Target = Vec; + type Target = SmallVec<[Tile; 28]>; - fn deref(&self) -> &Vec { + fn deref(&self) -> &SmallVec<[Tile; 28]> { &self.0 } } impl DerefMut for Market { - fn deref_mut(&mut self) -> &mut Vec { + fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 28]> { &mut self.0 } } -type Patterns = [Vec; 5]; +type Patterns = [SmallVec<[Tile; 5]>; 5]; type Row = [bool; 5]; type Wall = [Row; 5]; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] struct Board { score: u8, wall: Wall, - floor: Vec, + floor: SmallVec<[Tile; 7]>, patterns: Patterns, } impl Board { @@ -303,16 +303,6 @@ impl Board { return sum } } -impl Default for Board { - fn default() -> Self { - Board { - score: 0, - wall: Wall::default(), - floor: Vec::default(), - patterns: Patterns::default() - } - } -} #[derive(Debug, Clone)] pub struct Game { @@ -348,7 +338,7 @@ impl Game { random: random, turn: 0, player: 0, - box_top: Vec::::with_capacity(100).into(), + box_top: Bag(SmallVec::<[Tile; 128]>::new()), bag: Bag::default(), market: Market::default(), factories: factories, @@ -416,8 +406,8 @@ impl Game { GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"), GameMove(0, _, 0) => { if self.market.contains(&game_move.1) { - let mut hand = self.market.deref().clone(); - hand.retain(|&x| x == Tile::Start || x == game_move.1); + let mut hand= self.market.clone(); + hand.retain(|x| *x == Tile::Start || *x == game_move.1); self.market.retain(|x| *x != Tile::Start && *x != game_move.1); board.floor.append(&mut hand) @@ -442,7 +432,7 @@ impl Game { } let mut hand = self.market.deref().clone(); - hand.retain(|&x| x == Tile::Start || x == game_move.1); + hand.retain(|x| *x == Tile::Start || *x == game_move.1); self.market.retain(|x| *x != Tile::Start && *x != game_move.1); for tile in hand.drain(..) { @@ -472,10 +462,9 @@ impl Game { let factory = &mut self.factories[game_move.0 - 1]; if factory.contains(&game_move.1) { - let mut hand = factory.deref().clone(); - - hand.retain(|&x| x == game_move.1); - factory.retain(|&x| x != game_move.1); + let mut hand = factory.clone(); + hand.retain(|x| *x == game_move.1); + factory.retain(|x| *x != game_move.1); self.market.append(factory); diff --git a/src/main.rs b/src/main.rs index 2d1a2cc..bb2ab51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use rand::prelude::*; fn main() -> Result<(), &'static str> { let mut g_rng = StdRng::seed_from_u64(42); - for _ in 0..10000 { + for _ in 0..1000000 { let rng = match StdRng::from_rng(&mut g_rng) { Ok(r) => r, Err(e) => {