smallvecs
This commit is contained in:
parent
875e232054
commit
6bf2071850
|
@ -39,6 +39,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"coz",
|
"coz",
|
||||||
"rand",
|
"rand",
|
||||||
|
"smallvec",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -93,6 +94,12 @@ dependencies = [
|
||||||
"rand_core",
|
"rand_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a55ca5f3b68e41c979bf8c46a6f1da892ca4db8f94023ce0bd32407573b1ac0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.9.0+wasi-snapshot-preview1"
|
version = "0.9.0+wasi-snapshot-preview1"
|
||||||
|
|
|
@ -9,6 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.8.0"
|
rand = "0.8.0"
|
||||||
coz = "0.1"
|
coz = "0.1"
|
||||||
|
smallvec = "1.6.0"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
debug = 1
|
debug = 1
|
65
src/azul.rs
65
src/azul.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
use smallvec::{SmallVec, smallvec};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Tile {
|
pub enum Tile {
|
||||||
|
@ -109,10 +110,10 @@ impl Iterator for GameMoveIter {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Bag (Vec<Tile>);
|
struct Bag (SmallVec<[Tile; 128]>);
|
||||||
impl Default for Bag {
|
impl Default for Bag {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut bag = Vec::<Tile>::with_capacity(100);
|
let mut bag = SmallVec::<[Tile; 128]>::new();
|
||||||
for _ in 0..20 {
|
for _ in 0..20 {
|
||||||
bag.push(Tile::Blue);
|
bag.push(Tile::Blue);
|
||||||
};
|
};
|
||||||
|
@ -134,37 +135,37 @@ impl Default for Bag {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Bag {
|
impl Deref for Bag {
|
||||||
type Target = Vec<Tile>;
|
type Target = SmallVec<[Tile; 128]>;
|
||||||
|
|
||||||
fn deref(&self) -> &Vec<Tile> {
|
fn deref(&self) -> &SmallVec<[Tile; 128]> {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl DerefMut for Bag {
|
impl DerefMut for Bag {
|
||||||
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 128]> {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<Tile>> for Bag {
|
impl From<SmallVec<[Tile; 128]>> for Bag {
|
||||||
fn from(vector: Vec<Tile>) -> Bag {
|
fn from(vector: SmallVec<[Tile; 128]>) -> Bag {
|
||||||
Bag(vector)
|
Bag(vector)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
struct Factory (Vec<Tile>);
|
struct Factory (SmallVec<[Tile; 4]>);
|
||||||
|
|
||||||
impl Deref for Factory {
|
impl Deref for Factory {
|
||||||
type Target = Vec<Tile>;
|
type Target = SmallVec<[Tile; 4]>;
|
||||||
|
|
||||||
fn deref(&self) -> &Vec<Tile> {
|
fn deref(&self) -> &SmallVec<[Tile; 4]> {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl DerefMut for Factory {
|
impl DerefMut for Factory {
|
||||||
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 4]> {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,38 +173,37 @@ impl DerefMut for Factory {
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Market (Vec<Tile>);
|
struct Market (SmallVec<[Tile; 28]>);
|
||||||
|
|
||||||
impl Default for Market {
|
impl Default for Market {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut market = Vec::<Tile>::with_capacity(20);
|
let mut market = SmallVec::<[Tile; 28]>::new();
|
||||||
market.push(Tile::Start);
|
market.push(Tile::Start);
|
||||||
Market(market)
|
Market(market)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Deref for Market {
|
impl Deref for Market {
|
||||||
type Target = Vec<Tile>;
|
type Target = SmallVec<[Tile; 28]>;
|
||||||
|
|
||||||
fn deref(&self) -> &Vec<Tile> {
|
fn deref(&self) -> &SmallVec<[Tile; 28]> {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl DerefMut for Market {
|
impl DerefMut for Market {
|
||||||
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 28]> {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Patterns = [Vec<Tile>; 5];
|
type Patterns = [SmallVec<[Tile; 5]>; 5];
|
||||||
|
|
||||||
type Row = [bool; 5];
|
type Row = [bool; 5];
|
||||||
type Wall = [Row; 5];
|
type Wall = [Row; 5];
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default)]
|
||||||
struct Board {
|
struct Board {
|
||||||
score: u8,
|
score: u8,
|
||||||
wall: Wall,
|
wall: Wall,
|
||||||
floor: Vec<Tile>,
|
floor: SmallVec<[Tile; 7]>,
|
||||||
patterns: Patterns,
|
patterns: Patterns,
|
||||||
}
|
}
|
||||||
impl Board {
|
impl Board {
|
||||||
|
@ -303,16 +303,6 @@ impl Board {
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Default for Board {
|
|
||||||
fn default() -> Self {
|
|
||||||
Board {
|
|
||||||
score: 0,
|
|
||||||
wall: Wall::default(),
|
|
||||||
floor: Vec::default(),
|
|
||||||
patterns: Patterns::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
|
@ -348,7 +338,7 @@ impl Game {
|
||||||
random: random,
|
random: random,
|
||||||
turn: 0,
|
turn: 0,
|
||||||
player: 0,
|
player: 0,
|
||||||
box_top: Vec::<Tile>::with_capacity(100).into(),
|
box_top: Bag(SmallVec::<[Tile; 128]>::new()),
|
||||||
bag: Bag::default(),
|
bag: Bag::default(),
|
||||||
market: Market::default(),
|
market: Market::default(),
|
||||||
factories: factories,
|
factories: factories,
|
||||||
|
@ -416,8 +406,8 @@ impl Game {
|
||||||
GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"),
|
GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"),
|
||||||
GameMove(0, _, 0) => {
|
GameMove(0, _, 0) => {
|
||||||
if self.market.contains(&game_move.1) {
|
if self.market.contains(&game_move.1) {
|
||||||
let mut hand = self.market.deref().clone();
|
let mut hand= self.market.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);
|
self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
||||||
|
|
||||||
board.floor.append(&mut hand)
|
board.floor.append(&mut hand)
|
||||||
|
@ -442,7 +432,7 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut hand = self.market.deref().clone();
|
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);
|
self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
||||||
|
|
||||||
for tile in hand.drain(..) {
|
for tile in hand.drain(..) {
|
||||||
|
@ -472,10 +462,9 @@ impl Game {
|
||||||
|
|
||||||
let factory = &mut self.factories[game_move.0 - 1];
|
let factory = &mut self.factories[game_move.0 - 1];
|
||||||
if factory.contains(&game_move.1) {
|
if factory.contains(&game_move.1) {
|
||||||
let mut hand = factory.deref().clone();
|
let mut hand = factory.clone();
|
||||||
|
hand.retain(|x| *x == game_move.1);
|
||||||
hand.retain(|&x| x == game_move.1);
|
factory.retain(|x| *x != game_move.1);
|
||||||
factory.retain(|&x| x != game_move.1);
|
|
||||||
|
|
||||||
self.market.append(factory);
|
self.market.append(factory);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rand::prelude::*;
|
||||||
|
|
||||||
fn main() -> Result<(), &'static str> {
|
fn main() -> Result<(), &'static str> {
|
||||||
let mut g_rng = StdRng::seed_from_u64(42);
|
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) {
|
let rng = match StdRng::from_rng(&mut g_rng) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Reference in New Issue