From: Marco Costalba Date: Sun, 10 Feb 2013 18:10:01 +0000 (+0100) Subject: Rename and de-templetize sort() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=733d0099b2a3e3ad594bb551d37c8a06c62f13db Rename and de-templetize sort() Rename to insertion_sort so to avoid confusion with std::sort, also move it to movepicker.cpp and use the bit slower std::stable_sort in search.cpp where it is used in not performance critical paths. No functional change. --- diff --git a/src/movepick.cpp b/src/movepick.cpp index 72601bb3..5c8338c0 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -35,6 +35,20 @@ namespace { STOP }; + // Our insertion sort, guaranteed to be stable, as is needed + void insertion_sort(MoveStack* begin, MoveStack* end) + { + MoveStack tmp, *p, *q; + + for (p = begin + 1; p < end; ++p) + { + tmp = *p; + for (q = p; q != begin && *(q-1) < tmp; --q) + *q = *(q-1); + *q = tmp; + } + } + // Unary predicate used by std::partition to split positive scores from remaining // ones so to sort separately the two sets, and with the second sort delayed. inline bool has_positive_score(const MoveStack& ms) { return ms.score > 0; } @@ -230,14 +244,14 @@ void MovePicker::generate_next() { endQuiets = end = generate(pos, moves); score(); end = std::partition(cur, end, has_positive_score); - sort(cur, end); + insertion_sort(cur, end); return; case QUIETS_2_S1: cur = end; end = endQuiets; if (depth >= 3 * ONE_PLY) - sort(cur, end); + insertion_sort(cur, end); return; case BAD_CAPTURES_S1: diff --git a/src/position.cpp b/src/position.cpp index 310e46e3..ad0a621d 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -802,7 +802,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI // Update piece list, move the last piece at index[capsq] position and // shrink the list. // - // WARNING: This is a not revresible operation. When we will reinsert the + // WARNING: This is a not reversible operation. When we will reinsert the // captured piece in undo_move() we will put it at the end of the list and // not in its original place, it means index[] and pieceList[] are not // guaranteed to be invariant to a do_move() + undo_move() sequence. diff --git a/src/search.cpp b/src/search.cpp index 091c00fe..74dc9e29 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -354,7 +354,7 @@ namespace { // we want to keep the same order for all the moves but the new // PV that goes to the front. Note that in case of MultiPV search // the already searched PV lines are preserved. - sort(RootMoves.begin() + PVIdx, RootMoves.end()); + std::stable_sort(RootMoves.begin() + PVIdx, RootMoves.end()); // Write PV back to transposition table in case the relevant // entries have been overwritten during the search. @@ -399,7 +399,7 @@ namespace { } // Sort the PV lines searched so far and update the GUI - sort(RootMoves.begin(), RootMoves.begin() + PVIdx + 1); + std::stable_sort(RootMoves.begin(), RootMoves.begin() + PVIdx + 1); if (PVIdx + 1 == PVSize || Time::now() - SearchTime > 3000) sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl; diff --git a/src/search.h b/src/search.h index 600d7a75..4a66b11f 100644 --- a/src/search.h +++ b/src/search.h @@ -56,12 +56,11 @@ struct Stack { /// all non-pv moves. struct RootMove { - RootMove(){} // Needed by sort() RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) { pv.push_back(m); pv.push_back(MOVE_NONE); } - bool operator<(const RootMove& m) const { return score < m.score; } + bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort bool operator==(const Move& m) const { return pv[0] == m; } void extract_pv_from_tt(Position& pos); diff --git a/src/types.h b/src/types.h index b7bcb4e8..aa1f34fc 100644 --- a/src/types.h +++ b/src/types.h @@ -489,21 +489,4 @@ inline const std::string square_to_string(Square s) { return ch; } -/// Our insertion sort implementation, works with pointers and iterators and is -/// guaranteed to be stable, as is needed. -template -void sort(K begin, K end) -{ - T tmp; - K p, q; - - for (p = begin + 1; p < end; p++) - { - tmp = *p; - for (q = p; q != begin && *(q-1) < tmp; --q) - *q = *(q-1); - *q = tmp; - } -} - #endif // !defined(TYPES_H_INCLUDED)