BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Sort.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
25
26#pragma once
27
28#include <cstddef>
29#include <iterator>
30#include <span>
31#include <utility>
32
36namespace util::sort {
37
38namespace detail {
39
40 // elements in ranges this short are sorted by insertion sort at the end,
41 // instead of being partitioned further
42 inline constexpr auto INSERTION_THRESHOLD { 16z };
43
44 template <typename T, typename Compare>
45 constexpr void insertion_sort(const std::span<T> range, Compare& comp)
46 {
47 for (auto idx = 1uz; idx < range.size(); ++idx) {
48 auto value = std::move(range[idx]);
49 auto pos = idx;
50
51 for (; pos > 0uz and comp(value, range[pos - 1uz]); --pos)
52 range[pos] = std::move(range[pos - 1uz]);
53
54 range[pos] = std::move(value);
55 }
56 }
57
58 template <typename T, typename Compare>
59 constexpr void sift_down(
60 const std::span<T> range, std::size_t root, const std::size_t size, Compare& comp)
61 {
62 while (true) {
63 auto child = (root * 2uz) + 1uz;
64
65 if (child >= size)
66 return;
67
68 if (child + 1uz < size and comp(range[child], range[child + 1uz]))
69 ++child;
70
71 if (not comp(range[root], range[child]))
72 return;
73
74 std::swap(range[root], range[child]);
75 root = child;
76 }
77 }
78
79 // the fallback when partitioning goes badly, to keep the worst case at O(n log n)
80 template <typename T, typename Compare>
81 constexpr void heap_sort(const std::span<T> range, Compare& comp)
82 {
83 const auto size = range.size();
84
85 for (auto idx = size / 2uz; idx > 0uz; --idx)
86 sift_down(range, idx - 1uz, size, comp);
87
88 for (auto end = size; end > 1uz; --end) {
89 std::swap(range[0uz], range[end - 1uz]);
90 sift_down(range, 0uz, end - 1uz, comp);
91 }
92 }
93
94 // puts the median of the first, middle & last elements at the front, as the pivot
95 template <typename T, typename Compare>
96 constexpr void move_median_to_front(const std::span<T> range, Compare& comp)
97 {
98 auto& first = range[1uz];
99 auto& middle = range[range.size() / 2uz];
100 auto& last = range.back();
101
102 auto* median = &first;
103
104 if (comp(first, middle)) {
105 if (comp(middle, last))
106 median = &middle;
107 else if (comp(first, last))
108 median = &last;
109 } else if (comp(first, last)) {
110 // first is already the median
111 } else if (comp(middle, last)) {
112 median = &last;
113 } else {
114 median = &middle;
115 }
116
117 std::swap(range.front(), *median);
118 }
119
120 // partitions the range around its first element & returns the index where
121 // the second part starts. Elements equal to the pivot may go on either side.
122 template <typename T, typename Compare>
123 constexpr auto partition(const std::span<T> range, Compare& comp) -> std::size_t
124 {
125 const auto& pivot = range.front();
126
127 auto left = 1uz;
128 auto right = range.size();
129
130 while (true) {
131 while (comp(range[left], pivot))
132 ++left;
133
134 --right;
135
136 while (comp(pivot, range[right]))
137 --right;
138
139 if (left >= right)
140 return left;
141
142 std::swap(range[left], range[right]);
143 ++left;
144 }
145 }
146
147 template <typename T, typename Compare>
148 constexpr void introsort(std::span<T> range, int depthLimit, Compare& comp)
149 {
150 while (std::ssize(range) > INSERTION_THRESHOLD) {
151 if (depthLimit == 0) {
152 heap_sort(range, comp);
153 return;
154 }
155
156 --depthLimit;
157
158 move_median_to_front(range, comp);
159
160 const auto cut = partition(range, comp);
161
162 introsort(range.subspan(cut), depthLimit, comp);
163
164 range = range.first(cut);
165 }
166 }
167
168} // namespace detail
169
178template <typename T, typename Compare>
179constexpr void portable_sort(const std::span<T> range, Compare comp)
180{
181 if (range.size() < 2uz)
182 return;
183
184 auto depthLimit = 0;
185
186 for (auto size = range.size(); size > 1uz; size /= 2uz)
187 depthLimit += 2;
188
189 detail::introsort(range, depthLimit, comp);
190 detail::insertion_sort(range, comp);
191}
192
193} // namespace util::sort
constexpr void portable_sort(const std::span< T > range, Compare comp)
Definition Sort.hpp:179
constexpr void insertion_sort(const std::span< T > range, Compare &comp)
Definition Sort.hpp:45
constexpr void heap_sort(const std::span< T > range, Compare &comp)
Definition Sort.hpp:81
constexpr void sift_down(const std::span< T > range, std::size_t root, const std::size_t size, Compare &comp)
Definition Sort.hpp:59
constexpr void move_median_to_front(const std::span< T > range, Compare &comp)
Definition Sort.hpp:96
constexpr auto INSERTION_THRESHOLD
Definition Sort.hpp:42
constexpr auto partition(const std::span< T > range, Compare &comp) -> std::size_t
Definition Sort.hpp:123
constexpr void introsort(std::span< T > range, int depthLimit, Compare &comp)
Definition Sort.hpp:148