181ad6265SDimitry Andric //===----------------------------------------------------------------------===// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric 981ad6265SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_COUNT_IF_H 1081ad6265SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_COUNT_IF_H 1181ad6265SDimitry Andric 1281ad6265SDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__functional/identity.h> 1481ad6265SDimitry Andric #include <__functional/invoke.h> 1581ad6265SDimitry Andric #include <__functional/ranges_operations.h> 1681ad6265SDimitry Andric #include <__iterator/concepts.h> 1781ad6265SDimitry Andric #include <__iterator/incrementable_traits.h> 1881ad6265SDimitry Andric #include <__iterator/iterator_traits.h> 1981ad6265SDimitry Andric #include <__iterator/projected.h> 2081ad6265SDimitry Andric #include <__ranges/access.h> 2181ad6265SDimitry Andric #include <__ranges/concepts.h> 2281ad6265SDimitry Andric #include <__utility/move.h> 2381ad6265SDimitry Andric 2481ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2581ad6265SDimitry Andric # pragma GCC system_header 2681ad6265SDimitry Andric #endif 2781ad6265SDimitry Andric 28b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS 29b3edf446SDimitry Andric #include <__undef_macros> 30b3edf446SDimitry Andric 3106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 3281ad6265SDimitry Andric 3381ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 3481ad6265SDimitry Andric 3581ad6265SDimitry Andric namespace ranges { 3681ad6265SDimitry Andric template <class _Iter, class _Sent, class _Proj, class _Pred> 3706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Iter> 3806c3fb27SDimitry Andric __count_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { 3981ad6265SDimitry Andric iter_difference_t<_Iter> __counter(0); 4081ad6265SDimitry Andric for (; __first != __last; ++__first) { 4181ad6265SDimitry Andric if (std::invoke(__pred, std::invoke(__proj, *__first))) 4281ad6265SDimitry Andric ++__counter; 4381ad6265SDimitry Andric } 4481ad6265SDimitry Andric return __counter; 4581ad6265SDimitry Andric } 4681ad6265SDimitry Andric 4781ad6265SDimitry Andric namespace __count_if { 4881ad6265SDimitry Andric struct __fn { 4906c3fb27SDimitry Andric template <input_iterator _Iter, 5006c3fb27SDimitry Andric sentinel_for<_Iter> _Sent, 5106c3fb27SDimitry Andric class _Proj = identity, 5281ad6265SDimitry Andric indirect_unary_predicate<projected<_Iter, _Proj>> _Predicate> 53*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Iter> 5406c3fb27SDimitry Andric operator()(_Iter __first, _Sent __last, _Predicate __pred, _Proj __proj = {}) const { 5581ad6265SDimitry Andric return ranges::__count_if_impl(std::move(__first), std::move(__last), __pred, __proj); 5681ad6265SDimitry Andric } 5781ad6265SDimitry Andric 5806c3fb27SDimitry Andric template <input_range _Range, 5906c3fb27SDimitry Andric class _Proj = identity, 6081ad6265SDimitry Andric indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Predicate> 61*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_difference_t<_Range> 6206c3fb27SDimitry Andric operator()(_Range&& __r, _Predicate __pred, _Proj __proj = {}) const { 6381ad6265SDimitry Andric return ranges::__count_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj); 6481ad6265SDimitry Andric } 6581ad6265SDimitry Andric }; 6681ad6265SDimitry Andric } // namespace __count_if 6781ad6265SDimitry Andric 6881ad6265SDimitry Andric inline namespace __cpo { 6981ad6265SDimitry Andric inline constexpr auto count_if = __count_if::__fn{}; 7081ad6265SDimitry Andric } // namespace __cpo 7181ad6265SDimitry Andric } // namespace ranges 7281ad6265SDimitry Andric 7381ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD 7481ad6265SDimitry Andric 7506c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 7681ad6265SDimitry Andric 77b3edf446SDimitry Andric _LIBCPP_POP_MACROS 78b3edf446SDimitry Andric 7981ad6265SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_COUNT_IF_H 80