const_fn bitvec + bitvec build fixes
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
21
src/board.rs
21
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<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)
|
||||
|
||||
Reference in New Issue
Block a user