Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
minmax_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_MINMAX_ELEMENT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MINMAX_ELEMENT_HPP_
7
8#include "arene/base/algorithm/detail/traits.hpp"
10#include "arene/base/functional/detail/operator_less.hpp"
20
21// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
22// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
23// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
24
25// IWYU pragma: private, include <algorithm>
26// IWYU pragma: friend "stdlib_detail/.*"
27
28namespace std {
29
30/// @brief returns the smallest and largest elements in a range
31/// @tparam ForwardIterator iterator type
32/// @tparam Compare binary predicate type
33/// @param first beginning of the range of elements
34/// @param last end of the range of elements
35/// @param comp binary predicate which returns @c true if the first argument is
36/// ordered before the second
37///
38/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
39/// @pre <tt>[begin, end)</tt> must be a valid range.
40/// @pre The expression <tt>comp(e1, e2)</tt> must be convertible to @c bool
41/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the
42/// reference type of @c ForwardIterator, regardless of value category, and
43/// must not modify @c e1 or @c e2.
44///
45/// Finds both the first smallest element, i.e. the element ordered before all
46/// other elements, and the last largest element, i.e. the element ordered after
47/// all other elements, in the range <tt>[first, last)</tt>.
48///
49/// @return pair of iterators to the smallest element of the range <tt>[first,
50/// last)</tt>. If several elements in the range are equivalent to the
51/// smallest element, returns the iterator to the first such element. If
52/// several elements in the range are equivalent to the largest element,
53/// returns the iterator to the last such element. Returns
54/// <tt>make_pair(first, first)</tt> if the range is empty.
55///
56/// @note Complexity
57/// Given N as <tt>std::distance(first, last)</tt>, at most
58/// <tt>max(floor(3*(N-1)/2), 0)</tt> applications of the comparator
59/// @c comp.
60///
61template <
62 class ForwardIterator,
63 class Compare,
67 Compare&,
68 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
69constexpr auto minmax_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(
70 noexcept(!comp(*first, *first)) && //
71 noexcept(first == first) && //
72 noexcept(first != first) && //
73 noexcept(++first) && //
74 noexcept(first++) && //
77 std::pair<ForwardIterator, ForwardIterator>,
78 ForwardIterator&,
79 ForwardIterator&>
80) -> std::pair<ForwardIterator, ForwardIterator> {
82 auto result = std::pair<ForwardIterator, ForwardIterator>{first, first};
83
84 if (first == last || ++first == last) {
85 return result;
86 }
87
88 auto& min = result.first;
89 auto& max = result.second;
90
91 if (comp(*first, *min)) {
92 min = first;
93 } else {
94 max = first;
95 }
96 ++first;
97
98 auto const update_with_last = [&min, &max, &comp](auto const& iter) {
99 if (comp(*iter, *min)) {
100 min = iter;
101 } else if (!comp(*iter, *max)) {
102 max = iter;
103 } else {
104 // empty clause required by M6-4-2
105 }
106 };
107
108 auto const update_with_next_two = [&min, &max, &comp](auto lower, auto upper) {
109 if (!comp(*lower, *upper)) {
110 arene::base::swap(lower, upper);
111 }
112 if (comp(*lower, *min)) {
113 min = lower;
114 }
115 if (!comp(*upper, *max)) {
116 max = upper;
117 }
118 };
119
120 while (first != last) {
121 auto const iter = first++;
122
123 if (first == last) {
124 update_with_last(iter);
125 } else {
126 update_with_next_two(first, iter);
127 ++first;
128 }
129 }
130
131 return result;
132}
133
134/// @brief returns the smallest and largest elements in a range
135/// @tparam ForwardIterator iterator type
136/// @tparam Compare binary predicate type
137/// @param first beginning of the range of elements
138/// @param last end of the range of elements
139///
140/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
141/// @pre <tt>[begin, end)</tt> must be a valid range.
142/// @pre The expression <tt>e1 < e2</tt> must be convertible to @c bool
143/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the
144/// reference type of @c ForwardIterator, regardless of value category, and
145/// must not modify @c e1 or @c e2.
146///
147/// Finds both the first smallest element, i.e. the element ordered before all
148/// other elements, and the last largest element, i.e. the element ordered after
149/// all other elements, in the range <tt>[first, last)</tt>.
150///
151/// @return pair of iterators to the smallest element of the range <tt>[first,
152/// last)</tt>. If several elements in the range are equivalent to the
153/// smallest element, returns the iterator to the first such element. If
154/// several elements in the range are equivalent to the largest element,
155/// returns the iterator to the last such element. Returns
156/// <tt>make_pair(first, first)</tt> if the range is empty.
157///
158/// @note Complexity
159/// Given N as <tt>std::distance(first, last)</tt>, at most
160/// <tt>max(floor(3*(N-1)/2), 0)</tt> applications of the comparator
161/// @c operator<.
162///
163template <
164 class ForwardIterator,
168 decltype(arene::base::functional_detail::operator_less)&,
169 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
170constexpr auto minmax_element(ForwardIterator first, ForwardIterator last) noexcept(
171 noexcept(std::minmax_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less))
172) -> std::pair<ForwardIterator, ForwardIterator> {
174 return std::minmax_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less);
175}
176
177// parasoft-end-suppress AUTOSAR-M3_3_2-a
178
179} // namespace std
180
181#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MINMAX_ELEMENT_HPP_
Public export header for the constraints subpackage.
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
constexpr bool is_compare_v
determines if a type satisfies the Compare named requirement
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
constexpr auto disable_constexpr_in_cpp14() noexcept -> void
function used to disable constexpr in C++14 but allow for it in C++17
Definition internal_disable_constexpr_in_cpp14.hpp:23
STL namespace.
constexpr auto max(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the largest value in the std::initializer_list.
Definition max_initializer_list_overload.hpp:49
constexpr auto minmax_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(noexcept(!comp(*first, *first)) &&//noexcept(first==first) &&//noexcept(first !=first) &&//noexcept(++first) &&//noexcept(first++) &&//std::is_nothrow_copy_assignable_v< ForwardIterator > &&//std::is_nothrow_constructible_v< std::pair< ForwardIterator, ForwardIterator >, ForwardIterator &, ForwardIterator & >) -> std::pair< ForwardIterator, ForwardIterator >
returns the smallest and largest elements in a range
Definition minmax_element.hpp:69
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
constexpr bool is_nothrow_copy_assignable_v
Type trait to detect if an instance of T is copy assignable without throwing.
constexpr bool is_nothrow_constructible_v
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
constexpr auto min(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the smallest value in the std::initializer_list.
Definition min_initializer_list_overload.hpp:50