xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/unique.h (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
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_H
10 #define _LIBCPP___ALGORITHM_UNIQUE_H
11 
12 #include <__algorithm/adjacent_find.h>
13 #include <__algorithm/comp.h>
14 #include <__config>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 // unique
25 
26 template <class _ForwardIterator, class _BinaryPredicate>
27 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
28 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
29 {
30     __first = _VSTD::adjacent_find<_ForwardIterator, _BinaryPredicate&>(__first, __last, __pred);
31     if (__first != __last)
32     {
33         // ...  a  a  ?  ...
34         //      f     i
35         _ForwardIterator __i = __first;
36         for (++__i; ++__i != __last;)
37             if (!__pred(*__first, *__i))
38                 *++__first = _VSTD::move(*__i);
39         ++__first;
40     }
41     return __first;
42 }
43 
44 template <class _ForwardIterator>
45 _LIBCPP_NODISCARD_EXT inline
46 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
47 _ForwardIterator
48 unique(_ForwardIterator __first, _ForwardIterator __last)
49 {
50     typedef typename iterator_traits<_ForwardIterator>::value_type __v;
51     return _VSTD::unique(__first, __last, __equal_to<__v>());
52 }
53 
54 _LIBCPP_END_NAMESPACE_STD
55 
56 #endif // _LIBCPP___ALGORITHM_UNIQUE_H
57