use bitvec

This commit is contained in:
2025-02-12 20:07:29 -05:00
parent dbd0a97602
commit 2c241948f7
6 changed files with 74 additions and 45 deletions

View File

@@ -1,48 +1,35 @@
use crate::board::BOARD_SIZE;
use static_assertions::const_assert_eq;
use crate::board::{BOARD_AREA, BOARD_SIZE};
use bitvec::prelude::*;
use static_assertions::const_assert;
// BitBoard should be big enough to fit all points on the board
const_assert_eq!(
std::mem::size_of::<BitBoard>() * 8,
(BOARD_SIZE * BOARD_SIZE)
);
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= BOARD_AREA);
/// Backing Type of BitBoard
type BBBaseType = u64;
/// 8x8
/// TODO! look into variable length bit arrays in rust
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct BitBoard(u64);
pub struct BitBoard(BitArr!(for BOARD_AREA, in BBBaseType, Lsb0));
impl BitBoard {
pub const fn new() -> Self {
BitBoard(0)
Self(bitarr!(BBBaseType, Lsb0; 0; BOARD_AREA))
}
const fn get_index(row: usize, col: usize) -> usize {
row * BOARD_SIZE + col
}
pub const fn get(&self, row: usize, col: usize) -> bool {
((self.0 >> Self::get_index(row, col)) & 1) != 0
pub fn get(&self, row: usize, col: usize) -> bool {
self.0[Self::get_index(row, col)]
}
pub const fn set(&mut self, row: usize, col: usize, value: bool) {
pub fn set(&mut self, row: usize, col: usize, value: bool) {
let index = Self::get_index(row, col);
if value {
self.set_bit(index); // Set the bit at (row, col) to 1
} else {
self.clear_bit(index); // Clear the bit at (row, col)
}
self.0.set(index, value);
}
const fn clear_bit(&mut self, index: usize) {
self.0 &= !(1 << index)
}
const fn set_bit(&mut self, index: usize) {
self.0 |= 1 << index
}
pub const fn count(&self) -> u32 {
pub fn count(&self) -> usize {
self.0.count_ones()
}
}