File indexing completed on 2026-05-03 08:13:20
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_MERGE_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_MERGE_H
0011
0012 #include <__cxx03/__algorithm/in_in_out_result.h>
0013 #include <__cxx03/__algorithm/ranges_copy.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__functional/identity.h>
0016 #include <__cxx03/__functional/invoke.h>
0017 #include <__cxx03/__functional/ranges_operations.h>
0018 #include <__cxx03/__iterator/concepts.h>
0019 #include <__cxx03/__iterator/mergeable.h>
0020 #include <__cxx03/__ranges/access.h>
0021 #include <__cxx03/__ranges/concepts.h>
0022 #include <__cxx03/__ranges/dangling.h>
0023 #include <__cxx03/__type_traits/remove_cvref.h>
0024 #include <__cxx03/__utility/move.h>
0025
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 # pragma GCC system_header
0028 #endif
0029
0030 _LIBCPP_PUSH_MACROS
0031 #include <__cxx03/__undef_macros>
0032
0033 #if _LIBCPP_STD_VER >= 20
0034
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036
0037 namespace ranges {
0038
0039 template <class _InIter1, class _InIter2, class _OutIter>
0040 using merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
0041
0042 namespace __merge {
0043
0044 template < class _InIter1,
0045 class _Sent1,
0046 class _InIter2,
0047 class _Sent2,
0048 class _OutIter,
0049 class _Comp,
0050 class _Proj1,
0051 class _Proj2>
0052 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<__remove_cvref_t<_InIter1>,
0053 __remove_cvref_t<_InIter2>,
0054 __remove_cvref_t<_OutIter>>
0055 __merge_impl(_InIter1&& __first1,
0056 _Sent1&& __last1,
0057 _InIter2&& __first2,
0058 _Sent2&& __last2,
0059 _OutIter&& __result,
0060 _Comp&& __comp,
0061 _Proj1&& __proj1,
0062 _Proj2&& __proj2) {
0063 for (; __first1 != __last1 && __first2 != __last2; ++__result) {
0064 if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) {
0065 *__result = *__first2;
0066 ++__first2;
0067 } else {
0068 *__result = *__first1;
0069 ++__first1;
0070 }
0071 }
0072 auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result));
0073 auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out));
0074 return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)};
0075 }
0076
0077 struct __fn {
0078 template <input_iterator _InIter1,
0079 sentinel_for<_InIter1> _Sent1,
0080 input_iterator _InIter2,
0081 sentinel_for<_InIter2> _Sent2,
0082 weakly_incrementable _OutIter,
0083 class _Comp = less,
0084 class _Proj1 = identity,
0085 class _Proj2 = identity>
0086 requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
0087 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<_InIter1, _InIter2, _OutIter> operator()(
0088 _InIter1 __first1,
0089 _Sent1 __last1,
0090 _InIter2 __first2,
0091 _Sent2 __last2,
0092 _OutIter __result,
0093 _Comp __comp = {},
0094 _Proj1 __proj1 = {},
0095 _Proj2 __proj2 = {}) const {
0096 return __merge::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2);
0097 }
0098
0099 template <input_range _Range1,
0100 input_range _Range2,
0101 weakly_incrementable _OutIter,
0102 class _Comp = less,
0103 class _Proj1 = identity,
0104 class _Proj2 = identity>
0105 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2>
0106 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter>
0107 operator()(_Range1&& __range1,
0108 _Range2&& __range2,
0109 _OutIter __result,
0110 _Comp __comp = {},
0111 _Proj1 __proj1 = {},
0112 _Proj2 __proj2 = {}) const {
0113 return __merge::__merge_impl(
0114 ranges::begin(__range1),
0115 ranges::end(__range1),
0116 ranges::begin(__range2),
0117 ranges::end(__range2),
0118 __result,
0119 __comp,
0120 __proj1,
0121 __proj2);
0122 }
0123 };
0124
0125 }
0126
0127 inline namespace __cpo {
0128 inline constexpr auto merge = __merge::__fn{};
0129 }
0130 }
0131
0132 _LIBCPP_END_NAMESPACE_STD
0133
0134 #endif
0135
0136 _LIBCPP_POP_MACROS
0137
0138 #endif