pstl_count.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_PSTL_COUNT_H
  9. #define _LIBCPP___ALGORITHM_PSTL_COUNT_H
  10. #include <__algorithm/count.h>
  11. #include <__algorithm/for_each.h>
  12. #include <__algorithm/pstl_backend.h>
  13. #include <__algorithm/pstl_for_each.h>
  14. #include <__algorithm/pstl_frontend_dispatch.h>
  15. #include <__atomic/atomic.h>
  16. #include <__config>
  17. #include <__functional/operations.h>
  18. #include <__iterator/iterator_traits.h>
  19. #include <__numeric/pstl_transform_reduce.h>
  20. #include <__type_traits/enable_if.h>
  21. #include <__type_traits/is_execution_policy.h>
  22. #include <__type_traits/remove_cvref.h>
  23. #include <__utility/move.h>
  24. #include <optional>
  25. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  26. # pragma GCC system_header
  27. #endif
  28. _LIBCPP_PUSH_MACROS
  29. #include <__undef_macros>
  30. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. template <class>
  33. void __pstl_count_if(); // declaration needed for the frontend dispatch below
  34. template <class _ExecutionPolicy,
  35. class _ForwardIterator,
  36. class _Predicate,
  37. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  38. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  39. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>> __count_if(
  40. _ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
  41. using __diff_t = __iter_diff_t<_ForwardIterator>;
  42. return std::__pstl_frontend_dispatch(
  43. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count_if, _RawPolicy),
  44. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) -> optional<__diff_t> {
  45. return std::__transform_reduce(
  46. __policy,
  47. std::move(__g_first),
  48. std::move(__g_last),
  49. __diff_t(),
  50. std::plus{},
  51. [&](__iter_reference<_ForwardIterator> __element) -> bool { return __g_pred(__element); });
  52. },
  53. std::move(__first),
  54. std::move(__last),
  55. std::move(__pred));
  56. }
  57. template <class _ExecutionPolicy,
  58. class _ForwardIterator,
  59. class _Predicate,
  60. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  61. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  62. _LIBCPP_HIDE_FROM_ABI __iter_diff_t<_ForwardIterator>
  63. count_if(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  64. auto __res = std::__count_if(__policy, std::move(__first), std::move(__last), std::move(__pred));
  65. if (!__res)
  66. std::__throw_bad_alloc();
  67. return *std::move(__res);
  68. }
  69. template <class>
  70. void __pstl_count(); // declaration needed for the frontend dispatch below
  71. template <class _ExecutionPolicy,
  72. class _ForwardIterator,
  73. class _Tp,
  74. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  75. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  76. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>>
  77. __count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  78. return std::__pstl_frontend_dispatch(
  79. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count, _RawPolicy),
  80. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value)
  81. -> optional<__iter_diff_t<_ForwardIterator>> {
  82. return std::count_if(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __v) {
  83. return __v == __g_value;
  84. });
  85. },
  86. std::move(__first),
  87. std::move(__last),
  88. __value);
  89. }
  90. template <class _ExecutionPolicy,
  91. class _ForwardIterator,
  92. class _Tp,
  93. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  94. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  95. _LIBCPP_HIDE_FROM_ABI __iter_diff_t<_ForwardIterator>
  96. count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  97. auto __res = std::__count(__policy, std::move(__first), std::move(__last), __value);
  98. if (!__res)
  99. std::__throw_bad_alloc();
  100. return *__res;
  101. }
  102. _LIBCPP_END_NAMESPACE_STD
  103. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  104. _LIBCPP_POP_MACROS
  105. #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H