Skip to content

Commit d2f5005

Browse files
authored
refactor: separate libutil (#484)
2 parents 6e62f4a + 051cfe4 commit d2f5005

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+286
-180
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cpack_add_component_group (
3333
ben_bot_all DISPLAY_NAME "BenBot" DESCRIPTION "All components of BenBot" EXPANDED BOLD_TITLE
3434
)
3535

36+
add_subdirectory (libutil)
3637
add_subdirectory (libchess)
3738
add_subdirectory (libbenbot)
3839
add_subdirectory (ben-bot)

ben-bot/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <iterator>
2323
#include <libbenbot/engine/Engine.hpp>
2424
#include <libchess/uci/Printing.hpp>
25-
#include <libchess/util/Console.hpp>
25+
#include <libutil/Console.hpp>
2626
#include <span>
2727
#include <string>
2828
#include <string_view>
@@ -104,7 +104,7 @@ using chess::uci::printing::info_string;
104104

105105
int main(const int argc, const char** argv)
106106
try {
107-
chess::util::enable_utf8_console_output();
107+
util::enable_utf8_console_output();
108108

109109
const auto [noLoop, uciCommand] = Arguments::parse(argc, argv);
110110

docs/top-level-groups.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
/** @defgroup util Utilities
2+
General utilities not specifically related to chess.
3+
*/
4+
5+
/** @namespace util
6+
This namespace contains generic utilities.
7+
@ingroup util
8+
*/
9+
110
/** @defgroup libchess libchess
211
A general purpose chess library and generic chess engine backend.
312
*/
413

14+
/** @namespace chess
15+
This namespace contains the libchess chess library.
16+
@ingroup libchess
17+
*/
18+
519
/** @defgroup libbenbot libbenbot
620
BenBot's evaluation and search logic, implemented as a library.
721
*/
822

9-
/** @defgroup benbot BenBot
10-
Code outside the libraries, in the engine executable itself.
23+
/** @namespace ben_bot
24+
This namespace contains all BenBot-specific code.
25+
@ingroup libbenbot
1126
*/

libbenbot/include/libbenbot/engine/ColorPrinting.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
#include <string_view>
2424

25-
namespace chess::util::strings {
25+
namespace util::strings {
2626
struct TextTable;
27-
} // namespace chess::util::strings
27+
} // namespace util::strings
2828

2929
namespace chess::game {
3030
struct Position;
@@ -42,13 +42,16 @@ using std::string_view;
4242
/** Prints the given table with bold headings, faint outlines,
4343
and regular content cells.
4444
*/
45-
void print_colored_table(const chess::util::strings::TextTable& table);
45+
void print_colored_table(
46+
const util::strings::TextTable& table);
4647

4748
/** Prints the given position with faint file/rank labels. */
48-
void print_colored_board(const Position& pos, bool utf8);
49+
void print_colored_board(
50+
const Position& pos, bool utf8);
4951

5052
/** Prints the given label faintly, and the info as regular text. */
51-
void print_labeled_info(string_view label, string_view info);
53+
void print_labeled_info(
54+
string_view label, string_view info);
5255

5356
/** @} */
5457

libbenbot/src/data-structures/TranspositionTable.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#include <libbenbot/eval/Score.hpp>
3030
#include <libbenbot/search/Bounds.hpp>
3131
#include <libchess/game/Position.hpp>
32-
#include <libchess/util/Math.hpp>
33-
#include <libchess/util/Memory.hpp>
32+
#include <libutil/Math.hpp>
33+
#include <libutil/Memory.hpp>
3434
#include <memory>
3535
#include <new>
3636
#include <numeric>
@@ -162,7 +162,7 @@ void TranspositionTable::resize(const size_t sizeMB)
162162
clusterCount = newClusterCount;
163163

164164
table = static_cast<Cluster*>(
165-
chess::util::memory::page_aligned_alloc(
165+
util::memory::page_aligned_alloc(
166166
clusterCount * sizeof(Cluster)));
167167

168168
if (table == nullptr) {
@@ -183,7 +183,7 @@ void TranspositionTable::deallocate()
183183
{
184184
std::destroy_n(table, clusterCount);
185185

186-
chess::util::memory::page_aligned_free(table);
186+
util::memory::page_aligned_free(table);
187187

188188
table = nullptr;
189189
clusterCount = 0uz;
@@ -219,7 +219,7 @@ void TranspositionTable::new_search() noexcept
219219
auto TranspositionTable::find_cluster(const Position::Hash key) const noexcept -> std::span<Entry>
220220
{
221221
return index_table(
222-
chess::util::math::mul_hi64(key, clusterCount))
222+
util::math::mul_hi64(key, clusterCount))
223223
.records;
224224
}
225225

@@ -333,7 +333,7 @@ void TranspositionTable::store(const Position& pos, const TTData& record)
333333

334334
void TranspositionTable::prefetch(const Position& pos) const noexcept
335335
{
336-
chess::util::memory::prefetch(
336+
util::memory::prefetch(
337337
find_cluster(pos.hash).data());
338338
}
339339

libbenbot/src/engine/Bench.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
#include <libbenbot/search/Thread.hpp>
3030
#include <libchess/notation/EPD.hpp>
3131
#include <libchess/uci/Printing.hpp>
32-
#include <libchess/util/Chrono.hpp>
33-
#include <libchess/util/Files.hpp>
34-
#include <libchess/util/Strings.hpp>
35-
#include <libchess/util/Threading.hpp>
32+
#include <libutil/Chrono.hpp>
33+
#include <libutil/Files.hpp>
34+
#include <libutil/Strings.hpp>
35+
#include <libutil/Threading.hpp>
3636
#include <memory>
3737
#include <numeric>
3838
#include <ranges>
@@ -47,7 +47,6 @@ using std::size_t;
4747
using std::string_view;
4848
using uci::printing::info_string;
4949

50-
namespace util = chess::util;
5150
namespace notation = chess::notation;
5251

5352
namespace {
@@ -218,7 +217,7 @@ void Engine::run_bench(const string_view arguments) const
218217
const auto epdPath = absolute(std::filesystem::path { filePath });
219218

220219
[[maybe_unused]] const auto result
221-
= util::load_file_as_string(epdPath)
220+
= util::files::load(epdPath)
222221
.transform([this, defaultDepth, absPathStr = epdPath.string()](const string_view fileContent) {
223222
info_string(std::format("Running bench for {}...", absPathStr));
224223

libbenbot/src/engine/ColorPrinting.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include <libbenbot/engine/ColorPrinting.hpp>
1919
#include <libbenbot/engine/Engine.hpp>
2020
#include <libchess/game/Position.hpp>
21-
#include <libchess/util/Strings.hpp>
22-
#include <libchess/util/TextTable.hpp>
21+
#include <libutil/Strings.hpp>
22+
#include <libutil/TextTable.hpp>
2323
#include <ranges>
2424
#include <string_view>
2525
#include <termcolor/termcolor.hpp>
@@ -35,7 +35,7 @@ namespace {
3535
{
3636
using Lines = beman::inplace_vector::inplace_vector<string_view, MaxLines>;
3737

38-
return chess::util::strings::lines_view(input)
38+
return util::strings::lines_view(input)
3939
| std::views::take(MaxLines)
4040
| std::ranges::to<Lines>();
4141
}
@@ -57,13 +57,14 @@ void Engine::print_logo_and_version() const
5757
<< termcolor::reset;
5858
}
5959

60-
void print_colored_table(const chess::util::strings::TextTable& table)
60+
void print_colored_table(
61+
const util::strings::TextTable& table)
6162
{
6263
table.print(
6364
[](const string_view heading) {
6465
// we want the heading text to be underlined, but not the
6566
// whitespace that follows the text to complete the cell
66-
const auto trimmed = chess::util::strings::trim(heading);
67+
const auto trimmed = util::strings::trim(heading);
6768

6869
cout << termcolor::bold << termcolor::underline
6970
<< trimmed
@@ -85,7 +86,8 @@ void print_colored_table(const chess::util::strings::TextTable& table)
8586
cout << termcolor::reset;
8687
}
8788

88-
void print_colored_board(const Position& pos, const bool utf8)
89+
void print_colored_board(
90+
const Position& pos, const bool utf8)
8991
{
9092
const auto boardStr = utf8 ? print_utf8(pos) : print_ascii(pos);
9193

@@ -101,7 +103,8 @@ void print_colored_board(const Position& pos, const bool utf8)
101103
<< termcolor::reset;
102104
}
103105

104-
void print_labeled_info(const string_view label, const string_view info)
106+
void print_labeled_info(
107+
const string_view label, const string_view info)
105108
{
106109
cout << termcolor::white << label << termcolor::reset << info << '\n';
107110
}

libbenbot/src/engine/Engine.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
#include <libchess/notation/MoveFormats.hpp>
2525
#include <libchess/uci/Options.hpp>
2626
#include <libchess/uci/Printing.hpp>
27-
#include <libchess/util/Files.hpp>
28-
#include <libchess/util/Logger.hpp>
29-
#include <libchess/util/Variant.hpp>
27+
#include <libutil/Files.hpp>
28+
#include <libutil/Logger.hpp>
29+
#include <libutil/Variant.hpp>
3030
#include <magic_enum/magic_enum.hpp>
3131
#include <nlohmann/json.hpp>
3232
#include <ranges>
@@ -132,7 +132,7 @@ void Engine::start_file_logger(const string_view arg)
132132
}
133133

134134
[[maybe_unused]] const auto result
135-
= chess::util::start_file_logger(path { arg })
135+
= util::start_file_logger(path { arg })
136136
.transform_error(info_string);
137137
}
138138

@@ -167,7 +167,7 @@ void Engine::write_config_file(const string_view arg) const
167167
continue;
168168

169169
std::visit(
170-
chess::util::Visitor {
170+
util::Visitor {
171171
[&optionsData, name = opt->get_name()](const auto value) {
172172
optionsData[name] = value;
173173
} },
@@ -182,7 +182,7 @@ void Engine::write_config_file(const string_view arg) const
182182
const auto filePath = absolute(path { arg });
183183

184184
[[maybe_unused]] const auto result
185-
= chess::util::overwrite_file(
185+
= util::files::overwrite(
186186
filePath, data.dump(2))
187187
.transform([&filePath] {
188188
info_string(std::format(
@@ -206,7 +206,7 @@ void Engine::read_config_file(const path& file)
206206
const auto filePath = absolute(file);
207207

208208
[[maybe_unused]] const auto result
209-
= chess::util::load_file_as_string(filePath)
209+
= util::files::load(filePath)
210210
.transform([this, &filePath](const string_view fileContent) {
211211
const auto data = json::parse(fileContent);
212212

libbenbot/src/engine/Perft.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
#include <libchess/moves/Move.hpp>
2020
#include <libchess/moves/Perft.hpp>
2121
#include <libchess/uci/Printing.hpp>
22-
#include <libchess/util/Strings.hpp>
22+
#include <libutil/Strings.hpp>
2323
#include <nlohmann/json.hpp>
2424
#include <print>
2525
#include <string>
2626

2727
namespace ben_bot {
2828

29-
namespace util = chess::util;
30-
3129
using uci::printing::info_string;
3230

3331
namespace {

libbenbot/src/engine/Printing.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include <libchess/notation/FEN.hpp>
2424
#include <libchess/notation/MoveFormats.hpp>
2525
#include <libchess/uci/Printing.hpp>
26-
#include <libchess/util/Strings.hpp>
27-
#include <libchess/util/TextTable.hpp>
26+
#include <libutil/Strings.hpp>
27+
#include <libutil/TextTable.hpp>
2828
#include <magic_enum/magic_enum.hpp>
2929
#include <print>
3030
#include <string>
@@ -33,13 +33,12 @@
3333

3434
namespace ben_bot {
3535

36-
using Result = search::Result;
37-
38-
using chess::util::strings::trim;
3936
using std::println;
4037
using uci::printing::info_string;
38+
using util::strings::trim;
4139

42-
using chess::util::strings::TextTable;
40+
using Result = search::Result;
41+
using util::strings::TextTable;
4342

4443
auto Engine::get_name() const -> std::string
4544
{

0 commit comments

Comments
 (0)