Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __algorithm / unique.h
blob307c424a7c2fb5edb2fa7add32564927c85ee8db
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_UNIQUE_H
10 #define _LIBCPP___ALGORITHM_UNIQUE_H
12 #include <__algorithm/adjacent_find.h>
13 #include <__algorithm/comp.h>
14 #include <__algorithm/iterator_operations.h>
15 #include <__config>
16 #include <__functional/identity.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__utility/move.h>
19 #include <__utility/pair.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
23 #endif
25 _LIBCPP_PUSH_MACROS
26 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 // unique
32 template <class _AlgPolicy, class _Iter, class _Sent, class _BinaryPredicate>
33 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 std::pair<_Iter, _Iter>
34 __unique(_Iter __first, _Sent __last, _BinaryPredicate&& __pred) {
35 __identity __proj;
36 __first = std::__adjacent_find(__first, __last, __pred, __proj);
37 if (__first != __last) {
38 // ... a a ? ...
39 // f i
40 _Iter __i = __first;
41 for (++__i; ++__i != __last;)
42 if (!__pred(*__first, *__i))
43 *++__first = _IterOps<_AlgPolicy>::__iter_move(__i);
44 ++__first;
45 return std::pair<_Iter, _Iter>(std::move(__first), std::move(__i));
47 return std::pair<_Iter, _Iter>(__first, __first);
50 template <class _ForwardIterator, class _BinaryPredicate>
51 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
52 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) {
53 return std::__unique<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred).first;
56 template <class _ForwardIterator>
57 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
58 unique(_ForwardIterator __first, _ForwardIterator __last) {
59 return std::unique(__first, __last, __equal_to());
62 _LIBCPP_END_NAMESPACE_STD
64 _LIBCPP_POP_MACROS
66 #endif // _LIBCPP___ALGORITHM_UNIQUE_H