BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
SearchStack.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 <array>
23#include <cassert>
24#include <cstddef> // IWYU pragma: keep - for size_t & ptrdiff_t
25#include <initializer_list>
26#include <iterator>
30#include <optional>
31
32namespace ben_bot {
33
34using chess::moves::Move;
35using std::size_t;
36
42struct SearchStackEntry final {
48
54 std::optional<eval::Score> staticEval;
55};
56
70struct SearchStack final {
72 static constexpr auto SENTINELS { 4uz };
73
78 [[nodiscard]] auto at(const size_t plyFromRoot) noexcept -> SearchStackEntry*
79 {
80 assert(plyFromRoot <= search::MAX_PLY);
81
82 return std::next(entries.data(), static_cast<std::ptrdiff_t>(SENTINELS + plyFromRoot));
83 }
84
86 [[nodiscard]] auto at(const size_t plyFromRoot) const noexcept -> const SearchStackEntry*
87 {
88 assert(plyFromRoot <= search::MAX_PLY);
89
90 return std::next(entries.data(), static_cast<std::ptrdiff_t>(SENTINELS + plyFromRoot));
91 }
92
93private:
94 // the search aborts when it reaches MAX_PLY, so that's the deepest node.
95 // The extra entry is for the child entry that a node at MAX_PLY would
96 // write if it ever recursed.
97 std::array<SearchStackEntry, SENTINELS + search::MAX_PLY + 2uz> entries { };
98};
99
118[[nodiscard]] inline auto is_improving(const SearchStackEntry* const entry) noexcept -> bool
119{
120 assert(entry != nullptr);
121
122 const auto current = entry->staticEval;
123
124 if (not current.has_value())
125 return false;
126
127 static_assert(SearchStack::SENTINELS >= 4uz);
128
129 for (const auto pliesBack : { 2, 4 }) {
130 if (const auto previous = std::prev(entry, pliesBack)->staticEval)
131 return *current > *previous;
132 }
133
134 return true;
135}
136
137} // namespace ben_bot
auto is_improving(const SearchStackEntry *const entry) noexcept -> bool
constexpr auto MAX_PLY
Definition Constants.hpp:27
std::optional< eval::Score > staticEval
auto at(const size_t plyFromRoot) const noexcept -> const SearchStackEntry *
static constexpr auto SENTINELS
auto at(const size_t plyFromRoot) noexcept -> SearchStackEntry *