File indexing completed on 2026-05-03 08:13:09
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_PARTITION_H
0010 #define _LIBCPP___ALGORITHM_PARTITION_H
0011
0012 #include <__algorithm/iterator_operations.h>
0013 #include <__config>
0014 #include <__iterator/iterator_traits.h>
0015 #include <__type_traits/remove_cvref.h>
0016 #include <__utility/move.h>
0017 #include <__utility/pair.h>
0018
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 # pragma GCC system_header
0021 #endif
0022
0023 _LIBCPP_PUSH_MACROS
0024 #include <__undef_macros>
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 template <class _Predicate, class _AlgPolicy, class _ForwardIterator, class _Sentinel>
0029 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
0030 __partition_impl(_ForwardIterator __first, _Sentinel __last, _Predicate __pred, forward_iterator_tag) {
0031 while (true) {
0032 if (__first == __last)
0033 return std::make_pair(__first, __first);
0034 if (!__pred(*__first))
0035 break;
0036 ++__first;
0037 }
0038
0039 _ForwardIterator __p = __first;
0040 while (++__p != __last) {
0041 if (__pred(*__p)) {
0042 _IterOps<_AlgPolicy>::iter_swap(__first, __p);
0043 ++__first;
0044 }
0045 }
0046 return std::make_pair(std::move(__first), std::move(__p));
0047 }
0048
0049 template <class _Predicate, class _AlgPolicy, class _BidirectionalIterator, class _Sentinel>
0050 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator, _BidirectionalIterator>
0051 __partition_impl(_BidirectionalIterator __first, _Sentinel __sentinel, _Predicate __pred, bidirectional_iterator_tag) {
0052 _BidirectionalIterator __original_last = _IterOps<_AlgPolicy>::next(__first, __sentinel);
0053 _BidirectionalIterator __last = __original_last;
0054
0055 while (true) {
0056 while (true) {
0057 if (__first == __last)
0058 return std::make_pair(std::move(__first), std::move(__original_last));
0059 if (!__pred(*__first))
0060 break;
0061 ++__first;
0062 }
0063 do {
0064 if (__first == --__last)
0065 return std::make_pair(std::move(__first), std::move(__original_last));
0066 } while (!__pred(*__last));
0067 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
0068 ++__first;
0069 }
0070 }
0071
0072 template <class _AlgPolicy, class _ForwardIterator, class _Sentinel, class _Predicate, class _IterCategory>
0073 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
0074 __partition(_ForwardIterator __first, _Sentinel __last, _Predicate&& __pred, _IterCategory __iter_category) {
0075 return std::__partition_impl<__remove_cvref_t<_Predicate>&, _AlgPolicy>(
0076 std::move(__first), std::move(__last), __pred, __iter_category);
0077 }
0078
0079 template <class _ForwardIterator, class _Predicate>
0080 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0081 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
0082 using _IterCategory = typename iterator_traits<_ForwardIterator>::iterator_category;
0083 auto __result = std::__partition<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred, _IterCategory());
0084 return __result.first;
0085 }
0086
0087 _LIBCPP_END_NAMESPACE_STD
0088
0089 _LIBCPP_POP_MACROS
0090
0091 #endif