0

I have a vector of Moves, and a vector of scores. I would like to sort the vector of moves (based on the other vector), without creating an intermediate container. (and preferable without writing my own sort, there are already so many good). The reason I don't want an intermediate is performance reason, this portion is currently the primary bottleneck. Do someone know a neat fast hack that accomplish this? Note: making score part of Move wasn't a good idea, Move grew to much and I got loads of cache-misses that way. Also, most move-vectors wont need sorting.

struct Move { unsigned char f,t,p,m; };
std::vector<Move> mvlst;
std::vector<int> scores;

Sort mvlst based on scores

Update: I found this, It seams to work.

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • 1
    please give an example of vectors and desired output – 4pie0 Apr 02 '14 at 09:47
  • I do not see how creating a struct ``MoveWithScore { int score; Move m; }`` and sort it based on score would raise a performance issue... – Jean Logeart Apr 02 '14 at 09:51
  • This happens in an inner loop, and I would like to avoid as much copying as possible. The search-tree is usually 6-8 ply deep. (where this sort happens) – sp2danny Apr 02 '14 at 09:55
  • 2
    Does scores can be sorted too ? if so, you can use a `std::sort` with a `zip_iterator` (from [boost](http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/zip_iterator.html)). – Jarod42 Apr 02 '14 at 09:56
  • zip iterator! exactly the kind of thing I was looking for. Thanks! – sp2danny Apr 02 '14 at 09:59
  • @Jarod42 are you sure you can sort with a zip_iterator? http://stackoverflow.com/a/8584552/2436175 (See also the comment) – Antonio Apr 02 '14 at 10:51

0 Answers0