xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_merge.h (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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_MERGE_H
10 #define _LIBCPP___ALGORITHM_RANGES_MERGE_H
11 
12 #include <__algorithm/in_in_out_result.h>
13 #include <__algorithm/ranges_copy.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/invoke.h>
17 #include <__functional/ranges_operations.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/mergeable.h>
20 #include <__ranges/access.h>
21 #include <__ranges/concepts.h>
22 #include <__ranges/dangling.h>
23 #include <__utility/move.h>
24 #include <type_traits>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 #if _LIBCPP_STD_VER > 17
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 namespace ranges {
35 
36 template <class _InIter1, class _InIter2, class _OutIter>
37 using merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
38 
39 namespace __merge {
40 
41 template <
42     class _InIter1,
43     class _Sent1,
44     class _InIter2,
45     class _Sent2,
46     class _OutIter,
47     class _Comp,
48     class _Proj1,
49     class _Proj2>
50 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<__remove_cvref_t<_InIter1>, __remove_cvref_t<_InIter2>, __remove_cvref_t<_OutIter>>
51 __merge_impl(
52     _InIter1&& __first1,
53     _Sent1&& __last1,
54     _InIter2&& __first2,
55     _Sent2&& __last2,
56     _OutIter&& __result,
57     _Comp&& __comp,
58     _Proj1&& __proj1,
59     _Proj2&& __proj2) {
60   for (; __first1 != __last1 && __first2 != __last2; ++__result) {
61     if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) {
62       *__result = *__first2;
63       ++__first2;
64     } else {
65       *__result = *__first1;
66       ++__first1;
67     }
68   }
69   auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result));
70   auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out));
71   return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)};
72 }
73 
74 struct __fn {
75   template <
76       input_iterator _InIter1,
77       sentinel_for<_InIter1> _Sent1,
78       input_iterator _InIter2,
79       sentinel_for<_InIter2> _Sent2,
80       weakly_incrementable _OutIter,
81       class _Comp  = less,
82       class _Proj1 = identity,
83       class _Proj2 = identity>
84     requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
85   _LIBCPP_HIDE_FROM_ABI constexpr merge_result<_InIter1, _InIter2, _OutIter> operator()(
86       _InIter1 __first1,
87       _Sent1 __last1,
88       _InIter2 __first2,
89       _Sent2 __last2,
90       _OutIter __result,
91       _Comp __comp   = {},
92       _Proj1 __proj1 = {},
93       _Proj2 __proj2 = {}) const {
94     return __merge::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2);
95   }
96 
97   template <
98       input_range _Range1,
99       input_range _Range2,
100       weakly_incrementable _OutIter,
101       class _Comp  = less,
102       class _Proj1 = identity,
103       class _Proj2 = identity>
104     requires mergeable<
105         iterator_t<_Range1>,
106         iterator_t<_Range2>,
107         _OutIter,
108         _Comp,
109         _Proj1,
110         _Proj2>
111   _LIBCPP_HIDE_FROM_ABI constexpr merge_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter>
112         operator()(
113             _Range1&& __range1,
114             _Range2&& __range2,
115             _OutIter __result,
116             _Comp __comp   = {},
117             _Proj1 __proj1 = {},
118             _Proj2 __proj2 = {}) const {
119     return __merge::__merge_impl(
120         ranges::begin(__range1),
121         ranges::end(__range1),
122         ranges::begin(__range2),
123         ranges::end(__range2),
124         __result,
125         __comp,
126         __proj1,
127         __proj2);
128   }
129 };
130 
131 } // namespace __merge
132 
133 inline namespace __cpo {
134   inline constexpr auto merge = __merge::__fn{};
135 } // namespace __cpo
136 } // namespace ranges
137 
138 _LIBCPP_END_NAMESPACE_STD
139 
140 #endif // _LIBCPP_STD_VER > 17
141 
142 #endif // _LIBCPP___ALGORITHM_RANGES_MERGE_H
143