tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
minmax_element.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_MINMAX_ELEMENT_HPP
4#define TETL_ALGORITHM_MINMAX_ELEMENT_HPP
5
9
10namespace etl {
11
14template <typename ForwardIt, typename Compare>
15[[nodiscard]] constexpr auto minmax_element(ForwardIt first, ForwardIt last, Compare comp) -> pair<ForwardIt, ForwardIt>
16{
17 auto min = first;
18 auto max = first;
19
20 if (first == last or ++first == last) {
21 return {min, max};
22 }
23
24 if (comp(*first, *min)) {
25 min = first;
26 } else {
27 max = first;
28 }
29
30 while (++first != last) {
31 auto i = first;
32 if (++first == last) {
33 if (comp(*i, *min)) {
34 min = i;
35 } else if (not comp(*i, *max)) {
36 max = i;
37 }
38 break;
39 }
40
41 if (comp(*first, *i)) {
42 if (comp(*first, *min)) {
43 min = first;
44 }
45 if (not comp(*i, *max)) {
46 max = i;
47 }
48 } else {
49 if (comp(*i, *min)) {
50 min = i;
51 }
52 if (not comp(*first, *max)) {
53 max = first;
54 }
55 }
56 }
57
58 return {min, max};
59}
60
63template <typename ForwardIt>
64[[nodiscard]] constexpr auto minmax_element(ForwardIt first, ForwardIt last) -> pair<ForwardIt, ForwardIt>
65{
66 return etl::minmax_element(first, last, etl::less());
67}
68
69} // namespace etl
70
71#endif // TETL_ALGORITHM_MINMAX_ELEMENT_HPP
constexpr auto min(Type const &a, Type const &b, Compare comp) noexcept -> Type const &
Returns the smaller of a and b, using a compare function.
Definition min.hpp:13
constexpr auto minmax_element(ForwardIt first, ForwardIt last, Compare comp) -> pair< ForwardIt, ForwardIt >
Finds the smallest and greatest element in the range [first, last).
Definition minmax_element.hpp:15
constexpr auto max(Type const &a, Type const &b, Compare comp) noexcept -> Type const &
Returns the greater of a and b, using a compare function.
Definition max.hpp:13
Definition adjacent_find.hpp:8
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14
etl::pair is a class template that provides a way to store two heterogeneous objects as a single unit...
Definition pair.hpp:36