xref: /llvm-project/libcxx/include/__algorithm/set_symmetric_difference.h (revision a2042521a0387d7d7b80b2987f4b21f5a50bc7bb)
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_SYMMETRIC_DIFFERENCE_H
10 #define _LIBCPP___ALGORITHM_SET_SYMMETRIC_DIFFERENCE_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 #include <__utility/pair.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template <class _InIter1, class _InIter2, class _OutIter>
30 struct __set_symmetric_difference_result {
31   _InIter1 __in1_;
32   _InIter2 __in2_;
33   _OutIter __out_;
34 
35   // need a constructor as C++03 aggregate init is hard
36   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
37   __set_symmetric_difference_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter)
38       : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {}
39 };
40 
41 template <class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
42 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __set_symmetric_difference_result<_InIter1, _InIter2, _OutIter>
43 __set_symmetric_difference(
44     _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
45   while (__first1 != __last1) {
46     if (__first2 == __last2) {
47       auto __ret1 = std::__copy(std::move(__first1), std::move(__last1), std::move(__result));
48       return __set_symmetric_difference_result<_InIter1, _InIter2, _OutIter>(
49           std::move(__ret1.first), std::move(__first2), std::move((__ret1.second)));
50     }
51     if (__comp(*__first1, *__first2)) {
52       *__result = *__first1;
53       ++__result;
54       ++__first1;
55     } else {
56       if (__comp(*__first2, *__first1)) {
57         *__result = *__first2;
58         ++__result;
59       } else {
60         ++__first1;
61       }
62       ++__first2;
63     }
64   }
65   auto __ret2 = std::__copy(std::move(__first2), std::move(__last2), std::move(__result));
66   return __set_symmetric_difference_result<_InIter1, _InIter2, _OutIter>(
67       std::move(__first1), std::move(__ret2.first), std::move((__ret2.second)));
68 }
69 
70 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
71 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_symmetric_difference(
72     _InputIterator1 __first1,
73     _InputIterator1 __last1,
74     _InputIterator2 __first2,
75     _InputIterator2 __last2,
76     _OutputIterator __result,
77     _Compare __comp) {
78   return std::__set_symmetric_difference<__comp_ref_type<_Compare> >(
79              std::move(__first1),
80              std::move(__last1),
81              std::move(__first2),
82              std::move(__last2),
83              std::move(__result),
84              __comp)
85       .__out_;
86 }
87 
88 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
89 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_symmetric_difference(
90     _InputIterator1 __first1,
91     _InputIterator1 __last1,
92     _InputIterator2 __first2,
93     _InputIterator2 __last2,
94     _OutputIterator __result) {
95   return std::set_symmetric_difference(
96       std::move(__first1),
97       std::move(__last1),
98       std::move(__first2),
99       std::move(__last2),
100       std::move(__result),
101       __less<>());
102 }
103 
104 _LIBCPP_END_NAMESPACE_STD
105 
106 _LIBCPP_POP_MACROS
107 
108 #endif // _LIBCPP___ALGORITHM_SET_SYMMETRIC_DIFFERENCE_H
109