tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
find_end.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_FIND_END_HPP
5#define TETL_ALGORITHM_FIND_END_HPP
6
7#include <etl/_algorithm/search.hpp>
8#include <etl/_functional/equal_to.hpp>
9
10namespace etl {
11
12/// \brief Searches for the last occurrence of the sequence [sFirst, sLast) in
13/// the range `[first, last)`. Elements are compared using the given binary
14/// predicate p.
15/// \param first The range of elements to examine
16/// \param last The range of elements to examine
17/// \param sFirst The range of elements to search for
18/// \param sLast The range of elements to search for
19/// \param p Binary predicate
20/// \returns Iterator to the beginning of last occurrence of the sequence
21/// [sFirst, sLast) in range `[first, last)`. If [sFirst, sLast) is empty or if
22/// no such sequence is found, last is returned.
23/// \ingroup algorithm
24template <typename ForwardIt1, typename ForwardIt2, typename Predicate>
25[[nodiscard]] constexpr auto
26find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 sFirst, ForwardIt2 sLast, Predicate p) -> ForwardIt1
27{
28 if (sFirst == sLast) {
29 return last;
30 }
31 auto result = last;
32 while (true) {
33 auto newResult = etl::search(first, last, sFirst, sLast, p);
34 if (newResult == last) {
35 break;
36 }
37 result = newResult;
38 first = result;
39 ++first;
40 }
41 return result;
42}
43
44/// \ingroup algorithm
45template <typename ForwardIt1, typename ForwardIt2>
46[[nodiscard]] constexpr auto find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 sFirst, ForwardIt2 sLast)
47 -> ForwardIt1
48{
49 return etl::find_end(first, last, sFirst, sLast, etl::equal_to());
50}
51
52} // namespace etl
53
54#endif // TETL_ALGORITHM_FIND_END_HPP
constexpr auto find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 sFirst, ForwardIt2 sLast) -> ForwardIt1
Definition find_end.hpp:46
constexpr auto find_end(ForwardIt1 first, ForwardIt1 last, ForwardIt2 sFirst, ForwardIt2 sLast, Predicate p) -> ForwardIt1
Searches for the last occurrence of the sequence [sFirst, sLast) in the range [first,...
Definition find_end.hpp:26
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15