mirror of
https://github.com/sockspls/badfish
synced 2025-07-13 20:49:15 +00:00
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.
This commit is contained in:
parent
0be7b8c542
commit
733d0099b2
5 changed files with 20 additions and 24 deletions
|
@ -35,6 +35,20 @@ namespace {
|
||||||
STOP
|
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
|
// 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.
|
// 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; }
|
inline bool has_positive_score(const MoveStack& ms) { return ms.score > 0; }
|
||||||
|
@ -230,14 +244,14 @@ void MovePicker::generate_next() {
|
||||||
endQuiets = end = generate<QUIETS>(pos, moves);
|
endQuiets = end = generate<QUIETS>(pos, moves);
|
||||||
score<QUIETS>();
|
score<QUIETS>();
|
||||||
end = std::partition(cur, end, has_positive_score);
|
end = std::partition(cur, end, has_positive_score);
|
||||||
sort<MoveStack>(cur, end);
|
insertion_sort(cur, end);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case QUIETS_2_S1:
|
case QUIETS_2_S1:
|
||||||
cur = end;
|
cur = end;
|
||||||
end = endQuiets;
|
end = endQuiets;
|
||||||
if (depth >= 3 * ONE_PLY)
|
if (depth >= 3 * ONE_PLY)
|
||||||
sort<MoveStack>(cur, end);
|
insertion_sort(cur, end);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case BAD_CAPTURES_S1:
|
case BAD_CAPTURES_S1:
|
||||||
|
|
|
@ -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
|
// Update piece list, move the last piece at index[capsq] position and
|
||||||
// shrink the list.
|
// 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
|
// 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
|
// not in its original place, it means index[] and pieceList[] are not
|
||||||
// guaranteed to be invariant to a do_move() + undo_move() sequence.
|
// guaranteed to be invariant to a do_move() + undo_move() sequence.
|
||||||
|
|
|
@ -354,7 +354,7 @@ namespace {
|
||||||
// we want to keep the same order for all the moves but the new
|
// 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
|
// PV that goes to the front. Note that in case of MultiPV search
|
||||||
// the already searched PV lines are preserved.
|
// the already searched PV lines are preserved.
|
||||||
sort<RootMove>(RootMoves.begin() + PVIdx, RootMoves.end());
|
std::stable_sort(RootMoves.begin() + PVIdx, RootMoves.end());
|
||||||
|
|
||||||
// Write PV back to transposition table in case the relevant
|
// Write PV back to transposition table in case the relevant
|
||||||
// entries have been overwritten during the search.
|
// entries have been overwritten during the search.
|
||||||
|
@ -399,7 +399,7 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the PV lines searched so far and update the GUI
|
// Sort the PV lines searched so far and update the GUI
|
||||||
sort<RootMove>(RootMoves.begin(), RootMoves.begin() + PVIdx + 1);
|
std::stable_sort(RootMoves.begin(), RootMoves.begin() + PVIdx + 1);
|
||||||
|
|
||||||
if (PVIdx + 1 == PVSize || Time::now() - SearchTime > 3000)
|
if (PVIdx + 1 == PVSize || Time::now() - SearchTime > 3000)
|
||||||
sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
|
sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
|
||||||
|
|
|
@ -56,12 +56,11 @@ struct Stack {
|
||||||
/// all non-pv moves.
|
/// all non-pv moves.
|
||||||
struct RootMove {
|
struct RootMove {
|
||||||
|
|
||||||
RootMove(){} // Needed by sort()
|
|
||||||
RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) {
|
RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) {
|
||||||
pv.push_back(m); pv.push_back(MOVE_NONE);
|
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; }
|
bool operator==(const Move& m) const { return pv[0] == m; }
|
||||||
|
|
||||||
void extract_pv_from_tt(Position& pos);
|
void extract_pv_from_tt(Position& pos);
|
||||||
|
|
17
src/types.h
17
src/types.h
|
@ -489,21 +489,4 @@ inline const std::string square_to_string(Square s) {
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Our insertion sort implementation, works with pointers and iterators and is
|
|
||||||
/// guaranteed to be stable, as is needed.
|
|
||||||
template<typename T, typename K>
|
|
||||||
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)
|
#endif // !defined(TYPES_H_INCLUDED)
|
||||||
|
|
Loading…
Add table
Reference in a new issue