algorithm revision 1.1.1.1 1 1.1 joerg // -*- C++ -*-
2 1.1 joerg //===-------------------------- algorithm ---------------------------------===//
3 1.1 joerg //
4 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 1.1 joerg // See https://llvm.org/LICENSE.txt for license information.
6 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 1.1 joerg //
8 1.1 joerg //===----------------------------------------------------------------------===//
9 1.1 joerg
10 1.1 joerg #ifndef _LIBCPP_ALGORITHM
11 1.1 joerg #define _LIBCPP_ALGORITHM
12 1.1 joerg
13 1.1 joerg /*
14 1.1 joerg algorithm synopsis
15 1.1 joerg
16 1.1 joerg #include <initializer_list>
17 1.1 joerg
18 1.1 joerg namespace std
19 1.1 joerg {
20 1.1 joerg
21 1.1 joerg template <class InputIterator, class Predicate>
22 1.1 joerg constexpr bool // constexpr in C++20
23 1.1 joerg all_of(InputIterator first, InputIterator last, Predicate pred);
24 1.1 joerg
25 1.1 joerg template <class InputIterator, class Predicate>
26 1.1 joerg constexpr bool // constexpr in C++20
27 1.1 joerg any_of(InputIterator first, InputIterator last, Predicate pred);
28 1.1 joerg
29 1.1 joerg template <class InputIterator, class Predicate>
30 1.1 joerg constexpr bool // constexpr in C++20
31 1.1 joerg none_of(InputIterator first, InputIterator last, Predicate pred);
32 1.1 joerg
33 1.1 joerg template <class InputIterator, class Function>
34 1.1 joerg constexpr Function // constexpr in C++20
35 1.1 joerg for_each(InputIterator first, InputIterator last, Function f);
36 1.1 joerg
37 1.1 joerg template<class InputIterator, class Size, class Function>
38 1.1 joerg constexpr InputIterator // constexpr in C++20
39 1.1 joerg for_each_n(InputIterator first, Size n, Function f); // C++17
40 1.1 joerg
41 1.1 joerg template <class InputIterator, class T>
42 1.1 joerg constexpr InputIterator // constexpr in C++20
43 1.1 joerg find(InputIterator first, InputIterator last, const T& value);
44 1.1 joerg
45 1.1 joerg template <class InputIterator, class Predicate>
46 1.1 joerg constexpr InputIterator // constexpr in C++20
47 1.1 joerg find_if(InputIterator first, InputIterator last, Predicate pred);
48 1.1 joerg
49 1.1 joerg template<class InputIterator, class Predicate>
50 1.1 joerg constexpr InputIterator // constexpr in C++20
51 1.1 joerg find_if_not(InputIterator first, InputIterator last, Predicate pred);
52 1.1 joerg
53 1.1 joerg template <class ForwardIterator1, class ForwardIterator2>
54 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
55 1.1 joerg find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2);
57 1.1 joerg
58 1.1 joerg template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
59 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
60 1.1 joerg find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62 1.1 joerg
63 1.1 joerg template <class ForwardIterator1, class ForwardIterator2>
64 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
65 1.1 joerg find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2);
67 1.1 joerg
68 1.1 joerg template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
69 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
70 1.1 joerg find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72 1.1 joerg
73 1.1 joerg template <class ForwardIterator>
74 1.1 joerg constexpr ForwardIterator // constexpr in C++20
75 1.1 joerg adjacent_find(ForwardIterator first, ForwardIterator last);
76 1.1 joerg
77 1.1 joerg template <class ForwardIterator, class BinaryPredicate>
78 1.1 joerg constexpr ForwardIterator // constexpr in C++20
79 1.1 joerg adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80 1.1 joerg
81 1.1 joerg template <class InputIterator, class T>
82 1.1 joerg constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
83 1.1 joerg count(InputIterator first, InputIterator last, const T& value);
84 1.1 joerg
85 1.1 joerg template <class InputIterator, class Predicate>
86 1.1 joerg constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
87 1.1 joerg count_if(InputIterator first, InputIterator last, Predicate pred);
88 1.1 joerg
89 1.1 joerg template <class InputIterator1, class InputIterator2>
90 1.1 joerg constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
91 1.1 joerg mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
92 1.1 joerg
93 1.1 joerg template <class InputIterator1, class InputIterator2>
94 1.1 joerg constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
95 1.1 joerg mismatch(InputIterator1 first1, InputIterator1 last1,
96 1.1 joerg InputIterator2 first2, InputIterator2 last2); // **C++14**
97 1.1 joerg
98 1.1 joerg template <class InputIterator1, class InputIterator2, class BinaryPredicate>
99 1.1 joerg constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
100 1.1 joerg mismatch(InputIterator1 first1, InputIterator1 last1,
101 1.1 joerg InputIterator2 first2, BinaryPredicate pred);
102 1.1 joerg
103 1.1 joerg template <class InputIterator1, class InputIterator2, class BinaryPredicate>
104 1.1 joerg constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
105 1.1 joerg mismatch(InputIterator1 first1, InputIterator1 last1,
106 1.1 joerg InputIterator2 first2, InputIterator2 last2,
107 1.1 joerg BinaryPredicate pred); // **C++14**
108 1.1 joerg
109 1.1 joerg template <class InputIterator1, class InputIterator2>
110 1.1 joerg constexpr bool // constexpr in C++20
111 1.1 joerg equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
112 1.1 joerg
113 1.1 joerg template <class InputIterator1, class InputIterator2>
114 1.1 joerg constexpr bool // constexpr in C++20
115 1.1 joerg equal(InputIterator1 first1, InputIterator1 last1,
116 1.1 joerg InputIterator2 first2, InputIterator2 last2); // **C++14**
117 1.1 joerg
118 1.1 joerg template <class InputIterator1, class InputIterator2, class BinaryPredicate>
119 1.1 joerg constexpr bool // constexpr in C++20
120 1.1 joerg equal(InputIterator1 first1, InputIterator1 last1,
121 1.1 joerg InputIterator2 first2, BinaryPredicate pred);
122 1.1 joerg
123 1.1 joerg template <class InputIterator1, class InputIterator2, class BinaryPredicate>
124 1.1 joerg constexpr bool // constexpr in C++20
125 1.1 joerg equal(InputIterator1 first1, InputIterator1 last1,
126 1.1 joerg InputIterator2 first2, InputIterator2 last2,
127 1.1 joerg BinaryPredicate pred); // **C++14**
128 1.1 joerg
129 1.1 joerg template<class ForwardIterator1, class ForwardIterator2>
130 1.1 joerg constexpr bool // constexpr in C++20
131 1.1 joerg is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 1.1 joerg ForwardIterator2 first2);
133 1.1 joerg
134 1.1 joerg template<class ForwardIterator1, class ForwardIterator2>
135 1.1 joerg constexpr bool // constexpr in C++20
136 1.1 joerg is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
138 1.1 joerg
139 1.1 joerg template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
140 1.1 joerg constexpr bool // constexpr in C++20
141 1.1 joerg is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 1.1 joerg ForwardIterator2 first2, BinaryPredicate pred);
143 1.1 joerg
144 1.1 joerg template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
145 1.1 joerg constexpr bool // constexpr in C++20
146 1.1 joerg is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2,
148 1.1 joerg BinaryPredicate pred); // **C++14**
149 1.1 joerg
150 1.1 joerg template <class ForwardIterator1, class ForwardIterator2>
151 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
152 1.1 joerg search(ForwardIterator1 first1, ForwardIterator1 last1,
153 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2);
154 1.1 joerg
155 1.1 joerg template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
156 1.1 joerg constexpr ForwardIterator1 // constexpr in C++20
157 1.1 joerg search(ForwardIterator1 first1, ForwardIterator1 last1,
158 1.1 joerg ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
159 1.1 joerg
160 1.1 joerg template <class ForwardIterator, class Size, class T>
161 1.1 joerg constexpr ForwardIterator // constexpr in C++20
162 1.1 joerg search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
163 1.1 joerg
164 1.1 joerg template <class ForwardIterator, class Size, class T, class BinaryPredicate>
165 1.1 joerg constexpr ForwardIterator // constexpr in C++20
166 1.1 joerg search_n(ForwardIterator first, ForwardIterator last,
167 1.1 joerg Size count, const T& value, BinaryPredicate pred);
168 1.1 joerg
169 1.1 joerg template <class InputIterator, class OutputIterator>
170 1.1 joerg constexpr OutputIterator // constexpr in C++20
171 1.1 joerg copy(InputIterator first, InputIterator last, OutputIterator result);
172 1.1 joerg
173 1.1 joerg template<class InputIterator, class OutputIterator, class Predicate>
174 1.1 joerg constexpr OutputIterator // constexpr in C++20
175 1.1 joerg copy_if(InputIterator first, InputIterator last,
176 1.1 joerg OutputIterator result, Predicate pred);
177 1.1 joerg
178 1.1 joerg template<class InputIterator, class Size, class OutputIterator>
179 1.1 joerg constexpr OutputIterator // constexpr in C++20
180 1.1 joerg copy_n(InputIterator first, Size n, OutputIterator result);
181 1.1 joerg
182 1.1 joerg template <class BidirectionalIterator1, class BidirectionalIterator2>
183 1.1 joerg constexpr BidirectionalIterator2 // constexpr in C++20
184 1.1 joerg copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 1.1 joerg BidirectionalIterator2 result);
186 1.1 joerg
187 1.1 joerg template <class ForwardIterator1, class ForwardIterator2>
188 1.1 joerg constexpr ForwardIterator2 // constexpr in C++20
189 1.1 joerg swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
190 1.1 joerg
191 1.1 joerg template <class ForwardIterator1, class ForwardIterator2>
192 1.1 joerg constexpr void // constexpr in C++20
193 1.1 joerg iter_swap(ForwardIterator1 a, ForwardIterator2 b);
194 1.1 joerg
195 1.1 joerg template <class InputIterator, class OutputIterator, class UnaryOperation>
196 1.1 joerg constexpr OutputIterator // constexpr in C++20
197 1.1 joerg transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
198 1.1 joerg
199 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
200 1.1 joerg constexpr OutputIterator // constexpr in C++20
201 1.1 joerg transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 1.1 joerg OutputIterator result, BinaryOperation binary_op);
203 1.1 joerg
204 1.1 joerg template <class ForwardIterator, class T>
205 1.1 joerg constexpr void // constexpr in C++20
206 1.1 joerg replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
207 1.1 joerg
208 1.1 joerg template <class ForwardIterator, class Predicate, class T>
209 1.1 joerg constexpr void // constexpr in C++20
210 1.1 joerg replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
211 1.1 joerg
212 1.1 joerg template <class InputIterator, class OutputIterator, class T>
213 1.1 joerg constexpr OutputIterator // constexpr in C++20
214 1.1 joerg replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 1.1 joerg const T& old_value, const T& new_value);
216 1.1 joerg
217 1.1 joerg template <class InputIterator, class OutputIterator, class Predicate, class T>
218 1.1 joerg constexpr OutputIterator // constexpr in C++20
219 1.1 joerg replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
220 1.1 joerg
221 1.1 joerg template <class ForwardIterator, class T>
222 1.1 joerg constexpr void // constexpr in C++20
223 1.1 joerg fill(ForwardIterator first, ForwardIterator last, const T& value);
224 1.1 joerg
225 1.1 joerg template <class OutputIterator, class Size, class T>
226 1.1 joerg constexpr OutputIterator // constexpr in C++20
227 1.1 joerg fill_n(OutputIterator first, Size n, const T& value);
228 1.1 joerg
229 1.1 joerg template <class ForwardIterator, class Generator>
230 1.1 joerg constexpr void // constexpr in C++20
231 1.1 joerg generate(ForwardIterator first, ForwardIterator last, Generator gen);
232 1.1 joerg
233 1.1 joerg template <class OutputIterator, class Size, class Generator>
234 1.1 joerg constexpr OutputIterator // constexpr in C++20
235 1.1 joerg generate_n(OutputIterator first, Size n, Generator gen);
236 1.1 joerg
237 1.1 joerg template <class ForwardIterator, class T>
238 1.1 joerg constexpr ForwardIterator // constexpr in C++20
239 1.1 joerg remove(ForwardIterator first, ForwardIterator last, const T& value);
240 1.1 joerg
241 1.1 joerg template <class ForwardIterator, class Predicate>
242 1.1 joerg constexpr ForwardIterator // constexpr in C++20
243 1.1 joerg remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
244 1.1 joerg
245 1.1 joerg template <class InputIterator, class OutputIterator, class T>
246 1.1 joerg constexpr OutputIterator // constexpr in C++20
247 1.1 joerg remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
248 1.1 joerg
249 1.1 joerg template <class InputIterator, class OutputIterator, class Predicate>
250 1.1 joerg constexpr OutputIterator // constexpr in C++20
251 1.1 joerg remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
252 1.1 joerg
253 1.1 joerg template <class ForwardIterator>
254 1.1 joerg constexpr ForwardIterator // constexpr in C++20
255 1.1 joerg unique(ForwardIterator first, ForwardIterator last);
256 1.1 joerg
257 1.1 joerg template <class ForwardIterator, class BinaryPredicate>
258 1.1 joerg constexpr ForwardIterator // constexpr in C++20
259 1.1 joerg unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
260 1.1 joerg
261 1.1 joerg template <class InputIterator, class OutputIterator>
262 1.1 joerg constexpr OutputIterator // constexpr in C++20
263 1.1 joerg unique_copy(InputIterator first, InputIterator last, OutputIterator result);
264 1.1 joerg
265 1.1 joerg template <class InputIterator, class OutputIterator, class BinaryPredicate>
266 1.1 joerg constexpr OutputIterator // constexpr in C++20
267 1.1 joerg unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
268 1.1 joerg
269 1.1 joerg template <class BidirectionalIterator>
270 1.1 joerg constexpr void // constexpr in C++20
271 1.1 joerg reverse(BidirectionalIterator first, BidirectionalIterator last);
272 1.1 joerg
273 1.1 joerg template <class BidirectionalIterator, class OutputIterator>
274 1.1 joerg constexpr OutputIterator // constexpr in C++20
275 1.1 joerg reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
276 1.1 joerg
277 1.1 joerg template <class ForwardIterator>
278 1.1 joerg constexpr ForwardIterator // constexpr in C++20
279 1.1 joerg rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
280 1.1 joerg
281 1.1 joerg template <class ForwardIterator, class OutputIterator>
282 1.1 joerg constexpr OutputIterator // constexpr in C++20
283 1.1 joerg rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
284 1.1 joerg
285 1.1 joerg template <class RandomAccessIterator>
286 1.1 joerg void
287 1.1 joerg random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
288 1.1 joerg
289 1.1 joerg template <class RandomAccessIterator, class RandomNumberGenerator>
290 1.1 joerg void
291 1.1 joerg random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
292 1.1 joerg RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
293 1.1 joerg
294 1.1 joerg template<class PopulationIterator, class SampleIterator,
295 1.1 joerg class Distance, class UniformRandomBitGenerator>
296 1.1 joerg SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 1.1 joerg SampleIterator out, Distance n,
298 1.1 joerg UniformRandomBitGenerator&& g); // C++17
299 1.1 joerg
300 1.1 joerg template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 1.1 joerg void shuffle(RandomAccessIterator first, RandomAccessIterator last,
302 1.1 joerg UniformRandomNumberGenerator&& g);
303 1.1 joerg
304 1.1 joerg template<class ForwardIterator>
305 1.1 joerg constexpr ForwardIterator
306 1.1 joerg shift_left(ForwardIterator first, ForwardIterator last,
307 1.1 joerg typename iterator_traits<ForwardIterator>::difference_type n); // C++20
308 1.1 joerg
309 1.1 joerg template<class ForwardIterator>
310 1.1 joerg constexpr ForwardIterator
311 1.1 joerg shift_right(ForwardIterator first, ForwardIterator last,
312 1.1 joerg typename iterator_traits<ForwardIterator>::difference_type n); // C++20
313 1.1 joerg
314 1.1 joerg template <class InputIterator, class Predicate>
315 1.1 joerg constexpr bool // constexpr in C++20
316 1.1 joerg is_partitioned(InputIterator first, InputIterator last, Predicate pred);
317 1.1 joerg
318 1.1 joerg template <class ForwardIterator, class Predicate>
319 1.1 joerg constexpr ForwardIterator // constexpr in C++20
320 1.1 joerg partition(ForwardIterator first, ForwardIterator last, Predicate pred);
321 1.1 joerg
322 1.1 joerg template <class InputIterator, class OutputIterator1,
323 1.1 joerg class OutputIterator2, class Predicate>
324 1.1 joerg constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
325 1.1 joerg partition_copy(InputIterator first, InputIterator last,
326 1.1 joerg OutputIterator1 out_true, OutputIterator2 out_false,
327 1.1 joerg Predicate pred);
328 1.1 joerg
329 1.1 joerg template <class ForwardIterator, class Predicate>
330 1.1 joerg ForwardIterator
331 1.1 joerg stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
332 1.1 joerg
333 1.1 joerg template<class ForwardIterator, class Predicate>
334 1.1 joerg constexpr ForwardIterator // constexpr in C++20
335 1.1 joerg partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
336 1.1 joerg
337 1.1 joerg template <class ForwardIterator>
338 1.1 joerg constexpr bool // constexpr in C++20
339 1.1 joerg is_sorted(ForwardIterator first, ForwardIterator last);
340 1.1 joerg
341 1.1 joerg template <class ForwardIterator, class Compare>
342 1.1 joerg constexpr bool // constexpr in C++20
343 1.1 joerg is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
344 1.1 joerg
345 1.1 joerg template<class ForwardIterator>
346 1.1 joerg constexpr ForwardIterator // constexpr in C++20
347 1.1 joerg is_sorted_until(ForwardIterator first, ForwardIterator last);
348 1.1 joerg
349 1.1 joerg template <class ForwardIterator, class Compare>
350 1.1 joerg constexpr ForwardIterator // constexpr in C++20
351 1.1 joerg is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
352 1.1 joerg
353 1.1 joerg template <class RandomAccessIterator>
354 1.1 joerg constexpr void // constexpr in C++20
355 1.1 joerg sort(RandomAccessIterator first, RandomAccessIterator last);
356 1.1 joerg
357 1.1 joerg template <class RandomAccessIterator, class Compare>
358 1.1 joerg constexpr void // constexpr in C++20
359 1.1 joerg sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
360 1.1 joerg
361 1.1 joerg template <class RandomAccessIterator>
362 1.1 joerg void
363 1.1 joerg stable_sort(RandomAccessIterator first, RandomAccessIterator last);
364 1.1 joerg
365 1.1 joerg template <class RandomAccessIterator, class Compare>
366 1.1 joerg void
367 1.1 joerg stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
368 1.1 joerg
369 1.1 joerg template <class RandomAccessIterator>
370 1.1 joerg constexpr void // constexpr in C++20
371 1.1 joerg partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
372 1.1 joerg
373 1.1 joerg template <class RandomAccessIterator, class Compare>
374 1.1 joerg constexpr void // constexpr in C++20
375 1.1 joerg partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
376 1.1 joerg
377 1.1 joerg template <class InputIterator, class RandomAccessIterator>
378 1.1 joerg constexpr RandomAccessIterator // constexpr in C++20
379 1.1 joerg partial_sort_copy(InputIterator first, InputIterator last,
380 1.1 joerg RandomAccessIterator result_first, RandomAccessIterator result_last);
381 1.1 joerg
382 1.1 joerg template <class InputIterator, class RandomAccessIterator, class Compare>
383 1.1 joerg constexpr RandomAccessIterator // constexpr in C++20
384 1.1 joerg partial_sort_copy(InputIterator first, InputIterator last,
385 1.1 joerg RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
386 1.1 joerg
387 1.1 joerg template <class RandomAccessIterator>
388 1.1 joerg constexpr void // constexpr in C++20
389 1.1 joerg nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
390 1.1 joerg
391 1.1 joerg template <class RandomAccessIterator, class Compare>
392 1.1 joerg constexpr void // constexpr in C++20
393 1.1 joerg nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
394 1.1 joerg
395 1.1 joerg template <class ForwardIterator, class T>
396 1.1 joerg constexpr ForwardIterator // constexpr in C++20
397 1.1 joerg lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
398 1.1 joerg
399 1.1 joerg template <class ForwardIterator, class T, class Compare>
400 1.1 joerg constexpr ForwardIterator // constexpr in C++20
401 1.1 joerg lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
402 1.1 joerg
403 1.1 joerg template <class ForwardIterator, class T>
404 1.1 joerg constexpr ForwardIterator // constexpr in C++20
405 1.1 joerg upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
406 1.1 joerg
407 1.1 joerg template <class ForwardIterator, class T, class Compare>
408 1.1 joerg constexpr ForwardIterator // constexpr in C++20
409 1.1 joerg upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
410 1.1 joerg
411 1.1 joerg template <class ForwardIterator, class T>
412 1.1 joerg constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
413 1.1 joerg equal_range(ForwardIterator first, ForwardIterator last, const T& value);
414 1.1 joerg
415 1.1 joerg template <class ForwardIterator, class T, class Compare>
416 1.1 joerg constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
417 1.1 joerg equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
418 1.1 joerg
419 1.1 joerg template <class ForwardIterator, class T>
420 1.1 joerg constexpr bool // constexpr in C++20
421 1.1 joerg binary_search(ForwardIterator first, ForwardIterator last, const T& value);
422 1.1 joerg
423 1.1 joerg template <class ForwardIterator, class T, class Compare>
424 1.1 joerg constexpr bool // constexpr in C++20
425 1.1 joerg binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
426 1.1 joerg
427 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator>
428 1.1 joerg constexpr OutputIterator // constexpr in C++20
429 1.1 joerg merge(InputIterator1 first1, InputIterator1 last1,
430 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result);
431 1.1 joerg
432 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
433 1.1 joerg constexpr OutputIterator // constexpr in C++20
434 1.1 joerg merge(InputIterator1 first1, InputIterator1 last1,
435 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
436 1.1 joerg
437 1.1 joerg template <class BidirectionalIterator>
438 1.1 joerg void
439 1.1 joerg inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
440 1.1 joerg
441 1.1 joerg template <class BidirectionalIterator, class Compare>
442 1.1 joerg void
443 1.1 joerg inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
444 1.1 joerg
445 1.1 joerg template <class InputIterator1, class InputIterator2>
446 1.1 joerg constexpr bool // constexpr in C++20
447 1.1 joerg includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
448 1.1 joerg
449 1.1 joerg template <class InputIterator1, class InputIterator2, class Compare>
450 1.1 joerg constexpr bool // constexpr in C++20
451 1.1 joerg includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
452 1.1 joerg
453 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator>
454 1.1 joerg constexpr OutputIterator // constexpr in C++20
455 1.1 joerg set_union(InputIterator1 first1, InputIterator1 last1,
456 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result);
457 1.1 joerg
458 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
459 1.1 joerg constexpr OutputIterator // constexpr in C++20
460 1.1 joerg set_union(InputIterator1 first1, InputIterator1 last1,
461 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
462 1.1 joerg
463 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator>
464 1.1 joerg constexpr OutputIterator // constexpr in C++20
465 1.1 joerg set_intersection(InputIterator1 first1, InputIterator1 last1,
466 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result);
467 1.1 joerg
468 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
469 1.1 joerg constexpr OutputIterator // constexpr in C++20
470 1.1 joerg set_intersection(InputIterator1 first1, InputIterator1 last1,
471 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
472 1.1 joerg
473 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator>
474 1.1 joerg constexpr OutputIterator // constexpr in C++20
475 1.1 joerg set_difference(InputIterator1 first1, InputIterator1 last1,
476 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result);
477 1.1 joerg
478 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
479 1.1 joerg constexpr OutputIterator // constexpr in C++20
480 1.1 joerg set_difference(InputIterator1 first1, InputIterator1 last1,
481 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
482 1.1 joerg
483 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator>
484 1.1 joerg constexpr OutputIterator // constexpr in C++20
485 1.1 joerg set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
486 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result);
487 1.1 joerg
488 1.1 joerg template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
489 1.1 joerg constexpr OutputIterator // constexpr in C++20
490 1.1 joerg set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
491 1.1 joerg InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
492 1.1 joerg
493 1.1 joerg template <class RandomAccessIterator>
494 1.1 joerg constexpr void // constexpr in C++20
495 1.1 joerg push_heap(RandomAccessIterator first, RandomAccessIterator last);
496 1.1 joerg
497 1.1 joerg template <class RandomAccessIterator, class Compare>
498 1.1 joerg constexpr void // constexpr in C++20
499 1.1 joerg push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
500 1.1 joerg
501 1.1 joerg template <class RandomAccessIterator>
502 1.1 joerg constexpr void // constexpr in C++20
503 1.1 joerg pop_heap(RandomAccessIterator first, RandomAccessIterator last);
504 1.1 joerg
505 1.1 joerg template <class RandomAccessIterator, class Compare>
506 1.1 joerg constexpr void // constexpr in C++20
507 1.1 joerg pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
508 1.1 joerg
509 1.1 joerg template <class RandomAccessIterator>
510 1.1 joerg constexpr void // constexpr in C++20
511 1.1 joerg make_heap(RandomAccessIterator first, RandomAccessIterator last);
512 1.1 joerg
513 1.1 joerg template <class RandomAccessIterator, class Compare>
514 1.1 joerg constexpr void // constexpr in C++20
515 1.1 joerg make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
516 1.1 joerg
517 1.1 joerg template <class RandomAccessIterator>
518 1.1 joerg constexpr void // constexpr in C++20
519 1.1 joerg sort_heap(RandomAccessIterator first, RandomAccessIterator last);
520 1.1 joerg
521 1.1 joerg template <class RandomAccessIterator, class Compare>
522 1.1 joerg constexpr void // constexpr in C++20
523 1.1 joerg sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
524 1.1 joerg
525 1.1 joerg template <class RandomAccessIterator>
526 1.1 joerg constexpr bool // constexpr in C++20
527 1.1 joerg is_heap(RandomAccessIterator first, RandomAccessiterator last);
528 1.1 joerg
529 1.1 joerg template <class RandomAccessIterator, class Compare>
530 1.1 joerg constexpr bool // constexpr in C++20
531 1.1 joerg is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
532 1.1 joerg
533 1.1 joerg template <class RandomAccessIterator>
534 1.1 joerg constexpr RandomAccessIterator // constexpr in C++20
535 1.1 joerg is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
536 1.1 joerg
537 1.1 joerg template <class RandomAccessIterator, class Compare>
538 1.1 joerg constexpr RandomAccessIterator // constexpr in C++20
539 1.1 joerg is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
540 1.1 joerg
541 1.1 joerg template <class ForwardIterator>
542 1.1 joerg constexpr ForwardIterator // constexpr in C++14
543 1.1 joerg min_element(ForwardIterator first, ForwardIterator last);
544 1.1 joerg
545 1.1 joerg template <class ForwardIterator, class Compare>
546 1.1 joerg constexpr ForwardIterator // constexpr in C++14
547 1.1 joerg min_element(ForwardIterator first, ForwardIterator last, Compare comp);
548 1.1 joerg
549 1.1 joerg template <class T>
550 1.1 joerg constexpr const T& // constexpr in C++14
551 1.1 joerg min(const T& a, const T& b);
552 1.1 joerg
553 1.1 joerg template <class T, class Compare>
554 1.1 joerg constexpr const T& // constexpr in C++14
555 1.1 joerg min(const T& a, const T& b, Compare comp);
556 1.1 joerg
557 1.1 joerg template<class T>
558 1.1 joerg constexpr T // constexpr in C++14
559 1.1 joerg min(initializer_list<T> t);
560 1.1 joerg
561 1.1 joerg template<class T, class Compare>
562 1.1 joerg constexpr T // constexpr in C++14
563 1.1 joerg min(initializer_list<T> t, Compare comp);
564 1.1 joerg
565 1.1 joerg template<class T>
566 1.1 joerg constexpr const T& clamp(const T& v, const T& lo, const T& hi); // C++17
567 1.1 joerg
568 1.1 joerg template<class T, class Compare>
569 1.1 joerg constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
570 1.1 joerg
571 1.1 joerg template <class ForwardIterator>
572 1.1 joerg constexpr ForwardIterator // constexpr in C++14
573 1.1 joerg max_element(ForwardIterator first, ForwardIterator last);
574 1.1 joerg
575 1.1 joerg template <class ForwardIterator, class Compare>
576 1.1 joerg constexpr ForwardIterator // constexpr in C++14
577 1.1 joerg max_element(ForwardIterator first, ForwardIterator last, Compare comp);
578 1.1 joerg
579 1.1 joerg template <class T>
580 1.1 joerg constexpr const T& // constexpr in C++14
581 1.1 joerg max(const T& a, const T& b);
582 1.1 joerg
583 1.1 joerg template <class T, class Compare>
584 1.1 joerg constexpr const T& // constexpr in C++14
585 1.1 joerg max(const T& a, const T& b, Compare comp);
586 1.1 joerg
587 1.1 joerg template<class T>
588 1.1 joerg constexpr T // constexpr in C++14
589 1.1 joerg max(initializer_list<T> t);
590 1.1 joerg
591 1.1 joerg template<class T, class Compare>
592 1.1 joerg constexpr T // constexpr in C++14
593 1.1 joerg max(initializer_list<T> t, Compare comp);
594 1.1 joerg
595 1.1 joerg template<class ForwardIterator>
596 1.1 joerg constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
597 1.1 joerg minmax_element(ForwardIterator first, ForwardIterator last);
598 1.1 joerg
599 1.1 joerg template<class ForwardIterator, class Compare>
600 1.1 joerg constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
601 1.1 joerg minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
602 1.1 joerg
603 1.1 joerg template<class T>
604 1.1 joerg constexpr pair<const T&, const T&> // constexpr in C++14
605 1.1 joerg minmax(const T& a, const T& b);
606 1.1 joerg
607 1.1 joerg template<class T, class Compare>
608 1.1 joerg constexpr pair<const T&, const T&> // constexpr in C++14
609 1.1 joerg minmax(const T& a, const T& b, Compare comp);
610 1.1 joerg
611 1.1 joerg template<class T>
612 1.1 joerg constexpr pair<T, T> // constexpr in C++14
613 1.1 joerg minmax(initializer_list<T> t);
614 1.1 joerg
615 1.1 joerg template<class T, class Compare>
616 1.1 joerg constexpr pair<T, T> // constexpr in C++14
617 1.1 joerg minmax(initializer_list<T> t, Compare comp);
618 1.1 joerg
619 1.1 joerg template <class InputIterator1, class InputIterator2>
620 1.1 joerg constexpr bool // constexpr in C++20
621 1.1 joerg lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
622 1.1 joerg
623 1.1 joerg template <class InputIterator1, class InputIterator2, class Compare>
624 1.1 joerg constexpr bool // constexpr in C++20
625 1.1 joerg lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
626 1.1 joerg InputIterator2 first2, InputIterator2 last2, Compare comp);
627 1.1 joerg
628 1.1 joerg template <class BidirectionalIterator>
629 1.1 joerg constexpr bool // constexpr in C++20
630 1.1 joerg next_permutation(BidirectionalIterator first, BidirectionalIterator last);
631 1.1 joerg
632 1.1 joerg template <class BidirectionalIterator, class Compare>
633 1.1 joerg constexpr bool // constexpr in C++20
634 1.1 joerg next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
635 1.1 joerg
636 1.1 joerg template <class BidirectionalIterator>
637 1.1 joerg constexpr bool // constexpr in C++20
638 1.1 joerg prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
639 1.1 joerg
640 1.1 joerg template <class BidirectionalIterator, class Compare>
641 1.1 joerg constexpr bool // constexpr in C++20
642 1.1 joerg prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
643 1.1 joerg
644 1.1 joerg } // std
645 1.1 joerg
646 1.1 joerg */
647 1.1 joerg
648 1.1 joerg #include <__config>
649 1.1 joerg #include <initializer_list>
650 1.1 joerg #include <type_traits>
651 1.1 joerg #include <cstring>
652 1.1 joerg #include <utility> // needed to provide swap_ranges.
653 1.1 joerg #include <memory>
654 1.1 joerg #include <functional>
655 1.1 joerg #include <iterator>
656 1.1 joerg #include <cstddef>
657 1.1 joerg #include <bit>
658 1.1 joerg #include <version>
659 1.1 joerg
660 1.1 joerg #include <__debug>
661 1.1 joerg
662 1.1 joerg #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
663 1.1 joerg #pragma GCC system_header
664 1.1 joerg #endif
665 1.1 joerg
666 1.1 joerg _LIBCPP_PUSH_MACROS
667 1.1 joerg #include <__undef_macros>
668 1.1 joerg
669 1.1 joerg
670 1.1 joerg _LIBCPP_BEGIN_NAMESPACE_STD
671 1.1 joerg
672 1.1 joerg // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
673 1.1 joerg // * That only works with C++14 and later, and
674 1.1 joerg // * We haven't included <functional> here.
675 1.1 joerg template <class _T1, class _T2 = _T1>
676 1.1 joerg struct __equal_to
677 1.1 joerg {
678 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
679 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
680 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
681 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
682 1.1 joerg };
683 1.1 joerg
684 1.1 joerg template <class _T1>
685 1.1 joerg struct __equal_to<_T1, _T1>
686 1.1 joerg {
687 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
688 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
689 1.1 joerg };
690 1.1 joerg
691 1.1 joerg template <class _T1>
692 1.1 joerg struct __equal_to<const _T1, _T1>
693 1.1 joerg {
694 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
696 1.1 joerg };
697 1.1 joerg
698 1.1 joerg template <class _T1>
699 1.1 joerg struct __equal_to<_T1, const _T1>
700 1.1 joerg {
701 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
703 1.1 joerg };
704 1.1 joerg
705 1.1 joerg template <class _T1, class _T2 = _T1>
706 1.1 joerg struct __less
707 1.1 joerg {
708 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
710 1.1 joerg
711 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
712 1.1 joerg bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
713 1.1 joerg
714 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 1.1 joerg bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
716 1.1 joerg
717 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
718 1.1 joerg bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
719 1.1 joerg };
720 1.1 joerg
721 1.1 joerg template <class _T1>
722 1.1 joerg struct __less<_T1, _T1>
723 1.1 joerg {
724 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
725 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
726 1.1 joerg };
727 1.1 joerg
728 1.1 joerg template <class _T1>
729 1.1 joerg struct __less<const _T1, _T1>
730 1.1 joerg {
731 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
732 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
733 1.1 joerg };
734 1.1 joerg
735 1.1 joerg template <class _T1>
736 1.1 joerg struct __less<_T1, const _T1>
737 1.1 joerg {
738 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
739 1.1 joerg bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
740 1.1 joerg };
741 1.1 joerg
742 1.1 joerg template <class _Predicate>
743 1.1 joerg class __invert // invert the sense of a comparison
744 1.1 joerg {
745 1.1 joerg private:
746 1.1 joerg _Predicate __p_;
747 1.1 joerg public:
748 1.1 joerg _LIBCPP_INLINE_VISIBILITY __invert() {}
749 1.1 joerg
750 1.1 joerg _LIBCPP_INLINE_VISIBILITY
751 1.1 joerg explicit __invert(_Predicate __p) : __p_(__p) {}
752 1.1 joerg
753 1.1 joerg template <class _T1>
754 1.1 joerg _LIBCPP_INLINE_VISIBILITY
755 1.1 joerg bool operator()(const _T1& __x) {return !__p_(__x);}
756 1.1 joerg
757 1.1 joerg template <class _T1, class _T2>
758 1.1 joerg _LIBCPP_INLINE_VISIBILITY
759 1.1 joerg bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
760 1.1 joerg };
761 1.1 joerg
762 1.1 joerg // Perform division by two quickly for positive integers (llvm.org/PR39129)
763 1.1 joerg
764 1.1 joerg template <typename _Integral>
765 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
766 1.1 joerg typename enable_if
767 1.1 joerg <
768 1.1 joerg is_integral<_Integral>::value,
769 1.1 joerg _Integral
770 1.1 joerg >::type
771 1.1 joerg __half_positive(_Integral __value)
772 1.1 joerg {
773 1.1 joerg return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
774 1.1 joerg }
775 1.1 joerg
776 1.1 joerg template <typename _Tp>
777 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
778 1.1 joerg typename enable_if
779 1.1 joerg <
780 1.1 joerg !is_integral<_Tp>::value,
781 1.1 joerg _Tp
782 1.1 joerg >::type
783 1.1 joerg __half_positive(_Tp __value)
784 1.1 joerg {
785 1.1 joerg return __value / 2;
786 1.1 joerg }
787 1.1 joerg
788 1.1 joerg #ifdef _LIBCPP_DEBUG
789 1.1 joerg
790 1.1 joerg template <class _Compare>
791 1.1 joerg struct __debug_less
792 1.1 joerg {
793 1.1 joerg _Compare &__comp_;
794 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
795 1.1 joerg __debug_less(_Compare& __c) : __comp_(__c) {}
796 1.1 joerg
797 1.1 joerg template <class _Tp, class _Up>
798 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
799 1.1 joerg bool operator()(const _Tp& __x, const _Up& __y)
800 1.1 joerg {
801 1.1 joerg bool __r = __comp_(__x, __y);
802 1.1 joerg if (__r)
803 1.1 joerg __do_compare_assert(0, __y, __x);
804 1.1 joerg return __r;
805 1.1 joerg }
806 1.1 joerg
807 1.1 joerg template <class _Tp, class _Up>
808 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
809 1.1 joerg bool operator()(_Tp& __x, _Up& __y)
810 1.1 joerg {
811 1.1 joerg bool __r = __comp_(__x, __y);
812 1.1 joerg if (__r)
813 1.1 joerg __do_compare_assert(0, __y, __x);
814 1.1 joerg return __r;
815 1.1 joerg }
816 1.1 joerg
817 1.1 joerg template <class _LHS, class _RHS>
818 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
819 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
820 1.1 joerg decltype((void)declval<_Compare&>()(
821 1.1 joerg declval<_LHS &>(), declval<_RHS &>()))
822 1.1 joerg __do_compare_assert(int, _LHS & __l, _RHS & __r) {
823 1.1 joerg _LIBCPP_ASSERT(!__comp_(__l, __r),
824 1.1 joerg "Comparator does not induce a strict weak ordering");
825 1.1 joerg }
826 1.1 joerg
827 1.1 joerg template <class _LHS, class _RHS>
828 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
829 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
830 1.1 joerg void __do_compare_assert(long, _LHS &, _RHS &) {}
831 1.1 joerg };
832 1.1 joerg
833 1.1 joerg #endif // _LIBCPP_DEBUG
834 1.1 joerg
835 1.1 joerg template <class _Comp>
836 1.1 joerg struct __comp_ref_type {
837 1.1 joerg // Pass the comparator by lvalue reference. Or in debug mode, using a
838 1.1 joerg // debugging wrapper that stores a reference.
839 1.1 joerg #ifndef _LIBCPP_DEBUG
840 1.1 joerg typedef typename add_lvalue_reference<_Comp>::type type;
841 1.1 joerg #else
842 1.1 joerg typedef __debug_less<_Comp> type;
843 1.1 joerg #endif
844 1.1 joerg };
845 1.1 joerg
846 1.1 joerg // all_of
847 1.1 joerg
848 1.1 joerg template <class _InputIterator, class _Predicate>
849 1.1 joerg _LIBCPP_NODISCARD_EXT inline
850 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
851 1.1 joerg bool
852 1.1 joerg all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
853 1.1 joerg {
854 1.1 joerg for (; __first != __last; ++__first)
855 1.1 joerg if (!__pred(*__first))
856 1.1 joerg return false;
857 1.1 joerg return true;
858 1.1 joerg }
859 1.1 joerg
860 1.1 joerg // any_of
861 1.1 joerg
862 1.1 joerg template <class _InputIterator, class _Predicate>
863 1.1 joerg _LIBCPP_NODISCARD_EXT inline
864 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
865 1.1 joerg bool
866 1.1 joerg any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
867 1.1 joerg {
868 1.1 joerg for (; __first != __last; ++__first)
869 1.1 joerg if (__pred(*__first))
870 1.1 joerg return true;
871 1.1 joerg return false;
872 1.1 joerg }
873 1.1 joerg
874 1.1 joerg // none_of
875 1.1 joerg
876 1.1 joerg template <class _InputIterator, class _Predicate>
877 1.1 joerg _LIBCPP_NODISCARD_EXT inline
878 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
879 1.1 joerg bool
880 1.1 joerg none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
881 1.1 joerg {
882 1.1 joerg for (; __first != __last; ++__first)
883 1.1 joerg if (__pred(*__first))
884 1.1 joerg return false;
885 1.1 joerg return true;
886 1.1 joerg }
887 1.1 joerg
888 1.1 joerg // for_each
889 1.1 joerg
890 1.1 joerg template <class _InputIterator, class _Function>
891 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
892 1.1 joerg _Function
893 1.1 joerg for_each(_InputIterator __first, _InputIterator __last, _Function __f)
894 1.1 joerg {
895 1.1 joerg for (; __first != __last; ++__first)
896 1.1 joerg __f(*__first);
897 1.1 joerg return __f;
898 1.1 joerg }
899 1.1 joerg
900 1.1 joerg #if _LIBCPP_STD_VER > 14
901 1.1 joerg // for_each_n
902 1.1 joerg
903 1.1 joerg template <class _InputIterator, class _Size, class _Function>
904 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
905 1.1 joerg _InputIterator
906 1.1 joerg for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
907 1.1 joerg {
908 1.1 joerg typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
909 1.1 joerg _IntegralSize __n = __orig_n;
910 1.1 joerg while (__n > 0)
911 1.1 joerg {
912 1.1 joerg __f(*__first);
913 1.1 joerg ++__first;
914 1.1 joerg --__n;
915 1.1 joerg }
916 1.1 joerg return __first;
917 1.1 joerg }
918 1.1 joerg #endif
919 1.1 joerg
920 1.1 joerg // find
921 1.1 joerg
922 1.1 joerg template <class _InputIterator, class _Tp>
923 1.1 joerg _LIBCPP_NODISCARD_EXT inline
924 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
925 1.1 joerg _InputIterator
926 1.1 joerg find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
927 1.1 joerg {
928 1.1 joerg for (; __first != __last; ++__first)
929 1.1 joerg if (*__first == __value_)
930 1.1 joerg break;
931 1.1 joerg return __first;
932 1.1 joerg }
933 1.1 joerg
934 1.1 joerg // find_if
935 1.1 joerg
936 1.1 joerg template <class _InputIterator, class _Predicate>
937 1.1 joerg _LIBCPP_NODISCARD_EXT inline
938 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
939 1.1 joerg _InputIterator
940 1.1 joerg find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
941 1.1 joerg {
942 1.1 joerg for (; __first != __last; ++__first)
943 1.1 joerg if (__pred(*__first))
944 1.1 joerg break;
945 1.1 joerg return __first;
946 1.1 joerg }
947 1.1 joerg
948 1.1 joerg // find_if_not
949 1.1 joerg
950 1.1 joerg template<class _InputIterator, class _Predicate>
951 1.1 joerg _LIBCPP_NODISCARD_EXT inline
952 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
953 1.1 joerg _InputIterator
954 1.1 joerg find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
955 1.1 joerg {
956 1.1 joerg for (; __first != __last; ++__first)
957 1.1 joerg if (!__pred(*__first))
958 1.1 joerg break;
959 1.1 joerg return __first;
960 1.1 joerg }
961 1.1 joerg
962 1.1 joerg // find_end
963 1.1 joerg
964 1.1 joerg template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
965 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
966 1.1 joerg __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
967 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
968 1.1 joerg forward_iterator_tag, forward_iterator_tag)
969 1.1 joerg {
970 1.1 joerg // modeled after search algorithm
971 1.1 joerg _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
972 1.1 joerg if (__first2 == __last2)
973 1.1 joerg return __r;
974 1.1 joerg while (true)
975 1.1 joerg {
976 1.1 joerg while (true)
977 1.1 joerg {
978 1.1 joerg if (__first1 == __last1) // if source exhausted return last correct answer
979 1.1 joerg return __r; // (or __last1 if never found)
980 1.1 joerg if (__pred(*__first1, *__first2))
981 1.1 joerg break;
982 1.1 joerg ++__first1;
983 1.1 joerg }
984 1.1 joerg // *__first1 matches *__first2, now match elements after here
985 1.1 joerg _ForwardIterator1 __m1 = __first1;
986 1.1 joerg _ForwardIterator2 __m2 = __first2;
987 1.1 joerg while (true)
988 1.1 joerg {
989 1.1 joerg if (++__m2 == __last2)
990 1.1 joerg { // Pattern exhaused, record answer and search for another one
991 1.1 joerg __r = __first1;
992 1.1 joerg ++__first1;
993 1.1 joerg break;
994 1.1 joerg }
995 1.1 joerg if (++__m1 == __last1) // Source exhausted, return last answer
996 1.1 joerg return __r;
997 1.1 joerg if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
998 1.1 joerg {
999 1.1 joerg ++__first1;
1000 1.1 joerg break;
1001 1.1 joerg } // else there is a match, check next elements
1002 1.1 joerg }
1003 1.1 joerg }
1004 1.1 joerg }
1005 1.1 joerg
1006 1.1 joerg template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1007 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
1008 1.1 joerg __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1009 1.1 joerg _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1010 1.1 joerg bidirectional_iterator_tag, bidirectional_iterator_tag)
1011 1.1 joerg {
1012 1.1 joerg // modeled after search algorithm (in reverse)
1013 1.1 joerg if (__first2 == __last2)
1014 1.1 joerg return __last1; // Everything matches an empty sequence
1015 1.1 joerg _BidirectionalIterator1 __l1 = __last1;
1016 1.1 joerg _BidirectionalIterator2 __l2 = __last2;
1017 1.1 joerg --__l2;
1018 1.1 joerg while (true)
1019 1.1 joerg {
1020 1.1 joerg // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1021 1.1 joerg while (true)
1022 1.1 joerg {
1023 1.1 joerg if (__first1 == __l1) // return __last1 if no element matches *__first2
1024 1.1 joerg return __last1;
1025 1.1 joerg if (__pred(*--__l1, *__l2))
1026 1.1 joerg break;
1027 1.1 joerg }
1028 1.1 joerg // *__l1 matches *__l2, now match elements before here
1029 1.1 joerg _BidirectionalIterator1 __m1 = __l1;
1030 1.1 joerg _BidirectionalIterator2 __m2 = __l2;
1031 1.1 joerg while (true)
1032 1.1 joerg {
1033 1.1 joerg if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1034 1.1 joerg return __m1;
1035 1.1 joerg if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1036 1.1 joerg return __last1;
1037 1.1 joerg if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1038 1.1 joerg {
1039 1.1 joerg break;
1040 1.1 joerg } // else there is a match, check next elements
1041 1.1 joerg }
1042 1.1 joerg }
1043 1.1 joerg }
1044 1.1 joerg
1045 1.1 joerg template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1046 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1047 1.1 joerg __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1048 1.1 joerg _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1049 1.1 joerg random_access_iterator_tag, random_access_iterator_tag)
1050 1.1 joerg {
1051 1.1 joerg // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1052 1.1 joerg typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1053 1.1 joerg if (__len2 == 0)
1054 1.1 joerg return __last1;
1055 1.1 joerg typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1056 1.1 joerg if (__len1 < __len2)
1057 1.1 joerg return __last1;
1058 1.1 joerg const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1059 1.1 joerg _RandomAccessIterator1 __l1 = __last1;
1060 1.1 joerg _RandomAccessIterator2 __l2 = __last2;
1061 1.1 joerg --__l2;
1062 1.1 joerg while (true)
1063 1.1 joerg {
1064 1.1 joerg while (true)
1065 1.1 joerg {
1066 1.1 joerg if (__s == __l1)
1067 1.1 joerg return __last1;
1068 1.1 joerg if (__pred(*--__l1, *__l2))
1069 1.1 joerg break;
1070 1.1 joerg }
1071 1.1 joerg _RandomAccessIterator1 __m1 = __l1;
1072 1.1 joerg _RandomAccessIterator2 __m2 = __l2;
1073 1.1 joerg while (true)
1074 1.1 joerg {
1075 1.1 joerg if (__m2 == __first2)
1076 1.1 joerg return __m1;
1077 1.1 joerg // no need to check range on __m1 because __s guarantees we have enough source
1078 1.1 joerg if (!__pred(*--__m1, *--__m2))
1079 1.1 joerg {
1080 1.1 joerg break;
1081 1.1 joerg }
1082 1.1 joerg }
1083 1.1 joerg }
1084 1.1 joerg }
1085 1.1 joerg
1086 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1087 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1088 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1089 1.1 joerg _ForwardIterator1
1090 1.1 joerg find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1091 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1092 1.1 joerg {
1093 1.1 joerg return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1094 1.1 joerg (__first1, __last1, __first2, __last2, __pred,
1095 1.1 joerg typename iterator_traits<_ForwardIterator1>::iterator_category(),
1096 1.1 joerg typename iterator_traits<_ForwardIterator2>::iterator_category());
1097 1.1 joerg }
1098 1.1 joerg
1099 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2>
1100 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1101 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1102 1.1 joerg _ForwardIterator1
1103 1.1 joerg find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1104 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1105 1.1 joerg {
1106 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1107 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1108 1.1 joerg return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1109 1.1 joerg }
1110 1.1 joerg
1111 1.1 joerg // find_first_of
1112 1.1 joerg
1113 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1114 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1115 1.1 joerg __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1116 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1117 1.1 joerg {
1118 1.1 joerg for (; __first1 != __last1; ++__first1)
1119 1.1 joerg for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1120 1.1 joerg if (__pred(*__first1, *__j))
1121 1.1 joerg return __first1;
1122 1.1 joerg return __last1;
1123 1.1 joerg }
1124 1.1 joerg
1125 1.1 joerg
1126 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1127 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1128 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1129 1.1 joerg _ForwardIterator1
1130 1.1 joerg find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1131 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1132 1.1 joerg {
1133 1.1 joerg return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1134 1.1 joerg }
1135 1.1 joerg
1136 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2>
1137 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1138 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1139 1.1 joerg _ForwardIterator1
1140 1.1 joerg find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1141 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1142 1.1 joerg {
1143 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1144 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1145 1.1 joerg return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1146 1.1 joerg }
1147 1.1 joerg
1148 1.1 joerg // adjacent_find
1149 1.1 joerg
1150 1.1 joerg template <class _ForwardIterator, class _BinaryPredicate>
1151 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1152 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1153 1.1 joerg _ForwardIterator
1154 1.1 joerg adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1155 1.1 joerg {
1156 1.1 joerg if (__first != __last)
1157 1.1 joerg {
1158 1.1 joerg _ForwardIterator __i = __first;
1159 1.1 joerg while (++__i != __last)
1160 1.1 joerg {
1161 1.1 joerg if (__pred(*__first, *__i))
1162 1.1 joerg return __first;
1163 1.1 joerg __first = __i;
1164 1.1 joerg }
1165 1.1 joerg }
1166 1.1 joerg return __last;
1167 1.1 joerg }
1168 1.1 joerg
1169 1.1 joerg template <class _ForwardIterator>
1170 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1171 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1172 1.1 joerg _ForwardIterator
1173 1.1 joerg adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1174 1.1 joerg {
1175 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1176 1.1 joerg return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1177 1.1 joerg }
1178 1.1 joerg
1179 1.1 joerg // count
1180 1.1 joerg
1181 1.1 joerg template <class _InputIterator, class _Tp>
1182 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1183 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1184 1.1 joerg typename iterator_traits<_InputIterator>::difference_type
1185 1.1 joerg count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1186 1.1 joerg {
1187 1.1 joerg typename iterator_traits<_InputIterator>::difference_type __r(0);
1188 1.1 joerg for (; __first != __last; ++__first)
1189 1.1 joerg if (*__first == __value_)
1190 1.1 joerg ++__r;
1191 1.1 joerg return __r;
1192 1.1 joerg }
1193 1.1 joerg
1194 1.1 joerg // count_if
1195 1.1 joerg
1196 1.1 joerg template <class _InputIterator, class _Predicate>
1197 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1198 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1199 1.1 joerg typename iterator_traits<_InputIterator>::difference_type
1200 1.1 joerg count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1201 1.1 joerg {
1202 1.1 joerg typename iterator_traits<_InputIterator>::difference_type __r(0);
1203 1.1 joerg for (; __first != __last; ++__first)
1204 1.1 joerg if (__pred(*__first))
1205 1.1 joerg ++__r;
1206 1.1 joerg return __r;
1207 1.1 joerg }
1208 1.1 joerg
1209 1.1 joerg // mismatch
1210 1.1 joerg
1211 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1212 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1213 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1214 1.1 joerg pair<_InputIterator1, _InputIterator2>
1215 1.1 joerg mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1216 1.1 joerg _InputIterator2 __first2, _BinaryPredicate __pred)
1217 1.1 joerg {
1218 1.1 joerg for (; __first1 != __last1; ++__first1, (void) ++__first2)
1219 1.1 joerg if (!__pred(*__first1, *__first2))
1220 1.1 joerg break;
1221 1.1 joerg return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1222 1.1 joerg }
1223 1.1 joerg
1224 1.1 joerg template <class _InputIterator1, class _InputIterator2>
1225 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1226 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1227 1.1 joerg pair<_InputIterator1, _InputIterator2>
1228 1.1 joerg mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1229 1.1 joerg {
1230 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1231 1.1 joerg typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1232 1.1 joerg return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1233 1.1 joerg }
1234 1.1 joerg
1235 1.1 joerg #if _LIBCPP_STD_VER > 11
1236 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1238 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1239 1.1 joerg pair<_InputIterator1, _InputIterator2>
1240 1.1 joerg mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1241 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2,
1242 1.1 joerg _BinaryPredicate __pred)
1243 1.1 joerg {
1244 1.1 joerg for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1245 1.1 joerg if (!__pred(*__first1, *__first2))
1246 1.1 joerg break;
1247 1.1 joerg return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1248 1.1 joerg }
1249 1.1 joerg
1250 1.1 joerg template <class _InputIterator1, class _InputIterator2>
1251 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1252 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1253 1.1 joerg pair<_InputIterator1, _InputIterator2>
1254 1.1 joerg mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1255 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2)
1256 1.1 joerg {
1257 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1258 1.1 joerg typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1259 1.1 joerg return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1260 1.1 joerg }
1261 1.1 joerg #endif
1262 1.1 joerg
1263 1.1 joerg // equal
1264 1.1 joerg
1265 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1266 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1267 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1268 1.1 joerg bool
1269 1.1 joerg equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1270 1.1 joerg {
1271 1.1 joerg for (; __first1 != __last1; ++__first1, (void) ++__first2)
1272 1.1 joerg if (!__pred(*__first1, *__first2))
1273 1.1 joerg return false;
1274 1.1 joerg return true;
1275 1.1 joerg }
1276 1.1 joerg
1277 1.1 joerg template <class _InputIterator1, class _InputIterator2>
1278 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1279 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1280 1.1 joerg bool
1281 1.1 joerg equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1282 1.1 joerg {
1283 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1284 1.1 joerg typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1285 1.1 joerg return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1286 1.1 joerg }
1287 1.1 joerg
1288 1.1 joerg #if _LIBCPP_STD_VER > 11
1289 1.1 joerg template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1290 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1291 1.1 joerg bool
1292 1.1 joerg __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1293 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1294 1.1 joerg input_iterator_tag, input_iterator_tag )
1295 1.1 joerg {
1296 1.1 joerg for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1297 1.1 joerg if (!__pred(*__first1, *__first2))
1298 1.1 joerg return false;
1299 1.1 joerg return __first1 == __last1 && __first2 == __last2;
1300 1.1 joerg }
1301 1.1 joerg
1302 1.1 joerg template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1303 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1304 1.1 joerg bool
1305 1.1 joerg __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1306 1.1 joerg _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1307 1.1 joerg random_access_iterator_tag, random_access_iterator_tag )
1308 1.1 joerg {
1309 1.1 joerg if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1310 1.1 joerg return false;
1311 1.1 joerg return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1312 1.1 joerg typename add_lvalue_reference<_BinaryPredicate>::type>
1313 1.1 joerg (__first1, __last1, __first2, __pred );
1314 1.1 joerg }
1315 1.1 joerg
1316 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1317 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1318 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1319 1.1 joerg bool
1320 1.1 joerg equal(_InputIterator1 __first1, _InputIterator1 __last1,
1321 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1322 1.1 joerg {
1323 1.1 joerg return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1324 1.1 joerg (__first1, __last1, __first2, __last2, __pred,
1325 1.1 joerg typename iterator_traits<_InputIterator1>::iterator_category(),
1326 1.1 joerg typename iterator_traits<_InputIterator2>::iterator_category());
1327 1.1 joerg }
1328 1.1 joerg
1329 1.1 joerg template <class _InputIterator1, class _InputIterator2>
1330 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1331 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1332 1.1 joerg bool
1333 1.1 joerg equal(_InputIterator1 __first1, _InputIterator1 __last1,
1334 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2)
1335 1.1 joerg {
1336 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1337 1.1 joerg typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1338 1.1 joerg return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1339 1.1 joerg typename iterator_traits<_InputIterator1>::iterator_category(),
1340 1.1 joerg typename iterator_traits<_InputIterator2>::iterator_category());
1341 1.1 joerg }
1342 1.1 joerg #endif
1343 1.1 joerg
1344 1.1 joerg // is_permutation
1345 1.1 joerg
1346 1.1 joerg template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1347 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1348 1.1 joerg is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1349 1.1 joerg _ForwardIterator2 __first2, _BinaryPredicate __pred)
1350 1.1 joerg {
1351 1.1 joerg // shorten sequences as much as possible by lopping of any equal prefix
1352 1.1 joerg for (; __first1 != __last1; ++__first1, (void) ++__first2)
1353 1.1 joerg if (!__pred(*__first1, *__first2))
1354 1.1 joerg break;
1355 1.1 joerg if (__first1 == __last1)
1356 1.1 joerg return true;
1357 1.1 joerg
1358 1.1 joerg // __first1 != __last1 && *__first1 != *__first2
1359 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1360 1.1 joerg _D1 __l1 = _VSTD::distance(__first1, __last1);
1361 1.1 joerg if (__l1 == _D1(1))
1362 1.1 joerg return false;
1363 1.1 joerg _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1364 1.1 joerg // For each element in [f1, l1) see if there are the same number of
1365 1.1 joerg // equal elements in [f2, l2)
1366 1.1 joerg for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1367 1.1 joerg {
1368 1.1 joerg // Have we already counted the number of *__i in [f1, l1)?
1369 1.1 joerg _ForwardIterator1 __match = __first1;
1370 1.1 joerg for (; __match != __i; ++__match)
1371 1.1 joerg if (__pred(*__match, *__i))
1372 1.1 joerg break;
1373 1.1 joerg if (__match == __i) {
1374 1.1 joerg // Count number of *__i in [f2, l2)
1375 1.1 joerg _D1 __c2 = 0;
1376 1.1 joerg for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1377 1.1 joerg if (__pred(*__i, *__j))
1378 1.1 joerg ++__c2;
1379 1.1 joerg if (__c2 == 0)
1380 1.1 joerg return false;
1381 1.1 joerg // Count number of *__i in [__i, l1) (we can start with 1)
1382 1.1 joerg _D1 __c1 = 1;
1383 1.1 joerg for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1384 1.1 joerg if (__pred(*__i, *__j))
1385 1.1 joerg ++__c1;
1386 1.1 joerg if (__c1 != __c2)
1387 1.1 joerg return false;
1388 1.1 joerg }
1389 1.1 joerg }
1390 1.1 joerg return true;
1391 1.1 joerg }
1392 1.1 joerg
1393 1.1 joerg template<class _ForwardIterator1, class _ForwardIterator2>
1394 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1395 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1396 1.1 joerg bool
1397 1.1 joerg is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1398 1.1 joerg _ForwardIterator2 __first2)
1399 1.1 joerg {
1400 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1401 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1402 1.1 joerg return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1403 1.1 joerg }
1404 1.1 joerg
1405 1.1 joerg #if _LIBCPP_STD_VER > 11
1406 1.1 joerg template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1407 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1408 1.1 joerg __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1409 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1410 1.1 joerg _BinaryPredicate __pred,
1411 1.1 joerg forward_iterator_tag, forward_iterator_tag )
1412 1.1 joerg {
1413 1.1 joerg // shorten sequences as much as possible by lopping of any equal prefix
1414 1.1 joerg for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1415 1.1 joerg if (!__pred(*__first1, *__first2))
1416 1.1 joerg break;
1417 1.1 joerg if (__first1 == __last1)
1418 1.1 joerg return __first2 == __last2;
1419 1.1 joerg else if (__first2 == __last2)
1420 1.1 joerg return false;
1421 1.1 joerg
1422 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1423 1.1 joerg _D1 __l1 = _VSTD::distance(__first1, __last1);
1424 1.1 joerg
1425 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1426 1.1 joerg _D2 __l2 = _VSTD::distance(__first2, __last2);
1427 1.1 joerg if (__l1 != __l2)
1428 1.1 joerg return false;
1429 1.1 joerg
1430 1.1 joerg // For each element in [f1, l1) see if there are the same number of
1431 1.1 joerg // equal elements in [f2, l2)
1432 1.1 joerg for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1433 1.1 joerg {
1434 1.1 joerg // Have we already counted the number of *__i in [f1, l1)?
1435 1.1 joerg _ForwardIterator1 __match = __first1;
1436 1.1 joerg for (; __match != __i; ++__match)
1437 1.1 joerg if (__pred(*__match, *__i))
1438 1.1 joerg break;
1439 1.1 joerg if (__match == __i) {
1440 1.1 joerg // Count number of *__i in [f2, l2)
1441 1.1 joerg _D1 __c2 = 0;
1442 1.1 joerg for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1443 1.1 joerg if (__pred(*__i, *__j))
1444 1.1 joerg ++__c2;
1445 1.1 joerg if (__c2 == 0)
1446 1.1 joerg return false;
1447 1.1 joerg // Count number of *__i in [__i, l1) (we can start with 1)
1448 1.1 joerg _D1 __c1 = 1;
1449 1.1 joerg for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1450 1.1 joerg if (__pred(*__i, *__j))
1451 1.1 joerg ++__c1;
1452 1.1 joerg if (__c1 != __c2)
1453 1.1 joerg return false;
1454 1.1 joerg }
1455 1.1 joerg }
1456 1.1 joerg return true;
1457 1.1 joerg }
1458 1.1 joerg
1459 1.1 joerg template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1460 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1461 1.1 joerg __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1462 1.1 joerg _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1463 1.1 joerg _BinaryPredicate __pred,
1464 1.1 joerg random_access_iterator_tag, random_access_iterator_tag )
1465 1.1 joerg {
1466 1.1 joerg if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1467 1.1 joerg return false;
1468 1.1 joerg return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1469 1.1 joerg typename add_lvalue_reference<_BinaryPredicate>::type>
1470 1.1 joerg (__first1, __last1, __first2, __pred );
1471 1.1 joerg }
1472 1.1 joerg
1473 1.1 joerg template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1474 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1475 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1476 1.1 joerg bool
1477 1.1 joerg is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1478 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1479 1.1 joerg _BinaryPredicate __pred )
1480 1.1 joerg {
1481 1.1 joerg return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1482 1.1 joerg (__first1, __last1, __first2, __last2, __pred,
1483 1.1 joerg typename iterator_traits<_ForwardIterator1>::iterator_category(),
1484 1.1 joerg typename iterator_traits<_ForwardIterator2>::iterator_category());
1485 1.1 joerg }
1486 1.1 joerg
1487 1.1 joerg template<class _ForwardIterator1, class _ForwardIterator2>
1488 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1489 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1490 1.1 joerg bool
1491 1.1 joerg is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1492 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1493 1.1 joerg {
1494 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1495 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1496 1.1 joerg return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1497 1.1 joerg __equal_to<__v1, __v2>(),
1498 1.1 joerg typename iterator_traits<_ForwardIterator1>::iterator_category(),
1499 1.1 joerg typename iterator_traits<_ForwardIterator2>::iterator_category());
1500 1.1 joerg }
1501 1.1 joerg #endif
1502 1.1 joerg
1503 1.1 joerg // search
1504 1.1 joerg // __search is in <functional>
1505 1.1 joerg
1506 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1507 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1508 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1509 1.1 joerg _ForwardIterator1
1510 1.1 joerg search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1511 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1512 1.1 joerg {
1513 1.1 joerg return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1514 1.1 joerg (__first1, __last1, __first2, __last2, __pred,
1515 1.1 joerg typename iterator_traits<_ForwardIterator1>::iterator_category(),
1516 1.1 joerg typename iterator_traits<_ForwardIterator2>::iterator_category())
1517 1.1 joerg .first;
1518 1.1 joerg }
1519 1.1 joerg
1520 1.1 joerg template <class _ForwardIterator1, class _ForwardIterator2>
1521 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1522 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1523 1.1 joerg _ForwardIterator1
1524 1.1 joerg search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1525 1.1 joerg _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1526 1.1 joerg {
1527 1.1 joerg typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1528 1.1 joerg typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1529 1.1 joerg return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1530 1.1 joerg }
1531 1.1 joerg
1532 1.1 joerg
1533 1.1 joerg #if _LIBCPP_STD_VER > 14
1534 1.1 joerg template <class _ForwardIterator, class _Searcher>
1535 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1536 1.1 joerg _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1537 1.1 joerg { return __s(__f, __l).first; }
1538 1.1 joerg #endif
1539 1.1 joerg
1540 1.1 joerg // search_n
1541 1.1 joerg
1542 1.1 joerg template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1543 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
1544 1.1 joerg __search_n(_ForwardIterator __first, _ForwardIterator __last,
1545 1.1 joerg _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1546 1.1 joerg {
1547 1.1 joerg if (__count <= 0)
1548 1.1 joerg return __first;
1549 1.1 joerg while (true)
1550 1.1 joerg {
1551 1.1 joerg // Find first element in sequence that matchs __value_, with a mininum of loop checks
1552 1.1 joerg while (true)
1553 1.1 joerg {
1554 1.1 joerg if (__first == __last) // return __last if no element matches __value_
1555 1.1 joerg return __last;
1556 1.1 joerg if (__pred(*__first, __value_))
1557 1.1 joerg break;
1558 1.1 joerg ++__first;
1559 1.1 joerg }
1560 1.1 joerg // *__first matches __value_, now match elements after here
1561 1.1 joerg _ForwardIterator __m = __first;
1562 1.1 joerg _Size __c(0);
1563 1.1 joerg while (true)
1564 1.1 joerg {
1565 1.1 joerg if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1566 1.1 joerg return __first;
1567 1.1 joerg if (++__m == __last) // Otherwise if source exhaused, pattern not found
1568 1.1 joerg return __last;
1569 1.1 joerg if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1570 1.1 joerg {
1571 1.1 joerg __first = __m;
1572 1.1 joerg ++__first;
1573 1.1 joerg break;
1574 1.1 joerg } // else there is a match, check next elements
1575 1.1 joerg }
1576 1.1 joerg }
1577 1.1 joerg }
1578 1.1 joerg
1579 1.1 joerg template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1580 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
1581 1.1 joerg __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1582 1.1 joerg _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1583 1.1 joerg {
1584 1.1 joerg if (__count <= 0)
1585 1.1 joerg return __first;
1586 1.1 joerg _Size __len = static_cast<_Size>(__last - __first);
1587 1.1 joerg if (__len < __count)
1588 1.1 joerg return __last;
1589 1.1 joerg const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1590 1.1 joerg while (true)
1591 1.1 joerg {
1592 1.1 joerg // Find first element in sequence that matchs __value_, with a mininum of loop checks
1593 1.1 joerg while (true)
1594 1.1 joerg {
1595 1.1 joerg if (__first >= __s) // return __last if no element matches __value_
1596 1.1 joerg return __last;
1597 1.1 joerg if (__pred(*__first, __value_))
1598 1.1 joerg break;
1599 1.1 joerg ++__first;
1600 1.1 joerg }
1601 1.1 joerg // *__first matches __value_, now match elements after here
1602 1.1 joerg _RandomAccessIterator __m = __first;
1603 1.1 joerg _Size __c(0);
1604 1.1 joerg while (true)
1605 1.1 joerg {
1606 1.1 joerg if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1607 1.1 joerg return __first;
1608 1.1 joerg ++__m; // no need to check range on __m because __s guarantees we have enough source
1609 1.1 joerg if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1610 1.1 joerg {
1611 1.1 joerg __first = __m;
1612 1.1 joerg ++__first;
1613 1.1 joerg break;
1614 1.1 joerg } // else there is a match, check next elements
1615 1.1 joerg }
1616 1.1 joerg }
1617 1.1 joerg }
1618 1.1 joerg
1619 1.1 joerg template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1620 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1621 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1622 1.1 joerg _ForwardIterator
1623 1.1 joerg search_n(_ForwardIterator __first, _ForwardIterator __last,
1624 1.1 joerg _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1625 1.1 joerg {
1626 1.1 joerg return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1627 1.1 joerg (__first, __last, _VSTD::__convert_to_integral(__count), __value_, __pred,
1628 1.1 joerg typename iterator_traits<_ForwardIterator>::iterator_category());
1629 1.1 joerg }
1630 1.1 joerg
1631 1.1 joerg template <class _ForwardIterator, class _Size, class _Tp>
1632 1.1 joerg _LIBCPP_NODISCARD_EXT inline
1633 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1634 1.1 joerg _ForwardIterator
1635 1.1 joerg search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1636 1.1 joerg {
1637 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1638 1.1 joerg return _VSTD::search_n(__first, __last, _VSTD::__convert_to_integral(__count),
1639 1.1 joerg __value_, __equal_to<__v, _Tp>());
1640 1.1 joerg }
1641 1.1 joerg
1642 1.1 joerg // __unwrap_iter, __rewrap_iter
1643 1.1 joerg
1644 1.1 joerg // The job of __unwrap_iter is to lower contiguous iterators (such as
1645 1.1 joerg // vector<T>::iterator) into pointers, to reduce the number of template
1646 1.1 joerg // instantiations and to enable pointer-based optimizations e.g. in std::copy.
1647 1.1 joerg // For iterators that are not contiguous, it must be a no-op.
1648 1.1 joerg // In debug mode, we don't do this.
1649 1.1 joerg //
1650 1.1 joerg // __unwrap_iter is non-constexpr for user-defined iterators whose
1651 1.1 joerg // `to_address` and/or `operator->` is non-constexpr. This is okay; but we
1652 1.1 joerg // try to avoid doing __unwrap_iter in constant-evaluated contexts anyway.
1653 1.1 joerg //
1654 1.1 joerg // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
1655 1.1 joerg // "unwrapped" result back into a contiguous iterator. Since contiguous iterators
1656 1.1 joerg // are random-access, we can do this portably using iterator arithmetic; this
1657 1.1 joerg // is the job of __rewrap_iter.
1658 1.1 joerg
1659 1.1 joerg template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
1660 1.1 joerg struct __unwrap_iter_impl {
1661 1.1 joerg static _LIBCPP_CONSTEXPR _Iter
1662 1.1 joerg __apply(_Iter __i) _NOEXCEPT {
1663 1.1 joerg return __i;
1664 1.1 joerg }
1665 1.1 joerg };
1666 1.1 joerg
1667 1.1 joerg #if _LIBCPP_DEBUG_LEVEL < 2
1668 1.1 joerg
1669 1.1 joerg template <class _Iter>
1670 1.1 joerg struct __unwrap_iter_impl<_Iter, true> {
1671 1.1 joerg static _LIBCPP_CONSTEXPR decltype(_VSTD::__to_address(declval<_Iter>()))
1672 1.1 joerg __apply(_Iter __i) _NOEXCEPT {
1673 1.1 joerg return _VSTD::__to_address(__i);
1674 1.1 joerg }
1675 1.1 joerg };
1676 1.1 joerg
1677 1.1 joerg #endif // _LIBCPP_DEBUG_LEVEL < 2
1678 1.1 joerg
1679 1.1 joerg template<class _Iter, class _Impl = __unwrap_iter_impl<_Iter> >
1680 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1681 1.1 joerg decltype(_Impl::__apply(declval<_Iter>()))
1682 1.1 joerg __unwrap_iter(_Iter __i) _NOEXCEPT
1683 1.1 joerg {
1684 1.1 joerg return _Impl::__apply(__i);
1685 1.1 joerg }
1686 1.1 joerg
1687 1.1 joerg template<class _OrigIter>
1688 1.1 joerg _OrigIter __rewrap_iter(_OrigIter, _OrigIter __result)
1689 1.1 joerg {
1690 1.1 joerg return __result;
1691 1.1 joerg }
1692 1.1 joerg
1693 1.1 joerg template<class _OrigIter, class _UnwrappedIter>
1694 1.1 joerg _OrigIter __rewrap_iter(_OrigIter __first, _UnwrappedIter __result)
1695 1.1 joerg {
1696 1.1 joerg // Precondition: __result is reachable from __first
1697 1.1 joerg // Precondition: _OrigIter is a contiguous iterator
1698 1.1 joerg return __first + (__result - _VSTD::__unwrap_iter(__first));
1699 1.1 joerg }
1700 1.1 joerg
1701 1.1 joerg // copy
1702 1.1 joerg
1703 1.1 joerg template <class _InputIterator, class _OutputIterator>
1704 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1705 1.1 joerg _OutputIterator
1706 1.1 joerg __copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1707 1.1 joerg {
1708 1.1 joerg for (; __first != __last; ++__first, (void) ++__result)
1709 1.1 joerg *__result = *__first;
1710 1.1 joerg return __result;
1711 1.1 joerg }
1712 1.1 joerg
1713 1.1 joerg template <class _InputIterator, class _OutputIterator>
1714 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
1715 1.1 joerg _OutputIterator
1716 1.1 joerg __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1717 1.1 joerg {
1718 1.1 joerg return _VSTD::__copy_constexpr(__first, __last, __result);
1719 1.1 joerg }
1720 1.1 joerg
1721 1.1 joerg template <class _Tp, class _Up>
1722 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
1723 1.1 joerg typename enable_if
1724 1.1 joerg <
1725 1.1 joerg is_same<typename remove_const<_Tp>::type, _Up>::value &&
1726 1.1 joerg is_trivially_copy_assignable<_Up>::value,
1727 1.1 joerg _Up*
1728 1.1 joerg >::type
1729 1.1 joerg __copy(_Tp* __first, _Tp* __last, _Up* __result)
1730 1.1 joerg {
1731 1.1 joerg const size_t __n = static_cast<size_t>(__last - __first);
1732 1.1 joerg if (__n > 0)
1733 1.1 joerg _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1734 1.1 joerg return __result + __n;
1735 1.1 joerg }
1736 1.1 joerg
1737 1.1 joerg template <class _InputIterator, class _OutputIterator>
1738 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1739 1.1 joerg _OutputIterator
1740 1.1 joerg copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1741 1.1 joerg {
1742 1.1 joerg if (__libcpp_is_constant_evaluated()) {
1743 1.1 joerg return _VSTD::__copy_constexpr(__first, __last, __result);
1744 1.1 joerg } else {
1745 1.1 joerg return _VSTD::__rewrap_iter(__result,
1746 1.1 joerg _VSTD::__copy(_VSTD::__unwrap_iter(__first),
1747 1.1 joerg _VSTD::__unwrap_iter(__last),
1748 1.1 joerg _VSTD::__unwrap_iter(__result)));
1749 1.1 joerg }
1750 1.1 joerg }
1751 1.1 joerg
1752 1.1 joerg // copy_backward
1753 1.1 joerg
1754 1.1 joerg template <class _BidirectionalIterator, class _OutputIterator>
1755 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1756 1.1 joerg _OutputIterator
1757 1.1 joerg __copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1758 1.1 joerg {
1759 1.1 joerg while (__first != __last)
1760 1.1 joerg *--__result = *--__last;
1761 1.1 joerg return __result;
1762 1.1 joerg }
1763 1.1 joerg
1764 1.1 joerg template <class _BidirectionalIterator, class _OutputIterator>
1765 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
1766 1.1 joerg _OutputIterator
1767 1.1 joerg __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1768 1.1 joerg {
1769 1.1 joerg return _VSTD::__copy_backward_constexpr(__first, __last, __result);
1770 1.1 joerg }
1771 1.1 joerg
1772 1.1 joerg template <class _Tp, class _Up>
1773 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
1774 1.1 joerg typename enable_if
1775 1.1 joerg <
1776 1.1 joerg is_same<typename remove_const<_Tp>::type, _Up>::value &&
1777 1.1 joerg is_trivially_copy_assignable<_Up>::value,
1778 1.1 joerg _Up*
1779 1.1 joerg >::type
1780 1.1 joerg __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1781 1.1 joerg {
1782 1.1 joerg const size_t __n = static_cast<size_t>(__last - __first);
1783 1.1 joerg if (__n > 0)
1784 1.1 joerg {
1785 1.1 joerg __result -= __n;
1786 1.1 joerg _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1787 1.1 joerg }
1788 1.1 joerg return __result;
1789 1.1 joerg }
1790 1.1 joerg
1791 1.1 joerg template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1792 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1793 1.1 joerg _BidirectionalIterator2
1794 1.1 joerg copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1795 1.1 joerg _BidirectionalIterator2 __result)
1796 1.1 joerg {
1797 1.1 joerg if (__libcpp_is_constant_evaluated()) {
1798 1.1 joerg return _VSTD::__copy_backward_constexpr(__first, __last, __result);
1799 1.1 joerg } else {
1800 1.1 joerg return _VSTD::__rewrap_iter(__result,
1801 1.1 joerg _VSTD::__copy_backward(_VSTD::__unwrap_iter(__first),
1802 1.1 joerg _VSTD::__unwrap_iter(__last),
1803 1.1 joerg _VSTD::__unwrap_iter(__result)));
1804 1.1 joerg }
1805 1.1 joerg }
1806 1.1 joerg
1807 1.1 joerg // copy_if
1808 1.1 joerg
1809 1.1 joerg template<class _InputIterator, class _OutputIterator, class _Predicate>
1810 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1811 1.1 joerg _OutputIterator
1812 1.1 joerg copy_if(_InputIterator __first, _InputIterator __last,
1813 1.1 joerg _OutputIterator __result, _Predicate __pred)
1814 1.1 joerg {
1815 1.1 joerg for (; __first != __last; ++__first)
1816 1.1 joerg {
1817 1.1 joerg if (__pred(*__first))
1818 1.1 joerg {
1819 1.1 joerg *__result = *__first;
1820 1.1 joerg ++__result;
1821 1.1 joerg }
1822 1.1 joerg }
1823 1.1 joerg return __result;
1824 1.1 joerg }
1825 1.1 joerg
1826 1.1 joerg // copy_n
1827 1.1 joerg
1828 1.1 joerg template<class _InputIterator, class _Size, class _OutputIterator>
1829 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1830 1.1 joerg typename enable_if
1831 1.1 joerg <
1832 1.1 joerg __is_cpp17_input_iterator<_InputIterator>::value &&
1833 1.1 joerg !__is_cpp17_random_access_iterator<_InputIterator>::value,
1834 1.1 joerg _OutputIterator
1835 1.1 joerg >::type
1836 1.1 joerg copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1837 1.1 joerg {
1838 1.1 joerg typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
1839 1.1 joerg _IntegralSize __n = __orig_n;
1840 1.1 joerg if (__n > 0)
1841 1.1 joerg {
1842 1.1 joerg *__result = *__first;
1843 1.1 joerg ++__result;
1844 1.1 joerg for (--__n; __n > 0; --__n)
1845 1.1 joerg {
1846 1.1 joerg ++__first;
1847 1.1 joerg *__result = *__first;
1848 1.1 joerg ++__result;
1849 1.1 joerg }
1850 1.1 joerg }
1851 1.1 joerg return __result;
1852 1.1 joerg }
1853 1.1 joerg
1854 1.1 joerg template<class _InputIterator, class _Size, class _OutputIterator>
1855 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1856 1.1 joerg typename enable_if
1857 1.1 joerg <
1858 1.1 joerg __is_cpp17_random_access_iterator<_InputIterator>::value,
1859 1.1 joerg _OutputIterator
1860 1.1 joerg >::type
1861 1.1 joerg copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1862 1.1 joerg {
1863 1.1 joerg typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
1864 1.1 joerg _IntegralSize __n = __orig_n;
1865 1.1 joerg return _VSTD::copy(__first, __first + __n, __result);
1866 1.1 joerg }
1867 1.1 joerg
1868 1.1 joerg // move
1869 1.1 joerg
1870 1.1 joerg template <class _InputIterator, class _OutputIterator>
1871 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1872 1.1 joerg _OutputIterator
1873 1.1 joerg __move_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1874 1.1 joerg {
1875 1.1 joerg for (; __first != __last; ++__first, (void) ++__result)
1876 1.1 joerg *__result = _VSTD::move(*__first);
1877 1.1 joerg return __result;
1878 1.1 joerg }
1879 1.1 joerg
1880 1.1 joerg template <class _InputIterator, class _OutputIterator>
1881 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1882 1.1 joerg _OutputIterator
1883 1.1 joerg __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1884 1.1 joerg {
1885 1.1 joerg return _VSTD::__move_constexpr(__first, __last, __result);
1886 1.1 joerg }
1887 1.1 joerg
1888 1.1 joerg template <class _Tp, class _Up>
1889 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1890 1.1 joerg typename enable_if
1891 1.1 joerg <
1892 1.1 joerg is_same<typename remove_const<_Tp>::type, _Up>::value &&
1893 1.1 joerg is_trivially_move_assignable<_Up>::value,
1894 1.1 joerg _Up*
1895 1.1 joerg >::type
1896 1.1 joerg __move(_Tp* __first, _Tp* __last, _Up* __result)
1897 1.1 joerg {
1898 1.1 joerg const size_t __n = static_cast<size_t>(__last - __first);
1899 1.1 joerg if (__n > 0)
1900 1.1 joerg _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1901 1.1 joerg return __result + __n;
1902 1.1 joerg }
1903 1.1 joerg
1904 1.1 joerg template <class _InputIterator, class _OutputIterator>
1905 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1906 1.1 joerg _OutputIterator
1907 1.1 joerg move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1908 1.1 joerg {
1909 1.1 joerg if (__libcpp_is_constant_evaluated()) {
1910 1.1 joerg return _VSTD::__move_constexpr(__first, __last, __result);
1911 1.1 joerg } else {
1912 1.1 joerg return _VSTD::__rewrap_iter(__result,
1913 1.1 joerg _VSTD::__move(_VSTD::__unwrap_iter(__first),
1914 1.1 joerg _VSTD::__unwrap_iter(__last),
1915 1.1 joerg _VSTD::__unwrap_iter(__result)));
1916 1.1 joerg }
1917 1.1 joerg }
1918 1.1 joerg
1919 1.1 joerg // move_backward
1920 1.1 joerg
1921 1.1 joerg template <class _InputIterator, class _OutputIterator>
1922 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1923 1.1 joerg _OutputIterator
1924 1.1 joerg __move_backward_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1925 1.1 joerg {
1926 1.1 joerg while (__first != __last)
1927 1.1 joerg *--__result = _VSTD::move(*--__last);
1928 1.1 joerg return __result;
1929 1.1 joerg }
1930 1.1 joerg
1931 1.1 joerg template <class _InputIterator, class _OutputIterator>
1932 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1933 1.1 joerg _OutputIterator
1934 1.1 joerg __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1935 1.1 joerg {
1936 1.1 joerg return _VSTD::__move_backward_constexpr(__first, __last, __result);
1937 1.1 joerg }
1938 1.1 joerg
1939 1.1 joerg template <class _Tp, class _Up>
1940 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1941 1.1 joerg typename enable_if
1942 1.1 joerg <
1943 1.1 joerg is_same<typename remove_const<_Tp>::type, _Up>::value &&
1944 1.1 joerg is_trivially_move_assignable<_Up>::value,
1945 1.1 joerg _Up*
1946 1.1 joerg >::type
1947 1.1 joerg __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1948 1.1 joerg {
1949 1.1 joerg const size_t __n = static_cast<size_t>(__last - __first);
1950 1.1 joerg if (__n > 0)
1951 1.1 joerg {
1952 1.1 joerg __result -= __n;
1953 1.1 joerg _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1954 1.1 joerg }
1955 1.1 joerg return __result;
1956 1.1 joerg }
1957 1.1 joerg
1958 1.1 joerg template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1959 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1960 1.1 joerg _BidirectionalIterator2
1961 1.1 joerg move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1962 1.1 joerg _BidirectionalIterator2 __result)
1963 1.1 joerg {
1964 1.1 joerg if (__libcpp_is_constant_evaluated()) {
1965 1.1 joerg return _VSTD::__move_backward_constexpr(__first, __last, __result);
1966 1.1 joerg } else {
1967 1.1 joerg return _VSTD::__rewrap_iter(__result,
1968 1.1 joerg _VSTD::__move_backward(_VSTD::__unwrap_iter(__first),
1969 1.1 joerg _VSTD::__unwrap_iter(__last),
1970 1.1 joerg _VSTD::__unwrap_iter(__result)));
1971 1.1 joerg }
1972 1.1 joerg }
1973 1.1 joerg
1974 1.1 joerg // iter_swap
1975 1.1 joerg
1976 1.1 joerg // moved to <type_traits> for better swap / noexcept support
1977 1.1 joerg
1978 1.1 joerg // transform
1979 1.1 joerg
1980 1.1 joerg template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1981 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1982 1.1 joerg _OutputIterator
1983 1.1 joerg transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1984 1.1 joerg {
1985 1.1 joerg for (; __first != __last; ++__first, (void) ++__result)
1986 1.1 joerg *__result = __op(*__first);
1987 1.1 joerg return __result;
1988 1.1 joerg }
1989 1.1 joerg
1990 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1991 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1992 1.1 joerg _OutputIterator
1993 1.1 joerg transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1994 1.1 joerg _OutputIterator __result, _BinaryOperation __binary_op)
1995 1.1 joerg {
1996 1.1 joerg for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1997 1.1 joerg *__result = __binary_op(*__first1, *__first2);
1998 1.1 joerg return __result;
1999 1.1 joerg }
2000 1.1 joerg
2001 1.1 joerg // replace
2002 1.1 joerg
2003 1.1 joerg template <class _ForwardIterator, class _Tp>
2004 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2005 1.1 joerg void
2006 1.1 joerg replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2007 1.1 joerg {
2008 1.1 joerg for (; __first != __last; ++__first)
2009 1.1 joerg if (*__first == __old_value)
2010 1.1 joerg *__first = __new_value;
2011 1.1 joerg }
2012 1.1 joerg
2013 1.1 joerg // replace_if
2014 1.1 joerg
2015 1.1 joerg template <class _ForwardIterator, class _Predicate, class _Tp>
2016 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2017 1.1 joerg void
2018 1.1 joerg replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2019 1.1 joerg {
2020 1.1 joerg for (; __first != __last; ++__first)
2021 1.1 joerg if (__pred(*__first))
2022 1.1 joerg *__first = __new_value;
2023 1.1 joerg }
2024 1.1 joerg
2025 1.1 joerg // replace_copy
2026 1.1 joerg
2027 1.1 joerg template <class _InputIterator, class _OutputIterator, class _Tp>
2028 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2029 1.1 joerg _OutputIterator
2030 1.1 joerg replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2031 1.1 joerg const _Tp& __old_value, const _Tp& __new_value)
2032 1.1 joerg {
2033 1.1 joerg for (; __first != __last; ++__first, (void) ++__result)
2034 1.1 joerg if (*__first == __old_value)
2035 1.1 joerg *__result = __new_value;
2036 1.1 joerg else
2037 1.1 joerg *__result = *__first;
2038 1.1 joerg return __result;
2039 1.1 joerg }
2040 1.1 joerg
2041 1.1 joerg // replace_copy_if
2042 1.1 joerg
2043 1.1 joerg template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2044 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2045 1.1 joerg _OutputIterator
2046 1.1 joerg replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2047 1.1 joerg _Predicate __pred, const _Tp& __new_value)
2048 1.1 joerg {
2049 1.1 joerg for (; __first != __last; ++__first, (void) ++__result)
2050 1.1 joerg if (__pred(*__first))
2051 1.1 joerg *__result = __new_value;
2052 1.1 joerg else
2053 1.1 joerg *__result = *__first;
2054 1.1 joerg return __result;
2055 1.1 joerg }
2056 1.1 joerg
2057 1.1 joerg // fill_n
2058 1.1 joerg
2059 1.1 joerg template <class _OutputIterator, class _Size, class _Tp>
2060 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2061 1.1 joerg _OutputIterator
2062 1.1 joerg __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2063 1.1 joerg {
2064 1.1 joerg for (; __n > 0; ++__first, (void) --__n)
2065 1.1 joerg *__first = __value_;
2066 1.1 joerg return __first;
2067 1.1 joerg }
2068 1.1 joerg
2069 1.1 joerg template <class _OutputIterator, class _Size, class _Tp>
2070 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2071 1.1 joerg _OutputIterator
2072 1.1 joerg fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2073 1.1 joerg {
2074 1.1 joerg return _VSTD::__fill_n(__first, _VSTD::__convert_to_integral(__n), __value_);
2075 1.1 joerg }
2076 1.1 joerg
2077 1.1 joerg // fill
2078 1.1 joerg
2079 1.1 joerg template <class _ForwardIterator, class _Tp>
2080 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2081 1.1 joerg void
2082 1.1 joerg __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2083 1.1 joerg {
2084 1.1 joerg for (; __first != __last; ++__first)
2085 1.1 joerg *__first = __value_;
2086 1.1 joerg }
2087 1.1 joerg
2088 1.1 joerg template <class _RandomAccessIterator, class _Tp>
2089 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2090 1.1 joerg void
2091 1.1 joerg __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2092 1.1 joerg {
2093 1.1 joerg _VSTD::fill_n(__first, __last - __first, __value_);
2094 1.1 joerg }
2095 1.1 joerg
2096 1.1 joerg template <class _ForwardIterator, class _Tp>
2097 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2098 1.1 joerg void
2099 1.1 joerg fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2100 1.1 joerg {
2101 1.1 joerg _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2102 1.1 joerg }
2103 1.1 joerg
2104 1.1 joerg // generate
2105 1.1 joerg
2106 1.1 joerg template <class _ForwardIterator, class _Generator>
2107 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2108 1.1 joerg void
2109 1.1 joerg generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2110 1.1 joerg {
2111 1.1 joerg for (; __first != __last; ++__first)
2112 1.1 joerg *__first = __gen();
2113 1.1 joerg }
2114 1.1 joerg
2115 1.1 joerg // generate_n
2116 1.1 joerg
2117 1.1 joerg template <class _OutputIterator, class _Size, class _Generator>
2118 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2119 1.1 joerg _OutputIterator
2120 1.1 joerg generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2121 1.1 joerg {
2122 1.1 joerg typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
2123 1.1 joerg _IntegralSize __n = __orig_n;
2124 1.1 joerg for (; __n > 0; ++__first, (void) --__n)
2125 1.1 joerg *__first = __gen();
2126 1.1 joerg return __first;
2127 1.1 joerg }
2128 1.1 joerg
2129 1.1 joerg // remove
2130 1.1 joerg
2131 1.1 joerg template <class _ForwardIterator, class _Tp>
2132 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2133 1.1 joerg remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2134 1.1 joerg {
2135 1.1 joerg __first = _VSTD::find(__first, __last, __value_);
2136 1.1 joerg if (__first != __last)
2137 1.1 joerg {
2138 1.1 joerg _ForwardIterator __i = __first;
2139 1.1 joerg while (++__i != __last)
2140 1.1 joerg {
2141 1.1 joerg if (!(*__i == __value_))
2142 1.1 joerg {
2143 1.1 joerg *__first = _VSTD::move(*__i);
2144 1.1 joerg ++__first;
2145 1.1 joerg }
2146 1.1 joerg }
2147 1.1 joerg }
2148 1.1 joerg return __first;
2149 1.1 joerg }
2150 1.1 joerg
2151 1.1 joerg // remove_if
2152 1.1 joerg
2153 1.1 joerg template <class _ForwardIterator, class _Predicate>
2154 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2155 1.1 joerg remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2156 1.1 joerg {
2157 1.1 joerg __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2158 1.1 joerg (__first, __last, __pred);
2159 1.1 joerg if (__first != __last)
2160 1.1 joerg {
2161 1.1 joerg _ForwardIterator __i = __first;
2162 1.1 joerg while (++__i != __last)
2163 1.1 joerg {
2164 1.1 joerg if (!__pred(*__i))
2165 1.1 joerg {
2166 1.1 joerg *__first = _VSTD::move(*__i);
2167 1.1 joerg ++__first;
2168 1.1 joerg }
2169 1.1 joerg }
2170 1.1 joerg }
2171 1.1 joerg return __first;
2172 1.1 joerg }
2173 1.1 joerg
2174 1.1 joerg // remove_copy
2175 1.1 joerg
2176 1.1 joerg template <class _InputIterator, class _OutputIterator, class _Tp>
2177 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2178 1.1 joerg _OutputIterator
2179 1.1 joerg remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2180 1.1 joerg {
2181 1.1 joerg for (; __first != __last; ++__first)
2182 1.1 joerg {
2183 1.1 joerg if (!(*__first == __value_))
2184 1.1 joerg {
2185 1.1 joerg *__result = *__first;
2186 1.1 joerg ++__result;
2187 1.1 joerg }
2188 1.1 joerg }
2189 1.1 joerg return __result;
2190 1.1 joerg }
2191 1.1 joerg
2192 1.1 joerg // remove_copy_if
2193 1.1 joerg
2194 1.1 joerg template <class _InputIterator, class _OutputIterator, class _Predicate>
2195 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2196 1.1 joerg _OutputIterator
2197 1.1 joerg remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2198 1.1 joerg {
2199 1.1 joerg for (; __first != __last; ++__first)
2200 1.1 joerg {
2201 1.1 joerg if (!__pred(*__first))
2202 1.1 joerg {
2203 1.1 joerg *__result = *__first;
2204 1.1 joerg ++__result;
2205 1.1 joerg }
2206 1.1 joerg }
2207 1.1 joerg return __result;
2208 1.1 joerg }
2209 1.1 joerg
2210 1.1 joerg // unique
2211 1.1 joerg
2212 1.1 joerg template <class _ForwardIterator, class _BinaryPredicate>
2213 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2214 1.1 joerg unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2215 1.1 joerg {
2216 1.1 joerg __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2217 1.1 joerg (__first, __last, __pred);
2218 1.1 joerg if (__first != __last)
2219 1.1 joerg {
2220 1.1 joerg // ... a a ? ...
2221 1.1 joerg // f i
2222 1.1 joerg _ForwardIterator __i = __first;
2223 1.1 joerg for (++__i; ++__i != __last;)
2224 1.1 joerg if (!__pred(*__first, *__i))
2225 1.1 joerg *++__first = _VSTD::move(*__i);
2226 1.1 joerg ++__first;
2227 1.1 joerg }
2228 1.1 joerg return __first;
2229 1.1 joerg }
2230 1.1 joerg
2231 1.1 joerg template <class _ForwardIterator>
2232 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2233 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2234 1.1 joerg _ForwardIterator
2235 1.1 joerg unique(_ForwardIterator __first, _ForwardIterator __last)
2236 1.1 joerg {
2237 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2238 1.1 joerg return _VSTD::unique(__first, __last, __equal_to<__v>());
2239 1.1 joerg }
2240 1.1 joerg
2241 1.1 joerg // unique_copy
2242 1.1 joerg
2243 1.1 joerg template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2244 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2245 1.1 joerg __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2246 1.1 joerg input_iterator_tag, output_iterator_tag)
2247 1.1 joerg {
2248 1.1 joerg if (__first != __last)
2249 1.1 joerg {
2250 1.1 joerg typename iterator_traits<_InputIterator>::value_type __t(*__first);
2251 1.1 joerg *__result = __t;
2252 1.1 joerg ++__result;
2253 1.1 joerg while (++__first != __last)
2254 1.1 joerg {
2255 1.1 joerg if (!__pred(__t, *__first))
2256 1.1 joerg {
2257 1.1 joerg __t = *__first;
2258 1.1 joerg *__result = __t;
2259 1.1 joerg ++__result;
2260 1.1 joerg }
2261 1.1 joerg }
2262 1.1 joerg }
2263 1.1 joerg return __result;
2264 1.1 joerg }
2265 1.1 joerg
2266 1.1 joerg template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2267 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2268 1.1 joerg __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2269 1.1 joerg forward_iterator_tag, output_iterator_tag)
2270 1.1 joerg {
2271 1.1 joerg if (__first != __last)
2272 1.1 joerg {
2273 1.1 joerg _ForwardIterator __i = __first;
2274 1.1 joerg *__result = *__i;
2275 1.1 joerg ++__result;
2276 1.1 joerg while (++__first != __last)
2277 1.1 joerg {
2278 1.1 joerg if (!__pred(*__i, *__first))
2279 1.1 joerg {
2280 1.1 joerg *__result = *__first;
2281 1.1 joerg ++__result;
2282 1.1 joerg __i = __first;
2283 1.1 joerg }
2284 1.1 joerg }
2285 1.1 joerg }
2286 1.1 joerg return __result;
2287 1.1 joerg }
2288 1.1 joerg
2289 1.1 joerg template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2290 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2291 1.1 joerg __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2292 1.1 joerg input_iterator_tag, forward_iterator_tag)
2293 1.1 joerg {
2294 1.1 joerg if (__first != __last)
2295 1.1 joerg {
2296 1.1 joerg *__result = *__first;
2297 1.1 joerg while (++__first != __last)
2298 1.1 joerg if (!__pred(*__result, *__first))
2299 1.1 joerg *++__result = *__first;
2300 1.1 joerg ++__result;
2301 1.1 joerg }
2302 1.1 joerg return __result;
2303 1.1 joerg }
2304 1.1 joerg
2305 1.1 joerg template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2306 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2307 1.1 joerg _OutputIterator
2308 1.1 joerg unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2309 1.1 joerg {
2310 1.1 joerg return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2311 1.1 joerg (__first, __last, __result, __pred,
2312 1.1 joerg typename iterator_traits<_InputIterator>::iterator_category(),
2313 1.1 joerg typename iterator_traits<_OutputIterator>::iterator_category());
2314 1.1 joerg }
2315 1.1 joerg
2316 1.1 joerg template <class _InputIterator, class _OutputIterator>
2317 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2318 1.1 joerg _OutputIterator
2319 1.1 joerg unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2320 1.1 joerg {
2321 1.1 joerg typedef typename iterator_traits<_InputIterator>::value_type __v;
2322 1.1 joerg return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2323 1.1 joerg }
2324 1.1 joerg
2325 1.1 joerg // reverse
2326 1.1 joerg
2327 1.1 joerg template <class _BidirectionalIterator>
2328 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2329 1.1 joerg void
2330 1.1 joerg __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2331 1.1 joerg {
2332 1.1 joerg while (__first != __last)
2333 1.1 joerg {
2334 1.1 joerg if (__first == --__last)
2335 1.1 joerg break;
2336 1.1 joerg _VSTD::iter_swap(__first, __last);
2337 1.1 joerg ++__first;
2338 1.1 joerg }
2339 1.1 joerg }
2340 1.1 joerg
2341 1.1 joerg template <class _RandomAccessIterator>
2342 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2343 1.1 joerg void
2344 1.1 joerg __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2345 1.1 joerg {
2346 1.1 joerg if (__first != __last)
2347 1.1 joerg for (; __first < --__last; ++__first)
2348 1.1 joerg _VSTD::iter_swap(__first, __last);
2349 1.1 joerg }
2350 1.1 joerg
2351 1.1 joerg template <class _BidirectionalIterator>
2352 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2353 1.1 joerg void
2354 1.1 joerg reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2355 1.1 joerg {
2356 1.1 joerg _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2357 1.1 joerg }
2358 1.1 joerg
2359 1.1 joerg // reverse_copy
2360 1.1 joerg
2361 1.1 joerg template <class _BidirectionalIterator, class _OutputIterator>
2362 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2363 1.1 joerg _OutputIterator
2364 1.1 joerg reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2365 1.1 joerg {
2366 1.1 joerg for (; __first != __last; ++__result)
2367 1.1 joerg *__result = *--__last;
2368 1.1 joerg return __result;
2369 1.1 joerg }
2370 1.1 joerg
2371 1.1 joerg // rotate
2372 1.1 joerg
2373 1.1 joerg template <class _ForwardIterator>
2374 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
2375 1.1 joerg __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2376 1.1 joerg {
2377 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2378 1.1 joerg value_type __tmp = _VSTD::move(*__first);
2379 1.1 joerg _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2380 1.1 joerg *__lm1 = _VSTD::move(__tmp);
2381 1.1 joerg return __lm1;
2382 1.1 joerg }
2383 1.1 joerg
2384 1.1 joerg template <class _BidirectionalIterator>
2385 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
2386 1.1 joerg __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2387 1.1 joerg {
2388 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2389 1.1 joerg _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2390 1.1 joerg value_type __tmp = _VSTD::move(*__lm1);
2391 1.1 joerg _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2392 1.1 joerg *__first = _VSTD::move(__tmp);
2393 1.1 joerg return __fp1;
2394 1.1 joerg }
2395 1.1 joerg
2396 1.1 joerg template <class _ForwardIterator>
2397 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX14 _ForwardIterator
2398 1.1 joerg __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2399 1.1 joerg {
2400 1.1 joerg _ForwardIterator __i = __middle;
2401 1.1 joerg while (true)
2402 1.1 joerg {
2403 1.1 joerg swap(*__first, *__i);
2404 1.1 joerg ++__first;
2405 1.1 joerg if (++__i == __last)
2406 1.1 joerg break;
2407 1.1 joerg if (__first == __middle)
2408 1.1 joerg __middle = __i;
2409 1.1 joerg }
2410 1.1 joerg _ForwardIterator __r = __first;
2411 1.1 joerg if (__first != __middle)
2412 1.1 joerg {
2413 1.1 joerg __i = __middle;
2414 1.1 joerg while (true)
2415 1.1 joerg {
2416 1.1 joerg swap(*__first, *__i);
2417 1.1 joerg ++__first;
2418 1.1 joerg if (++__i == __last)
2419 1.1 joerg {
2420 1.1 joerg if (__first == __middle)
2421 1.1 joerg break;
2422 1.1 joerg __i = __middle;
2423 1.1 joerg }
2424 1.1 joerg else if (__first == __middle)
2425 1.1 joerg __middle = __i;
2426 1.1 joerg }
2427 1.1 joerg }
2428 1.1 joerg return __r;
2429 1.1 joerg }
2430 1.1 joerg
2431 1.1 joerg template<typename _Integral>
2432 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
2433 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX14 _Integral
2434 1.1 joerg __algo_gcd(_Integral __x, _Integral __y)
2435 1.1 joerg {
2436 1.1 joerg do
2437 1.1 joerg {
2438 1.1 joerg _Integral __t = __x % __y;
2439 1.1 joerg __x = __y;
2440 1.1 joerg __y = __t;
2441 1.1 joerg } while (__y);
2442 1.1 joerg return __x;
2443 1.1 joerg }
2444 1.1 joerg
2445 1.1 joerg template<typename _RandomAccessIterator>
2446 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX14 _RandomAccessIterator
2447 1.1 joerg __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2448 1.1 joerg {
2449 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2450 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2451 1.1 joerg
2452 1.1 joerg const difference_type __m1 = __middle - __first;
2453 1.1 joerg const difference_type __m2 = __last - __middle;
2454 1.1 joerg if (__m1 == __m2)
2455 1.1 joerg {
2456 1.1 joerg _VSTD::swap_ranges(__first, __middle, __middle);
2457 1.1 joerg return __middle;
2458 1.1 joerg }
2459 1.1 joerg const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
2460 1.1 joerg for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2461 1.1 joerg {
2462 1.1 joerg value_type __t(_VSTD::move(*--__p));
2463 1.1 joerg _RandomAccessIterator __p1 = __p;
2464 1.1 joerg _RandomAccessIterator __p2 = __p1 + __m1;
2465 1.1 joerg do
2466 1.1 joerg {
2467 1.1 joerg *__p1 = _VSTD::move(*__p2);
2468 1.1 joerg __p1 = __p2;
2469 1.1 joerg const difference_type __d = __last - __p2;
2470 1.1 joerg if (__m1 < __d)
2471 1.1 joerg __p2 += __m1;
2472 1.1 joerg else
2473 1.1 joerg __p2 = __first + (__m1 - __d);
2474 1.1 joerg } while (__p2 != __p);
2475 1.1 joerg *__p1 = _VSTD::move(__t);
2476 1.1 joerg }
2477 1.1 joerg return __first + __m2;
2478 1.1 joerg }
2479 1.1 joerg
2480 1.1 joerg template <class _ForwardIterator>
2481 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
2482 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
2483 1.1 joerg __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2484 1.1 joerg _VSTD::forward_iterator_tag)
2485 1.1 joerg {
2486 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2487 1.1 joerg if (is_trivially_move_assignable<value_type>::value)
2488 1.1 joerg {
2489 1.1 joerg if (_VSTD::next(__first) == __middle)
2490 1.1 joerg return _VSTD::__rotate_left(__first, __last);
2491 1.1 joerg }
2492 1.1 joerg return _VSTD::__rotate_forward(__first, __middle, __last);
2493 1.1 joerg }
2494 1.1 joerg
2495 1.1 joerg template <class _BidirectionalIterator>
2496 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
2497 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
2498 1.1 joerg __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2499 1.1 joerg bidirectional_iterator_tag)
2500 1.1 joerg {
2501 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2502 1.1 joerg if (is_trivially_move_assignable<value_type>::value)
2503 1.1 joerg {
2504 1.1 joerg if (_VSTD::next(__first) == __middle)
2505 1.1 joerg return _VSTD::__rotate_left(__first, __last);
2506 1.1 joerg if (_VSTD::next(__middle) == __last)
2507 1.1 joerg return _VSTD::__rotate_right(__first, __last);
2508 1.1 joerg }
2509 1.1 joerg return _VSTD::__rotate_forward(__first, __middle, __last);
2510 1.1 joerg }
2511 1.1 joerg
2512 1.1 joerg template <class _RandomAccessIterator>
2513 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
2514 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
2515 1.1 joerg __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2516 1.1 joerg random_access_iterator_tag)
2517 1.1 joerg {
2518 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2519 1.1 joerg if (is_trivially_move_assignable<value_type>::value)
2520 1.1 joerg {
2521 1.1 joerg if (_VSTD::next(__first) == __middle)
2522 1.1 joerg return _VSTD::__rotate_left(__first, __last);
2523 1.1 joerg if (_VSTD::next(__middle) == __last)
2524 1.1 joerg return _VSTD::__rotate_right(__first, __last);
2525 1.1 joerg return _VSTD::__rotate_gcd(__first, __middle, __last);
2526 1.1 joerg }
2527 1.1 joerg return _VSTD::__rotate_forward(__first, __middle, __last);
2528 1.1 joerg }
2529 1.1 joerg
2530 1.1 joerg template <class _ForwardIterator>
2531 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
2532 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2533 1.1 joerg rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2534 1.1 joerg {
2535 1.1 joerg if (__first == __middle)
2536 1.1 joerg return __last;
2537 1.1 joerg if (__middle == __last)
2538 1.1 joerg return __first;
2539 1.1 joerg return _VSTD::__rotate(__first, __middle, __last,
2540 1.1 joerg typename iterator_traits<_ForwardIterator>::iterator_category());
2541 1.1 joerg }
2542 1.1 joerg
2543 1.1 joerg // rotate_copy
2544 1.1 joerg
2545 1.1 joerg template <class _ForwardIterator, class _OutputIterator>
2546 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2547 1.1 joerg _OutputIterator
2548 1.1 joerg rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2549 1.1 joerg {
2550 1.1 joerg return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2551 1.1 joerg }
2552 1.1 joerg
2553 1.1 joerg // min_element
2554 1.1 joerg
2555 1.1 joerg template <class _ForwardIterator, class _Compare>
2556 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2557 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2558 1.1 joerg _ForwardIterator
2559 1.1 joerg min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2560 1.1 joerg {
2561 1.1 joerg static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2562 1.1 joerg "std::min_element requires a ForwardIterator");
2563 1.1 joerg if (__first != __last)
2564 1.1 joerg {
2565 1.1 joerg _ForwardIterator __i = __first;
2566 1.1 joerg while (++__i != __last)
2567 1.1 joerg if (__comp(*__i, *__first))
2568 1.1 joerg __first = __i;
2569 1.1 joerg }
2570 1.1 joerg return __first;
2571 1.1 joerg }
2572 1.1 joerg
2573 1.1 joerg template <class _ForwardIterator>
2574 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2575 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2576 1.1 joerg _ForwardIterator
2577 1.1 joerg min_element(_ForwardIterator __first, _ForwardIterator __last)
2578 1.1 joerg {
2579 1.1 joerg return _VSTD::min_element(__first, __last,
2580 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type>());
2581 1.1 joerg }
2582 1.1 joerg
2583 1.1 joerg // min
2584 1.1 joerg
2585 1.1 joerg template <class _Tp, class _Compare>
2586 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2587 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2588 1.1 joerg const _Tp&
2589 1.1 joerg min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2590 1.1 joerg {
2591 1.1 joerg return __comp(__b, __a) ? __b : __a;
2592 1.1 joerg }
2593 1.1 joerg
2594 1.1 joerg template <class _Tp>
2595 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2596 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2597 1.1 joerg const _Tp&
2598 1.1 joerg min(const _Tp& __a, const _Tp& __b)
2599 1.1 joerg {
2600 1.1 joerg return _VSTD::min(__a, __b, __less<_Tp>());
2601 1.1 joerg }
2602 1.1 joerg
2603 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
2604 1.1 joerg
2605 1.1 joerg template<class _Tp, class _Compare>
2606 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2607 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2608 1.1 joerg _Tp
2609 1.1 joerg min(initializer_list<_Tp> __t, _Compare __comp)
2610 1.1 joerg {
2611 1.1 joerg return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2612 1.1 joerg }
2613 1.1 joerg
2614 1.1 joerg template<class _Tp>
2615 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2616 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2617 1.1 joerg _Tp
2618 1.1 joerg min(initializer_list<_Tp> __t)
2619 1.1 joerg {
2620 1.1 joerg return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2621 1.1 joerg }
2622 1.1 joerg
2623 1.1 joerg #endif // _LIBCPP_CXX03_LANG
2624 1.1 joerg
2625 1.1 joerg // max_element
2626 1.1 joerg
2627 1.1 joerg template <class _ForwardIterator, class _Compare>
2628 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2629 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2630 1.1 joerg _ForwardIterator
2631 1.1 joerg max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2632 1.1 joerg {
2633 1.1 joerg static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2634 1.1 joerg "std::max_element requires a ForwardIterator");
2635 1.1 joerg if (__first != __last)
2636 1.1 joerg {
2637 1.1 joerg _ForwardIterator __i = __first;
2638 1.1 joerg while (++__i != __last)
2639 1.1 joerg if (__comp(*__first, *__i))
2640 1.1 joerg __first = __i;
2641 1.1 joerg }
2642 1.1 joerg return __first;
2643 1.1 joerg }
2644 1.1 joerg
2645 1.1 joerg
2646 1.1 joerg template <class _ForwardIterator>
2647 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2648 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2649 1.1 joerg _ForwardIterator
2650 1.1 joerg max_element(_ForwardIterator __first, _ForwardIterator __last)
2651 1.1 joerg {
2652 1.1 joerg return _VSTD::max_element(__first, __last,
2653 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type>());
2654 1.1 joerg }
2655 1.1 joerg
2656 1.1 joerg // max
2657 1.1 joerg
2658 1.1 joerg template <class _Tp, class _Compare>
2659 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2660 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2661 1.1 joerg const _Tp&
2662 1.1 joerg max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2663 1.1 joerg {
2664 1.1 joerg return __comp(__a, __b) ? __b : __a;
2665 1.1 joerg }
2666 1.1 joerg
2667 1.1 joerg template <class _Tp>
2668 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2669 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2670 1.1 joerg const _Tp&
2671 1.1 joerg max(const _Tp& __a, const _Tp& __b)
2672 1.1 joerg {
2673 1.1 joerg return _VSTD::max(__a, __b, __less<_Tp>());
2674 1.1 joerg }
2675 1.1 joerg
2676 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
2677 1.1 joerg
2678 1.1 joerg template<class _Tp, class _Compare>
2679 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2680 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2681 1.1 joerg _Tp
2682 1.1 joerg max(initializer_list<_Tp> __t, _Compare __comp)
2683 1.1 joerg {
2684 1.1 joerg return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2685 1.1 joerg }
2686 1.1 joerg
2687 1.1 joerg template<class _Tp>
2688 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2689 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2690 1.1 joerg _Tp
2691 1.1 joerg max(initializer_list<_Tp> __t)
2692 1.1 joerg {
2693 1.1 joerg return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2694 1.1 joerg }
2695 1.1 joerg
2696 1.1 joerg #endif // _LIBCPP_CXX03_LANG
2697 1.1 joerg
2698 1.1 joerg #if _LIBCPP_STD_VER > 14
2699 1.1 joerg // clamp
2700 1.1 joerg template<class _Tp, class _Compare>
2701 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2702 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2703 1.1 joerg const _Tp&
2704 1.1 joerg clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2705 1.1 joerg {
2706 1.1 joerg _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2707 1.1 joerg return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2708 1.1 joerg
2709 1.1 joerg }
2710 1.1 joerg
2711 1.1 joerg template<class _Tp>
2712 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2713 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2714 1.1 joerg const _Tp&
2715 1.1 joerg clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2716 1.1 joerg {
2717 1.1 joerg return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2718 1.1 joerg }
2719 1.1 joerg #endif
2720 1.1 joerg
2721 1.1 joerg // minmax_element
2722 1.1 joerg
2723 1.1 joerg template <class _ForwardIterator, class _Compare>
2724 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
2725 1.1 joerg pair<_ForwardIterator, _ForwardIterator>
2726 1.1 joerg minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2727 1.1 joerg {
2728 1.1 joerg static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2729 1.1 joerg "std::minmax_element requires a ForwardIterator");
2730 1.1 joerg pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2731 1.1 joerg if (__first != __last)
2732 1.1 joerg {
2733 1.1 joerg if (++__first != __last)
2734 1.1 joerg {
2735 1.1 joerg if (__comp(*__first, *__result.first))
2736 1.1 joerg __result.first = __first;
2737 1.1 joerg else
2738 1.1 joerg __result.second = __first;
2739 1.1 joerg while (++__first != __last)
2740 1.1 joerg {
2741 1.1 joerg _ForwardIterator __i = __first;
2742 1.1 joerg if (++__first == __last)
2743 1.1 joerg {
2744 1.1 joerg if (__comp(*__i, *__result.first))
2745 1.1 joerg __result.first = __i;
2746 1.1 joerg else if (!__comp(*__i, *__result.second))
2747 1.1 joerg __result.second = __i;
2748 1.1 joerg break;
2749 1.1 joerg }
2750 1.1 joerg else
2751 1.1 joerg {
2752 1.1 joerg if (__comp(*__first, *__i))
2753 1.1 joerg {
2754 1.1 joerg if (__comp(*__first, *__result.first))
2755 1.1 joerg __result.first = __first;
2756 1.1 joerg if (!__comp(*__i, *__result.second))
2757 1.1 joerg __result.second = __i;
2758 1.1 joerg }
2759 1.1 joerg else
2760 1.1 joerg {
2761 1.1 joerg if (__comp(*__i, *__result.first))
2762 1.1 joerg __result.first = __i;
2763 1.1 joerg if (!__comp(*__first, *__result.second))
2764 1.1 joerg __result.second = __first;
2765 1.1 joerg }
2766 1.1 joerg }
2767 1.1 joerg }
2768 1.1 joerg }
2769 1.1 joerg }
2770 1.1 joerg return __result;
2771 1.1 joerg }
2772 1.1 joerg
2773 1.1 joerg template <class _ForwardIterator>
2774 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2775 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2776 1.1 joerg pair<_ForwardIterator, _ForwardIterator>
2777 1.1 joerg minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2778 1.1 joerg {
2779 1.1 joerg return _VSTD::minmax_element(__first, __last,
2780 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type>());
2781 1.1 joerg }
2782 1.1 joerg
2783 1.1 joerg // minmax
2784 1.1 joerg
2785 1.1 joerg template<class _Tp, class _Compare>
2786 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2787 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2788 1.1 joerg pair<const _Tp&, const _Tp&>
2789 1.1 joerg minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2790 1.1 joerg {
2791 1.1 joerg return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2792 1.1 joerg pair<const _Tp&, const _Tp&>(__a, __b);
2793 1.1 joerg }
2794 1.1 joerg
2795 1.1 joerg template<class _Tp>
2796 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2797 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2798 1.1 joerg pair<const _Tp&, const _Tp&>
2799 1.1 joerg minmax(const _Tp& __a, const _Tp& __b)
2800 1.1 joerg {
2801 1.1 joerg return _VSTD::minmax(__a, __b, __less<_Tp>());
2802 1.1 joerg }
2803 1.1 joerg
2804 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
2805 1.1 joerg
2806 1.1 joerg template<class _Tp, class _Compare>
2807 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2808 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2809 1.1 joerg pair<_Tp, _Tp>
2810 1.1 joerg minmax(initializer_list<_Tp> __t, _Compare __comp)
2811 1.1 joerg {
2812 1.1 joerg typedef typename initializer_list<_Tp>::const_iterator _Iter;
2813 1.1 joerg _Iter __first = __t.begin();
2814 1.1 joerg _Iter __last = __t.end();
2815 1.1 joerg pair<_Tp, _Tp> __result(*__first, *__first);
2816 1.1 joerg
2817 1.1 joerg ++__first;
2818 1.1 joerg if (__t.size() % 2 == 0)
2819 1.1 joerg {
2820 1.1 joerg if (__comp(*__first, __result.first))
2821 1.1 joerg __result.first = *__first;
2822 1.1 joerg else
2823 1.1 joerg __result.second = *__first;
2824 1.1 joerg ++__first;
2825 1.1 joerg }
2826 1.1 joerg
2827 1.1 joerg while (__first != __last)
2828 1.1 joerg {
2829 1.1 joerg _Tp __prev = *__first++;
2830 1.1 joerg if (__comp(*__first, __prev)) {
2831 1.1 joerg if ( __comp(*__first, __result.first)) __result.first = *__first;
2832 1.1 joerg if (!__comp(__prev, __result.second)) __result.second = __prev;
2833 1.1 joerg }
2834 1.1 joerg else {
2835 1.1 joerg if ( __comp(__prev, __result.first)) __result.first = __prev;
2836 1.1 joerg if (!__comp(*__first, __result.second)) __result.second = *__first;
2837 1.1 joerg }
2838 1.1 joerg
2839 1.1 joerg __first++;
2840 1.1 joerg }
2841 1.1 joerg return __result;
2842 1.1 joerg }
2843 1.1 joerg
2844 1.1 joerg template<class _Tp>
2845 1.1 joerg _LIBCPP_NODISCARD_EXT inline
2846 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2847 1.1 joerg pair<_Tp, _Tp>
2848 1.1 joerg minmax(initializer_list<_Tp> __t)
2849 1.1 joerg {
2850 1.1 joerg return _VSTD::minmax(__t, __less<_Tp>());
2851 1.1 joerg }
2852 1.1 joerg
2853 1.1 joerg #endif // _LIBCPP_CXX03_LANG
2854 1.1 joerg
2855 1.1 joerg // random_shuffle
2856 1.1 joerg
2857 1.1 joerg // __independent_bits_engine
2858 1.1 joerg
2859 1.1 joerg template <unsigned long long _Xp, size_t _Rp>
2860 1.1 joerg struct __log2_imp
2861 1.1 joerg {
2862 1.1 joerg static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2863 1.1 joerg : __log2_imp<_Xp, _Rp - 1>::value;
2864 1.1 joerg };
2865 1.1 joerg
2866 1.1 joerg template <unsigned long long _Xp>
2867 1.1 joerg struct __log2_imp<_Xp, 0>
2868 1.1 joerg {
2869 1.1 joerg static const size_t value = 0;
2870 1.1 joerg };
2871 1.1 joerg
2872 1.1 joerg template <size_t _Rp>
2873 1.1 joerg struct __log2_imp<0, _Rp>
2874 1.1 joerg {
2875 1.1 joerg static const size_t value = _Rp + 1;
2876 1.1 joerg };
2877 1.1 joerg
2878 1.1 joerg template <class _UIntType, _UIntType _Xp>
2879 1.1 joerg struct __log2
2880 1.1 joerg {
2881 1.1 joerg static const size_t value = __log2_imp<_Xp,
2882 1.1 joerg sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
2883 1.1 joerg };
2884 1.1 joerg
2885 1.1 joerg template<class _Engine, class _UIntType>
2886 1.1 joerg class __independent_bits_engine
2887 1.1 joerg {
2888 1.1 joerg public:
2889 1.1 joerg // types
2890 1.1 joerg typedef _UIntType result_type;
2891 1.1 joerg
2892 1.1 joerg private:
2893 1.1 joerg typedef typename _Engine::result_type _Engine_result_type;
2894 1.1 joerg typedef typename conditional
2895 1.1 joerg <
2896 1.1 joerg sizeof(_Engine_result_type) <= sizeof(result_type),
2897 1.1 joerg result_type,
2898 1.1 joerg _Engine_result_type
2899 1.1 joerg >::type _Working_result_type;
2900 1.1 joerg
2901 1.1 joerg _Engine& __e_;
2902 1.1 joerg size_t __w_;
2903 1.1 joerg size_t __w0_;
2904 1.1 joerg size_t __n_;
2905 1.1 joerg size_t __n0_;
2906 1.1 joerg _Working_result_type __y0_;
2907 1.1 joerg _Working_result_type __y1_;
2908 1.1 joerg _Engine_result_type __mask0_;
2909 1.1 joerg _Engine_result_type __mask1_;
2910 1.1 joerg
2911 1.1 joerg #ifdef _LIBCPP_CXX03_LANG
2912 1.1 joerg static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2913 1.1 joerg + _Working_result_type(1);
2914 1.1 joerg #else
2915 1.1 joerg static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2916 1.1 joerg + _Working_result_type(1);
2917 1.1 joerg #endif
2918 1.1 joerg static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2919 1.1 joerg static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2920 1.1 joerg static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2921 1.1 joerg
2922 1.1 joerg public:
2923 1.1 joerg // constructors and seeding functions
2924 1.1 joerg __independent_bits_engine(_Engine& __e, size_t __w);
2925 1.1 joerg
2926 1.1 joerg // generating functions
2927 1.1 joerg result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2928 1.1 joerg
2929 1.1 joerg private:
2930 1.1 joerg result_type __eval(false_type);
2931 1.1 joerg result_type __eval(true_type);
2932 1.1 joerg };
2933 1.1 joerg
2934 1.1 joerg template<class _Engine, class _UIntType>
2935 1.1 joerg __independent_bits_engine<_Engine, _UIntType>
2936 1.1 joerg ::__independent_bits_engine(_Engine& __e, size_t __w)
2937 1.1 joerg : __e_(__e),
2938 1.1 joerg __w_(__w)
2939 1.1 joerg {
2940 1.1 joerg __n_ = __w_ / __m + (__w_ % __m != 0);
2941 1.1 joerg __w0_ = __w_ / __n_;
2942 1.1 joerg if (_Rp == 0)
2943 1.1 joerg __y0_ = _Rp;
2944 1.1 joerg else if (__w0_ < _WDt)
2945 1.1 joerg __y0_ = (_Rp >> __w0_) << __w0_;
2946 1.1 joerg else
2947 1.1 joerg __y0_ = 0;
2948 1.1 joerg if (_Rp - __y0_ > __y0_ / __n_)
2949 1.1 joerg {
2950 1.1 joerg ++__n_;
2951 1.1 joerg __w0_ = __w_ / __n_;
2952 1.1 joerg if (__w0_ < _WDt)
2953 1.1 joerg __y0_ = (_Rp >> __w0_) << __w0_;
2954 1.1 joerg else
2955 1.1 joerg __y0_ = 0;
2956 1.1 joerg }
2957 1.1 joerg __n0_ = __n_ - __w_ % __n_;
2958 1.1 joerg if (__w0_ < _WDt - 1)
2959 1.1 joerg __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2960 1.1 joerg else
2961 1.1 joerg __y1_ = 0;
2962 1.1 joerg __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2963 1.1 joerg _Engine_result_type(0);
2964 1.1 joerg __mask1_ = __w0_ < _EDt - 1 ?
2965 1.1 joerg _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2966 1.1 joerg _Engine_result_type(~0);
2967 1.1 joerg }
2968 1.1 joerg
2969 1.1 joerg template<class _Engine, class _UIntType>
2970 1.1 joerg inline
2971 1.1 joerg _UIntType
2972 1.1 joerg __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2973 1.1 joerg {
2974 1.1 joerg return static_cast<result_type>(__e_() & __mask0_);
2975 1.1 joerg }
2976 1.1 joerg
2977 1.1 joerg template<class _Engine, class _UIntType>
2978 1.1 joerg _UIntType
2979 1.1 joerg __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2980 1.1 joerg {
2981 1.1 joerg const size_t _WRt = numeric_limits<result_type>::digits;
2982 1.1 joerg result_type _Sp = 0;
2983 1.1 joerg for (size_t __k = 0; __k < __n0_; ++__k)
2984 1.1 joerg {
2985 1.1 joerg _Engine_result_type __u;
2986 1.1 joerg do
2987 1.1 joerg {
2988 1.1 joerg __u = __e_() - _Engine::min();
2989 1.1 joerg } while (__u >= __y0_);
2990 1.1 joerg if (__w0_ < _WRt)
2991 1.1 joerg _Sp <<= __w0_;
2992 1.1 joerg else
2993 1.1 joerg _Sp = 0;
2994 1.1 joerg _Sp += __u & __mask0_;
2995 1.1 joerg }
2996 1.1 joerg for (size_t __k = __n0_; __k < __n_; ++__k)
2997 1.1 joerg {
2998 1.1 joerg _Engine_result_type __u;
2999 1.1 joerg do
3000 1.1 joerg {
3001 1.1 joerg __u = __e_() - _Engine::min();
3002 1.1 joerg } while (__u >= __y1_);
3003 1.1 joerg if (__w0_ < _WRt - 1)
3004 1.1 joerg _Sp <<= __w0_ + 1;
3005 1.1 joerg else
3006 1.1 joerg _Sp = 0;
3007 1.1 joerg _Sp += __u & __mask1_;
3008 1.1 joerg }
3009 1.1 joerg return _Sp;
3010 1.1 joerg }
3011 1.1 joerg
3012 1.1 joerg // uniform_int_distribution
3013 1.1 joerg
3014 1.1 joerg template<class _IntType = int>
3015 1.1 joerg class uniform_int_distribution
3016 1.1 joerg {
3017 1.1 joerg public:
3018 1.1 joerg // types
3019 1.1 joerg typedef _IntType result_type;
3020 1.1 joerg
3021 1.1 joerg class param_type
3022 1.1 joerg {
3023 1.1 joerg result_type __a_;
3024 1.1 joerg result_type __b_;
3025 1.1 joerg public:
3026 1.1 joerg typedef uniform_int_distribution distribution_type;
3027 1.1 joerg
3028 1.1 joerg explicit param_type(result_type __a = 0,
3029 1.1 joerg result_type __b = numeric_limits<result_type>::max())
3030 1.1 joerg : __a_(__a), __b_(__b) {}
3031 1.1 joerg
3032 1.1 joerg result_type a() const {return __a_;}
3033 1.1 joerg result_type b() const {return __b_;}
3034 1.1 joerg
3035 1.1 joerg friend bool operator==(const param_type& __x, const param_type& __y)
3036 1.1 joerg {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3037 1.1 joerg friend bool operator!=(const param_type& __x, const param_type& __y)
3038 1.1 joerg {return !(__x == __y);}
3039 1.1 joerg };
3040 1.1 joerg
3041 1.1 joerg private:
3042 1.1 joerg param_type __p_;
3043 1.1 joerg
3044 1.1 joerg public:
3045 1.1 joerg // constructors and reset functions
3046 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
3047 1.1 joerg uniform_int_distribution() : uniform_int_distribution(0) {}
3048 1.1 joerg explicit uniform_int_distribution(
3049 1.1 joerg result_type __a, result_type __b = numeric_limits<result_type>::max())
3050 1.1 joerg : __p_(param_type(__a, __b)) {}
3051 1.1 joerg #else
3052 1.1 joerg explicit uniform_int_distribution(
3053 1.1 joerg result_type __a = 0,
3054 1.1 joerg result_type __b = numeric_limits<result_type>::max())
3055 1.1 joerg : __p_(param_type(__a, __b)) {}
3056 1.1 joerg #endif
3057 1.1 joerg explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3058 1.1 joerg void reset() {}
3059 1.1 joerg
3060 1.1 joerg // generating functions
3061 1.1 joerg template<class _URNG> result_type operator()(_URNG& __g)
3062 1.1 joerg {return (*this)(__g, __p_);}
3063 1.1 joerg template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3064 1.1 joerg
3065 1.1 joerg // property functions
3066 1.1 joerg result_type a() const {return __p_.a();}
3067 1.1 joerg result_type b() const {return __p_.b();}
3068 1.1 joerg
3069 1.1 joerg param_type param() const {return __p_;}
3070 1.1 joerg void param(const param_type& __p) {__p_ = __p;}
3071 1.1 joerg
3072 1.1 joerg result_type min() const {return a();}
3073 1.1 joerg result_type max() const {return b();}
3074 1.1 joerg
3075 1.1 joerg friend bool operator==(const uniform_int_distribution& __x,
3076 1.1 joerg const uniform_int_distribution& __y)
3077 1.1 joerg {return __x.__p_ == __y.__p_;}
3078 1.1 joerg friend bool operator!=(const uniform_int_distribution& __x,
3079 1.1 joerg const uniform_int_distribution& __y)
3080 1.1 joerg {return !(__x == __y);}
3081 1.1 joerg };
3082 1.1 joerg
3083 1.1 joerg template<class _IntType>
3084 1.1 joerg template<class _URNG>
3085 1.1 joerg typename uniform_int_distribution<_IntType>::result_type
3086 1.1 joerg uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3087 1.1 joerg _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
3088 1.1 joerg {
3089 1.1 joerg typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3090 1.1 joerg uint32_t, uint64_t>::type _UIntType;
3091 1.1 joerg const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
3092 1.1 joerg if (_Rp == 1)
3093 1.1 joerg return __p.a();
3094 1.1 joerg const size_t _Dt = numeric_limits<_UIntType>::digits;
3095 1.1 joerg typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3096 1.1 joerg if (_Rp == 0)
3097 1.1 joerg return static_cast<result_type>(_Eng(__g, _Dt)());
3098 1.1 joerg size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
3099 1.1 joerg if ((_Rp & (numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3100 1.1 joerg ++__w;
3101 1.1 joerg _Eng __e(__g, __w);
3102 1.1 joerg _UIntType __u;
3103 1.1 joerg do
3104 1.1 joerg {
3105 1.1 joerg __u = __e();
3106 1.1 joerg } while (__u >= _Rp);
3107 1.1 joerg return static_cast<result_type>(__u + __p.a());
3108 1.1 joerg }
3109 1.1 joerg
3110 1.1 joerg #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3111 1.1 joerg || defined(_LIBCPP_BUILDING_LIBRARY)
3112 1.1 joerg class _LIBCPP_TYPE_VIS __rs_default;
3113 1.1 joerg
3114 1.1 joerg _LIBCPP_FUNC_VIS __rs_default __rs_get();
3115 1.1 joerg
3116 1.1 joerg class _LIBCPP_TYPE_VIS __rs_default
3117 1.1 joerg {
3118 1.1 joerg static unsigned __c_;
3119 1.1 joerg
3120 1.1 joerg __rs_default();
3121 1.1 joerg public:
3122 1.1 joerg typedef uint_fast32_t result_type;
3123 1.1 joerg
3124 1.1 joerg static const result_type _Min = 0;
3125 1.1 joerg static const result_type _Max = 0xFFFFFFFF;
3126 1.1 joerg
3127 1.1 joerg __rs_default(const __rs_default&);
3128 1.1 joerg ~__rs_default();
3129 1.1 joerg
3130 1.1 joerg result_type operator()();
3131 1.1 joerg
3132 1.1 joerg static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3133 1.1 joerg static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3134 1.1 joerg
3135 1.1 joerg friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3136 1.1 joerg };
3137 1.1 joerg
3138 1.1 joerg _LIBCPP_FUNC_VIS __rs_default __rs_get();
3139 1.1 joerg
3140 1.1 joerg template <class _RandomAccessIterator>
3141 1.1 joerg _LIBCPP_DEPRECATED_IN_CXX14 void
3142 1.1 joerg random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3143 1.1 joerg {
3144 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3145 1.1 joerg typedef uniform_int_distribution<ptrdiff_t> _Dp;
3146 1.1 joerg typedef typename _Dp::param_type _Pp;
3147 1.1 joerg difference_type __d = __last - __first;
3148 1.1 joerg if (__d > 1)
3149 1.1 joerg {
3150 1.1 joerg _Dp __uid;
3151 1.1 joerg __rs_default __g = __rs_get();
3152 1.1 joerg for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3153 1.1 joerg {
3154 1.1 joerg difference_type __i = __uid(__g, _Pp(0, __d));
3155 1.1 joerg if (__i != difference_type(0))
3156 1.1 joerg swap(*__first, *(__first + __i));
3157 1.1 joerg }
3158 1.1 joerg }
3159 1.1 joerg }
3160 1.1 joerg
3161 1.1 joerg template <class _RandomAccessIterator, class _RandomNumberGenerator>
3162 1.1 joerg _LIBCPP_DEPRECATED_IN_CXX14 void
3163 1.1 joerg random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3164 1.1 joerg #ifndef _LIBCPP_CXX03_LANG
3165 1.1 joerg _RandomNumberGenerator&& __rand)
3166 1.1 joerg #else
3167 1.1 joerg _RandomNumberGenerator& __rand)
3168 1.1 joerg #endif
3169 1.1 joerg {
3170 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3171 1.1 joerg difference_type __d = __last - __first;
3172 1.1 joerg if (__d > 1)
3173 1.1 joerg {
3174 1.1 joerg for (--__last; __first < __last; ++__first, (void) --__d)
3175 1.1 joerg {
3176 1.1 joerg difference_type __i = __rand(__d);
3177 1.1 joerg if (__i != difference_type(0))
3178 1.1 joerg swap(*__first, *(__first + __i));
3179 1.1 joerg }
3180 1.1 joerg }
3181 1.1 joerg }
3182 1.1 joerg #endif
3183 1.1 joerg
3184 1.1 joerg template <class _PopulationIterator, class _SampleIterator, class _Distance,
3185 1.1 joerg class _UniformRandomNumberGenerator>
3186 1.1 joerg _LIBCPP_INLINE_VISIBILITY
3187 1.1 joerg _SampleIterator __sample(_PopulationIterator __first,
3188 1.1 joerg _PopulationIterator __last, _SampleIterator __output_iter,
3189 1.1 joerg _Distance __n,
3190 1.1 joerg _UniformRandomNumberGenerator & __g,
3191 1.1 joerg input_iterator_tag) {
3192 1.1 joerg
3193 1.1 joerg _Distance __k = 0;
3194 1.1 joerg for (; __first != __last && __k < __n; ++__first, (void) ++__k)
3195 1.1 joerg __output_iter[__k] = *__first;
3196 1.1 joerg _Distance __sz = __k;
3197 1.1 joerg for (; __first != __last; ++__first, (void) ++__k) {
3198 1.1 joerg _Distance __r = uniform_int_distribution<_Distance>(0, __k)(__g);
3199 1.1 joerg if (__r < __sz)
3200 1.1 joerg __output_iter[__r] = *__first;
3201 1.1 joerg }
3202 1.1 joerg return __output_iter + _VSTD::min(__n, __k);
3203 1.1 joerg }
3204 1.1 joerg
3205 1.1 joerg template <class _PopulationIterator, class _SampleIterator, class _Distance,
3206 1.1 joerg class _UniformRandomNumberGenerator>
3207 1.1 joerg _LIBCPP_INLINE_VISIBILITY
3208 1.1 joerg _SampleIterator __sample(_PopulationIterator __first,
3209 1.1 joerg _PopulationIterator __last, _SampleIterator __output_iter,
3210 1.1 joerg _Distance __n,
3211 1.1 joerg _UniformRandomNumberGenerator& __g,
3212 1.1 joerg forward_iterator_tag) {
3213 1.1 joerg _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3214 1.1 joerg for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3215 1.1 joerg _Distance __r = uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3216 1.1 joerg if (__r < __n) {
3217 1.1 joerg *__output_iter++ = *__first;
3218 1.1 joerg --__n;
3219 1.1 joerg }
3220 1.1 joerg }
3221 1.1 joerg return __output_iter;
3222 1.1 joerg }
3223 1.1 joerg
3224 1.1 joerg template <class _PopulationIterator, class _SampleIterator, class _Distance,
3225 1.1 joerg class _UniformRandomNumberGenerator>
3226 1.1 joerg _LIBCPP_INLINE_VISIBILITY
3227 1.1 joerg _SampleIterator __sample(_PopulationIterator __first,
3228 1.1 joerg _PopulationIterator __last, _SampleIterator __output_iter,
3229 1.1 joerg _Distance __n, _UniformRandomNumberGenerator& __g) {
3230 1.1 joerg typedef typename iterator_traits<_PopulationIterator>::iterator_category
3231 1.1 joerg _PopCategory;
3232 1.1 joerg typedef typename iterator_traits<_PopulationIterator>::difference_type
3233 1.1 joerg _Difference;
3234 1.1 joerg static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
3235 1.1 joerg __is_cpp17_random_access_iterator<_SampleIterator>::value,
3236 1.1 joerg "SampleIterator must meet the requirements of RandomAccessIterator");
3237 1.1 joerg typedef typename common_type<_Distance, _Difference>::type _CommonType;
3238 1.1 joerg _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3239 1.1 joerg return _VSTD::__sample(
3240 1.1 joerg __first, __last, __output_iter, _CommonType(__n),
3241 1.1 joerg __g, _PopCategory());
3242 1.1 joerg }
3243 1.1 joerg
3244 1.1 joerg #if _LIBCPP_STD_VER > 14
3245 1.1 joerg template <class _PopulationIterator, class _SampleIterator, class _Distance,
3246 1.1 joerg class _UniformRandomNumberGenerator>
3247 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
3248 1.1 joerg _SampleIterator sample(_PopulationIterator __first,
3249 1.1 joerg _PopulationIterator __last, _SampleIterator __output_iter,
3250 1.1 joerg _Distance __n, _UniformRandomNumberGenerator&& __g) {
3251 1.1 joerg return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
3252 1.1 joerg }
3253 1.1 joerg #endif // _LIBCPP_STD_VER > 14
3254 1.1 joerg
3255 1.1 joerg template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3256 1.1 joerg void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3257 1.1 joerg _UniformRandomNumberGenerator&& __g)
3258 1.1 joerg {
3259 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3260 1.1 joerg typedef uniform_int_distribution<ptrdiff_t> _Dp;
3261 1.1 joerg typedef typename _Dp::param_type _Pp;
3262 1.1 joerg difference_type __d = __last - __first;
3263 1.1 joerg if (__d > 1)
3264 1.1 joerg {
3265 1.1 joerg _Dp __uid;
3266 1.1 joerg for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3267 1.1 joerg {
3268 1.1 joerg difference_type __i = __uid(__g, _Pp(0, __d));
3269 1.1 joerg if (__i != difference_type(0))
3270 1.1 joerg swap(*__first, *(__first + __i));
3271 1.1 joerg }
3272 1.1 joerg }
3273 1.1 joerg }
3274 1.1 joerg
3275 1.1 joerg #if _LIBCPP_STD_VER > 17
3276 1.1 joerg
3277 1.1 joerg // shift_left, shift_right
3278 1.1 joerg
3279 1.1 joerg template <class _ForwardIterator>
3280 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY constexpr
3281 1.1 joerg _ForwardIterator
3282 1.1 joerg shift_left(_ForwardIterator __first, _ForwardIterator __last,
3283 1.1 joerg typename iterator_traits<_ForwardIterator>::difference_type __n)
3284 1.1 joerg {
3285 1.1 joerg if (__n == 0) {
3286 1.1 joerg return __last;
3287 1.1 joerg }
3288 1.1 joerg
3289 1.1 joerg _ForwardIterator __m = __first;
3290 1.1 joerg if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3291 1.1 joerg if (__n >= __last - __first) {
3292 1.1 joerg return __first;
3293 1.1 joerg }
3294 1.1 joerg __m += __n;
3295 1.1 joerg } else {
3296 1.1 joerg for (; __n > 0; --__n) {
3297 1.1 joerg if (__m == __last) {
3298 1.1 joerg return __first;
3299 1.1 joerg }
3300 1.1 joerg ++__m;
3301 1.1 joerg }
3302 1.1 joerg }
3303 1.1 joerg return _VSTD::move(__m, __last, __first);
3304 1.1 joerg }
3305 1.1 joerg
3306 1.1 joerg template <class _ForwardIterator>
3307 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY constexpr
3308 1.1 joerg _ForwardIterator
3309 1.1 joerg shift_right(_ForwardIterator __first, _ForwardIterator __last,
3310 1.1 joerg typename iterator_traits<_ForwardIterator>::difference_type __n)
3311 1.1 joerg {
3312 1.1 joerg if (__n == 0) {
3313 1.1 joerg return __first;
3314 1.1 joerg }
3315 1.1 joerg
3316 1.1 joerg if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3317 1.1 joerg decltype(__n) __d = __last - __first;
3318 1.1 joerg if (__n >= __d) {
3319 1.1 joerg return __last;
3320 1.1 joerg }
3321 1.1 joerg _ForwardIterator __m = __first + (__d - __n);
3322 1.1 joerg return _VSTD::move_backward(__first, __m, __last);
3323 1.1 joerg } else if constexpr (__is_cpp17_bidirectional_iterator<_ForwardIterator>::value) {
3324 1.1 joerg _ForwardIterator __m = __last;
3325 1.1 joerg for (; __n > 0; --__n) {
3326 1.1 joerg if (__m == __first) {
3327 1.1 joerg return __last;
3328 1.1 joerg }
3329 1.1 joerg --__m;
3330 1.1 joerg }
3331 1.1 joerg return _VSTD::move_backward(__first, __m, __last);
3332 1.1 joerg } else {
3333 1.1 joerg _ForwardIterator __ret = __first;
3334 1.1 joerg for (; __n > 0; --__n) {
3335 1.1 joerg if (__ret == __last) {
3336 1.1 joerg return __last;
3337 1.1 joerg }
3338 1.1 joerg ++__ret;
3339 1.1 joerg }
3340 1.1 joerg
3341 1.1 joerg // We have an __n-element scratch space from __first to __ret.
3342 1.1 joerg // Slide an __n-element window [__trail, __lead) from left to right.
3343 1.1 joerg // We're essentially doing swap_ranges(__first, __ret, __trail, __lead)
3344 1.1 joerg // over and over; but once __lead reaches __last we needn't bother
3345 1.1 joerg // to save the values of elements [__trail, __last).
3346 1.1 joerg
3347 1.1 joerg auto __trail = __first;
3348 1.1 joerg auto __lead = __ret;
3349 1.1 joerg while (__trail != __ret) {
3350 1.1 joerg if (__lead == __last) {
3351 1.1 joerg _VSTD::move(__first, __trail, __ret);
3352 1.1 joerg return __ret;
3353 1.1 joerg }
3354 1.1 joerg ++__trail;
3355 1.1 joerg ++__lead;
3356 1.1 joerg }
3357 1.1 joerg
3358 1.1 joerg _ForwardIterator __mid = __first;
3359 1.1 joerg while (true) {
3360 1.1 joerg if (__lead == __last) {
3361 1.1 joerg __trail = _VSTD::move(__mid, __ret, __trail);
3362 1.1 joerg _VSTD::move(__first, __mid, __trail);
3363 1.1 joerg return __ret;
3364 1.1 joerg }
3365 1.1 joerg swap(*__mid, *__trail);
3366 1.1 joerg ++__mid;
3367 1.1 joerg ++__trail;
3368 1.1 joerg ++__lead;
3369 1.1 joerg if (__mid == __ret) {
3370 1.1 joerg __mid = __first;
3371 1.1 joerg }
3372 1.1 joerg }
3373 1.1 joerg }
3374 1.1 joerg }
3375 1.1 joerg
3376 1.1 joerg #endif // _LIBCPP_STD_VER > 17
3377 1.1 joerg
3378 1.1 joerg // is_partitioned
3379 1.1 joerg
3380 1.1 joerg template <class _InputIterator, class _Predicate>
3381 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
3382 1.1 joerg is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3383 1.1 joerg {
3384 1.1 joerg for (; __first != __last; ++__first)
3385 1.1 joerg if (!__pred(*__first))
3386 1.1 joerg break;
3387 1.1 joerg if ( __first == __last )
3388 1.1 joerg return true;
3389 1.1 joerg ++__first;
3390 1.1 joerg for (; __first != __last; ++__first)
3391 1.1 joerg if (__pred(*__first))
3392 1.1 joerg return false;
3393 1.1 joerg return true;
3394 1.1 joerg }
3395 1.1 joerg
3396 1.1 joerg // partition
3397 1.1 joerg
3398 1.1 joerg template <class _Predicate, class _ForwardIterator>
3399 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3400 1.1 joerg __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3401 1.1 joerg {
3402 1.1 joerg while (true)
3403 1.1 joerg {
3404 1.1 joerg if (__first == __last)
3405 1.1 joerg return __first;
3406 1.1 joerg if (!__pred(*__first))
3407 1.1 joerg break;
3408 1.1 joerg ++__first;
3409 1.1 joerg }
3410 1.1 joerg for (_ForwardIterator __p = __first; ++__p != __last;)
3411 1.1 joerg {
3412 1.1 joerg if (__pred(*__p))
3413 1.1 joerg {
3414 1.1 joerg swap(*__first, *__p);
3415 1.1 joerg ++__first;
3416 1.1 joerg }
3417 1.1 joerg }
3418 1.1 joerg return __first;
3419 1.1 joerg }
3420 1.1 joerg
3421 1.1 joerg template <class _Predicate, class _BidirectionalIterator>
3422 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
3423 1.1 joerg __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3424 1.1 joerg bidirectional_iterator_tag)
3425 1.1 joerg {
3426 1.1 joerg while (true)
3427 1.1 joerg {
3428 1.1 joerg while (true)
3429 1.1 joerg {
3430 1.1 joerg if (__first == __last)
3431 1.1 joerg return __first;
3432 1.1 joerg if (!__pred(*__first))
3433 1.1 joerg break;
3434 1.1 joerg ++__first;
3435 1.1 joerg }
3436 1.1 joerg do
3437 1.1 joerg {
3438 1.1 joerg if (__first == --__last)
3439 1.1 joerg return __first;
3440 1.1 joerg } while (!__pred(*__last));
3441 1.1 joerg swap(*__first, *__last);
3442 1.1 joerg ++__first;
3443 1.1 joerg }
3444 1.1 joerg }
3445 1.1 joerg
3446 1.1 joerg template <class _ForwardIterator, class _Predicate>
3447 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3448 1.1 joerg _ForwardIterator
3449 1.1 joerg partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3450 1.1 joerg {
3451 1.1 joerg return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3452 1.1 joerg (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3453 1.1 joerg }
3454 1.1 joerg
3455 1.1 joerg // partition_copy
3456 1.1 joerg
3457 1.1 joerg template <class _InputIterator, class _OutputIterator1,
3458 1.1 joerg class _OutputIterator2, class _Predicate>
3459 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
3460 1.1 joerg partition_copy(_InputIterator __first, _InputIterator __last,
3461 1.1 joerg _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3462 1.1 joerg _Predicate __pred)
3463 1.1 joerg {
3464 1.1 joerg for (; __first != __last; ++__first)
3465 1.1 joerg {
3466 1.1 joerg if (__pred(*__first))
3467 1.1 joerg {
3468 1.1 joerg *__out_true = *__first;
3469 1.1 joerg ++__out_true;
3470 1.1 joerg }
3471 1.1 joerg else
3472 1.1 joerg {
3473 1.1 joerg *__out_false = *__first;
3474 1.1 joerg ++__out_false;
3475 1.1 joerg }
3476 1.1 joerg }
3477 1.1 joerg return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3478 1.1 joerg }
3479 1.1 joerg
3480 1.1 joerg // partition_point
3481 1.1 joerg
3482 1.1 joerg template<class _ForwardIterator, class _Predicate>
3483 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3484 1.1 joerg partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3485 1.1 joerg {
3486 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3487 1.1 joerg difference_type __len = _VSTD::distance(__first, __last);
3488 1.1 joerg while (__len != 0)
3489 1.1 joerg {
3490 1.1 joerg difference_type __l2 = _VSTD::__half_positive(__len);
3491 1.1 joerg _ForwardIterator __m = __first;
3492 1.1 joerg _VSTD::advance(__m, __l2);
3493 1.1 joerg if (__pred(*__m))
3494 1.1 joerg {
3495 1.1 joerg __first = ++__m;
3496 1.1 joerg __len -= __l2 + 1;
3497 1.1 joerg }
3498 1.1 joerg else
3499 1.1 joerg __len = __l2;
3500 1.1 joerg }
3501 1.1 joerg return __first;
3502 1.1 joerg }
3503 1.1 joerg
3504 1.1 joerg // stable_partition
3505 1.1 joerg
3506 1.1 joerg template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3507 1.1 joerg _ForwardIterator
3508 1.1 joerg __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3509 1.1 joerg _Distance __len, _Pair __p, forward_iterator_tag __fit)
3510 1.1 joerg {
3511 1.1 joerg // *__first is known to be false
3512 1.1 joerg // __len >= 1
3513 1.1 joerg if (__len == 1)
3514 1.1 joerg return __first;
3515 1.1 joerg if (__len == 2)
3516 1.1 joerg {
3517 1.1 joerg _ForwardIterator __m = __first;
3518 1.1 joerg if (__pred(*++__m))
3519 1.1 joerg {
3520 1.1 joerg swap(*__first, *__m);
3521 1.1 joerg return __m;
3522 1.1 joerg }
3523 1.1 joerg return __first;
3524 1.1 joerg }
3525 1.1 joerg if (__len <= __p.second)
3526 1.1 joerg { // The buffer is big enough to use
3527 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3528 1.1 joerg __destruct_n __d(0);
3529 1.1 joerg unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3530 1.1 joerg // Move the falses into the temporary buffer, and the trues to the front of the line
3531 1.1 joerg // Update __first to always point to the end of the trues
3532 1.1 joerg value_type* __t = __p.first;
3533 1.1 joerg ::new ((void*)__t) value_type(_VSTD::move(*__first));
3534 1.1 joerg __d.template __incr<value_type>();
3535 1.1 joerg ++__t;
3536 1.1 joerg _ForwardIterator __i = __first;
3537 1.1 joerg while (++__i != __last)
3538 1.1 joerg {
3539 1.1 joerg if (__pred(*__i))
3540 1.1 joerg {
3541 1.1 joerg *__first = _VSTD::move(*__i);
3542 1.1 joerg ++__first;
3543 1.1 joerg }
3544 1.1 joerg else
3545 1.1 joerg {
3546 1.1 joerg ::new ((void*)__t) value_type(_VSTD::move(*__i));
3547 1.1 joerg __d.template __incr<value_type>();
3548 1.1 joerg ++__t;
3549 1.1 joerg }
3550 1.1 joerg }
3551 1.1 joerg // All trues now at start of range, all falses in buffer
3552 1.1 joerg // Move falses back into range, but don't mess up __first which points to first false
3553 1.1 joerg __i = __first;
3554 1.1 joerg for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3555 1.1 joerg *__i = _VSTD::move(*__t2);
3556 1.1 joerg // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3557 1.1 joerg return __first;
3558 1.1 joerg }
3559 1.1 joerg // Else not enough buffer, do in place
3560 1.1 joerg // __len >= 3
3561 1.1 joerg _ForwardIterator __m = __first;
3562 1.1 joerg _Distance __len2 = __len / 2; // __len2 >= 2
3563 1.1 joerg _VSTD::advance(__m, __len2);
3564 1.1 joerg // recurse on [__first, __m), *__first know to be false
3565 1.1 joerg // F?????????????????
3566 1.1 joerg // f m l
3567 1.1 joerg typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3568 1.1 joerg _ForwardIterator __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3569 1.1 joerg // TTTFFFFF??????????
3570 1.1 joerg // f ff m l
3571 1.1 joerg // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3572 1.1 joerg _ForwardIterator __m1 = __m;
3573 1.1 joerg _ForwardIterator __second_false = __last;
3574 1.1 joerg _Distance __len_half = __len - __len2;
3575 1.1 joerg while (__pred(*__m1))
3576 1.1 joerg {
3577 1.1 joerg if (++__m1 == __last)
3578 1.1 joerg goto __second_half_done;
3579 1.1 joerg --__len_half;
3580 1.1 joerg }
3581 1.1 joerg // TTTFFFFFTTTF??????
3582 1.1 joerg // f ff m m1 l
3583 1.1 joerg __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3584 1.1 joerg __second_half_done:
3585 1.1 joerg // TTTFFFFFTTTTTFFFFF
3586 1.1 joerg // f ff m sf l
3587 1.1 joerg return _VSTD::rotate(__first_false, __m, __second_false);
3588 1.1 joerg // TTTTTTTTFFFFFFFFFF
3589 1.1 joerg // |
3590 1.1 joerg }
3591 1.1 joerg
3592 1.1 joerg struct __return_temporary_buffer
3593 1.1 joerg {
3594 1.1 joerg template <class _Tp>
3595 1.1 joerg _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3596 1.1 joerg };
3597 1.1 joerg
3598 1.1 joerg template <class _Predicate, class _ForwardIterator>
3599 1.1 joerg _ForwardIterator
3600 1.1 joerg __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3601 1.1 joerg forward_iterator_tag)
3602 1.1 joerg {
3603 1.1 joerg const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3604 1.1 joerg // Either prove all true and return __first or point to first false
3605 1.1 joerg while (true)
3606 1.1 joerg {
3607 1.1 joerg if (__first == __last)
3608 1.1 joerg return __first;
3609 1.1 joerg if (!__pred(*__first))
3610 1.1 joerg break;
3611 1.1 joerg ++__first;
3612 1.1 joerg }
3613 1.1 joerg // We now have a reduced range [__first, __last)
3614 1.1 joerg // *__first is known to be false
3615 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3616 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3617 1.1 joerg difference_type __len = _VSTD::distance(__first, __last);
3618 1.1 joerg pair<value_type*, ptrdiff_t> __p(0, 0);
3619 1.1 joerg unique_ptr<value_type, __return_temporary_buffer> __h;
3620 1.1 joerg if (__len >= __alloc_limit)
3621 1.1 joerg {
3622 1.1 joerg __p = _VSTD::get_temporary_buffer<value_type>(__len);
3623 1.1 joerg __h.reset(__p.first);
3624 1.1 joerg }
3625 1.1 joerg return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3626 1.1 joerg (__first, __last, __pred, __len, __p, forward_iterator_tag());
3627 1.1 joerg }
3628 1.1 joerg
3629 1.1 joerg template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3630 1.1 joerg _BidirectionalIterator
3631 1.1 joerg __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3632 1.1 joerg _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3633 1.1 joerg {
3634 1.1 joerg // *__first is known to be false
3635 1.1 joerg // *__last is known to be true
3636 1.1 joerg // __len >= 2
3637 1.1 joerg if (__len == 2)
3638 1.1 joerg {
3639 1.1 joerg swap(*__first, *__last);
3640 1.1 joerg return __last;
3641 1.1 joerg }
3642 1.1 joerg if (__len == 3)
3643 1.1 joerg {
3644 1.1 joerg _BidirectionalIterator __m = __first;
3645 1.1 joerg if (__pred(*++__m))
3646 1.1 joerg {
3647 1.1 joerg swap(*__first, *__m);
3648 1.1 joerg swap(*__m, *__last);
3649 1.1 joerg return __last;
3650 1.1 joerg }
3651 1.1 joerg swap(*__m, *__last);
3652 1.1 joerg swap(*__first, *__m);
3653 1.1 joerg return __m;
3654 1.1 joerg }
3655 1.1 joerg if (__len <= __p.second)
3656 1.1 joerg { // The buffer is big enough to use
3657 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3658 1.1 joerg __destruct_n __d(0);
3659 1.1 joerg unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3660 1.1 joerg // Move the falses into the temporary buffer, and the trues to the front of the line
3661 1.1 joerg // Update __first to always point to the end of the trues
3662 1.1 joerg value_type* __t = __p.first;
3663 1.1 joerg ::new ((void*)__t) value_type(_VSTD::move(*__first));
3664 1.1 joerg __d.template __incr<value_type>();
3665 1.1 joerg ++__t;
3666 1.1 joerg _BidirectionalIterator __i = __first;
3667 1.1 joerg while (++__i != __last)
3668 1.1 joerg {
3669 1.1 joerg if (__pred(*__i))
3670 1.1 joerg {
3671 1.1 joerg *__first = _VSTD::move(*__i);
3672 1.1 joerg ++__first;
3673 1.1 joerg }
3674 1.1 joerg else
3675 1.1 joerg {
3676 1.1 joerg ::new ((void*)__t) value_type(_VSTD::move(*__i));
3677 1.1 joerg __d.template __incr<value_type>();
3678 1.1 joerg ++__t;
3679 1.1 joerg }
3680 1.1 joerg }
3681 1.1 joerg // move *__last, known to be true
3682 1.1 joerg *__first = _VSTD::move(*__i);
3683 1.1 joerg __i = ++__first;
3684 1.1 joerg // All trues now at start of range, all falses in buffer
3685 1.1 joerg // Move falses back into range, but don't mess up __first which points to first false
3686 1.1 joerg for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3687 1.1 joerg *__i = _VSTD::move(*__t2);
3688 1.1 joerg // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3689 1.1 joerg return __first;
3690 1.1 joerg }
3691 1.1 joerg // Else not enough buffer, do in place
3692 1.1 joerg // __len >= 4
3693 1.1 joerg _BidirectionalIterator __m = __first;
3694 1.1 joerg _Distance __len2 = __len / 2; // __len2 >= 2
3695 1.1 joerg _VSTD::advance(__m, __len2);
3696 1.1 joerg // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3697 1.1 joerg // F????????????????T
3698 1.1 joerg // f m l
3699 1.1 joerg _BidirectionalIterator __m1 = __m;
3700 1.1 joerg _BidirectionalIterator __first_false = __first;
3701 1.1 joerg _Distance __len_half = __len2;
3702 1.1 joerg while (!__pred(*--__m1))
3703 1.1 joerg {
3704 1.1 joerg if (__m1 == __first)
3705 1.1 joerg goto __first_half_done;
3706 1.1 joerg --__len_half;
3707 1.1 joerg }
3708 1.1 joerg // F???TFFF?????????T
3709 1.1 joerg // f m1 m l
3710 1.1 joerg typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3711 1.1 joerg __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3712 1.1 joerg __first_half_done:
3713 1.1 joerg // TTTFFFFF?????????T
3714 1.1 joerg // f ff m l
3715 1.1 joerg // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3716 1.1 joerg __m1 = __m;
3717 1.1 joerg _BidirectionalIterator __second_false = __last;
3718 1.1 joerg ++__second_false;
3719 1.1 joerg __len_half = __len - __len2;
3720 1.1 joerg while (__pred(*__m1))
3721 1.1 joerg {
3722 1.1 joerg if (++__m1 == __last)
3723 1.1 joerg goto __second_half_done;
3724 1.1 joerg --__len_half;
3725 1.1 joerg }
3726 1.1 joerg // TTTFFFFFTTTF?????T
3727 1.1 joerg // f ff m m1 l
3728 1.1 joerg __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3729 1.1 joerg __second_half_done:
3730 1.1 joerg // TTTFFFFFTTTTTFFFFF
3731 1.1 joerg // f ff m sf l
3732 1.1 joerg return _VSTD::rotate(__first_false, __m, __second_false);
3733 1.1 joerg // TTTTTTTTFFFFFFFFFF
3734 1.1 joerg // |
3735 1.1 joerg }
3736 1.1 joerg
3737 1.1 joerg template <class _Predicate, class _BidirectionalIterator>
3738 1.1 joerg _BidirectionalIterator
3739 1.1 joerg __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3740 1.1 joerg bidirectional_iterator_tag)
3741 1.1 joerg {
3742 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3743 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3744 1.1 joerg const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3745 1.1 joerg // Either prove all true and return __first or point to first false
3746 1.1 joerg while (true)
3747 1.1 joerg {
3748 1.1 joerg if (__first == __last)
3749 1.1 joerg return __first;
3750 1.1 joerg if (!__pred(*__first))
3751 1.1 joerg break;
3752 1.1 joerg ++__first;
3753 1.1 joerg }
3754 1.1 joerg // __first points to first false, everything prior to __first is already set.
3755 1.1 joerg // Either prove [__first, __last) is all false and return __first, or point __last to last true
3756 1.1 joerg do
3757 1.1 joerg {
3758 1.1 joerg if (__first == --__last)
3759 1.1 joerg return __first;
3760 1.1 joerg } while (!__pred(*__last));
3761 1.1 joerg // We now have a reduced range [__first, __last]
3762 1.1 joerg // *__first is known to be false
3763 1.1 joerg // *__last is known to be true
3764 1.1 joerg // __len >= 2
3765 1.1 joerg difference_type __len = _VSTD::distance(__first, __last) + 1;
3766 1.1 joerg pair<value_type*, ptrdiff_t> __p(0, 0);
3767 1.1 joerg unique_ptr<value_type, __return_temporary_buffer> __h;
3768 1.1 joerg if (__len >= __alloc_limit)
3769 1.1 joerg {
3770 1.1 joerg __p = _VSTD::get_temporary_buffer<value_type>(__len);
3771 1.1 joerg __h.reset(__p.first);
3772 1.1 joerg }
3773 1.1 joerg return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3774 1.1 joerg (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3775 1.1 joerg }
3776 1.1 joerg
3777 1.1 joerg template <class _ForwardIterator, class _Predicate>
3778 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
3779 1.1 joerg _ForwardIterator
3780 1.1 joerg stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3781 1.1 joerg {
3782 1.1 joerg return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3783 1.1 joerg (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3784 1.1 joerg }
3785 1.1 joerg
3786 1.1 joerg // is_sorted_until
3787 1.1 joerg
3788 1.1 joerg template <class _ForwardIterator, class _Compare>
3789 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3790 1.1 joerg is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3791 1.1 joerg {
3792 1.1 joerg if (__first != __last)
3793 1.1 joerg {
3794 1.1 joerg _ForwardIterator __i = __first;
3795 1.1 joerg while (++__i != __last)
3796 1.1 joerg {
3797 1.1 joerg if (__comp(*__i, *__first))
3798 1.1 joerg return __i;
3799 1.1 joerg __first = __i;
3800 1.1 joerg }
3801 1.1 joerg }
3802 1.1 joerg return __last;
3803 1.1 joerg }
3804 1.1 joerg
3805 1.1 joerg template<class _ForwardIterator>
3806 1.1 joerg _LIBCPP_NODISCARD_EXT inline
3807 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3808 1.1 joerg _ForwardIterator
3809 1.1 joerg is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3810 1.1 joerg {
3811 1.1 joerg return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3812 1.1 joerg }
3813 1.1 joerg
3814 1.1 joerg // is_sorted
3815 1.1 joerg
3816 1.1 joerg template <class _ForwardIterator, class _Compare>
3817 1.1 joerg _LIBCPP_NODISCARD_EXT inline
3818 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3819 1.1 joerg bool
3820 1.1 joerg is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3821 1.1 joerg {
3822 1.1 joerg return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3823 1.1 joerg }
3824 1.1 joerg
3825 1.1 joerg template<class _ForwardIterator>
3826 1.1 joerg _LIBCPP_NODISCARD_EXT inline
3827 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3828 1.1 joerg bool
3829 1.1 joerg is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3830 1.1 joerg {
3831 1.1 joerg return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3832 1.1 joerg }
3833 1.1 joerg
3834 1.1 joerg // sort
3835 1.1 joerg
3836 1.1 joerg // stable, 2-3 compares, 0-2 swaps
3837 1.1 joerg
3838 1.1 joerg template <class _Compare, class _ForwardIterator>
3839 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 unsigned
3840 1.1 joerg __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3841 1.1 joerg {
3842 1.1 joerg unsigned __r = 0;
3843 1.1 joerg if (!__c(*__y, *__x)) // if x <= y
3844 1.1 joerg {
3845 1.1 joerg if (!__c(*__z, *__y)) // if y <= z
3846 1.1 joerg return __r; // x <= y && y <= z
3847 1.1 joerg // x <= y && y > z
3848 1.1 joerg swap(*__y, *__z); // x <= z && y < z
3849 1.1 joerg __r = 1;
3850 1.1 joerg if (__c(*__y, *__x)) // if x > y
3851 1.1 joerg {
3852 1.1 joerg swap(*__x, *__y); // x < y && y <= z
3853 1.1 joerg __r = 2;
3854 1.1 joerg }
3855 1.1 joerg return __r; // x <= y && y < z
3856 1.1 joerg }
3857 1.1 joerg if (__c(*__z, *__y)) // x > y, if y > z
3858 1.1 joerg {
3859 1.1 joerg swap(*__x, *__z); // x < y && y < z
3860 1.1 joerg __r = 1;
3861 1.1 joerg return __r;
3862 1.1 joerg }
3863 1.1 joerg swap(*__x, *__y); // x > y && y <= z
3864 1.1 joerg __r = 1; // x < y && x <= z
3865 1.1 joerg if (__c(*__z, *__y)) // if y > z
3866 1.1 joerg {
3867 1.1 joerg swap(*__y, *__z); // x <= y && y < z
3868 1.1 joerg __r = 2;
3869 1.1 joerg }
3870 1.1 joerg return __r;
3871 1.1 joerg } // x <= y && y <= z
3872 1.1 joerg
3873 1.1 joerg // stable, 3-6 compares, 0-5 swaps
3874 1.1 joerg
3875 1.1 joerg template <class _Compare, class _ForwardIterator>
3876 1.1 joerg unsigned
3877 1.1 joerg __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3878 1.1 joerg _ForwardIterator __x4, _Compare __c)
3879 1.1 joerg {
3880 1.1 joerg unsigned __r = _VSTD::__sort3<_Compare>(__x1, __x2, __x3, __c);
3881 1.1 joerg if (__c(*__x4, *__x3))
3882 1.1 joerg {
3883 1.1 joerg swap(*__x3, *__x4);
3884 1.1 joerg ++__r;
3885 1.1 joerg if (__c(*__x3, *__x2))
3886 1.1 joerg {
3887 1.1 joerg swap(*__x2, *__x3);
3888 1.1 joerg ++__r;
3889 1.1 joerg if (__c(*__x2, *__x1))
3890 1.1 joerg {
3891 1.1 joerg swap(*__x1, *__x2);
3892 1.1 joerg ++__r;
3893 1.1 joerg }
3894 1.1 joerg }
3895 1.1 joerg }
3896 1.1 joerg return __r;
3897 1.1 joerg }
3898 1.1 joerg
3899 1.1 joerg // stable, 4-10 compares, 0-9 swaps
3900 1.1 joerg
3901 1.1 joerg template <class _Compare, class _ForwardIterator>
3902 1.1 joerg _LIBCPP_HIDDEN
3903 1.1 joerg unsigned
3904 1.1 joerg __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3905 1.1 joerg _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3906 1.1 joerg {
3907 1.1 joerg unsigned __r = _VSTD::__sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3908 1.1 joerg if (__c(*__x5, *__x4))
3909 1.1 joerg {
3910 1.1 joerg swap(*__x4, *__x5);
3911 1.1 joerg ++__r;
3912 1.1 joerg if (__c(*__x4, *__x3))
3913 1.1 joerg {
3914 1.1 joerg swap(*__x3, *__x4);
3915 1.1 joerg ++__r;
3916 1.1 joerg if (__c(*__x3, *__x2))
3917 1.1 joerg {
3918 1.1 joerg swap(*__x2, *__x3);
3919 1.1 joerg ++__r;
3920 1.1 joerg if (__c(*__x2, *__x1))
3921 1.1 joerg {
3922 1.1 joerg swap(*__x1, *__x2);
3923 1.1 joerg ++__r;
3924 1.1 joerg }
3925 1.1 joerg }
3926 1.1 joerg }
3927 1.1 joerg }
3928 1.1 joerg return __r;
3929 1.1 joerg }
3930 1.1 joerg
3931 1.1 joerg // Assumes size > 0
3932 1.1 joerg template <class _Compare, class _BidirectionalIterator>
3933 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 void
3934 1.1 joerg __selection_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
3935 1.1 joerg {
3936 1.1 joerg _BidirectionalIterator __lm1 = __last;
3937 1.1 joerg for (--__lm1; __first != __lm1; ++__first)
3938 1.1 joerg {
3939 1.1 joerg _BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator,
3940 1.1 joerg typename add_lvalue_reference<_Compare>::type>
3941 1.1 joerg (__first, __last, __comp);
3942 1.1 joerg if (__i != __first)
3943 1.1 joerg swap(*__first, *__i);
3944 1.1 joerg }
3945 1.1 joerg }
3946 1.1 joerg
3947 1.1 joerg template <class _Compare, class _BidirectionalIterator>
3948 1.1 joerg void
3949 1.1 joerg __insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
3950 1.1 joerg {
3951 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3952 1.1 joerg if (__first != __last)
3953 1.1 joerg {
3954 1.1 joerg _BidirectionalIterator __i = __first;
3955 1.1 joerg for (++__i; __i != __last; ++__i)
3956 1.1 joerg {
3957 1.1 joerg _BidirectionalIterator __j = __i;
3958 1.1 joerg value_type __t(_VSTD::move(*__j));
3959 1.1 joerg for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3960 1.1 joerg *__j = _VSTD::move(*__k);
3961 1.1 joerg *__j = _VSTD::move(__t);
3962 1.1 joerg }
3963 1.1 joerg }
3964 1.1 joerg }
3965 1.1 joerg
3966 1.1 joerg template <class _Compare, class _RandomAccessIterator>
3967 1.1 joerg void
3968 1.1 joerg __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3969 1.1 joerg {
3970 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3971 1.1 joerg _RandomAccessIterator __j = __first+2;
3972 1.1 joerg _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
3973 1.1 joerg for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3974 1.1 joerg {
3975 1.1 joerg if (__comp(*__i, *__j))
3976 1.1 joerg {
3977 1.1 joerg value_type __t(_VSTD::move(*__i));
3978 1.1 joerg _RandomAccessIterator __k = __j;
3979 1.1 joerg __j = __i;
3980 1.1 joerg do
3981 1.1 joerg {
3982 1.1 joerg *__j = _VSTD::move(*__k);
3983 1.1 joerg __j = __k;
3984 1.1 joerg } while (__j != __first && __comp(__t, *--__k));
3985 1.1 joerg *__j = _VSTD::move(__t);
3986 1.1 joerg }
3987 1.1 joerg __j = __i;
3988 1.1 joerg }
3989 1.1 joerg }
3990 1.1 joerg
3991 1.1 joerg template <class _Compare, class _RandomAccessIterator>
3992 1.1 joerg bool
3993 1.1 joerg __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3994 1.1 joerg {
3995 1.1 joerg switch (__last - __first)
3996 1.1 joerg {
3997 1.1 joerg case 0:
3998 1.1 joerg case 1:
3999 1.1 joerg return true;
4000 1.1 joerg case 2:
4001 1.1 joerg if (__comp(*--__last, *__first))
4002 1.1 joerg swap(*__first, *__last);
4003 1.1 joerg return true;
4004 1.1 joerg case 3:
4005 1.1 joerg _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
4006 1.1 joerg return true;
4007 1.1 joerg case 4:
4008 1.1 joerg _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
4009 1.1 joerg return true;
4010 1.1 joerg case 5:
4011 1.1 joerg _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
4012 1.1 joerg return true;
4013 1.1 joerg }
4014 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4015 1.1 joerg _RandomAccessIterator __j = __first+2;
4016 1.1 joerg _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
4017 1.1 joerg const unsigned __limit = 8;
4018 1.1 joerg unsigned __count = 0;
4019 1.1 joerg for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
4020 1.1 joerg {
4021 1.1 joerg if (__comp(*__i, *__j))
4022 1.1 joerg {
4023 1.1 joerg value_type __t(_VSTD::move(*__i));
4024 1.1 joerg _RandomAccessIterator __k = __j;
4025 1.1 joerg __j = __i;
4026 1.1 joerg do
4027 1.1 joerg {
4028 1.1 joerg *__j = _VSTD::move(*__k);
4029 1.1 joerg __j = __k;
4030 1.1 joerg } while (__j != __first && __comp(__t, *--__k));
4031 1.1 joerg *__j = _VSTD::move(__t);
4032 1.1 joerg if (++__count == __limit)
4033 1.1 joerg return ++__i == __last;
4034 1.1 joerg }
4035 1.1 joerg __j = __i;
4036 1.1 joerg }
4037 1.1 joerg return true;
4038 1.1 joerg }
4039 1.1 joerg
4040 1.1 joerg template <class _Compare, class _BidirectionalIterator>
4041 1.1 joerg void
4042 1.1 joerg __insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterator __last1,
4043 1.1 joerg typename iterator_traits<_BidirectionalIterator>::value_type* __first2, _Compare __comp)
4044 1.1 joerg {
4045 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4046 1.1 joerg if (__first1 != __last1)
4047 1.1 joerg {
4048 1.1 joerg __destruct_n __d(0);
4049 1.1 joerg unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
4050 1.1 joerg value_type* __last2 = __first2;
4051 1.1 joerg ::new ((void*)__last2) value_type(_VSTD::move(*__first1));
4052 1.1 joerg __d.template __incr<value_type>();
4053 1.1 joerg for (++__last2; ++__first1 != __last1; ++__last2)
4054 1.1 joerg {
4055 1.1 joerg value_type* __j2 = __last2;
4056 1.1 joerg value_type* __i2 = __j2;
4057 1.1 joerg if (__comp(*__first1, *--__i2))
4058 1.1 joerg {
4059 1.1 joerg ::new ((void*)__j2) value_type(_VSTD::move(*__i2));
4060 1.1 joerg __d.template __incr<value_type>();
4061 1.1 joerg for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
4062 1.1 joerg *__j2 = _VSTD::move(*__i2);
4063 1.1 joerg *__j2 = _VSTD::move(*__first1);
4064 1.1 joerg }
4065 1.1 joerg else
4066 1.1 joerg {
4067 1.1 joerg ::new ((void*)__j2) value_type(_VSTD::move(*__first1));
4068 1.1 joerg __d.template __incr<value_type>();
4069 1.1 joerg }
4070 1.1 joerg }
4071 1.1 joerg __h.release();
4072 1.1 joerg }
4073 1.1 joerg }
4074 1.1 joerg
4075 1.1 joerg template <class _Compare, class _RandomAccessIterator>
4076 1.1 joerg void
4077 1.1 joerg __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4078 1.1 joerg {
4079 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4080 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4081 1.1 joerg const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4082 1.1 joerg is_trivially_copy_assignable<value_type>::value ? 30 : 6;
4083 1.1 joerg while (true)
4084 1.1 joerg {
4085 1.1 joerg __restart:
4086 1.1 joerg difference_type __len = __last - __first;
4087 1.1 joerg switch (__len)
4088 1.1 joerg {
4089 1.1 joerg case 0:
4090 1.1 joerg case 1:
4091 1.1 joerg return;
4092 1.1 joerg case 2:
4093 1.1 joerg if (__comp(*--__last, *__first))
4094 1.1 joerg swap(*__first, *__last);
4095 1.1 joerg return;
4096 1.1 joerg case 3:
4097 1.1 joerg _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
4098 1.1 joerg return;
4099 1.1 joerg case 4:
4100 1.1 joerg _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
4101 1.1 joerg return;
4102 1.1 joerg case 5:
4103 1.1 joerg _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
4104 1.1 joerg return;
4105 1.1 joerg }
4106 1.1 joerg if (__len <= __limit)
4107 1.1 joerg {
4108 1.1 joerg _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
4109 1.1 joerg return;
4110 1.1 joerg }
4111 1.1 joerg // __len > 5
4112 1.1 joerg _RandomAccessIterator __m = __first;
4113 1.1 joerg _RandomAccessIterator __lm1 = __last;
4114 1.1 joerg --__lm1;
4115 1.1 joerg unsigned __n_swaps;
4116 1.1 joerg {
4117 1.1 joerg difference_type __delta;
4118 1.1 joerg if (__len >= 1000)
4119 1.1 joerg {
4120 1.1 joerg __delta = __len/2;
4121 1.1 joerg __m += __delta;
4122 1.1 joerg __delta /= 2;
4123 1.1 joerg __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
4124 1.1 joerg }
4125 1.1 joerg else
4126 1.1 joerg {
4127 1.1 joerg __delta = __len/2;
4128 1.1 joerg __m += __delta;
4129 1.1 joerg __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
4130 1.1 joerg }
4131 1.1 joerg }
4132 1.1 joerg // *__m is median
4133 1.1 joerg // partition [__first, __m) < *__m and *__m <= [__m, __last)
4134 1.1 joerg // (this inhibits tossing elements equivalent to __m around unnecessarily)
4135 1.1 joerg _RandomAccessIterator __i = __first;
4136 1.1 joerg _RandomAccessIterator __j = __lm1;
4137 1.1 joerg // j points beyond range to be tested, *__m is known to be <= *__lm1
4138 1.1 joerg // The search going up is known to be guarded but the search coming down isn't.
4139 1.1 joerg // Prime the downward search with a guard.
4140 1.1 joerg if (!__comp(*__i, *__m)) // if *__first == *__m
4141 1.1 joerg {
4142 1.1 joerg // *__first == *__m, *__first doesn't go in first part
4143 1.1 joerg // manually guard downward moving __j against __i
4144 1.1 joerg while (true)
4145 1.1 joerg {
4146 1.1 joerg if (__i == --__j)
4147 1.1 joerg {
4148 1.1 joerg // *__first == *__m, *__m <= all other elements
4149 1.1 joerg // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4150 1.1 joerg ++__i; // __first + 1
4151 1.1 joerg __j = __last;
4152 1.1 joerg if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4153 1.1 joerg {
4154 1.1 joerg while (true)
4155 1.1 joerg {
4156 1.1 joerg if (__i == __j)
4157 1.1 joerg return; // [__first, __last) all equivalent elements
4158 1.1 joerg if (__comp(*__first, *__i))
4159 1.1 joerg {
4160 1.1 joerg swap(*__i, *__j);
4161 1.1 joerg ++__n_swaps;
4162 1.1 joerg ++__i;
4163 1.1 joerg break;
4164 1.1 joerg }
4165 1.1 joerg ++__i;
4166 1.1 joerg }
4167 1.1 joerg }
4168 1.1 joerg // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4169 1.1 joerg if (__i == __j)
4170 1.1 joerg return;
4171 1.1 joerg while (true)
4172 1.1 joerg {
4173 1.1 joerg while (!__comp(*__first, *__i))
4174 1.1 joerg ++__i;
4175 1.1 joerg while (__comp(*__first, *--__j))
4176 1.1 joerg ;
4177 1.1 joerg if (__i >= __j)
4178 1.1 joerg break;
4179 1.1 joerg swap(*__i, *__j);
4180 1.1 joerg ++__n_swaps;
4181 1.1 joerg ++__i;
4182 1.1 joerg }
4183 1.1 joerg // [__first, __i) == *__first and *__first < [__i, __last)
4184 1.1 joerg // The first part is sorted, sort the second part
4185 1.1 joerg // _VSTD::__sort<_Compare>(__i, __last, __comp);
4186 1.1 joerg __first = __i;
4187 1.1 joerg goto __restart;
4188 1.1 joerg }
4189 1.1 joerg if (__comp(*__j, *__m))
4190 1.1 joerg {
4191 1.1 joerg swap(*__i, *__j);
4192 1.1 joerg ++__n_swaps;
4193 1.1 joerg break; // found guard for downward moving __j, now use unguarded partition
4194 1.1 joerg }
4195 1.1 joerg }
4196 1.1 joerg }
4197 1.1 joerg // It is known that *__i < *__m
4198 1.1 joerg ++__i;
4199 1.1 joerg // j points beyond range to be tested, *__m is known to be <= *__lm1
4200 1.1 joerg // if not yet partitioned...
4201 1.1 joerg if (__i < __j)
4202 1.1 joerg {
4203 1.1 joerg // known that *(__i - 1) < *__m
4204 1.1 joerg // known that __i <= __m
4205 1.1 joerg while (true)
4206 1.1 joerg {
4207 1.1 joerg // __m still guards upward moving __i
4208 1.1 joerg while (__comp(*__i, *__m))
4209 1.1 joerg ++__i;
4210 1.1 joerg // It is now known that a guard exists for downward moving __j
4211 1.1 joerg while (!__comp(*--__j, *__m))
4212 1.1 joerg ;
4213 1.1 joerg if (__i > __j)
4214 1.1 joerg break;
4215 1.1 joerg swap(*__i, *__j);
4216 1.1 joerg ++__n_swaps;
4217 1.1 joerg // It is known that __m != __j
4218 1.1 joerg // If __m just moved, follow it
4219 1.1 joerg if (__m == __i)
4220 1.1 joerg __m = __j;
4221 1.1 joerg ++__i;
4222 1.1 joerg }
4223 1.1 joerg }
4224 1.1 joerg // [__first, __i) < *__m and *__m <= [__i, __last)
4225 1.1 joerg if (__i != __m && __comp(*__m, *__i))
4226 1.1 joerg {
4227 1.1 joerg swap(*__i, *__m);
4228 1.1 joerg ++__n_swaps;
4229 1.1 joerg }
4230 1.1 joerg // [__first, __i) < *__i and *__i <= [__i+1, __last)
4231 1.1 joerg // If we were given a perfect partition, see if insertion sort is quick...
4232 1.1 joerg if (__n_swaps == 0)
4233 1.1 joerg {
4234 1.1 joerg bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4235 1.1 joerg if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4236 1.1 joerg {
4237 1.1 joerg if (__fs)
4238 1.1 joerg return;
4239 1.1 joerg __last = __i;
4240 1.1 joerg continue;
4241 1.1 joerg }
4242 1.1 joerg else
4243 1.1 joerg {
4244 1.1 joerg if (__fs)
4245 1.1 joerg {
4246 1.1 joerg __first = ++__i;
4247 1.1 joerg continue;
4248 1.1 joerg }
4249 1.1 joerg }
4250 1.1 joerg }
4251 1.1 joerg // sort smaller range with recursive call and larger with tail recursion elimination
4252 1.1 joerg if (__i - __first < __last - __i)
4253 1.1 joerg {
4254 1.1 joerg _VSTD::__sort<_Compare>(__first, __i, __comp);
4255 1.1 joerg // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4256 1.1 joerg __first = ++__i;
4257 1.1 joerg }
4258 1.1 joerg else
4259 1.1 joerg {
4260 1.1 joerg _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4261 1.1 joerg // _VSTD::__sort<_Compare>(__first, __i, __comp);
4262 1.1 joerg __last = __i;
4263 1.1 joerg }
4264 1.1 joerg }
4265 1.1 joerg }
4266 1.1 joerg
4267 1.1 joerg template <class _Compare, class _Tp>
4268 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
4269 1.1 joerg void
4270 1.1 joerg __sort(_Tp** __first, _Tp** __last, __less<_Tp*>&)
4271 1.1 joerg {
4272 1.1 joerg __less<uintptr_t> __comp;
4273 1.1 joerg _VSTD::__sort<__less<uintptr_t>&, uintptr_t*>((uintptr_t*)__first, (uintptr_t*)__last, __comp);
4274 1.1 joerg }
4275 1.1 joerg
4276 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4277 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4278 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4279 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4280 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4281 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4282 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4283 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4284 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4285 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4286 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4287 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4288 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4289 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4290 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4291 1.1 joerg
4292 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4293 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4294 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4295 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4296 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4297 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4298 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4299 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4300 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4301 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4302 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4303 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4304 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4305 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4306 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4307 1.1 joerg
4308 1.1 joerg _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
4309 1.1 joerg
4310 1.1 joerg // lower_bound
4311 1.1 joerg
4312 1.1 joerg template <class _Compare, class _ForwardIterator, class _Tp>
4313 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4314 1.1 joerg __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4315 1.1 joerg {
4316 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4317 1.1 joerg difference_type __len = _VSTD::distance(__first, __last);
4318 1.1 joerg while (__len != 0)
4319 1.1 joerg {
4320 1.1 joerg difference_type __l2 = _VSTD::__half_positive(__len);
4321 1.1 joerg _ForwardIterator __m = __first;
4322 1.1 joerg _VSTD::advance(__m, __l2);
4323 1.1 joerg if (__comp(*__m, __value_))
4324 1.1 joerg {
4325 1.1 joerg __first = ++__m;
4326 1.1 joerg __len -= __l2 + 1;
4327 1.1 joerg }
4328 1.1 joerg else
4329 1.1 joerg __len = __l2;
4330 1.1 joerg }
4331 1.1 joerg return __first;
4332 1.1 joerg }
4333 1.1 joerg
4334 1.1 joerg template <class _ForwardIterator, class _Tp, class _Compare>
4335 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4336 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4337 1.1 joerg _ForwardIterator
4338 1.1 joerg lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4339 1.1 joerg {
4340 1.1 joerg typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4341 1.1 joerg return _VSTD::__lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4342 1.1 joerg }
4343 1.1 joerg
4344 1.1 joerg template <class _ForwardIterator, class _Tp>
4345 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4346 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4347 1.1 joerg _ForwardIterator
4348 1.1 joerg lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4349 1.1 joerg {
4350 1.1 joerg return _VSTD::lower_bound(__first, __last, __value_,
4351 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4352 1.1 joerg }
4353 1.1 joerg
4354 1.1 joerg // upper_bound
4355 1.1 joerg
4356 1.1 joerg template <class _Compare, class _ForwardIterator, class _Tp>
4357 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4358 1.1 joerg __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4359 1.1 joerg {
4360 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4361 1.1 joerg difference_type __len = _VSTD::distance(__first, __last);
4362 1.1 joerg while (__len != 0)
4363 1.1 joerg {
4364 1.1 joerg difference_type __l2 = _VSTD::__half_positive(__len);
4365 1.1 joerg _ForwardIterator __m = __first;
4366 1.1 joerg _VSTD::advance(__m, __l2);
4367 1.1 joerg if (__comp(__value_, *__m))
4368 1.1 joerg __len = __l2;
4369 1.1 joerg else
4370 1.1 joerg {
4371 1.1 joerg __first = ++__m;
4372 1.1 joerg __len -= __l2 + 1;
4373 1.1 joerg }
4374 1.1 joerg }
4375 1.1 joerg return __first;
4376 1.1 joerg }
4377 1.1 joerg
4378 1.1 joerg template <class _ForwardIterator, class _Tp, class _Compare>
4379 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4380 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4381 1.1 joerg _ForwardIterator
4382 1.1 joerg upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4383 1.1 joerg {
4384 1.1 joerg typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4385 1.1 joerg return _VSTD::__upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4386 1.1 joerg }
4387 1.1 joerg
4388 1.1 joerg template <class _ForwardIterator, class _Tp>
4389 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4390 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4391 1.1 joerg _ForwardIterator
4392 1.1 joerg upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4393 1.1 joerg {
4394 1.1 joerg return _VSTD::upper_bound(__first, __last, __value_,
4395 1.1 joerg __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4396 1.1 joerg }
4397 1.1 joerg
4398 1.1 joerg // equal_range
4399 1.1 joerg
4400 1.1 joerg template <class _Compare, class _ForwardIterator, class _Tp>
4401 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
4402 1.1 joerg __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4403 1.1 joerg {
4404 1.1 joerg typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4405 1.1 joerg difference_type __len = _VSTD::distance(__first, __last);
4406 1.1 joerg while (__len != 0)
4407 1.1 joerg {
4408 1.1 joerg difference_type __l2 = _VSTD::__half_positive(__len);
4409 1.1 joerg _ForwardIterator __m = __first;
4410 1.1 joerg _VSTD::advance(__m, __l2);
4411 1.1 joerg if (__comp(*__m, __value_))
4412 1.1 joerg {
4413 1.1 joerg __first = ++__m;
4414 1.1 joerg __len -= __l2 + 1;
4415 1.1 joerg }
4416 1.1 joerg else if (__comp(__value_, *__m))
4417 1.1 joerg {
4418 1.1 joerg __last = __m;
4419 1.1 joerg __len = __l2;
4420 1.1 joerg }
4421 1.1 joerg else
4422 1.1 joerg {
4423 1.1 joerg _ForwardIterator __mp1 = __m;
4424 1.1 joerg return pair<_ForwardIterator, _ForwardIterator>
4425 1.1 joerg (
4426 1.1 joerg _VSTD::__lower_bound<_Compare>(__first, __m, __value_, __comp),
4427 1.1 joerg _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4428 1.1 joerg );
4429 1.1 joerg }
4430 1.1 joerg }
4431 1.1 joerg return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4432 1.1 joerg }
4433 1.1 joerg
4434 1.1 joerg template <class _ForwardIterator, class _Tp, class _Compare>
4435 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4436 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4437 1.1 joerg pair<_ForwardIterator, _ForwardIterator>
4438 1.1 joerg equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4439 1.1 joerg {
4440 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4441 1.1 joerg return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4442 1.1 joerg }
4443 1.1 joerg
4444 1.1 joerg template <class _ForwardIterator, class _Tp>
4445 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4446 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4447 1.1 joerg pair<_ForwardIterator, _ForwardIterator>
4448 1.1 joerg equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4449 1.1 joerg {
4450 1.1 joerg return _VSTD::equal_range(__first, __last, __value_,
4451 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4452 1.1 joerg }
4453 1.1 joerg
4454 1.1 joerg // binary_search
4455 1.1 joerg
4456 1.1 joerg template <class _Compare, class _ForwardIterator, class _Tp>
4457 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4458 1.1 joerg bool
4459 1.1 joerg __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4460 1.1 joerg {
4461 1.1 joerg __first = _VSTD::__lower_bound<_Compare>(__first, __last, __value_, __comp);
4462 1.1 joerg return __first != __last && !__comp(__value_, *__first);
4463 1.1 joerg }
4464 1.1 joerg
4465 1.1 joerg template <class _ForwardIterator, class _Tp, class _Compare>
4466 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4467 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4468 1.1 joerg bool
4469 1.1 joerg binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4470 1.1 joerg {
4471 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4472 1.1 joerg return _VSTD::__binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4473 1.1 joerg }
4474 1.1 joerg
4475 1.1 joerg template <class _ForwardIterator, class _Tp>
4476 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4477 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4478 1.1 joerg bool
4479 1.1 joerg binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4480 1.1 joerg {
4481 1.1 joerg return _VSTD::binary_search(__first, __last, __value_,
4482 1.1 joerg __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4483 1.1 joerg }
4484 1.1 joerg
4485 1.1 joerg // merge
4486 1.1 joerg
4487 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4488 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17
4489 1.1 joerg _OutputIterator
4490 1.1 joerg __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4491 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4492 1.1 joerg {
4493 1.1 joerg for (; __first1 != __last1; ++__result)
4494 1.1 joerg {
4495 1.1 joerg if (__first2 == __last2)
4496 1.1 joerg return _VSTD::copy(__first1, __last1, __result);
4497 1.1 joerg if (__comp(*__first2, *__first1))
4498 1.1 joerg {
4499 1.1 joerg *__result = *__first2;
4500 1.1 joerg ++__first2;
4501 1.1 joerg }
4502 1.1 joerg else
4503 1.1 joerg {
4504 1.1 joerg *__result = *__first1;
4505 1.1 joerg ++__first1;
4506 1.1 joerg }
4507 1.1 joerg }
4508 1.1 joerg return _VSTD::copy(__first2, __last2, __result);
4509 1.1 joerg }
4510 1.1 joerg
4511 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4512 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4513 1.1 joerg _OutputIterator
4514 1.1 joerg merge(_InputIterator1 __first1, _InputIterator1 __last1,
4515 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4516 1.1 joerg {
4517 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4518 1.1 joerg return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4519 1.1 joerg }
4520 1.1 joerg
4521 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4522 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4523 1.1 joerg _OutputIterator
4524 1.1 joerg merge(_InputIterator1 __first1, _InputIterator1 __last1,
4525 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4526 1.1 joerg {
4527 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4528 1.1 joerg typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4529 1.1 joerg return _VSTD::merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4530 1.1 joerg }
4531 1.1 joerg
4532 1.1 joerg // inplace_merge
4533 1.1 joerg
4534 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2,
4535 1.1 joerg class _OutputIterator>
4536 1.1 joerg void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4537 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2,
4538 1.1 joerg _OutputIterator __result, _Compare __comp)
4539 1.1 joerg {
4540 1.1 joerg for (; __first1 != __last1; ++__result)
4541 1.1 joerg {
4542 1.1 joerg if (__first2 == __last2)
4543 1.1 joerg {
4544 1.1 joerg _VSTD::move(__first1, __last1, __result);
4545 1.1 joerg return;
4546 1.1 joerg }
4547 1.1 joerg
4548 1.1 joerg if (__comp(*__first2, *__first1))
4549 1.1 joerg {
4550 1.1 joerg *__result = _VSTD::move(*__first2);
4551 1.1 joerg ++__first2;
4552 1.1 joerg }
4553 1.1 joerg else
4554 1.1 joerg {
4555 1.1 joerg *__result = _VSTD::move(*__first1);
4556 1.1 joerg ++__first1;
4557 1.1 joerg }
4558 1.1 joerg }
4559 1.1 joerg // __first2 through __last2 are already in the right spot.
4560 1.1 joerg }
4561 1.1 joerg
4562 1.1 joerg template <class _Compare, class _BidirectionalIterator>
4563 1.1 joerg void
4564 1.1 joerg __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4565 1.1 joerg _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4566 1.1 joerg typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4567 1.1 joerg typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4568 1.1 joerg {
4569 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4570 1.1 joerg __destruct_n __d(0);
4571 1.1 joerg unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4572 1.1 joerg if (__len1 <= __len2)
4573 1.1 joerg {
4574 1.1 joerg value_type* __p = __buff;
4575 1.1 joerg for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
4576 1.1 joerg ::new ((void*)__p) value_type(_VSTD::move(*__i));
4577 1.1 joerg _VSTD::__half_inplace_merge<_Compare>(__buff, __p, __middle, __last, __first, __comp);
4578 1.1 joerg }
4579 1.1 joerg else
4580 1.1 joerg {
4581 1.1 joerg value_type* __p = __buff;
4582 1.1 joerg for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
4583 1.1 joerg ::new ((void*)__p) value_type(_VSTD::move(*__i));
4584 1.1 joerg typedef reverse_iterator<_BidirectionalIterator> _RBi;
4585 1.1 joerg typedef reverse_iterator<value_type*> _Rv;
4586 1.1 joerg typedef __invert<_Compare> _Inverted;
4587 1.1 joerg _VSTD::__half_inplace_merge<_Inverted>(_Rv(__p), _Rv(__buff),
4588 1.1 joerg _RBi(__middle), _RBi(__first),
4589 1.1 joerg _RBi(__last), _Inverted(__comp));
4590 1.1 joerg }
4591 1.1 joerg }
4592 1.1 joerg
4593 1.1 joerg template <class _Compare, class _BidirectionalIterator>
4594 1.1 joerg void
4595 1.1 joerg __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4596 1.1 joerg _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4597 1.1 joerg typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4598 1.1 joerg typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4599 1.1 joerg {
4600 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4601 1.1 joerg while (true)
4602 1.1 joerg {
4603 1.1 joerg // if __middle == __last, we're done
4604 1.1 joerg if (__len2 == 0)
4605 1.1 joerg return;
4606 1.1 joerg if (__len1 <= __buff_size || __len2 <= __buff_size)
4607 1.1 joerg return _VSTD::__buffered_inplace_merge<_Compare>
4608 1.1 joerg (__first, __middle, __last, __comp, __len1, __len2, __buff);
4609 1.1 joerg // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4610 1.1 joerg for (; true; ++__first, (void) --__len1)
4611 1.1 joerg {
4612 1.1 joerg if (__len1 == 0)
4613 1.1 joerg return;
4614 1.1 joerg if (__comp(*__middle, *__first))
4615 1.1 joerg break;
4616 1.1 joerg }
4617 1.1 joerg // __first < __middle < __last
4618 1.1 joerg // *__first > *__middle
4619 1.1 joerg // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4620 1.1 joerg // all elements in:
4621 1.1 joerg // [__first, __m1) <= [__middle, __m2)
4622 1.1 joerg // [__middle, __m2) < [__m1, __middle)
4623 1.1 joerg // [__m1, __middle) <= [__m2, __last)
4624 1.1 joerg // and __m1 or __m2 is in the middle of its range
4625 1.1 joerg _BidirectionalIterator __m1; // "median" of [__first, __middle)
4626 1.1 joerg _BidirectionalIterator __m2; // "median" of [__middle, __last)
4627 1.1 joerg difference_type __len11; // distance(__first, __m1)
4628 1.1 joerg difference_type __len21; // distance(__middle, __m2)
4629 1.1 joerg // binary search smaller range
4630 1.1 joerg if (__len1 < __len2)
4631 1.1 joerg { // __len >= 1, __len2 >= 2
4632 1.1 joerg __len21 = __len2 / 2;
4633 1.1 joerg __m2 = __middle;
4634 1.1 joerg _VSTD::advance(__m2, __len21);
4635 1.1 joerg __m1 = _VSTD::__upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4636 1.1 joerg __len11 = _VSTD::distance(__first, __m1);
4637 1.1 joerg }
4638 1.1 joerg else
4639 1.1 joerg {
4640 1.1 joerg if (__len1 == 1)
4641 1.1 joerg { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4642 1.1 joerg // It is known *__first > *__middle
4643 1.1 joerg swap(*__first, *__middle);
4644 1.1 joerg return;
4645 1.1 joerg }
4646 1.1 joerg // __len1 >= 2, __len2 >= 1
4647 1.1 joerg __len11 = __len1 / 2;
4648 1.1 joerg __m1 = __first;
4649 1.1 joerg _VSTD::advance(__m1, __len11);
4650 1.1 joerg __m2 = _VSTD::__lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4651 1.1 joerg __len21 = _VSTD::distance(__middle, __m2);
4652 1.1 joerg }
4653 1.1 joerg difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4654 1.1 joerg difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4655 1.1 joerg // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4656 1.1 joerg // swap middle two partitions
4657 1.1 joerg __middle = _VSTD::rotate(__m1, __middle, __m2);
4658 1.1 joerg // __len12 and __len21 now have swapped meanings
4659 1.1 joerg // merge smaller range with recursive call and larger with tail recursion elimination
4660 1.1 joerg if (__len11 + __len21 < __len12 + __len22)
4661 1.1 joerg {
4662 1.1 joerg _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4663 1.1 joerg // _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4664 1.1 joerg __first = __middle;
4665 1.1 joerg __middle = __m2;
4666 1.1 joerg __len1 = __len12;
4667 1.1 joerg __len2 = __len22;
4668 1.1 joerg }
4669 1.1 joerg else
4670 1.1 joerg {
4671 1.1 joerg _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4672 1.1 joerg // _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4673 1.1 joerg __last = __middle;
4674 1.1 joerg __middle = __m1;
4675 1.1 joerg __len1 = __len11;
4676 1.1 joerg __len2 = __len21;
4677 1.1 joerg }
4678 1.1 joerg }
4679 1.1 joerg }
4680 1.1 joerg
4681 1.1 joerg template <class _BidirectionalIterator, class _Compare>
4682 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
4683 1.1 joerg void
4684 1.1 joerg inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4685 1.1 joerg _Compare __comp)
4686 1.1 joerg {
4687 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4688 1.1 joerg typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4689 1.1 joerg difference_type __len1 = _VSTD::distance(__first, __middle);
4690 1.1 joerg difference_type __len2 = _VSTD::distance(__middle, __last);
4691 1.1 joerg difference_type __buf_size = _VSTD::min(__len1, __len2);
4692 1.1 joerg pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4693 1.1 joerg unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4694 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4695 1.1 joerg return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4696 1.1 joerg __buf.first, __buf.second);
4697 1.1 joerg }
4698 1.1 joerg
4699 1.1 joerg template <class _BidirectionalIterator>
4700 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
4701 1.1 joerg void
4702 1.1 joerg inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4703 1.1 joerg {
4704 1.1 joerg _VSTD::inplace_merge(__first, __middle, __last,
4705 1.1 joerg __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4706 1.1 joerg }
4707 1.1 joerg
4708 1.1 joerg // stable_sort
4709 1.1 joerg
4710 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2>
4711 1.1 joerg void
4712 1.1 joerg __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4713 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2,
4714 1.1 joerg typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4715 1.1 joerg {
4716 1.1 joerg typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4717 1.1 joerg __destruct_n __d(0);
4718 1.1 joerg unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4719 1.1 joerg for (; true; ++__result)
4720 1.1 joerg {
4721 1.1 joerg if (__first1 == __last1)
4722 1.1 joerg {
4723 1.1 joerg for (; __first2 != __last2; ++__first2, ++__result, (void)__d.template __incr<value_type>())
4724 1.1 joerg ::new ((void*)__result) value_type(_VSTD::move(*__first2));
4725 1.1 joerg __h.release();
4726 1.1 joerg return;
4727 1.1 joerg }
4728 1.1 joerg if (__first2 == __last2)
4729 1.1 joerg {
4730 1.1 joerg for (; __first1 != __last1; ++__first1, ++__result, (void)__d.template __incr<value_type>())
4731 1.1 joerg ::new ((void*)__result) value_type(_VSTD::move(*__first1));
4732 1.1 joerg __h.release();
4733 1.1 joerg return;
4734 1.1 joerg }
4735 1.1 joerg if (__comp(*__first2, *__first1))
4736 1.1 joerg {
4737 1.1 joerg ::new ((void*)__result) value_type(_VSTD::move(*__first2));
4738 1.1 joerg __d.template __incr<value_type>();
4739 1.1 joerg ++__first2;
4740 1.1 joerg }
4741 1.1 joerg else
4742 1.1 joerg {
4743 1.1 joerg ::new ((void*)__result) value_type(_VSTD::move(*__first1));
4744 1.1 joerg __d.template __incr<value_type>();
4745 1.1 joerg ++__first1;
4746 1.1 joerg }
4747 1.1 joerg }
4748 1.1 joerg }
4749 1.1 joerg
4750 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4751 1.1 joerg void
4752 1.1 joerg __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4753 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2,
4754 1.1 joerg _OutputIterator __result, _Compare __comp)
4755 1.1 joerg {
4756 1.1 joerg for (; __first1 != __last1; ++__result)
4757 1.1 joerg {
4758 1.1 joerg if (__first2 == __last2)
4759 1.1 joerg {
4760 1.1 joerg for (; __first1 != __last1; ++__first1, (void) ++__result)
4761 1.1 joerg *__result = _VSTD::move(*__first1);
4762 1.1 joerg return;
4763 1.1 joerg }
4764 1.1 joerg if (__comp(*__first2, *__first1))
4765 1.1 joerg {
4766 1.1 joerg *__result = _VSTD::move(*__first2);
4767 1.1 joerg ++__first2;
4768 1.1 joerg }
4769 1.1 joerg else
4770 1.1 joerg {
4771 1.1 joerg *__result = _VSTD::move(*__first1);
4772 1.1 joerg ++__first1;
4773 1.1 joerg }
4774 1.1 joerg }
4775 1.1 joerg for (; __first2 != __last2; ++__first2, (void) ++__result)
4776 1.1 joerg *__result = _VSTD::move(*__first2);
4777 1.1 joerg }
4778 1.1 joerg
4779 1.1 joerg template <class _Compare, class _RandomAccessIterator>
4780 1.1 joerg void
4781 1.1 joerg __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4782 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4783 1.1 joerg typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4784 1.1 joerg
4785 1.1 joerg template <class _Compare, class _RandomAccessIterator>
4786 1.1 joerg void
4787 1.1 joerg __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4788 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4789 1.1 joerg typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4790 1.1 joerg {
4791 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4792 1.1 joerg switch (__len)
4793 1.1 joerg {
4794 1.1 joerg case 0:
4795 1.1 joerg return;
4796 1.1 joerg case 1:
4797 1.1 joerg ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4798 1.1 joerg return;
4799 1.1 joerg case 2:
4800 1.1 joerg __destruct_n __d(0);
4801 1.1 joerg unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4802 1.1 joerg if (__comp(*--__last1, *__first1))
4803 1.1 joerg {
4804 1.1 joerg ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
4805 1.1 joerg __d.template __incr<value_type>();
4806 1.1 joerg ++__first2;
4807 1.1 joerg ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4808 1.1 joerg }
4809 1.1 joerg else
4810 1.1 joerg {
4811 1.1 joerg ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4812 1.1 joerg __d.template __incr<value_type>();
4813 1.1 joerg ++__first2;
4814 1.1 joerg ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
4815 1.1 joerg }
4816 1.1 joerg __h2.release();
4817 1.1 joerg return;
4818 1.1 joerg }
4819 1.1 joerg if (__len <= 8)
4820 1.1 joerg {
4821 1.1 joerg _VSTD::__insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4822 1.1 joerg return;
4823 1.1 joerg }
4824 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4825 1.1 joerg _RandomAccessIterator __m = __first1 + __l2;
4826 1.1 joerg _VSTD::__stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4827 1.1 joerg _VSTD::__stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4828 1.1 joerg _VSTD::__merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4829 1.1 joerg }
4830 1.1 joerg
4831 1.1 joerg template <class _Tp>
4832 1.1 joerg struct __stable_sort_switch
4833 1.1 joerg {
4834 1.1 joerg static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4835 1.1 joerg };
4836 1.1 joerg
4837 1.1 joerg template <class _Compare, class _RandomAccessIterator>
4838 1.1 joerg void
4839 1.1 joerg __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4840 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4841 1.1 joerg typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4842 1.1 joerg {
4843 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4844 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4845 1.1 joerg switch (__len)
4846 1.1 joerg {
4847 1.1 joerg case 0:
4848 1.1 joerg case 1:
4849 1.1 joerg return;
4850 1.1 joerg case 2:
4851 1.1 joerg if (__comp(*--__last, *__first))
4852 1.1 joerg swap(*__first, *__last);
4853 1.1 joerg return;
4854 1.1 joerg }
4855 1.1 joerg if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4856 1.1 joerg {
4857 1.1 joerg _VSTD::__insertion_sort<_Compare>(__first, __last, __comp);
4858 1.1 joerg return;
4859 1.1 joerg }
4860 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4861 1.1 joerg _RandomAccessIterator __m = __first + __l2;
4862 1.1 joerg if (__len <= __buff_size)
4863 1.1 joerg {
4864 1.1 joerg __destruct_n __d(0);
4865 1.1 joerg unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4866 1.1 joerg _VSTD::__stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4867 1.1 joerg __d.__set(__l2, (value_type*)nullptr);
4868 1.1 joerg _VSTD::__stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4869 1.1 joerg __d.__set(__len, (value_type*)nullptr);
4870 1.1 joerg _VSTD::__merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4871 1.1 joerg // _VSTD::__merge<_Compare>(move_iterator<value_type*>(__buff),
4872 1.1 joerg // move_iterator<value_type*>(__buff + __l2),
4873 1.1 joerg // move_iterator<_RandomAccessIterator>(__buff + __l2),
4874 1.1 joerg // move_iterator<_RandomAccessIterator>(__buff + __len),
4875 1.1 joerg // __first, __comp);
4876 1.1 joerg return;
4877 1.1 joerg }
4878 1.1 joerg _VSTD::__stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4879 1.1 joerg _VSTD::__stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4880 1.1 joerg _VSTD::__inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4881 1.1 joerg }
4882 1.1 joerg
4883 1.1 joerg template <class _RandomAccessIterator, class _Compare>
4884 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
4885 1.1 joerg void
4886 1.1 joerg stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4887 1.1 joerg {
4888 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4889 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4890 1.1 joerg difference_type __len = __last - __first;
4891 1.1 joerg pair<value_type*, ptrdiff_t> __buf(0, 0);
4892 1.1 joerg unique_ptr<value_type, __return_temporary_buffer> __h;
4893 1.1 joerg if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4894 1.1 joerg {
4895 1.1 joerg __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4896 1.1 joerg __h.reset(__buf.first);
4897 1.1 joerg }
4898 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4899 1.1 joerg _VSTD::__stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4900 1.1 joerg }
4901 1.1 joerg
4902 1.1 joerg template <class _RandomAccessIterator>
4903 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY
4904 1.1 joerg void
4905 1.1 joerg stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4906 1.1 joerg {
4907 1.1 joerg _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4908 1.1 joerg }
4909 1.1 joerg
4910 1.1 joerg // is_heap_until
4911 1.1 joerg
4912 1.1 joerg template <class _RandomAccessIterator, class _Compare>
4913 1.1 joerg _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
4914 1.1 joerg is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4915 1.1 joerg {
4916 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4917 1.1 joerg difference_type __len = __last - __first;
4918 1.1 joerg difference_type __p = 0;
4919 1.1 joerg difference_type __c = 1;
4920 1.1 joerg _RandomAccessIterator __pp = __first;
4921 1.1 joerg while (__c < __len)
4922 1.1 joerg {
4923 1.1 joerg _RandomAccessIterator __cp = __first + __c;
4924 1.1 joerg if (__comp(*__pp, *__cp))
4925 1.1 joerg return __cp;
4926 1.1 joerg ++__c;
4927 1.1 joerg ++__cp;
4928 1.1 joerg if (__c == __len)
4929 1.1 joerg return __last;
4930 1.1 joerg if (__comp(*__pp, *__cp))
4931 1.1 joerg return __cp;
4932 1.1 joerg ++__p;
4933 1.1 joerg ++__pp;
4934 1.1 joerg __c = 2 * __p + 1;
4935 1.1 joerg }
4936 1.1 joerg return __last;
4937 1.1 joerg }
4938 1.1 joerg
4939 1.1 joerg template<class _RandomAccessIterator>
4940 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4941 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4942 1.1 joerg _RandomAccessIterator
4943 1.1 joerg is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4944 1.1 joerg {
4945 1.1 joerg return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4946 1.1 joerg }
4947 1.1 joerg
4948 1.1 joerg // is_heap
4949 1.1 joerg
4950 1.1 joerg template <class _RandomAccessIterator, class _Compare>
4951 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4952 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4953 1.1 joerg bool
4954 1.1 joerg is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4955 1.1 joerg {
4956 1.1 joerg return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4957 1.1 joerg }
4958 1.1 joerg
4959 1.1 joerg template<class _RandomAccessIterator>
4960 1.1 joerg _LIBCPP_NODISCARD_EXT inline
4961 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4962 1.1 joerg bool
4963 1.1 joerg is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4964 1.1 joerg {
4965 1.1 joerg return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4966 1.1 joerg }
4967 1.1 joerg
4968 1.1 joerg // push_heap
4969 1.1 joerg
4970 1.1 joerg template <class _Compare, class _RandomAccessIterator>
4971 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 void
4972 1.1 joerg __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4973 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4974 1.1 joerg {
4975 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4976 1.1 joerg if (__len > 1)
4977 1.1 joerg {
4978 1.1 joerg __len = (__len - 2) / 2;
4979 1.1 joerg _RandomAccessIterator __ptr = __first + __len;
4980 1.1 joerg if (__comp(*__ptr, *--__last))
4981 1.1 joerg {
4982 1.1 joerg value_type __t(_VSTD::move(*__last));
4983 1.1 joerg do
4984 1.1 joerg {
4985 1.1 joerg *__last = _VSTD::move(*__ptr);
4986 1.1 joerg __last = __ptr;
4987 1.1 joerg if (__len == 0)
4988 1.1 joerg break;
4989 1.1 joerg __len = (__len - 1) / 2;
4990 1.1 joerg __ptr = __first + __len;
4991 1.1 joerg } while (__comp(*__ptr, __t));
4992 1.1 joerg *__last = _VSTD::move(__t);
4993 1.1 joerg }
4994 1.1 joerg }
4995 1.1 joerg }
4996 1.1 joerg
4997 1.1 joerg template <class _RandomAccessIterator, class _Compare>
4998 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4999 1.1 joerg void
5000 1.1 joerg push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5001 1.1 joerg {
5002 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5003 1.1 joerg _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
5004 1.1 joerg }
5005 1.1 joerg
5006 1.1 joerg template <class _RandomAccessIterator>
5007 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5008 1.1 joerg void
5009 1.1 joerg push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5010 1.1 joerg {
5011 1.1 joerg _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5012 1.1 joerg }
5013 1.1 joerg
5014 1.1 joerg // pop_heap
5015 1.1 joerg
5016 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5017 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 void
5018 1.1 joerg __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
5019 1.1 joerg _Compare __comp,
5020 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5021 1.1 joerg _RandomAccessIterator __start)
5022 1.1 joerg {
5023 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5024 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5025 1.1 joerg // left-child of __start is at 2 * __start + 1
5026 1.1 joerg // right-child of __start is at 2 * __start + 2
5027 1.1 joerg difference_type __child = __start - __first;
5028 1.1 joerg
5029 1.1 joerg if (__len < 2 || (__len - 2) / 2 < __child)
5030 1.1 joerg return;
5031 1.1 joerg
5032 1.1 joerg __child = 2 * __child + 1;
5033 1.1 joerg _RandomAccessIterator __child_i = __first + __child;
5034 1.1 joerg
5035 1.1 joerg if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5036 1.1 joerg // right-child exists and is greater than left-child
5037 1.1 joerg ++__child_i;
5038 1.1 joerg ++__child;
5039 1.1 joerg }
5040 1.1 joerg
5041 1.1 joerg // check if we are in heap-order
5042 1.1 joerg if (__comp(*__child_i, *__start))
5043 1.1 joerg // we are, __start is larger than it's largest child
5044 1.1 joerg return;
5045 1.1 joerg
5046 1.1 joerg value_type __top(_VSTD::move(*__start));
5047 1.1 joerg do
5048 1.1 joerg {
5049 1.1 joerg // we are not in heap-order, swap the parent with its largest child
5050 1.1 joerg *__start = _VSTD::move(*__child_i);
5051 1.1 joerg __start = __child_i;
5052 1.1 joerg
5053 1.1 joerg if ((__len - 2) / 2 < __child)
5054 1.1 joerg break;
5055 1.1 joerg
5056 1.1 joerg // recompute the child based off of the updated parent
5057 1.1 joerg __child = 2 * __child + 1;
5058 1.1 joerg __child_i = __first + __child;
5059 1.1 joerg
5060 1.1 joerg if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5061 1.1 joerg // right-child exists and is greater than left-child
5062 1.1 joerg ++__child_i;
5063 1.1 joerg ++__child;
5064 1.1 joerg }
5065 1.1 joerg
5066 1.1 joerg // check if we are in heap-order
5067 1.1 joerg } while (!__comp(*__child_i, __top));
5068 1.1 joerg *__start = _VSTD::move(__top);
5069 1.1 joerg }
5070 1.1 joerg
5071 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5072 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5073 1.1 joerg void
5074 1.1 joerg __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5075 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5076 1.1 joerg {
5077 1.1 joerg if (__len > 1)
5078 1.1 joerg {
5079 1.1 joerg swap(*__first, *--__last);
5080 1.1 joerg _VSTD::__sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
5081 1.1 joerg }
5082 1.1 joerg }
5083 1.1 joerg
5084 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5085 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5086 1.1 joerg void
5087 1.1 joerg pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5088 1.1 joerg {
5089 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5090 1.1 joerg _VSTD::__pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
5091 1.1 joerg }
5092 1.1 joerg
5093 1.1 joerg template <class _RandomAccessIterator>
5094 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5095 1.1 joerg void
5096 1.1 joerg pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5097 1.1 joerg {
5098 1.1 joerg _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5099 1.1 joerg }
5100 1.1 joerg
5101 1.1 joerg // make_heap
5102 1.1 joerg
5103 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5104 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 void
5105 1.1 joerg __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5106 1.1 joerg {
5107 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5108 1.1 joerg difference_type __n = __last - __first;
5109 1.1 joerg if (__n > 1)
5110 1.1 joerg {
5111 1.1 joerg // start from the first parent, there is no need to consider children
5112 1.1 joerg for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5113 1.1 joerg {
5114 1.1 joerg _VSTD::__sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5115 1.1 joerg }
5116 1.1 joerg }
5117 1.1 joerg }
5118 1.1 joerg
5119 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5120 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5121 1.1 joerg void
5122 1.1 joerg make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5123 1.1 joerg {
5124 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5125 1.1 joerg _VSTD::__make_heap<_Comp_ref>(__first, __last, __comp);
5126 1.1 joerg }
5127 1.1 joerg
5128 1.1 joerg template <class _RandomAccessIterator>
5129 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5130 1.1 joerg void
5131 1.1 joerg make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5132 1.1 joerg {
5133 1.1 joerg _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5134 1.1 joerg }
5135 1.1 joerg
5136 1.1 joerg // sort_heap
5137 1.1 joerg
5138 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5139 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 void
5140 1.1 joerg __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5141 1.1 joerg {
5142 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5143 1.1 joerg for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
5144 1.1 joerg _VSTD::__pop_heap<_Compare>(__first, __last, __comp, __n);
5145 1.1 joerg }
5146 1.1 joerg
5147 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5148 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5149 1.1 joerg void
5150 1.1 joerg sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5151 1.1 joerg {
5152 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5153 1.1 joerg _VSTD::__sort_heap<_Comp_ref>(__first, __last, __comp);
5154 1.1 joerg }
5155 1.1 joerg
5156 1.1 joerg template <class _RandomAccessIterator>
5157 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5158 1.1 joerg void
5159 1.1 joerg sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5160 1.1 joerg {
5161 1.1 joerg _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5162 1.1 joerg }
5163 1.1 joerg
5164 1.1 joerg // partial_sort
5165 1.1 joerg
5166 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5167 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 void
5168 1.1 joerg __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5169 1.1 joerg _Compare __comp)
5170 1.1 joerg {
5171 1.1 joerg _VSTD::__make_heap<_Compare>(__first, __middle, __comp);
5172 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5173 1.1 joerg for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5174 1.1 joerg {
5175 1.1 joerg if (__comp(*__i, *__first))
5176 1.1 joerg {
5177 1.1 joerg swap(*__i, *__first);
5178 1.1 joerg _VSTD::__sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5179 1.1 joerg }
5180 1.1 joerg }
5181 1.1 joerg _VSTD::__sort_heap<_Compare>(__first, __middle, __comp);
5182 1.1 joerg }
5183 1.1 joerg
5184 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5185 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5186 1.1 joerg void
5187 1.1 joerg partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5188 1.1 joerg _Compare __comp)
5189 1.1 joerg {
5190 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5191 1.1 joerg _VSTD::__partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5192 1.1 joerg }
5193 1.1 joerg
5194 1.1 joerg template <class _RandomAccessIterator>
5195 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5196 1.1 joerg void
5197 1.1 joerg partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5198 1.1 joerg {
5199 1.1 joerg _VSTD::partial_sort(__first, __middle, __last,
5200 1.1 joerg __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5201 1.1 joerg }
5202 1.1 joerg
5203 1.1 joerg // partial_sort_copy
5204 1.1 joerg
5205 1.1 joerg template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5206 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
5207 1.1 joerg __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5208 1.1 joerg _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5209 1.1 joerg {
5210 1.1 joerg _RandomAccessIterator __r = __result_first;
5211 1.1 joerg if (__r != __result_last)
5212 1.1 joerg {
5213 1.1 joerg for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
5214 1.1 joerg *__r = *__first;
5215 1.1 joerg _VSTD::__make_heap<_Compare>(__result_first, __r, __comp);
5216 1.1 joerg typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5217 1.1 joerg for (; __first != __last; ++__first)
5218 1.1 joerg if (__comp(*__first, *__result_first))
5219 1.1 joerg {
5220 1.1 joerg *__result_first = *__first;
5221 1.1 joerg _VSTD::__sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5222 1.1 joerg }
5223 1.1 joerg _VSTD::__sort_heap<_Compare>(__result_first, __r, __comp);
5224 1.1 joerg }
5225 1.1 joerg return __r;
5226 1.1 joerg }
5227 1.1 joerg
5228 1.1 joerg template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5229 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5230 1.1 joerg _RandomAccessIterator
5231 1.1 joerg partial_sort_copy(_InputIterator __first, _InputIterator __last,
5232 1.1 joerg _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5233 1.1 joerg {
5234 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5235 1.1 joerg return _VSTD::__partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5236 1.1 joerg }
5237 1.1 joerg
5238 1.1 joerg template <class _InputIterator, class _RandomAccessIterator>
5239 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5240 1.1 joerg _RandomAccessIterator
5241 1.1 joerg partial_sort_copy(_InputIterator __first, _InputIterator __last,
5242 1.1 joerg _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5243 1.1 joerg {
5244 1.1 joerg return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5245 1.1 joerg __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5246 1.1 joerg }
5247 1.1 joerg
5248 1.1 joerg // nth_element
5249 1.1 joerg
5250 1.1 joerg template<class _Compare, class _RandomAccessIterator>
5251 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 bool
5252 1.1 joerg __nth_element_find_guard(_RandomAccessIterator& __i, _RandomAccessIterator& __j,
5253 1.1 joerg _RandomAccessIterator __m, _Compare __comp)
5254 1.1 joerg {
5255 1.1 joerg // manually guard downward moving __j against __i
5256 1.1 joerg while (true) {
5257 1.1 joerg if (__i == --__j) {
5258 1.1 joerg return false;
5259 1.1 joerg }
5260 1.1 joerg if (__comp(*__j, *__m)) {
5261 1.1 joerg return true; // found guard for downward moving __j, now use unguarded partition
5262 1.1 joerg }
5263 1.1 joerg }
5264 1.1 joerg }
5265 1.1 joerg
5266 1.1 joerg template <class _Compare, class _RandomAccessIterator>
5267 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX11 void
5268 1.1 joerg __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5269 1.1 joerg {
5270 1.1 joerg // _Compare is known to be a reference type
5271 1.1 joerg typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5272 1.1 joerg const difference_type __limit = 7;
5273 1.1 joerg while (true)
5274 1.1 joerg {
5275 1.1 joerg if (__nth == __last)
5276 1.1 joerg return;
5277 1.1 joerg difference_type __len = __last - __first;
5278 1.1 joerg switch (__len)
5279 1.1 joerg {
5280 1.1 joerg case 0:
5281 1.1 joerg case 1:
5282 1.1 joerg return;
5283 1.1 joerg case 2:
5284 1.1 joerg if (__comp(*--__last, *__first))
5285 1.1 joerg swap(*__first, *__last);
5286 1.1 joerg return;
5287 1.1 joerg case 3:
5288 1.1 joerg {
5289 1.1 joerg _RandomAccessIterator __m = __first;
5290 1.1 joerg _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5291 1.1 joerg return;
5292 1.1 joerg }
5293 1.1 joerg }
5294 1.1 joerg if (__len <= __limit)
5295 1.1 joerg {
5296 1.1 joerg _VSTD::__selection_sort<_Compare>(__first, __last, __comp);
5297 1.1 joerg return;
5298 1.1 joerg }
5299 1.1 joerg // __len > __limit >= 3
5300 1.1 joerg _RandomAccessIterator __m = __first + __len/2;
5301 1.1 joerg _RandomAccessIterator __lm1 = __last;
5302 1.1 joerg unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5303 1.1 joerg // *__m is median
5304 1.1 joerg // partition [__first, __m) < *__m and *__m <= [__m, __last)
5305 1.1 joerg // (this inhibits tossing elements equivalent to __m around unnecessarily)
5306 1.1 joerg _RandomAccessIterator __i = __first;
5307 1.1 joerg _RandomAccessIterator __j = __lm1;
5308 1.1 joerg // j points beyond range to be tested, *__lm1 is known to be <= *__m
5309 1.1 joerg // The search going up is known to be guarded but the search coming down isn't.
5310 1.1 joerg // Prime the downward search with a guard.
5311 1.1 joerg if (!__comp(*__i, *__m)) // if *__first == *__m
5312 1.1 joerg {
5313 1.1 joerg // *__first == *__m, *__first doesn't go in first part
5314 1.1 joerg if (_VSTD::__nth_element_find_guard<_Compare>(__i, __j, __m, __comp)) {
5315 1.1 joerg swap(*__i, *__j);
5316 1.1 joerg ++__n_swaps;
5317 1.1 joerg } else {
5318 1.1 joerg // *__first == *__m, *__m <= all other elements
5319 1.1 joerg // Partition instead into [__first, __i) == *__first and *__first < [__i, __last)
5320 1.1 joerg ++__i; // __first + 1
5321 1.1 joerg __j = __last;
5322 1.1 joerg if (!__comp(*__first, *--__j)) { // we need a guard if *__first == *(__last-1)
5323 1.1 joerg while (true) {
5324 1.1 joerg if (__i == __j) {
5325 1.1 joerg return; // [__first, __last) all equivalent elements
5326 1.1 joerg } else if (__comp(*__first, *__i)) {
5327 1.1 joerg swap(*__i, *__j);
5328 1.1 joerg ++__n_swaps;
5329 1.1 joerg ++__i;
5330 1.1 joerg break;
5331 1.1 joerg }
5332 1.1 joerg ++__i;
5333 1.1 joerg }
5334 1.1 joerg }
5335 1.1 joerg // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5336 1.1 joerg if (__i == __j) {
5337 1.1 joerg return;
5338 1.1 joerg }
5339 1.1 joerg while (true) {
5340 1.1 joerg while (!__comp(*__first, *__i))
5341 1.1 joerg ++__i;
5342 1.1 joerg while (__comp(*__first, *--__j))
5343 1.1 joerg ;
5344 1.1 joerg if (__i >= __j)
5345 1.1 joerg break;
5346 1.1 joerg swap(*__i, *__j);
5347 1.1 joerg ++__n_swaps;
5348 1.1 joerg ++__i;
5349 1.1 joerg }
5350 1.1 joerg // [__first, __i) == *__first and *__first < [__i, __last)
5351 1.1 joerg // The first part is sorted,
5352 1.1 joerg if (__nth < __i) {
5353 1.1 joerg return;
5354 1.1 joerg }
5355 1.1 joerg // __nth_element the second part
5356 1.1 joerg // _VSTD::__nth_element<_Compare>(__i, __nth, __last, __comp);
5357 1.1 joerg __first = __i;
5358 1.1 joerg continue;
5359 1.1 joerg }
5360 1.1 joerg }
5361 1.1 joerg ++__i;
5362 1.1 joerg // j points beyond range to be tested, *__lm1 is known to be <= *__m
5363 1.1 joerg // if not yet partitioned...
5364 1.1 joerg if (__i < __j)
5365 1.1 joerg {
5366 1.1 joerg // known that *(__i - 1) < *__m
5367 1.1 joerg while (true)
5368 1.1 joerg {
5369 1.1 joerg // __m still guards upward moving __i
5370 1.1 joerg while (__comp(*__i, *__m))
5371 1.1 joerg ++__i;
5372 1.1 joerg // It is now known that a guard exists for downward moving __j
5373 1.1 joerg while (!__comp(*--__j, *__m))
5374 1.1 joerg ;
5375 1.1 joerg if (__i >= __j)
5376 1.1 joerg break;
5377 1.1 joerg swap(*__i, *__j);
5378 1.1 joerg ++__n_swaps;
5379 1.1 joerg // It is known that __m != __j
5380 1.1 joerg // If __m just moved, follow it
5381 1.1 joerg if (__m == __i)
5382 1.1 joerg __m = __j;
5383 1.1 joerg ++__i;
5384 1.1 joerg }
5385 1.1 joerg }
5386 1.1 joerg // [__first, __i) < *__m and *__m <= [__i, __last)
5387 1.1 joerg if (__i != __m && __comp(*__m, *__i))
5388 1.1 joerg {
5389 1.1 joerg swap(*__i, *__m);
5390 1.1 joerg ++__n_swaps;
5391 1.1 joerg }
5392 1.1 joerg // [__first, __i) < *__i and *__i <= [__i+1, __last)
5393 1.1 joerg if (__nth == __i)
5394 1.1 joerg return;
5395 1.1 joerg if (__n_swaps == 0)
5396 1.1 joerg {
5397 1.1 joerg // We were given a perfectly partitioned sequence. Coincidence?
5398 1.1 joerg if (__nth < __i)
5399 1.1 joerg {
5400 1.1 joerg // Check for [__first, __i) already sorted
5401 1.1 joerg __j = __m = __first;
5402 1.1 joerg while (true) {
5403 1.1 joerg if (++__j == __i) {
5404 1.1 joerg // [__first, __i) sorted
5405 1.1 joerg return;
5406 1.1 joerg }
5407 1.1 joerg if (__comp(*__j, *__m)) {
5408 1.1 joerg // not yet sorted, so sort
5409 1.1 joerg break;
5410 1.1 joerg }
5411 1.1 joerg __m = __j;
5412 1.1 joerg }
5413 1.1 joerg }
5414 1.1 joerg else
5415 1.1 joerg {
5416 1.1 joerg // Check for [__i, __last) already sorted
5417 1.1 joerg __j = __m = __i;
5418 1.1 joerg while (true) {
5419 1.1 joerg if (++__j == __last) {
5420 1.1 joerg // [__i, __last) sorted
5421 1.1 joerg return;
5422 1.1 joerg }
5423 1.1 joerg if (__comp(*__j, *__m)) {
5424 1.1 joerg // not yet sorted, so sort
5425 1.1 joerg break;
5426 1.1 joerg }
5427 1.1 joerg __m = __j;
5428 1.1 joerg }
5429 1.1 joerg }
5430 1.1 joerg }
5431 1.1 joerg // __nth_element on range containing __nth
5432 1.1 joerg if (__nth < __i)
5433 1.1 joerg {
5434 1.1 joerg // _VSTD::__nth_element<_Compare>(__first, __nth, __i, __comp);
5435 1.1 joerg __last = __i;
5436 1.1 joerg }
5437 1.1 joerg else
5438 1.1 joerg {
5439 1.1 joerg // _VSTD::__nth_element<_Compare>(__i+1, __nth, __last, __comp);
5440 1.1 joerg __first = ++__i;
5441 1.1 joerg }
5442 1.1 joerg }
5443 1.1 joerg }
5444 1.1 joerg
5445 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5446 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5447 1.1 joerg void
5448 1.1 joerg nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5449 1.1 joerg {
5450 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5451 1.1 joerg _VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5452 1.1 joerg }
5453 1.1 joerg
5454 1.1 joerg template <class _RandomAccessIterator>
5455 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5456 1.1 joerg void
5457 1.1 joerg nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5458 1.1 joerg {
5459 1.1 joerg _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5460 1.1 joerg }
5461 1.1 joerg
5462 1.1 joerg // sort
5463 1.1 joerg
5464 1.1 joerg template <class _RandomAccessIterator, class _Compare>
5465 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5466 1.1 joerg void
5467 1.1 joerg sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5468 1.1 joerg {
5469 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5470 1.1 joerg if (__libcpp_is_constant_evaluated()) {
5471 1.1 joerg _VSTD::__partial_sort<_Comp_ref>(__first, __last, __last, _Comp_ref(__comp));
5472 1.1 joerg } else {
5473 1.1 joerg _VSTD::__sort<_Comp_ref>(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _Comp_ref(__comp));
5474 1.1 joerg }
5475 1.1 joerg }
5476 1.1 joerg
5477 1.1 joerg template <class _RandomAccessIterator>
5478 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5479 1.1 joerg void
5480 1.1 joerg sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5481 1.1 joerg {
5482 1.1 joerg _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5483 1.1 joerg }
5484 1.1 joerg
5485 1.1 joerg // includes
5486 1.1 joerg
5487 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2>
5488 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5489 1.1 joerg __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5490 1.1 joerg _Compare __comp)
5491 1.1 joerg {
5492 1.1 joerg for (; __first2 != __last2; ++__first1)
5493 1.1 joerg {
5494 1.1 joerg if (__first1 == __last1 || __comp(*__first2, *__first1))
5495 1.1 joerg return false;
5496 1.1 joerg if (!__comp(*__first1, *__first2))
5497 1.1 joerg ++__first2;
5498 1.1 joerg }
5499 1.1 joerg return true;
5500 1.1 joerg }
5501 1.1 joerg
5502 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _Compare>
5503 1.1 joerg _LIBCPP_NODISCARD_EXT inline
5504 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5505 1.1 joerg bool
5506 1.1 joerg includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5507 1.1 joerg _Compare __comp)
5508 1.1 joerg {
5509 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5510 1.1 joerg return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5511 1.1 joerg }
5512 1.1 joerg
5513 1.1 joerg template <class _InputIterator1, class _InputIterator2>
5514 1.1 joerg _LIBCPP_NODISCARD_EXT inline
5515 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5516 1.1 joerg bool
5517 1.1 joerg includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5518 1.1 joerg {
5519 1.1 joerg return _VSTD::includes(__first1, __last1, __first2, __last2,
5520 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5521 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5522 1.1 joerg }
5523 1.1 joerg
5524 1.1 joerg // set_union
5525 1.1 joerg
5526 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5527 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5528 1.1 joerg __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5529 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5530 1.1 joerg {
5531 1.1 joerg for (; __first1 != __last1; ++__result)
5532 1.1 joerg {
5533 1.1 joerg if (__first2 == __last2)
5534 1.1 joerg return _VSTD::copy(__first1, __last1, __result);
5535 1.1 joerg if (__comp(*__first2, *__first1))
5536 1.1 joerg {
5537 1.1 joerg *__result = *__first2;
5538 1.1 joerg ++__first2;
5539 1.1 joerg }
5540 1.1 joerg else
5541 1.1 joerg {
5542 1.1 joerg if (!__comp(*__first1, *__first2))
5543 1.1 joerg ++__first2;
5544 1.1 joerg *__result = *__first1;
5545 1.1 joerg ++__first1;
5546 1.1 joerg }
5547 1.1 joerg }
5548 1.1 joerg return _VSTD::copy(__first2, __last2, __result);
5549 1.1 joerg }
5550 1.1 joerg
5551 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5552 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5553 1.1 joerg _OutputIterator
5554 1.1 joerg set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5555 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5556 1.1 joerg {
5557 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5558 1.1 joerg return _VSTD::__set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5559 1.1 joerg }
5560 1.1 joerg
5561 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5562 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5563 1.1 joerg _OutputIterator
5564 1.1 joerg set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5565 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5566 1.1 joerg {
5567 1.1 joerg return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5568 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5569 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5570 1.1 joerg }
5571 1.1 joerg
5572 1.1 joerg // set_intersection
5573 1.1 joerg
5574 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5575 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5576 1.1 joerg __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5577 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5578 1.1 joerg {
5579 1.1 joerg while (__first1 != __last1 && __first2 != __last2)
5580 1.1 joerg {
5581 1.1 joerg if (__comp(*__first1, *__first2))
5582 1.1 joerg ++__first1;
5583 1.1 joerg else
5584 1.1 joerg {
5585 1.1 joerg if (!__comp(*__first2, *__first1))
5586 1.1 joerg {
5587 1.1 joerg *__result = *__first1;
5588 1.1 joerg ++__result;
5589 1.1 joerg ++__first1;
5590 1.1 joerg }
5591 1.1 joerg ++__first2;
5592 1.1 joerg }
5593 1.1 joerg }
5594 1.1 joerg return __result;
5595 1.1 joerg }
5596 1.1 joerg
5597 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5598 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5599 1.1 joerg _OutputIterator
5600 1.1 joerg set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5601 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5602 1.1 joerg {
5603 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5604 1.1 joerg return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5605 1.1 joerg }
5606 1.1 joerg
5607 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5608 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5609 1.1 joerg _OutputIterator
5610 1.1 joerg set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5611 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5612 1.1 joerg {
5613 1.1 joerg return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5614 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5615 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5616 1.1 joerg }
5617 1.1 joerg
5618 1.1 joerg // set_difference
5619 1.1 joerg
5620 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5621 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5622 1.1 joerg __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5623 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5624 1.1 joerg {
5625 1.1 joerg while (__first1 != __last1)
5626 1.1 joerg {
5627 1.1 joerg if (__first2 == __last2)
5628 1.1 joerg return _VSTD::copy(__first1, __last1, __result);
5629 1.1 joerg if (__comp(*__first1, *__first2))
5630 1.1 joerg {
5631 1.1 joerg *__result = *__first1;
5632 1.1 joerg ++__result;
5633 1.1 joerg ++__first1;
5634 1.1 joerg }
5635 1.1 joerg else
5636 1.1 joerg {
5637 1.1 joerg if (!__comp(*__first2, *__first1))
5638 1.1 joerg ++__first1;
5639 1.1 joerg ++__first2;
5640 1.1 joerg }
5641 1.1 joerg }
5642 1.1 joerg return __result;
5643 1.1 joerg }
5644 1.1 joerg
5645 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5646 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5647 1.1 joerg _OutputIterator
5648 1.1 joerg set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5649 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5650 1.1 joerg {
5651 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5652 1.1 joerg return _VSTD::__set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5653 1.1 joerg }
5654 1.1 joerg
5655 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5656 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5657 1.1 joerg _OutputIterator
5658 1.1 joerg set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5659 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5660 1.1 joerg {
5661 1.1 joerg return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5662 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5663 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5664 1.1 joerg }
5665 1.1 joerg
5666 1.1 joerg // set_symmetric_difference
5667 1.1 joerg
5668 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5669 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5670 1.1 joerg __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5671 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5672 1.1 joerg {
5673 1.1 joerg while (__first1 != __last1)
5674 1.1 joerg {
5675 1.1 joerg if (__first2 == __last2)
5676 1.1 joerg return _VSTD::copy(__first1, __last1, __result);
5677 1.1 joerg if (__comp(*__first1, *__first2))
5678 1.1 joerg {
5679 1.1 joerg *__result = *__first1;
5680 1.1 joerg ++__result;
5681 1.1 joerg ++__first1;
5682 1.1 joerg }
5683 1.1 joerg else
5684 1.1 joerg {
5685 1.1 joerg if (__comp(*__first2, *__first1))
5686 1.1 joerg {
5687 1.1 joerg *__result = *__first2;
5688 1.1 joerg ++__result;
5689 1.1 joerg }
5690 1.1 joerg else
5691 1.1 joerg ++__first1;
5692 1.1 joerg ++__first2;
5693 1.1 joerg }
5694 1.1 joerg }
5695 1.1 joerg return _VSTD::copy(__first2, __last2, __result);
5696 1.1 joerg }
5697 1.1 joerg
5698 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5699 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5700 1.1 joerg _OutputIterator
5701 1.1 joerg set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5702 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5703 1.1 joerg {
5704 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5705 1.1 joerg return _VSTD::__set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5706 1.1 joerg }
5707 1.1 joerg
5708 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5709 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5710 1.1 joerg _OutputIterator
5711 1.1 joerg set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5712 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5713 1.1 joerg {
5714 1.1 joerg return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5715 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5716 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5717 1.1 joerg }
5718 1.1 joerg
5719 1.1 joerg // lexicographical_compare
5720 1.1 joerg
5721 1.1 joerg template <class _Compare, class _InputIterator1, class _InputIterator2>
5722 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5723 1.1 joerg __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5724 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5725 1.1 joerg {
5726 1.1 joerg for (; __first2 != __last2; ++__first1, (void) ++__first2)
5727 1.1 joerg {
5728 1.1 joerg if (__first1 == __last1 || __comp(*__first1, *__first2))
5729 1.1 joerg return true;
5730 1.1 joerg if (__comp(*__first2, *__first1))
5731 1.1 joerg return false;
5732 1.1 joerg }
5733 1.1 joerg return false;
5734 1.1 joerg }
5735 1.1 joerg
5736 1.1 joerg template <class _InputIterator1, class _InputIterator2, class _Compare>
5737 1.1 joerg _LIBCPP_NODISCARD_EXT inline
5738 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5739 1.1 joerg bool
5740 1.1 joerg lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5741 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5742 1.1 joerg {
5743 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5744 1.1 joerg return _VSTD::__lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5745 1.1 joerg }
5746 1.1 joerg
5747 1.1 joerg template <class _InputIterator1, class _InputIterator2>
5748 1.1 joerg _LIBCPP_NODISCARD_EXT inline
5749 1.1 joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5750 1.1 joerg bool
5751 1.1 joerg lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5752 1.1 joerg _InputIterator2 __first2, _InputIterator2 __last2)
5753 1.1 joerg {
5754 1.1 joerg return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5755 1.1 joerg __less<typename iterator_traits<_InputIterator1>::value_type,
5756 1.1 joerg typename iterator_traits<_InputIterator2>::value_type>());
5757 1.1 joerg }
5758 1.1 joerg
5759 1.1 joerg // next_permutation
5760 1.1 joerg
5761 1.1 joerg template <class _Compare, class _BidirectionalIterator>
5762 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5763 1.1 joerg __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5764 1.1 joerg {
5765 1.1 joerg _BidirectionalIterator __i = __last;
5766 1.1 joerg if (__first == __last || __first == --__i)
5767 1.1 joerg return false;
5768 1.1 joerg while (true)
5769 1.1 joerg {
5770 1.1 joerg _BidirectionalIterator __ip1 = __i;
5771 1.1 joerg if (__comp(*--__i, *__ip1))
5772 1.1 joerg {
5773 1.1 joerg _BidirectionalIterator __j = __last;
5774 1.1 joerg while (!__comp(*__i, *--__j))
5775 1.1 joerg ;
5776 1.1 joerg swap(*__i, *__j);
5777 1.1 joerg _VSTD::reverse(__ip1, __last);
5778 1.1 joerg return true;
5779 1.1 joerg }
5780 1.1 joerg if (__i == __first)
5781 1.1 joerg {
5782 1.1 joerg _VSTD::reverse(__first, __last);
5783 1.1 joerg return false;
5784 1.1 joerg }
5785 1.1 joerg }
5786 1.1 joerg }
5787 1.1 joerg
5788 1.1 joerg template <class _BidirectionalIterator, class _Compare>
5789 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5790 1.1 joerg bool
5791 1.1 joerg next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5792 1.1 joerg {
5793 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5794 1.1 joerg return _VSTD::__next_permutation<_Comp_ref>(__first, __last, __comp);
5795 1.1 joerg }
5796 1.1 joerg
5797 1.1 joerg template <class _BidirectionalIterator>
5798 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5799 1.1 joerg bool
5800 1.1 joerg next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5801 1.1 joerg {
5802 1.1 joerg return _VSTD::next_permutation(__first, __last,
5803 1.1 joerg __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5804 1.1 joerg }
5805 1.1 joerg
5806 1.1 joerg // prev_permutation
5807 1.1 joerg
5808 1.1 joerg template <class _Compare, class _BidirectionalIterator>
5809 1.1 joerg _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5810 1.1 joerg __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5811 1.1 joerg {
5812 1.1 joerg _BidirectionalIterator __i = __last;
5813 1.1 joerg if (__first == __last || __first == --__i)
5814 1.1 joerg return false;
5815 1.1 joerg while (true)
5816 1.1 joerg {
5817 1.1 joerg _BidirectionalIterator __ip1 = __i;
5818 1.1 joerg if (__comp(*__ip1, *--__i))
5819 1.1 joerg {
5820 1.1 joerg _BidirectionalIterator __j = __last;
5821 1.1 joerg while (!__comp(*--__j, *__i))
5822 1.1 joerg ;
5823 1.1 joerg swap(*__i, *__j);
5824 1.1 joerg _VSTD::reverse(__ip1, __last);
5825 1.1 joerg return true;
5826 1.1 joerg }
5827 1.1 joerg if (__i == __first)
5828 1.1 joerg {
5829 1.1 joerg _VSTD::reverse(__first, __last);
5830 1.1 joerg return false;
5831 1.1 joerg }
5832 1.1 joerg }
5833 1.1 joerg }
5834 1.1 joerg
5835 1.1 joerg template <class _BidirectionalIterator, class _Compare>
5836 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5837 1.1 joerg bool
5838 1.1 joerg prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5839 1.1 joerg {
5840 1.1 joerg typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5841 1.1 joerg return _VSTD::__prev_permutation<_Comp_ref>(__first, __last, __comp);
5842 1.1 joerg }
5843 1.1 joerg
5844 1.1 joerg template <class _BidirectionalIterator>
5845 1.1 joerg inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5846 1.1 joerg bool
5847 1.1 joerg prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5848 1.1 joerg {
5849 1.1 joerg return _VSTD::prev_permutation(__first, __last,
5850 1.1 joerg __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5851 1.1 joerg }
5852 1.1 joerg
5853 1.1 joerg _LIBCPP_END_NAMESPACE_STD
5854 1.1 joerg
5855 1.1 joerg _LIBCPP_POP_MACROS
5856 1.1 joerg
5857 1.1 joerg #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5858 1.1 joerg # include <__pstl_algorithm>
5859 1.1 joerg #endif
5860 1.1 joerg
5861 1.1 joerg #endif // _LIBCPP_ALGORITHM
5862