xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/unique.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 <__algorithm/iterator_operations.h>
15 #include <__config>
16 #include <__iterator/iterator_traits.h>
17 #include <__utility/move.h>
18 #include <__utility/pair.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 // unique
27 
28 template <class _AlgPolicy, class _Iter, class _Sent, class _BinaryPredicate>
29 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 std::pair<_Iter, _Iter>
30 __unique(_Iter __first, _Sent __last, _BinaryPredicate&& __pred) {
31   __first = std::__adjacent_find(__first, __last, __pred);
32   if (__first != __last) {
33     // ...  a  a  ?  ...
34     //      f     i
35     _Iter __i = __first;
36     for (++__i; ++__i != __last;)
37       if (!__pred(*__first, *__i))
38         *++__first = _IterOps<_AlgPolicy>::__iter_move(__i);
39     ++__first;
40     return std::pair<_Iter, _Iter>(std::move(__first), std::move(__i));
41   }
42   return std::pair<_Iter, _Iter>(__first, __first);
43 }
44 
45 template <class _ForwardIterator, class _BinaryPredicate>
46 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
47 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) {
48   return std::__unique<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred).first;
49 }
50 
51 template <class _ForwardIterator>
52 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
53 unique(_ForwardIterator __first, _ForwardIterator __last) {
54   return std::unique(__first, __last, __equal_to());
55 }
56 
57 _LIBCPP_END_NAMESPACE_STD
58 
59 #endif // _LIBCPP___ALGORITHM_UNIQUE_H
60