xref: /llvm-project/libcxx/include/__algorithm/ranges_set_intersection.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_SET_INTERSECTION_H
10 #define _LIBCPP___ALGORITHM_RANGES_SET_INTERSECTION_H
11 
12 #include <__algorithm/in_in_out_result.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__algorithm/make_projected.h>
15 #include <__algorithm/set_intersection.h>
16 #include <__config>
17 #include <__functional/identity.h>
18 #include <__functional/invoke.h>
19 #include <__functional/ranges_operations.h>
20 #include <__iterator/concepts.h>
21 #include <__iterator/mergeable.h>
22 #include <__ranges/access.h>
23 #include <__ranges/concepts.h>
24 #include <__ranges/dangling.h>
25 #include <__utility/move.h>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 #if _LIBCPP_STD_VER >= 20
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 namespace ranges {
39 
40 template <class _InIter1, class _InIter2, class _OutIter>
41 using set_intersection_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
42 
43 struct __set_intersection {
44   template <input_iterator _InIter1,
45             sentinel_for<_InIter1> _Sent1,
46             input_iterator _InIter2,
47             sentinel_for<_InIter2> _Sent2,
48             weakly_incrementable _OutIter,
49             class _Comp  = less,
50             class _Proj1 = identity,
51             class _Proj2 = identity>
52     requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
53   _LIBCPP_HIDE_FROM_ABI constexpr set_intersection_result<_InIter1, _InIter2, _OutIter> operator()(
54       _InIter1 __first1,
55       _Sent1 __last1,
56       _InIter2 __first2,
57       _Sent2 __last2,
58       _OutIter __result,
59       _Comp __comp   = {},
60       _Proj1 __proj1 = {},
61       _Proj2 __proj2 = {}) const {
62     auto __ret = std::__set_intersection<_RangeAlgPolicy>(
63         std::move(__first1),
64         std::move(__last1),
65         std::move(__first2),
66         std::move(__last2),
67         std::move(__result),
68         ranges::__make_projected_comp(__comp, __proj1, __proj2));
69     return {std::move(__ret.__in1_), std::move(__ret.__in2_), std::move(__ret.__out_)};
70   }
71 
72   template <input_range _Range1,
73             input_range _Range2,
74             weakly_incrementable _OutIter,
75             class _Comp  = less,
76             class _Proj1 = identity,
77             class _Proj2 = identity>
78     requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2>
79   _LIBCPP_HIDE_FROM_ABI constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
80                                                           borrowed_iterator_t<_Range2>,
81                                                           _OutIter>
82   operator()(_Range1&& __range1,
83              _Range2&& __range2,
84              _OutIter __result,
85              _Comp __comp   = {},
86              _Proj1 __proj1 = {},
87              _Proj2 __proj2 = {}) const {
88     auto __ret = std::__set_intersection<_RangeAlgPolicy>(
89         ranges::begin(__range1),
90         ranges::end(__range1),
91         ranges::begin(__range2),
92         ranges::end(__range2),
93         std::move(__result),
94         ranges::__make_projected_comp(__comp, __proj1, __proj2));
95     return {std::move(__ret.__in1_), std::move(__ret.__in2_), std::move(__ret.__out_)};
96   }
97 };
98 
99 inline namespace __cpo {
100 inline constexpr auto set_intersection = __set_intersection{};
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_SET_INTERSECTION_H
111