set_intersection.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef _LIBCPP___ALGORITHM_SET_INTERSECTION_H
  9. #define _LIBCPP___ALGORITHM_SET_INTERSECTION_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__config>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__iterator/next.h>
  16. #include <__utility/move.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template <class _InIter1, class _InIter2, class _OutIter>
  24. struct __set_intersection_result {
  25. _InIter1 __in1_;
  26. _InIter2 __in2_;
  27. _OutIter __out_;
  28. // need a constructor as C++03 aggregate init is hard
  29. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  30. __set_intersection_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter)
  31. : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {}
  32. };
  33. template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
  34. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
  35. __set_intersection(
  36. _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
  37. while (__first1 != __last1 && __first2 != __last2) {
  38. if (__comp(*__first1, *__first2))
  39. ++__first1;
  40. else {
  41. if (!__comp(*__first2, *__first1)) {
  42. *__result = *__first1;
  43. ++__result;
  44. ++__first1;
  45. }
  46. ++__first2;
  47. }
  48. }
  49. return __set_intersection_result<_InIter1, _InIter2, _OutIter>(
  50. _IterOps<_AlgPolicy>::next(std::move(__first1), std::move(__last1)),
  51. _IterOps<_AlgPolicy>::next(std::move(__first2), std::move(__last2)),
  52. std::move(__result));
  53. }
  54. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  55. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection(
  56. _InputIterator1 __first1,
  57. _InputIterator1 __last1,
  58. _InputIterator2 __first2,
  59. _InputIterator2 __last2,
  60. _OutputIterator __result,
  61. _Compare __comp) {
  62. return std::__set_intersection<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
  63. std::move(__first1),
  64. std::move(__last1),
  65. std::move(__first2),
  66. std::move(__last2),
  67. std::move(__result),
  68. __comp)
  69. .__out_;
  70. }
  71. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  72. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_intersection(
  73. _InputIterator1 __first1,
  74. _InputIterator1 __last1,
  75. _InputIterator2 __first2,
  76. _InputIterator2 __last2,
  77. _OutputIterator __result) {
  78. return std::__set_intersection<_ClassicAlgPolicy>(
  79. std::move(__first1),
  80. std::move(__last1),
  81. std::move(__first2),
  82. std::move(__last2),
  83. std::move(__result),
  84. __less<>())
  85. .__out_;
  86. }
  87. _LIBCPP_END_NAMESPACE_STD
  88. _LIBCPP_POP_MACROS
  89. #endif // _LIBCPP___ALGORITHM_SET_INTERSECTION_H