partition.h 3.3 KB

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