[CodeGenPrepare] Drop nsw flags in `optimizeLoadExt` (#118180)
[llvm-project.git] / libcxx / include / __algorithm / ranges_merge.h
blobf3e0486fe488e95c42cca22625a212a473ffd0ea
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_MERGE_H
10 #define _LIBCPP___ALGORITHM_RANGES_MERGE_H
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 <__type_traits/remove_cvref.h>
24 #include <__utility/move.h>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
28 #endif
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
33 #if _LIBCPP_STD_VER >= 20
35 _LIBCPP_BEGIN_NAMESPACE_STD
37 namespace ranges {
39 template <class _InIter1, class _InIter2, class _OutIter>
40 using merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
42 struct __merge {
43 template <input_iterator _InIter1,
44 sentinel_for<_InIter1> _Sent1,
45 input_iterator _InIter2,
46 sentinel_for<_InIter2> _Sent2,
47 weakly_incrementable _OutIter,
48 class _Comp = less,
49 class _Proj1 = identity,
50 class _Proj2 = identity>
51 requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
52 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<_InIter1, _InIter2, _OutIter> operator()(
53 _InIter1 __first1,
54 _Sent1 __last1,
55 _InIter2 __first2,
56 _Sent2 __last2,
57 _OutIter __result,
58 _Comp __comp = {},
59 _Proj1 __proj1 = {},
60 _Proj2 __proj2 = {}) const {
61 return __merge::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2);
64 template <input_range _Range1,
65 input_range _Range2,
66 weakly_incrementable _OutIter,
67 class _Comp = less,
68 class _Proj1 = identity,
69 class _Proj2 = identity>
70 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2>
71 _LIBCPP_HIDE_FROM_ABI constexpr merge_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter>
72 operator()(_Range1&& __range1,
73 _Range2&& __range2,
74 _OutIter __result,
75 _Comp __comp = {},
76 _Proj1 __proj1 = {},
77 _Proj2 __proj2 = {}) const {
78 return __merge::__merge_impl(
79 ranges::begin(__range1),
80 ranges::end(__range1),
81 ranges::begin(__range2),
82 ranges::end(__range2),
83 __result,
84 __comp,
85 __proj1,
86 __proj2);
89 template < class _InIter1,
90 class _Sent1,
91 class _InIter2,
92 class _Sent2,
93 class _OutIter,
94 class _Comp,
95 class _Proj1,
96 class _Proj2>
97 _LIBCPP_HIDE_FROM_ABI static constexpr merge_result<__remove_cvref_t<_InIter1>,
98 __remove_cvref_t<_InIter2>,
99 __remove_cvref_t<_OutIter>>
100 __merge_impl(_InIter1&& __first1,
101 _Sent1&& __last1,
102 _InIter2&& __first2,
103 _Sent2&& __last2,
104 _OutIter&& __result,
105 _Comp&& __comp,
106 _Proj1&& __proj1,
107 _Proj2&& __proj2) {
108 for (; __first1 != __last1 && __first2 != __last2; ++__result) {
109 if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) {
110 *__result = *__first2;
111 ++__first2;
112 } else {
113 *__result = *__first1;
114 ++__first1;
117 auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result));
118 auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out));
119 return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)};
123 inline namespace __cpo {
124 inline constexpr auto merge = __merge{};
125 } // namespace __cpo
126 } // namespace ranges
128 _LIBCPP_END_NAMESPACE_STD
130 #endif // _LIBCPP_STD_VER >= 20
132 _LIBCPP_POP_MACROS
134 #endif // _LIBCPP___ALGORITHM_RANGES_MERGE_H