49#include <lal/detail/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)
146 bool memory_has_frequencies,
148 typename value_iterator_t,
150 std::enable_if_t< is_pointer_iterator_v<value_t, value_iterator_t>,
bool > =
true
153 const value_iterator_t begin,
const value_iterator_t end,
154 const std::size_t largest_key_plus_1,
162 std::is_constructible_v<
163 std::function<std::size_t (
const value_t&)>,
168 if (begin == end) {
return; }
170 if constexpr (not memory_has_frequencies) {
172 for (
auto it = begin; it != end; ) {
175 const std::size_t elem_key = key(*(it++));
177 ++mem.count[elem_key];
181 std::size_t total = 0;
182 for (std::size_t k = 0; k < largest_key_plus_1; ++k) {
183 std::tie(mem.count[k], total)
184 = std::make_pair(total, mem.count[k] + total);
187 std::size_t actual_container_size = 0;
188 for (
auto it = begin; it != end; ) {
189 ++actual_container_size;
193 const std::size_t elem_key = key(*it);
195 mem.output[mem.count[elem_key]] = std::move(*(it++));
196 ++mem.count[elem_key];
202 for (std::size_t k = 0; k < actual_container_size;) {
203 *(it++) = std::move(mem.output[k++]);
208 for (std::size_t k = actual_container_size - 1; k > 0;) {
209 *(it++) = std::move(mem.output[k--]);
211 *it = std::move(mem.output[0]);
247 typename value_iterator_t,
250 std::enable_if_t< is_pointer_iterator_v<value_t, value_iterator_t>,
bool > =
true
253 const value_iterator_t begin,
const value_iterator_t end,
254 const std::size_t largest_key,
255 const std::size_t upper_bound_size,
262 std::is_constructible_v<
263 std::function<std::size_t (
const value_t&)>,
269 if (begin == end) {
return; }
274 (begin, end, largest_key+1, key, mem);
sort_type
The different types of sorting patterns.
Definition sorting_types.hpp:49
@ non_decreasing
Non-decreasing sort type.
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const Callable &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition counting_sort.hpp:152
Main namespace of the library.
Definition basic_types.hpp:48
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
void fill(const T &v) noexcept
Assign the same value to every element in the data.
Definition array.hpp:272
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
array< std::size_t > count
Amount of times the key of an element occurs.
Definition counting_sort.hpp:74
~memory() noexcept=default
Destructor.
array< T > output
The output array.
Definition counting_sort.hpp:76
memory() noexcept=default
Constructor.