Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:12

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_SET_INTERSECTION_H
0010 #define _LIBCPP___ALGORITHM_SET_INTERSECTION_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/comp_ref_type.h>
0014 #include <__algorithm/iterator_operations.h>
0015 #include <__algorithm/lower_bound.h>
0016 #include <__config>
0017 #include <__functional/identity.h>
0018 #include <__iterator/iterator_traits.h>
0019 #include <__iterator/next.h>
0020 #include <__type_traits/is_same.h>
0021 #include <__utility/exchange.h>
0022 #include <__utility/forward.h>
0023 #include <__utility/move.h>
0024 #include <__utility/swap.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 <__undef_macros>
0032 
0033 _LIBCPP_BEGIN_NAMESPACE_STD
0034 
0035 template <class _InIter1, class _InIter2, class _OutIter>
0036 struct __set_intersection_result {
0037   _InIter1 __in1_;
0038   _InIter2 __in2_;
0039   _OutIter __out_;
0040 
0041   // need a constructor as C++03 aggregate init is hard
0042   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0043   __set_intersection_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter)
0044       : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {}
0045 };
0046 
0047 // Helper for __set_intersection() with one-sided binary search: populate result and advance input iterators if they
0048 // are found to potentially contain the same value in two consecutive calls. This function is very intimately related to
0049 // the way it is used and doesn't attempt to abstract that, it's not appropriate for general usage outside of its
0050 // context.
0051 template <class _InForwardIter1, class _InForwardIter2, class _OutIter>
0052 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_intersection_add_output_if_equal(
0053     bool __may_be_equal,
0054     _InForwardIter1& __first1,
0055     _InForwardIter2& __first2,
0056     _OutIter& __result,
0057     bool& __prev_may_be_equal) {
0058   if (__may_be_equal && __prev_may_be_equal) {
0059     *__result = *__first1;
0060     ++__result;
0061     ++__first1;
0062     ++__first2;
0063     __prev_may_be_equal = false;
0064   } else {
0065     __prev_may_be_equal = __may_be_equal;
0066   }
0067 }
0068 
0069 // With forward iterators we can make multiple passes over the data, allowing the use of one-sided binary search to
0070 // reduce best-case complexity to log(N). Understanding how we can use binary search and still respect complexity
0071 // guarantees is _not_ straightforward: the guarantee is "at most 2*(N+M)-1 comparisons", and one-sided binary search
0072 // will necessarily overshoot depending on the position of the needle in the haystack -- for instance, if we're
0073 // 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
0074 // comparisons, when linear search would have yielded 3. However, because we won't need to perform the intervening
0075 // reciprocal comparisons (ie 1<3, 2<3, 4<3), that extra comparison doesn't run afoul of the guarantee. Additionally,
0076 // this type of scenario can only happen for match distances of up to 5 elements, because 2*log2(8) is 6, and we'll
0077 // 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
0078 // 1 comparison worse-off compared to the classic linear-searching algorithm if matching position 3 of a set with 4
0079 // elements, or position 5 if the set has 7 or 8 elements, but we'll never exceed the complexity guarantees from the
0080 // standard.
0081 template <class _AlgPolicy,
0082           class _Compare,
0083           class _InForwardIter1,
0084           class _Sent1,
0085           class _InForwardIter2,
0086           class _Sent2,
0087           class _OutIter>
0088 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
0089 _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>
0090 __set_intersection(
0091     _InForwardIter1 __first1,
0092     _Sent1 __last1,
0093     _InForwardIter2 __first2,
0094     _Sent2 __last2,
0095     _OutIter __result,
0096     _Compare&& __comp,
0097     std::forward_iterator_tag,
0098     std::forward_iterator_tag) {
0099   _LIBCPP_CONSTEXPR std::__identity __proj;
0100   bool __prev_may_be_equal = false;
0101 
0102   while (__first2 != __last2) {
0103     _InForwardIter1 __first1_next =
0104         std::__lower_bound_onesided<_AlgPolicy>(__first1, __last1, *__first2, __comp, __proj);
0105     std::swap(__first1_next, __first1);
0106     // keeping in mind that a==b iff !(a<b) && !(b<a):
0107     // if we can't advance __first1, that means !(*__first1 < *_first2), therefore __may_be_equal==true
0108     std::__set_intersection_add_output_if_equal(
0109         __first1 == __first1_next, __first1, __first2, __result, __prev_may_be_equal);
0110     if (__first1 == __last1)
0111       break;
0112 
0113     _InForwardIter2 __first2_next =
0114         std::__lower_bound_onesided<_AlgPolicy>(__first2, __last2, *__first1, __comp, __proj);
0115     std::swap(__first2_next, __first2);
0116     std::__set_intersection_add_output_if_equal(
0117         __first2 == __first2_next, __first1, __first2, __result, __prev_may_be_equal);
0118   }
0119   return __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>(
0120       _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)),
0121       _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)),
0122       std::move(__result));
0123 }
0124 
0125 // input iterators are not suitable for multipass algorithms, so we stick to the classic single-pass version
0126 template <class _AlgPolicy,
0127           class _Compare,
0128           class _InInputIter1,
0129           class _Sent1,
0130           class _InInputIter2,
0131           class _Sent2,
0132           class _OutIter>
0133 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
0134 _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>
0135 __set_intersection(
0136     _InInputIter1 __first1,
0137     _Sent1 __last1,
0138     _InInputIter2 __first2,
0139     _Sent2 __last2,
0140     _OutIter __result,
0141     _Compare&& __comp,
0142     std::input_iterator_tag,
0143     std::input_iterator_tag) {
0144   while (__first1 != __last1 && __first2 != __last2) {
0145     if (__comp(*__first1, *__first2))
0146       ++__first1;
0147     else {
0148       if (!__comp(*__first2, *__first1)) {
0149         *__result = *__first1;
0150         ++__result;
0151         ++__first1;
0152       }
0153       ++__first2;
0154     }
0155   }
0156 
0157   return __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>(
0158       _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)),
0159       _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)),
0160       std::move(__result));
0161 }
0162 
0163 template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
0164 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
0165 _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
0166 __set_intersection(
0167     _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
0168   return std::__set_intersection<_AlgPolicy>(
0169       std::move(__first1),
0170       std::move(__last1),
0171       std::move(__first2),
0172       std::move(__last2),
0173       std::move(__result),
0174       std::forward<_Compare>(__comp),
0175       typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter1>(),
0176       typename std::_IterOps<_AlgPolicy>::template __iterator_category<_InIter2>());
0177 }
0178 
0179 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
0180 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection(
0181     _InputIterator1 __first1,
0182     _InputIterator1 __last1,
0183     _InputIterator2 __first2,
0184     _InputIterator2 __last2,
0185     _OutputIterator __result,
0186     _Compare __comp) {
0187   return std::__set_intersection<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
0188              std::move(__first1),
0189              std::move(__last1),
0190              std::move(__first2),
0191              std::move(__last2),
0192              std::move(__result),
0193              __comp)
0194       .__out_;
0195 }
0196 
0197 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
0198 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection(
0199     _InputIterator1 __first1,
0200     _InputIterator1 __last1,
0201     _InputIterator2 __first2,
0202     _InputIterator2 __last2,
0203     _OutputIterator __result) {
0204   return std::__set_intersection<_ClassicAlgPolicy>(
0205              std::move(__first1),
0206              std::move(__last1),
0207              std::move(__first2),
0208              std::move(__last2),
0209              std::move(__result),
0210              __less<>())
0211       .__out_;
0212 }
0213 
0214 _LIBCPP_END_NAMESPACE_STD
0215 
0216 _LIBCPP_POP_MACROS
0217 
0218 #endif // _LIBCPP___ALGORITHM_SET_INTERSECTION_H