EnTT: src/entt/core/algorithm.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
algorithm.hpp
1#ifndef ENTT_CORE_ALGORITHM_HPP
2#define ENTT_CORE_ALGORITHM_HPP
3
4#include <algorithm>
5#include <functional>
6#include <iterator>
7#include <utility>
8#include <vector>
9#include "utility.hpp"
10
11namespace entt {
12
21 struct std_sort {
35 template<typename It, typename Compare = std::less<>, typename... Args>
36 void operator()(It first, It last, Compare compare = Compare{}, Args &&...args) const {
37 std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
38 }
39};
40
42 struct insertion_sort {
54 template<typename It, typename Compare = std::less<>>
55 void operator()(It first, It last, Compare compare = Compare{}) const {
56 if(first < last) {
57 for(auto it = first + 1; it < last; ++it) {
58 auto value = std::move(*it);
59 auto pre = it;
60
61 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
62 for(; pre > first && compare(value, *(pre - 1)); --pre) {
63 *pre = std::move(*(pre - 1));
64 }
65 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
66
67 *pre = std::move(value);
68 }
69 }
70 }
71};
72
78template<std::size_t Bit, std::size_t N>
79 struct radix_sort {
80 static_assert((N % Bit) == 0, "The maximum number of bits to sort must be a multiple of the number of bits processed per pass");
81
97 template<typename It, typename Getter = identity>
98 void operator()(It first, It last, Getter getter = Getter{}) const {
99 if(first < last) {
100 constexpr auto passes = N / Bit;
101
102 using value_type = typename std::iterator_traits<It>::value_type;
103 using difference_type = typename std::iterator_traits<It>::difference_type;
104 std::vector<value_type> aux(static_cast<std::size_t>(std::distance(first, last)));
105
106 auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
107 constexpr auto mask = (1 << Bit) - 1;
108 constexpr auto buckets = 1 << Bit;
109
110 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays, misc-const-correctness)
111 std::size_t count[buckets]{};
112
113 for(auto it = from; it != to; ++it) {
114 ++count[(getter(*it) >> start) & mask];
115 }
116
117 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
118 std::size_t index[buckets]{};
119
120 for(std::size_t pos{}, end = buckets - 1u; pos < end; ++pos) {
121 index[pos + 1u] = index[pos] + count[pos];
122 }
123
124 for(auto it = from; it != to; ++it) {
125 const auto pos = index[(getter(*it) >> start) & mask]++;
126 out[static_cast<difference_type>(pos)] = std::move(*it);
127 }
128 };
129
130 for(std::size_t pass = 0; pass < (passes & ~1u); pass += 2) {
131 part(first, last, aux.begin(), pass * Bit);
132 part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
133 }
134
135 if constexpr(passes & 1) {
136 part(first, last, aux.begin(), (passes - 1) * Bit);
137 std::move(aux.begin(), aux.end(), first);
138 }
139 }
140 }
141};
142
143} // namespace entt
144
145#endif
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::insertion_sort
Function object for performing insertion sort.
Definition algorithm.hpp:42
entt::insertion_sort::operator()
void operator()(It first, It last, Compare compare=Compare{}) const
Sorts the elements in a range.
Definition algorithm.hpp:55
entt::radix_sort
Function object for performing LSD radix sort.
Definition algorithm.hpp:79
entt::radix_sort::operator()
void operator()(It first, It last, Getter getter=Getter{}) const
Sorts the elements in a range.
Definition algorithm.hpp:98
entt::std_sort
Function object to wrap std::sort in a class type.
Definition algorithm.hpp:21
entt::std_sort::operator()
void operator()(It first, It last, Compare compare=Compare{}, Args &&...args) const
Sorts the elements in a range.
Definition algorithm.hpp:36