• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <optional>
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>
__count_if(_ExecutionPolicy && __policy,_ForwardIterator && __first,_ForwardIterator && __last,_Predicate && __pred)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(
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 _ExecutionPolicy,
64           class _ForwardIterator,
65           class _Predicate,
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>
count_if(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,_Predicate __pred)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));
71   if (!__res)
72     std::__throw_bad_alloc();
73   return *std::move(__res);
74 }
75 
76 template <class>
77 void __pstl_count(); // declaration needed for the frontend dispatch below
78 
79 template <class _ExecutionPolicy,
80           class _ForwardIterator,
81           class _Tp,
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>>
__count(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,const _Tp & __value)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;
92         });
93       },
94       std::move(__first),
95       std::move(__last),
96       __value);
97 }
98 
99 template <class _ExecutionPolicy,
100           class _ForwardIterator,
101           class _Tp,
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>
count(_ExecutionPolicy && __policy,_ForwardIterator __first,_ForwardIterator __last,const _Tp & __value)105 count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
106   auto __res = std::__count(__policy, std::move(__first), std::move(__last), __value);
107   if (!__res)
108     std::__throw_bad_alloc();
109   return *__res;
110 }
111 
112 _LIBCPP_END_NAMESPACE_STD
113 
114 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
115 
116 #endif // _LIBCPP___ALGORITHM_PSTL_COUNT_H
117