Skip to content

Commit ba89c3d

Browse files
authored
refactor: use fmt over stringstream (#496)
1 parent a991309 commit ba89c3d

File tree

5 files changed

+58
-128
lines changed

5 files changed

+58
-128
lines changed

app/src/matchmaking/output/output_cutechess.hpp

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,89 +18,44 @@ class Cutechess : public IOutput {
1818
void printResult(const Stats& stats, const std::string& first, const std::string& second) override {
1919
const elo::EloWDL elo(stats);
2020

21-
std::stringstream ss;
22-
ss << "Score of " //
23-
<< first //
24-
<< " vs " //
25-
<< second //
26-
<< ": " //
27-
<< stats.wins //
28-
<< " - " //
29-
<< stats.losses //
30-
<< " - " //
31-
<< stats.draws //
32-
<< " [" //
33-
<< elo.printScore() //
34-
<< "] " //
35-
<< stats.wins + stats.losses + stats.draws //
36-
<< "\n";
37-
38-
std::cout << ss.str() << std::flush;
21+
auto fmt = fmt::format("Score of {} vs {}: {} - {} - {} [{}] {}\n", first, second, stats.wins, stats.losses,
22+
stats.draws, elo.printScore(), stats.wins + stats.losses + stats.draws);
23+
24+
std::cout << fmt << std::flush;
3925
}
4026

4127
void printElo(const Stats& stats, const std::string&, const std::string&,
4228
const std::pair<const engine::UciEngine&, const engine::UciEngine&>&, const std::string&) override {
4329
const elo::EloWDL elo(stats);
4430

45-
std::stringstream ss;
46-
ss << "Elo difference: " //
47-
<< elo.getElo() //
48-
<< ", " //
49-
<< "LOS: " //
50-
<< elo.los() //
51-
<< ", " //
52-
<< "DrawRatio: " //
53-
<< elo.drawRatio(stats) //
54-
<< "\n";
55-
56-
std::cout << ss.str() << std::flush;
31+
auto fmt =
32+
fmt::format("Elo difference: {}, LOS: {}, DrawRatio: {}\n", elo.getElo(), elo.los(), elo.drawRatio(stats));
33+
34+
std::cout << fmt << std::flush;
5735
}
5836

5937
void printSprt(const SPRT& sprt, const Stats& stats) override {
6038
if (sprt.isEnabled()) {
61-
std::stringstream ss;
39+
auto fmt = fmt::format("LLR: {:.2f} {} {}\n", sprt.getLLR(stats.wins, stats.draws, stats.losses),
40+
sprt.getBounds(), sprt.getElo());
6241

63-
ss << "LLR: " << std::fixed << std::setprecision(2) << sprt.getLLR(stats.wins, stats.draws, stats.losses)
64-
<< " " << sprt.getBounds() << " " << sprt.getElo() << "\n";
65-
std::cout << ss.str() << std::flush;
42+
std::cout << fmt << std::flush;
6643
}
6744
};
6845

6946
void startGame(const pair_config& configs, std::size_t current_game_count, std::size_t max_game_count) override {
70-
std::stringstream ss;
71-
72-
ss << "Started game " //
73-
<< current_game_count //
74-
<< " of " //
75-
<< max_game_count //
76-
<< " (" //
77-
<< configs.first.name //
78-
<< " vs " //
79-
<< configs.second.name //
80-
<< ")" //
81-
<< "\n";
82-
83-
std::cout << ss.str() << std::flush;
47+
auto fmt = fmt::format("Started game {} of {} ({} vs {})\n", current_game_count, max_game_count,
48+
configs.first.name, configs.second.name);
49+
50+
std::cout << fmt << std::flush;
8451
}
8552

8653
void endGame(const pair_config& configs, const Stats& stats, const std::string& annotation,
8754
std::size_t id) override {
88-
std::stringstream ss;
89-
90-
ss << "Finished game " //
91-
<< id //
92-
<< " (" //
93-
<< configs.first.name //
94-
<< " vs " //
95-
<< configs.second.name //
96-
<< "): " //
97-
<< formatStats(stats) //
98-
<< " {" //
99-
<< annotation //
100-
<< "}" //
101-
<< "\n";
102-
103-
std::cout << ss.str() << std::flush;
55+
auto fmt = fmt::format("Finished game {} ({} vs {}): {} {}\n", id, configs.first.name, configs.second.name,
56+
formatStats(stats), annotation);
57+
58+
std::cout << fmt << std::flush;
10459
}
10560

10661
void endTournament() override { std::cout << "Tournament finished" << std::endl; }

app/src/matchmaking/output/output_fastchess.hpp

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ class Fastchess : public IOutput {
7373
}
7474

7575
// engine, engine2, tc, threads, hash, book
76-
auto line1 = fmt::format("Results of {} vs {} ({}, {}, {}{}):", first, second, tc, threads, hash, book.empty()
77-
? "" : fmt::format(", {}",bookname));
76+
auto line1 = fmt::format("Results of {} vs {} ({}, {}, {}{}):", first, second, tc, threads, hash,
77+
book.empty() ? "" : fmt::format(", {}", bookname));
7878
auto line2 = fmt::format("Elo: {}, nElo: {}", elo->getElo(), elo->nElo());
7979
auto line3 =
8080
fmt::format("LOS: {}, DrawRatio: {}, PairsRatio: {:.2f}", elo->los(), elo->drawRatio(stats), pairsRatio);
@@ -91,72 +91,47 @@ class Fastchess : public IOutput {
9191
if (sprt.isEnabled()) {
9292
double llr = sprt.getLLR(stats, report_penta_);
9393

94-
std::stringstream ss;
94+
auto fmt = fmt::format("LLR: {:.2f} {} {}\n", llr, sprt.getBounds(), sprt.getElo());
9595

96-
ss << "LLR: " << std::fixed << std::setprecision(2) << llr //
97-
<< " " << sprt.getBounds() //
98-
<< " " << sprt.getElo() << "\n";
99-
100-
std::cout << ss.str() << std::flush;
96+
std::cout << fmt << std::flush;
10197
}
10298
}
10399

104100
void startGame(const pair_config& configs, std::size_t current_game_count, std::size_t max_game_count) override {
105-
std::stringstream ss;
106-
107-
ss << "Started game " //
108-
<< current_game_count //
109-
<< " of " //
110-
<< max_game_count //
111-
<< " (" //
112-
<< configs.first.name //
113-
<< " vs " //
114-
<< configs.second.name //
115-
<< ")" //
116-
<< "\n";
117-
118-
std::cout << ss.str() << std::flush;
101+
auto fmt = fmt::format("Started game {} of {} ({} vs {})\n", current_game_count, max_game_count,
102+
configs.first.name, configs.second.name);
103+
104+
std::cout << fmt << std::flush;
119105
}
120106

121107
void endGame(const pair_config& configs, const Stats& stats, const std::string& annotation,
122108
std::size_t id) override {
123-
std::stringstream ss;
124-
125-
ss << "Finished game " //
126-
<< id //
127-
<< " (" //
128-
<< configs.first.name //
129-
<< " vs " //
130-
<< configs.second.name //
131-
<< "): " //
132-
<< formatStats(stats) //
133-
<< " {" //
134-
<< annotation //
135-
<< "}" //
136-
<< "\n";
137-
138-
std::cout << ss.str() << std::flush;
109+
auto fmt = fmt::format("Finished game {} ({} vs {}): {} {}\n", id, configs.first.name, configs.second.name,
110+
formatStats(stats), annotation);
111+
112+
std::cout << fmt << std::flush;
139113
}
140114

141115
void endTournament() override { std::cout << "Tournament finished" << std::endl; }
142116

143117
private:
144118
std::string getTime(const engine::UciEngine& engine) {
145-
if (engine.getConfig().limit.tc.time > 0) {
146-
return fmt::format("{}{:.2g}{}",
147-
engine.getConfig().limit.tc.moves > 0
148-
? fmt::format("{}/", engine.getConfig().limit.tc.moves)
149-
: "",
150-
engine.getConfig().limit.tc.time / 1000.0,
151-
engine.getConfig().limit.tc.increment > 0
152-
? fmt::format("+{:.2g}", engine.getConfig().limit.tc.increment / 1000.0)
153-
: "");
154-
} else if (engine.getConfig().limit.tc.fixed_time > 0) {
155-
return fmt::format("{:.2f}/move", engine.getConfig().limit.tc.fixed_time / 1000.0);
156-
} else if (engine.getConfig().limit.plies > 0) {
157-
return fmt::format("{} plies", engine.getConfig().limit.plies);
158-
} else if (engine.getConfig().limit.nodes > 0) {
159-
return fmt::format("{} nodes", engine.getConfig().limit.nodes);
119+
const auto& limit = engine.getConfig().limit;
120+
121+
if (limit.tc.time > 0) {
122+
auto moves = limit.tc.moves > 0 ? fmt::format("{}/", limit.tc.moves) : "";
123+
auto time = limit.tc.time / 1000.0;
124+
auto increment = limit.tc.increment > 0 ? fmt::format("+{:.2g}", limit.tc.increment / 1000.0) : "";
125+
126+
return fmt::format("{}{:.2g}{}", moves, time, increment);
127+
} else if (limit.tc.fixed_time > 0) {
128+
auto time = limit.tc.fixed_time / 1000.0;
129+
130+
return fmt::format("{:.2f}/move", time);
131+
} else if (limit.plies > 0) {
132+
return fmt::format("{} plies", limit.plies);
133+
} else if (limit.nodes > 0) {
134+
return fmt::format("{} nodes", limit.nodes);
160135
}
161136

162137
return "";

app/src/matchmaking/player.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ class Player {
8080
return input.str();
8181
}
8282

83-
[[nodiscard]] const TimeControl &getTimeControl() const { return time_control_; }
83+
[[nodiscard]] const TimeControl &getTimeControl() const noexcept { return time_control_; }
8484

85-
engine::UciEngine &engine;
85+
void setLost() noexcept { result = chess::GameResult::LOSE; }
86+
void setDraw() noexcept { result = chess::GameResult::DRAW; }
87+
void setWon() noexcept { result = chess::GameResult::WIN; }
8688

87-
chess::Color color = chess::Color::NONE;
89+
[[nodiscard]] chess::GameResult getResult() const noexcept { return result; }
8890

89-
void setLost() { result = chess::GameResult::LOSE; }
90-
void setDraw() { result = chess::GameResult::DRAW; }
91-
void setWon() { result = chess::GameResult::WIN; }
91+
engine::UciEngine &engine;
9292

93-
[[nodiscard]] chess::GameResult getResult() const { return result; }
93+
chess::Color color = chess::Color::NONE;
9494

9595
private:
9696
chess::GameResult result = chess::GameResult::NONE;

app/src/time/timecontrol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ TimeControl::TimeControl(const Limits &limits) : limits_(limits) {
2424
moves_left_ = limits_.moves;
2525
}
2626

27-
std::chrono::milliseconds TimeControl::getTimeoutThreshold() const {
27+
std::chrono::milliseconds TimeControl::getTimeoutThreshold() const noexcept {
2828
return std::chrono::milliseconds(time_left_ + limits_.timemargin + 100);
2929
}
3030

31-
bool TimeControl::updateTime(const int64_t elapsed_millis) {
31+
bool TimeControl::updateTime(const int64_t elapsed_millis) noexcept {
3232
if (limits_.moves > 0) {
3333
if (moves_left_ == 1) {
3434
moves_left_ = limits_.moves;

app/src/time/timecontrol.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class TimeControl {
3838

3939
TimeControl(const Limits &limits);
4040

41-
[[nodiscard]] std::chrono::milliseconds getTimeoutThreshold() const;
42-
[[nodiscard]] bool updateTime(const int64_t elapsed_millis);
41+
[[nodiscard]] std::chrono::milliseconds getTimeoutThreshold() const noexcept;
42+
[[nodiscard]] bool updateTime(const int64_t elapsed_millis) noexcept;
4343

4444
[[nodiscard]] int64_t getTimeLeft() const { return time_left_; }
4545
[[nodiscard]] int64_t getMovesLeft() const { return moves_left_; }

0 commit comments

Comments
 (0)