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