ranges_partition.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_RANGES_PARTITION_H
  9. #define _LIBCPP___ALGORITHM_RANGES_PARTITION_H
  10. #include <__algorithm/iterator_operations.h>
  11. #include <__algorithm/make_projected.h>
  12. #include <__algorithm/partition.h>
  13. #include <__algorithm/ranges_iterator_concept.h>
  14. #include <__config>
  15. #include <__functional/identity.h>
  16. #include <__functional/invoke.h>
  17. #include <__functional/ranges_operations.h>
  18. #include <__iterator/concepts.h>
  19. #include <__iterator/iterator_traits.h>
  20. #include <__iterator/permutable.h>
  21. #include <__iterator/projected.h>
  22. #include <__ranges/access.h>
  23. #include <__ranges/concepts.h>
  24. #include <__ranges/subrange.h>
  25. #include <__utility/forward.h>
  26. #include <__utility/move.h>
  27. #include <__utility/pair.h>
  28. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  29. # pragma GCC system_header
  30. #endif
  31. _LIBCPP_PUSH_MACROS
  32. #include <__undef_macros>
  33. #if _LIBCPP_STD_VER >= 20
  34. _LIBCPP_BEGIN_NAMESPACE_STD
  35. namespace ranges {
  36. namespace __partition {
  37. struct __fn {
  38. template <class _Iter, class _Sent, class _Proj, class _Pred>
  39. _LIBCPP_HIDE_FROM_ABI static constexpr subrange<__remove_cvref_t<_Iter>>
  40. __partition_fn_impl(_Iter&& __first, _Sent&& __last, _Pred&& __pred, _Proj&& __proj) {
  41. auto&& __projected_pred = std::__make_projected(__pred, __proj);
  42. auto __result = std::__partition<_RangeAlgPolicy>(
  43. std::move(__first), std::move(__last), __projected_pred, __iterator_concept<_Iter>());
  44. return {std::move(__result.first), std::move(__result.second)};
  45. }
  46. template <permutable _Iter,
  47. sentinel_for<_Iter> _Sent,
  48. class _Proj = identity,
  49. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  50. _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
  51. operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
  52. return __partition_fn_impl(__first, __last, __pred, __proj);
  53. }
  54. template <forward_range _Range,
  55. class _Proj = identity,
  56. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
  57. requires permutable<iterator_t<_Range>>
  58. _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
  59. operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
  60. return __partition_fn_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
  61. }
  62. };
  63. } // namespace __partition
  64. inline namespace __cpo {
  65. inline constexpr auto partition = __partition::__fn{};
  66. } // namespace __cpo
  67. } // namespace ranges
  68. _LIBCPP_END_NAMESPACE_STD
  69. #endif // _LIBCPP_STD_VER >= 20
  70. _LIBCPP_POP_MACROS
  71. #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_H