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