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/traits.hpp"
15#include "arene/base/compiler_support/cpp14_inline.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/less.hpp"
18#include "arene/base/stdlib_choice/move.hpp"
19#include "arene/base/type_traits/is_compare.hpp"
20#include "arene/base/type_traits/is_invocable.hpp"
21#include "arene/base/type_traits/is_movable.hpp"
22#include "arene/base/type_traits/is_swappable.hpp"
23#include "arene/base/type_traits/iterator_category_traits.hpp"
24#include "arene/base/type_traits/remove_cvref.hpp"
25// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
26
27namespace arene {
28namespace base {
29namespace algorithm_detail {
30namespace sort_detail {
31
32/// @brief function object implementing the sort algorithm
33///
34class sort_impl_fn {
35 public:
36 /// @brief sorts a range into ascending order
37 /// @tparam I iterator type
38 /// @tparam C compare type
39 /// @param first beginning of the range
40 /// @param last end of the range
41 /// @param comp comparison function object
42 ///
43 /// @pre @c I must satisfy the random access iterator requirements.
44 /// @pre @c I must be ValueSwappable.
45 /// @pre the type of <c>*first</c> must be Movable.
46 /// @pre <c>[first, last)</c> must be a valid range.
47 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
48 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
49 /// reference type of @c I, regardless of value category, and must not modify
50 /// @c r1 or @c r2.
51 /// @pre comp must define a *strict weak ordering relation*. For values
52 /// @c a, @c b, and @c c, the following properties must be satisfied:
53 /// * irreflexivity: <c> comp(a, a) </c> is @c false
54 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
55 /// @c true, then <c> pred(a, c) </c> is @c true
56 /// * transitivity of incomparability: let <c> e(a, b) </c> be
57 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
58 ///
59 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
60 /// order, specified by @c comp. The order of equal elements is not guaranteed
61 /// to be preserved.
62 ///
63 /// @post <c> is_sorted(first, last, comp) </c> is @c true
64 ///
65 /// @note Complexity
66 /// Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
67 /// applications of @c comp.
68 ///
69 template <
70 class I,
71 class C,
72 class = std::enable_if_t<
73 is_random_access_iterator_v<I> &&
74 is_swappable_v<iter_reference_t<I>> &&
75 is_movable_v<remove_cvref_t<iter_reference_t<I>>> &&
76 is_compare_v<C&, iter_reference_t<I>>
77 >>
78 constexpr auto operator()(I first, I last, C comp) const noexcept( //
79 is_nothrow_invocable_v<decltype(heap_sort), I&&, I&&, C&&>
80 ) -> void {
81 heap_sort(std::move(first), std::move(last), std::move(comp));
82 }
83
84 /// @brief sorts a range into ascending order
85 /// @tparam I iterator type
86 /// @param first beginning of the range
87 /// @param last end of the range
88 ///
89 /// @pre @c I must satisfy the random access iterator requirements.
90 /// @pre @c I must be ValueSwappable.
91 /// @pre the type of <c>*first</c> must be Movable.
92 /// @pre <c>[first, last)</c> must be a valid range.
93 /// @pre The expression <c>std::less<>{}(r1, r2)</c> must be convertible to @c bool
94 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
95 /// reference type of @c I, regardless of value category, and must not modify
96 /// @c r1 or @c r2.
97 /// @pre @c std::less<> must define a *strict weak ordering relation*. For values
98 /// @c a, @c b, and @c c, the following properties must be satisfied:
99 /// * irreflexivity: <c> std::less<>{}(a, a) </c> is @c false
100 /// * transitivity: if <c> std::less<>{}(a, b) </c> and
101 /// <c> std::less<>{}(b, c) </c> are both @c true, then
102 /// <c> std::less<>{}(a, c) </c> is @c true
103 /// * transitivity of incomparability: let <c> e(a, b) </c> be
104 /// <c> !std::less<>{}(a, b) && !std::less<>{}(b, a) </c>, then @c e is
105 /// transitive
106 ///
107 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
108 /// order, specified by @c std::less<>. The order of equal elements is not
109 /// guaranteed to be preserved.
110 ///
111 /// @note Complexity
112 /// Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
113 /// applications of @c std::less<>.
114 ///
115 template <
116 class I,
117 class = std::enable_if_t<
118 is_random_access_iterator_v<I> &&
119 is_swappable_v<iter_reference_t<I>> &&
120 is_movable_v<remove_cvref_t<iter_reference_t<I>>> &&
121 is_compare_v<std::less<>&, iter_reference_t<I>>>>
122 constexpr auto operator()(I first, I last) const noexcept( //
123 is_nothrow_invocable_v<sort_impl_fn const&, I&&, I&&, std::less<>>
124 ) -> void {
125 return (*this)(std::move(first), std::move(last), std::less<>{});
126 }
127};
128
129} // namespace sort_detail
130} // namespace algorithm_detail
131
132/// @def arene::base::sort
133/// @copydoc arene::base::algorithm_detail::sort_detail::sort_impl_fn::operator()
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."
138ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::sort_detail::sort_impl_fn, sort);
139// parasoft-end-suppress AUTOSAR-M7_3_3-a
140// parasoft-end-suppress CERT_CPP-DCL59-a
141
142} // namespace base
143} // namespace arene
144#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_SORT_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::sort_detail::sort_impl_fn, sort)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10