49#include <lal/detail/data_array.hpp>
50#include <lal/detail/type_traits/is_pointer_iterator.hpp>
51#include <lal/detail/sorting/sorting_types.hpp>
63namespace countingsort {
97 memory(const std::
size_t largest_key_plus_1, const std::
size_t max_size_container)
99 :
count(largest_key_plus_1, 0),
100 output(max_size_container)
144 bool memory_has_frequencies,
145 typename value_iterator_t,
147 is_pointer_iterator_v<value_t, value_iterator_t> &&
149 std::is_same_v<sort_type, non_decreasing_t> ||
150 std::is_same_v<sort_type, non_increasing_t>
156 const value_iterator_t begin,
const value_iterator_t end,
157 const std::size_t largest_key_plus_1,
158 const std::function<std::size_t (
const value_t&)>& key,
163 constexpr bool is_increasing =
164 std::is_same_v<sort_type, non_decreasing_t>;
166 if (begin == end) {
return; }
168 if constexpr (not memory_has_frequencies) {
170 for (
auto it = begin; it != end; ) {
173 const std::size_t elem_key = key(*(it++));
175 ++mem.count[elem_key];
179 std::size_t total = 0;
180 for (std::size_t k = 0; k < largest_key_plus_1; ++k) {
181 std::tie(mem.count[k], total)
182 = std::make_pair(total, mem.count[k] + total);
185 std::size_t actual_container_size = 0;
186 for (
auto it = begin; it != end; ) {
187 ++actual_container_size;
191 const std::size_t elem_key = key(*it);
193 mem.output[mem.count[elem_key]] = std::move(*(it++));
194 ++mem.count[elem_key];
198 if constexpr (is_increasing) {
200 for (std::size_t k = 0; k < actual_container_size;) {
201 *(it++) = std::move(mem.output[k++]);
206 for (std::size_t k = actual_container_size - 1; k > 0;) {
207 *(it++) = std::move(mem.output[k--]);
209 *it = std::move(mem.output[0]);
244 typename value_iterator_t,
246 is_pointer_iterator_v<value_t, value_iterator_t> &&
248 std::is_same_v<sort_type, non_decreasing_t> ||
249 std::is_same_v<sort_type, non_increasing_t>
255 const value_iterator_t begin,
const value_iterator_t end,
256 const std::size_t largest_key,
257 const std::size_t upper_bound_size,
258 const std::function<std::size_t (
const value_t&)>& key
263 if (begin == end) {
return; }
267 counting_sort<value_t, sort_type, false, value_iterator_t>
268 (begin, end, largest_key+1, key, mem);
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const std::function< std::size_t(const value_t &)> &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition: counting_sort.hpp:155
Main namespace of the library.
Definition: basic_types.hpp:50
Wrapper of a C array for autmatic deallocation of memory.
Definition: data_array.hpp:59
void fill(const T &v) noexcept
Assign the same value to every element in the data.
Definition: data_array.hpp:263
Memory used for the counting sort algorithm.
Definition: counting_sort.hpp:72
void reset_count() noexcept
Set the count member to 0.
Definition: counting_sort.hpp:107
~memory() noexcept=default
Destructor.
data_array< T > output
The output array.
Definition: counting_sort.hpp:76
memory() noexcept=default
Constructor.
data_array< std::size_t > count
Amount of times the key of an element occurs.
Definition: counting_sort.hpp:74