pdqsort.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Orson Peters 2017.
  4. // (C) Copyright Ion Gaztanaga 2017-2018.
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/move for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. //
  13. // This implementation of Pattern-defeating quicksort (pdqsort) was written
  14. // by Orson Peters, and discussed in the Boost mailing list:
  15. // http://boost.2283326.n4.nabble.com/sort-pdqsort-td4691031.html
  16. //
  17. // This implementation is the adaptation by Ion Gaztanaga of code originally in GitHub
  18. // with permission from the author to relicense it under the Boost Software License
  19. // (see the Boost mailing list for details).
  20. //
  21. // The original copyright statement is pasted here for completeness:
  22. //
  23. // pdqsort.h - Pattern-defeating quicksort.
  24. // Copyright (c) 2015 Orson Peters
  25. // This software is provided 'as-is', without any express or implied warranty. In no event will the
  26. // authors be held liable for any damages arising from the use of this software.
  27. // Permission is granted to anyone to use this software for any purpose, including commercial
  28. // applications, and to alter it and redistribute it freely, subject to the following restrictions:
  29. // 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
  30. // original software. If you use this software in a product, an acknowledgment in the product
  31. // documentation would be appreciated but is not required.
  32. // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
  33. // being the original software.
  34. // 3. This notice may not be removed or altered from any source distribution.
  35. //
  36. //////////////////////////////////////////////////////////////////////////////
  37. #ifndef BOOST_MOVE_ALGO_PDQSORT_HPP
  38. #define BOOST_MOVE_ALGO_PDQSORT_HPP
  39. #ifndef BOOST_CONFIG_HPP
  40. # include <boost/config.hpp>
  41. #endif
  42. #
  43. #if defined(BOOST_HAS_PRAGMA_ONCE)
  44. # pragma once
  45. #endif
  46. #include <boost/move/detail/config_begin.hpp>
  47. #include <boost/move/detail/workaround.hpp>
  48. #include <boost/move/utility_core.hpp>
  49. #include <boost/move/algo/detail/insertion_sort.hpp>
  50. #include <boost/move/algo/detail/heap_sort.hpp>
  51. #include <boost/move/detail/iterator_traits.hpp>
  52. #include <boost/move/adl_move_swap.hpp>
  53. #include <cstddef>
  54. namespace boost {
  55. namespace movelib {
  56. namespace pdqsort_detail {
  57. //A simple pair implementation to avoid including <utility>
  58. template<class T1, class T2>
  59. struct pair
  60. {
  61. pair()
  62. {}
  63. pair(const T1 &t1, const T2 &t2)
  64. : first(t1), second(t2)
  65. {}
  66. T1 first;
  67. T2 second;
  68. };
  69. enum {
  70. // Partitions below this size are sorted using insertion sort.
  71. insertion_sort_threshold = 24,
  72. // Partitions above this size use Tukey's ninther to select the pivot.
  73. ninther_threshold = 128,
  74. // When we detect an already sorted partition, attempt an insertion sort that allows this
  75. // amount of element moves before giving up.
  76. partial_insertion_sort_limit = 8,
  77. // Must be multiple of 8 due to loop unrolling, and < 256 to fit in unsigned char.
  78. block_size = 64,
  79. // Cacheline size, assumes power of two.
  80. cacheline_size = 64
  81. };
  82. // Returns floor(log2(n)), assumes n > 0.
  83. template<class Unsigned>
  84. Unsigned log2(Unsigned n) {
  85. Unsigned log = 0;
  86. while (n >>= 1) ++log;
  87. return log;
  88. }
  89. // Attempts to use insertion sort on [begin, end). Will return false if more than
  90. // partial_insertion_sort_limit elements were moved, and abort sorting. Otherwise it will
  91. // successfully sort and return true.
  92. template<class Iter, class Compare>
  93. inline bool partial_insertion_sort(Iter begin, Iter end, Compare comp) {
  94. typedef typename boost::movelib::iterator_traits<Iter>::value_type T;
  95. typedef typename boost::movelib::iterator_traits<Iter>::size_type size_type;
  96. if (begin == end) return true;
  97. size_type limit = 0;
  98. for (Iter cur = begin + 1; cur != end; ++cur) {
  99. if (limit > partial_insertion_sort_limit) return false;
  100. Iter sift = cur;
  101. Iter sift_1 = cur - 1;
  102. // Compare first so we can avoid 2 moves for an element already positioned correctly.
  103. if (comp(*sift, *sift_1)) {
  104. T tmp = boost::move(*sift);
  105. do { *sift-- = boost::move(*sift_1); }
  106. while (sift != begin && comp(tmp, *--sift_1));
  107. *sift = boost::move(tmp);
  108. limit += size_type(cur - sift);
  109. }
  110. }
  111. return true;
  112. }
  113. template<class Iter, class Compare>
  114. inline void sort2(Iter a, Iter b, Compare comp) {
  115. if (comp(*b, *a)) boost::adl_move_iter_swap(a, b);
  116. }
  117. // Sorts the elements *a, *b and *c using comparison function comp.
  118. template<class Iter, class Compare>
  119. inline void sort3(Iter a, Iter b, Iter c, Compare comp) {
  120. sort2(a, b, comp);
  121. sort2(b, c, comp);
  122. sort2(a, b, comp);
  123. }
  124. // Partitions [begin, end) around pivot *begin using comparison function comp. Elements equal
  125. // to the pivot are put in the right-hand partition. Returns the position of the pivot after
  126. // partitioning and whether the passed sequence already was correctly partitioned. Assumes the
  127. // pivot is a median of at least 3 elements and that [begin, end) is at least
  128. // insertion_sort_threshold long.
  129. template<class Iter, class Compare>
  130. pdqsort_detail::pair<Iter, bool> partition_right(Iter begin, Iter end, Compare comp) {
  131. typedef typename boost::movelib::iterator_traits<Iter>::value_type T;
  132. // Move pivot into local for speed.
  133. T pivot(boost::move(*begin));
  134. Iter first = begin;
  135. Iter last = end;
  136. // Find the first element greater than or equal than the pivot (the median of 3 guarantees
  137. // this exists).
  138. while (comp(*++first, pivot));
  139. // Find the first element strictly smaller than the pivot. We have to guard this search if
  140. // there was no element before *first.
  141. if (first - 1 == begin) while (first < last && !comp(*--last, pivot));
  142. else while ( !comp(*--last, pivot));
  143. // If the first pair of elements that should be swapped to partition are the same element,
  144. // the passed in sequence already was correctly partitioned.
  145. bool already_partitioned = first >= last;
  146. // Keep swapping pairs of elements that are on the wrong side of the pivot. Previously
  147. // swapped pairs guard the searches, which is why the first iteration is special-cased
  148. // above.
  149. while (first < last) {
  150. boost::adl_move_iter_swap(first, last);
  151. while (comp(*++first, pivot));
  152. while (!comp(*--last, pivot));
  153. }
  154. // Put the pivot in the right place.
  155. Iter pivot_pos = first - 1;
  156. *begin = boost::move(*pivot_pos);
  157. *pivot_pos = boost::move(pivot);
  158. return pdqsort_detail::pair<Iter, bool>(pivot_pos, already_partitioned);
  159. }
  160. // Similar function to the one above, except elements equal to the pivot are put to the left of
  161. // the pivot and it doesn't check or return if the passed sequence already was partitioned.
  162. // Since this is rarely used (the many equal case), and in that case pdqsort already has O(n)
  163. // performance, no block quicksort is applied here for simplicity.
  164. template<class Iter, class Compare>
  165. inline Iter partition_left(Iter begin, Iter end, Compare comp) {
  166. typedef typename boost::movelib::iterator_traits<Iter>::value_type T;
  167. T pivot(boost::move(*begin));
  168. Iter first = begin;
  169. Iter last = end;
  170. while (comp(pivot, *--last));
  171. if (last + 1 == end) while (first < last && !comp(pivot, *++first));
  172. else while ( !comp(pivot, *++first));
  173. while (first < last) {
  174. boost::adl_move_iter_swap(first, last);
  175. while (comp(pivot, *--last));
  176. while (!comp(pivot, *++first));
  177. }
  178. Iter pivot_pos = last;
  179. *begin = boost::move(*pivot_pos);
  180. *pivot_pos = boost::move(pivot);
  181. return pivot_pos;
  182. }
  183. template<class Iter, class Compare>
  184. void pdqsort_loop( Iter begin, Iter end, Compare comp
  185. , typename boost::movelib::iterator_traits<Iter>::size_type bad_allowed
  186. , bool leftmost = true)
  187. {
  188. typedef typename boost::movelib::iterator_traits<Iter>::size_type size_type;
  189. // Use a while loop for tail recursion elimination.
  190. while (true) {
  191. size_type size = size_type(end - begin);
  192. // Insertion sort is faster for small arrays.
  193. if (size < insertion_sort_threshold) {
  194. insertion_sort(begin, end, comp);
  195. return;
  196. }
  197. // Choose pivot as median of 3 or pseudomedian of 9.
  198. size_type s2 = size / 2;
  199. if (size > ninther_threshold) {
  200. sort3(begin, begin + s2, end - 1, comp);
  201. sort3(begin + 1, begin + (s2 - 1), end - 2, comp);
  202. sort3(begin + 2, begin + (s2 + 1), end - 3, comp);
  203. sort3(begin + (s2 - 1), begin + s2, begin + (s2 + 1), comp);
  204. boost::adl_move_iter_swap(begin, begin + s2);
  205. } else sort3(begin + s2, begin, end - 1, comp);
  206. // If *(begin - 1) is the end of the right partition of a previous partition operation
  207. // there is no element in [begin, end) that is smaller than *(begin - 1). Then if our
  208. // pivot compares equal to *(begin - 1) we change strategy, putting equal elements in
  209. // the left partition, greater elements in the right partition. We do not have to
  210. // recurse on the left partition, since it's sorted (all equal).
  211. if (!leftmost && !comp(*(begin - 1), *begin)) {
  212. begin = partition_left(begin, end, comp) + 1;
  213. continue;
  214. }
  215. // Partition and get results.
  216. pdqsort_detail::pair<Iter, bool> part_result = partition_right(begin, end, comp);
  217. Iter pivot_pos = part_result.first;
  218. bool already_partitioned = part_result.second;
  219. // Check for a highly unbalanced partition.
  220. size_type l_size = size_type(pivot_pos - begin);
  221. size_type r_size = size_type(end - (pivot_pos + 1));
  222. bool highly_unbalanced = l_size < size / 8 || r_size < size / 8;
  223. // If we got a highly unbalanced partition we shuffle elements to break many patterns.
  224. if (highly_unbalanced) {
  225. // If we had too many bad partitions, switch to heapsort to guarantee O(n log n).
  226. if (--bad_allowed == 0) {
  227. boost::movelib::heap_sort(begin, end, comp);
  228. return;
  229. }
  230. if (l_size >= insertion_sort_threshold) {
  231. boost::adl_move_iter_swap(begin, begin + l_size / 4);
  232. boost::adl_move_iter_swap(pivot_pos - 1, pivot_pos - l_size / 4);
  233. if (l_size > ninther_threshold) {
  234. boost::adl_move_iter_swap(begin + 1, begin + (l_size / 4 + 1));
  235. boost::adl_move_iter_swap(begin + 2, begin + (l_size / 4 + 2));
  236. boost::adl_move_iter_swap(pivot_pos - 2, pivot_pos - (l_size / 4 + 1));
  237. boost::adl_move_iter_swap(pivot_pos - 3, pivot_pos - (l_size / 4 + 2));
  238. }
  239. }
  240. if (r_size >= insertion_sort_threshold) {
  241. boost::adl_move_iter_swap(pivot_pos + 1, pivot_pos + (1 + r_size / 4));
  242. boost::adl_move_iter_swap(end - 1, end - r_size / 4);
  243. if (r_size > ninther_threshold) {
  244. boost::adl_move_iter_swap(pivot_pos + 2, pivot_pos + (2 + r_size / 4));
  245. boost::adl_move_iter_swap(pivot_pos + 3, pivot_pos + (3 + r_size / 4));
  246. boost::adl_move_iter_swap(end - 2, end - (1 + r_size / 4));
  247. boost::adl_move_iter_swap(end - 3, end - (2 + r_size / 4));
  248. }
  249. }
  250. } else {
  251. // If we were decently balanced and we tried to sort an already partitioned
  252. // sequence try to use insertion sort.
  253. if (already_partitioned && partial_insertion_sort(begin, pivot_pos, comp)
  254. && partial_insertion_sort(pivot_pos + 1, end, comp)) return;
  255. }
  256. // Sort the left partition first using recursion and do tail recursion elimination for
  257. // the right-hand partition.
  258. pdqsort_loop<Iter, Compare>(begin, pivot_pos, comp, bad_allowed, leftmost);
  259. begin = pivot_pos + 1;
  260. leftmost = false;
  261. }
  262. }
  263. }
  264. template<class Iter, class Compare>
  265. void pdqsort(Iter begin, Iter end, Compare comp)
  266. {
  267. if (begin == end) return;
  268. typedef typename boost::movelib::iterator_traits<Iter>::size_type size_type;
  269. pdqsort_detail::pdqsort_loop<Iter, Compare>(begin, end, comp, pdqsort_detail::log2(size_type(end - begin)));
  270. }
  271. } //namespace movelib {
  272. } //namespace boost {
  273. #include <boost/move/detail/config_end.hpp>
  274. #endif //BOOST_MOVE_ALGO_PDQSORT_HPP