tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
count_if.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_COUNT_IF_HPP
5#define TETL_ALGORITHM_COUNT_IF_HPP
6
7#include <etl/_iterator/iterator_traits.hpp>
8
9namespace etl {
10
11/// Returns the number of elements in the range `[first, last)`
12/// satisfying specific criteria. Counts elements for which predicate p returns
13/// true.
14///
15/// \param first The range of elements to examine.
16/// \param last The range of elements to examine.
17/// \param p Unary predicate which returns ​true for the required elements.
18///
19/// https://en.cppreference.com/w/cpp/algorithm/count
20///
21/// \ingroup algorithm
22template <typename InputIt, typename Predicate>
23[[nodiscard]] constexpr auto count_if(InputIt first, InputIt last, Predicate p) ->
24 typename iterator_traits<InputIt>::difference_type
25{
26 auto result = typename etl::iterator_traits<InputIt>::difference_type{0};
27 for (; first != last; ++first) {
28 if (p(*first)) {
29 ++result;
30 }
31 }
32 return result;
33}
34
35} // namespace etl
36
37#endif // TETL_ALGORITHM_COUNT_IF_HPP
constexpr auto count_if(InputIt first, InputIt last, Predicate p) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count_if.hpp:23
Definition adjacent_find.hpp:9
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48