Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
inplace_merge.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::inplace_merge"
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_INPLACE_MERGE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_INPLACE_MERGE_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/bind_back_with_iterator.hpp"
14#include "arene/base/algorithm/detail/traits.hpp"
22#include "arene/base/stdlib_choice/declval.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
25#include "arene/base/stdlib_choice/is_move_assignable.hpp"
26#include "arene/base/stdlib_choice/move.hpp"
35
36// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
37// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
38// IWYU pragma: no_include "arene/base/stdlib_choice/remove_reference.hpp"
39
40namespace arene {
41namespace base {
42namespace algorithm_detail {
43namespace inplace_merge_detail {
44
45/// @brief function object implementing the @c inplace_merge algorithm
46///
47class inplace_merge_impl_fn {
48 public:
49 /// @brief merges a range consisting of two ordered sub-ranges in-place so the whole range is sorted
50 /// @tparam BidirectionalIt iterator type
51 /// @tparam Compare compare type
52 /// @param first beginning of the first range
53 /// @param middle end of the first range, beginning of the second range
54 /// @param last end of the second range
55 /// @param comp comparison function object
56 ///
57 /// @pre @c BidirectionalIt must satisfy the bidirectional iterator requirements.
58 /// @pre @c BidiectionalIt must be ValueSwappable.
59 /// @pre the type of <c>*first</c> must be Movable.
60 /// @pre @c Compare must be Compare.
61 /// @pre <c>[first, last)</c> must be a valid range.
62 /// @pre <c> is_sorted(first, middle, comp) </c> is @c true
63 /// @pre <c> is_sorted(middle, last, comp) </c> is @c true
64 /// @pre The expression <c>comp(r1, r2)</c> must be convertible to @c bool
65 /// for every argument @c r1 and @c r2 of type @c RT, where @c RT is the
66 /// reference type of @c I, regardless of value category, and must not modify
67 /// @c r1 or @c r2.
68 /// @pre comp must define a *strict weak ordering relation*. For values
69 /// @c a, @c b, and @c c, the following properties must be satisfied:
70 /// * irreflexivity: <c> comp(a, a) </c> is @c false
71 /// * transitivity: if <c> comp(a, b) </c> and <c> comp(b, c) </c> are both
72 /// @c true, then <c> comp(a, c) </c> is @c true
73 /// * transitivity of incomparability: let <c> e(a, b) </c> be
74 /// <c> !comp(a, b) && !comp(b, a) </c>, then @c e is transitive
75 ///
76 /// Merges two consecutive sorted ranges <c> [first, middle) </c> and <c>
77 /// [middle, last) </c> into one sorted range <c> [first, last) </c>.
78 ///
79 /// This merge function is stable, which means that for equivalent elements in
80 /// the original two ranges, the elements from the first range (preserving
81 /// their original order) precede the elements from the second range
82 /// (preserving their original order).
83 ///
84 /// @post <c> is_sorted(first, last, comp) </c> is @c true
85 ///
86 /// @note Complexity
87 /// Given N as <c> last - first </c>, worst case of <c> O(N * log(N)) </c>
88 /// applications of @c comp.
89 /// @note This implementation never tries to allocate additional memory.
90 template <
91 class BidirectionalIt,
92 class Compare,
98 constexpr auto operator()(BidirectionalIt first, BidirectionalIt middle, BidirectionalIt last, Compare comp) const
99 noexcept( //
103 noexcept(arene::base::find_if( //
106 std::declval<bind_back_with_iterator<Compare, BidirectionalIt>>() //
107 )) && //
108 is_nothrow_invocable_v<decltype(rotate), BidirectionalIt, BidirectionalIt, BidirectionalIt>&& //
109 noexcept(arene::base::advance( //
114 ) //
115 )) && //
117 ) -> void {
118 auto left = first;
119 auto right = middle;
120
121 while ((left != middle) && (right != last)) {
122 auto right_end = arene::base::find_if( //
123 right,
124 last,
125 not_fn(bind_back_with_iterator<Compare, BidirectionalIt>{comp, left})
126 );
127 if (right_end == right) {
128 ++left;
129 } else {
130 auto const block_size = arene::base::distance(right, right_end);
131 left = rotate(left, right, right_end);
132 arene::base::advance(middle, block_size);
133 right = std::move(right_end);
134 }
135 }
136 }
137};
138
139} // namespace inplace_merge_detail
140} // namespace algorithm_detail
141
142/// @brief Merge adjacent sorted ranges in place.
143// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
144// object used in multiple TUs."
145// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
146// object used in multiple TUs."
147ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::inplace_merge_detail::inplace_merge_impl_fn, inplace_merge);
148// parasoft-end-suppress AUTOSAR-M7_3_3-a
149// parasoft-end-suppress CERT_CPP-DCL59-a
150
151} // namespace base
152} // namespace arene
153#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_INPLACE_MERGE_HPP_
Provides a backport of C++17's constexpr std::advance.
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
constexpr auto find_if(Iterator first, Iterator last, Predicate pred) noexcept(denotes_nothrow_iterable_range_v< Iterator > &&base::is_nothrow_invocable_v< Predicate &, typename std::iterator_traits< Iterator >::reference >) -> Iterator
Returns an iterator to the first element in the range [first, last) that satisfies equality criteria:...
Definition find_if.hpp:42
constexpr auto distance(Iterator first, Iterator last) noexcept(noexcept(distance_impl::do_distance(first, last, typename std::iterator_traits< Iterator >::iterator_category{}))) -> typename std::iterator_traits< Iterator >::difference_type
Find the number of elements between first and last.
Definition distance.hpp:79
constexpr bool denotes_nothrow_iterable_range_v
Check if an iterator/sentinel pair form an iterable range that can be iterated without throwing.
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
constexpr bool is_nothrow_equality_comparable_v
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...