Skip to content

Commit

Permalink
Optimize group building by using pre-computed neighbor bitboards
Browse files Browse the repository at this point in the history
  • Loading branch information
MortenLohne committed Jul 29, 2024
1 parent 162f7c7 commit 849750d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/position/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,28 +1827,25 @@ impl<const S: usize> pgn_traits::PgnPosition for Position<S> {
}

pub(crate) fn connected_components_graph<const S: usize>(
road_pieces: BitBoard,
mut road_pieces: BitBoard,
components: &mut AbstractBoard<u8, S>,
id: &mut u8,
) {
for square in square::squares_iterator::<S>() {
if components[square] == 0 && road_pieces.get_square(square) {
connect_component(road_pieces, components, square, *id);
*id += 1;
}
while let Some(square) = road_pieces.occupied_square() {
connect_component(&mut road_pieces, components, square, *id);
*id += 1;
}
}

fn connect_component<const S: usize>(
road_pieces: BitBoard,
road_pieces: &mut BitBoard,
components: &mut AbstractBoard<u8, S>,
square: Square<S>,
id: u8,
) {
components[square] = id;
for neighbour in square.neighbors() {
if road_pieces.get_square(neighbour) && components[neighbour] == 0 {
connect_component(road_pieces, components, neighbour, id);
}
*road_pieces = road_pieces.clear_square(square);
while let Some(neighbor) = (square.neighbors_bitboard() & *road_pieces).occupied_square() {
connect_component(road_pieces, components, neighbor, id);
}
}
4 changes: 4 additions & 0 deletions src/position/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@ impl<const S: usize> Square<S> {
.map(|(_, neighbor)| neighbor)
}

pub(crate) fn neighbors_bitboard(self) -> BitBoard {
lookup_neighbor_table::<S>(self)
}

pub fn directions(self) -> impl Iterator<Item = Direction> {
lookup_neighbor_array_table::<S>(self)
.into_iter()
Expand Down

0 comments on commit 849750d

Please sign in to comment.