From f51c940b3f542af889e39ea1e64efeececc4aa98 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 20 Mar 2025 01:12:27 -0400 Subject: [PATCH] improve PosMap --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/logic/board_value.rs | 8 +++++++- src/logic/move.rs | 2 +- src/repr/board.rs | 44 +++++++++++++++++----------------------- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbd6c34..0010c38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,6 +145,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "const_for" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c50fcfdf972929aff202c16b80086aa3cfc6a3a820af714096c58c7c1d0582" + [[package]] name = "criterion" version = "0.5.1" @@ -443,6 +449,7 @@ dependencies = [ "allocative", "arrayvec", "console", + "const_for", "criterion", "crossbeam-channel", "either", diff --git a/Cargo.toml b/Cargo.toml index 28d2a98..6e2f99a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ debug = true allocative = "0.3" arrayvec = "0.7" console = "0.15" +const_for = "0.1" crossbeam-channel = "0.5" either = "1.13" indicatif = { version = "0.17", features = [ "rayon" ] } diff --git a/src/logic/board_value.rs b/src/logic/board_value.rs index 4c97690..a9d36eb 100644 --- a/src/logic/board_value.rs +++ b/src/logic/board_value.rs @@ -19,7 +19,7 @@ impl BoardValueMap { } /// Weights from: https://repub.eur.nl/pub/7142/ei2005-47.pdf - pub fn new() -> Self { + pub const fn weighted() -> Self { const POSITION_VALUES: [[i16; 8]; 8] = [ [100, -20, 10, 5, 5, 10, -20, 100], [-20, -50, -2, -2, -2, -2, -50, -20], @@ -32,4 +32,10 @@ impl BoardValueMap { ]; Self(PosMap::from(POSITION_VALUES)) } + + pub const fn flat() -> Self { + Self(PosMap::from( + [[1; Board::SIZE as usize]; Board::SIZE as usize], + )) + } } diff --git a/src/logic/move.rs b/src/logic/move.rs index b390825..56645ab 100644 --- a/src/logic/move.rs +++ b/src/logic/move.rs @@ -37,7 +37,7 @@ pub struct Move { pub is_trimmed: bool, } -static BVM: LazyLock = LazyLock::new(BoardValueMap::new); +const BVM: BoardValueMap = BoardValueMap::weighted(); impl Move { pub fn new(coord: Option, board: Board, color: Piece, agent_color: Piece) -> Self { diff --git a/src/repr/board.rs b/src/repr/board.rs index c199ba6..e3065db 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -1,45 +1,39 @@ use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair}; use allocative::Allocative; -use arrayvec::ArrayVec; +use const_for::const_for; use rand::seq::IteratorRandom; -use std::{cmp::Ordering, fmt}; +use std::{cmp::Ordering, fmt, mem::MaybeUninit}; /// Map of all points on the board against some type T /// Used to index like so: example[i][j] /// with each coordinate -pub struct PosMap(ArrayVec); +pub struct PosMap([T; Board::AREA.0 as usize]); -impl PosMap { - #[allow(clippy::new_without_default)] - pub fn new() -> Self { - Self(ArrayVec::from_iter( - (0..Board::AREA.0).map(|_| Default::default()), - )) +impl PosMap { + pub const unsafe fn uninit() -> Self { + Self([MaybeUninit::zeroed().assume_init(); Board::AREA.0 as usize]) } - pub fn get(&self, coords: CoordPair) -> &T { + pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self { + let mut n = unsafe { Self::uninit() }; + + const_for!(i in 0..Board::SIZE => { + const_for!(j in 0..Board::SIZE => { + n.set(CoordPair::from_axes(i, j), v[i as usize][j as usize]); + }); + }); + n + } + + pub const fn get(&self, coords: CoordPair) -> &T { &self.0[coords.0 as usize] } - pub fn set(&mut self, coords: CoordPair, value: T) { + pub const fn set(&mut self, coords: CoordPair, value: T) { self.0[coords.0 as usize] = value; } } -type PosMapOrig = [[T; Board::SIZE as usize]; Board::SIZE as usize]; - -impl From> for PosMap { - fn from(value: PosMapOrig) -> Self { - let mut new = Self::new(); - for i in 0..Board::SIZE { - for j in 0..Board::SIZE { - new.set((i, j).into(), value[i as usize][j as usize]); - } - } - new - } -} - #[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)] pub enum Winner { Player(Piece),