Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
stable_sort.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::stable_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_STABLE_SORT_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_STABLE_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/inplace_merge_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 stable_sort_detail {
31
32/// @brief function object implementing the stable sort algorithm
33///
34class stable_sort_impl_fn {
35 public:
36 /// @brief sorts a range into ascending order
37 ///
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> comp(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 guaranteed to
62 /// be preserved.
63 ///
64 /// @post <c> is_sorted(first, last, comp) </c> is @c true
65 ///
66 /// @note Complexity <br>
67 /// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
68 /// applications of @c comp.
69 ///
70 /// @note This sorting algorithm is stable.
71 ///
72 template <
73 class I,
74 class C,
75 class = std::enable_if_t<
76 is_random_access_iterator_v<I> &&
77 is_swappable_v<iter_reference_t<I>> &&
78 is_movable_v<remove_cvref_t<iter_reference_t<I>>> &&
79 is_compare_v<C&, iter_reference_t<I>>
80 >>
81 constexpr auto operator()(I first, I last, C comp) const noexcept( //
82 is_nothrow_invocable_v<decltype(inplace_merge_sort), I&&, I&&, C&&>
83 ) -> void {
84 inplace_merge_sort(std::move(first), std::move(last), std::move(comp));
85 }
86
87 /// @brief sorts a range into ascending order
88 ///
89 /// @tparam I iterator type
90 /// @param first beginning of the range
91 /// @param last end of the range
92 ///
93 /// @pre @c I must satisfy the random access iterator requirements.
94 /// @pre @c I must be ValueSwappable.
95 /// @pre the type of <c>*first</c> must be Movable.
96 /// @pre <c>[first, last)</c> must be a valid range.
97 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
98 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
99 /// reference type of @c I, regardless of value category, and must not modify
100 /// @c r1 or @c r2.
101 /// @pre comp must define a *strict weak ordering relation*. For values
102 /// @c a, @c b, and @c c, the following properties must be satisfied:
103 /// * irreflexivity: <c> comp(a, a) </c> is @c false
104 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
105 /// @c true, then <c> comp(a, c) </c> is @c true
106 /// * transitivity of incomparability: let <c> e(a, b) </c> be
107 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
108 ///
109 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
110 /// order, specified by @c std::less<>. The order of equal elements is
111 /// guaranteed to be preserved.
112 ///
113 /// @post <c> is_sorted(first, last) </c> is @c true
114 ///
115 /// @note Complexity <br>
116 /// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
117 /// applications of @c std::less<>.
118 ///
119 /// @note This sorting algorithm is stable.
120 ///
121 template <
122 class I,
123 class = std::enable_if_t<
124 is_random_access_iterator_v<I> &&
125 is_swappable_v<iter_reference_t<I>> &&
126 is_movable_v<remove_cvref_t<iter_reference_t<I>>> &&
127 is_compare_v<std::less<>&, iter_reference_t<I>>
128 >>
129 constexpr auto operator()(I first, I last) const noexcept( //
130 is_nothrow_invocable_v<stable_sort_impl_fn, I&&, I&&, std::less<>>
131 ) -> void {
132 (*this)(std::move(first), std::move(last), std::less<>{});
133 }
134};
135} // namespace stable_sort_detail
136} // namespace algorithm_detail
137
138/// @def arene::base::stable_sort
139/// @copydoc arene::base::algorithm_detail::stable_sort_detail::stable_sort_impl_fn::operator()
140// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
141// object used in multiple TUs."
142// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
143// object used in multiple TUs."
144ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::stable_sort_detail::stable_sort_impl_fn, stable_sort);
145// parasoft-end-suppress AUTOSAR-M7_3_3-a
146// parasoft-end-suppress CERT_CPP-DCL59-a
147
148} // namespace base
149} // namespace arene
150#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_STABLE_SORT_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::stable_sort_detail::stable_sort_impl_fn, stable_sort)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10