11#ifndef PBAT_COMMON_COUNTINGSORT_H
12#define PBAT_COMMON_COUNTINGSORT_H
41 std::random_access_iterator TWorkBegin,
42 std::random_access_iterator TWorkEnd,
43 std::random_access_iterator TValuesBegin,
44 std::random_access_iterator TValuesEnd,
46 class T =
typename std::iterator_traits<TValuesBegin>::value_type,
47 class TKey =
typename std::invoke_result_t<FKey, T>>
53 TKey keyMin = std::numeric_limits<TKey>::max(),
54 FKey fKey = [](T
const& key) {
return key; })
56 using IndexType = std::iterator_traits<TWorkBegin>::value_type;
58 std::is_integral_v<IndexType> and not std::is_same_v<IndexType, bool>,
59 "Work index must be range over integers");
60 using KeyType = std::invoke_result_t<FKey, T>;
61 static_assert(std::is_integral_v<KeyType>,
"Key type must be integral");
62 auto const n = std::distance(vb, ve);
66 if (keyMin == std::numeric_limits<KeyType>::max())
67 for (
auto it = vb; it != ve; ++it)
68 keyMin = std::min(keyMin, fKey(*it));
70 for (
auto it = vb; it != ve; ++it)
72 KeyType key = fKey(*it);
73 KeyType j = key - keyMin;
77 std::exclusive_scan(wb, we, wb, IndexType(0));
81 for (
auto i = n - 1; i >= 0; --i)
84 KeyType key = fKey(val) - keyMin;
85 IndexType j = *(wb + key);
91 std::swap(val, *(vb + j));
92 key = fKey(val) - keyMin;
113 std::random_access_iterator TValuesBegin,
114 std::random_access_iterator TValuesEnd,
115 std::random_access_iterator TWorkBegin,
120 std::invoke_result_t<FKey, typename std::iterator_traits<TValuesBegin>::value_type>;
121 static_assert(std::is_integral_v<KeyType>,
"Key type must be integral");
122 using IndexType = std::iterator_traits<TWorkBegin>::value_type;
124 std::is_integral_v<IndexType> and not std::is_same_v<IndexType, bool>,
125 "Work index must be range over integers");
128 KeyType key = fKey(*vb);
131 for (
auto it = vb; it != ve; ++it)
133 auto keyNext = fKey(*it);
Common functionality.
Definition ArgSort.h:20
void CountingSort(TWorkBegin wb, TWorkEnd we, TValuesBegin vb, TValuesEnd ve, TKey keyMin=std::numeric_limits< TKey >::max(), FKey fKey=[](T const &key) { return key;})
Counting sort.
Definition CountingSort.h:48
void PrefixSumFromSortedKeys(TValuesBegin vb, TValuesEnd ve, TWorkBegin wb, FKey fKey)
Definition CountingSort.h:117