BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
TranspositionTable.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
25
26#pragma once
27
28#include <cstddef> // IWYU pragma: keep - for size_t
29#include <cstdint> // IWYU pragma: keep - for std::uint_least8_t
32#include <optional>
33#include <span>
34#include <utility>
35
36namespace chess::game {
37struct Position;
38} // namespace chess::game
39
40namespace ben_bot {
41
42namespace search {
43 struct Bounds;
44} // namespace search
45
48using std::size_t;
49
55enum class EvalType : std::uint_least8_t {
59};
60
66struct [[nodiscard]] TTData final {
68 size_t searchedDepth { 0uz };
69
73 eval::Value eval { UINT16_C(0) };
74
77
82 std::optional<Move> bestMove;
83
84 constexpr auto operator==(const TTData& other) const noexcept -> bool = default;
85};
86
91class TranspositionTable final {
92public:
93 explicit TranspositionTable(const size_t initialSize = 16uz)
94 {
95 resize(initialSize);
96 }
97
98 ~TranspositionTable() { deallocate(); }
99
102
105
109 [[nodiscard]] auto find(const Position& pos) const -> std::optional<TTData>;
110
114 using ProbedEval = std::pair<eval::Value, EvalType>;
115
122 [[nodiscard]] auto probe_eval(
123 const Position& pos, size_t depth, const search::Bounds& bounds) const
124 -> std::optional<ProbedEval>;
125
129 [[nodiscard]] auto get_best_response(
130 const Position& pos, Move move) const
131 -> std::optional<Move>;
132
134 void store(const Position& pos, const TTData& record);
135
137 void clear();
138
144 [[clang::reinitializes]] void resize(size_t sizeMB);
145
149 [[nodiscard]] auto hashfull() const -> size_t;
150
154 void new_search() noexcept;
155
159 void prefetch(const Position& pos) const noexcept;
160
161private:
162 struct Entry;
163 struct Cluster;
164
165 void deallocate();
166
167 [[nodiscard]] auto index_table(size_t clusterIdx) const noexcept -> Cluster&;
168
169 // this is the hash function
170 [[nodiscard]] auto find_cluster(std::uint64_t key) const noexcept -> std::span<Entry>;
171
172 Cluster* table { nullptr };
173
174 size_t clusterCount { 0uz };
175
176 std::uint8_t generation { UINT8_C(0) };
177};
178
179} // namespace ben_bot
void resize(size_t sizeMB)
TranspositionTable(const size_t initialSize=16uz)
auto find(const Position &pos) const -> std::optional< TTData >
TranspositionTable & operator=(TranspositionTable &&other) noexcept
TranspositionTable(const TranspositionTable &)=delete
TranspositionTable & operator=(const TranspositionTable &)=delete
auto hashfull() const -> size_t
std::pair< eval::Value, EvalType > ProbedEval
TranspositionTable(TranspositionTable &&other) noexcept
void new_search() noexcept
void prefetch(const Position &pos) const noexcept
auto get_best_response(const Position &pos, Move move) const -> std::optional< Move >
void store(const Position &pos, const TTData &record)
auto probe_eval(const Position &pos, size_t depth, const search::Bounds &bounds) const -> std::optional< ProbedEval >
@ Beta
Indicates that the evaluation is a minimum evaluation; for example, if eval is 16,...
@ Exact
Indicates that the evaluation value is an exact evaluation. This also indicates that this is a PV nod...
@ Alpha
Indicates that the evaluation value is a maximum evaluation; for example, if eval is 16,...
std::int16_t Value
Definition Score.hpp:38
std::optional< Move > bestMove
constexpr auto operator==(const TTData &other) const noexcept -> bool=default