Skip to content

Commit

Permalink
Remove redundant equatable conformances and make Game and MoveTree ha…
Browse files Browse the repository at this point in the history
…shable
  • Loading branch information
pdil committed Aug 21, 2024
1 parent e3b0b26 commit 17aa97a
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# [unreleased]

### Improvements
* Conform more types to `Hashable` such as `Game` and `MoveTree`.
* `Game.Tag` now publicly exposes `name`.
* `Game.Tags` is now `Hashable` and `Sendable`.

Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Bitboards/PieceSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
///
/// Also contains convenient amalgamations
/// of different combinations of pieces.
struct PieceSet: Equatable, Hashable, Sendable {
struct PieceSet: Hashable, Sendable {
/// Bitboard for black king pieces.
var k: Bitboard = 0
/// Bitboard for black queen pieces.
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Board.swift
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public struct Board: Sendable {

extension Board {
/// Represents an end result of a standard chess game.
public enum EndResult: Equatable, Sendable {
public enum EndResult: Hashable, Sendable {
/// The board represents a win for the given color.
case win(Piece.Color)
/// The board represents a draw with a given reason.
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/// Tracks the number of moves in a game for
/// the purposes of regulating the 50 move rule.
public struct Clock: Equatable, Sendable {
public struct Clock: Hashable, Sendable {

/// The maximum number of half moves before
/// a draw by the fifty move rule should be called.
Expand Down
6 changes: 3 additions & 3 deletions Sources/ChessKit/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
/// This object is the entry point for interacting with a full
/// chess game within `ChessKit`. It provides methods for
/// making moves and publishes the played moves in an observable way.
public struct Game: Equatable {
public struct Game: Hashable {

// MARK: - Properties

Expand Down Expand Up @@ -206,7 +206,7 @@ extension Game {

/// Denotes a PGN tag pair.
@propertyWrapper
public struct Tag: Equatable, Hashable, Sendable {
public struct Tag: Hashable, Sendable {

/// The name of the tag pair.
///
Expand All @@ -232,7 +232,7 @@ extension Game {
}

/// Contains the PGN tag pairs for a game.
public struct Tags: Equatable, Hashable, Sendable {
public struct Tags: Hashable, Sendable {

/// Whether or not all the standard mandatory tags for
/// PGN archival are set.
Expand Down
6 changes: 3 additions & 3 deletions Sources/ChessKit/Move.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// ChessKit
//

public struct Move: Equatable, Hashable, Sendable {
public struct Move: Hashable, Sendable {

/// The result of the move.
public enum Result: Equatable, Hashable, Sendable {
public enum Result: Hashable, Sendable {
case move
case capture(Piece)
case castle(Castling)
Expand All @@ -29,7 +29,7 @@ public struct Move: Equatable, Hashable, Sendable {
}

/// Rank, file, or square disambiguation of moves.
public enum Disambiguation: Equatable, Hashable, Sendable {
public enum Disambiguation: Hashable, Sendable {
case byFile(Square.File)
case byRank(Square.Rank)
case bySquare(Square)
Expand Down
15 changes: 12 additions & 3 deletions Sources/ChessKit/MoveTree/MoveTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
///
/// The tree maintains the move order including variations and
/// provides index-based access for any element in the tree.
public struct MoveTree {
public struct MoveTree: Hashable {

/// The index of the root of the move tree.
///
Expand Down Expand Up @@ -263,7 +263,7 @@ public struct MoveTree {

/// An element for representing the ``MoveTree`` in
/// PGN (Portable Game Notation) format.
public enum PGNElement: Hashable, Equatable, Sendable {
public enum PGNElement: Hashable, Sendable {
/// e.g. `1.`
case whiteNumber(Int)
/// e.g. `1...`
Expand Down Expand Up @@ -340,7 +340,7 @@ extension MoveTree: Equatable {
extension MoveTree {

/// Object that represents a node in the move tree.
class Node: Equatable {
class Node: Hashable {

/// The move for this node.
var move: Move
Expand All @@ -362,6 +362,15 @@ extension MoveTree {
lhs.index == rhs.index && lhs.move == rhs.move
}

// MARK: - Hashable
func hash(into hasher: inout Hasher) {
hasher.combine(move)
hasher.combine(index)
hasher.combine(previous)
hasher.combine(next)
hasher.combine(children)
}

}

}
2 changes: 1 addition & 1 deletion Sources/ChessKit/Piece.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

/// Represents a piece on the chess board.
public struct Piece: Equatable, Hashable, Sendable {
public struct Piece: Hashable, Sendable {

/// Represents the color of a piece.
public enum Color: String, CaseIterable, Sendable {
Expand Down
5 changes: 4 additions & 1 deletion Sources/ChessKit/Position.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

/// Represents the collection of pieces on the chess board.
public struct Position: Equatable, Sendable {
public struct Position: Sendable {

/// The pieces currently existing on the board in this position.
public var pieces: [Piece] {
Expand Down Expand Up @@ -210,6 +210,7 @@ public struct Position: Equatable, Sendable {

}

// MARK: - Sample positions
extension Position {
/// A random chess position that can be used for testing.
public static let test = Position(pieces: [
Expand All @@ -227,6 +228,7 @@ extension Position {
public static let standard = Position(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")!
}

// MARK: - CustomStringConvertible
extension Position: CustomStringConvertible {

public var description: String {
Expand All @@ -235,6 +237,7 @@ extension Position: CustomStringConvertible {

}

// MARK: - Hashable
extension Position: Hashable {

public func hash(into hasher: inout Hasher) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ChessKit/Special Moves/Castling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

/// Structure that captures legal castling moves.
struct LegalCastlings: Equatable, Hashable, Sendable {
struct LegalCastlings: Hashable, Sendable {

private var legal: [Castling]

Expand Down Expand Up @@ -55,7 +55,7 @@ struct LegalCastlings: Equatable, Hashable, Sendable {
///
/// Contains various characteristics of the castling move
/// such as king and rook start and end squares and notation.
public struct Castling: Equatable, Hashable, Sendable {
public struct Castling: Hashable, Sendable {

/// Kingside castle for black.
static let bK = Castling(side: .king, color: .black)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Special Moves/EnPassant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

/// Structure that captures en passant moves.
struct EnPassant: Equatable, Hashable, Sendable {
struct EnPassant: Hashable, Sendable {

/// Pawn that is capable of being captured by en passant.
var pawn: Piece
Expand Down
4 changes: 2 additions & 2 deletions Sources/ChessKit/Square.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChessKit
//

public enum Square: Int, Equatable, CaseIterable, Sendable {
public enum Square: Int, CaseIterable, Sendable {
/// The file on the chess board, from a to h.
public enum File: String, CaseIterable, Sendable {
case a, b, c, d, e, f, g, h
Expand Down Expand Up @@ -45,7 +45,7 @@ public enum Square: Int, Equatable, CaseIterable, Sendable {
}

/// The rank on the chess board, from 1 to 8.
public struct Rank: ExpressibleByIntegerLiteral, Equatable, Hashable, Sendable {
public struct Rank: ExpressibleByIntegerLiteral, Hashable, Sendable {
/// The possible range of Rank numbers.
public static let range = 1...8

Expand Down

0 comments on commit 17aa97a

Please sign in to comment.