Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
adjacent_find.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ADJACENT_FIND_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ADJACENT_FIND_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <algorithm>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/algorithm/detail/traits.hpp"
16#include "arene/base/functional/detail/operator_equal.hpp"
20
21namespace std {
22
23/// @brief finds the first two adjacent items that satisfy a given predicate
24/// @tparam ForwardIterator iterator type
25/// @tparam BinaryPred binary predicate type
26/// @param first the beginning of the range
27/// @param last the end of the range
28/// @param pred binary predicate which returns @c true if the elements should be treated as equal
29///
30/// @pre @c ForwardIteratorerator must satisfy the forward iterator requirements.
31/// @pre <tt>[begin, end)</tt> must be a valid range.
32/// @pre The expression <tt>pred(e1, e2)</tt> must be convertible to @c bool
33/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the reference type
34/// of @c ForwardIterator, regardless of value category, and must not modify
35/// @c e1 or @c e2.
36///
37/// @return iterator to the first element of the range <tt>[first, last)</tt>
38/// such that <tt>pred(*it, *(it + 1)) != false</tt> or @c last if no such element is found.
39///
40/// @note Complexity
41/// For a nonempty range, exactly <tt>min((i - first) + 1, (last - first) - 1)</tt> applications of the
42/// corresponding predicate, where i is adjacent_find's return value.
43template <
44 class ForwardIterator,
45 class BinaryPred,
49 BinaryPred&,
50 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>,
51 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
52auto adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPred pred) noexcept( //
53 noexcept(++first) && //
54 noexcept(first++) && //
55 noexcept(first == last) && //
56 noexcept(first != last) && //
57 noexcept(pred(*first, *first))
58) -> ForwardIterator {
59 if (first == last) {
60 return last;
61 }
62
63 auto prev = first++;
64 while (first != last) {
65 if (pred(*prev, *first)) {
66 return prev;
67 }
68
69 ++first;
70 ++prev;
71 }
72
73 return last;
74}
75
76/// @brief finds the first two adjacent items that are equal
77/// @tparam ForwardIterator iterator type
78/// @param first the beginning of the range
79/// @param last the end of the range
80///
81/// @pre @c ForwardIteratorerator must satisfy the forward iterator requirements.
82/// @pre <tt>[begin, end)</tt> must be a valid range.
83/// @pre The expression <tt>e1 == e2</tt> must be convertible to @c bool
84/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the reference type
85/// of @c ForwardIterator, regardless of value category, and must not modify
86/// @c e1 or @c e2.
87///
88/// @return iterator to the first element of the range <tt>[first, last)</tt>
89/// such that <tt>*it == *(it + 1)</tt> or @c last if no such element is found.
90///
91/// @note Complexity
92/// For a nonempty range, exactly <tt>min((i - first) + 1, (last - first) - 1)</tt> equality comparisons, where i is
93/// adjacent_find's return value.
94template <
95 class ForwardIterator,
99 arene::base::functional_detail::operator_equal_t&,
100 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>,
101 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
102auto adjacent_find(ForwardIterator first, ForwardIterator last) noexcept(
103 noexcept(std::adjacent_find(first, last, arene::base::functional_detail::operator_equal))
104) -> ForwardIterator {
105 return std::adjacent_find(first, last, arene::base::functional_detail::operator_equal);
106}
107
108} // namespace std
109
110#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ADJACENT_FIND_HPP_
Public export header for the constraints subpackage.
constexpr bool is_predicate_v
backport for the C++20 predicate concept
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
STL namespace.
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPred pred) noexcept(//noexcept(++first) &&//noexcept(first++) &&//noexcept(first==last) &&//noexcept(first !=last) &&//noexcept(pred(*first, *first))) -> ForwardIterator
finds the first two adjacent items that satisfy a given predicate
Definition adjacent_find.hpp:52