tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
adjacent_find.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_ADJACENT_FIND_HPP
5#define TETL_ALGORITHM_ADJACENT_FIND_HPP
6
7#include <etl/_functional/equal_to.hpp>
8
9namespace etl {
10/// \ingroup algorithm
11/// @{
12
13/// \brief Searches the range `[first, last)` for two consecutive equal
14/// elements. Elements are compared using the given binary predicate p.
15///
16/// \param first The range of elements to examine.
17/// \param last The range of elements to examine.
18/// \param pred Binary predicate which returns ​true if the elements should be
19/// treated as equal.
20///
21/// https://en.cppreference.com/w/cpp/algorithm/adjacent_find
22template <typename ForwardIt, typename Predicate>
23[[nodiscard]] constexpr auto adjacent_find(ForwardIt first, ForwardIt last, Predicate pred) -> ForwardIt
24{
25 if (first == last) {
26 return last;
27 }
28
29 auto next = first;
30 ++next;
31
32 for (; next != last; ++next, (void)++first) {
33 if (pred(*first, *next)) {
34 return first;
35 }
36 }
37
38 return last;
39}
40
41template <typename ForwardIt>
42[[nodiscard]] constexpr auto adjacent_find(ForwardIt first, ForwardIt last) -> ForwardIt
43{
44 return etl::adjacent_find(first, last, etl::equal_to());
45}
46
47/// @}
48
49} // namespace etl
50
51#endif // TETL_ALGORITHM_ADJACENT_FIND_HPP
constexpr auto adjacent_find(ForwardIt first, ForwardIt last) -> ForwardIt
Searches the range [first, last) for two consecutive equal elements. Elements are compared using the ...
Definition adjacent_find.hpp:42
constexpr auto adjacent_find(ForwardIt first, ForwardIt last, Predicate pred) -> ForwardIt
Searches the range [first, last) for two consecutive equal elements. Elements are compared using the ...
Definition adjacent_find.hpp:23
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15