tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
search.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_HPP
5#define TETL_ALGORITHM_SEARCH_HPP
6
7#include <etl/_functional/equal_to.hpp>
8
9namespace etl {
10
11/// \ingroup algorithm
12/// @{
13
14/// Searches for the first occurrence of the sequence of elements
15/// [sFirst, sLast) in the range `[first, last)`.
16///
17/// https://en.cppreference.com/w/cpp/algorithm/search
18///
19/// \param first The range of elements to examine.
20/// \param last The range of elements to examine.
21/// \param sFirst The range of elements to search for.
22/// \param sLast The range of elements to search for.
23/// \param pred Binary predicate which returns ​true if the elements should be treated as equal.
24template <typename FwdIt1, typename FwdIt2, typename Predicate>
25[[nodiscard]] constexpr auto search(FwdIt1 first, FwdIt1 last, FwdIt2 sFirst, FwdIt2 sLast, Predicate pred) -> FwdIt1
26{
27 for (;; ++first) {
28 auto it = first;
29 for (auto sIt = sFirst;; ++it, (void)++sIt) {
30 if (sIt == sLast) {
31 return first;
32 }
33 if (it == last) {
34 return last;
35 }
36 if (not pred(*it, *sIt)) {
37 break;
38 }
39 }
40 }
41}
42
43template <typename FwdIt1, typename FwdIt2>
44[[nodiscard]] constexpr auto search(FwdIt1 first, FwdIt1 last, FwdIt2 sFirst, FwdIt2 sLast) -> FwdIt1
45{
46 return etl::search(first, last, sFirst, sLast, etl::equal_to());
47}
48
49template <typename FwdIt, typename Searcher>
50[[nodiscard]] constexpr auto search(FwdIt first, FwdIt last, Searcher const& searcher) -> FwdIt
51{
52 return searcher(first, last).first;
53}
54
55/// @}
56
57} // namespace etl
58
59#endif // TETL_ALGORITHM_SEARCH_HPP
constexpr auto search(FwdIt first, FwdIt last, Searcher const &searcher) -> FwdIt
Definition search.hpp:50
constexpr auto search(FwdIt1 first, FwdIt1 last, FwdIt2 sFirst, FwdIt2 sLast) -> FwdIt1
Definition search.hpp:44
constexpr auto search(FwdIt1 first, FwdIt1 last, FwdIt2 sFirst, FwdIt2 sLast, Predicate pred) -> FwdIt1
Definition search.hpp:25
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15