xref: /llvm-project/libcxx/include/__algorithm/ranges_partial_sort_copy.h (revision d10dc5a06fac4dcabf2264c64c8672c6f6ae36fb)
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 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_PARTIAL_SORT_COPY_H
10 #define _LIBCPP___ALGORITHM_RANGES_PARTIAL_SORT_COPY_H
11 
12 #include <__algorithm/in_out_result.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__algorithm/make_projected.h>
15 #include <__algorithm/partial_sort_copy.h>
16 #include <__config>
17 #include <__functional/identity.h>
18 #include <__functional/ranges_operations.h>
19 #include <__iterator/concepts.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__iterator/projected.h>
22 #include <__iterator/sortable.h>
23 #include <__ranges/access.h>
24 #include <__ranges/concepts.h>
25 #include <__ranges/dangling.h>
26 #include <__utility/move.h>
27 #include <__utility/pair.h>
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #  pragma GCC system_header
31 #endif
32 
33 _LIBCPP_PUSH_MACROS
34 #include <__undef_macros>
35 
36 #if _LIBCPP_STD_VER >= 20
37 
38 _LIBCPP_BEGIN_NAMESPACE_STD
39 
40 namespace ranges {
41 
42 template <class _InIter, class _OutIter>
43 using partial_sort_copy_result = in_out_result<_InIter, _OutIter>;
44 
45 struct __partial_sort_copy {
46   template <input_iterator _Iter1,
47             sentinel_for<_Iter1> _Sent1,
48             random_access_iterator _Iter2,
49             sentinel_for<_Iter2> _Sent2,
50             class _Comp  = ranges::less,
51             class _Proj1 = identity,
52             class _Proj2 = identity>
53     requires indirectly_copyable<_Iter1, _Iter2> && sortable<_Iter2, _Comp, _Proj2> &&
54              indirect_strict_weak_order<_Comp, projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>>
55   _LIBCPP_HIDE_FROM_ABI constexpr partial_sort_copy_result<_Iter1, _Iter2> operator()(
56       _Iter1 __first,
57       _Sent1 __last,
58       _Iter2 __result_first,
59       _Sent2 __result_last,
60       _Comp __comp   = {},
61       _Proj1 __proj1 = {},
62       _Proj2 __proj2 = {}) const {
63     auto __result = std::__partial_sort_copy<_RangeAlgPolicy>(
64         std::move(__first),
65         std::move(__last),
66         std::move(__result_first),
67         std::move(__result_last),
68         __comp,
69         __proj1,
70         __proj2);
71     return {std::move(__result.first), std::move(__result.second)};
72   }
73 
74   template <input_range _Range1,
75             random_access_range _Range2,
76             class _Comp  = ranges::less,
77             class _Proj1 = identity,
78             class _Proj2 = identity>
79     requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>> &&
80              sortable<iterator_t<_Range2>, _Comp, _Proj2> &&
81              indirect_strict_weak_order<_Comp,
82                                         projected<iterator_t<_Range1>, _Proj1>,
83                                         projected<iterator_t<_Range2>, _Proj2>>
84   _LIBCPP_HIDE_FROM_ABI constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>>
85   operator()(
86       _Range1&& __range, _Range2&& __result_range, _Comp __comp = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
87     auto __result = std::__partial_sort_copy<_RangeAlgPolicy>(
88         ranges::begin(__range),
89         ranges::end(__range),
90         ranges::begin(__result_range),
91         ranges::end(__result_range),
92         __comp,
93         __proj1,
94         __proj2);
95     return {std::move(__result.first), std::move(__result.second)};
96   }
97 };
98 
99 inline namespace __cpo {
100 inline constexpr auto partial_sort_copy = __partial_sort_copy{};
101 } // namespace __cpo
102 } // namespace ranges
103 
104 _LIBCPP_END_NAMESPACE_STD
105 
106 #endif // _LIBCPP_STD_VER >= 20
107 
108 _LIBCPP_POP_MACROS
109 
110 #endif // _LIBCPP___ALGORITHM_RANGES_PARTIAL_SORT_COPY_H
111