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// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_SEARCH_N_HPP
5#define TETL_ALGORITHM_SEARCH_N_HPP
6
7#include <etl/_functional/equal_to.hpp>
8
9namespace etl {
10
11/// \ingroup algorithm
12/// @{
13
14/// \brief Searches the range `[first, last)` for the first sequence of count
15/// identical elements, each equal to the given value.
16template <typename ForwardIt, typename Size, typename ValueT, typename Predicate>
17[[nodiscard]] constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const& value, Predicate pred)
18 -> ForwardIt
19{
20 if (count <= Size{}) {
21 return first;
22 }
23
24 auto localCounter = Size{};
25 ForwardIt found = nullptr;
26
27 for (; first != last; ++first) {
28 if (pred(*first, value)) {
29 localCounter++;
30 if (found == nullptr) {
31 found = first;
32 }
33 } else {
34 localCounter = 0;
35 }
36
37 if (localCounter == count) {
38 return found;
39 }
40 }
41
42 return last;
43}
44
45template <typename ForwardIt, typename Size, typename ValueT>
46[[nodiscard]] constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const& value) -> ForwardIt
47{
48 return etl::search_n(first, last, count, value, etl::equal_to());
49}
50
51/// @}
52
53} // namespace etl
54
55#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:17
constexpr auto search_n(ForwardIt first, ForwardIt last, Size count, ValueT const &value) -> ForwardIt
Searches the range [first, last) for the first sequence of count identical elements,...
Definition search_n.hpp:46
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15