Back to home page

EIC code displayed by LXR

 
 

    


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

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