Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
internal_relative_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_INTERNAL_RELATIVE_FIND_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTERNAL_RELATIVE_FIND_HPP_
7
8#include "arene/base/algorithm/detail/functional.hpp"
9#include "arene/base/algorithm/detail/traits.hpp"
10#include "arene/base/constraints.hpp"
11#include "arene/base/type_traits/is_predicate.hpp"
12#include "arene/base/type_traits/iterator_category_traits.hpp"
13#include "stdlib/include/stdlib_detail/enable_if.hpp"
14#include "stdlib/include/stdlib_detail/is_copy_assignable.hpp"
15#include "stdlib/include/stdlib_detail/is_copy_constructible.hpp"
16
17// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
18// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
19
20// IWYU pragma: friend "stdlib_detail/.*"
21
22namespace std {
23namespace internal {
24
25// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
26
27/// @brief finds the element that best satisfies a relative metric
28/// @tparam ForwardIterator iterator type
29/// @tparam BinaryPredicate binary predicate type
30/// @param first beginning of the range of elements
31/// @param last end of the range of elements
32/// @param pred binary predicate type implementing the relative metric
33///
34/// @return iterator to the first element that best satisfies @c pred.
35///
36template <
37 class ForwardIterator,
38 class BinaryPredicate,
48 noexcept(first == last) && //
49 noexcept(++first != last) && //
50 noexcept(pred(*first, *first))
51) -> ForwardIterator {
52 if (first == last) {
53 return first;
54 }
55
56 auto best = first;
57
58 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "This is an iterator type, so incrementing is OK"
59 ++first;
60 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
61 while (first != last) {
62 if (pred(*first, *best)) {
63 best = first;
64 }
65 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "This is an iterator type, so incrementing is OK"
66 ++first;
67 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
68 }
69
70 return best;
71}
72
73// parasoft-end-suppress AUTOSAR-M3_3_2-a
74
75} // namespace internal
76} // namespace std
77
78#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTERNAL_RELATIVE_FIND_HPP_
Definition internal_named_requirements.hpp:19
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