BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Context.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <atomic>
28#include <optional>
29#include <utility>
30
31namespace ben_bot::search {
32
33using chess::game::Position;
34
41struct Context final {
43 Context() = default;
44
46 explicit Context(Callbacks&& callbacksToUse)
47 : callbacks { std::move(callbacksToUse) }
48 {
49 }
50
51 Context(const Context&) = delete;
52 Context& operator=(const Context&) = delete;
53
54 Context(Context&&) = delete;
56
57 ~Context() = default;
58
67 void search();
68
75 void abort() noexcept { exitFlag.store(true, std::memory_order::release); }
76
81 {
82 wait();
83 transTable.clear();
84 }
85
89 void resize_transposition_table(size_t sizeMB)
90 {
91 wait();
92 transTable.resize(sizeMB);
93 }
94
96 [[nodiscard]] auto probe_transposition_table(const Position& pos) const
97 -> std::optional<TTData>
98 {
99 return transTable.find(pos);
100 }
101
103 [[nodiscard]] auto in_progress() const noexcept -> bool { return activeFlag.load(std::memory_order::acquire); }
104
108 void wait() const;
109
114 void set_pondering(const bool isPonderMode) noexcept { pondering.store(isPonderMode, std::memory_order::release); }
115
119 void ponder_hit() noexcept { pondering.store(false, std::memory_order::release); }
120
124 void set_position(const Position& pos)
125 {
126 wait();
127 position = pos;
128
129 // clear this so that all legal moves will be searched by default
130 options.movesToSearch.clear();
131 }
132
136 void set_options(const Options& opts)
137 {
138 wait();
139 options = opts;
140 }
141
146 {
147 wait();
148 options = Options::from_libchess(opts, position.is_white_to_move());
149 }
150
154 void set_callbacks(Callbacks&& callbacksToUse)
155 {
156 wait();
157 callbacks = std::move(callbacksToUse);
158 }
159
161 [[nodiscard]] auto get_position() const noexcept -> const Position& { return position; }
162
163private:
164 std::atomic_bool exitFlag { false };
165
166 std::atomic_bool activeFlag { false };
167
168 std::atomic_bool pondering { false };
169
170 Position position;
171
172 Options options;
173
174 Callbacks callbacks;
175
176 TranspositionTable transTable;
177
178 KillerMoves killerMoves;
179};
180
181} // namespace ben_bot::search
void resize_transposition_table(size_t sizeMB)
Definition Context.hpp:89
Context(Callbacks &&callbacksToUse)
Definition Context.hpp:46
void ponder_hit() noexcept
Definition Context.hpp:119
void set_options(const chess::uci::GoCommandOptions &opts)
Definition Context.hpp:145
Context(const Context &)=delete
auto probe_transposition_table(const Position &pos) const -> std::optional< TTData >
Definition Context.hpp:96
void set_callbacks(Callbacks &&callbacksToUse)
Definition Context.hpp:154
Context & operator=(Context &&)=delete
auto in_progress() const noexcept -> bool
Definition Context.hpp:103
Context(Context &&)=delete
auto get_position() const noexcept -> const Position &
Definition Context.hpp:161
void abort() noexcept
Definition Context.hpp:75
void set_position(const Position &pos)
Definition Context.hpp:124
void set_pondering(const bool isPonderMode) noexcept
Definition Context.hpp:114
Context & operator=(const Context &)=delete
void set_options(const Options &opts)
Definition Context.hpp:136
static auto from_libchess(const chess::uci::GoCommandOptions &goOptions, bool isWhiteToMove) -> Options