Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
partial_sort_copy.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_PARTIAL_SORT_COPY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_PARTIAL_SORT_COPY_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <algorithm>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/algorithm/detail/traits.hpp"
18#include "arene/base/functional/detail/operator_less.hpp"
31// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
32
33namespace std {
34// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
35/// @brief Copy the first _K_ elements from the source range, as sorted with respect to the supplied comparator, into
36/// the target range, where _K_ is the minimum of @c std::distance(source_begin,source_end) and @c
37/// std::distance(target_begin,target_end).
38///
39/// This performs at most <c>N * (log2(K) + 1)</c> comparisons, where _N_ is @c std::distance(source_begin,source_end)
40///
41/// @tparam SourceIterator The type of the iterator for the input range
42/// @tparam TargetIterator The type of the iterator for the output range
43/// @tparam Comparator The comparator type
44/// @param source_begin The start of the input range
45/// @param source_end The end of the input range
46/// @param target_begin The start of the output range
47/// @param target_end The end of the output range
48/// @param comp The comparator
49/// @return A @c TargetIterator referring to one-past the last stored element in the output
50/// @pre The @c SourceIterator must be an input iterator, and @c [source_begin,source_end) must be a valid range
51/// @pre The @c TargetIterator must be a random access iterator, and @c [target_begin,target_end) must be a valid range
52/// @pre The result of dereferencing a source iterator must be assignable to the result of dereferencing a target
53/// iterator
54/// @pre The value type of the iterators must be less-than-comparable
55/// @pre The comparator must be a binary predicate such that @c comp(*source_begin,*target_begin) and @c
56/// comp(*target_begin,*target_begin) are both well-formed and convertible to @c bool
57template <
58 typename SourceIterator,
59 typename TargetIterator,
60 typename Comparator,
66 arene::base::algorithm_detail::iter_reference_t<TargetIterator>,
67 arene::base::algorithm_detail::iter_reference_t<SourceIterator>>>,
73 Comparator&,
74 arene::base::algorithm_detail::iter_reference_t<SourceIterator>,
75 arene::base::algorithm_detail::iter_reference_t<TargetIterator>>>,
78 nullptr>
80 SourceIterator source_begin,
81 SourceIterator source_end,
82 TargetIterator target_begin,
83 TargetIterator target_end,
84 Comparator comp
85) -> TargetIterator {
86 if ((target_begin == target_end) || (source_begin == source_end)) {
87 return target_begin;
88 }
89 auto current_last = target_begin;
90 auto const target_last = target_end - 1;
91 *current_last = *source_begin;
92 ++source_begin;
93 for (auto&& elem : arene::base::make_subrange(std::move(source_begin), std::move(source_end))) {
94 auto const next_last = current_last + 1;
95 auto target_pos = std::lower_bound(target_begin, next_last, elem, comp);
96 bool const is_room{current_last != target_last};
97 bool const new_is_smaller{target_pos != next_last};
98 if ((!new_is_smaller) && (!is_room)) {
99 continue;
100 }
101 if (is_room) {
102 current_last = next_last;
103 }
104 *current_last = std::forward<decltype(elem)>(elem);
105 if (new_is_smaller) {
106 ignore = arene::base::rotate(target_pos, current_last, current_last + 1);
107 }
108 }
109 return current_last + 1;
110}
111// parasoft-end-suppress AUTOSAR-M3_3_2-a
112
113/// @brief Copy the first _K_ elements from the source range, as sorted with respect to @c operator< , into the target
114/// range, where _K_ is the minimum of @c std::distance(source_begin,source_end) and @c
115/// std::distance(target_begin,target_end)
116///
117/// This performs at most <c>N * (log2(K) + 1)</c> comparisons, where _N_ is @c std::distance(source_begin,source_end)
118///
119/// @tparam SourceIterator The type of the iterator for the input range
120/// @tparam TargetIterator The type of the iterator for the output range
121/// @param source_begin The start of the input range
122/// @param source_end The end of the input range
123/// @param target_begin The start of the output range
124/// @param target_end The end of the output range
125/// @return A @c TargetIterator referring to one-past the last stored element in the output
126/// @pre The @c SourceIterator must be an input iterator, and @c [source_begin,source_end) must be a valid range
127/// @pre The @c TargetIterator must be a random access iterator, and @c [target_begin,target_end) must be a valid range
128/// @pre The result of dereferencing a source iterator must be assignable to the result of dereferencing a target
129/// iterator
130/// @pre The value type of the iterators must be less-than-comparable
131template <
132 typename SourceIterator,
133 typename TargetIterator,
139 arene::base::algorithm_detail::iter_reference_t<TargetIterator>,
140 arene::base::algorithm_detail::iter_reference_t<SourceIterator>>>,
146 SourceIterator source_begin,
147 SourceIterator source_end,
148 TargetIterator target_begin,
149 TargetIterator target_end
150) -> TargetIterator {
151 return partial_sort_copy(
152 std::move(source_begin),
153 std::move(source_end),
154 std::move(target_begin),
155 std::move(target_end),
156 arene::base::functional_detail::operator_less
157 );
158}
159
160} // namespace std
161
162#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_PARTIAL_SORT_COPY_HPP_
Public export header for the constraints subpackage.
constexpr bool is_compare_v
determines if a type satisfies the Compare named requirement
constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(std::is_nothrow_move_constructible< Iterator >::value &&//std::is_nothrow_move_constructible< Sentinel >::value) -> make_subrange_detail::iterator_range< Iterator, Sentinel >
Combine an iterator and sentinel into a range type that can be used with range-based for loops.
Definition make_subrange.hpp:98
constexpr bool is_less_than_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
STL namespace.
constexpr auto lower_bound(ForwardIterator first, ForwardIterator last, T const &value, Compare comp) noexcept(noexcept(std::internal::partition_point(std::move(first), std::move(last), arene::base::bind_back_detail::bind_back_t< Compare, T const & >(comp, value)))) -> ForwardIterator
returns an iterator to the first element not less than the given value
Definition lower_bound.hpp:63
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
constexpr bool is_assignable_v
Type trait to detect if an instance of To is assignable to from arguments of From.
auto partial_sort_copy(SourceIterator source_begin, SourceIterator source_end, TargetIterator target_begin, TargetIterator target_end, Comparator comp) -> TargetIterator
Copy the first K elements from the source range, as sorted with respect to the supplied comparator,...
Definition partial_sort_copy.hpp:79
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24