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