set_union.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_BEGIN_NAMESPACE_STD
  22. template <class _InIter1, class _InIter2, class _OutIter>
  23. struct __set_union_result {
  24. _InIter1 __in1_;
  25. _InIter2 __in2_;
  26. _OutIter __out_;
  27. // need a constructor as C++03 aggregate init is hard
  28. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  29. __set_union_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter)
  30. : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {}
  31. };
  32. template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
  33. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_union_result<_InIter1, _InIter2, _OutIter> __set_union(
  34. _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
  35. for (; __first1 != __last1; ++__result) {
  36. if (__first2 == __last2) {
  37. auto __ret1 = std::__copy<_AlgPolicy>(std::move(__first1), std::move(__last1), std::move(__result));
  38. return __set_union_result<_InIter1, _InIter2, _OutIter>(
  39. std::move(__ret1.first), std::move(__first2), std::move((__ret1.second)));
  40. }
  41. if (__comp(*__first2, *__first1)) {
  42. *__result = *__first2;
  43. ++__first2;
  44. } else {
  45. if (!__comp(*__first1, *__first2)) {
  46. ++__first2;
  47. }
  48. *__result = *__first1;
  49. ++__first1;
  50. }
  51. }
  52. auto __ret2 = std::__copy<_AlgPolicy>(std::move(__first2), std::move(__last2), std::move(__result));
  53. return __set_union_result<_InIter1, _InIter2, _OutIter>(
  54. std::move(__first1), std::move(__ret2.first), std::move((__ret2.second)));
  55. }
  56. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  57. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_union(
  58. _InputIterator1 __first1,
  59. _InputIterator1 __last1,
  60. _InputIterator2 __first2,
  61. _InputIterator2 __last2,
  62. _OutputIterator __result,
  63. _Compare __comp) {
  64. return std::__set_union<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
  65. std::move(__first1),
  66. std::move(__last1),
  67. std::move(__first2),
  68. std::move(__last2),
  69. std::move(__result),
  70. __comp)
  71. .__out_;
  72. }
  73. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  74. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_union(
  75. _InputIterator1 __first1,
  76. _InputIterator1 __last1,
  77. _InputIterator2 __first2,
  78. _InputIterator2 __last2,
  79. _OutputIterator __result) {
  80. return std::set_union(
  81. std::move(__first1),
  82. std::move(__last1),
  83. std::move(__first2),
  84. std::move(__last2),
  85. std::move(__result),
  86. __less<>());
  87. }
  88. _LIBCPP_END_NAMESPACE_STD
  89. #endif // _LIBCPP___ALGORITHM_SET_UNION_H