Created
February 12, 2021 01:08
-
-
Save ntessore/6f46e1a6c311669e12ffd448dbab717c to your computer and use it in GitHub Desktop.
C++ algorithm scrapbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// argsort(first, last, a_first, [compare]) | |
// --- | |
// Store the order of the input range [`first`, `last`) in the output range | |
// starting at `a_first`. Both iterator types must allow random access. | |
// | |
template<class ValueIt, class IndexIt, | |
class Op = std::less<typename std::iterator_traits<ValueIt>::value_type>> | |
void argsort(ValueIt first, ValueIt last, IndexIt a_first, Op compare = Op()) | |
{ | |
using T = typename std::iterator_traits<IndexIt>::value_type; | |
IndexIt a_last = std::next(a_first, std::distance(first, last)); | |
std::iota(a_first, a_last, 0); | |
std::stable_sort(a_first, a_last, [&](const T& i, const T& j) { | |
return compare(*std::next(first, i), *std::next(first, j)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment