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
31
namespace
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
55
template
<
56
typename
SourceIterator
,
57
typename
TargetIterator
,
58
typename
Comparator
,
59
arene
::
base
::
constraints
<
60
enable_if_t
<
arene
::
base
::
denotes_range_v
<
SourceIterator
>>,
61
enable_if_t
<
arene
::
base
::
denotes_range_v
<
TargetIterator
>>,
62
enable_if_t
<
arene
::
base
::
is_random_access_iterator_v
<
TargetIterator
>>,
63
enable_if_t
<
is_assignable_v
<
64
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>,
65
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
SourceIterator
>>>,
66
enable_if_t
<
67
arene
::
base
::
is_less_than_comparable_v
<
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>>>,
68
enable_if_t
<
is_move_assignable_v
<
arene
::
base
::
algorithm_detail
::
iter_value_t
<
TargetIterator
>>>,
69
enable_if_t
<
is_move_constructible_v
<
arene
::
base
::
algorithm_detail
::
iter_value_t
<
TargetIterator
>>>,
70
enable_if_t
<
arene
::
base
::
is_compare_v
<
71
Comparator
&,
72
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
SourceIterator
>,
73
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>>>,
74
enable_if_t
<
75
arene
::
base
::
is_compare_v
<
Comparator
&,
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>>>> =
76
nullptr
>
77
auto
partial_sort_copy
(
78
SourceIterator
source_begin
,
79
SourceIterator
source_end
,
80
TargetIterator
target_begin
,
81
TargetIterator
target_end
,
82
Comparator
comp
83
) ->
TargetIterator
{
84
if
((
target_begin
==
target_end
) || (
source_begin
==
source_end
)) {
85
return
target_begin
;
86
}
87
auto
current_last
=
target_begin
;
88
auto
const
target_last
=
target_end
- 1;
89
*
current_last
= *
source_begin
;
90
++
source_begin
;
91
for
(
auto
&&
elem
:
arene
::
base
::
make_subrange
(
std
::
move
(
source_begin
),
std
::
move
(
source_end
))) {
92
auto
const
next_last
=
current_last
+ 1;
93
auto
target_pos
=
std
::
lower_bound
(
target_begin
,
next_last
,
elem
,
comp
);
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
) {
100
current_last
=
next_last
;
101
}
102
*
current_last
=
std
::
forward
<
decltype
(
elem
)>(
elem
);
103
if
(
new_is_smaller
) {
104
ignore
=
arene
::
base
::
rotate
(
target_pos
,
current_last
,
current_last
+ 1);
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
129
template
<
130
typename
SourceIterator
,
131
typename
TargetIterator
,
132
arene
::
base
::
constraints
<
133
enable_if_t
<
arene
::
base
::
denotes_range_v
<
SourceIterator
>>,
134
enable_if_t
<
arene
::
base
::
denotes_range_v
<
TargetIterator
>>,
135
enable_if_t
<
arene
::
base
::
is_random_access_iterator_v
<
TargetIterator
>>,
136
enable_if_t
<
is_assignable_v
<
137
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>,
138
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
SourceIterator
>>>,
139
enable_if_t
<
140
arene
::
base
::
is_less_than_comparable_v
<
arene
::
base
::
algorithm_detail
::
iter_reference_t
<
TargetIterator
>>>,
141
enable_if_t
<
is_move_assignable_v
<
arene
::
base
::
algorithm_detail
::
iter_value_t
<
TargetIterator
>>>,
142
enable_if_t
<
is_move_constructible_v
<
arene
::
base
::
algorithm_detail
::
iter_value_t
<
TargetIterator
>>>> =
nullptr
>
143
auto
partial_sort_copy
(
144
SourceIterator
source_begin
,
145
SourceIterator
source_end
,
146
TargetIterator
target_begin
,
147
TargetIterator
target_end
148
) ->
TargetIterator
{
149
return
partial_sort_copy
(
150
std
::
move
(
source_begin
),
151
std
::
move
(
source_end
),
152
std
::
move
(
target_begin
),
153
std
::
move
(
target_end
),
154
arene
::
base
::
algorithm_detail
::
operator_less
155
);
156
}
157
158
}
// namespace std
159
160
#
endif
// INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_PARTIAL_SORT_COPY_HPP_
std::hash<::arene::base::result< void, E > >::operator()
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
stdlib
include
stdlib_detail
partial_sort_copy.hpp
Generated by
1.13.2