sort: use a comparator object for index sort

This commit is contained in:
Danny Robson 2020-10-15 09:40:01 +10:00
parent 801ccf2ebd
commit f3302ffd5f

View File

@ -96,16 +96,27 @@ namespace cruft::sort {
/// RandomIterator `data`.
///
/// Does not modify any data element pointed to by `data`.
template <typename IndexT, typename RandomT>
template <
typename IndexT,
typename RandomT,
typename ComparatorT = std::less<>
>
void
indices (IndexT idx_first, IndexT idx_last, RandomT data)
{
indices (
IndexT idx_first,
IndexT idx_last,
RandomT data,
ComparatorT &&cmp
) {
// initialise a monotonic sequence of indices
std::iota (idx_first, idx_last, 0);
// sort using the indices
std::sort (idx_first, idx_last, [&] (auto a, auto b) {
return *(data + a) < *(data + b);
return cmp (
*(data + a),
*(data + b)
);
});
}