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