sort_array.h 9.7 KB

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