tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
search_n.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_SEARCH_N_HPP
4#define TETL_ALGORITHM_SEARCH_N_HPP
5
7
8namespace etl {
9
12
15template <typename ForwardIt, typename Size, typename ValueT, typename Predicate>
16[[nodiscard]] constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const& value, Predicate pred)
17 -> ForwardIt
18{
19 if (count <= Size{}) {
20 return first;
21 }
22
23 auto localCounter = Size{};
24 ForwardIt found = nullptr;
25
26 for (; first != last; ++first) {
27 if (pred(*first, value)) {
28 localCounter++;
29 if (found == nullptr) {
30 found = first;
31 }
32 } else {
33 localCounter = 0;
34 }
35
36 if (localCounter == count) {
37 return found;
38 }
39 }
40
41 return last;
42}
43
44template <typename ForwardIt, typename Size, typename ValueT>
45[[nodiscard]] constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const& value) -> ForwardIt
46{
47 return etl::search_n(first, last, count, value, etl::equal_to());
48}
49
51
52} // namespace etl
53
54#endif // TETL_ALGORITHM_SEARCH_N_HPP
constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const &value, Predicate pred) -> ForwardIt
Searches the range [first, last) for the first sequence of count identical elements,...
Definition search_n.hpp:16
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
Definition adjacent_find.hpp:8
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:14