const_fn bitvec + bitvec build fixes

This commit is contained in:
2025-02-18 23:44:56 -05:00
parent c14e5703ef
commit 2ef316ab52
4 changed files with 32 additions and 6 deletions

7
Cargo.lock generated
View File

@@ -142,6 +142,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "const_fn"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f8a2ca5ac02d09563609681103aada9e1777d54fc57a5acd7a41404f9c93b6e"
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.5.1" version = "0.5.1"
@@ -425,6 +431,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bitvec", "bitvec",
"const_fn",
"criterion", "criterion",
"either", "either",
"indicatif", "indicatif",

View File

@@ -31,6 +31,7 @@ bitvec = [ "dep:bitvec" ]
[dependencies] [dependencies]
arrayvec = "0.7" arrayvec = "0.7"
bitvec = { version = "1", optional = true } bitvec = { version = "1", optional = true }
const_fn = "0.4.11"
either = "1.13" either = "1.13"
indicatif = "0.17" indicatif = "0.17"
lazy_static = "1.5" lazy_static = "1.5"

View File

@@ -1,4 +1,5 @@
use crate::board::{BOARD_AREA, BOARD_SIZE}; use crate::board::{BOARD_AREA, BOARD_SIZE};
use const_fn::const_fn;
use static_assertions::const_assert; use static_assertions::const_assert;
// quick explanation for the dual-nature of [`BitBoard`] // quick explanation for the dual-nature of [`BitBoard`]
@@ -11,7 +12,10 @@ use static_assertions::const_assert;
use bitvec::prelude::*; use bitvec::prelude::*;
#[cfg(feature = "bitvec")] #[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"))] #[cfg(not(feature = "bitvec"))]
pub type BitBoardInner = u64; pub type BitBoardInner = u64;
@@ -78,7 +82,8 @@ impl BitBoard {
self.0.set(Self::get_index(row, col), value); 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 { pub fn count(&self) -> usize {
self.0.count_ones() as usize self.0.count_ones() as usize
} }

View File

@@ -4,8 +4,9 @@ use crate::{
piece::Piece, piece::Piece,
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use const_fn::const_fn;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::{cmp::Ordering, fmt}; use std::{cmp::Ordering, collections::HashSet, fmt};
/// Size of each dim of the board /// Size of each dim of the board
pub const BOARD_SIZE: usize = 8; pub const BOARD_SIZE: usize = 8;
@@ -62,6 +63,12 @@ fn gen_adj_lookup() -> PosMap<ChainCollection> {
"chains go out-of-bounds" "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 chains
}) })
.collect() .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) self.board(color).get(i, j)
} }
/// Returns the color of a place on the [`Board`] at a position /// 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<Piece> { pub fn get(&self, i: usize, j: usize) -> Option<Piece> {
if self.get_piece(i, j, Piece::White) { if self.get_piece(i, j, Piece::White) {
Some(Piece::White) Some(Piece::White)
@@ -207,11 +216,13 @@ impl Board {
} }
/// Place a piece without checking for propegation of validity /// 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) { 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, 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) { fn delete(&mut self, i: usize, j: usize) {
self.board_mut(Piece::White).set(i, j, false); self.board_mut(Piece::White).set(i, j, false);
self.board_mut(Piece::Black).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 /// Count the number of a type of [`Piece`] on the board
#[const_fn(cfg(not(feature = "bitvec")))]
pub fn count(&self, piece: Piece) -> usize { pub fn count(&self, piece: Piece) -> usize {
self.board(piece).count() self.board(piece).count()
} }
/// Get the "net score" of a player /// Get the "net score" of a player
/// Formula: `net_score = Score_player - Score_opponent` /// Formula: `net_score = Score_player - Score_opponent`
#[const_fn(cfg(not(feature = "bitvec")))]
pub fn net_score(&self, piece: Piece) -> isize { 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) /// Returns the winner of the board (if any)