bit board improvements

This commit is contained in:
2025-02-12 10:14:48 -05:00
parent 59944e1e9e
commit 01ae7be70d

View File

@@ -10,19 +10,31 @@ impl BitBoard {
BitBoard(0) BitBoard(0)
} }
const fn get_index(row: usize, col: usize) -> usize {
row * BOARD_SIZE + col
}
pub const fn get(&self, row: usize, col: usize) -> bool { pub const fn get(&self, row: usize, col: usize) -> bool {
((self.0 >> (row * BOARD_SIZE + col)) & 1) != 0 ((self.0 >> Self::get_index(row, col)) & 1) != 0
} }
pub const fn set(&mut self, row: usize, col: usize, value: bool) { pub const fn set(&mut self, row: usize, col: usize, value: bool) {
let index = row * BOARD_SIZE + col; let index = Self::get_index(row, col);
if value { if value {
self.0 |= 1 << index; // Set the bit at (row, col) to 1 self.set_bit(index); // Set the bit at (row, col) to 1
} else { } else {
self.0 &= !(1 << index); // Clear the bit at (row, col) self.clear_bit(index); // Clear the bit at (row, col)
} }
} }
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 const fn count(&self) -> u32 {
self.0.count_ones() self.0.count_ones()
} }