]> git.sesse.net Git - stockfish/commitdiff
Prefer operator<<() to pretty()
authorMarco Costalba <mcostalba@gmail.com>
Sat, 1 Nov 2014 17:50:27 +0000 (18:50 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 1 Nov 2014 18:02:35 +0000 (19:02 +0100)
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.

src/position.cpp
src/position.h
src/uci.cpp

index 4f72d6588ef3c3f4e6cdf8e60e3206a8cba7de10..d43554d18e20fd9ff14a52014ee2a5dbcf086dd6 100644 (file)
@@ -108,6 +108,30 @@ CheckInfo::CheckInfo(const Position& pos) {
 }
 
 
+/// operator<<(Position) returns an ASCII representation of the position
+
+std::ostream& operator<<(std::ostream& os, const Position& pos) {
+
+  os << "\n +---+---+---+---+---+---+---+---+\n";
+
+  for (Rank r = RANK_8; r >= RANK_1; --r)
+  {
+      for (File f = FILE_A; f <= FILE_H; ++f)
+          os << " | " << PieceToChar[pos.piece_on(make_square(f, r))];
+
+      os << " |\n +---+---+---+---+---+---+---+---+\n";
+  }
+
+  os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
+     << std::setfill('0') << std::setw(16) << pos.st->key << "\nCheckers: ";
+
+  for (Bitboard b = pos.checkers(); b; )
+      os << UCI::format_square(pop_lsb(&b)) << " ";
+
+  return os;
+}
+
+
 /// Position::init() initializes at startup the various arrays used to compute
 /// hash keys and the piece square tables. The latter is a two-step operation:
 /// Firstly, the white halves of the tables are copied from PSQT[] tables.
@@ -430,32 +454,6 @@ const string Position::fen() const {
 }
 
 
-/// Position::pretty() returns an ASCII representation of the position
-
-const string Position::pretty() const {
-
-  std::ostringstream ss;
-
-  ss << "\n +---+---+---+---+---+---+---+---+\n";
-
-  for (Rank r = RANK_8; r >= RANK_1; --r)
-  {
-      for (File f = FILE_A; f <= FILE_H; ++f)
-          ss << " | " << PieceToChar[piece_on(make_square(f, r))];
-
-      ss << " |\n +---+---+---+---+---+---+---+---+\n";
-  }
-
-  ss << "\nFen: " << fen() << "\nKey: " << std::hex << std::uppercase
-     << std::setfill('0') << std::setw(16) << st->key << "\nCheckers: ";
-
-  for (Bitboard b = checkers(); b; )
-      ss << UCI::format_square(pop_lsb(&b)) << " ";
-
-  return ss.str();
-}
-
-
 /// Position::game_phase() calculates the game phase interpolating total non-pawn
 /// material between endgame and midgame limits.
 
index 72932f778e9c07af6bcb9ce89e2822a8b78ddc61..c39f8477b0521597e2a10cfb5adefbc072310020 100644 (file)
@@ -73,6 +73,9 @@ const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
 /// when traversing the search tree.
 
 class Position {
+
+  friend std::ostream& operator<<(std::ostream&, const Position&);
+
 public:
   Position() {}
   Position(const Position& pos, Thread* t) { *this = pos; thisThread = t; }
@@ -80,10 +83,9 @@ public:
   Position& operator=(const Position&);
   static void init();
 
-  // Text input/output
+  // FEN string input/output
   void set(const std::string& fenStr, bool isChess960, Thread* th);
   const std::string fen() const;
-  const std::string pretty() const;
 
   // Position representation
   Bitboard pieces() const;
index cfd112172bf11fe048c4a78447c43888006ca10b..ee1b07023a5fe97b17a8c28cb8911fd9caaefc73 100644 (file)
@@ -203,7 +203,7 @@ void UCI::loop(int argc, char* argv[]) {
       else if (token == "setoption")  setoption(is);
       else if (token == "flip")       pos.flip();
       else if (token == "bench")      benchmark(pos, is);
-      else if (token == "d")          sync_cout << pos.pretty() << sync_endl;
+      else if (token == "d")          sync_cout << pos << sync_endl;
       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
       else if (token == "eval")       sync_cout << Eval::trace(pos) << sync_endl;
       else