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/functional.hpp"
16#include "arene/base/algorithm/detail/traits.hpp"
17#include "arene/base/algorithm/rotate.hpp"
18#include "arene/base/constraints.hpp"
19#include "arene/base/type_traits.hpp"
20#include "arene/base/type_traits/is_compare.hpp"
21#include "arene/base/utility.hpp"
22#include "stdlib/include/stdlib_detail/enable_if.hpp"
23#include "stdlib/include/stdlib_detail/ignore.hpp"
24#include "stdlib/include/stdlib_detail/is_assignable.hpp"
25#include "stdlib/include/stdlib_detail/is_move_assignable.hpp"
26#include "stdlib/include/stdlib_detail/is_move_constructible.hpp"
27#include "stdlib/include/stdlib_detail/lower_bound.hpp"
28#include "stdlib/include/stdlib_detail/move.hpp"
29// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
30
31namespace std {
32// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
33/// @brief Copy the first _K_ elements from the source range, as sorted with respect to the supplied comparator, into
34/// the target range, where _K_ is the minimum of @c std::distance(source_begin,source_end) and @c
35/// std::distance(target_begin,target_end).
36///
37/// This performs at most <c>N * (log2(K) + 1)</c> comparisons, where _N_ is @c std::distance(source_begin,source_end)
38///
39/// @tparam SourceIterator The type of the iterator for the input range
40/// @tparam TargetIterator The type of the iterator for the output range
41/// @tparam Comparator The comparator type
42/// @param source_begin The start of the input range
43/// @param source_end The end of the input range
44/// @param target_begin The start of the output range
45/// @param target_end The end of the output range
46/// @param comp The comparator
47/// @return A @c TargetIterator referring to one-past the last stored element in the output
48/// @pre The @c SourceIterator must be an input iterator, and @c [source_begin,source_end) must be a valid range
49/// @pre The @c TargetIterator must be a random access iterator, and @c [target_begin,target_end) must be a valid range
50/// @pre The result of dereferencing a source iterator must be assignable to the result of dereferencing a target
51/// iterator
52/// @pre The value type of the iterators must be less-than-comparable
53/// @pre The comparator must be a binary predicate such that @c comp(*source_begin,*target_begin) and @c
54/// comp(*target_begin,*target_begin) are both well-formed and convertible to @c bool
55template <
56 typename SourceIterator,
57 typename TargetIterator,
58 typename Comparator,
76 nullptr>
83) -> TargetIterator {
85 return target_begin;
86 }
88 auto const target_last = target_end - 1;
92 auto const next_last = current_last + 1;
94 bool const is_room{current_last != target_last};
95 bool const new_is_smaller{target_pos != next_last};
96 if ((!new_is_smaller) && (!is_room)) {
97 continue;
98 }
99 if (is_room) {
101 }
102 *current_last = std::forward<decltype(elem)>(elem);
103 if (new_is_smaller) {
105 }
106 }
107 return current_last + 1;
108}
109// parasoft-end-suppress AUTOSAR-M3_3_2-a
110
111/// @brief Copy the first _K_ elements from the source range, as sorted with respect to @c operator< , into the target
112/// range, where _K_ is the minimum of @c std::distance(source_begin,source_end) and @c
113/// std::distance(target_begin,target_end)
114///
115/// This performs at most <c>N * (log2(K) + 1)</c> comparisons, where _N_ is @c std::distance(source_begin,source_end)
116///
117/// @tparam SourceIterator The type of the iterator for the input range
118/// @tparam TargetIterator The type of the iterator for the output range
119/// @param source_begin The start of the input range
120/// @param source_end The end of the input range
121/// @param target_begin The start of the output range
122/// @param target_end The end of the output range
123/// @return A @c TargetIterator referring to one-past the last stored element in the output
124/// @pre The @c SourceIterator must be an input iterator, and @c [source_begin,source_end) must be a valid range
125/// @pre The @c TargetIterator must be a random access iterator, and @c [target_begin,target_end) must be a valid range
126/// @pre The result of dereferencing a source iterator must be assignable to the result of dereferencing a target
127/// iterator
128/// @pre The value type of the iterators must be less-than-comparable
129template <
130 typename SourceIterator,
131 typename TargetIterator,
157
158} // namespace std
159
160#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_PARTIAL_SORT_COPY_HPP_
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827