sort_array.hpp 9.7 KB

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