Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
min_element.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_MIN_ELEMENT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MIN_ELEMENT_HPP_
7
8#include "arene/base/algorithm/detail/relative_find.hpp"
9#include "arene/base/algorithm/detail/traits.hpp"
11#include "arene/base/functional/detail/operator_less.hpp"
18
19// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
20// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
21
22// IWYU pragma: private, include <algorithm>
23// IWYU pragma: friend "stdlib_detail/.*"
24
25namespace std {
26
27// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
28
29/// @brief returns the smallest element in a range
30/// @tparam ForwardIterator iterator type
31/// @tparam Compare binary predicate type
32/// @param first beginning of the range of elements
33/// @param last end of the range of elements
34/// @param comp binary predicate which returns @c true if the first argument is ordered before the second
35///
36/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
37/// @pre <tt>[begin, end)</tt> must be a valid range.
38/// @pre The expression <tt>comp(e1, e2)</tt> must be convertible to @c bool
39/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the
40/// reference type of @c ForwardIterator, regardless of value category, and
41/// must not modify @c e1 or @c e2.
42///
43/// Finds the smallest element, i.e. the element ordered before all other
44/// elements, in the range <tt>[first, last)</tt>.
45///
46/// @return iterator to the smallest element of the range <tt>[first, last)</tt>.
47/// If several elements in the range are equivalent to the smallest element,
48/// returns the iterator to the first such element. Returns @c last if the
49/// range is empty.
50///
51/// @note Complexity
52/// Given N as <tt>std::distance(first, last)</tt>, exactly <tt>max(N - 1,
53/// 0)</tt> applications of the comparator @c comp.
54///
55template <
56 class ForwardIterator,
57 class Compare,
62 nullptr>
63constexpr auto min_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(
64 noexcept(arene::base::algorithm_detail::relative_find(
65 std::move(first),
66 std::move(last),
67 std::move(comp),
69 ))
70) -> ForwardIterator {
71 constexpr auto identity = arene::base::identity{};
72 return arene::base::algorithm_detail::relative_find(std::move(first), std::move(last), std::move(comp), identity);
73}
74
75/// @brief returns the smallest element in a range
76/// @tparam ForwardIterator iterator type
77/// @tparam Compare binary predicate type
78/// @param first beginning of the range of elements
79/// @param last end of the range of elements
80///
81/// @pre @c ForwardIterator 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
85/// reference type of @c ForwardIterator, regardless of value category, and
86/// must not modify @c e1 or @c e2.
87///
88/// Finds the smallest element, i.e. the element ordered before all other
89/// elements, in the range <tt>[first, last)</tt>.
90///
91/// @return iterator to the smallest element of the range <tt>[first, last)</tt>.
92/// If several elements in the range are equivalent to the smallest element,
93/// returns the iterator to the first such element. Returns @c last if the
94/// range is empty.
95///
96/// @note Complexity
97/// Given N as <tt>std::distance(first, last)</tt>, exactly <tt>max(N - 1,
98/// 0)</tt> comparisons using @c operator<.
99///
100template <
101 class ForwardIterator,
105 decltype(arene::base::functional_detail::operator_less)&,
106 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>,
107 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
108constexpr auto min_element(ForwardIterator first, ForwardIterator last) noexcept(
109 noexcept(std::min_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less))
110) -> ForwardIterator {
111 return std::min_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less);
112}
113
114// parasoft-end-suppress AUTOSAR-M3_3_2-a
115
116} // namespace std
117
118#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MIN_ELEMENT_HPP_
A backport of the C++20 std::identity function object. It returns its input unchanged.
Definition identity.hpp:21
Public export header for the constraints subpackage.
constexpr bool is_compare_v
determines if a type satisfies the Compare named requirement
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.
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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 declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
constexpr auto min_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(noexcept(arene::base::algorithm_detail::relative_find(std::move(first), std::move(last), std::move(comp), std::declval< arene::base::identity const & >()))) -> ForwardIterator
returns the smallest element in a range
Definition min_element.hpp:63