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_UNIQUE_COPY_H
10 #define _LIBCPP___ALGORITHM_UNIQUE_COPY_H
11 
12 #include <__algorithm/comp.h>
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #  pragma GCC system_header
18 #endif
19 
20 _LIBCPP_BEGIN_NAMESPACE_STD
21 
22 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
23 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
24 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
25               input_iterator_tag, output_iterator_tag)
26 {
27     if (__first != __last)
28     {
29         typename iterator_traits<_InputIterator>::value_type __t(*__first);
30         *__result = __t;
31         ++__result;
32         while (++__first != __last)
33         {
34             if (!__pred(__t, *__first))
35             {
36                 __t = *__first;
37                 *__result = __t;
38                 ++__result;
39             }
40         }
41     }
42     return __result;
43 }
44 
45 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
46 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
47 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
48               forward_iterator_tag, output_iterator_tag)
49 {
50     if (__first != __last)
51     {
52         _ForwardIterator __i = __first;
53         *__result = *__i;
54         ++__result;
55         while (++__first != __last)
56         {
57             if (!__pred(*__i, *__first))
58             {
59                 *__result = *__first;
60                 ++__result;
61                 __i = __first;
62             }
63         }
64     }
65     return __result;
66 }
67 
68 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
69 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
70 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
71               input_iterator_tag, forward_iterator_tag)
72 {
73     if (__first != __last)
74     {
75         *__result = *__first;
76         while (++__first != __last)
77             if (!__pred(*__result, *__first))
78                 *++__result = *__first;
79         ++__result;
80     }
81     return __result;
82 }
83 
84 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
85 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
86 _OutputIterator
87 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
88 {
89     return _VSTD::__unique_copy<_BinaryPredicate&>(__first, __last, __result, __pred,
90                                typename iterator_traits<_InputIterator>::iterator_category(),
91                                typename iterator_traits<_OutputIterator>::iterator_category());
92 }
93 
94 template <class _InputIterator, class _OutputIterator>
95 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
96 _OutputIterator
97 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
98 {
99     typedef typename iterator_traits<_InputIterator>::value_type __v;
100     return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
101 }
102 
103 
104 _LIBCPP_END_NAMESPACE_STD
105 
106 #endif // _LIBCPP___ALGORITHM_UNIQUE_COPY_H
107