pstl_count.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <__utility/terminate_on_exception.h>
  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. _LIBCPP_HIDE_FROM_ABI __iter_diff_t<_ForwardIterator>
  38. count_if(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  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) {
  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>
  56. void __pstl_count(); // declaration needed for the frontend dispatch below
  57. template <class _ExecutionPolicy,
  58. class _ForwardIterator,
  59. class _Tp,
  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(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  64. return std::__pstl_frontend_dispatch(
  65. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count, _RawPolicy),
  66. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) {
  67. return std::count_if(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __v) {
  68. return __v == __g_value;
  69. });
  70. },
  71. std::move(__first),
  72. std::move(__last),
  73. __value);
  74. }
  75. _LIBCPP_END_NAMESPACE_STD
  76. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  77. #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H