sort_array.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 const T &median_of_3(const T &a, const T &b, const T &c) const {
  51. if (compare(a, b)) {
  52. if (compare(b, c)) {
  53. return b;
  54. } else if (compare(a, c)) {
  55. return c;
  56. } else {
  57. return a;
  58. }
  59. } else if (compare(a, c)) {
  60. return a;
  61. } else if (compare(b, c)) {
  62. return c;
  63. } else {
  64. return b;
  65. }
  66. }
  67. inline int64_t bitlog(int64_t n) const {
  68. int64_t k;
  69. for (k = 0; n != 1; n >>= 1) {
  70. ++k;
  71. }
  72. return k;
  73. }
  74. /* Heap / Heapsort functions */
  75. 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 {
  76. int64_t parent = (p_hole_idx - 1) / 2;
  77. while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
  78. p_array[p_first + p_hole_idx] = p_array[p_first + parent];
  79. p_hole_idx = parent;
  80. parent = (p_hole_idx - 1) / 2;
  81. }
  82. p_array[p_first + p_hole_idx] = p_value;
  83. }
  84. inline void pop_heap(int64_t p_first, int64_t p_last, int64_t p_result, T p_value, T *p_array) const {
  85. p_array[p_result] = p_array[p_first];
  86. adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
  87. }
  88. inline void pop_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  89. pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
  90. }
  91. inline void adjust_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_len, T p_value, T *p_array) const {
  92. int64_t top_index = p_hole_idx;
  93. int64_t second_child = 2 * p_hole_idx + 2;
  94. while (second_child < p_len) {
  95. if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)])) {
  96. second_child--;
  97. }
  98. p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
  99. p_hole_idx = second_child;
  100. second_child = 2 * (second_child + 1);
  101. }
  102. if (second_child == p_len) {
  103. p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
  104. p_hole_idx = second_child - 1;
  105. }
  106. push_heap(p_first, p_hole_idx, top_index, p_value, p_array);
  107. }
  108. inline void sort_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  109. while (p_last - p_first > 1) {
  110. pop_heap(p_first, p_last--, p_array);
  111. }
  112. }
  113. inline void make_heap(int64_t p_first, int64_t p_last, T *p_array) const {
  114. if (p_last - p_first < 2) {
  115. return;
  116. }
  117. int64_t len = p_last - p_first;
  118. int64_t parent = (len - 2) / 2;
  119. while (true) {
  120. adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
  121. if (parent == 0) {
  122. return;
  123. }
  124. parent--;
  125. }
  126. }
  127. inline void partial_sort(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
  128. make_heap(p_first, p_middle, p_array);
  129. for (int64_t i = p_middle; i < p_last; i++) {
  130. if (compare(p_array[i], p_array[p_first])) {
  131. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  132. }
  133. }
  134. sort_heap(p_first, p_middle, p_array);
  135. }
  136. inline void partial_select(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
  137. make_heap(p_first, p_middle, p_array);
  138. for (int64_t i = p_middle; i < p_last; i++) {
  139. if (compare(p_array[i], p_array[p_first])) {
  140. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  141. }
  142. }
  143. }
  144. inline int64_t partitioner(int64_t p_first, int64_t p_last, T p_pivot, T *p_array) const {
  145. const int64_t unmodified_first = p_first;
  146. const int64_t unmodified_last = p_last;
  147. while (true) {
  148. while (compare(p_array[p_first], p_pivot)) {
  149. if constexpr (Validate) {
  150. ERR_BAD_COMPARE(p_first == unmodified_last - 1);
  151. }
  152. p_first++;
  153. }
  154. p_last--;
  155. while (compare(p_pivot, p_array[p_last])) {
  156. if constexpr (Validate) {
  157. ERR_BAD_COMPARE(p_last == unmodified_first);
  158. }
  159. p_last--;
  160. }
  161. if (!(p_first < p_last)) {
  162. return p_first;
  163. }
  164. SWAP(p_array[p_first], p_array[p_last]);
  165. p_first++;
  166. }
  167. }
  168. inline void introsort(int64_t p_first, int64_t p_last, T *p_array, int64_t p_max_depth) const {
  169. while (p_last - p_first > INTROSORT_THRESHOLD) {
  170. if (p_max_depth == 0) {
  171. partial_sort(p_first, p_last, p_last, p_array);
  172. return;
  173. }
  174. p_max_depth--;
  175. int64_t cut = partitioner(
  176. p_first,
  177. p_last,
  178. median_of_3(
  179. p_array[p_first],
  180. p_array[p_first + (p_last - p_first) / 2],
  181. p_array[p_last - 1]),
  182. p_array);
  183. introsort(cut, p_last, p_array, p_max_depth);
  184. p_last = cut;
  185. }
  186. }
  187. inline void introselect(int64_t p_first, int64_t p_nth, int64_t p_last, T *p_array, int64_t p_max_depth) const {
  188. while (p_last - p_first > 3) {
  189. if (p_max_depth == 0) {
  190. partial_select(p_first, p_nth + 1, p_last, p_array);
  191. SWAP(p_first, p_nth);
  192. return;
  193. }
  194. p_max_depth--;
  195. int64_t cut = partitioner(
  196. p_first,
  197. p_last,
  198. median_of_3(
  199. p_array[p_first],
  200. p_array[p_first + (p_last - p_first) / 2],
  201. p_array[p_last - 1]),
  202. p_array);
  203. if (cut <= p_nth) {
  204. p_first = cut;
  205. } else {
  206. p_last = cut;
  207. }
  208. }
  209. insertion_sort(p_first, p_last, p_array);
  210. }
  211. inline void unguarded_linear_insert(int64_t p_last, T p_value, T *p_array) const {
  212. int64_t next = p_last - 1;
  213. while (compare(p_value, p_array[next])) {
  214. if constexpr (Validate) {
  215. ERR_BAD_COMPARE(next == 0);
  216. }
  217. p_array[p_last] = p_array[next];
  218. p_last = next;
  219. next--;
  220. }
  221. p_array[p_last] = p_value;
  222. }
  223. inline void linear_insert(int64_t p_first, int64_t p_last, T *p_array) const {
  224. T val = p_array[p_last];
  225. if (compare(val, p_array[p_first])) {
  226. for (int64_t i = p_last; i > p_first; i--) {
  227. p_array[i] = p_array[i - 1];
  228. }
  229. p_array[p_first] = val;
  230. } else {
  231. unguarded_linear_insert(p_last, val, p_array);
  232. }
  233. }
  234. inline void insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  235. if (p_first == p_last) {
  236. return;
  237. }
  238. for (int64_t i = p_first + 1; i != p_last; i++) {
  239. linear_insert(p_first, i, p_array);
  240. }
  241. }
  242. inline void unguarded_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  243. for (int64_t i = p_first; i != p_last; i++) {
  244. unguarded_linear_insert(i, p_array[i], p_array);
  245. }
  246. }
  247. inline void final_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
  248. if (p_last - p_first > INTROSORT_THRESHOLD) {
  249. insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
  250. unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
  251. } else {
  252. insertion_sort(p_first, p_last, p_array);
  253. }
  254. }
  255. inline void sort_range(int64_t p_first, int64_t p_last, T *p_array) const {
  256. if (p_first != p_last) {
  257. introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
  258. final_insertion_sort(p_first, p_last, p_array);
  259. }
  260. }
  261. inline void sort(T *p_array, int64_t p_len) const {
  262. sort_range(0, p_len, p_array);
  263. }
  264. inline void nth_element(int64_t p_first, int64_t p_last, int64_t p_nth, T *p_array) const {
  265. if (p_first == p_last || p_nth == p_last) {
  266. return;
  267. }
  268. introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2);
  269. }
  270. };