nth_element.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_NTH_ELEMENT_H
  9. #define _LIBCPP___ALGORITHM_NTH_ELEMENT_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__algorithm/sort.h>
  14. #include <__config>
  15. #include <__debug_utils/randomize_range.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__utility/move.h>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template<class _Compare, class _RandomAccessIterator>
  23. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  24. __nth_element_find_guard(_RandomAccessIterator& __i, _RandomAccessIterator& __j,
  25. _RandomAccessIterator __m, _Compare __comp)
  26. {
  27. // manually guard downward moving __j against __i
  28. while (true) {
  29. if (__i == --__j) {
  30. return false;
  31. }
  32. if (__comp(*__j, *__m)) {
  33. return true; // found guard for downward moving __j, now use unguarded partition
  34. }
  35. }
  36. }
  37. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  38. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
  39. __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
  40. {
  41. using _Ops = _IterOps<_AlgPolicy>;
  42. // _Compare is known to be a reference type
  43. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  44. const difference_type __limit = 7;
  45. while (true)
  46. {
  47. if (__nth == __last)
  48. return;
  49. difference_type __len = __last - __first;
  50. switch (__len)
  51. {
  52. case 0:
  53. case 1:
  54. return;
  55. case 2:
  56. if (__comp(*--__last, *__first))
  57. _Ops::iter_swap(__first, __last);
  58. return;
  59. case 3:
  60. {
  61. _RandomAccessIterator __m = __first;
  62. std::__sort3<_AlgPolicy, _Compare>(__first, ++__m, --__last, __comp);
  63. return;
  64. }
  65. }
  66. if (__len <= __limit)
  67. {
  68. std::__selection_sort<_AlgPolicy, _Compare>(__first, __last, __comp);
  69. return;
  70. }
  71. // __len > __limit >= 3
  72. _RandomAccessIterator __m = __first + __len/2;
  73. _RandomAccessIterator __lm1 = __last;
  74. unsigned __n_swaps = std::__sort3<_AlgPolicy, _Compare>(__first, __m, --__lm1, __comp);
  75. // *__m is median
  76. // partition [__first, __m) < *__m and *__m <= [__m, __last)
  77. // (this inhibits tossing elements equivalent to __m around unnecessarily)
  78. _RandomAccessIterator __i = __first;
  79. _RandomAccessIterator __j = __lm1;
  80. // j points beyond range to be tested, *__lm1 is known to be <= *__m
  81. // The search going up is known to be guarded but the search coming down isn't.
  82. // Prime the downward search with a guard.
  83. if (!__comp(*__i, *__m)) // if *__first == *__m
  84. {
  85. // *__first == *__m, *__first doesn't go in first part
  86. if (_VSTD::__nth_element_find_guard<_Compare>(__i, __j, __m, __comp)) {
  87. _Ops::iter_swap(__i, __j);
  88. ++__n_swaps;
  89. } else {
  90. // *__first == *__m, *__m <= all other elements
  91. // Partition instead into [__first, __i) == *__first and *__first < [__i, __last)
  92. ++__i; // __first + 1
  93. __j = __last;
  94. if (!__comp(*__first, *--__j)) { // we need a guard if *__first == *(__last-1)
  95. while (true) {
  96. if (__i == __j) {
  97. return; // [__first, __last) all equivalent elements
  98. } else if (__comp(*__first, *__i)) {
  99. _Ops::iter_swap(__i, __j);
  100. ++__n_swaps;
  101. ++__i;
  102. break;
  103. }
  104. ++__i;
  105. }
  106. }
  107. // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
  108. if (__i == __j) {
  109. return;
  110. }
  111. while (true) {
  112. while (!__comp(*__first, *__i))
  113. ++__i;
  114. while (__comp(*__first, *--__j))
  115. ;
  116. if (__i >= __j)
  117. break;
  118. _Ops::iter_swap(__i, __j);
  119. ++__n_swaps;
  120. ++__i;
  121. }
  122. // [__first, __i) == *__first and *__first < [__i, __last)
  123. // The first part is sorted,
  124. if (__nth < __i) {
  125. return;
  126. }
  127. // __nth_element the second part
  128. // _VSTD::__nth_element<_Compare>(__i, __nth, __last, __comp);
  129. __first = __i;
  130. continue;
  131. }
  132. }
  133. ++__i;
  134. // j points beyond range to be tested, *__lm1 is known to be <= *__m
  135. // if not yet partitioned...
  136. if (__i < __j)
  137. {
  138. // known that *(__i - 1) < *__m
  139. while (true)
  140. {
  141. // __m still guards upward moving __i
  142. while (__comp(*__i, *__m))
  143. ++__i;
  144. // It is now known that a guard exists for downward moving __j
  145. while (!__comp(*--__j, *__m))
  146. ;
  147. if (__i >= __j)
  148. break;
  149. _Ops::iter_swap(__i, __j);
  150. ++__n_swaps;
  151. // It is known that __m != __j
  152. // If __m just moved, follow it
  153. if (__m == __i)
  154. __m = __j;
  155. ++__i;
  156. }
  157. }
  158. // [__first, __i) < *__m and *__m <= [__i, __last)
  159. if (__i != __m && __comp(*__m, *__i))
  160. {
  161. _Ops::iter_swap(__i, __m);
  162. ++__n_swaps;
  163. }
  164. // [__first, __i) < *__i and *__i <= [__i+1, __last)
  165. if (__nth == __i)
  166. return;
  167. if (__n_swaps == 0)
  168. {
  169. // We were given a perfectly partitioned sequence. Coincidence?
  170. if (__nth < __i)
  171. {
  172. // Check for [__first, __i) already sorted
  173. __j = __m = __first;
  174. while (true) {
  175. if (++__j == __i) {
  176. // [__first, __i) sorted
  177. return;
  178. }
  179. if (__comp(*__j, *__m)) {
  180. // not yet sorted, so sort
  181. break;
  182. }
  183. __m = __j;
  184. }
  185. }
  186. else
  187. {
  188. // Check for [__i, __last) already sorted
  189. __j = __m = __i;
  190. while (true) {
  191. if (++__j == __last) {
  192. // [__i, __last) sorted
  193. return;
  194. }
  195. if (__comp(*__j, *__m)) {
  196. // not yet sorted, so sort
  197. break;
  198. }
  199. __m = __j;
  200. }
  201. }
  202. }
  203. // __nth_element on range containing __nth
  204. if (__nth < __i)
  205. {
  206. // _VSTD::__nth_element<_Compare>(__first, __nth, __i, __comp);
  207. __last = __i;
  208. }
  209. else
  210. {
  211. // _VSTD::__nth_element<_Compare>(__i+1, __nth, __last, __comp);
  212. __first = ++__i;
  213. }
  214. }
  215. }
  216. template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
  217. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  218. void __nth_element_impl(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last,
  219. _Compare& __comp) {
  220. if (__nth == __last)
  221. return;
  222. std::__debug_randomize_range<_AlgPolicy>(__first, __last);
  223. std::__nth_element<_AlgPolicy, __comp_ref_type<_Compare> >(__first, __nth, __last, __comp);
  224. std::__debug_randomize_range<_AlgPolicy>(__first, __nth);
  225. if (__nth != __last) {
  226. std::__debug_randomize_range<_AlgPolicy>(++__nth, __last);
  227. }
  228. }
  229. template <class _RandomAccessIterator, class _Compare>
  230. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  231. void nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last,
  232. _Compare __comp) {
  233. std::__nth_element_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__nth), std::move(__last), __comp);
  234. }
  235. template <class _RandomAccessIterator>
  236. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  237. void nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last) {
  238. std::nth_element(std::move(__first), std::move(__nth), std::move(__last), __less<>());
  239. }
  240. _LIBCPP_END_NAMESPACE_STD
  241. #endif // _LIBCPP___ALGORITHM_NTH_ELEMENT_H