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/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 stable_sort_detail {
32
33/// @brief function object implementing the stable sort algorithm
34///
35class stable_sort_impl_fn {
36 public:
37 /// @brief sorts a range into ascending order
38 ///
39 /// @tparam I iterator type
40 /// @tparam C compare type
41 /// @param first beginning of the range
42 /// @param last end of the range
43 /// @param comp comparison function object
44 ///
45 /// @pre @c I must satisfy the random access iterator requirements.
46 /// @pre @c I must be ValueSwappable.
47 /// @pre the type of <c>*first</c> must be Movable.
48 /// @pre <c>[first, last)</c> must be a valid range.
49 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
50 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
51 /// reference type of @c I, regardless of value category, and must not modify
52 /// @c r1 or @c r2.
53 /// @pre comp must define a *strict weak ordering relation*. For values
54 /// @c a, @c b, and @c c, the following properties must be satisfied:
55 /// * irreflexivity: <c> comp(a, a) </c> is @c false
56 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
57 /// @c true, then <c> comp(a, c) </c> is @c true
58 /// * transitivity of incomparability: let <c> e(a, b) </c> be
59 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
60 ///
61 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
62 /// order, specified by @c comp. The order of equal elements is guaranteed to
63 /// be preserved.
64 ///
65 /// @post <c> is_sorted(first, last, comp) </c> is @c true
66 ///
67 /// @note Complexity <br>
68 /// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
69 /// applications of @c comp.
70 ///
71 /// @note This sorting algorithm is stable.
72 ///
73 template <
74 class I,
75 class C,
76 class = std::enable_if_t<
81 >>
82 constexpr auto operator()(I first, I last, C comp) const noexcept( //
83 is_nothrow_invocable_v<decltype(inplace_merge_sort), I&&, I&&, C&&>
84 ) -> void {
85 inplace_merge_sort(std::move(first), std::move(last), std::move(comp));
86 }
87
88 /// @brief sorts a range into ascending order
89 ///
90 /// @tparam I iterator type
91 /// @param first beginning of the range
92 /// @param last end of the range
93 ///
94 /// @pre @c I must satisfy the random access iterator requirements.
95 /// @pre @c I must be ValueSwappable.
96 /// @pre the type of <c>*first</c> must be Movable.
97 /// @pre <c>[first, last)</c> must be a valid range.
98 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
99 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
100 /// reference type of @c I, regardless of value category, and must not modify
101 /// @c r1 or @c r2.
102 /// @pre comp must define a *strict weak ordering relation*. For values
103 /// @c a, @c b, and @c c, the following properties must be satisfied:
104 /// * irreflexivity: <c> comp(a, a) </c> is @c false
105 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
106 /// @c true, then <c> comp(a, c) </c> is @c true
107 /// * transitivity of incomparability: let <c> e(a, b) </c> be
108 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
109 ///
110 /// Sorts the elements in the range <c> [first, last) </c> in non-descending
111 /// order, specified by @c std::less<>. The order of equal elements is
112 /// guaranteed to be preserved.
113 ///
114 /// @post <c> is_sorted(first, last) </c> is @c true
115 ///
116 /// @note Complexity <br>
117 /// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
118 /// applications of @c std::less<>.
119 ///
120 /// @note This sorting algorithm is stable.
121 ///
122 template <
123 class I,
124 class = std::enable_if_t<
129 >>
130 constexpr auto operator()(I first, I last) const noexcept( //
132 ) -> void {
133 (*this)(std::move(first), std::move(last), std::less<>{});
134 }
135};
136} // namespace stable_sort_detail
137} // namespace algorithm_detail
138
139/// @brief Sort a range while preserving the order of equivalent elements.
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."
144// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
146 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::stable_sort_detail::stable_sort_impl_fn>,
147 stable_sort
148);
149// parasoft-end-suppress CERT_CPP-DCL56-a
150// parasoft-end-suppress AUTOSAR-M7_3_3-a
151// parasoft-end-suppress CERT_CPP-DCL59-a
152
153} // namespace base
154} // namespace arene
155#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_STABLE_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