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