xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/partition.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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_PARTITION_H
10 #define _LIBCPP___ALGORITHM_PARTITION_H
11 
12 #include <__algorithm/iterator_operations.h>
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 #include <__utility/move.h>
16 #include <__utility/pair.h>
17 #include <type_traits>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 template <class _Predicate, class _AlgPolicy, class _ForwardIterator, class _Sentinel>
26 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
27 __partition_impl(_ForwardIterator __first, _Sentinel __last, _Predicate __pred, forward_iterator_tag)
28 {
29     while (true)
30     {
31         if (__first == __last)
32             return std::make_pair(std::move(__first), std::move(__first));
33         if (!__pred(*__first))
34             break;
35         ++__first;
36     }
37 
38     _ForwardIterator __p = __first;
39     while (++__p != __last)
40     {
41         if (__pred(*__p))
42         {
43             _IterOps<_AlgPolicy>::iter_swap(__first, __p);
44             ++__first;
45         }
46     }
47     return std::make_pair(std::move(__first), std::move(__p));
48 }
49 
50 template <class _Predicate, class _AlgPolicy, class _BidirectionalIterator, class _Sentinel>
51 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator, _BidirectionalIterator>
52 __partition_impl(_BidirectionalIterator __first, _Sentinel __sentinel, _Predicate __pred,
53             bidirectional_iterator_tag)
54 {
55     _BidirectionalIterator __original_last = _IterOps<_AlgPolicy>::next(__first, __sentinel);
56     _BidirectionalIterator __last = __original_last;
57 
58     while (true)
59     {
60         while (true)
61         {
62             if (__first == __last)
63                 return std::make_pair(std::move(__first), std::move(__original_last));
64             if (!__pred(*__first))
65                 break;
66             ++__first;
67         }
68         do
69         {
70             if (__first == --__last)
71                 return std::make_pair(std::move(__first), std::move(__original_last));
72         } while (!__pred(*__last));
73         _IterOps<_AlgPolicy>::iter_swap(__first, __last);
74         ++__first;
75     }
76 }
77 
78 template <class _AlgPolicy, class _ForwardIterator, class _Sentinel, class _Predicate, class _IterCategory>
79 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
80 pair<_ForwardIterator, _ForwardIterator> __partition(
81     _ForwardIterator __first, _Sentinel __last, _Predicate&& __pred, _IterCategory __iter_category) {
82   return std::__partition_impl<__remove_cvref_t<_Predicate>&, _AlgPolicy>(
83       std::move(__first), std::move(__last), __pred, __iter_category);
84 }
85 
86 template <class _ForwardIterator, class _Predicate>
87 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
88 _ForwardIterator
89 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
90 {
91   using _IterCategory = typename iterator_traits<_ForwardIterator>::iterator_category;
92   auto __result = std::__partition<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred, _IterCategory());
93   return __result.first;
94 }
95 
96 _LIBCPP_END_NAMESPACE_STD
97 
98 #endif // _LIBCPP___ALGORITHM_PARTITION_H
99