2
0

sort_array.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**************************************************************************/
  2. /* sort_array.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/typedefs.h"
  33. #define ERR_BAD_COMPARE(cond) \
  34. if (unlikely(cond)) { \
  35. ERR_PRINT("bad comparison function; sorting will be broken"); \
  36. break; \
  37. }
  38. #ifdef DEBUG_ENABLED
  39. #define SORT_ARRAY_VALIDATE_ENABLED true
  40. #else
  41. #define SORT_ARRAY_VALIDATE_ENABLED false
  42. #endif
  43. template <typename T, typename Comparator = Comparator<T>, bool Validate = SORT_ARRAY_VALIDATE_ENABLED>
  44. class SortArray {
  45. enum {
  46. INTROSORT_THRESHOLD = 16
  47. };
  48. public:
  49. Comparator compare;
  50. inline int64_t median_of_3_index(const T *p_ptr, int64_t a_index, int64_t b_index, int64_t c_index) const {
  51. const T &a = p_ptr[a_index];
  52. const T &b = p_ptr[b_index];
  53. const T &c = p_ptr[c_index];
  54. if (compare(a, b)) {
  55. if (compare(b, c)) {
  56. return b_index;
  57. } else if (compare(a, c)) {
  58. return c_index;
  59. } else {
  60. return a_index;
  61. }
  62. } else if (compare(a, c)) {
  63. return a_index;
  64. } else if (compare(b, c)) {
  65. return c_index;
  66. } else {
  67. return b_index;
  68. }
  69. }
  70. inline int64_t bitlog(int64_t n) const {
  71. int64_t k;
  72. for (k = 0; n != 1; n >>= 1) {
  73. ++k;
  74. }
  75. return k;
  76. }
  77. /* Heap / Heapsort functions */
  78. 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 {
  79. int64_t parent = (p_hole_idx - 1) / 2;
  80. while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
  81. p_array[p_first + p_hole_idx] = p_array[p_first + parent];
  82. p_hole_idx = parent;
  83. parent = (p_hole_idx - 1) / 2;
  84. }
  85. p_array[p_first + p_hole_idx] = p_value;
  86. }
  87. inline void pop_heap(int64_t p_first, int64_t p_last, int64_t p_result, T p_value, T *p_array) const {
  88. p_array[p_result] = p_array[p_first];
  89. adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
  90. }
  91. inline void pop_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  92. pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
  93. }
  94. inline void adjust_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_len, T p_value, T *p_array) const {
  95. int64_t top_index = p_hole_idx;
  96. int64_t second_child = 2 * p_hole_idx + 2;
  97. while (second_child < p_len) {
  98. if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)])) {
  99. second_child--;
  100. }
  101. p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
  102. p_hole_idx = second_child;
  103. second_child = 2 * (second_child + 1);
  104. }
  105. if (second_child == p_len) {
  106. p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
  107. p_hole_idx = second_child - 1;
  108. }
  109. push_heap(p_first, p_hole_idx, top_index, p_value, p_array);
  110. }
  111. inline void sort_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  112. while (p_last - p_first > 1) {
  113. pop_heap(p_first, p_last--, p_array);
  114. }
  115. }
  116. inline void make_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  117. if (p_last - p_first < 2) {
  118. return;
  119. }
  120. int64_t len = p_last - p_first;
  121. int64_t parent = (len - 2) / 2;
  122. while (true) {
  123. adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
  124. if (parent == 0) {
  125. return;
  126. }
  127. parent--;
  128. }
  129. }
  130. inline void partial_sort(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
  131. make_heap(p_first, p_middle, p_array);
  132. for (int64_t i = p_middle; i < p_last; i++) {
  133. if (compare(p_array[i], p_array[p_first])) {
  134. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  135. }
  136. }
  137. sort_heap(p_first, p_middle, p_array);
  138. }
  139. inline void partial_select(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
  140. make_heap(p_first, p_middle, p_array);
  141. for (int64_t i = p_middle; i < p_last; i++) {
  142. if (compare(p_array[i], p_array[p_first])) {
  143. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  144. }
  145. }
  146. }
  147. inline int64_t partitioner(int64_t p_first, int64_t p_last, int64_t p_pivot, T *p_array) const {
  148. const int64_t unmodified_first = p_first;
  149. const int64_t unmodified_last = p_last;
  150. const T *pivot_element_location = &p_array[p_pivot];
  151. while (true) {
  152. while (p_first != p_pivot && compare(p_array[p_first], *pivot_element_location)) {
  153. if constexpr (Validate) {
  154. ERR_BAD_COMPARE(p_first == unmodified_last - 1);
  155. }
  156. p_first++;
  157. }
  158. p_last--;
  159. while (p_last != p_pivot && compare(*pivot_element_location, p_array[p_last])) {
  160. if constexpr (Validate) {
  161. ERR_BAD_COMPARE(p_last == unmodified_first);
  162. }
  163. p_last--;
  164. }
  165. if (p_first >= p_last) {
  166. return p_first;
  167. }
  168. if (pivot_element_location == &p_array[p_first]) {
  169. pivot_element_location = &p_array[p_last];
  170. } else if (pivot_element_location == &p_array[p_last]) {
  171. pivot_element_location = &p_array[p_first];
  172. }
  173. SWAP(p_array[p_first], p_array[p_last]);
  174. p_first++;
  175. }
  176. }
  177. inline void introsort(int64_t p_first, int64_t p_last, T *p_array, int64_t p_max_depth) const {
  178. while (p_last - p_first > INTROSORT_THRESHOLD) {
  179. if (p_max_depth == 0) {
  180. partial_sort(p_first, p_last, p_last, p_array);
  181. return;
  182. }
  183. p_max_depth--;
  184. int64_t cut = partitioner(
  185. p_first,
  186. p_last,
  187. median_of_3_index(p_array, p_first, p_first + (p_last - p_first) / 2, p_last - 1),
  188. p_array);
  189. introsort(cut, p_last, p_array, p_max_depth);
  190. p_last = cut;
  191. }
  192. }
  193. inline void introselect(int64_t p_first, int64_t p_nth, int64_t p_last, T *p_array, int64_t p_max_depth) const {
  194. while (p_last - p_first > 3) {
  195. if (p_max_depth == 0) {
  196. partial_select(p_first, p_nth + 1, p_last, p_array);
  197. SWAP(p_first, p_nth);
  198. return;
  199. }
  200. p_max_depth--;
  201. int64_t cut = partitioner(
  202. p_first,
  203. p_last,
  204. median_of_3_index(p_array, p_first, p_first + (p_last - p_first) / 2, p_last - 1),
  205. p_array);
  206. if (cut <= p_nth) {
  207. p_first = cut;
  208. } else {
  209. p_last = cut;
  210. }
  211. }
  212. insertion_sort(p_first, p_last, p_array);
  213. }
  214. inline void unguarded_linear_insert(int64_t p_last, T p_value, T *p_array) const {
  215. int64_t next = p_last - 1;
  216. while (compare(p_value, p_array[next])) {
  217. if constexpr (Validate) {
  218. ERR_BAD_COMPARE(next == 0);
  219. }
  220. p_array[p_last] = p_array[next];
  221. p_last = next;
  222. next--;
  223. }
  224. p_array[p_last] = p_value;
  225. }
  226. inline void linear_insert(int64_t p_first, int64_t p_last, T *p_array) const {
  227. T val = p_array[p_last];
  228. if (compare(val, p_array[p_first])) {
  229. for (int64_t i = p_last; i > p_first; i--) {
  230. p_array[i] = p_array[i - 1];
  231. }
  232. p_array[p_first] = val;
  233. } else {
  234. unguarded_linear_insert(p_last, val, p_array);
  235. }
  236. }
  237. inline void insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  238. if (p_first == p_last) {
  239. return;
  240. }
  241. for (int64_t i = p_first + 1; i != p_last; i++) {
  242. linear_insert(p_first, i, p_array);
  243. }
  244. }
  245. inline void unguarded_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  246. for (int64_t i = p_first; i != p_last; i++) {
  247. unguarded_linear_insert(i, p_array[i], p_array);
  248. }
  249. }
  250. inline void final_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  251. if (p_last - p_first > INTROSORT_THRESHOLD) {
  252. insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
  253. unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
  254. } else {
  255. insertion_sort(p_first, p_last, p_array);
  256. }
  257. }
  258. inline void sort_range(int64_t p_first, int64_t p_last, T *p_array) const {
  259. if (p_first != p_last) {
  260. introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
  261. final_insertion_sort(p_first, p_last, p_array);
  262. }
  263. }
  264. inline void sort(T *p_array, int64_t p_len) const {
  265. sort_range(0, p_len, p_array);
  266. }
  267. inline void nth_element(int64_t p_first, int64_t p_last, int64_t p_nth, T *p_array) const {
  268. if (p_first == p_last || p_nth == p_last) {
  269. return;
  270. }
  271. introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2);
  272. }
  273. };