]> git.sesse.net Git - stockfish/commitdiff
Use std::stack instead of fixed size array
authorMarco Costalba <mcostalba@gmail.com>
Fri, 26 Oct 2012 16:01:13 +0000 (18:01 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 27 Oct 2012 11:31:23 +0000 (13:31 +0200)
Only in not performance critical code like pretty_pv(),
otherwise continue to use the good old C-style arrays
like in extract/insert PV where I have done some code
refactoring anyhow.

No functional change.

src/notation.cpp
src/search.cpp

index dea72441620b3a0ddb5bdd6a2b3fd4adcf2a6af0..15661eec725ab7c7b788f51cc825cb9e201a57f1 100644 (file)
@@ -20,7 +20,7 @@
 #include <cassert>
 #include <iomanip>
 #include <sstream>
-#include <string>
+#include <stack>
 
 #include "movegen.h"
 #include "notation.h"
@@ -226,7 +226,7 @@ string pretty_pv(Position& pos, int depth, Value value, int64_t msecs, Move pv[]
   const int64_t K = 1000;
   const int64_t M = 1000000;
 
-  StateInfo state[MAX_PLY_PLUS_2], *st = state;
+  std::stack<StateInfo> st;
   Move* m = pv;
   string san, padding;
   size_t length;
@@ -261,7 +261,8 @@ string pretty_pv(Position& pos, int depth, Value value, int64_t msecs, Move pv[]
       s << san << ' ';
       length += san.length() + 1;
 
-      pos.do_move(*m++, *st++);
+      st.push(StateInfo());
+      pos.do_move(*m++, st.top());
   }
 
   while (m != pv)
index f82197f87286f83b678b593d8526b479235c9325..bf227d1fb9d2ac034797cfbdb62d380dd65dd034 100644 (file)
@@ -1547,29 +1547,27 @@ void RootMove::extract_pv_from_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
   TTEntry* tte;
-  int ply = 1;
+  int ply = 0;
   Move m = pv[0];
 
-  assert(m != MOVE_NONE && pos.is_pseudo_legal(m));
-
   pv.clear();
-  pv.push_back(m);
-  pos.do_move(m, *st++);
-
-  while (   (tte = TT.probe(pos.key())) != NULL
-         && (m = tte->move()) != MOVE_NONE // Local copy, TT entry could change
-         && pos.is_pseudo_legal(m)
-         && pos.pl_move_is_legal(m, pos.pinned_pieces())
-         && ply < MAX_PLY
-         && (!pos.is_draw<true, true>() || ply < 2))
-  {
+
+  do {
       pv.push_back(m);
-      pos.do_move(m, *st++);
-      ply++;
-  }
-  pv.push_back(MOVE_NONE);
 
-  do pos.undo_move(pv[--ply]); while (ply);
+      assert(pos.move_is_legal(pv[ply]));
+      pos.do_move(pv[ply++], *st++);
+      tte = TT.probe(pos.key());
+
+  } while (   tte
+           && pos.is_pseudo_legal(m = tte->move()) // Local copy, TT could change
+           && pos.pl_move_is_legal(m, pos.pinned_pieces())
+           && ply < MAX_PLY
+           && (!pos.is_draw<true, true>() || ply < 2));
+
+  pv.push_back(MOVE_NONE); // Must be zero-terminating
+
+  while (ply) pos.undo_move(pv[--ply]);
 }
 
 
@@ -1581,27 +1579,28 @@ void RootMove::insert_pv_in_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
   TTEntry* tte;
-  Key k;
-  Value v, m = VALUE_NONE;
   int ply = 0;
-
-  assert(pv[ply] != MOVE_NONE && pos.is_pseudo_legal(pv[ply]));
+  Value v, m;
 
   do {
-      k = pos.key();
-      tte = TT.probe(k);
+      tte = TT.probe(pos.key());
 
-      // Don't overwrite existing correct entries
-      if (!tte || tte->move() != pv[ply])
+      if (!tte || tte->move() != pv[ply]) // Don't overwrite correct entries
       {
-          v = (pos.in_check() ? VALUE_NONE : evaluate(pos, m));
-          TT.store(k, VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply], v, m);
+          if (pos.in_check())
+              v = m = VALUE_NONE;
+          else
+              v = evaluate(pos, m);
+
+          TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply], v, m);
       }
-      pos.do_move(pv[ply], *st++);
 
-  } while (pv[++ply] != MOVE_NONE);
+      assert(pos.move_is_legal(pv[ply]));
+      pos.do_move(pv[ply++], *st++);
+
+  } while (pv[ply] != MOVE_NONE);
 
-  do pos.undo_move(pv[--ply]); while (ply);
+  while (ply) pos.undo_move(pv[--ply]);
 }