From: Marco Costalba Date: Fri, 26 Oct 2012 16:01:13 +0000 (+0200) Subject: Use std::stack instead of fixed size array X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=cd80762c13961eb8fe0a89c0bb19d58e2808a1cf Use std::stack instead of fixed size array 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. --- diff --git a/src/notation.cpp b/src/notation.cpp index dea72441..15661eec 100644 --- a/src/notation.cpp +++ b/src/notation.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #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 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) diff --git a/src/search.cpp b/src/search.cpp index f82197f8..bf227d1f 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -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() || 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() || 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]); }