1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PSTL_COUNT_H
10 #define _LIBCPP___ALGORITHM_PSTL_COUNT_H
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>
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>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
32 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
34 _LIBCPP_BEGIN_NAMESPACE_STD
37 void __pstl_count_if(); // declaration needed for the frontend dispatch below
39 template <class _ExecutionPolicy
,
40 class _ForwardIterator
,
42 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
43 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
44 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__iter_diff_t
<_ForwardIterator
>> __count_if(
45 _ExecutionPolicy
&& __policy
, _ForwardIterator
&& __first
, _ForwardIterator
&& __last
, _Predicate
&& __pred
) noexcept
{
46 using __diff_t
= __iter_diff_t
<_ForwardIterator
>;
47 return std::__pstl_frontend_dispatch(
48 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count_if
, _RawPolicy
),
49 [&](_ForwardIterator __g_first
, _ForwardIterator __g_last
, _Predicate __g_pred
) -> optional
<__diff_t
> {
50 return std::__transform_reduce(
56 [&](__iter_reference
<_ForwardIterator
> __element
) -> bool { return __g_pred(__element
); });
63 template <class _ExecutionPolicy
,
64 class _ForwardIterator
,
66 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
67 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
68 _LIBCPP_HIDE_FROM_ABI __iter_diff_t
<_ForwardIterator
>
69 count_if(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, _Predicate __pred
) {
70 auto __res
= std::__count_if(__policy
, std::move(__first
), std::move(__last
), std::move(__pred
));
72 std::__throw_bad_alloc();
73 return *std::move(__res
);
77 void __pstl_count(); // declaration needed for the frontend dispatch below
79 template <class _ExecutionPolicy
,
80 class _ForwardIterator
,
82 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
83 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
84 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI optional
<__iter_diff_t
<_ForwardIterator
>>
85 __count(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) {
86 return std::__pstl_frontend_dispatch(
87 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count
, _RawPolicy
),
88 [&](_ForwardIterator __g_first
, _ForwardIterator __g_last
, const _Tp
& __g_value
)
89 -> optional
<__iter_diff_t
<_ForwardIterator
>> {
90 return std::count_if(__policy
, __g_first
, __g_last
, [&](__iter_reference
<_ForwardIterator
> __v
) {
91 return __v
== __g_value
;
99 template <class _ExecutionPolicy
,
100 class _ForwardIterator
,
102 class _RawPolicy
= __remove_cvref_t
<_ExecutionPolicy
>,
103 enable_if_t
<is_execution_policy_v
<_RawPolicy
>, int> = 0>
104 _LIBCPP_HIDE_FROM_ABI __iter_diff_t
<_ForwardIterator
>
105 count(_ExecutionPolicy
&& __policy
, _ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
) {
106 auto __res
= std::__count(__policy
, std::move(__first
), std::move(__last
), __value
);
108 std::__throw_bad_alloc();
112 _LIBCPP_END_NAMESPACE_STD
114 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
116 #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H