Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
sort.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::sort"
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_SORT_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_SORT_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/heap_sort.hpp"
14#include "arene/base/algorithm/detail/synthesize_range1_overload.hpp" // IWYU pragma: export
15#include "arene/base/algorithm/detail/traits.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/less.hpp"
19#include "arene/base/stdlib_choice/move.hpp"
26// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
27
28namespace arene {
29namespace base {
30namespace algorithm_detail {
31namespace sort_detail {
32
33/// @brief function object implementing the sort algorithm
34///
35class sort_impl_fn {
36 public:
37 /// @brief sorts a range into ascending order
38 /// @tparam I iterator type
39 /// @tparam C compare type
40 /// @param first beginning of the range
41 /// @param last end of the range
42 /// @param comp comparison function object
43 ///
44 /// @pre @c I must satisfy the random access iterator requirements.
45 /// @pre @c I must be ValueSwappable.
46 /// @pre the type of <c>*first</c> must be Movable.
47 /// @pre <c>[first, last)</c> must be a valid range.
48 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
49 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
50 /// reference type of @c I, regardless of value category, and must not modify
51 /// @c r1 or @c r2.
52 /// @pre comp must define a *strict weak ordering relation*. For values
53 /// @c a, @c b, and @c c, the following properties must be satisfied:
54 /// * irreflexivity: <c> comp(a, a) </c> is @c false
55 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
56 /// @c true, then <c> pred(a, c) </c> is @c true
57 /// * transitivity of incomparability: let <c> e(a, b) </c> be
58 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
59 ///
60 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
61 /// order, specified by @c comp. The order of equal elements is not guaranteed
62 /// to be preserved.
63 ///
64 /// @post <c> is_sorted(first, last, comp) </c> is @c true
65 ///
66 /// @note Complexity
67 /// Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
68 /// applications of @c comp.
69 ///
70 template <
71 class I,
72 class C,
73 class = std::enable_if_t<
78 >>
79 constexpr auto operator()(I first, I last, C comp) const noexcept( //
80 is_nothrow_invocable_v<decltype(heap_sort), I&&, I&&, C&&>
81 ) -> void {
82 heap_sort(std::move(first), std::move(last), std::move(comp));
83 }
84
85 /// @brief sorts a range into ascending order
86 /// @tparam I iterator type
87 /// @param first beginning of the range
88 /// @param last end of the range
89 ///
90 /// @pre @c I must satisfy the random access iterator requirements.
91 /// @pre @c I must be ValueSwappable.
92 /// @pre the type of <c>*first</c> must be Movable.
93 /// @pre <c>[first, last)</c> must be a valid range.
94 /// @pre The expression <c>std::less<>{}(r1, r2)</c> must be convertible to @c bool
95 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
96 /// reference type of @c I, regardless of value category, and must not modify
97 /// @c r1 or @c r2.
98 /// @pre @c std::less<> must define a *strict weak ordering relation*. For values
99 /// @c a, @c b, and @c c, the following properties must be satisfied:
100 /// * irreflexivity: <c> std::less<>{}(a, a) </c> is @c false
101 /// * transitivity: if <c> std::less<>{}(a, b) </c> and
102 /// <c> std::less<>{}(b, c) </c> are both @c true, then
103 /// <c> std::less<>{}(a, c) </c> is @c true
104 /// * transitivity of incomparability: let <c> e(a, b) </c> be
105 /// <c> !std::less<>{}(a, b) && !std::less<>{}(b, a) </c>, then @c e is
106 /// transitive
107 ///
108 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
109 /// order, specified by @c std::less<>. The order of equal elements is not
110 /// guaranteed to be preserved.
111 ///
112 /// @note Complexity
113 /// Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
114 /// applications of @c std::less<>.
115 ///
116 template <
117 class I,
118 class = std::enable_if_t<
123 constexpr auto operator()(I first, I last) const noexcept( //
125 ) -> void {
126 return (*this)(std::move(first), std::move(last), std::less<>{});
127 }
128};
129
130} // namespace sort_detail
131} // namespace algorithm_detail
132
133/// @brief Sort a range in ascending order using the supplied comparison.
134// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
135// object used in multiple TUs."
136// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
137// object used in multiple TUs."
138// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
140 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::sort_detail::sort_impl_fn>,
141 sort
142);
143// parasoft-end-suppress CERT_CPP-DCL56-a
144// parasoft-end-suppress AUTOSAR-M7_3_3-a
145// parasoft-end-suppress CERT_CPP-DCL59-a
146
147} // namespace base
148} // namespace arene
149#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_SORT_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
constexpr bool is_compare_v
determines if a type satisfies the Compare named requirement
constexpr bool is_random_access_iterator_v
Definition iterator_category_traits.hpp:332
constexpr bool is_movable_v
Backport for the C++20 movable concept.
constexpr bool is_swappable_v
Trait query indicating if a type can be swapped.
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename iter_reference_detail::iter_reference_impl< Itr >::type iter_reference_t
Determins the reference type of the given iterator.
Definition iter_reference_t.hpp:47
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