sort.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*************************************************************************/
  2. /* sort.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_H
  31. #define SORT_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. template <class 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 <class T, class 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. else if (compare(a, c))
  63. return a;
  64. else if (compare(b, c))
  65. return c;
  66. else
  67. return b;
  68. }
  69. inline int bitlog(int n) const {
  70. int k;
  71. for (k = 0; n != 1; n >>= 1)
  72. ++k;
  73. return k;
  74. }
  75. /* Heap / Heapsort functions */
  76. inline void push_heap(int p_first, int p_hole_idx, int p_top_index, T p_value, T *p_array) const {
  77. int parent = (p_hole_idx - 1) / 2;
  78. while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
  79. p_array[p_first + p_hole_idx] = p_array[p_first + parent];
  80. p_hole_idx = parent;
  81. parent = (p_hole_idx - 1) / 2;
  82. }
  83. p_array[p_first + p_hole_idx] = p_value;
  84. }
  85. inline void pop_heap(int p_first, int p_last, int p_result, T p_value, T *p_array) const {
  86. p_array[p_result] = p_array[p_first];
  87. adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
  88. }
  89. inline void pop_heap(int p_first, int p_last, T *p_array) const {
  90. pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
  91. }
  92. inline void adjust_heap(int p_first, int p_hole_idx, int p_len, T p_value, T *p_array) const {
  93. int top_index = p_hole_idx;
  94. int second_child = 2 * p_hole_idx + 2;
  95. while (second_child < p_len) {
  96. if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)]))
  97. second_child--;
  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(int p_first, int 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(int p_first, int p_last, T *p_array) const {
  114. if (p_last - p_first < 2)
  115. return;
  116. int len = p_last - p_first;
  117. int parent = (len - 2) / 2;
  118. while (true) {
  119. adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
  120. if (parent == 0)
  121. return;
  122. parent--;
  123. }
  124. }
  125. inline void partial_sort(int p_first, int p_last, int p_middle, T *p_array) const {
  126. make_heap(p_first, p_middle, p_array);
  127. for (int i = p_middle; i < p_last; i++)
  128. if (compare(p_array[i], p_array[p_first]))
  129. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  130. sort_heap(p_first, p_middle, p_array);
  131. }
  132. inline void partial_select(int p_first, int p_last, int p_middle, T *p_array) const {
  133. make_heap(p_first, p_middle, p_array);
  134. for (int 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. inline int partitioner(int p_first, int p_last, T p_pivot, T *p_array) const {
  139. const int unmodified_first = p_first;
  140. const int unmodified_last = p_last;
  141. while (true) {
  142. while (compare(p_array[p_first], p_pivot)) {
  143. if (Validate) {
  144. ERR_BAD_COMPARE(p_first == unmodified_last - 1)
  145. }
  146. p_first++;
  147. }
  148. p_last--;
  149. while (compare(p_pivot, p_array[p_last])) {
  150. if (Validate) {
  151. ERR_BAD_COMPARE(p_last == unmodified_first)
  152. }
  153. p_last--;
  154. }
  155. if (!(p_first < p_last))
  156. return p_first;
  157. SWAP(p_array[p_first], p_array[p_last]);
  158. p_first++;
  159. }
  160. }
  161. inline void introsort(int p_first, int p_last, T *p_array, int p_max_depth) const {
  162. while (p_last - p_first > INTROSORT_THRESHOLD) {
  163. if (p_max_depth == 0) {
  164. partial_sort(p_first, p_last, p_last, p_array);
  165. return;
  166. }
  167. p_max_depth--;
  168. int cut = partitioner(
  169. p_first,
  170. p_last,
  171. median_of_3(
  172. p_array[p_first],
  173. p_array[p_first + (p_last - p_first) / 2],
  174. p_array[p_last - 1]),
  175. p_array);
  176. introsort(cut, p_last, p_array, p_max_depth);
  177. p_last = cut;
  178. }
  179. }
  180. inline void introselect(int p_first, int p_nth, int p_last, T *p_array, int p_max_depth) const {
  181. while (p_last - p_first > 3) {
  182. if (p_max_depth == 0) {
  183. partial_select(p_first, p_nth + 1, p_last, p_array);
  184. SWAP(p_first, p_nth);
  185. return;
  186. }
  187. p_max_depth--;
  188. int cut = partitioner(
  189. p_first,
  190. p_last,
  191. median_of_3(
  192. p_array[p_first],
  193. p_array[p_first + (p_last - p_first) / 2],
  194. p_array[p_last - 1]),
  195. p_array);
  196. if (cut <= p_nth)
  197. p_first = cut;
  198. else
  199. p_last = cut;
  200. }
  201. insertion_sort(p_first, p_last, p_array);
  202. }
  203. inline void unguarded_linear_insert(int p_last, T p_value, T *p_array) const {
  204. int next = p_last - 1;
  205. while (compare(p_value, p_array[next])) {
  206. if (Validate) {
  207. ERR_BAD_COMPARE(next == 0)
  208. }
  209. p_array[p_last] = p_array[next];
  210. p_last = next;
  211. next--;
  212. }
  213. p_array[p_last] = p_value;
  214. }
  215. inline void linear_insert(int p_first, int p_last, T *p_array) const {
  216. T val = p_array[p_last];
  217. if (compare(val, p_array[p_first])) {
  218. for (int i = p_last; i > p_first; i--)
  219. p_array[i] = p_array[i - 1];
  220. p_array[p_first] = val;
  221. } else
  222. unguarded_linear_insert(p_last, val, p_array);
  223. }
  224. inline void insertion_sort(int p_first, int p_last, T *p_array) const {
  225. if (p_first == p_last)
  226. return;
  227. for (int i = p_first + 1; i != p_last; i++)
  228. linear_insert(p_first, i, p_array);
  229. }
  230. inline void unguarded_insertion_sort(int p_first, int p_last, T *p_array) const {
  231. for (int i = p_first; i != p_last; i++)
  232. unguarded_linear_insert(i, p_array[i], p_array);
  233. }
  234. inline void final_insertion_sort(int p_first, int p_last, T *p_array) const {
  235. if (p_last - p_first > INTROSORT_THRESHOLD) {
  236. insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
  237. unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
  238. } else {
  239. insertion_sort(p_first, p_last, p_array);
  240. }
  241. }
  242. inline void sort_range(int p_first, int p_last, T *p_array) const {
  243. if (p_first != p_last) {
  244. introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
  245. final_insertion_sort(p_first, p_last, p_array);
  246. }
  247. }
  248. inline void sort(T *p_array, int p_len) const {
  249. sort_range(0, p_len, p_array);
  250. }
  251. inline void nth_element(int p_first, int p_last, int p_nth, T *p_array) const {
  252. if (p_first == p_last || p_nth == p_last)
  253. return;
  254. introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2);
  255. }
  256. };
  257. #endif