1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___ALGORITHM_COUNT_IF_H
11#define _LIBCPP___ALGORITHM_COUNT_IF_H
12
13#include <__algorithm/iterator_operations.h>
14#include <__config>
15#include <__functional/identity.h>
16#include <__iterator/iterator_traits.h>
17#include <__type_traits/invoke.h>
18
19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20# pragma GCC system_header
21#endif
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25template <class _AlgPolicy, class _Iter, class _Sent, class _Proj, class _Pred>
26_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __policy_iter_diff_t<_AlgPolicy, _Iter>
27__count_if(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
28 __policy_iter_diff_t<_AlgPolicy, _Iter> __counter(0);
29 for (; __first != __last; ++__first) {
30 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
31 ++__counter;
32 }
33 return __counter;
34}
35
36template <class _InputIterator, class _Predicate>
37[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
38typename iterator_traits<_InputIterator>::difference_type
39count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
40 __identity __proj;
41 return std::__count_if<_ClassicAlgPolicy>(__first, __last, __pred, __proj);
42}
43
44_LIBCPP_END_NAMESPACE_STD
45
46#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
47