49#include <lal/internal/data_array.hpp>
50#include <lal/internal/macros.hpp>
55namespace countingsort {
58struct increasing_t { };
60struct decreasing_t { };
63struct memory_counting_sort {
64 data_array<std::size_t> count;
68 const std::size_t largest_key_plus_1,
69 const std::size_t max_size_container
71 count(largest_key_plus_1, 0), output(max_size_container)
74 ~memory_counting_sort() { }
76 void reset_count() { count.fill(0); }
105 typename T,
typename It,
107 bool memory_has_frequencies,
109 is_pointer_iterator_v<T, It> &&
111 std::is_same_v<sort_type, countingsort::increasing_t> ||
112 std::is_same_v<sort_type, countingsort::decreasing_t>
119 const std::size_t largest_key_plus_1,
120 const std::function<std::size_t (
const T&)>& key,
121 countingsort::memory_counting_sort<T>& mem
124 constexpr bool is_increasing =
125 std::is_same_v<sort_type, countingsort::increasing_t>;
128 if (begin == end) {
return; }
130 if constexpr (not memory_has_frequencies) {
132 for (
auto it = begin; it != end; ) {
133 const std::size_t elem_key = key(*(it++));
134 ++mem.count[elem_key];
138 std::size_t total = 0;
139 for (std::size_t k = 0; k < largest_key_plus_1; ++k) {
140 std::tie(mem.count[k], total)
141 = std::make_pair(total, mem.count[k] + total);
144 std::size_t size_container = 0;
145 for (
auto it = begin; it != end; ) {
148 const std::size_t elem_key = key(*it);
149 mem.output[mem.count[elem_key]] = std::move(*(it++));
150 mem.count[elem_key] += 1;
154 if constexpr (is_increasing) {
156 for (std::size_t k = 0; k < size_container;) {
157 *(it++) = std::move(mem.output[k++]);
162 for (std::size_t k = size_container - 1; k > 0;) {
163 *(it++) = std::move(mem.output[k--]);
165 *it = std::move(mem.output[0]);
193 typename T,
typename It,
196 is_pointer_iterator_v<T, It>&&
198 std::is_same_v<sort_type, countingsort::increasing_t> ||
199 std::is_same_v<sort_type, countingsort::decreasing_t>
206 const std::size_t largest_key,
207 const std::size_t upper_bound_size,
208 const std::function<std::size_t (
const T&)>& key
212 if (begin == end) {
return; }
214 countingsort::memory_counting_sort<T> mem(largest_key+1, upper_bound_size);
216 counting_sort<T, It, sort_type, false>
217 (begin, end, largest_key+1, key, mem);
Main namespace of the library.
Definition definitions.hpp:48