| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/error/error_macros.h" |
| 34 |
#include "core/typedefs.h" |
| 35 |
|
| 36 |
#define ERR_BAD_COMPARE(cond) \ |
| 37 |
if (unlikely(cond)) { \ |
| 38 |
ERR_PRINT("bad comparison function; sorting will be broken"); \ |
| 39 |
break; \ |
| 40 |
} |
| 41 |
|
| 42 |
#ifdef DEBUG_ENABLED |
| 43 |
#define SORT_ARRAY_VALIDATE_ENABLED true |
| 44 |
#else |
| 45 |
#define SORT_ARRAY_VALIDATE_ENABLED false |
| 46 |
#endif |
| 47 |
|
| 48 |
template <typename T, typename Comparator = Comparator<T>, bool Validate = SORT_ARRAY_VALIDATE_ENABLED> |
| 49 |
class SortArray { |
| 50 |
enum { |
| 51 |
INTROSORT_THRESHOLD = 16 |
| 52 |
}; |
| 53 |
|
| 54 |
public: |
| 55 |
Comparator compare; |
| 56 |
|
| 57 |
inline int64_t median_of_3_index(const T *p_ptr, int64_t a_index, int64_t b_index, int64_t c_index) const { |
| 58 |
const T &a = p_ptr[a_index]; |
| 59 |
const T &b = p_ptr[b_index]; |
| 60 |
const T &c = p_ptr[c_index]; |
| 61 |
if (compare(a, b)) { |
| 62 |
if (compare(b, c)) { |
| 63 |
return b_index; |
| 64 |
} else if (compare(a, c)) { |
| 65 |
return c_index; |
| 66 |
} else { |
| 67 |
return a_index; |
| 68 |
} |
| 69 |
} else if (compare(a, c)) { |
| 70 |
return a_index; |
| 71 |
} else if (compare(b, c)) { |
| 72 |
return c_index; |
| 73 |
} else { |
| 74 |
return b_index; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
inline int64_t bitlog(int64_t n) const { |
| 79 |
int64_t k; |
| 80 |
for (k = 0; n != 1; n >>= 1) { |
| 81 |
++k; |
| 82 |
} |
| 83 |
return k; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
inline void push_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_top_index, T p_value, T *p_array) const { |
| 89 |
int64_t parent = (p_hole_idx - 1) / 2; |
| 90 |
while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) { |
| 91 |
p_array[p_first + p_hole_idx] = p_array[p_first + parent]; |
| 92 |
p_hole_idx = parent; |
| 93 |
parent = (p_hole_idx - 1) / 2; |
| 94 |
} |
| 95 |
p_array[p_first + p_hole_idx] = p_value; |
| 96 |
} |
| 97 |
|
| 98 |
inline void pop_heap(int64_t p_first, int64_t p_last, int64_t p_result, T p_value, T *p_array) const { |
| 99 |
p_array[p_result] = p_array[p_first]; |
| 100 |
adjust_heap(p_first, 0, p_last - p_first, p_value, p_array); |
| 101 |
} |
| 102 |
inline void pop_heap(int64_t p_first, int64_t p_last, T *p_array) const { |
| 103 |
pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array); |
| 104 |
} |
| 105 |
|
| 106 |
inline void adjust_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_len, T p_value, T *p_array) const { |
| 107 |
int64_t top_index = p_hole_idx; |
| 108 |
int64_t second_child = 2 * p_hole_idx + 2; |
| 109 |
|
| 110 |
while (second_child < p_len) { |
| 111 |
if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)])) { |
| 112 |
second_child--; |
| 113 |
} |
| 114 |
|
| 115 |
p_array[p_first + p_hole_idx] = p_array[p_first + second_child]; |
| 116 |
p_hole_idx = second_child; |
| 117 |
second_child = 2 * (second_child + 1); |
| 118 |
} |
| 119 |
|
| 120 |
if (second_child == p_len) { |
| 121 |
p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)]; |
| 122 |
p_hole_idx = second_child - 1; |
| 123 |
} |
| 124 |
push_heap(p_first, p_hole_idx, top_index, p_value, p_array); |
| 125 |
} |
| 126 |
|
| 127 |
inline void sort_heap(int64_t p_first, int64_t p_last, T *p_array) const { |
| 128 |
while (p_last - p_first > 1) { |
| 129 |
pop_heap(p_first, p_last--, p_array); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
inline void make_heap(int64_t p_first, int64_t p_last, T *p_array) const { |
| 134 |
if (p_last - p_first < 2) { |
| 135 |
return; |
| 136 |
} |
| 137 |
int64_t len = p_last - p_first; |
| 138 |
int64_t parent = (len - 2) / 2; |
| 139 |
|
| 140 |
while (true) { |
| 141 |
adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array); |
| 142 |
if (parent == 0) { |
| 143 |
return; |
| 144 |
} |
| 145 |
parent--; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
inline void partial_sort(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const { |
| 150 |
make_heap(p_first, p_middle, p_array); |
| 151 |
for (int64_t i = p_middle; i < p_last; i++) { |
| 152 |
if (compare(p_array[i], p_array[p_first])) { |
| 153 |
pop_heap(p_first, p_middle, i, p_array[i], p_array); |
| 154 |
} |
| 155 |
} |
| 156 |
sort_heap(p_first, p_middle, p_array); |
| 157 |
} |
| 158 |
|
| 159 |
inline void partial_select(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const { |
| 160 |
make_heap(p_first, p_middle, p_array); |
| 161 |
for (int64_t i = p_middle; i < p_last; i++) { |
| 162 |
if (compare(p_array[i], p_array[p_first])) { |
| 163 |
pop_heap(p_first, p_middle, i, p_array[i], p_array); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
inline int64_t partitioner(int64_t p_first, int64_t p_last, int64_t p_pivot, T *p_array) const { |
| 169 |
const int64_t unmodified_first = p_first; |
| 170 |
const int64_t unmodified_last = p_last; |
| 171 |
const T *pivot_element_location = &p_array[p_pivot]; |
| 172 |
|
| 173 |
while (true) { |
| 174 |
while (p_first != p_pivot && compare(p_array[p_first], *pivot_element_location)) { |
| 175 |
if constexpr (Validate) { |
| 176 |
ERR_BAD_COMPARE(p_first == unmodified_last - 1); |
| 177 |
} |
| 178 |
p_first++; |
| 179 |
} |
| 180 |
p_last--; |
| 181 |
while (p_last != p_pivot && compare(*pivot_element_location, p_array[p_last])) { |
| 182 |
if constexpr (Validate) { |
| 183 |
ERR_BAD_COMPARE(p_last == unmodified_first); |
| 184 |
} |
| 185 |
p_last--; |
| 186 |
} |
| 187 |
|
| 188 |
if (p_first >= p_last) { |
| 189 |
return p_first; |
| 190 |
} |
| 191 |
|
| 192 |
if (pivot_element_location == &p_array[p_first]) { |
| 193 |
pivot_element_location = &p_array[p_last]; |
| 194 |
} else if (pivot_element_location == &p_array[p_last]) { |
| 195 |
pivot_element_location = &p_array[p_first]; |
| 196 |
} |
| 197 |
SWAP(p_array[p_first], p_array[p_last]); |
| 198 |
p_first++; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
inline void introsort(int64_t p_first, int64_t p_last, T *p_array, int64_t p_max_depth) const { |
| 203 |
while (p_last - p_first > INTROSORT_THRESHOLD) { |
| 204 |
if (p_max_depth == 0) { |
| 205 |
partial_sort(p_first, p_last, p_last, p_array); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
p_max_depth--; |
| 210 |
|
| 211 |
int64_t cut = partitioner( |
| 212 |
p_first, |
| 213 |
p_last, |
| 214 |
median_of_3_index(p_array, p_first, p_first + (p_last - p_first) / 2, p_last - 1), |
| 215 |
p_array); |
| 216 |
|
| 217 |
introsort(cut, p_last, p_array, p_max_depth); |
| 218 |
p_last = cut; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
inline void introselect(int64_t p_first, int64_t p_nth, int64_t p_last, T *p_array, int64_t p_max_depth) const { |
| 223 |
while (p_last - p_first > 3) { |
| 224 |
if (p_max_depth == 0) { |
| 225 |
partial_select(p_first, p_nth + 1, p_last, p_array); |
| 226 |
SWAP(p_first, p_nth); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
p_max_depth--; |
| 231 |
|
| 232 |
int64_t cut = partitioner( |
| 233 |
p_first, |
| 234 |
p_last, |
| 235 |
median_of_3_index(p_array, p_first, p_first + (p_last - p_first) / 2, p_last - 1), |
| 236 |
p_array); |
| 237 |
|
| 238 |
if (cut <= p_nth) { |
| 239 |
p_first = cut; |
| 240 |
} else { |
| 241 |
p_last = cut; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
insertion_sort(p_first, p_last, p_array); |
| 246 |
} |
| 247 |
|
| 248 |
inline void unguarded_linear_insert(int64_t p_last, T p_value, T *p_array) const { |
| 249 |
int64_t next = p_last - 1; |
| 250 |
while (compare(p_value, p_array[next])) { |
| 251 |
if constexpr (Validate) { |
| 252 |
ERR_BAD_COMPARE(next == 0); |
| 253 |
} |
| 254 |
p_array[p_last] = p_array[next]; |
| 255 |
p_last = next; |
| 256 |
next--; |
| 257 |
} |
| 258 |
p_array[p_last] = p_value; |
| 259 |
} |
| 260 |
|
| 261 |
inline void linear_insert(int64_t p_first, int64_t p_last, T *p_array) const { |
| 262 |
T val = p_array[p_last]; |
| 263 |
if (compare(val, p_array[p_first])) { |
| 264 |
for (int64_t i = p_last; i > p_first; i--) { |
| 265 |
p_array[i] = p_array[i - 1]; |
| 266 |
} |
| 267 |
|
| 268 |
p_array[p_first] = val; |
| 269 |
} else { |
| 270 |
unguarded_linear_insert(p_last, val, p_array); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
inline void insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const { |
| 275 |
if (p_first == p_last) { |
| 276 |
return; |
| 277 |
} |
| 278 |
for (int64_t i = p_first + 1; i != p_last; i++) { |
| 279 |
linear_insert(p_first, i, p_array); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
inline void unguarded_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const { |
| 284 |
for (int64_t i = p_first; i != p_last; i++) { |
| 285 |
unguarded_linear_insert(i, p_array[i], p_array); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
inline void final_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const { |
| 290 |
if (p_last - p_first > INTROSORT_THRESHOLD) { |
| 291 |
insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array); |
| 292 |
unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array); |
| 293 |
} else { |
| 294 |
insertion_sort(p_first, p_last, p_array); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
inline void sort_range(int64_t p_first, int64_t p_last, T *p_array) const { |
| 299 |
if (p_first != p_last) { |
| 300 |
introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2); |
| 301 |
final_insertion_sort(p_first, p_last, p_array); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
inline void sort(T *p_array, int64_t p_len) const { |
| 306 |
sort_range(0, p_len, p_array); |
| 307 |
} |
| 308 |
|
| 309 |
inline void nth_element(int64_t p_first, int64_t p_last, int64_t p_nth, T *p_array) const { |
| 310 |
if (p_first == p_last || p_nth == p_last) { |
| 311 |
return; |
| 312 |
} |
| 313 |
introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2); |
| 314 |
} |
| 315 |
}; |