mcostalba wrote:Rein Halbersma wrote: My bad: the return statement should yield the move at the front of course. Note that I also changed the return type to T rather than T*, so it now has the same signature as the current pick_best in Stockfish.
I still get different results, although not so different
That is because your current algorithm is not stable and mine is

Take e.g. a list of for move structs with the following labels and scores that are generated in this order
{ {a, 1}, {b, 2}, {c, 2}, {d, 3} }
Your current algorithm does a number of swaps and returns {d, 3}. After the algorithm the list looks like this:
{ {a, 1}, {a, 1}, {c, 2}, {b, 2} }
Using the std::max_element and std::swap you also get {d, 3}, but the list looks like this:
{ {d, 3}, {b, 2}, {c, 2}, {a, 1} }
So you will search moves with the same score such as b and c in a different order than you generate them, whereas with my suggestion the order is stable.
BTW after a very quick test I see the following is faster (but still slower than mine, although not posible to speed compare correctly until functionality is different).
Code: Select all
template<typename T>
inline T pick_best(T* curMove, T* lastMove)
{
T* bestMove = std::max_element(curMove, lastMove); // find the best move
std::swap(*curMove, *bestMove); // swap best move to the front
return *curMove; // return the front move;
}
Tested with g++ 4.4.0
Makes sense to eliminate the branch and always do the swap.