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/functional.hpp"
15#include "arene/base/algorithm/detail/traits.hpp"
16#include "arene/base/constraints.hpp"
17#include "arene/base/type_traits/is_predicate.hpp"
18#include "arene/base/type_traits/iterator_category_traits.hpp"
19#include "stdlib/include/stdlib_detail/enable_if.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,
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,
107
108} // namespace std
109
110#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ADJACENT_FIND_HPP_
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827