mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-21 09:37:16 +08:00
Factor out pieceList updating code
The new Position methods add_piece, move_piece, and remove_piece now manage the member variables pieceList, pieceCount, and index, and 9 blocks of code in Position that used to manipulate those data structures by hand now call the new methods. There is a slightly slowdown (< 1%) on Clang and on perft, but the cleanup compensates the little speed loss. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
committed by
Marco Costalba
parent
55948623e7
commit
7b4f5c8f72
@@ -194,6 +194,9 @@ private:
|
||||
// Helper functions
|
||||
void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
|
||||
Bitboard hidden_checkers(Square ksq, Color c) const;
|
||||
void remove_piece(Square s, Color c, PieceType pt);
|
||||
void add_piece(Square s, Color c, PieceType pt);
|
||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
||||
|
||||
// Computing hash keys from scratch (for initialization and debugging)
|
||||
Key compute_key() const;
|
||||
@@ -414,4 +417,27 @@ inline Thread* Position::this_thread() const {
|
||||
return thisThread;
|
||||
}
|
||||
|
||||
inline void Position::add_piece(Square s, Color c, PieceType pt) {
|
||||
index[s] = pieceCount[c][pt]++;
|
||||
pieceList[c][pt][index[s]] = s;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
||||
// index[from] is not updated and becomes stale. This works as long
|
||||
// as index[] is accessed just by known occupied squares.
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
}
|
||||
|
||||
inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
||||
// WARNING: This is not a reversible operation. If we remove a piece in
|
||||
// do_move() and then replace it in undo_move() we will put it at the end of
|
||||
// the list and not in its original place, it means index[] and pieceList[]
|
||||
// are not guaranteed to be invariant to a do_move() + undo_move() sequence.
|
||||
Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
|
||||
index[lastSquare] = index[s];
|
||||
pieceList[c][pt][index[lastSquare]] = lastSquare;
|
||||
pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
|
||||
}
|
||||
|
||||
#endif // #ifndef POSITION_H_INCLUDED
|
||||
|
||||
Reference in New Issue
Block a user