sort.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* sort.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SORT_H
  30. #define SORT_H
  31. #include "typedefs.h"
  32. /**
  33. @author ,,, <red@lunatea>
  34. */
  35. template<class T>
  36. struct _DefaultComparator {
  37. inline bool operator()(const T&a,const T&b) const { return (a<b); }
  38. };
  39. template<class T, class Comparator=_DefaultComparator<T> >
  40. class SortArray {
  41. enum {
  42. INTROSORT_TRESHOLD=16
  43. };
  44. public:
  45. Comparator compare;
  46. inline const T& median_of_3(const T& a, const T& b, const T& c) const {
  47. if (compare(a, b))
  48. if (compare(b, c))
  49. return b;
  50. else if (compare(a, c))
  51. return c;
  52. else
  53. return a;
  54. else if (compare(a, c))
  55. return a;
  56. else if (compare(b, c))
  57. return c;
  58. else
  59. return b;
  60. }
  61. inline int bitlog(int n) const {
  62. int k;
  63. for (k = 0; n != 1; n >>= 1)
  64. ++k;
  65. return k;
  66. }
  67. /* Heap / Heapsort functions */
  68. inline void push_heap(int p_first,int p_hole_idx,int p_top_index,T p_value,T* p_array) const {
  69. int parent = (p_hole_idx - 1) / 2;
  70. while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
  71. p_array[p_first + p_hole_idx] = p_array[p_first + parent];
  72. p_hole_idx = parent;
  73. parent = (p_hole_idx - 1) / 2;
  74. }
  75. p_array[p_first + p_hole_idx] = p_value;
  76. }
  77. inline void pop_heap(int p_first, int p_last, int p_result, T p_value, T* p_array) const {
  78. p_array[p_result]=p_array[p_first];
  79. adjust_heap(p_first,0,p_last-p_first,p_value,p_array);
  80. }
  81. inline void pop_heap(int p_first,int p_last,T* p_array) const {
  82. pop_heap(p_first,p_last-1,p_last-1,p_array[p_last-1],p_array);
  83. }
  84. inline void adjust_heap(int p_first,int p_hole_idx,int p_len,T p_value,T* p_array) const {
  85. int top_index = p_hole_idx;
  86. int second_child = 2 * p_hole_idx + 2;
  87. while (second_child < p_len) {
  88. if (compare(p_array[p_first + second_child],p_array[p_first + (second_child - 1)]))
  89. second_child--;
  90. p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
  91. p_hole_idx = second_child;
  92. second_child = 2 * (second_child + 1);
  93. }
  94. if (second_child == p_len) {
  95. p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
  96. p_hole_idx = second_child - 1;
  97. }
  98. push_heap(p_first, p_hole_idx, top_index, p_value,p_array);
  99. }
  100. inline void sort_heap(int p_first,int p_last,T* p_array) const {
  101. while(p_last-p_first > 1) {
  102. pop_heap(p_first,p_last--,p_array);
  103. }
  104. }
  105. inline void make_heap(int p_first, int p_last,T* p_array) const {
  106. if (p_last - p_first < 2)
  107. return;
  108. int len = p_last - p_first;
  109. int parent = (len - 2)/2;
  110. while (true) {
  111. adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
  112. if (parent == 0)
  113. return;
  114. parent--;
  115. }
  116. }
  117. inline void partial_sort(int p_first,int p_last,int p_middle,T* p_array) const {
  118. make_heap(p_first,p_middle,p_array);
  119. for(int i=p_middle;i<p_last;i++)
  120. if (compare( p_array[i],p_array[p_first]))
  121. pop_heap(p_first,p_middle,i,p_array[i],p_array);
  122. sort_heap(p_first,p_middle,p_array);
  123. }
  124. inline void partial_select(int p_first,int p_last,int p_middle,T* p_array) const {
  125. make_heap(p_first,p_middle,p_array);
  126. for(int i=p_middle;i<p_last;i++)
  127. if (compare( p_array[i],p_array[p_first]))
  128. pop_heap(p_first,p_middle,i,p_array[i],p_array);
  129. }
  130. inline int partitioner(int p_first, int p_last, T p_pivot, T* p_array) const {
  131. while (true) {
  132. while (compare(p_array[p_first],p_pivot))
  133. p_first++;
  134. p_last--;
  135. while (compare(p_pivot,p_array[p_last]))
  136. p_last--;
  137. if (!(p_first < p_last))
  138. return p_first;
  139. SWAP(p_array[p_first],p_array[p_last]);
  140. p_first++;
  141. }
  142. }
  143. inline void introsort(int p_first, int p_last, T* p_array, int p_max_depth) const {
  144. while( p_last - p_first > INTROSORT_TRESHOLD ) {
  145. if (p_max_depth == 0) {
  146. partial_sort(p_first,p_last,p_last,p_array);
  147. return;
  148. }
  149. p_max_depth--;
  150. int cut = partitioner(
  151. p_first,
  152. p_last,
  153. median_of_3(
  154. p_array[p_first],
  155. p_array[p_first + (p_last-p_first)/2],
  156. p_array[p_last-1]
  157. ),
  158. p_array
  159. );
  160. introsort(cut,p_last,p_array,p_max_depth);
  161. p_last=cut;
  162. }
  163. }
  164. inline void introselect(int p_first, int p_nth, int p_last, T* p_array, int p_max_depth) const {
  165. while( p_last - p_first > 3 ) {
  166. if (p_max_depth == 0) {
  167. partial_select(p_first,p_nth+1,p_last,p_array);
  168. SWAP(p_first,p_nth);
  169. return;
  170. }
  171. p_max_depth--;
  172. int cut = partitioner(
  173. p_first,
  174. p_last,
  175. median_of_3(
  176. p_array[p_first],
  177. p_array[p_first + (p_last-p_first)/2],
  178. p_array[p_last-1]
  179. ),
  180. p_array
  181. );
  182. if (cut<=p_nth)
  183. p_first=cut;
  184. else
  185. p_last=cut;
  186. }
  187. insertion_sort(p_first,p_last,p_array);
  188. }
  189. inline void unguarded_linear_insert(int p_last,T p_value,T* p_array) const {
  190. int next = p_last-1;
  191. while (compare(p_value,p_array[next])) {
  192. p_array[p_last]=p_array[next];
  193. p_last = next;
  194. next--;
  195. }
  196. p_array[p_last] = p_value;
  197. }
  198. inline void linear_insert(int p_first,int p_last,T*p_array) const {
  199. T val = p_array[p_last];
  200. if (compare(val, p_array[p_first])) {
  201. for (int i=p_last; i>p_first; i--)
  202. p_array[i]=p_array[i-1];
  203. p_array[p_first] = val;
  204. } else
  205. unguarded_linear_insert(p_last, val, p_array);
  206. }
  207. inline void insertion_sort(int p_first,int p_last,T* p_array) const {
  208. if (p_first==p_last)
  209. return;
  210. for (int i=p_first+1; i!=p_last ; i++)
  211. linear_insert(p_first,i,p_array);
  212. }
  213. inline void unguarded_insertion_sort(int p_first,int p_last,T* p_array) const {
  214. for (int i=p_first; i!=p_last ; i++)
  215. unguarded_linear_insert(i,p_array[i],p_array);
  216. }
  217. inline void final_insertion_sort(int p_first,int p_last,T* p_array) const {
  218. if (p_last - p_first > INTROSORT_TRESHOLD) {
  219. insertion_sort(p_first,p_first+INTROSORT_TRESHOLD,p_array);
  220. unguarded_insertion_sort(p_first+INTROSORT_TRESHOLD,p_last,p_array);
  221. } else {
  222. insertion_sort(p_first,p_last,p_array);
  223. }
  224. }
  225. inline void sort_range(int p_first, int p_last,T* p_array) const {
  226. if (p_first != p_last) {
  227. introsort(p_first, p_last,p_array,bitlog(p_last - p_first) * 2);
  228. final_insertion_sort(p_first, p_last, p_array);
  229. }
  230. }
  231. inline void sort(T* p_array,int p_len) const {
  232. sort_range(0,p_len,p_array);
  233. }
  234. inline void nth_element(int p_first,int p_last,int p_nth,T* p_array) const {
  235. if (p_first==p_last || p_nth==p_last)
  236. return;
  237. introselect(p_first,p_nth,p_last,p_array,bitlog(p_last - p_first) * 2);
  238. }
  239. };
  240. #endif