algo/sort: add explicit index sorting routine

This commit is contained in:
Danny Robson 2018-09-21 12:25:47 +10:00
parent 483c43c732
commit dee4a155e3

View File

@ -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 <typename IndexT, typename RandomT>
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 // sort an array specified by the iterators key_first:key_last using a
// comparator, and optionally a series of additional value iterators // comparator, and optionally a series of additional value iterators