partition.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <__config>
  11. #include <__iterator/iterator_traits.h>
  12. #include <__utility/swap.h>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. template <class _Predicate, class _ForwardIterator>
  18. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  19. __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
  20. {
  21. while (true)
  22. {
  23. if (__first == __last)
  24. return __first;
  25. if (!__pred(*__first))
  26. break;
  27. ++__first;
  28. }
  29. for (_ForwardIterator __p = __first; ++__p != __last;)
  30. {
  31. if (__pred(*__p))
  32. {
  33. swap(*__first, *__p);
  34. ++__first;
  35. }
  36. }
  37. return __first;
  38. }
  39. template <class _Predicate, class _BidirectionalIterator>
  40. _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
  41. __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
  42. bidirectional_iterator_tag)
  43. {
  44. while (true)
  45. {
  46. while (true)
  47. {
  48. if (__first == __last)
  49. return __first;
  50. if (!__pred(*__first))
  51. break;
  52. ++__first;
  53. }
  54. do
  55. {
  56. if (__first == --__last)
  57. return __first;
  58. } while (!__pred(*__last));
  59. swap(*__first, *__last);
  60. ++__first;
  61. }
  62. }
  63. template <class _ForwardIterator, class _Predicate>
  64. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  65. _ForwardIterator
  66. partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  67. {
  68. return _VSTD::__partition<_Predicate&>(
  69. __first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
  70. }
  71. _LIBCPP_END_NAMESPACE_STD
  72. #endif // _LIBCPP___ALGORITHM_PARTITION_H