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