1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_PARTITION_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_PARTITION_H
11fe6060f1SDimitry Andric
12fcaf7f86SDimitry Andric #include <__algorithm/iterator_operations.h>
13fe6060f1SDimitry Andric #include <__config>
14fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
15fcaf7f86SDimitry Andric #include <__utility/move.h>
16fcaf7f86SDimitry Andric #include <__utility/pair.h>
17fe6060f1SDimitry Andric
18fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19fe6060f1SDimitry Andric # pragma GCC system_header
20fe6060f1SDimitry Andric #endif
21fe6060f1SDimitry Andric
22*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
23*b3edf446SDimitry Andric #include <__undef_macros>
24*b3edf446SDimitry Andric
25fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
26fe6060f1SDimitry Andric
27fcaf7f86SDimitry Andric template <class _Predicate, class _AlgPolicy, class _ForwardIterator, class _Sentinel>
28bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
__partition_impl(_ForwardIterator __first,_Sentinel __last,_Predicate __pred,forward_iterator_tag)29cb14a3feSDimitry Andric __partition_impl(_ForwardIterator __first, _Sentinel __last, _Predicate __pred, forward_iterator_tag) {
30cb14a3feSDimitry Andric while (true) {
31fe6060f1SDimitry Andric if (__first == __last)
32fcaf7f86SDimitry Andric return std::make_pair(std::move(__first), std::move(__first));
33fe6060f1SDimitry Andric if (!__pred(*__first))
34fe6060f1SDimitry Andric break;
35fe6060f1SDimitry Andric ++__first;
36fe6060f1SDimitry Andric }
37fcaf7f86SDimitry Andric
38fcaf7f86SDimitry Andric _ForwardIterator __p = __first;
39cb14a3feSDimitry Andric while (++__p != __last) {
40cb14a3feSDimitry Andric if (__pred(*__p)) {
41fcaf7f86SDimitry Andric _IterOps<_AlgPolicy>::iter_swap(__first, __p);
42fe6060f1SDimitry Andric ++__first;
43fe6060f1SDimitry Andric }
44fe6060f1SDimitry Andric }
45fcaf7f86SDimitry Andric return std::make_pair(std::move(__first), std::move(__p));
46fe6060f1SDimitry Andric }
47fe6060f1SDimitry Andric
48fcaf7f86SDimitry Andric template <class _Predicate, class _AlgPolicy, class _BidirectionalIterator, class _Sentinel>
49bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator, _BidirectionalIterator>
__partition_impl(_BidirectionalIterator __first,_Sentinel __sentinel,_Predicate __pred,bidirectional_iterator_tag)50cb14a3feSDimitry Andric __partition_impl(_BidirectionalIterator __first, _Sentinel __sentinel, _Predicate __pred, bidirectional_iterator_tag) {
51fcaf7f86SDimitry Andric _BidirectionalIterator __original_last = _IterOps<_AlgPolicy>::next(__first, __sentinel);
52fcaf7f86SDimitry Andric _BidirectionalIterator __last = __original_last;
53fcaf7f86SDimitry Andric
54cb14a3feSDimitry Andric while (true) {
55cb14a3feSDimitry Andric while (true) {
56fe6060f1SDimitry Andric if (__first == __last)
57fcaf7f86SDimitry Andric return std::make_pair(std::move(__first), std::move(__original_last));
58fe6060f1SDimitry Andric if (!__pred(*__first))
59fe6060f1SDimitry Andric break;
60fe6060f1SDimitry Andric ++__first;
61fe6060f1SDimitry Andric }
62cb14a3feSDimitry Andric do {
63fe6060f1SDimitry Andric if (__first == --__last)
64fcaf7f86SDimitry Andric return std::make_pair(std::move(__first), std::move(__original_last));
65fe6060f1SDimitry Andric } while (!__pred(*__last));
66fcaf7f86SDimitry Andric _IterOps<_AlgPolicy>::iter_swap(__first, __last);
67fe6060f1SDimitry Andric ++__first;
68fe6060f1SDimitry Andric }
69fe6060f1SDimitry Andric }
70fe6060f1SDimitry Andric
71fcaf7f86SDimitry Andric template <class _AlgPolicy, class _ForwardIterator, class _Sentinel, class _Predicate, class _IterCategory>
72cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
__partition(_ForwardIterator __first,_Sentinel __last,_Predicate && __pred,_IterCategory __iter_category)73cb14a3feSDimitry Andric __partition(_ForwardIterator __first, _Sentinel __last, _Predicate&& __pred, _IterCategory __iter_category) {
74bdd1243dSDimitry Andric return std::__partition_impl<__remove_cvref_t<_Predicate>&, _AlgPolicy>(
75fcaf7f86SDimitry Andric std::move(__first), std::move(__last), __pred, __iter_category);
76fcaf7f86SDimitry Andric }
77fcaf7f86SDimitry Andric
78fe6060f1SDimitry Andric template <class _ForwardIterator, class _Predicate>
79cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
partition(_ForwardIterator __first,_ForwardIterator __last,_Predicate __pred)80cb14a3feSDimitry Andric partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
81fcaf7f86SDimitry Andric using _IterCategory = typename iterator_traits<_ForwardIterator>::iterator_category;
82fcaf7f86SDimitry Andric auto __result = std::__partition<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred, _IterCategory());
83fcaf7f86SDimitry Andric return __result.first;
84fe6060f1SDimitry Andric }
85fe6060f1SDimitry Andric
86fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
87fe6060f1SDimitry Andric
88*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
89*b3edf446SDimitry Andric
90fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_PARTITION_H
91