From f3302ffd5f4da380e8da80a575549a33139ad2a5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 15 Oct 2020 09:40:01 +1000 Subject: [PATCH] sort: use a comparator object for index sort --- algo/sort.hpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/algo/sort.hpp b/algo/sort.hpp index bbfa990e..977aff73 100644 --- a/algo/sort.hpp +++ b/algo/sort.hpp @@ -96,16 +96,27 @@ namespace cruft::sort { /// RandomIterator `data`. /// /// Does not modify any data element pointed to by `data`. - template + 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) + ); }); }