1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_MERGE_H
10 #define _LIBCPP___CXX03___ALGORITHM_RANGES_MERGE_H
12 #include <__cxx03/__algorithm/in_in_out_result.h>
13 #include <__cxx03/__algorithm/ranges_copy.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__functional/identity.h>
16 #include <__cxx03/__functional/invoke.h>
17 #include <__cxx03/__functional/ranges_operations.h>
18 #include <__cxx03/__iterator/concepts.h>
19 #include <__cxx03/__iterator/mergeable.h>
20 #include <__cxx03/__ranges/access.h>
21 #include <__cxx03/__ranges/concepts.h>
22 #include <__cxx03/__ranges/dangling.h>
23 #include <__cxx03/__type_traits/remove_cvref.h>
24 #include <__cxx03/__utility/move.h>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
31 #include <__cxx03/__undef_macros>
33 #if _LIBCPP_STD_VER >= 20
35 _LIBCPP_BEGIN_NAMESPACE_STD
39 template <class _InIter1
, class _InIter2
, class _OutIter
>
40 using merge_result
= in_in_out_result
<_InIter1
, _InIter2
, _OutIter
>;
44 template < class _InIter1
,
52 _LIBCPP_HIDE_FROM_ABI
constexpr merge_result
<__remove_cvref_t
<_InIter1
>,
53 __remove_cvref_t
<_InIter2
>,
54 __remove_cvref_t
<_OutIter
>>
55 __merge_impl(_InIter1
&& __first1
,
63 for (; __first1
!= __last1
&& __first2
!= __last2
; ++__result
) {
64 if (std::invoke(__comp
, std::invoke(__proj2
, *__first2
), std::invoke(__proj1
, *__first1
))) {
65 *__result
= *__first2
;
68 *__result
= *__first1
;
72 auto __ret1
= ranges::copy(std::move(__first1
), std::move(__last1
), std::move(__result
));
73 auto __ret2
= ranges::copy(std::move(__first2
), std::move(__last2
), std::move(__ret1
.out
));
74 return {std::move(__ret1
.in
), std::move(__ret2
.in
), std::move(__ret2
.out
)};
78 template <input_iterator _InIter1
,
79 sentinel_for
<_InIter1
> _Sent1
,
80 input_iterator _InIter2
,
81 sentinel_for
<_InIter2
> _Sent2
,
82 weakly_incrementable _OutIter
,
84 class _Proj1
= identity
,
85 class _Proj2
= identity
>
86 requires mergeable
<_InIter1
, _InIter2
, _OutIter
, _Comp
, _Proj1
, _Proj2
>
87 _LIBCPP_HIDE_FROM_ABI
constexpr merge_result
<_InIter1
, _InIter2
, _OutIter
> operator()(
95 _Proj2 __proj2
= {}) const {
96 return __merge::__merge_impl(__first1
, __last1
, __first2
, __last2
, __result
, __comp
, __proj1
, __proj2
);
99 template <input_range _Range1
,
101 weakly_incrementable _OutIter
,
103 class _Proj1
= identity
,
104 class _Proj2
= identity
>
105 requires mergeable
<iterator_t
<_Range1
>, iterator_t
<_Range2
>, _OutIter
, _Comp
, _Proj1
, _Proj2
>
106 _LIBCPP_HIDE_FROM_ABI
constexpr merge_result
<borrowed_iterator_t
<_Range1
>, borrowed_iterator_t
<_Range2
>, _OutIter
>
107 operator()(_Range1
&& __range1
,
112 _Proj2 __proj2
= {}) const {
113 return __merge::__merge_impl(
114 ranges::begin(__range1
),
115 ranges::end(__range1
),
116 ranges::begin(__range2
),
117 ranges::end(__range2
),
125 } // namespace __merge
127 inline namespace __cpo
{
128 inline constexpr auto merge
= __merge::__fn
{};
130 } // namespace ranges
132 _LIBCPP_END_NAMESPACE_STD
134 #endif // _LIBCPP_STD_VER >= 20
138 #endif // _LIBCPP___CXX03___ALGORITHM_RANGES_MERGE_H