xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_nth_element.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
1753f127fSDimitry Andric //===----------------------------------------------------------------------===//
2753f127fSDimitry Andric //
3753f127fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4753f127fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5753f127fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6753f127fSDimitry Andric //
7753f127fSDimitry Andric //===----------------------------------------------------------------------===//
8753f127fSDimitry Andric 
9753f127fSDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
10753f127fSDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
11753f127fSDimitry Andric 
12fcaf7f86SDimitry Andric #include <__algorithm/iterator_operations.h>
13753f127fSDimitry Andric #include <__algorithm/make_projected.h>
14753f127fSDimitry Andric #include <__algorithm/nth_element.h>
15753f127fSDimitry Andric #include <__config>
16753f127fSDimitry Andric #include <__functional/identity.h>
17753f127fSDimitry Andric #include <__functional/invoke.h>
18753f127fSDimitry Andric #include <__functional/ranges_operations.h>
19753f127fSDimitry Andric #include <__iterator/concepts.h>
20753f127fSDimitry Andric #include <__iterator/iterator_traits.h>
21753f127fSDimitry Andric #include <__iterator/next.h>
22753f127fSDimitry Andric #include <__iterator/projected.h>
23753f127fSDimitry Andric #include <__iterator/sortable.h>
24753f127fSDimitry Andric #include <__ranges/access.h>
25753f127fSDimitry Andric #include <__ranges/concepts.h>
26753f127fSDimitry Andric #include <__ranges/dangling.h>
27753f127fSDimitry Andric #include <__utility/forward.h>
28753f127fSDimitry Andric #include <__utility/move.h>
29753f127fSDimitry Andric 
30753f127fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31753f127fSDimitry Andric #  pragma GCC system_header
32753f127fSDimitry Andric #endif
33753f127fSDimitry Andric 
34*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
35*b3edf446SDimitry Andric #include <__undef_macros>
36*b3edf446SDimitry Andric 
3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
38753f127fSDimitry Andric 
39753f127fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
40753f127fSDimitry Andric 
41753f127fSDimitry Andric namespace ranges {
42753f127fSDimitry Andric namespace __nth_element {
43753f127fSDimitry Andric 
44753f127fSDimitry Andric struct __fn {
45753f127fSDimitry Andric   template <class _Iter, class _Sent, class _Comp, class _Proj>
4606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr static _Iter
__nth_element_fn_impl__fn4706c3fb27SDimitry Andric   __nth_element_fn_impl(_Iter __first, _Iter __nth, _Sent __last, _Comp& __comp, _Proj& __proj) {
48753f127fSDimitry Andric     auto __last_iter = ranges::next(__first, __last);
49753f127fSDimitry Andric 
5061cfbce3SDimitry Andric     auto&& __projected_comp = std::__make_projected(__comp, __proj);
51fcaf7f86SDimitry Andric     std::__nth_element_impl<_RangeAlgPolicy>(std::move(__first), std::move(__nth), __last_iter, __projected_comp);
52753f127fSDimitry Andric 
53753f127fSDimitry Andric     return __last_iter;
54753f127fSDimitry Andric   }
55753f127fSDimitry Andric 
56753f127fSDimitry Andric   template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
57753f127fSDimitry Andric     requires sortable<_Iter, _Comp, _Proj>
5806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Iter
operator__fn5906c3fb27SDimitry Andric   operator()(_Iter __first, _Iter __nth, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
60753f127fSDimitry Andric     return __nth_element_fn_impl(std::move(__first), std::move(__nth), std::move(__last), __comp, __proj);
61753f127fSDimitry Andric   }
62753f127fSDimitry Andric 
63753f127fSDimitry Andric   template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
64753f127fSDimitry Andric     requires sortable<iterator_t<_Range>, _Comp, _Proj>
6506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
operator__fn6606c3fb27SDimitry Andric   operator()(_Range&& __r, iterator_t<_Range> __nth, _Comp __comp = {}, _Proj __proj = {}) const {
67753f127fSDimitry Andric     return __nth_element_fn_impl(ranges::begin(__r), std::move(__nth), ranges::end(__r), __comp, __proj);
68753f127fSDimitry Andric   }
69753f127fSDimitry Andric };
70753f127fSDimitry Andric 
71753f127fSDimitry Andric } // namespace __nth_element
72753f127fSDimitry Andric 
73753f127fSDimitry Andric inline namespace __cpo {
74753f127fSDimitry Andric inline constexpr auto nth_element = __nth_element::__fn{};
75753f127fSDimitry Andric } // namespace __cpo
76753f127fSDimitry Andric } // namespace ranges
77753f127fSDimitry Andric 
78753f127fSDimitry Andric _LIBCPP_END_NAMESPACE_STD
79753f127fSDimitry Andric 
8006c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
81753f127fSDimitry Andric 
82*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
83*b3edf446SDimitry Andric 
84753f127fSDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
85