diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index eedad1c..df6b852 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -1,4 +1,4 @@ -use super::board::Board; +use super::{board::Board, misc::get_index}; use const_fn::const_fn; use static_assertions::const_assert; @@ -41,7 +41,7 @@ impl BitBoard { #[const_fn(cfg(not(feature = "bitvec")))] pub const fn get(&self, row: usize, col: usize) -> bool { - self.get_by_index(Self::get_index(row, col)) + self.get_by_index(get_index(row, col)) } #[cfg(not(feature = "bitvec"))] @@ -51,7 +51,7 @@ impl BitBoard { #[const_fn(cfg(not(feature = "bitvec")))] pub const fn set(&mut self, row: usize, col: usize, value: bool) { - self.set_by_index(Self::get_index(row, col), value); + self.set_by_index(get_index(row, col), value); } #[cfg(not(feature = "bitvec"))] @@ -61,10 +61,6 @@ impl BitBoard { self.0 |= (value as BitBoardInner) << index; // set bit (if needed) } - const fn get_index(row: usize, col: usize) -> usize { - row * Board::BOARD_SIZE + col - } - #[cfg(feature = "bitvec")] pub fn get_by_index(&self, index: usize) -> bool { self.0[index] diff --git a/src/repr/board.rs b/src/repr/board.rs index 88b74ef..a9074aa 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -1,5 +1,6 @@ use super::{ bitboard::BitBoard, + misc::get_index, misc::{diag_raw, split_from}, piece::Piece, }; @@ -20,10 +21,6 @@ type ChainCollection = ArrayVec; pub struct PosMap(ArrayVec); impl PosMap { - const fn index(row: usize, col: usize) -> usize { - row * Board::BOARD_SIZE + col - } - #[allow(clippy::new_without_default)] pub fn new() -> Self { Self(ArrayVec::from_iter( @@ -32,7 +29,7 @@ impl PosMap { } pub fn get(&self, row: usize, col: usize) -> &T { - let index = Self::index(row, col); + let index = get_index(row, col); debug_assert!( Board::BOARD_AREA + 1 >= index, @@ -44,7 +41,7 @@ impl PosMap { } pub fn set(&mut self, row: usize, col: usize, value: T) { - let index = Self::index(row, col); + let index = get_index(row, col); debug_assert!( Board::BOARD_AREA + 1 >= index, "index out of range, was: {}", diff --git a/src/repr/misc.rs b/src/repr/misc.rs index 21e1033..f453578 100644 --- a/src/repr/misc.rs +++ b/src/repr/misc.rs @@ -1,3 +1,4 @@ +use super::Board; use either::Either; use std::{iter::Rev, ops::RangeInclusive}; @@ -38,6 +39,11 @@ where [(0, 0), (1, 1), (1, 0), (0, 1)].map(move |(a, b)| i_chains[a].clone().zip(j_chains[b].clone())) } +/// Convert a row and column to an index in the board +pub const fn get_index(row: usize, col: usize) -> usize { + row * Board::BOARD_SIZE + col +} + #[cfg(test)] mod test { use super::*;