Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
count_if.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COUNT_IF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COUNT_IF_HPP_
7
8#include "arene/base/algorithm/detail/traits.hpp"
14
15// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
16// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
17
18// IWYU pragma: private, include <algorithm>
19// IWYU pragma: friend "stdlib_detail/.*"
20
21namespace std {
22
23// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
24/// @brief Count the number of elements in a range satisfying a predicate
25/// @tparam InputIterator The type of the iterators denoting the input range.
26/// @tparam UnaryPredicate The type of the unary callable that returns a boolean testable value
27/// @param begin The start of the range
28/// @param end The end of the range
29/// @param predicate The unary predicate which returns @c true for the counted elements
30/// @return The count of elements in the range for which @c predicate returns @c true
31/// @pre @c InputIterator must satisfy the input iterator requirements.
32/// @pre @c [begin,end) must be a valid range.
33/// @pre The expression @c predicate(v) must be convertible to bool for every argument v of type (possibly const) VT,
34/// where VT is the value type of InputIterator, regardless of value category, and must not modify v.
35// parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifiers 'begin' and 'end' do not hide anything"
36template <
37 class InputIterator,
38 class UnaryPredicate,
42 UnaryPredicate&,
43 arene::base::algorithm_detail::iter_reference_t<InputIterator>>>> = nullptr>
44constexpr auto count_if(InputIterator begin, InputIterator end, UnaryPredicate predicate) ->
47 while (begin != end) {
48 if (static_cast<bool>(predicate(*begin))) {
49 ++count;
50 }
51 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "This is an iterator type, so incrementing is OK"
52 ++begin;
53 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
54 }
55 return count;
56}
57// parasoft-end-suppress AUTOSAR-A2_10_1-a
58// parasoft-end-suppress AUTOSAR-M3_3_2-a
59
60} // namespace std
61
62#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COUNT_IF_HPP_
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
Public export header for the constraints subpackage.
constexpr bool is_predicate_v
backport for the C++20 predicate concept
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
STL namespace.
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
constexpr auto end(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the one-past-the-end element of the array.
Definition end.hpp:28
constexpr auto count_if(InputIterator begin, InputIterator end, UnaryPredicate predicate) -> typename std::iterator_traits< InputIterator >::difference_type
Count the number of elements in a range satisfying a predicate.
Definition count_if.hpp:44
constexpr auto begin(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the first element of the array.
Definition begin.hpp:28