diff --git a/algo/sort.hpp b/algo/sort.hpp index 3a170a3d..1c2d67cc 100644 --- a/algo/sort.hpp +++ b/algo/sort.hpp @@ -89,6 +89,27 @@ namespace cruft::sort { } } + + /////////////////////////////////////////////////////////////////////////// + /// Fill a supplied index buffer with a sorted range of indices over an + /// identically size collection of data elements addressed by the + /// RandomIterator `data`. + /// + /// Does not modify any data element pointed to by `data`. + template + void + indices (IndexT idx_first, IndexT idx_last, RandomT data) + { + // 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); + }); + } + + /////////////////////////////////////////////////////////////////////////// // sort an array specified by the iterators key_first:key_last using a // comparator, and optionally a series of additional value iterators