Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hash #1118

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,8 @@ def restore(self, board: Board) -> None:
board.halfmove_clock = self.halfmove_clock
board.fullmove_number = self.fullmove_number

#from numba.experimental import jitclass
#@jitclass
class Board(BaseBoard):
"""
A :class:`~chess.BaseBoard`, additional information representing
Expand Down Expand Up @@ -3817,14 +3819,21 @@ def _repr_svg_(self) -> str:

def __eq__(self, board: object) -> bool:
if isinstance(board, Board):
return (
self.halfmove_clock == board.halfmove_clock and
self.fullmove_number == board.fullmove_number and
type(self).uci_variant == type(board).uci_variant and
self._transposition_key() == board._transposition_key())
return hash(self) == hash(board)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hash(self) == hash(board) does not imply self equals board (there can be hash collisions).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand the claim. Are you claiming the hash is weak? Because hash collisions are negligible.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for your use case.

But this is a general purpose library. So we shouldn't assume that we will only ever see 2^32 positions (at which point collisions are likely even for strong 64 bit hashes), or even that inputs aren't specifically chosen by an adversary.


else:
return NotImplemented

def __hash__(self) -> int:
return hash((
self.pawns, self.knights, self.bishops, self.rooks,
self.queens, self.kings,
self.occupied_co[WHITE], self.occupied_co[BLACK],
self.turn, self.castling_rights, self.ep_square,
self.halfmove_clock, self.fullmove_number,
type(self).uci_variant, self.chess960
))

def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
super().apply_transform(f)
self.clear_stack()
Expand Down
Loading