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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_MINMAX_ELEMENT_HPP
5#define TETL_ALGORITHM_MINMAX_ELEMENT_HPP
6
7#include <etl/_functional/less.hpp>
8#include <etl/_iterator/iterator_traits.hpp>
9#include <etl/_utility/pair.hpp>
10
11namespace etl {
12
13/// \brief Finds the smallest and greatest element in the range `[first, last)`.
14/// \ingroup algorithm
15template <typename ForwardIt, typename Compare>
16[[nodiscard]] constexpr auto minmax_element(ForwardIt first, ForwardIt last, Compare comp) -> pair<ForwardIt, ForwardIt>
17{
18 auto min = first;
19 auto max = first;
20
21 if (first == last or ++first == last) {
22 return {min, max};
23 }
24
25 if (comp(*first, *min)) {
26 min = first;
27 } else {
28 max = first;
29 }
30
31 while (++first != last) {
32 auto i = first;
33 if (++first == last) {
34 if (comp(*i, *min)) {
35 min = i;
36 } else if (not comp(*i, *max)) {
37 max = i;
38 }
39 break;
40 }
41
42 if (comp(*first, *i)) {
43 if (comp(*first, *min)) {
44 min = first;
45 }
46 if (not comp(*i, *max)) {
47 max = i;
48 }
49 } else {
50 if (comp(*i, *min)) {
51 min = i;
52 }
53 if (not comp(*first, *max)) {
54 max = first;
55 }
56 }
57 }
58
59 return {min, max};
60}
61
62/// \brief Finds the smallest and greatest element in the range `[first, last)`.
63/// \ingroup algorithm
64template <typename ForwardIt>
65[[nodiscard]] constexpr auto minmax_element(ForwardIt first, ForwardIt last) -> pair<ForwardIt, ForwardIt>
66{
67 return etl::minmax_element(first, last, etl::less());
68}
69
70} // namespace etl
71
72#endif // TETL_ALGORITHM_MINMAX_ELEMENT_HPP
constexpr auto minmax_element(ForwardIt first, ForwardIt last) -> pair< ForwardIt, ForwardIt >
Finds the smallest and greatest element in the range [first, last).
Definition minmax_element.hpp:65
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:16
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15
etl::pair is a class template that provides a way to store two heterogeneous objects as a single unit...
Definition pair.hpp:37