From 849750dc80b07a1de6212b02761ba5408f116364 Mon Sep 17 00:00:00 2001 From: Morten Lohne Date: Mon, 29 Jul 2024 18:36:30 +0200 Subject: [PATCH] Optimize group building by using pre-computed neighbor bitboards --- src/position/mod.rs | 19 ++++++++----------- src/position/utils.rs | 4 ++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/position/mod.rs b/src/position/mod.rs index c7a6e1b..19c413b 100644 --- a/src/position/mod.rs +++ b/src/position/mod.rs @@ -1827,28 +1827,25 @@ impl pgn_traits::PgnPosition for Position { } pub(crate) fn connected_components_graph( - road_pieces: BitBoard, + mut road_pieces: BitBoard, components: &mut AbstractBoard, id: &mut u8, ) { - for square in square::squares_iterator::() { - 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( - road_pieces: BitBoard, + road_pieces: &mut BitBoard, components: &mut AbstractBoard, square: Square, 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); } } diff --git a/src/position/utils.rs b/src/position/utils.rs index ebc7781..c3cd011 100644 --- a/src/position/utils.rs +++ b/src/position/utils.rs @@ -610,6 +610,10 @@ impl Square { .map(|(_, neighbor)| neighbor) } + pub(crate) fn neighbors_bitboard(self) -> BitBoard { + lookup_neighbor_table::(self) + } + pub fn directions(self) -> impl Iterator { lookup_neighbor_array_table::(self) .into_iter()