xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/unique_copy.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_UNIQUE_COPY_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_UNIQUE_COPY_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
1361cfbce3SDimitry Andric #include <__algorithm/iterator_operations.h>
1404eeddc0SDimitry Andric #include <__config>
15fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
1661cfbce3SDimitry Andric #include <__type_traits/conditional.h>
1761cfbce3SDimitry Andric #include <__type_traits/is_base_of.h>
1861cfbce3SDimitry Andric #include <__type_traits/is_same.h>
1961cfbce3SDimitry Andric #include <__utility/move.h>
2061cfbce3SDimitry Andric #include <__utility/pair.h>
21fe6060f1SDimitry Andric 
22fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23fe6060f1SDimitry Andric #  pragma GCC system_header
24fe6060f1SDimitry Andric #endif
25fe6060f1SDimitry Andric 
26*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
27*b3edf446SDimitry Andric #include <__undef_macros>
28*b3edf446SDimitry Andric 
29fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
30fe6060f1SDimitry Andric 
3161cfbce3SDimitry Andric namespace __unique_copy_tags {
3261cfbce3SDimitry Andric 
3361cfbce3SDimitry Andric struct __reread_from_input_tag {};
3461cfbce3SDimitry Andric struct __reread_from_output_tag {};
3561cfbce3SDimitry Andric struct __read_from_tmp_value_tag {};
3661cfbce3SDimitry Andric 
3761cfbce3SDimitry Andric } // namespace __unique_copy_tags
3861cfbce3SDimitry Andric 
3961cfbce3SDimitry Andric template <class _AlgPolicy, class _BinaryPredicate, class _InputIterator, class _Sent, class _OutputIterator>
40bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _OutputIterator>
__unique_copy(_InputIterator __first,_Sent __last,_OutputIterator __result,_BinaryPredicate && __pred,__unique_copy_tags::__read_from_tmp_value_tag)4161cfbce3SDimitry Andric __unique_copy(_InputIterator __first,
4261cfbce3SDimitry Andric               _Sent __last,
4361cfbce3SDimitry Andric               _OutputIterator __result,
4461cfbce3SDimitry Andric               _BinaryPredicate&& __pred,
4561cfbce3SDimitry Andric               __unique_copy_tags::__read_from_tmp_value_tag) {
4661cfbce3SDimitry Andric   if (__first != __last) {
4761cfbce3SDimitry Andric     typename _IterOps<_AlgPolicy>::template __value_type<_InputIterator> __t(*__first);
48fe6060f1SDimitry Andric     *__result = __t;
49fe6060f1SDimitry Andric     ++__result;
5061cfbce3SDimitry Andric     while (++__first != __last) {
5161cfbce3SDimitry Andric       if (!__pred(__t, *__first)) {
52fe6060f1SDimitry Andric         __t       = *__first;
53fe6060f1SDimitry Andric         *__result = __t;
54fe6060f1SDimitry Andric         ++__result;
55fe6060f1SDimitry Andric       }
56fe6060f1SDimitry Andric     }
57fe6060f1SDimitry Andric   }
5861cfbce3SDimitry Andric   return pair<_InputIterator, _OutputIterator>(std::move(__first), std::move(__result));
59fe6060f1SDimitry Andric }
60fe6060f1SDimitry Andric 
6161cfbce3SDimitry Andric template <class _AlgPolicy, class _BinaryPredicate, class _ForwardIterator, class _Sent, class _OutputIterator>
62bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _OutputIterator>
__unique_copy(_ForwardIterator __first,_Sent __last,_OutputIterator __result,_BinaryPredicate && __pred,__unique_copy_tags::__reread_from_input_tag)6361cfbce3SDimitry Andric __unique_copy(_ForwardIterator __first,
6461cfbce3SDimitry Andric               _Sent __last,
6561cfbce3SDimitry Andric               _OutputIterator __result,
6661cfbce3SDimitry Andric               _BinaryPredicate&& __pred,
6761cfbce3SDimitry Andric               __unique_copy_tags::__reread_from_input_tag) {
6861cfbce3SDimitry Andric   if (__first != __last) {
69fe6060f1SDimitry Andric     _ForwardIterator __i = __first;
70fe6060f1SDimitry Andric     *__result            = *__i;
71fe6060f1SDimitry Andric     ++__result;
7261cfbce3SDimitry Andric     while (++__first != __last) {
7361cfbce3SDimitry Andric       if (!__pred(*__i, *__first)) {
74fe6060f1SDimitry Andric         *__result = *__first;
75fe6060f1SDimitry Andric         ++__result;
76fe6060f1SDimitry Andric         __i = __first;
77fe6060f1SDimitry Andric       }
78fe6060f1SDimitry Andric     }
79fe6060f1SDimitry Andric   }
8061cfbce3SDimitry Andric   return pair<_ForwardIterator, _OutputIterator>(std::move(__first), std::move(__result));
81fe6060f1SDimitry Andric }
82fe6060f1SDimitry Andric 
8361cfbce3SDimitry Andric template <class _AlgPolicy, class _BinaryPredicate, class _InputIterator, class _Sent, class _InputAndOutputIterator>
84bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _InputAndOutputIterator>
__unique_copy(_InputIterator __first,_Sent __last,_InputAndOutputIterator __result,_BinaryPredicate && __pred,__unique_copy_tags::__reread_from_output_tag)8561cfbce3SDimitry Andric __unique_copy(_InputIterator __first,
8661cfbce3SDimitry Andric               _Sent __last,
8761cfbce3SDimitry Andric               _InputAndOutputIterator __result,
8861cfbce3SDimitry Andric               _BinaryPredicate&& __pred,
8961cfbce3SDimitry Andric               __unique_copy_tags::__reread_from_output_tag) {
9061cfbce3SDimitry Andric   if (__first != __last) {
91fe6060f1SDimitry Andric     *__result = *__first;
92fe6060f1SDimitry Andric     while (++__first != __last)
93fe6060f1SDimitry Andric       if (!__pred(*__result, *__first))
94fe6060f1SDimitry Andric         *++__result = *__first;
95fe6060f1SDimitry Andric     ++__result;
96fe6060f1SDimitry Andric   }
9761cfbce3SDimitry Andric   return pair<_InputIterator, _InputAndOutputIterator>(std::move(__first), std::move(__result));
98fe6060f1SDimitry Andric }
99fe6060f1SDimitry Andric 
100fe6060f1SDimitry Andric template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
101bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
unique_copy(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryPredicate __pred)10261cfbce3SDimitry Andric unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) {
103bdd1243dSDimitry Andric   using __algo_tag = __conditional_t<
10461cfbce3SDimitry Andric       is_base_of<forward_iterator_tag, typename iterator_traits<_InputIterator>::iterator_category>::value,
10561cfbce3SDimitry Andric       __unique_copy_tags::__reread_from_input_tag,
106bdd1243dSDimitry Andric       __conditional_t<
10761cfbce3SDimitry Andric           is_base_of<forward_iterator_tag, typename iterator_traits<_OutputIterator>::iterator_category>::value &&
10861cfbce3SDimitry Andric               is_same< typename iterator_traits<_InputIterator>::value_type,
10961cfbce3SDimitry Andric                        typename iterator_traits<_OutputIterator>::value_type>::value,
11061cfbce3SDimitry Andric           __unique_copy_tags::__reread_from_output_tag,
111bdd1243dSDimitry Andric           __unique_copy_tags::__read_from_tmp_value_tag> >;
11261cfbce3SDimitry Andric   return std::__unique_copy<_ClassicAlgPolicy>(
11361cfbce3SDimitry Andric              std::move(__first), std::move(__last), std::move(__result), __pred, __algo_tag())
11461cfbce3SDimitry Andric       .second;
115fe6060f1SDimitry Andric }
116fe6060f1SDimitry Andric 
117fe6060f1SDimitry Andric template <class _InputIterator, class _OutputIterator>
118bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
unique_copy(_InputIterator __first,_InputIterator __last,_OutputIterator __result)11961cfbce3SDimitry Andric unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
120bdd1243dSDimitry Andric   return std::unique_copy(std::move(__first), std::move(__last), std::move(__result), __equal_to());
121fe6060f1SDimitry Andric }
122fe6060f1SDimitry Andric 
123fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
124fe6060f1SDimitry Andric 
125*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
126*b3edf446SDimitry Andric 
127fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_UNIQUE_COPY_H
128