2424#include < libbenbot/data-structures/TranspositionTable.hpp>
2525#include < libbenbot/search/Callbacks.hpp>
2626#include < libbenbot/search/Options.hpp>
27+ #include < libchess/game/Position.hpp>
28+ #include < optional>
2729#include < utility>
2830
2931namespace ben_bot ::search {
3032
33+ using chess::game::Position;
34+
3135/* * This struct encapsulates everything needed to perform a search.
3236 You can keep one of these alive between searches by simply updating
3337 the options and then calling ``search()`` again.
@@ -50,31 +54,13 @@ struct Context final {
5054 Context (Context&&) = delete ;
5155 Context& operator =(Context&&) = delete ;
5256
53- /* * The options to use for the search.
54- This object can only be safely mutated when no search is executing.
55- */
56- Options options;
57-
58- /* * The transposition table used for this search.
59- This object's methods can only be safely called when no search is executing.
60- */
61- TranspositionTable transTable;
62-
63- /* * The callbacks used to provide results about the search. */
64- Callbacks callbacks;
65-
6657 /* * Performs a search.
6758 Results will be propagated via the ``callbacks`` that have been
6859 assigned.
6960
7061 The search may execute for a potentially unbounded amount of time.
7162 The search can be interrupted by calling the ``abort()`` method while
7263 ``search()`` is executing.
73-
74- This function accesses ``options`` and ``transTable``; these objects
75- must not be mutated while ``search()`` is executing. ``abort()``,
76- ``wait()``, ``in_progress()``, and ``reset()`` may be called while
77- ``search()`` is executing without introducing data races.
7864 */
7965 void search ();
8066
@@ -88,14 +74,29 @@ struct Context final {
8874
8975 /* * Clears the transposition table.
9076 If a search is in progress, this method blocks until it returns.
91- Invoking this method is thread-safe, even if a search was in progress.
9277 */
9378 void clear_transposition_table ()
9479 {
9580 wait ();
9681 transTable.clear ();
9782 }
9883
84+ /* * Resizes the transposition table.
85+ If a search is in progress, this method blocks until it returns.
86+ */
87+ void resize_transposition_table (size_t sizeMB)
88+ {
89+ wait ();
90+ transTable.resize (sizeMB);
91+ }
92+
93+ /* * Probes the transposition table for the given position. */
94+ [[nodiscard]] auto probe_transposition_table (const Position& pos) const
95+ -> std::optional<TTData>
96+ {
97+ return transTable.find (pos);
98+ }
99+
99100 /* * Returns true if a search is currently in progress. */
100101 [[nodiscard]] auto in_progress () const noexcept -> bool { return activeFlag.load (std::memory_order::acquire); }
101102
@@ -115,13 +116,63 @@ struct Context final {
115116 */
116117 void ponder_hit () noexcept { pondering.store (false , std::memory_order::release); }
117118
119+ /* * Sets the position to be searched by the next search.
120+ If a search is in progress, this function blocks until it completes.
121+ */
122+ void set_position (const Position& pos)
123+ {
124+ wait ();
125+ position = pos;
126+
127+ // clear this so that all legal moves will be searched by default
128+ options.movesToSearch .clear ();
129+ }
130+
131+ /* * Sets the options to be used by the next search.
132+ If a search is in progress, this function blocks until it completes.
133+ */
134+ void set_options (const Options& opts)
135+ {
136+ wait ();
137+ options = opts;
138+ }
139+
140+ /* * Sets the options to be used by the next search.
141+ If a search is in progress, this function blocks until it completes.
142+ */
143+ void set_options (const chess::uci::GoCommandOptions& opts)
144+ {
145+ wait ();
146+ options = Options::from_libchess (opts, position.is_white_to_move ());
147+ }
148+
149+ /* * Sets the result callbacks that will be used for the next search.
150+ If a search is in progress, this function blocks until it completes.
151+ */
152+ void set_callbacks (Callbacks&& callbacksToUse)
153+ {
154+ wait ();
155+ callbacks = std::move (callbacksToUse);
156+ }
157+
158+ /* * Returns the current position. */
159+ [[nodiscard]] auto get_position () const noexcept -> const Position& { return position; }
160+
118161private:
119162 std::atomic_bool exitFlag { false };
120163
121164 std::atomic_bool activeFlag { false };
122165
123166 std::atomic_bool pondering { false };
124167
168+ Position position;
169+
170+ Options options;
171+
172+ Callbacks callbacks;
173+
174+ TranspositionTable transTable;
175+
125176 KillerMoves killerMoves;
126177};
127178
0 commit comments