sort.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "typedefs.h"
  33. /**
  34. @author ,,, <red@lunatea>
  35. */
  36. template <class T>
  37. struct _DefaultComparator {
  38. inline bool operator()(const T &a, const T &b) const { return (a < b); }
  39. };
  40. template <class T, class Comparator = _DefaultComparator<T> >
  41. class SortArray {
  42. enum {
  43. INTROSORT_THRESHOLD = 16
  44. };
  45. public:
  46. Comparator compare;
  47. inline const T &median_of_3(const T &a, const T &b, const T &c) const {
  48. if (compare(a, b))
  49. if (compare(b, c))
  50. return b;
  51. else if (compare(a, c))
  52. return c;
  53. else
  54. return a;
  55. else if (compare(a, c))
  56. return a;
  57. else if (compare(b, c))
  58. return c;
  59. else
  60. return b;
  61. }
  62. inline int bitlog(int n) const {
  63. int k;
  64. for (k = 0; n != 1; n >>= 1)
  65. ++k;
  66. return k;
  67. }
  68. /* Heap / Heapsort functions */
  69. inline void push_heap(int p_first, int p_hole_idx, int p_top_index, T p_value, T *p_array) const {
  70. int parent = (p_hole_idx - 1) / 2;
  71. while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
  72. p_array[p_first + p_hole_idx] = p_array[p_first + parent];
  73. p_hole_idx = parent;
  74. parent = (p_hole_idx - 1) / 2;
  75. }
  76. p_array[p_first + p_hole_idx] = p_value;
  77. }
  78. inline void pop_heap(int p_first, int p_last, int p_result, T p_value, T *p_array) const {
  79. p_array[p_result] = p_array[p_first];
  80. adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
  81. }
  82. inline void pop_heap(int p_first, int p_last, T *p_array) const {
  83. pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
  84. }
  85. inline void adjust_heap(int p_first, int p_hole_idx, int p_len, T p_value, T *p_array) const {
  86. int top_index = p_hole_idx;
  87. int second_child = 2 * p_hole_idx + 2;
  88. while (second_child < p_len) {
  89. if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)]))
  90. second_child--;
  91. p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
  92. p_hole_idx = second_child;
  93. second_child = 2 * (second_child + 1);
  94. }
  95. if (second_child == p_len) {
  96. p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
  97. p_hole_idx = second_child - 1;
  98. }
  99. push_heap(p_first, p_hole_idx, top_index, p_value, p_array);
  100. }
  101. inline void sort_heap(int p_first, int p_last, T *p_array) const {
  102. while (p_last - p_first > 1) {
  103. pop_heap(p_first, p_last--, p_array);
  104. }
  105. }
  106. inline void make_heap(int p_first, int p_last, T *p_array) const {
  107. if (p_last - p_first < 2)
  108. return;
  109. int len = p_last - p_first;
  110. int parent = (len - 2) / 2;
  111. while (true) {
  112. adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
  113. if (parent == 0)
  114. return;
  115. parent--;
  116. }
  117. }
  118. inline void partial_sort(int p_first, int p_last, int p_middle, T *p_array) const {
  119. make_heap(p_first, p_middle, p_array);
  120. for (int i = p_middle; i < p_last; i++)
  121. if (compare(p_array[i], p_array[p_first]))
  122. pop_heap(p_first, p_middle, i, p_array[i], p_array);
  123. sort_heap(p_first, p_middle, p_array);
  124. }
  125. inline void partial_select(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. }
  131. inline int partitioner(int p_first, int p_last, T p_pivot, T *p_array) const {
  132. while (true) {
  133. while (compare(p_array[p_first], p_pivot))
  134. p_first++;
  135. p_last--;
  136. while (compare(p_pivot, p_array[p_last]))
  137. p_last--;
  138. if (!(p_first < p_last))
  139. return p_first;
  140. SWAP(p_array[p_first], p_array[p_last]);
  141. p_first++;
  142. }
  143. }
  144. inline void introsort(int p_first, int p_last, T *p_array, int p_max_depth) const {
  145. while (p_last - p_first > INTROSORT_THRESHOLD) {
  146. if (p_max_depth == 0) {
  147. partial_sort(p_first, p_last, p_last, p_array);
  148. return;
  149. }
  150. p_max_depth--;
  151. int cut = partitioner(
  152. p_first,
  153. p_last,
  154. median_of_3(
  155. p_array[p_first],
  156. p_array[p_first + (p_last - p_first) / 2],
  157. p_array[p_last - 1]),
  158. p_array);
  159. introsort(cut, p_last, p_array, p_max_depth);
  160. p_last = cut;
  161. }
  162. }
  163. inline void introselect(int p_first, int p_nth, int p_last, T *p_array, int p_max_depth) const {
  164. while (p_last - p_first > 3) {
  165. if (p_max_depth == 0) {
  166. partial_select(p_first, p_nth + 1, p_last, p_array);
  167. SWAP(p_first, p_nth);
  168. return;
  169. }
  170. p_max_depth--;
  171. int cut = partitioner(
  172. p_first,
  173. p_last,
  174. median_of_3(
  175. p_array[p_first],
  176. p_array[p_first + (p_last - p_first) / 2],
  177. p_array[p_last - 1]),
  178. p_array);
  179. if (cut <= p_nth)
  180. p_first = cut;
  181. else
  182. p_last = cut;
  183. }
  184. insertion_sort(p_first, p_last, p_array);
  185. }
  186. inline void unguarded_linear_insert(int p_last, T p_value, T *p_array) const {
  187. int next = p_last - 1;
  188. while (compare(p_value, p_array[next])) {
  189. p_array[p_last] = p_array[next];
  190. p_last = next;
  191. next--;
  192. }
  193. p_array[p_last] = p_value;
  194. }
  195. inline void linear_insert(int p_first, int p_last, T *p_array) const {
  196. T val = p_array[p_last];
  197. if (compare(val, p_array[p_first])) {
  198. for (int i = p_last; i > p_first; i--)
  199. p_array[i] = p_array[i - 1];
  200. p_array[p_first] = val;
  201. } else
  202. unguarded_linear_insert(p_last, val, p_array);
  203. }
  204. inline void insertion_sort(int p_first, int p_last, T *p_array) const {
  205. if (p_first == p_last)
  206. return;
  207. for (int i = p_first + 1; i != p_last; i++)
  208. linear_insert(p_first, i, p_array);
  209. }
  210. inline void unguarded_insertion_sort(int p_first, int p_last, T *p_array) const {
  211. for (int i = p_first; i != p_last; i++)
  212. unguarded_linear_insert(i, p_array[i], p_array);
  213. }
  214. inline void final_insertion_sort(int p_first, int p_last, T *p_array) const {
  215. if (p_last - p_first > INTROSORT_THRESHOLD) {
  216. insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
  217. unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
  218. } else {
  219. insertion_sort(p_first, p_last, p_array);
  220. }
  221. }
  222. inline void sort_range(int p_first, int p_last, T *p_array) const {
  223. if (p_first != p_last) {
  224. introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
  225. final_insertion_sort(p_first, p_last, p_array);
  226. }
  227. }
  228. inline void sort(T *p_array, int p_len) const {
  229. sort_range(0, p_len, p_array);
  230. }
  231. inline void nth_element(int p_first, int p_last, int p_nth, T *p_array) const {
  232. if (p_first == p_last || p_nth == p_last)
  233. return;
  234. introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2);
  235. }
  236. };
  237. #endif