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