Implement Index<T>, IndexMut<T> for Castle, PieceType and Color enums, use swap_remove instead of remove on sort_moves

This commit is contained in:
stefiosif
2024-12-01 10:52:38 +02:00
parent 316f40e6e5
commit f7604b27a5
4 changed files with 62 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
use u64 as Bitboard;
use crate::board::bitboard::{have_common_bit, lsb};
use crate::board::bitboard::{have_common_bit, lsb, square_to_bitboard};
use crate::board::state::State;
use crate::movegen::attack_generator::{
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
@@ -272,17 +272,15 @@ impl PieceType {
}
use std::ops::{Index, IndexMut};
use super::bitboard::square_to_bitboard;
impl Index<PieceType> for [Piece] {
type Output = Piece;
impl<T> Index<PieceType> for [T] {
type Output = T;
fn index(&self, piece_type: PieceType) -> &Self::Output {
&self[piece_type.idx()]
}
}
impl IndexMut<PieceType> for [Piece] {
impl<T> IndexMut<PieceType> for [T] {
fn index_mut(&mut self, piece_type: PieceType) -> &mut Self::Output {
&mut self[piece_type.idx()]
}
@@ -307,6 +305,20 @@ impl Color {
}
}
impl<T> Index<Color> for [T] {
type Output = T;
fn index(&self, color: Color) -> &Self::Output {
&self[color.idx()]
}
}
impl<T> IndexMut<Color> for [T] {
fn index_mut(&mut self, color: Color) -> &mut Self::Output {
&mut self[color.idx()]
}
}
#[cfg(test)]
mod tests {
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};