Skip to content

Commit 5644e14

Browse files
committed
Prefer operator<<() to pretty()
It is more idiomatic, we didn't used it in the past because Position::pretty(Move) had a calling argument, but now we can. As an added benefit, we avoid a lot of string copies in the process because now we avoid std::ostringstream ss. No functional change.
1 parent d07a875 commit 5644e14

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/position.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ CheckInfo::CheckInfo(const Position& pos) {
108108
}
109109

110110

111+
/// operator<<(Position) returns an ASCII representation of the position
112+
113+
std::ostream& operator<<(std::ostream& os, const Position& pos) {
114+
115+
os << "\n +---+---+---+---+---+---+---+---+\n";
116+
117+
for (Rank r = RANK_8; r >= RANK_1; --r)
118+
{
119+
for (File f = FILE_A; f <= FILE_H; ++f)
120+
os << " | " << PieceToChar[pos.piece_on(make_square(f, r))];
121+
122+
os << " |\n +---+---+---+---+---+---+---+---+\n";
123+
}
124+
125+
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
126+
<< std::setfill('0') << std::setw(16) << pos.st->key << "\nCheckers: ";
127+
128+
for (Bitboard b = pos.checkers(); b; )
129+
os << UCI::format_square(pop_lsb(&b)) << " ";
130+
131+
return os;
132+
}
133+
134+
111135
/// Position::init() initializes at startup the various arrays used to compute
112136
/// hash keys and the piece square tables. The latter is a two-step operation:
113137
/// Firstly, the white halves of the tables are copied from PSQT[] tables.
@@ -430,32 +454,6 @@ const string Position::fen() const {
430454
}
431455

432456

433-
/// Position::pretty() returns an ASCII representation of the position
434-
435-
const string Position::pretty() const {
436-
437-
std::ostringstream ss;
438-
439-
ss << "\n +---+---+---+---+---+---+---+---+\n";
440-
441-
for (Rank r = RANK_8; r >= RANK_1; --r)
442-
{
443-
for (File f = FILE_A; f <= FILE_H; ++f)
444-
ss << " | " << PieceToChar[piece_on(make_square(f, r))];
445-
446-
ss << " |\n +---+---+---+---+---+---+---+---+\n";
447-
}
448-
449-
ss << "\nFen: " << fen() << "\nKey: " << std::hex << std::uppercase
450-
<< std::setfill('0') << std::setw(16) << st->key << "\nCheckers: ";
451-
452-
for (Bitboard b = checkers(); b; )
453-
ss << UCI::format_square(pop_lsb(&b)) << " ";
454-
455-
return ss.str();
456-
}
457-
458-
459457
/// Position::game_phase() calculates the game phase interpolating total non-pawn
460458
/// material between endgame and midgame limits.
461459

src/position.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,19 @@ const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
7373
/// when traversing the search tree.
7474

7575
class Position {
76+
77+
friend std::ostream& operator<<(std::ostream&, const Position&);
78+
7679
public:
7780
Position() {}
7881
Position(const Position& pos, Thread* t) { *this = pos; thisThread = t; }
7982
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
8083
Position& operator=(const Position&);
8184
static void init();
8285

83-
// Text input/output
86+
// FEN string input/output
8487
void set(const std::string& fenStr, bool isChess960, Thread* th);
8588
const std::string fen() const;
86-
const std::string pretty() const;
8789

8890
// Position representation
8991
Bitboard pieces() const;

src/uci.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void UCI::loop(int argc, char* argv[]) {
203203
else if (token == "setoption") setoption(is);
204204
else if (token == "flip") pos.flip();
205205
else if (token == "bench") benchmark(pos, is);
206-
else if (token == "d") sync_cout << pos.pretty() << sync_endl;
206+
else if (token == "d") sync_cout << pos << sync_endl;
207207
else if (token == "isready") sync_cout << "readyok" << sync_endl;
208208
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
209209
else

0 commit comments

Comments
 (0)