HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
lookup-utils.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__LOOKUP_UTILS_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__LOOKUP_UTILS_H_INCLUDED
19
20#include "chessboard-types.h"
21
22#include <array>
23#include <cstdint>
24
25
27{
28
31
33
34
63template <typename U64Type>
64inline const U64Type &turnSpecificLookup(const std::array<U64Type, 2U> &array, Color turn) noexcept
65{
66 static_assert(sizeof(U64Type) == 8U);
67 static_assert(Color::WHITE == Color { 0U });
68 static_assert(Color::BLACK == Color { 8U });
69
70 assert(turn == Color::WHITE || turn == Color::BLACK);
71
72 const std::uint8_t *ptr { std::bit_cast<const std::uint8_t *>(array.data()) };
73 const U64Type *ptrAdjusted { std::bit_cast<const U64Type *>(ptr + static_cast<std::ptrdiff_t>(turn)) };
74 return *ptrAdjusted;
75}
76
91template <typename U64Type, std::size_t N>
92inline const U64Type &turnSpecificArrayLookup(
93 const std::array<std::array<U64Type, 2U>, N> &array, std::size_t i, Color turn) noexcept
94{
95 static_assert(sizeof(U64Type) == 8U);
96 static_assert(Color::WHITE == Color { 0U });
97 static_assert(Color::BLACK == Color { 8U });
98
99 assert(turn == Color::WHITE || turn == Color::BLACK);
100 assert(i < N);
101
102 const std::uint8_t *ptr { std::bit_cast<const std::uint8_t *>(array.data()) };
103 const U64Type *ptrAdjusted {
104 std::bit_cast<const U64Type *>(
105 ptr + static_cast<std::ptrdiff_t>(turn) + 16U * i) };
106 return *ptrAdjusted;
107}
108
109
110
112
113}
114
115#endif
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
@ BLACK
Black piece or black side to move.
@ WHITE
White piece or white side to move.
const U64Type & turnSpecificLookup(const std::array< U64Type, 2U > &array, Color turn) noexcept
Piece attack tables.
Definition lookup-utils.h:64
const U64Type & turnSpecificArrayLookup(const std::array< std::array< U64Type, 2U >, N > &array, std::size_t i, Color turn) noexcept
Optimized lookup for arrays indexed by general index and turn (side-to-move).
Definition lookup-utils.h:92
Definition chessboard-types-squareset.h:30