set_union.h 3.6 KB

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