Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
max_element.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::max_element"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_MAX_ELEMENT_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_MAX_ELEMENT_HPP_
9
10// IWYU pragma: private, include "arene/base/algorithm.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13#include "arene/base/algorithm/detail/relative_find.hpp"
14#include "arene/base/algorithm/detail/traits.hpp"
17#include "arene/base/functional/detail/flip.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
21#include "arene/base/stdlib_choice/less.hpp"
22#include "arene/base/stdlib_choice/move.hpp"
26
27namespace arene {
28namespace base {
29namespace algorithm_detail {
30namespace max_element_detail {
31
32/// @brief function object implementing the max_element algorithm
33class max_element_fn {
34 public:
35 /// @brief returns the largest element in a range
36 /// @tparam ForwardIterator iterator type
37 /// @tparam Compare binary predicate type, defaults to <c>std::less<></c>
38 /// @tparam Projection projection applied to each element before comparison,
39 /// defaults to <c>arene::base::identity</c>
40 /// @param first beginning of the range of elements
41 /// @param last end of the range of elements
42 /// @param comp binary predicate which returns @c true if the first projected
43 /// argument is ordered before the second projected argument
44 /// @param proj projection applied to each element before it is passed to @c comp
45 ///
46 /// @pre @c ForwardIterator must satisfy the forward iterator requirements.
47 /// @pre <c>[begin, end)</c> must be a valid range.
48 /// @pre The expression <c>comp(invoke(proj, e1), invoke(proj, e2))</c> must
49 /// be convertible to @c bool for every argument @c e1, @c e2 of type @c RT,
50 /// where @c RT is the reference type of @c ForwardIterator, regardless of
51 /// value category, and must not modify @c e1 or @c e2.
52 ///
53 /// Finds the largest element, i.e. the element whose projected value is
54 /// ordered after the projected values of all other elements, in the range
55 /// <c>[first, last)</c>.
56 ///
57 /// @return iterator to the largest element of the range <c>[first, last)</c>,
58 /// i.e. the element @c e for which <c>invoke(proj, e)</c> is ordered after
59 /// the projection of every other element. If several elements in the range
60 /// are equivalent to the largest element, returns the iterator to the first
61 /// such element. Returns @c last if the range is empty.
62 ///
63 /// @note Complexity
64 /// Given N as <c>std::distance(first, last)</c>, exactly <c>max(N - 1,
65 /// 0)</c> applications of the comparator @c comp.
66 template <
67 class ForwardIterator,
68 class Compare = std::less<>,
69 class Projection = identity,
74 nullptr>
75 constexpr auto operator()(ForwardIterator first, ForwardIterator last, Compare comp = {}, Projection proj = {}) const
76 noexcept( //
77 noexcept(relative_find(std::move(first), std::move(last), functional_detail::flip(comp), proj))
78 ) -> ForwardIterator {
79 return relative_find(std::move(first), std::move(last), functional_detail::flip(comp), proj);
80 }
81};
82
83} // namespace max_element_detail
84} // namespace algorithm_detail
85
86/// @brief Find an iterator to the greatest element of a range.
87// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
88// object used in multiple TUs."
89// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
90// object used in multiple TUs."
91ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::max_element_detail::max_element_fn, max_element);
92// parasoft-end-suppress AUTOSAR-M7_3_3-a
93// parasoft-end-suppress CERT_CPP-DCL59-a
94
95} // namespace base
96} // namespace arene
97#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_MAX_ELEMENT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
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
Definition accumulate.hpp:36
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