xref: /llvm-project/libcxx/include/__algorithm/ranges_nth_element.h (revision d10dc5a06fac4dcabf2264c64c8672c6f6ae36fb)
123c7328bSKonstantin Varlamov //===----------------------------------------------------------------------===//
223c7328bSKonstantin Varlamov //
323c7328bSKonstantin Varlamov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
423c7328bSKonstantin Varlamov // See https://llvm.org/LICENSE.txt for license information.
523c7328bSKonstantin Varlamov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
623c7328bSKonstantin Varlamov //
723c7328bSKonstantin Varlamov //===----------------------------------------------------------------------===//
823c7328bSKonstantin Varlamov 
923c7328bSKonstantin Varlamov #ifndef _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
1023c7328bSKonstantin Varlamov #define _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
1123c7328bSKonstantin Varlamov 
12a7c3379cSKonstantin Varlamov #include <__algorithm/iterator_operations.h>
1323c7328bSKonstantin Varlamov #include <__algorithm/make_projected.h>
1423c7328bSKonstantin Varlamov #include <__algorithm/nth_element.h>
1523c7328bSKonstantin Varlamov #include <__config>
1623c7328bSKonstantin Varlamov #include <__functional/identity.h>
1723c7328bSKonstantin Varlamov #include <__functional/invoke.h>
1823c7328bSKonstantin Varlamov #include <__functional/ranges_operations.h>
1923c7328bSKonstantin Varlamov #include <__iterator/concepts.h>
2023c7328bSKonstantin Varlamov #include <__iterator/iterator_traits.h>
2123c7328bSKonstantin Varlamov #include <__iterator/next.h>
2223c7328bSKonstantin Varlamov #include <__iterator/projected.h>
2323c7328bSKonstantin Varlamov #include <__iterator/sortable.h>
2423c7328bSKonstantin Varlamov #include <__ranges/access.h>
2523c7328bSKonstantin Varlamov #include <__ranges/concepts.h>
2623c7328bSKonstantin Varlamov #include <__ranges/dangling.h>
2723c7328bSKonstantin Varlamov #include <__utility/forward.h>
2823c7328bSKonstantin Varlamov #include <__utility/move.h>
2923c7328bSKonstantin Varlamov 
3023c7328bSKonstantin Varlamov #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
3123c7328bSKonstantin Varlamov #  pragma GCC system_header
3223c7328bSKonstantin Varlamov #endif
3323c7328bSKonstantin Varlamov 
347b462251SLouis Dionne _LIBCPP_PUSH_MACROS
357b462251SLouis Dionne #include <__undef_macros>
367b462251SLouis Dionne 
374f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
3823c7328bSKonstantin Varlamov 
3923c7328bSKonstantin Varlamov _LIBCPP_BEGIN_NAMESPACE_STD
4023c7328bSKonstantin Varlamov 
4123c7328bSKonstantin Varlamov namespace ranges {
42*d10dc5a0SChristopher Di Bella struct __nth_element {
4323c7328bSKonstantin Varlamov   template <class _Iter, class _Sent, class _Comp, class _Proj>
445aa03b64SLouis Dionne   _LIBCPP_HIDE_FROM_ABI constexpr static _Iter
455aa03b64SLouis Dionne   __nth_element_fn_impl(_Iter __first, _Iter __nth, _Sent __last, _Comp& __comp, _Proj& __proj) {
4623c7328bSKonstantin Varlamov     auto __last_iter = ranges::next(__first, __last);
4723c7328bSKonstantin Varlamov 
48db7d7959SKonstantin Varlamov     auto&& __projected_comp = std::__make_projected(__comp, __proj);
49a7c3379cSKonstantin Varlamov     std::__nth_element_impl<_RangeAlgPolicy>(std::move(__first), std::move(__nth), __last_iter, __projected_comp);
5023c7328bSKonstantin Varlamov 
5123c7328bSKonstantin Varlamov     return __last_iter;
5223c7328bSKonstantin Varlamov   }
5323c7328bSKonstantin Varlamov 
5423c7328bSKonstantin Varlamov   template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
5523c7328bSKonstantin Varlamov     requires sortable<_Iter, _Comp, _Proj>
565aa03b64SLouis Dionne   _LIBCPP_HIDE_FROM_ABI constexpr _Iter
575aa03b64SLouis Dionne   operator()(_Iter __first, _Iter __nth, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
5823c7328bSKonstantin Varlamov     return __nth_element_fn_impl(std::move(__first), std::move(__nth), std::move(__last), __comp, __proj);
5923c7328bSKonstantin Varlamov   }
6023c7328bSKonstantin Varlamov 
6123c7328bSKonstantin Varlamov   template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
6223c7328bSKonstantin Varlamov     requires sortable<iterator_t<_Range>, _Comp, _Proj>
635aa03b64SLouis Dionne   _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
645aa03b64SLouis Dionne   operator()(_Range&& __r, iterator_t<_Range> __nth, _Comp __comp = {}, _Proj __proj = {}) const {
6523c7328bSKonstantin Varlamov     return __nth_element_fn_impl(ranges::begin(__r), std::move(__nth), ranges::end(__r), __comp, __proj);
6623c7328bSKonstantin Varlamov   }
6723c7328bSKonstantin Varlamov };
6823c7328bSKonstantin Varlamov 
6923c7328bSKonstantin Varlamov inline namespace __cpo {
70*d10dc5a0SChristopher Di Bella inline constexpr auto nth_element = __nth_element{};
7123c7328bSKonstantin Varlamov } // namespace __cpo
7223c7328bSKonstantin Varlamov } // namespace ranges
7323c7328bSKonstantin Varlamov 
7423c7328bSKonstantin Varlamov _LIBCPP_END_NAMESPACE_STD
7523c7328bSKonstantin Varlamov 
764f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 20
7723c7328bSKonstantin Varlamov 
787b462251SLouis Dionne _LIBCPP_POP_MACROS
797b462251SLouis Dionne 
8023c7328bSKonstantin Varlamov #endif // _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
81