Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
stable_sort.hpp
Go to the documentation of this file.
1#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STABLE_SORT_HPP_
2#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STABLE_SORT_HPP_
3
4// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
5// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
6
7// Copyright 2026, Toyota Motor Corporation
8//
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10
11// IWYU pragma: private, include <algorithm>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/algorithm/detail/traits.hpp"
17#include "arene/base/functional/detail/operator_less.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/move.hpp"
24
25namespace std {
26
27// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
28/// @brief sorts a range into ascending order
29///
30/// @tparam I iterator type
31/// @tparam C compare type
32/// @param first beginning of the range
33/// @param last end of the range
34/// @param comp comparison function object
35///
36/// @pre @c I must satisfy the random access iterator requirements.
37/// @pre @c I must be ValueSwappable.
38/// @pre the type of <c>*first</c> must be Movable.
39/// @pre <c>[first, last)</c> must be a valid range.
40/// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
41/// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
42/// reference type of @c I, regardless of value category, and must not modify
43/// @c r1 or @c r2.
44/// @pre comp must define a *strict weak ordering relation*. For values
45/// @c a, @c b, and @c c, the following properties must be satisfied:
46/// * irreflexivity: <c> comp(a, a) </c> is @c false
47/// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
48/// @c true, then <c> comp(a, c) </c> is @c true
49/// * transitivity of incomparability: let <c> e(a, b) </c> be
50/// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
51///
52/// Sorts the elements in the range <c> [first, last) </c> in non-descending
53/// order, specified by @c comp. The order of equal elements is guaranteed to
54/// be preserved.
55///
56/// @post <c> is_sorted(first, last, comp) </c> is @c true
57///
58/// @note Complexity <br>
59/// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
60/// applications of @c comp.
61///
62/// @note This sorting algorithm is stable.
63///
64template <
65 class I,
66 class C,
67 class = enable_if_t<
72 >
73 >
74auto stable_sort(I first, I last, C comp) noexcept( //
75 arene::base::is_nothrow_invocable_v<decltype(arene::base::stable_sort), I&&, I&&, C&&>
76) -> void {
77 return arene::base::stable_sort(std::move(first), std::move(last), std::move(comp));
78}
79// parasoft-end-suppress AUTOSAR-M3_3_2-a
80
81/// @brief sorts a range into ascending order
82///
83/// @tparam I iterator type
84/// @param first beginning of the range
85/// @param last end of the range
86///
87/// @pre @c I must satisfy the random access iterator requirements.
88/// @pre @c I must be ValueSwappable.
89/// @pre the type of <c>*first</c> must be Movable.
90/// @pre <c>[first, last)</c> must be a valid range.
91/// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
92/// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
93/// reference type of @c I, regardless of value category, and must not modify
94/// @c r1 or @c r2.
95/// @pre comp must define a *strict weak ordering relation*. For values
96/// @c a, @c b, and @c c, the following properties must be satisfied:
97/// * irreflexivity: <c> comp(a, a) </c> is @c false
98/// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
99/// @c true, then <c> comp(a, c) </c> is @c true
100/// * transitivity of incomparability: let <c> e(a, b) </c> be
101/// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
102///
103/// Sorts the elements in the range <c> [first, last) </c> in non-descending
104/// order, specified by @c operator<. The order of equal elements is
105/// guaranteed to be preserved.
106///
107/// @post <c> is_sorted(first, last) </c> is @c true
108///
109/// @note Complexity <br>
110/// * Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
111/// applications of @c operator<.
112///
113/// @note This sorting algorithm is stable.
114///
115template <
116 class I,
117 class = enable_if_t<
122 decltype(arene::base::functional_detail::operator_less)&,
123 arene::base::algorithm_detail::iter_reference_t<I>>
124 >
125 >
126auto stable_sort(I first, I last) noexcept( //
127 arene::base::is_nothrow_invocable_v<decltype(arene::base::stable_sort), I&&, I&&, decltype(arene::base::functional_detail::operator_less)>
128) -> void {
129 return arene::base::stable_sort(std::move(first), std::move(last), arene::base::functional_detail::operator_less);
130}
131
132} // namespace std
133
134#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STABLE_SORT_HPP_
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.
STL namespace.
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
auto stable_sort(I first, I last, C comp) noexcept(//arene::base::is_nothrow_invocable_v<, I &&, I &&, C && >) -> void
sorts a range into ascending order
Definition stable_sort.hpp:74