diff --git a/src/movegen/wizardry/mod.rs b/src/movegen/wizardry/mod.rs index 0727293..5fb8af1 100644 --- a/src/movegen/wizardry/mod.rs +++ b/src/movegen/wizardry/mod.rs @@ -1,7 +1,7 @@ pub(crate) mod generation; mod mask; -use crate::board::Bitboard; +use crate::board::{Bitboard, Square}; /// A type representing the magic board indexing a given [crate::board::Square]. #[derive(Clone, Debug)] @@ -24,3 +24,36 @@ impl Magic { base_index + self.offset } } + +/// A type encapsulating a database of [Magic] bitboard moves. +#[derive(Clone, Debug)] +#[allow(unused)] // FIXME: remove when used +pub(crate) struct MagicMoves { + magics: Vec, + moves: Vec, +} + +#[allow(unused)] // FIXME: remove when used +impl MagicMoves { + /// Initialize a new [MagicMoves] given a matching list of [Magic] and its corresponding moves + /// as a [Bitboard]. + /// + /// # Safety + /// + /// This should only be called with values generated by [crate::movegen::wizardry::generation]. + pub unsafe fn new(magics: Vec, moves: Vec) -> Self { + Self { magics, moves } + } + + /// Get the set of valid moves for a piece standing on a [Square], given a set of blockers. + pub fn query(&self, square: Square, blockers: Bitboard) -> Bitboard { + // SAFETY: indices are in range by construction + unsafe { + let index = self + .magics + .get_unchecked(square.index()) + .get_index(blockers); + *self.moves.get_unchecked(index) + } + } +}