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