1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_SET_INTERSECTION_H 10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_SET_INTERSECTION_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include <__algorithm/comp.h> 13fe6060f1SDimitry Andric #include <__algorithm/comp_ref_type.h> 14753f127fSDimitry Andric #include <__algorithm/iterator_operations.h> 15*0fca6ea1SDimitry Andric #include <__algorithm/lower_bound.h> 1604eeddc0SDimitry Andric #include <__config> 17*0fca6ea1SDimitry Andric #include <__functional/identity.h> 18fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h> 19753f127fSDimitry Andric #include <__iterator/next.h> 20*0fca6ea1SDimitry Andric #include <__type_traits/is_same.h> 21*0fca6ea1SDimitry Andric #include <__utility/exchange.h> 22753f127fSDimitry Andric #include <__utility/move.h> 23*0fca6ea1SDimitry Andric #include <__utility/swap.h> 24fe6060f1SDimitry Andric 25fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 26fe6060f1SDimitry Andric # pragma GCC system_header 27fe6060f1SDimitry Andric #endif 28fe6060f1SDimitry Andric 29b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS 30b3edf446SDimitry Andric #include <__undef_macros> 31b3edf446SDimitry Andric 32fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 33fe6060f1SDimitry Andric 34753f127fSDimitry Andric template <class _InIter1, class _InIter2, class _OutIter> 35753f127fSDimitry Andric struct __set_intersection_result { 36753f127fSDimitry Andric _InIter1 __in1_; 37753f127fSDimitry Andric _InIter2 __in2_; 38753f127fSDimitry Andric _OutIter __out_; 39753f127fSDimitry Andric 40753f127fSDimitry Andric // need a constructor as C++03 aggregate init is hard 41bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 42753f127fSDimitry Andric __set_intersection_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter) 43753f127fSDimitry Andric : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {} 44753f127fSDimitry Andric }; 45753f127fSDimitry Andric 46*0fca6ea1SDimitry Andric // Helper for __set_intersection() with one-sided binary search: populate result and advance input iterators if they 47*0fca6ea1SDimitry Andric // are found to potentially contain the same value in two consecutive calls. This function is very intimately related to 48*0fca6ea1SDimitry Andric // the way it is used and doesn't attempt to abstract that, it's not appropriate for general usage outside of its 49*0fca6ea1SDimitry Andric // context. 50*0fca6ea1SDimitry Andric template <class _InForwardIter1, class _InForwardIter2, class _OutIter> 51*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_intersection_add_output_if_equal( 52*0fca6ea1SDimitry Andric bool __may_be_equal, 53*0fca6ea1SDimitry Andric _InForwardIter1& __first1, 54*0fca6ea1SDimitry Andric _InForwardIter2& __first2, 55*0fca6ea1SDimitry Andric _OutIter& __result, 56*0fca6ea1SDimitry Andric bool& __prev_may_be_equal) { 57*0fca6ea1SDimitry Andric if (__may_be_equal && __prev_may_be_equal) { 58*0fca6ea1SDimitry Andric *__result = *__first1; 59*0fca6ea1SDimitry Andric ++__result; 60*0fca6ea1SDimitry Andric ++__first1; 61*0fca6ea1SDimitry Andric ++__first2; 62*0fca6ea1SDimitry Andric __prev_may_be_equal = false; 63*0fca6ea1SDimitry Andric } else { 64*0fca6ea1SDimitry Andric __prev_may_be_equal = __may_be_equal; 65*0fca6ea1SDimitry Andric } 66*0fca6ea1SDimitry Andric } 67*0fca6ea1SDimitry Andric 68*0fca6ea1SDimitry Andric // With forward iterators we can make multiple passes over the data, allowing the use of one-sided binary search to 69*0fca6ea1SDimitry Andric // reduce best-case complexity to log(N). Understanding how we can use binary search and still respect complexity 70*0fca6ea1SDimitry Andric // guarantees is _not_ straightforward: the guarantee is "at most 2*(N+M)-1 comparisons", and one-sided binary search 71*0fca6ea1SDimitry Andric // will necessarily overshoot depending on the position of the needle in the haystack -- for instance, if we're 72*0fca6ea1SDimitry Andric // 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 73*0fca6ea1SDimitry Andric // comparisons, when linear search would have yielded 3. However, because we won't need to perform the intervening 74*0fca6ea1SDimitry Andric // reciprocal comparisons (ie 1<3, 2<3, 4<3), that extra comparison doesn't run afoul of the guarantee. Additionally, 75*0fca6ea1SDimitry Andric // this type of scenario can only happen for match distances of up to 5 elements, because 2*log2(8) is 6, and we'll 76*0fca6ea1SDimitry Andric // 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 77*0fca6ea1SDimitry Andric // 1 comparison worse-off compared to the classic linear-searching algorithm if matching position 3 of a set with 4 78*0fca6ea1SDimitry Andric // elements, or position 5 if the set has 7 or 8 elements, but we'll never exceed the complexity guarantees from the 79*0fca6ea1SDimitry Andric // standard. 80*0fca6ea1SDimitry Andric template <class _AlgPolicy, 81*0fca6ea1SDimitry Andric class _Compare, 82*0fca6ea1SDimitry Andric class _InForwardIter1, 83*0fca6ea1SDimitry Andric class _Sent1, 84*0fca6ea1SDimitry Andric class _InForwardIter2, 85*0fca6ea1SDimitry Andric class _Sent2, 86*0fca6ea1SDimitry Andric class _OutIter> 87*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI 88*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter> 89753f127fSDimitry Andric __set_intersection( 90*0fca6ea1SDimitry Andric _InForwardIter1 __first1, 91*0fca6ea1SDimitry Andric _Sent1 __last1, 92*0fca6ea1SDimitry Andric _InForwardIter2 __first2, 93*0fca6ea1SDimitry Andric _Sent2 __last2, 94*0fca6ea1SDimitry Andric _OutIter __result, 95*0fca6ea1SDimitry Andric _Compare&& __comp, 96*0fca6ea1SDimitry Andric std::forward_iterator_tag, 97*0fca6ea1SDimitry Andric std::forward_iterator_tag) { 98*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR std::__identity __proj; 99*0fca6ea1SDimitry Andric bool __prev_may_be_equal = false; 100*0fca6ea1SDimitry Andric 101*0fca6ea1SDimitry Andric while (__first2 != __last2) { 102*0fca6ea1SDimitry Andric _InForwardIter1 __first1_next = 103*0fca6ea1SDimitry Andric std::__lower_bound_onesided<_AlgPolicy>(__first1, __last1, *__first2, __comp, __proj); 104*0fca6ea1SDimitry Andric std::swap(__first1_next, __first1); 105*0fca6ea1SDimitry Andric // keeping in mind that a==b iff !(a<b) && !(b<a): 106*0fca6ea1SDimitry Andric // if we can't advance __first1, that means !(*__first1 < *_first2), therefore __may_be_equal==true 107*0fca6ea1SDimitry Andric std::__set_intersection_add_output_if_equal( 108*0fca6ea1SDimitry Andric __first1 == __first1_next, __first1, __first2, __result, __prev_may_be_equal); 109*0fca6ea1SDimitry Andric if (__first1 == __last1) 110*0fca6ea1SDimitry Andric break; 111*0fca6ea1SDimitry Andric 112*0fca6ea1SDimitry Andric _InForwardIter2 __first2_next = 113*0fca6ea1SDimitry Andric std::__lower_bound_onesided<_AlgPolicy>(__first2, __last2, *__first1, __comp, __proj); 114*0fca6ea1SDimitry Andric std::swap(__first2_next, __first2); 115*0fca6ea1SDimitry Andric std::__set_intersection_add_output_if_equal( 116*0fca6ea1SDimitry Andric __first2 == __first2_next, __first1, __first2, __result, __prev_may_be_equal); 117*0fca6ea1SDimitry Andric } 118*0fca6ea1SDimitry Andric return __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>( 119*0fca6ea1SDimitry Andric _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)), 120*0fca6ea1SDimitry Andric _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)), 121*0fca6ea1SDimitry Andric std::move(__result)); 122*0fca6ea1SDimitry Andric } 123*0fca6ea1SDimitry Andric 124*0fca6ea1SDimitry Andric // input iterators are not suitable for multipass algorithms, so we stick to the classic single-pass version 125*0fca6ea1SDimitry Andric template <class _AlgPolicy, 126*0fca6ea1SDimitry Andric class _Compare, 127*0fca6ea1SDimitry Andric class _InInputIter1, 128*0fca6ea1SDimitry Andric class _Sent1, 129*0fca6ea1SDimitry Andric class _InInputIter2, 130*0fca6ea1SDimitry Andric class _Sent2, 131*0fca6ea1SDimitry Andric class _OutIter> 132*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI 133*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter> 134*0fca6ea1SDimitry Andric __set_intersection( 135*0fca6ea1SDimitry Andric _InInputIter1 __first1, 136*0fca6ea1SDimitry Andric _Sent1 __last1, 137*0fca6ea1SDimitry Andric _InInputIter2 __first2, 138*0fca6ea1SDimitry Andric _Sent2 __last2, 139*0fca6ea1SDimitry Andric _OutIter __result, 140*0fca6ea1SDimitry Andric _Compare&& __comp, 141*0fca6ea1SDimitry Andric std::input_iterator_tag, 142*0fca6ea1SDimitry Andric std::input_iterator_tag) { 143753f127fSDimitry Andric while (__first1 != __last1 && __first2 != __last2) { 144fe6060f1SDimitry Andric if (__comp(*__first1, *__first2)) 145fe6060f1SDimitry Andric ++__first1; 146753f127fSDimitry Andric else { 147753f127fSDimitry Andric if (!__comp(*__first2, *__first1)) { 148fe6060f1SDimitry Andric *__result = *__first1; 149fe6060f1SDimitry Andric ++__result; 150fe6060f1SDimitry Andric ++__first1; 151fe6060f1SDimitry Andric } 152fe6060f1SDimitry Andric ++__first2; 153fe6060f1SDimitry Andric } 154fe6060f1SDimitry Andric } 155753f127fSDimitry Andric 156*0fca6ea1SDimitry Andric return __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>( 157753f127fSDimitry Andric _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)), 158753f127fSDimitry Andric _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)), 159753f127fSDimitry Andric std::move(__result)); 160fe6060f1SDimitry Andric } 161fe6060f1SDimitry Andric 162*0fca6ea1SDimitry Andric template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter> 163*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI 164*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter> 165*0fca6ea1SDimitry Andric __set_intersection( 166*0fca6ea1SDimitry Andric _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) { 167*0fca6ea1SDimitry Andric return std::__set_intersection<_AlgPolicy>( 168*0fca6ea1SDimitry Andric std::move(__first1), 169*0fca6ea1SDimitry Andric std::move(__last1), 170*0fca6ea1SDimitry Andric std::move(__first2), 171*0fca6ea1SDimitry Andric std::move(__last2), 172*0fca6ea1SDimitry Andric std::move(__result), 173*0fca6ea1SDimitry Andric std::forward<_Compare>(__comp), 174*0fca6ea1SDimitry Andric typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter1>(), 175*0fca6ea1SDimitry Andric typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter2>()); 176*0fca6ea1SDimitry Andric } 177*0fca6ea1SDimitry Andric 178fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 179bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection( 180753f127fSDimitry Andric _InputIterator1 __first1, 181753f127fSDimitry Andric _InputIterator1 __last1, 182753f127fSDimitry Andric _InputIterator2 __first2, 183753f127fSDimitry Andric _InputIterator2 __last2, 184753f127fSDimitry Andric _OutputIterator __result, 185753f127fSDimitry Andric _Compare __comp) { 186bdd1243dSDimitry Andric return std::__set_intersection<_ClassicAlgPolicy, __comp_ref_type<_Compare> >( 187753f127fSDimitry Andric std::move(__first1), 188753f127fSDimitry Andric std::move(__last1), 189753f127fSDimitry Andric std::move(__first2), 190753f127fSDimitry Andric std::move(__last2), 191753f127fSDimitry Andric std::move(__result), 192753f127fSDimitry Andric __comp) 193753f127fSDimitry Andric .__out_; 194fe6060f1SDimitry Andric } 195fe6060f1SDimitry Andric 196fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2, class _OutputIterator> 197bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection( 198753f127fSDimitry Andric _InputIterator1 __first1, 199753f127fSDimitry Andric _InputIterator1 __last1, 200753f127fSDimitry Andric _InputIterator2 __first2, 201753f127fSDimitry Andric _InputIterator2 __last2, 202753f127fSDimitry Andric _OutputIterator __result) { 203753f127fSDimitry Andric return std::__set_intersection<_ClassicAlgPolicy>( 204753f127fSDimitry Andric std::move(__first1), 205753f127fSDimitry Andric std::move(__last1), 206753f127fSDimitry Andric std::move(__first2), 207753f127fSDimitry Andric std::move(__last2), 208753f127fSDimitry Andric std::move(__result), 20906c3fb27SDimitry Andric __less<>()) 210753f127fSDimitry Andric .__out_; 211fe6060f1SDimitry Andric } 212fe6060f1SDimitry Andric 213fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 214fe6060f1SDimitry Andric 215b3edf446SDimitry Andric _LIBCPP_POP_MACROS 216b3edf446SDimitry Andric 217fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_SET_INTERSECTION_H 218