-
Notifications
You must be signed in to change notification settings - Fork 13
/
scoreWDLstat.hpp
205 lines (172 loc) · 6.2 KB
/
scoreWDLstat.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <zlib.h>
#include <algorithm>
#include <cstdint>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "external/json.hpp"
enum class Result { WIN = 'W', DRAW = 'D', LOSS = 'L' };
struct ResultKey {
Result white;
Result black;
};
struct Key {
Result result; // game result from PoV of side to move
int move, material, eval; // move number, material count, engine's eval
bool operator==(const Key &k) const {
return result == k.result && move == k.move && material == k.material && eval == k.eval;
}
operator std::size_t() const {
// golden ratio hashing, thus 0x9e3779b9
std::uint32_t hash = static_cast<int>(result);
hash ^= move + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= material + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= eval + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
operator std::string() const {
return "('" + std::string(1, static_cast<char>(result)) + "', " + std::to_string(move) +
", " + std::to_string(material) + ", " + std::to_string(eval) + ")";
}
};
// overload the std::hash function for Key
template <>
struct std::hash<Key> {
std::size_t operator()(const Key &k) const { return static_cast<std::size_t>(k); }
};
// overload the std::equal_to function for Key
template <>
struct std::equal_to<Key> {
bool operator()(const Key &lhs, const Key &rhs) const { return lhs == rhs; }
};
struct TestMetaData {
std::optional<std::string> book, new_tc, resolved_base, resolved_new, tc;
std::optional<int> threads;
std::optional<bool> sprt;
std::optional<std::vector<int>> pentanomial;
};
template <typename T = std::string>
std::optional<T> get_optional(const nlohmann::json &j, const char *name) {
const auto it = j.find(name);
if (it != j.end()) {
return std::optional<T>(j[name]);
} else {
return std::nullopt;
}
}
void from_json(const nlohmann::json &nlohmann_json_j, TestMetaData &nlohmann_json_t) {
auto &j = nlohmann_json_j["args"];
nlohmann_json_t.sprt = j.contains("sprt") ? std::optional<bool>(true) : std::nullopt;
nlohmann_json_t.book = get_optional(j, "book");
nlohmann_json_t.new_tc = get_optional(j, "new_tc");
nlohmann_json_t.resolved_base = get_optional(j, "resolved_base");
nlohmann_json_t.resolved_new = get_optional(j, "resolved_new");
nlohmann_json_t.tc = get_optional(j, "tc");
nlohmann_json_t.threads = get_optional<int>(j, "threads");
auto &jr = nlohmann_json_j["results"];
nlohmann_json_t.pentanomial = get_optional<std::vector<int>>(jr, "pentanomial");
}
/// @brief Custom stof implementation to avoid locale issues, once clang supports std::from_chars
/// for floats this can be removed
/// @param str
/// @return
inline float fast_stof(const char *str) {
float result = 0.0f;
int sign = 1;
int decimal = 0;
float fraction = 1.0f;
// Handle sign
if (*str == '-') {
sign = -1;
str++;
} else if (*str == '+') {
str++;
}
// Convert integer part
while (*str >= '0' && *str <= '9') {
result = result * 10.0f + (*str - '0');
str++;
}
// Convert decimal part
if (*str == '.') {
str++;
while (*str >= '0' && *str <= '9') {
result = result * 10.0f + (*str - '0');
fraction *= 10.0f;
str++;
}
decimal = 1;
}
// Apply sign and adjust for decimal
result *= sign;
if (decimal) {
result /= fraction;
}
return result;
}
/// @brief Get all files from a directory.
/// @param path
/// @param recursive
/// @return
[[nodiscard]] inline std::vector<std::string> get_files(const std::string &path,
bool recursive = false) {
std::vector<std::string> files;
for (const auto &entry : std::filesystem::directory_iterator(path)) {
if (std::filesystem::is_regular_file(entry)) {
std::string stem = entry.path().stem().string();
std::string extension = entry.path().extension().string();
if (extension == ".gz") {
if (stem.size() >= 4 && stem.substr(stem.size() - 4) == ".pgn") {
files.push_back(entry.path().string());
}
} else if (extension == ".pgn") {
files.push_back(entry.path().string());
}
} else if (recursive && std::filesystem::is_directory(entry)) {
auto subdir_files = get_files(entry.path().string(), true);
files.insert(files.end(), subdir_files.begin(), subdir_files.end());
}
}
return files;
}
/// @brief Split into successive n-sized chunks from pgns.
/// @param pgns
/// @param target_chunks
/// @return
[[nodiscard]] inline std::vector<std::vector<std::string>> split_chunks(
const std::vector<std::string> &pgns, int target_chunks) {
const int chunks_size = (pgns.size() + target_chunks - 1) / target_chunks;
auto begin = pgns.begin();
auto end = pgns.end();
std::vector<std::vector<std::string>> chunks;
while (begin != end) {
auto next =
std::next(begin, std::min(chunks_size, static_cast<int>(std::distance(begin, end))));
chunks.push_back(std::vector<std::string>(begin, next));
begin = next;
}
return chunks;
}
class CommandLine {
public:
CommandLine(int argc, char const *argv[]) {
for (int i = 1; i < argc; ++i) {
args.emplace_back(argv[i]);
}
}
bool has_argument(const std::string &arg, bool without_parameter = false) const {
const auto pos = std::find(args.begin(), args.end(), arg);
return pos != args.end() && (without_parameter || std::next(pos) != args.end());
}
std::string get_argument(const std::string &arg, std::string default_value = "") const {
auto it = std::find(args.begin(), args.end(), arg);
if (it != args.end() && std::next(it) != args.end()) {
return *std::next(it);
}
return default_value;
}
private:
std::vector<std::string> args;
};