From 2ef316ab524ddcde970909dafa070076f68de71a Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 18 Feb 2025 23:44:56 -0500 Subject: [PATCH] const_fn bitvec + bitvec build fixes --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/bitboard.rs | 9 +++++++-- src/board.rs | 21 +++++++++++++++++---- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 776e2ed..0801c46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "const_fn" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f8a2ca5ac02d09563609681103aada9e1777d54fc57a5acd7a41404f9c93b6e" + [[package]] name = "criterion" version = "0.5.1" @@ -425,6 +431,7 @@ version = "0.1.0" dependencies = [ "arrayvec", "bitvec", + "const_fn", "criterion", "either", "indicatif", diff --git a/Cargo.toml b/Cargo.toml index cc035f9..2b0e35e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ bitvec = [ "dep:bitvec" ] [dependencies] arrayvec = "0.7" bitvec = { version = "1", optional = true } +const_fn = "0.4.11" either = "1.13" indicatif = "0.17" lazy_static = "1.5" diff --git a/src/bitboard.rs b/src/bitboard.rs index 5cb3676..af75efb 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -1,4 +1,5 @@ use crate::board::{BOARD_AREA, BOARD_SIZE}; +use const_fn::const_fn; use static_assertions::const_assert; // quick explanation for the dual-nature of [`BitBoard`] @@ -11,7 +12,10 @@ use static_assertions::const_assert; use bitvec::prelude::*; #[cfg(feature = "bitvec")] -pub type BitBoardInner = BitArr!(for BOARD_AREA, in u64, Lsb0); +type BBBaseType = u64; + +#[cfg(feature = "bitvec")] +pub type BitBoardInner = BitArr!(for BOARD_AREA, in BBBaseType, Lsb0); #[cfg(not(feature = "bitvec"))] pub type BitBoardInner = u64; @@ -78,7 +82,8 @@ impl BitBoard { self.0.set(Self::get_index(row, col), value); } - // works on both `bitvec` and native + // works on both `bitvec` and native (const on native) + #[const_fn(cfg(not(feature = "bitvec")))] pub fn count(&self) -> usize { self.0.count_ones() as usize } diff --git a/src/board.rs b/src/board.rs index c78a554..a9d630e 100644 --- a/src/board.rs +++ b/src/board.rs @@ -4,8 +4,9 @@ use crate::{ piece::Piece, }; use arrayvec::ArrayVec; +use const_fn::const_fn; use lazy_static::lazy_static; -use std::{cmp::Ordering, fmt}; +use std::{cmp::Ordering, collections::HashSet, fmt}; /// Size of each dim of the board pub const BOARD_SIZE: usize = 8; @@ -62,6 +63,12 @@ fn gen_adj_lookup() -> PosMap { "chains go out-of-bounds" ); + let mut uniq = HashSet::new(); + assert!( + chains.iter().flatten().all(move |x| uniq.insert(x)), + "there are duplicate nodes in chain" + ); + chains }) .collect() @@ -191,11 +198,13 @@ impl Board { } } - pub fn get_piece(&self, i: usize, j: usize, color: Piece) -> bool { + #[const_fn(cfg(not(feature = "bitvec")))] + pub const fn get_piece(&self, i: usize, j: usize, color: Piece) -> bool { self.board(color).get(i, j) } /// Returns the color of a place on the [`Board`] at a position + #[const_fn(cfg(not(feature = "bitvec")))] pub fn get(&self, i: usize, j: usize) -> Option { if self.get_piece(i, j, Piece::White) { Some(Piece::White) @@ -207,11 +216,13 @@ impl Board { } /// Place a piece without checking for propegation of validity + #[const_fn(cfg(not(feature = "bitvec")))] fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) { self.board_mut(piece).set(i, j, true); - self.board_mut(!piece).set(i, j, false); + self.board_mut(piece.flip()).set(i, j, false); } + #[const_fn(cfg(not(feature = "bitvec")))] fn delete(&mut self, i: usize, j: usize) { self.board_mut(Piece::White).set(i, j, false); self.board_mut(Piece::Black).set(i, j, false); @@ -300,14 +311,16 @@ impl Board { } /// Count the number of a type of [`Piece`] on the board + #[const_fn(cfg(not(feature = "bitvec")))] pub fn count(&self, piece: Piece) -> usize { self.board(piece).count() } /// Get the "net score" of a player /// Formula: `net_score = Score_player - Score_opponent` + #[const_fn(cfg(not(feature = "bitvec")))] pub fn net_score(&self, piece: Piece) -> isize { - self.count(piece) as isize - self.count(!piece) as isize + self.count(piece) as isize - self.count(piece.flip()) as isize } /// Returns the winner of the board (if any)