nth_element.h 10 KB

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