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/functional.hpp"
9
#
include
"arene/base/algorithm/detail/traits.hpp"
10
#
include
"arene/base/constraints.hpp"
11
#
include
"arene/base/type_traits/is_compare.hpp"
12
#
include
"arene/base/type_traits/iterator_category_traits.hpp"
13
#
include
"arene/base/utility/swap.hpp"
14
#
include
"stdlib/include/stdlib_detail/enable_if.hpp"
15
#
include
"stdlib/include/stdlib_detail/internal_disable_constexpr_in_cpp14.hpp"
16
#
include
"stdlib/include/stdlib_detail/is_constructible.hpp"
17
#
include
"stdlib/include/stdlib_detail/is_copy_assignable.hpp"
18
#
include
"stdlib/include/stdlib_detail/move.hpp"
19
#
include
"stdlib/include/stdlib_detail/pair.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
28
namespace
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
///
61
template
<
62
class
ForwardIterator
,
63
class
Compare
,
64
arene
::
base
::
constraints
<
65
std
::
enable_if_t
<
arene
::
base
::
is_forward_iterator_v
<
ForwardIterator
>>,
66
std
::
enable_if_t
<
arene
::
base
::
is_compare_v
<
67
Compare
&,
68
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
ForwardIterator
>>>> =
nullptr
>
69
constexpr
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
++) &&
//
75
std
::
is_nothrow_copy_assignable_v
<
ForwardIterator
> &&
//
76
std
::
is_nothrow_constructible_v
<
77
std
::
pair
<
ForwardIterator
,
ForwardIterator
>,
78
ForwardIterator
&,
79
ForwardIterator
&>
80
) ->
std
::
pair
<
ForwardIterator
,
ForwardIterator
> {
81
std
::
detail
::
disable_constexpr_in_cpp14
();
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
///
163
template
<
164
class
ForwardIterator
,
165
arene
::
base
::
constraints
<
166
std
::
enable_if_t
<
arene
::
base
::
is_forward_iterator_v
<
ForwardIterator
>>,
167
std
::
enable_if_t
<
arene
::
base
::
is_compare_v
<
168
decltype
(
arene
::
base
::
algorithm_detail
::
operator_less
)&,
169
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
ForwardIterator
>>>> =
nullptr
>
170
constexpr
auto
minmax_element
(
ForwardIterator
first
,
ForwardIterator
last
)
noexcept
(
171
noexcept
(
std
::
minmax_element
(
std
::
move
(
first
),
std
::
move
(
last
),
arene
::
base
::
algorithm_detail
::
operator_less
))
172
) ->
std
::
pair
<
ForwardIterator
,
ForwardIterator
> {
173
std
::
detail
::
disable_constexpr_in_cpp14
();
174
return
std
::
minmax_element
(
std
::
move
(
first
),
std
::
move
(
last
),
arene
::
base
::
algorithm_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_
std::hash<::arene::base::result< void, E > >::operator()
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
stdlib
include
stdlib_detail
minmax_element.hpp
Generated by
1.13.2