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

View File

@@ -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
}

View File

@@ -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<ChainCollection> {
"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<Piece> {
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)