pstl_count.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. template <class>
  31. void __pstl_count_if(); // declaration needed for the frontend dispatch below
  32. template <class _ExecutionPolicy,
  33. class _ForwardIterator,
  34. class _Predicate,
  35. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  36. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  37. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>> __count_if(
  38. _ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
  39. using __diff_t = __iter_diff_t<_ForwardIterator>;
  40. return std::__pstl_frontend_dispatch(
  41. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count_if, _RawPolicy),
  42. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) -> optional<__diff_t> {
  43. return std::__transform_reduce(
  44. __policy,
  45. std::move(__g_first),
  46. std::move(__g_last),
  47. __diff_t(),
  48. std::plus{},
  49. [&](__iter_reference<_ForwardIterator> __element) -> bool { return __g_pred(__element); });
  50. },
  51. std::move(__first),
  52. std::move(__last),
  53. std::move(__pred));
  54. }
  55. template <class _ExecutionPolicy,
  56. class _ForwardIterator,
  57. class _Predicate,
  58. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  59. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  60. _LIBCPP_HIDE_FROM_ABI __iter_diff_t<_ForwardIterator>
  61. count_if(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  62. auto __res = std::__count_if(__policy, std::move(__first), std::move(__last), std::move(__pred));
  63. if (!__res)
  64. std::__throw_bad_alloc();
  65. return *std::move(__res);
  66. }
  67. template <class>
  68. void __pstl_count(); // declaration needed for the frontend dispatch below
  69. template <class _ExecutionPolicy,
  70. class _ForwardIterator,
  71. class _Tp,
  72. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  73. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  74. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>>
  75. __count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  76. return std::__pstl_frontend_dispatch(
  77. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count, _RawPolicy),
  78. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value)
  79. -> optional<__iter_diff_t<_ForwardIterator>> {
  80. return std::count_if(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __v) {
  81. return __v == __g_value;
  82. });
  83. },
  84. std::move(__first),
  85. std::move(__last),
  86. __value);
  87. }
  88. template <class _ExecutionPolicy,
  89. class _ForwardIterator,
  90. class _Tp,
  91. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  92. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  93. _LIBCPP_HIDE_FROM_ABI __iter_diff_t<_ForwardIterator>
  94. count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  95. auto __res = std::__count(__policy, std::move(__first), std::move(__last), __value);
  96. if (!__res)
  97. std::__throw_bad_alloc();
  98. return *__res;
  99. }
  100. _LIBCPP_END_NAMESPACE_STD
  101. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  102. #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H