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___CXX03___ALGORITHM_SET_INTERSECTION_H
10 #define _LIBCPP___CXX03___ALGORITHM_SET_INTERSECTION_H
11
12 #include <__cxx03/__algorithm/comp.h>
13 #include <__cxx03/__algorithm/comp_ref_type.h>
14 #include <__cxx03/__algorithm/iterator_operations.h>
15 #include <__cxx03/__algorithm/lower_bound.h>
16 #include <__cxx03/__config>
17 #include <__cxx03/__functional/identity.h>
18 #include <__cxx03/__iterator/iterator_traits.h>
19 #include <__cxx03/__iterator/next.h>
20 #include <__cxx03/__type_traits/is_same.h>
21 #include <__cxx03/__utility/move.h>
22 #include <__cxx03/__utility/swap.h>
23
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
26 #endif
27
28 _LIBCPP_PUSH_MACROS
29 #include <__cxx03/__undef_macros>
30
31 _LIBCPP_BEGIN_NAMESPACE_STD
32
33 template <class _InIter1, class _InIter2, class _OutIter>
34 struct __set_intersection_result {
35 _InIter1 __in1_;
36 _InIter2 __in2_;
37 _OutIter __out_;
38
39 // need a constructor as C++03 aggregate init is hard
__set_intersection_result__set_intersection_result40 _LIBCPP_HIDE_FROM_ABI __set_intersection_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter)
41 : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {}
42 };
43
44 // Helper for __set_intersection() with one-sided binary search: populate result and advance input iterators if they
45 // are found to potentially contain the same value in two consecutive calls. This function is very intimately related to
46 // the way it is used and doesn't attempt to abstract that, it's not appropriate for general usage outside of its
47 // context.
48 template <class _InForwardIter1, class _InForwardIter2, class _OutIter>
__set_intersection_add_output_if_equal(bool __may_be_equal,_InForwardIter1 & __first1,_InForwardIter2 & __first2,_OutIter & __result,bool & __prev_may_be_equal)49 _LIBCPP_HIDE_FROM_ABI void __set_intersection_add_output_if_equal(
50 bool __may_be_equal,
51 _InForwardIter1& __first1,
52 _InForwardIter2& __first2,
53 _OutIter& __result,
54 bool& __prev_may_be_equal) {
55 if (__may_be_equal && __prev_may_be_equal) {
56 *__result = *__first1;
57 ++__result;
58 ++__first1;
59 ++__first2;
60 __prev_may_be_equal = false;
61 } else {
62 __prev_may_be_equal = __may_be_equal;
63 }
64 }
65
66 // With forward iterators we can make multiple passes over the data, allowing the use of one-sided binary search to
67 // reduce best-case complexity to log(N). Understanding how we can use binary search and still respect complexity
68 // guarantees is _not_ straightforward: the guarantee is "at most 2*(N+M)-1 comparisons", and one-sided binary search
69 // will necessarily overshoot depending on the position of the needle in the haystack -- for instance, if we're
70 // searching for 3 in (1, 2, 3, 4), we'll check if 3<1, then 3<2, then 3<4, and, finally, 3<3, for a total of 4
71 // comparisons, when linear search would have yielded 3. However, because we won't need to perform the intervening
72 // reciprocal comparisons (ie 1<3, 2<3, 4<3), that extra comparison doesn't run afoul of the guarantee. Additionally,
73 // this type of scenario can only happen for match distances of up to 5 elements, because 2*log2(8) is 6, and we'll
74 // still be worse-off at position 5 of an 8-element set. From then onwards these scenarios can't happen. TL;DR: we'll be
75 // 1 comparison worse-off compared to the classic linear-searching algorithm if matching position 3 of a set with 4
76 // elements, or position 5 if the set has 7 or 8 elements, but we'll never exceed the complexity guarantees from the
77 // standard.
78 template <class _AlgPolicy,
79 class _Compare,
80 class _InForwardIter1,
81 class _Sent1,
82 class _InForwardIter2,
83 class _Sent2,
84 class _OutIter>
85 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>
__set_intersection(_InForwardIter1 __first1,_Sent1 __last1,_InForwardIter2 __first2,_Sent2 __last2,_OutIter __result,_Compare && __comp,std::forward_iterator_tag,std::forward_iterator_tag)86 __set_intersection(
87 _InForwardIter1 __first1,
88 _Sent1 __last1,
89 _InForwardIter2 __first2,
90 _Sent2 __last2,
91 _OutIter __result,
92 _Compare&& __comp,
93 std::forward_iterator_tag,
94 std::forward_iterator_tag) {
95 std::__identity __proj;
96 bool __prev_may_be_equal = false;
97
98 while (__first2 != __last2) {
99 _InForwardIter1 __first1_next =
100 std::__lower_bound_onesided<_AlgPolicy>(__first1, __last1, *__first2, __comp, __proj);
101 std::swap(__first1_next, __first1);
102 // keeping in mind that a==b iff !(a<b) && !(b<a):
103 // if we can't advance __first1, that means !(*__first1 < *_first2), therefore __may_be_equal==true
104 std::__set_intersection_add_output_if_equal(
105 __first1 == __first1_next, __first1, __first2, __result, __prev_may_be_equal);
106 if (__first1 == __last1)
107 break;
108
109 _InForwardIter2 __first2_next =
110 std::__lower_bound_onesided<_AlgPolicy>(__first2, __last2, *__first1, __comp, __proj);
111 std::swap(__first2_next, __first2);
112 std::__set_intersection_add_output_if_equal(
113 __first2 == __first2_next, __first1, __first2, __result, __prev_may_be_equal);
114 }
115 return __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>(
116 _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)),
117 _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)),
118 std::move(__result));
119 }
120
121 // input iterators are not suitable for multipass algorithms, so we stick to the classic single-pass version
122 template <class _AlgPolicy,
123 class _Compare,
124 class _InInputIter1,
125 class _Sent1,
126 class _InInputIter2,
127 class _Sent2,
128 class _OutIter>
129 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>
__set_intersection(_InInputIter1 __first1,_Sent1 __last1,_InInputIter2 __first2,_Sent2 __last2,_OutIter __result,_Compare && __comp,std::input_iterator_tag,std::input_iterator_tag)130 __set_intersection(
131 _InInputIter1 __first1,
132 _Sent1 __last1,
133 _InInputIter2 __first2,
134 _Sent2 __last2,
135 _OutIter __result,
136 _Compare&& __comp,
137 std::input_iterator_tag,
138 std::input_iterator_tag) {
139 while (__first1 != __last1 && __first2 != __last2) {
140 if (__comp(*__first1, *__first2))
141 ++__first1;
142 else {
143 if (!__comp(*__first2, *__first1)) {
144 *__result = *__first1;
145 ++__result;
146 ++__first1;
147 }
148 ++__first2;
149 }
150 }
151
152 return __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>(
153 _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)),
154 _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)),
155 std::move(__result));
156 }
157
158 template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
__set_intersection(_InIter1 __first1,_Sent1 __last1,_InIter2 __first2,_Sent2 __last2,_OutIter __result,_Compare && __comp)159 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI __set_intersection_result<_InIter1, _InIter2, _OutIter> __set_intersection(
160 _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
161 return std::__set_intersection<_AlgPolicy>(
162 std::move(__first1),
163 std::move(__last1),
164 std::move(__first2),
165 std::move(__last2),
166 std::move(__result),
167 std::forward<_Compare>(__comp),
168 typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter1>(),
169 typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter2>());
170 }
171
172 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
set_intersection(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_InputIterator2 __last2,_OutputIterator __result,_Compare __comp)173 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator set_intersection(
174 _InputIterator1 __first1,
175 _InputIterator1 __last1,
176 _InputIterator2 __first2,
177 _InputIterator2 __last2,
178 _OutputIterator __result,
179 _Compare __comp) {
180 return std::__set_intersection<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
181 std::move(__first1),
182 std::move(__last1),
183 std::move(__first2),
184 std::move(__last2),
185 std::move(__result),
186 __comp)
187 .__out_;
188 }
189
190 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
set_intersection(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_InputIterator2 __last2,_OutputIterator __result)191 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator set_intersection(
192 _InputIterator1 __first1,
193 _InputIterator1 __last1,
194 _InputIterator2 __first2,
195 _InputIterator2 __last2,
196 _OutputIterator __result) {
197 return std::__set_intersection<_ClassicAlgPolicy>(
198 std::move(__first1),
199 std::move(__last1),
200 std::move(__first2),
201 std::move(__last2),
202 std::move(__result),
203 __less<>())
204 .__out_;
205 }
206
207 _LIBCPP_END_NAMESPACE_STD
208
209 _LIBCPP_POP_MACROS
210
211 #endif // _LIBCPP___CXX03___ALGORITHM_SET_INTERSECTION_H
212