2 //===-------------------------- algorithm ---------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_ALGORITHM
11 #define _LIBCPP_ALGORITHM
16 #include <initializer_list>
21 template <class InputIterator, class Predicate>
22 constexpr bool // constexpr in C++20
23 all_of(InputIterator first, InputIterator last, Predicate pred);
25 template <class InputIterator, class Predicate>
26 constexpr bool // constexpr in C++20
27 any_of(InputIterator first, InputIterator last, Predicate pred);
29 template <class InputIterator, class Predicate>
30 constexpr bool // constexpr in C++20
31 none_of(InputIterator first, InputIterator last, Predicate pred);
33 template <class InputIterator, class Function>
34 constexpr Function // constexpr in C++20
35 for_each(InputIterator first, InputIterator last, Function f);
37 template<class InputIterator, class Size, class Function>
38 constexpr InputIterator // constexpr in C++20
39 for_each_n(InputIterator first, Size n, Function f); // C++17
41 template <class InputIterator, class T>
42 constexpr InputIterator // constexpr in C++20
43 find(InputIterator first, InputIterator last, const T& value);
45 template <class InputIterator, class Predicate>
46 constexpr InputIterator // constexpr in C++20
47 find_if(InputIterator first, InputIterator last, Predicate pred);
49 template<class InputIterator, class Predicate>
50 constexpr InputIterator // constexpr in C++20
51 find_if_not(InputIterator first, InputIterator last, Predicate pred);
53 template <class ForwardIterator1, class ForwardIterator2>
54 constexpr ForwardIterator1 // constexpr in C++20
55 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
58 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
59 constexpr ForwardIterator1 // constexpr in C++20
60 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
63 template <class ForwardIterator1, class ForwardIterator2>
64 constexpr ForwardIterator1 // constexpr in C++20
65 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
68 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
69 constexpr ForwardIterator1 // constexpr in C++20
70 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
73 template <class ForwardIterator>
74 constexpr ForwardIterator // constexpr in C++20
75 adjacent_find(ForwardIterator first, ForwardIterator last);
77 template <class ForwardIterator, class BinaryPredicate>
78 constexpr ForwardIterator // constexpr in C++20
79 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
81 template <class InputIterator, class T>
82 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
83 count(InputIterator first, InputIterator last, const T& value);
85 template <class InputIterator, class Predicate>
86 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
87 count_if(InputIterator first, InputIterator last, Predicate pred);
89 template <class InputIterator1, class InputIterator2>
90 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
91 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
93 template <class InputIterator1, class InputIterator2>
94 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
95 mismatch(InputIterator1 first1, InputIterator1 last1,
96 InputIterator2 first2, InputIterator2 last2); // **C++14**
98 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
99 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
103 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
104 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
109 template <class InputIterator1, class InputIterator2>
110 constexpr bool // constexpr in C++20
111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
113 template <class InputIterator1, class InputIterator2>
114 constexpr bool // constexpr in C++20
115 equal(InputIterator1 first1, InputIterator1 last1,
116 InputIterator2 first2, InputIterator2 last2); // **C++14**
118 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
119 constexpr bool // constexpr in C++20
120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
123 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
124 constexpr bool // constexpr in C++20
125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
129 template<class ForwardIterator1, class ForwardIterator2>
130 constexpr bool // constexpr in C++20
131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
134 template<class ForwardIterator1, class ForwardIterator2>
135 constexpr bool // constexpr in C++20
136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
139 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
140 constexpr bool // constexpr in C++20
141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
144 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
145 constexpr bool // constexpr in C++20
146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
150 template <class ForwardIterator1, class ForwardIterator2>
151 constexpr ForwardIterator1 // constexpr in C++20
152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
155 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
156 constexpr ForwardIterator1 // constexpr in C++20
157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
160 template <class ForwardIterator, class Size, class T>
161 constexpr ForwardIterator // constexpr in C++20
162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
164 template <class ForwardIterator, class Size, class T, class BinaryPredicate>
165 constexpr ForwardIterator // constexpr in C++20
166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
169 template <class InputIterator, class OutputIterator>
170 constexpr OutputIterator // constexpr in C++20
171 copy(InputIterator first, InputIterator last, OutputIterator result);
173 template<class InputIterator, class OutputIterator, class Predicate>
174 constexpr OutputIterator // constexpr in C++20
175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
178 template<class InputIterator, class Size, class OutputIterator>
179 constexpr OutputIterator // constexpr in C++20
180 copy_n(InputIterator first, Size n, OutputIterator result);
182 template <class BidirectionalIterator1, class BidirectionalIterator2>
183 constexpr BidirectionalIterator2 // constexpr in C++20
184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
187 template <class ForwardIterator1, class ForwardIterator2>
188 constexpr ForwardIterator2 // constexpr in C++20
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
191 template <class ForwardIterator1, class ForwardIterator2>
192 constexpr void // constexpr in C++20
193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
195 template <class InputIterator, class OutputIterator, class UnaryOperation>
196 constexpr OutputIterator // constexpr in C++20
197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
199 template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
200 constexpr OutputIterator // constexpr in C++20
201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
204 template <class ForwardIterator, class T>
205 constexpr void // constexpr in C++20
206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
208 template <class ForwardIterator, class Predicate, class T>
209 constexpr void // constexpr in C++20
210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
212 template <class InputIterator, class OutputIterator, class T>
213 constexpr OutputIterator // constexpr in C++20
214 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 const T& old_value, const T& new_value);
217 template <class InputIterator, class OutputIterator, class Predicate, class T>
218 constexpr OutputIterator // constexpr in C++20
219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
221 template <class ForwardIterator, class T>
222 constexpr void // constexpr in C++20
223 fill(ForwardIterator first, ForwardIterator last, const T& value);
225 template <class OutputIterator, class Size, class T>
226 constexpr OutputIterator // constexpr in C++20
227 fill_n(OutputIterator first, Size n, const T& value);
229 template <class ForwardIterator, class Generator>
230 constexpr void // constexpr in C++20
231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
233 template <class OutputIterator, class Size, class Generator>
234 constexpr OutputIterator // constexpr in C++20
235 generate_n(OutputIterator first, Size n, Generator gen);
237 template <class ForwardIterator, class T>
238 constexpr ForwardIterator // constexpr in C++20
239 remove(ForwardIterator first, ForwardIterator last, const T& value);
241 template <class ForwardIterator, class Predicate>
242 constexpr ForwardIterator // constexpr in C++20
243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
245 template <class InputIterator, class OutputIterator, class T>
246 constexpr OutputIterator // constexpr in C++20
247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
249 template <class InputIterator, class OutputIterator, class Predicate>
250 constexpr OutputIterator // constexpr in C++20
251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
253 template <class ForwardIterator>
254 constexpr ForwardIterator // constexpr in C++20
255 unique(ForwardIterator first, ForwardIterator last);
257 template <class ForwardIterator, class BinaryPredicate>
258 constexpr ForwardIterator // constexpr in C++20
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
261 template <class InputIterator, class OutputIterator>
262 constexpr OutputIterator // constexpr in C++20
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
265 template <class InputIterator, class OutputIterator, class BinaryPredicate>
266 constexpr OutputIterator // constexpr in C++20
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
269 template <class BidirectionalIterator>
270 constexpr void // constexpr in C++20
271 reverse(BidirectionalIterator first, BidirectionalIterator last);
273 template <class BidirectionalIterator, class OutputIterator>
274 constexpr OutputIterator // constexpr in C++20
275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
277 template <class ForwardIterator>
278 constexpr ForwardIterator // constexpr in C++20
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
281 template <class ForwardIterator, class OutputIterator>
282 constexpr OutputIterator // constexpr in C++20
283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
285 template <class RandomAccessIterator>
287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
289 template <class RandomAccessIterator, class RandomNumberGenerator>
291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
294 template<class PopulationIterator, class SampleIterator,
295 class Distance, class UniformRandomBitGenerator>
296 SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 SampleIterator out, Distance n,
298 UniformRandomBitGenerator&& g); // C++17
300 template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
302 UniformRandomNumberGenerator&& g);
304 template<class ForwardIterator>
305 constexpr ForwardIterator
306 shift_left(ForwardIterator first, ForwardIterator last,
307 typename iterator_traits<ForwardIterator>::difference_type n); // C++20
309 template<class ForwardIterator>
310 constexpr ForwardIterator
311 shift_right(ForwardIterator first, ForwardIterator last,
312 typename iterator_traits<ForwardIterator>::difference_type n); // C++20
314 template <class InputIterator, class Predicate>
315 constexpr bool // constexpr in C++20
316 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
318 template <class ForwardIterator, class Predicate>
319 constexpr ForwardIterator // constexpr in C++20
320 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
322 template <class InputIterator, class OutputIterator1,
323 class OutputIterator2, class Predicate>
324 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
325 partition_copy(InputIterator first, InputIterator last,
326 OutputIterator1 out_true, OutputIterator2 out_false,
329 template <class ForwardIterator, class Predicate>
331 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
333 template<class ForwardIterator, class Predicate>
334 constexpr ForwardIterator // constexpr in C++20
335 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
337 template <class ForwardIterator>
338 constexpr bool // constexpr in C++20
339 is_sorted(ForwardIterator first, ForwardIterator last);
341 template <class ForwardIterator, class Compare>
342 constexpr bool // constexpr in C++20
343 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
345 template<class ForwardIterator>
346 constexpr ForwardIterator // constexpr in C++20
347 is_sorted_until(ForwardIterator first, ForwardIterator last);
349 template <class ForwardIterator, class Compare>
350 constexpr ForwardIterator // constexpr in C++20
351 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
353 template <class RandomAccessIterator>
355 sort(RandomAccessIterator first, RandomAccessIterator last);
357 template <class RandomAccessIterator, class Compare>
359 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
361 template <class RandomAccessIterator>
363 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
365 template <class RandomAccessIterator, class Compare>
367 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
369 template <class RandomAccessIterator>
371 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
373 template <class RandomAccessIterator, class Compare>
375 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
377 template <class InputIterator, class RandomAccessIterator>
379 partial_sort_copy(InputIterator first, InputIterator last,
380 RandomAccessIterator result_first, RandomAccessIterator result_last);
382 template <class InputIterator, class RandomAccessIterator, class Compare>
384 partial_sort_copy(InputIterator first, InputIterator last,
385 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
387 template <class RandomAccessIterator>
389 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
391 template <class RandomAccessIterator, class Compare>
393 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
395 template <class ForwardIterator, class T>
396 constexpr ForwardIterator // constexpr in C++20
397 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
399 template <class ForwardIterator, class T, class Compare>
400 constexpr ForwardIterator // constexpr in C++20
401 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
403 template <class ForwardIterator, class T>
404 constexpr ForwardIterator // constexpr in C++20
405 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
407 template <class ForwardIterator, class T, class Compare>
408 constexpr ForwardIterator // constexpr in C++20
409 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
411 template <class ForwardIterator, class T>
412 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
413 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
415 template <class ForwardIterator, class T, class Compare>
416 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
417 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
419 template <class ForwardIterator, class T>
420 constexpr bool // constexpr in C++20
421 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
423 template <class ForwardIterator, class T, class Compare>
424 constexpr bool // constexpr in C++20
425 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
427 template <class InputIterator1, class InputIterator2, class OutputIterator>
428 constexpr OutputIterator // constexpr in C++20
429 merge(InputIterator1 first1, InputIterator1 last1,
430 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
432 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
433 constexpr OutputIterator // constexpr in C++20
434 merge(InputIterator1 first1, InputIterator1 last1,
435 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
437 template <class BidirectionalIterator>
439 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
441 template <class BidirectionalIterator, class Compare>
443 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
445 template <class InputIterator1, class InputIterator2>
446 constexpr bool // constexpr in C++20
447 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
449 template <class InputIterator1, class InputIterator2, class Compare>
450 constexpr bool // constexpr in C++20
451 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
453 template <class InputIterator1, class InputIterator2, class OutputIterator>
454 constexpr OutputIterator // constexpr in C++20
455 set_union(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
459 constexpr OutputIterator // constexpr in C++20
460 set_union(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463 template <class InputIterator1, class InputIterator2, class OutputIterator>
464 constexpr OutputIterator // constexpr in C++20
465 set_intersection(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
469 constexpr OutputIterator // constexpr in C++20
470 set_intersection(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473 template <class InputIterator1, class InputIterator2, class OutputIterator>
474 constexpr OutputIterator // constexpr in C++20
475 set_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
478 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
479 constexpr OutputIterator // constexpr in C++20
480 set_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
483 template <class InputIterator1, class InputIterator2, class OutputIterator>
484 constexpr OutputIterator // constexpr in C++20
485 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
486 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
488 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
489 constexpr OutputIterator // constexpr in C++20
490 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
491 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
493 template <class RandomAccessIterator>
495 push_heap(RandomAccessIterator first, RandomAccessIterator last);
497 template <class RandomAccessIterator, class Compare>
499 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
501 template <class RandomAccessIterator>
503 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
505 template <class RandomAccessIterator, class Compare>
507 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
509 template <class RandomAccessIterator>
511 make_heap(RandomAccessIterator first, RandomAccessIterator last);
513 template <class RandomAccessIterator, class Compare>
515 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
517 template <class RandomAccessIterator>
519 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
521 template <class RandomAccessIterator, class Compare>
523 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
525 template <class RandomAccessIterator>
526 constexpr bool // constexpr in C++20
527 is_heap(RandomAccessIterator first, RandomAccessiterator last);
529 template <class RandomAccessIterator, class Compare>
530 constexpr bool // constexpr in C++20
531 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
533 template <class RandomAccessIterator>
534 constexpr RandomAccessIterator // constexpr in C++20
535 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
537 template <class RandomAccessIterator, class Compare>
538 constexpr RandomAccessIterator // constexpr in C++20
539 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
541 template <class ForwardIterator>
542 constexpr ForwardIterator // constexpr in C++14
543 min_element(ForwardIterator first, ForwardIterator last);
545 template <class ForwardIterator, class Compare>
546 constexpr ForwardIterator // constexpr in C++14
547 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
550 constexpr const T& // constexpr in C++14
551 min(const T& a, const T& b);
553 template <class T, class Compare>
554 constexpr const T& // constexpr in C++14
555 min(const T& a, const T& b, Compare comp);
558 constexpr T // constexpr in C++14
559 min(initializer_list<T> t);
561 template<class T, class Compare>
562 constexpr T // constexpr in C++14
563 min(initializer_list<T> t, Compare comp);
566 constexpr const T& clamp(const T& v, const T& lo, const T& hi); // C++17
568 template<class T, class Compare>
569 constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
571 template <class ForwardIterator>
572 constexpr ForwardIterator // constexpr in C++14
573 max_element(ForwardIterator first, ForwardIterator last);
575 template <class ForwardIterator, class Compare>
576 constexpr ForwardIterator // constexpr in C++14
577 max_element(ForwardIterator first, ForwardIterator last, Compare comp);
580 constexpr const T& // constexpr in C++14
581 max(const T& a, const T& b);
583 template <class T, class Compare>
584 constexpr const T& // constexpr in C++14
585 max(const T& a, const T& b, Compare comp);
588 constexpr T // constexpr in C++14
589 max(initializer_list<T> t);
591 template<class T, class Compare>
592 constexpr T // constexpr in C++14
593 max(initializer_list<T> t, Compare comp);
595 template<class ForwardIterator>
596 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
597 minmax_element(ForwardIterator first, ForwardIterator last);
599 template<class ForwardIterator, class Compare>
600 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
601 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
604 constexpr pair<const T&, const T&> // constexpr in C++14
605 minmax(const T& a, const T& b);
607 template<class T, class Compare>
608 constexpr pair<const T&, const T&> // constexpr in C++14
609 minmax(const T& a, const T& b, Compare comp);
612 constexpr pair<T, T> // constexpr in C++14
613 minmax(initializer_list<T> t);
615 template<class T, class Compare>
616 constexpr pair<T, T> // constexpr in C++14
617 minmax(initializer_list<T> t, Compare comp);
619 template <class InputIterator1, class InputIterator2>
620 constexpr bool // constexpr in C++20
621 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
623 template <class InputIterator1, class InputIterator2, class Compare>
624 constexpr bool // constexpr in C++20
625 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
626 InputIterator2 first2, InputIterator2 last2, Compare comp);
628 template <class BidirectionalIterator>
629 constexpr bool // constexpr in C++20
630 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
632 template <class BidirectionalIterator, class Compare>
633 constexpr bool // constexpr in C++20
634 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
636 template <class BidirectionalIterator>
637 constexpr bool // constexpr in C++20
638 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
640 template <class BidirectionalIterator, class Compare>
641 constexpr bool // constexpr in C++20
642 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
649 #include <initializer_list>
650 #include <type_traits>
652 #include <utility> // needed to provide swap_ranges.
654 #include <functional>
662 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
663 #pragma GCC system_header
667 #include <__undef_macros>
670 _LIBCPP_BEGIN_NAMESPACE_STD
672 // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
673 // * That only works with C++14 and later, and
674 // * We haven't included <functional> here.
675 template <class _T1, class _T2 = _T1>
678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
679 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
680 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
685 struct __equal_to<_T1, _T1>
687 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
688 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
692 struct __equal_to<const _T1, _T1>
694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
699 struct __equal_to<_T1, const _T1>
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
705 template <class _T1, class _T2 = _T1>
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
711 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
712 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
717 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
718 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
722 struct __less<_T1, _T1>
724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
725 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
729 struct __less<const _T1, _T1>
731 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
732 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
736 struct __less<_T1, const _T1>
738 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
739 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
742 template <class _Predicate>
743 class __invert // invert the sense of a comparison
748 _LIBCPP_INLINE_VISIBILITY __invert() {}
750 _LIBCPP_INLINE_VISIBILITY
751 explicit __invert(_Predicate __p) : __p_(__p) {}
754 _LIBCPP_INLINE_VISIBILITY
755 bool operator()(const _T1& __x) {return !__p_(__x);}
757 template <class _T1, class _T2>
758 _LIBCPP_INLINE_VISIBILITY
759 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
762 // Perform division by two quickly for positive integers (llvm.org/PR39129)
764 template <typename _Integral>
765 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
768 is_integral<_Integral>::value,
771 __half_positive(_Integral __value)
773 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
776 template <typename _Tp>
777 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
780 !is_integral<_Tp>::value,
783 __half_positive(_Tp __value)
790 template <class _Compare>
794 _LIBCPP_CONSTEXPR_AFTER_CXX17
795 __debug_less(_Compare& __c) : __comp_(__c) {}
797 template <class _Tp, class _Up>
798 _LIBCPP_CONSTEXPR_AFTER_CXX17
799 bool operator()(const _Tp& __x, const _Up& __y)
801 bool __r = __comp_(__x, __y);
803 __do_compare_assert(0, __y, __x);
807 template <class _Tp, class _Up>
808 _LIBCPP_CONSTEXPR_AFTER_CXX17
809 bool operator()(_Tp& __x, _Up& __y)
811 bool __r = __comp_(__x, __y);
813 __do_compare_assert(0, __y, __x);
817 template <class _LHS, class _RHS>
818 _LIBCPP_CONSTEXPR_AFTER_CXX17
819 inline _LIBCPP_INLINE_VISIBILITY
820 decltype((void)_VSTD::declval<_Compare&>()(
821 _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
822 __do_compare_assert(int, _LHS & __l, _RHS & __r) {
823 _LIBCPP_ASSERT(!__comp_(__l, __r),
824 "Comparator does not induce a strict weak ordering");
827 template <class _LHS, class _RHS>
828 _LIBCPP_CONSTEXPR_AFTER_CXX17
829 inline _LIBCPP_INLINE_VISIBILITY
830 void __do_compare_assert(long, _LHS &, _RHS &) {}
833 #endif // _LIBCPP_DEBUG
835 template <class _Comp>
836 struct __comp_ref_type {
837 // Pass the comparator by lvalue reference. Or in debug mode, using a
838 // debugging wrapper that stores a reference.
839 #ifndef _LIBCPP_DEBUG
840 typedef typename add_lvalue_reference<_Comp>::type type;
842 typedef __debug_less<_Comp> type;
848 template <class _InputIterator, class _Predicate>
849 _LIBCPP_NODISCARD_EXT inline
850 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
852 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
854 for (; __first != __last; ++__first)
855 if (!__pred(*__first))
862 template <class _InputIterator, class _Predicate>
863 _LIBCPP_NODISCARD_EXT inline
864 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
866 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
868 for (; __first != __last; ++__first)
869 if (__pred(*__first))
876 template <class _InputIterator, class _Predicate>
877 _LIBCPP_NODISCARD_EXT inline
878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
880 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
882 for (; __first != __last; ++__first)
883 if (__pred(*__first))
890 template <class _InputIterator, class _Function>
891 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
893 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
895 for (; __first != __last; ++__first)
900 #if _LIBCPP_STD_VER > 14
903 template <class _InputIterator, class _Size, class _Function>
904 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
906 for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
908 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
909 _IntegralSize __n = __orig_n;
922 template <class _InputIterator, class _Tp>
923 _LIBCPP_NODISCARD_EXT inline
924 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
926 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
928 for (; __first != __last; ++__first)
929 if (*__first == __value_)
936 template <class _InputIterator, class _Predicate>
937 _LIBCPP_NODISCARD_EXT inline
938 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
940 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
942 for (; __first != __last; ++__first)
943 if (__pred(*__first))
950 template<class _InputIterator, class _Predicate>
951 _LIBCPP_NODISCARD_EXT inline
952 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
954 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
956 for (; __first != __last; ++__first)
957 if (!__pred(*__first))
964 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
965 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
966 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
967 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
968 forward_iterator_tag, forward_iterator_tag)
970 // modeled after search algorithm
971 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
972 if (__first2 == __last2)
978 if (__first1 == __last1) // if source exhausted return last correct answer
979 return __r; // (or __last1 if never found)
980 if (__pred(*__first1, *__first2))
984 // *__first1 matches *__first2, now match elements after here
985 _ForwardIterator1 __m1 = __first1;
986 _ForwardIterator2 __m2 = __first2;
989 if (++__m2 == __last2)
990 { // Pattern exhaused, record answer and search for another one
995 if (++__m1 == __last1) // Source exhausted, return last answer
997 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1001 } // else there is a match, check next elements
1006 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1007 _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
1008 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1009 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1010 bidirectional_iterator_tag, bidirectional_iterator_tag)
1012 // modeled after search algorithm (in reverse)
1013 if (__first2 == __last2)
1014 return __last1; // Everything matches an empty sequence
1015 _BidirectionalIterator1 __l1 = __last1;
1016 _BidirectionalIterator2 __l2 = __last2;
1020 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1023 if (__first1 == __l1) // return __last1 if no element matches *__first2
1025 if (__pred(*--__l1, *__l2))
1028 // *__l1 matches *__l2, now match elements before here
1029 _BidirectionalIterator1 __m1 = __l1;
1030 _BidirectionalIterator2 __m2 = __l2;
1033 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1035 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1037 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1040 } // else there is a match, check next elements
1045 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1046 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1047 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1048 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1049 random_access_iterator_tag, random_access_iterator_tag)
1051 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1052 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1055 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1056 if (__len1 < __len2)
1058 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1059 _RandomAccessIterator1 __l1 = __last1;
1060 _RandomAccessIterator2 __l2 = __last2;
1068 if (__pred(*--__l1, *__l2))
1071 _RandomAccessIterator1 __m1 = __l1;
1072 _RandomAccessIterator2 __m2 = __l2;
1075 if (__m2 == __first2)
1077 // no need to check range on __m1 because __s guarantees we have enough source
1078 if (!__pred(*--__m1, *--__m2))
1086 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1087 _LIBCPP_NODISCARD_EXT inline
1088 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1090 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1091 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1093 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1094 (__first1, __last1, __first2, __last2, __pred,
1095 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1096 typename iterator_traits<_ForwardIterator2>::iterator_category());
1099 template <class _ForwardIterator1, class _ForwardIterator2>
1100 _LIBCPP_NODISCARD_EXT inline
1101 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1103 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1104 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1106 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1107 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1108 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1113 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1114 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1115 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1116 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1118 for (; __first1 != __last1; ++__first1)
1119 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1120 if (__pred(*__first1, *__j))
1126 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1127 _LIBCPP_NODISCARD_EXT inline
1128 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1130 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1131 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1133 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1136 template <class _ForwardIterator1, class _ForwardIterator2>
1137 _LIBCPP_NODISCARD_EXT inline
1138 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1140 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1141 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1143 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1144 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1145 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1150 template <class _ForwardIterator, class _BinaryPredicate>
1151 _LIBCPP_NODISCARD_EXT inline
1152 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1154 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1156 if (__first != __last)
1158 _ForwardIterator __i = __first;
1159 while (++__i != __last)
1161 if (__pred(*__first, *__i))
1169 template <class _ForwardIterator>
1170 _LIBCPP_NODISCARD_EXT inline
1171 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1173 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1175 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1176 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1181 template <class _InputIterator, class _Tp>
1182 _LIBCPP_NODISCARD_EXT inline
1183 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1184 typename iterator_traits<_InputIterator>::difference_type
1185 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1187 typename iterator_traits<_InputIterator>::difference_type __r(0);
1188 for (; __first != __last; ++__first)
1189 if (*__first == __value_)
1196 template <class _InputIterator, class _Predicate>
1197 _LIBCPP_NODISCARD_EXT inline
1198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1199 typename iterator_traits<_InputIterator>::difference_type
1200 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1202 typename iterator_traits<_InputIterator>::difference_type __r(0);
1203 for (; __first != __last; ++__first)
1204 if (__pred(*__first))
1211 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1212 _LIBCPP_NODISCARD_EXT inline
1213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1214 pair<_InputIterator1, _InputIterator2>
1215 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1216 _InputIterator2 __first2, _BinaryPredicate __pred)
1218 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1219 if (!__pred(*__first1, *__first2))
1221 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1224 template <class _InputIterator1, class _InputIterator2>
1225 _LIBCPP_NODISCARD_EXT inline
1226 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1227 pair<_InputIterator1, _InputIterator2>
1228 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1230 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1231 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1232 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1235 #if _LIBCPP_STD_VER > 11
1236 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237 _LIBCPP_NODISCARD_EXT inline
1238 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1239 pair<_InputIterator1, _InputIterator2>
1240 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1241 _InputIterator2 __first2, _InputIterator2 __last2,
1242 _BinaryPredicate __pred)
1244 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1245 if (!__pred(*__first1, *__first2))
1247 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1250 template <class _InputIterator1, class _InputIterator2>
1251 _LIBCPP_NODISCARD_EXT inline
1252 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1253 pair<_InputIterator1, _InputIterator2>
1254 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1255 _InputIterator2 __first2, _InputIterator2 __last2)
1257 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1258 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1259 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1265 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1266 _LIBCPP_NODISCARD_EXT inline
1267 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1269 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1271 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1272 if (!__pred(*__first1, *__first2))
1277 template <class _InputIterator1, class _InputIterator2>
1278 _LIBCPP_NODISCARD_EXT inline
1279 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1281 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1283 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1284 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1285 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1288 #if _LIBCPP_STD_VER > 11
1289 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1290 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1292 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1293 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1294 input_iterator_tag, input_iterator_tag )
1296 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1297 if (!__pred(*__first1, *__first2))
1299 return __first1 == __last1 && __first2 == __last2;
1302 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1303 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1305 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1306 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1307 random_access_iterator_tag, random_access_iterator_tag )
1309 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1311 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1312 typename add_lvalue_reference<_BinaryPredicate>::type>
1313 (__first1, __last1, __first2, __pred );
1316 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1317 _LIBCPP_NODISCARD_EXT inline
1318 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1320 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1321 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1323 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1324 (__first1, __last1, __first2, __last2, __pred,
1325 typename iterator_traits<_InputIterator1>::iterator_category(),
1326 typename iterator_traits<_InputIterator2>::iterator_category());
1329 template <class _InputIterator1, class _InputIterator2>
1330 _LIBCPP_NODISCARD_EXT inline
1331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1333 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1334 _InputIterator2 __first2, _InputIterator2 __last2)
1336 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1337 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1338 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1339 typename iterator_traits<_InputIterator1>::iterator_category(),
1340 typename iterator_traits<_InputIterator2>::iterator_category());
1346 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1347 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1348 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1349 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1351 // shorten sequences as much as possible by lopping of any equal prefix
1352 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1353 if (!__pred(*__first1, *__first2))
1355 if (__first1 == __last1)
1358 // __first1 != __last1 && *__first1 != *__first2
1359 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1360 _D1 __l1 = _VSTD::distance(__first1, __last1);
1363 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1364 // For each element in [f1, l1) see if there are the same number of
1365 // equal elements in [f2, l2)
1366 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1368 // Have we already counted the number of *__i in [f1, l1)?
1369 _ForwardIterator1 __match = __first1;
1370 for (; __match != __i; ++__match)
1371 if (__pred(*__match, *__i))
1373 if (__match == __i) {
1374 // Count number of *__i in [f2, l2)
1376 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1377 if (__pred(*__i, *__j))
1381 // Count number of *__i in [__i, l1) (we can start with 1)
1383 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1384 if (__pred(*__i, *__j))
1393 template<class _ForwardIterator1, class _ForwardIterator2>
1394 _LIBCPP_NODISCARD_EXT inline
1395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1397 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1398 _ForwardIterator2 __first2)
1400 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1401 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1402 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1405 #if _LIBCPP_STD_VER > 11
1406 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1407 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1408 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1409 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1410 _BinaryPredicate __pred,
1411 forward_iterator_tag, forward_iterator_tag )
1413 // shorten sequences as much as possible by lopping of any equal prefix
1414 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1415 if (!__pred(*__first1, *__first2))
1417 if (__first1 == __last1)
1418 return __first2 == __last2;
1419 else if (__first2 == __last2)
1422 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1423 _D1 __l1 = _VSTD::distance(__first1, __last1);
1425 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1426 _D2 __l2 = _VSTD::distance(__first2, __last2);
1430 // For each element in [f1, l1) see if there are the same number of
1431 // equal elements in [f2, l2)
1432 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1434 // Have we already counted the number of *__i in [f1, l1)?
1435 _ForwardIterator1 __match = __first1;
1436 for (; __match != __i; ++__match)
1437 if (__pred(*__match, *__i))
1439 if (__match == __i) {
1440 // Count number of *__i in [f2, l2)
1442 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1443 if (__pred(*__i, *__j))
1447 // Count number of *__i in [__i, l1) (we can start with 1)
1449 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1450 if (__pred(*__i, *__j))
1459 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1460 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1461 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1462 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1463 _BinaryPredicate __pred,
1464 random_access_iterator_tag, random_access_iterator_tag )
1466 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1468 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1469 typename add_lvalue_reference<_BinaryPredicate>::type>
1470 (__first1, __last1, __first2, __pred );
1473 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1474 _LIBCPP_NODISCARD_EXT inline
1475 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1477 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1478 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1479 _BinaryPredicate __pred )
1481 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1482 (__first1, __last1, __first2, __last2, __pred,
1483 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1484 typename iterator_traits<_ForwardIterator2>::iterator_category());
1487 template<class _ForwardIterator1, class _ForwardIterator2>
1488 _LIBCPP_NODISCARD_EXT inline
1489 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1491 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1492 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1494 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1495 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1496 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1497 __equal_to<__v1, __v2>(),
1498 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1499 typename iterator_traits<_ForwardIterator2>::iterator_category());
1504 // __search is in <functional>
1506 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1507 _LIBCPP_NODISCARD_EXT inline
1508 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1510 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1511 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1513 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1514 (__first1, __last1, __first2, __last2, __pred,
1515 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1516 typename iterator_traits<_ForwardIterator2>::iterator_category())
1520 template <class _ForwardIterator1, class _ForwardIterator2>
1521 _LIBCPP_NODISCARD_EXT inline
1522 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1524 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1525 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1527 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1528 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1529 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1533 #if _LIBCPP_STD_VER > 14
1534 template <class _ForwardIterator, class _Searcher>
1535 _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1536 _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1537 { return __s(__f, __l).first; }
1542 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1543 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
1544 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1545 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1551 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1554 if (__first == __last) // return __last if no element matches __value_
1556 if (__pred(*__first, __value_))
1560 // *__first matches __value_, now match elements after here
1561 _ForwardIterator __m = __first;
1565 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1567 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1569 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1574 } // else there is a match, check next elements
1579 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1580 _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
1581 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1582 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1586 _Size __len = static_cast<_Size>(__last - __first);
1587 if (__len < __count)
1589 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1592 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1595 if (__first >= __s) // return __last if no element matches __value_
1597 if (__pred(*__first, __value_))
1601 // *__first matches __value_, now match elements after here
1602 _RandomAccessIterator __m = __first;
1606 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1608 ++__m; // no need to check range on __m because __s guarantees we have enough source
1609 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1614 } // else there is a match, check next elements
1619 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1620 _LIBCPP_NODISCARD_EXT inline
1621 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1623 search_n(_ForwardIterator __first, _ForwardIterator __last,
1624 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1626 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1627 (__first, __last, _VSTD::__convert_to_integral(__count), __value_, __pred,
1628 typename iterator_traits<_ForwardIterator>::iterator_category());
1631 template <class _ForwardIterator, class _Size, class _Tp>
1632 _LIBCPP_NODISCARD_EXT inline
1633 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1635 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1637 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1638 return _VSTD::search_n(__first, __last, _VSTD::__convert_to_integral(__count),
1639 __value_, __equal_to<__v, _Tp>());
1643 template <class _Iter>
1644 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1646 __unwrap_iter(_Iter __i)
1651 template <class _Tp>
1652 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1655 is_trivially_copy_assignable<_Tp>::value,
1658 __unwrap_iter(move_iterator<_Tp*> __i)
1663 #if _LIBCPP_DEBUG_LEVEL < 2
1665 template <class _Tp>
1666 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1669 is_trivially_copy_assignable<_Tp>::value,
1672 __unwrap_iter(__wrap_iter<_Tp*> __i)
1677 template <class _Tp>
1678 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1681 is_trivially_copy_assignable<_Tp>::value,
1684 __unwrap_iter(__wrap_iter<const _Tp*> __i)
1691 template <class _Tp>
1692 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1695 is_trivially_copy_assignable<_Tp>::value,
1698 __unwrap_iter(__wrap_iter<_Tp*> __i)
1703 #endif // _LIBCPP_DEBUG_LEVEL < 2
1705 template <class _InputIterator, class _OutputIterator>
1706 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1708 __copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1710 for (; __first != __last; ++__first, (void) ++__result)
1711 *__result = *__first;
1715 template <class _InputIterator, class _OutputIterator>
1716 inline _LIBCPP_INLINE_VISIBILITY
1718 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1720 return _VSTD::__copy_constexpr(__first, __last, __result);
1723 template <class _Tp, class _Up>
1724 inline _LIBCPP_INLINE_VISIBILITY
1727 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1728 is_trivially_copy_assignable<_Up>::value,
1731 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1733 const size_t __n = static_cast<size_t>(__last - __first);
1735 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1736 return __result + __n;
1739 template <class _InputIterator, class _OutputIterator>
1740 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1742 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1744 if (__libcpp_is_constant_evaluated()) {
1745 return _VSTD::__copy_constexpr(
1746 _VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
1748 return _VSTD::__copy(
1749 _VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
1755 template <class _BidirectionalIterator, class _OutputIterator>
1756 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1758 __copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1760 while (__first != __last)
1761 *--__result = *--__last;
1765 template <class _BidirectionalIterator, class _OutputIterator>
1766 inline _LIBCPP_INLINE_VISIBILITY
1768 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1770 return _VSTD::__copy_backward_constexpr(__first, __last, __result);
1773 template <class _Tp, class _Up>
1774 inline _LIBCPP_INLINE_VISIBILITY
1777 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1778 is_trivially_copy_assignable<_Up>::value,
1781 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1783 const size_t __n = static_cast<size_t>(__last - __first);
1787 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1792 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1793 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1794 _BidirectionalIterator2
1795 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1796 _BidirectionalIterator2 __result)
1798 if (__libcpp_is_constant_evaluated()) {
1799 return _VSTD::__copy_backward_constexpr(_VSTD::__unwrap_iter(__first),
1800 _VSTD::__unwrap_iter(__last),
1801 _VSTD::__unwrap_iter(__result));
1803 return _VSTD::__copy_backward(_VSTD::__unwrap_iter(__first),
1804 _VSTD::__unwrap_iter(__last),
1805 _VSTD::__unwrap_iter(__result));
1811 template<class _InputIterator, class _OutputIterator, class _Predicate>
1812 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1814 copy_if(_InputIterator __first, _InputIterator __last,
1815 _OutputIterator __result, _Predicate __pred)
1817 for (; __first != __last; ++__first)
1819 if (__pred(*__first))
1821 *__result = *__first;
1830 template<class _InputIterator, class _Size, class _OutputIterator>
1831 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1834 __is_cpp17_input_iterator<_InputIterator>::value &&
1835 !__is_cpp17_random_access_iterator<_InputIterator>::value,
1838 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1840 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
1841 _IntegralSize __n = __orig_n;
1844 *__result = *__first;
1846 for (--__n; __n > 0; --__n)
1849 *__result = *__first;
1856 template<class _InputIterator, class _Size, class _OutputIterator>
1857 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1860 __is_cpp17_random_access_iterator<_InputIterator>::value,
1863 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1865 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
1866 _IntegralSize __n = __orig_n;
1867 return _VSTD::copy(__first, __first + __n, __result);
1872 // __move_constexpr exists so that __move doesn't call itself when delegating to the constexpr
1873 // version of __move.
1874 template <class _InputIterator, class _OutputIterator>
1875 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1877 __move_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1879 for (; __first != __last; ++__first, (void) ++__result)
1880 *__result = _VSTD::move(*__first);
1884 template <class _InputIterator, class _OutputIterator>
1885 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1887 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1889 return _VSTD::__move_constexpr(__first, __last, __result);
1892 template <class _Tp, class _Up>
1893 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1896 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1897 is_trivially_copy_assignable<_Up>::value,
1900 __move(_Tp* __first, _Tp* __last, _Up* __result)
1902 if (__libcpp_is_constant_evaluated())
1903 return _VSTD::__move_constexpr(__first, __last, __result);
1904 const size_t __n = static_cast<size_t>(__last - __first);
1906 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1907 return __result + __n;
1910 template <class _InputIterator, class _OutputIterator>
1911 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1913 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1915 return _VSTD::__move(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
1920 // __move_backward_constexpr exists so that __move_backward doesn't call itself when delegating to
1921 // the constexpr version of __move_backward.
1922 template <class _InputIterator, class _OutputIterator>
1923 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1925 __move_backward_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1927 while (__first != __last)
1928 *--__result = _VSTD::move(*--__last);
1932 template <class _InputIterator, class _OutputIterator>
1933 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1935 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1937 return _VSTD::__move_backward_constexpr(__first, __last, __result);
1940 template <class _Tp, class _Up>
1941 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1944 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1945 is_trivially_copy_assignable<_Up>::value,
1948 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1950 if (__libcpp_is_constant_evaluated())
1951 return _VSTD::__move_backward_constexpr(__first, __last, __result);
1952 const size_t __n = static_cast<size_t>(__last - __first);
1956 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1961 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1962 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1963 _BidirectionalIterator2
1964 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1965 _BidirectionalIterator2 __result)
1967 return _VSTD::__move_backward(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
1972 // moved to <type_traits> for better swap / noexcept support
1976 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1977 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1979 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1981 for (; __first != __last; ++__first, (void) ++__result)
1982 *__result = __op(*__first);
1986 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1987 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1989 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1990 _OutputIterator __result, _BinaryOperation __binary_op)
1992 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1993 *__result = __binary_op(*__first1, *__first2);
1999 template <class _ForwardIterator, class _Tp>
2000 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2002 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2004 for (; __first != __last; ++__first)
2005 if (*__first == __old_value)
2006 *__first = __new_value;
2011 template <class _ForwardIterator, class _Predicate, class _Tp>
2012 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2014 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2016 for (; __first != __last; ++__first)
2017 if (__pred(*__first))
2018 *__first = __new_value;
2023 template <class _InputIterator, class _OutputIterator, class _Tp>
2024 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2026 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2027 const _Tp& __old_value, const _Tp& __new_value)
2029 for (; __first != __last; ++__first, (void) ++__result)
2030 if (*__first == __old_value)
2031 *__result = __new_value;
2033 *__result = *__first;
2039 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2040 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2042 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2043 _Predicate __pred, const _Tp& __new_value)
2045 for (; __first != __last; ++__first, (void) ++__result)
2046 if (__pred(*__first))
2047 *__result = __new_value;
2049 *__result = *__first;
2055 template <class _OutputIterator, class _Size, class _Tp>
2056 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2058 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2060 for (; __n > 0; ++__first, (void) --__n)
2061 *__first = __value_;
2065 template <class _OutputIterator, class _Size, class _Tp>
2066 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2068 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2070 return _VSTD::__fill_n(__first, _VSTD::__convert_to_integral(__n), __value_);
2075 template <class _ForwardIterator, class _Tp>
2076 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2078 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2080 for (; __first != __last; ++__first)
2081 *__first = __value_;
2084 template <class _RandomAccessIterator, class _Tp>
2085 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2087 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2089 _VSTD::fill_n(__first, __last - __first, __value_);
2092 template <class _ForwardIterator, class _Tp>
2093 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2095 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2097 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2102 template <class _ForwardIterator, class _Generator>
2103 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2105 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2107 for (; __first != __last; ++__first)
2113 template <class _OutputIterator, class _Size, class _Generator>
2114 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2116 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2118 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
2119 _IntegralSize __n = __orig_n;
2120 for (; __n > 0; ++__first, (void) --__n)
2127 template <class _ForwardIterator, class _Tp>
2128 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2129 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2131 __first = _VSTD::find(__first, __last, __value_);
2132 if (__first != __last)
2134 _ForwardIterator __i = __first;
2135 while (++__i != __last)
2137 if (!(*__i == __value_))
2139 *__first = _VSTD::move(*__i);
2149 template <class _ForwardIterator, class _Predicate>
2150 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2151 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2153 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2154 (__first, __last, __pred);
2155 if (__first != __last)
2157 _ForwardIterator __i = __first;
2158 while (++__i != __last)
2162 *__first = _VSTD::move(*__i);
2172 template <class _InputIterator, class _OutputIterator, class _Tp>
2173 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2175 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2177 for (; __first != __last; ++__first)
2179 if (!(*__first == __value_))
2181 *__result = *__first;
2190 template <class _InputIterator, class _OutputIterator, class _Predicate>
2191 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2193 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2195 for (; __first != __last; ++__first)
2197 if (!__pred(*__first))
2199 *__result = *__first;
2208 template <class _ForwardIterator, class _BinaryPredicate>
2209 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2210 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2212 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2213 (__first, __last, __pred);
2214 if (__first != __last)
2218 _ForwardIterator __i = __first;
2219 for (++__i; ++__i != __last;)
2220 if (!__pred(*__first, *__i))
2221 *++__first = _VSTD::move(*__i);
2227 template <class _ForwardIterator>
2228 _LIBCPP_NODISCARD_EXT inline
2229 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2231 unique(_ForwardIterator __first, _ForwardIterator __last)
2233 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2234 return _VSTD::unique(__first, __last, __equal_to<__v>());
2239 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2240 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2241 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2242 input_iterator_tag, output_iterator_tag)
2244 if (__first != __last)
2246 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2249 while (++__first != __last)
2251 if (!__pred(__t, *__first))
2262 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2263 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2264 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2265 forward_iterator_tag, output_iterator_tag)
2267 if (__first != __last)
2269 _ForwardIterator __i = __first;
2272 while (++__first != __last)
2274 if (!__pred(*__i, *__first))
2276 *__result = *__first;
2285 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2286 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2287 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2288 input_iterator_tag, forward_iterator_tag)
2290 if (__first != __last)
2292 *__result = *__first;
2293 while (++__first != __last)
2294 if (!__pred(*__result, *__first))
2295 *++__result = *__first;
2301 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2302 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2304 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2306 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2307 (__first, __last, __result, __pred,
2308 typename iterator_traits<_InputIterator>::iterator_category(),
2309 typename iterator_traits<_OutputIterator>::iterator_category());
2312 template <class _InputIterator, class _OutputIterator>
2313 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2315 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2317 typedef typename iterator_traits<_InputIterator>::value_type __v;
2318 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2323 template <class _BidirectionalIterator>
2324 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2326 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2328 while (__first != __last)
2330 if (__first == --__last)
2332 _VSTD::iter_swap(__first, __last);
2337 template <class _RandomAccessIterator>
2338 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2340 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2342 if (__first != __last)
2343 for (; __first < --__last; ++__first)
2344 _VSTD::iter_swap(__first, __last);
2347 template <class _BidirectionalIterator>
2348 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2350 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2352 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2357 template <class _BidirectionalIterator, class _OutputIterator>
2358 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2360 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2362 for (; __first != __last; ++__result)
2363 *__result = *--__last;
2369 template <class _ForwardIterator>
2370 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
2371 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2373 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2374 value_type __tmp = _VSTD::move(*__first);
2375 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2376 *__lm1 = _VSTD::move(__tmp);
2380 template <class _BidirectionalIterator>
2381 _LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
2382 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2384 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2385 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2386 value_type __tmp = _VSTD::move(*__lm1);
2387 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2388 *__first = _VSTD::move(__tmp);
2392 template <class _ForwardIterator>
2393 _LIBCPP_CONSTEXPR_AFTER_CXX14 _ForwardIterator
2394 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2396 _ForwardIterator __i = __middle;
2399 swap(*__first, *__i);
2401 if (++__i == __last)
2403 if (__first == __middle)
2406 _ForwardIterator __r = __first;
2407 if (__first != __middle)
2412 swap(*__first, *__i);
2414 if (++__i == __last)
2416 if (__first == __middle)
2420 else if (__first == __middle)
2427 template<typename _Integral>
2428 inline _LIBCPP_INLINE_VISIBILITY
2429 _LIBCPP_CONSTEXPR_AFTER_CXX14 _Integral
2430 __algo_gcd(_Integral __x, _Integral __y)
2434 _Integral __t = __x % __y;
2441 template<typename _RandomAccessIterator>
2442 _LIBCPP_CONSTEXPR_AFTER_CXX14 _RandomAccessIterator
2443 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2445 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2446 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2448 const difference_type __m1 = __middle - __first;
2449 const difference_type __m2 = __last - __middle;
2452 _VSTD::swap_ranges(__first, __middle, __middle);
2455 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
2456 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2458 value_type __t(_VSTD::move(*--__p));
2459 _RandomAccessIterator __p1 = __p;
2460 _RandomAccessIterator __p2 = __p1 + __m1;
2463 *__p1 = _VSTD::move(*__p2);
2465 const difference_type __d = __last - __p2;
2469 __p2 = __first + (__m1 - __d);
2470 } while (__p2 != __p);
2471 *__p1 = _VSTD::move(__t);
2473 return __first + __m2;
2476 template <class _ForwardIterator>
2477 inline _LIBCPP_INLINE_VISIBILITY
2478 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
2479 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2480 _VSTD::forward_iterator_tag)
2482 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2483 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2485 if (_VSTD::next(__first) == __middle)
2486 return _VSTD::__rotate_left(__first, __last);
2488 return _VSTD::__rotate_forward(__first, __middle, __last);
2491 template <class _BidirectionalIterator>
2492 inline _LIBCPP_INLINE_VISIBILITY
2493 _LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
2494 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2495 _VSTD::bidirectional_iterator_tag)
2497 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2498 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2500 if (_VSTD::next(__first) == __middle)
2501 return _VSTD::__rotate_left(__first, __last);
2502 if (_VSTD::next(__middle) == __last)
2503 return _VSTD::__rotate_right(__first, __last);
2505 return _VSTD::__rotate_forward(__first, __middle, __last);
2508 template <class _RandomAccessIterator>
2509 inline _LIBCPP_INLINE_VISIBILITY
2510 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
2511 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2512 _VSTD::random_access_iterator_tag)
2514 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2515 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2517 if (_VSTD::next(__first) == __middle)
2518 return _VSTD::__rotate_left(__first, __last);
2519 if (_VSTD::next(__middle) == __last)
2520 return _VSTD::__rotate_right(__first, __last);
2521 return _VSTD::__rotate_gcd(__first, __middle, __last);
2523 return _VSTD::__rotate_forward(__first, __middle, __last);
2526 template <class _ForwardIterator>
2527 inline _LIBCPP_INLINE_VISIBILITY
2528 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2529 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2531 if (__first == __middle)
2533 if (__middle == __last)
2535 return _VSTD::__rotate(__first, __middle, __last,
2536 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2541 template <class _ForwardIterator, class _OutputIterator>
2542 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2544 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2546 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2551 template <class _ForwardIterator, class _Compare>
2552 _LIBCPP_NODISCARD_EXT inline
2553 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2555 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2557 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2558 "std::min_element requires a ForwardIterator");
2559 if (__first != __last)
2561 _ForwardIterator __i = __first;
2562 while (++__i != __last)
2563 if (__comp(*__i, *__first))
2569 template <class _ForwardIterator>
2570 _LIBCPP_NODISCARD_EXT inline
2571 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2573 min_element(_ForwardIterator __first, _ForwardIterator __last)
2575 return _VSTD::min_element(__first, __last,
2576 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2581 template <class _Tp, class _Compare>
2582 _LIBCPP_NODISCARD_EXT inline
2583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2585 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2587 return __comp(__b, __a) ? __b : __a;
2590 template <class _Tp>
2591 _LIBCPP_NODISCARD_EXT inline
2592 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2594 min(const _Tp& __a, const _Tp& __b)
2596 return _VSTD::min(__a, __b, __less<_Tp>());
2599 #ifndef _LIBCPP_CXX03_LANG
2601 template<class _Tp, class _Compare>
2602 _LIBCPP_NODISCARD_EXT inline
2603 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2605 min(initializer_list<_Tp> __t, _Compare __comp)
2607 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2611 _LIBCPP_NODISCARD_EXT inline
2612 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2614 min(initializer_list<_Tp> __t)
2616 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2619 #endif // _LIBCPP_CXX03_LANG
2623 template <class _ForwardIterator, class _Compare>
2624 _LIBCPP_NODISCARD_EXT inline
2625 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2627 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2629 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2630 "std::max_element requires a ForwardIterator");
2631 if (__first != __last)
2633 _ForwardIterator __i = __first;
2634 while (++__i != __last)
2635 if (__comp(*__first, *__i))
2642 template <class _ForwardIterator>
2643 _LIBCPP_NODISCARD_EXT inline
2644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2646 max_element(_ForwardIterator __first, _ForwardIterator __last)
2648 return _VSTD::max_element(__first, __last,
2649 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2654 template <class _Tp, class _Compare>
2655 _LIBCPP_NODISCARD_EXT inline
2656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2658 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2660 return __comp(__a, __b) ? __b : __a;
2663 template <class _Tp>
2664 _LIBCPP_NODISCARD_EXT inline
2665 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2667 max(const _Tp& __a, const _Tp& __b)
2669 return _VSTD::max(__a, __b, __less<_Tp>());
2672 #ifndef _LIBCPP_CXX03_LANG
2674 template<class _Tp, class _Compare>
2675 _LIBCPP_NODISCARD_EXT inline
2676 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2678 max(initializer_list<_Tp> __t, _Compare __comp)
2680 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2684 _LIBCPP_NODISCARD_EXT inline
2685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2687 max(initializer_list<_Tp> __t)
2689 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2692 #endif // _LIBCPP_CXX03_LANG
2694 #if _LIBCPP_STD_VER > 14
2696 template<class _Tp, class _Compare>
2697 _LIBCPP_NODISCARD_EXT inline
2698 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2700 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2702 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2703 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2708 _LIBCPP_NODISCARD_EXT inline
2709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2711 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2713 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2719 template <class _ForwardIterator, class _Compare>
2720 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
2721 pair<_ForwardIterator, _ForwardIterator>
2722 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2724 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2725 "std::minmax_element requires a ForwardIterator");
2726 pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2727 if (__first != __last)
2729 if (++__first != __last)
2731 if (__comp(*__first, *__result.first))
2732 __result.first = __first;
2734 __result.second = __first;
2735 while (++__first != __last)
2737 _ForwardIterator __i = __first;
2738 if (++__first == __last)
2740 if (__comp(*__i, *__result.first))
2741 __result.first = __i;
2742 else if (!__comp(*__i, *__result.second))
2743 __result.second = __i;
2748 if (__comp(*__first, *__i))
2750 if (__comp(*__first, *__result.first))
2751 __result.first = __first;
2752 if (!__comp(*__i, *__result.second))
2753 __result.second = __i;
2757 if (__comp(*__i, *__result.first))
2758 __result.first = __i;
2759 if (!__comp(*__first, *__result.second))
2760 __result.second = __first;
2769 template <class _ForwardIterator>
2770 _LIBCPP_NODISCARD_EXT inline
2771 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2772 pair<_ForwardIterator, _ForwardIterator>
2773 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2775 return _VSTD::minmax_element(__first, __last,
2776 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2781 template<class _Tp, class _Compare>
2782 _LIBCPP_NODISCARD_EXT inline
2783 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2784 pair<const _Tp&, const _Tp&>
2785 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2787 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2788 pair<const _Tp&, const _Tp&>(__a, __b);
2792 _LIBCPP_NODISCARD_EXT inline
2793 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2794 pair<const _Tp&, const _Tp&>
2795 minmax(const _Tp& __a, const _Tp& __b)
2797 return _VSTD::minmax(__a, __b, __less<_Tp>());
2800 #ifndef _LIBCPP_CXX03_LANG
2802 template<class _Tp, class _Compare>
2803 _LIBCPP_NODISCARD_EXT inline
2804 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2806 minmax(initializer_list<_Tp> __t, _Compare __comp)
2808 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2809 _Iter __first = __t.begin();
2810 _Iter __last = __t.end();
2811 pair<_Tp, _Tp> __result(*__first, *__first);
2814 if (__t.size() % 2 == 0)
2816 if (__comp(*__first, __result.first))
2817 __result.first = *__first;
2819 __result.second = *__first;
2823 while (__first != __last)
2825 _Tp __prev = *__first++;
2826 if (__comp(*__first, __prev)) {
2827 if ( __comp(*__first, __result.first)) __result.first = *__first;
2828 if (!__comp(__prev, __result.second)) __result.second = __prev;
2831 if ( __comp(__prev, __result.first)) __result.first = __prev;
2832 if (!__comp(*__first, __result.second)) __result.second = *__first;
2841 _LIBCPP_NODISCARD_EXT inline
2842 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2844 minmax(initializer_list<_Tp> __t)
2846 return _VSTD::minmax(__t, __less<_Tp>());
2849 #endif // _LIBCPP_CXX03_LANG
2853 // __independent_bits_engine
2855 template <unsigned long long _Xp, size_t _Rp>
2858 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2859 : __log2_imp<_Xp, _Rp - 1>::value;
2862 template <unsigned long long _Xp>
2863 struct __log2_imp<_Xp, 0>
2865 static const size_t value = 0;
2868 template <size_t _Rp>
2869 struct __log2_imp<0, _Rp>
2871 static const size_t value = _Rp + 1;
2874 template <class _UIntType, _UIntType _Xp>
2877 static const size_t value = __log2_imp<_Xp,
2878 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
2881 template<class _Engine, class _UIntType>
2882 class __independent_bits_engine
2886 typedef _UIntType result_type;
2889 typedef typename _Engine::result_type _Engine_result_type;
2890 typedef typename conditional
2892 sizeof(_Engine_result_type) <= sizeof(result_type),
2895 >::type _Working_result_type;
2902 _Working_result_type __y0_;
2903 _Working_result_type __y1_;
2904 _Engine_result_type __mask0_;
2905 _Engine_result_type __mask1_;
2907 #ifdef _LIBCPP_CXX03_LANG
2908 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2909 + _Working_result_type(1);
2911 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2912 + _Working_result_type(1);
2914 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2915 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2916 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2919 // constructors and seeding functions
2920 __independent_bits_engine(_Engine& __e, size_t __w);
2922 // generating functions
2923 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2926 result_type __eval(false_type);
2927 result_type __eval(true_type);
2930 template<class _Engine, class _UIntType>
2931 __independent_bits_engine<_Engine, _UIntType>
2932 ::__independent_bits_engine(_Engine& __e, size_t __w)
2936 __n_ = __w_ / __m + (__w_ % __m != 0);
2937 __w0_ = __w_ / __n_;
2940 else if (__w0_ < _WDt)
2941 __y0_ = (_Rp >> __w0_) << __w0_;
2944 if (_Rp - __y0_ > __y0_ / __n_)
2947 __w0_ = __w_ / __n_;
2949 __y0_ = (_Rp >> __w0_) << __w0_;
2953 __n0_ = __n_ - __w_ % __n_;
2954 if (__w0_ < _WDt - 1)
2955 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2958 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2959 _Engine_result_type(0);
2960 __mask1_ = __w0_ < _EDt - 1 ?
2961 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2962 _Engine_result_type(~0);
2965 template<class _Engine, class _UIntType>
2968 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2970 return static_cast<result_type>(__e_() & __mask0_);
2973 template<class _Engine, class _UIntType>
2975 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2977 const size_t _WRt = numeric_limits<result_type>::digits;
2978 result_type _Sp = 0;
2979 for (size_t __k = 0; __k < __n0_; ++__k)
2981 _Engine_result_type __u;
2984 __u = __e_() - _Engine::min();
2985 } while (__u >= __y0_);
2990 _Sp += __u & __mask0_;
2992 for (size_t __k = __n0_; __k < __n_; ++__k)
2994 _Engine_result_type __u;
2997 __u = __e_() - _Engine::min();
2998 } while (__u >= __y1_);
2999 if (__w0_ < _WRt - 1)
3003 _Sp += __u & __mask1_;
3008 // uniform_int_distribution
3010 template<class _IntType = int>
3011 class uniform_int_distribution
3015 typedef _IntType result_type;
3022 typedef uniform_int_distribution distribution_type;
3024 explicit param_type(result_type __a = 0,
3025 result_type __b = numeric_limits<result_type>::max())
3026 : __a_(__a), __b_(__b) {}
3028 result_type a() const {return __a_;}
3029 result_type b() const {return __b_;}
3031 friend bool operator==(const param_type& __x, const param_type& __y)
3032 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3033 friend bool operator!=(const param_type& __x, const param_type& __y)
3034 {return !(__x == __y);}
3041 // constructors and reset functions
3042 #ifndef _LIBCPP_CXX03_LANG
3043 uniform_int_distribution() : uniform_int_distribution(0) {}
3044 explicit uniform_int_distribution(
3045 result_type __a, result_type __b = numeric_limits<result_type>::max())
3046 : __p_(param_type(__a, __b)) {}
3048 explicit uniform_int_distribution(
3049 result_type __a = 0,
3050 result_type __b = numeric_limits<result_type>::max())
3051 : __p_(param_type(__a, __b)) {}
3053 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3056 // generating functions
3057 template<class _URNG> result_type operator()(_URNG& __g)
3058 {return (*this)(__g, __p_);}
3059 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3061 // property functions
3062 result_type a() const {return __p_.a();}
3063 result_type b() const {return __p_.b();}
3065 param_type param() const {return __p_;}
3066 void param(const param_type& __p) {__p_ = __p;}
3068 result_type min() const {return a();}
3069 result_type max() const {return b();}
3071 friend bool operator==(const uniform_int_distribution& __x,
3072 const uniform_int_distribution& __y)
3073 {return __x.__p_ == __y.__p_;}
3074 friend bool operator!=(const uniform_int_distribution& __x,
3075 const uniform_int_distribution& __y)
3076 {return !(__x == __y);}
3079 template<class _IntType>
3080 template<class _URNG>
3081 typename uniform_int_distribution<_IntType>::result_type
3082 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3083 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
3085 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3086 uint32_t, uint64_t>::type _UIntType;
3087 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
3090 const size_t _Dt = numeric_limits<_UIntType>::digits;
3091 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3093 return static_cast<result_type>(_Eng(__g, _Dt)());
3094 size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
3095 if ((_Rp & (numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3102 } while (__u >= _Rp);
3103 return static_cast<result_type>(__u + __p.a());
3106 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3107 || defined(_LIBCPP_BUILDING_LIBRARY)
3108 class _LIBCPP_TYPE_VIS __rs_default;
3110 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3112 class _LIBCPP_TYPE_VIS __rs_default
3114 static unsigned __c_;
3118 typedef uint_fast32_t result_type;
3120 static const result_type _Min = 0;
3121 static const result_type _Max = 0xFFFFFFFF;
3123 __rs_default(const __rs_default&);
3126 result_type operator()();
3128 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3129 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3131 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3134 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3136 template <class _RandomAccessIterator>
3137 _LIBCPP_DEPRECATED_IN_CXX14 void
3138 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3140 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3141 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3142 typedef typename _Dp::param_type _Pp;
3143 difference_type __d = __last - __first;
3147 __rs_default __g = __rs_get();
3148 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3150 difference_type __i = __uid(__g, _Pp(0, __d));
3151 if (__i != difference_type(0))
3152 swap(*__first, *(__first + __i));
3157 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3158 _LIBCPP_DEPRECATED_IN_CXX14 void
3159 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3160 #ifndef _LIBCPP_CXX03_LANG
3161 _RandomNumberGenerator&& __rand)
3163 _RandomNumberGenerator& __rand)
3166 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3167 difference_type __d = __last - __first;
3170 for (--__last; __first < __last; ++__first, (void) --__d)
3172 difference_type __i = __rand(__d);
3173 if (__i != difference_type(0))
3174 swap(*__first, *(__first + __i));
3180 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3181 class _UniformRandomNumberGenerator>
3182 _LIBCPP_INLINE_VISIBILITY
3183 _SampleIterator __sample(_PopulationIterator __first,
3184 _PopulationIterator __last, _SampleIterator __output_iter,
3186 _UniformRandomNumberGenerator & __g,
3187 input_iterator_tag) {
3190 for (; __first != __last && __k < __n; ++__first, (void) ++__k)
3191 __output_iter[__k] = *__first;
3192 _Distance __sz = __k;
3193 for (; __first != __last; ++__first, (void) ++__k) {
3194 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3196 __output_iter[__r] = *__first;
3198 return __output_iter + _VSTD::min(__n, __k);
3201 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3202 class _UniformRandomNumberGenerator>
3203 _LIBCPP_INLINE_VISIBILITY
3204 _SampleIterator __sample(_PopulationIterator __first,
3205 _PopulationIterator __last, _SampleIterator __output_iter,
3207 _UniformRandomNumberGenerator& __g,
3208 forward_iterator_tag) {
3209 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3210 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3212 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3214 *__output_iter++ = *__first;
3218 return __output_iter;
3221 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3222 class _UniformRandomNumberGenerator>
3223 _LIBCPP_INLINE_VISIBILITY
3224 _SampleIterator __sample(_PopulationIterator __first,
3225 _PopulationIterator __last, _SampleIterator __output_iter,
3226 _Distance __n, _UniformRandomNumberGenerator& __g) {
3227 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3229 typedef typename iterator_traits<_PopulationIterator>::difference_type
3231 static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
3232 __is_cpp17_random_access_iterator<_SampleIterator>::value,
3233 "SampleIterator must meet the requirements of RandomAccessIterator");
3234 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3235 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3236 return _VSTD::__sample(
3237 __first, __last, __output_iter, _CommonType(__n),
3238 __g, _PopCategory());
3241 #if _LIBCPP_STD_VER > 14
3242 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3243 class _UniformRandomNumberGenerator>
3244 inline _LIBCPP_INLINE_VISIBILITY
3245 _SampleIterator sample(_PopulationIterator __first,
3246 _PopulationIterator __last, _SampleIterator __output_iter,
3247 _Distance __n, _UniformRandomNumberGenerator&& __g) {
3248 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
3250 #endif // _LIBCPP_STD_VER > 14
3252 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3253 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3254 _UniformRandomNumberGenerator&& __g)
3256 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3257 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3258 typedef typename _Dp::param_type _Pp;
3259 difference_type __d = __last - __first;
3263 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3265 difference_type __i = __uid(__g, _Pp(0, __d));
3266 if (__i != difference_type(0))
3267 swap(*__first, *(__first + __i));
3272 #if _LIBCPP_STD_VER > 17
3274 // shift_left, shift_right
3276 template <class _ForwardIterator>
3277 inline _LIBCPP_INLINE_VISIBILITY constexpr
3279 shift_left(_ForwardIterator __first, _ForwardIterator __last,
3280 typename iterator_traits<_ForwardIterator>::difference_type __n)
3286 _ForwardIterator __m = __first;
3287 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3288 if (__n >= __last - __first) {
3293 for (; __n > 0; --__n) {
3294 if (__m == __last) {
3300 return _VSTD::move(__m, __last, __first);
3303 template <class _ForwardIterator>
3304 inline _LIBCPP_INLINE_VISIBILITY constexpr
3306 shift_right(_ForwardIterator __first, _ForwardIterator __last,
3307 typename iterator_traits<_ForwardIterator>::difference_type __n)
3313 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3314 decltype(__n) __d = __last - __first;
3318 _ForwardIterator __m = __first + (__d - __n);
3319 return _VSTD::move_backward(__first, __m, __last);
3320 } else if constexpr (__is_cpp17_bidirectional_iterator<_ForwardIterator>::value) {
3321 _ForwardIterator __m = __last;
3322 for (; __n > 0; --__n) {
3323 if (__m == __first) {
3328 return _VSTD::move_backward(__first, __m, __last);
3330 _ForwardIterator __ret = __first;
3331 for (; __n > 0; --__n) {
3332 if (__ret == __last) {
3338 // We have an __n-element scratch space from __first to __ret.
3339 // Slide an __n-element window [__trail, __lead) from left to right.
3340 // We're essentially doing swap_ranges(__first, __ret, __trail, __lead)
3341 // over and over; but once __lead reaches __last we needn't bother
3342 // to save the values of elements [__trail, __last).
3344 auto __trail = __first;
3345 auto __lead = __ret;
3346 while (__trail != __ret) {
3347 if (__lead == __last) {
3348 _VSTD::move(__first, __trail, __ret);
3355 _ForwardIterator __mid = __first;
3357 if (__lead == __last) {
3358 __trail = _VSTD::move(__mid, __ret, __trail);
3359 _VSTD::move(__first, __mid, __trail);
3362 swap(*__mid, *__trail);
3366 if (__mid == __ret) {
3373 #endif // _LIBCPP_STD_VER > 17
3377 template <class _InputIterator, class _Predicate>
3378 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
3379 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3381 for (; __first != __last; ++__first)
3382 if (!__pred(*__first))
3384 if ( __first == __last )
3387 for (; __first != __last; ++__first)
3388 if (__pred(*__first))
3395 template <class _Predicate, class _ForwardIterator>
3396 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3397 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3401 if (__first == __last)
3403 if (!__pred(*__first))
3407 for (_ForwardIterator __p = __first; ++__p != __last;)
3411 swap(*__first, *__p);
3418 template <class _Predicate, class _BidirectionalIterator>
3419 _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
3420 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3421 bidirectional_iterator_tag)
3427 if (__first == __last)
3429 if (!__pred(*__first))
3435 if (__first == --__last)
3437 } while (!__pred(*__last));
3438 swap(*__first, *__last);
3443 template <class _ForwardIterator, class _Predicate>
3444 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3446 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3448 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3449 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3454 template <class _InputIterator, class _OutputIterator1,
3455 class _OutputIterator2, class _Predicate>
3456 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
3457 partition_copy(_InputIterator __first, _InputIterator __last,
3458 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3461 for (; __first != __last; ++__first)
3463 if (__pred(*__first))
3465 *__out_true = *__first;
3470 *__out_false = *__first;
3474 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3479 template<class _ForwardIterator, class _Predicate>
3480 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3481 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3483 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3484 difference_type __len = _VSTD::distance(__first, __last);
3487 difference_type __l2 = _VSTD::__half_positive(__len);
3488 _ForwardIterator __m = __first;
3489 _VSTD::advance(__m, __l2);
3503 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3505 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3506 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3508 // *__first is known to be false
3514 _ForwardIterator __m = __first;
3517 swap(*__first, *__m);
3522 if (__len <= __p.second)
3523 { // The buffer is big enough to use
3524 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3525 __destruct_n __d(0);
3526 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3527 // Move the falses into the temporary buffer, and the trues to the front of the line
3528 // Update __first to always point to the end of the trues
3529 value_type* __t = __p.first;
3530 ::new ((void*)__t) value_type(_VSTD::move(*__first));
3531 __d.template __incr<value_type>();
3533 _ForwardIterator __i = __first;
3534 while (++__i != __last)
3538 *__first = _VSTD::move(*__i);
3543 ::new ((void*)__t) value_type(_VSTD::move(*__i));
3544 __d.template __incr<value_type>();
3548 // All trues now at start of range, all falses in buffer
3549 // Move falses back into range, but don't mess up __first which points to first false
3551 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3552 *__i = _VSTD::move(*__t2);
3553 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3556 // Else not enough buffer, do in place
3558 _ForwardIterator __m = __first;
3559 _Distance __len2 = __len / 2; // __len2 >= 2
3560 _VSTD::advance(__m, __len2);
3561 // recurse on [__first, __m), *__first know to be false
3562 // F?????????????????
3564 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3565 _ForwardIterator __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3566 // TTTFFFFF??????????
3568 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3569 _ForwardIterator __m1 = __m;
3570 _ForwardIterator __second_false = __last;
3571 _Distance __len_half = __len - __len2;
3572 while (__pred(*__m1))
3574 if (++__m1 == __last)
3575 goto __second_half_done;
3578 // TTTFFFFFTTTF??????
3580 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3582 // TTTFFFFFTTTTTFFFFF
3584 return _VSTD::rotate(__first_false, __m, __second_false);
3585 // TTTTTTTTFFFFFFFFFF
3589 struct __return_temporary_buffer
3591 template <class _Tp>
3592 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3595 template <class _Predicate, class _ForwardIterator>
3597 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3598 forward_iterator_tag)
3600 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3601 // Either prove all true and return __first or point to first false
3604 if (__first == __last)
3606 if (!__pred(*__first))
3610 // We now have a reduced range [__first, __last)
3611 // *__first is known to be false
3612 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3613 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3614 difference_type __len = _VSTD::distance(__first, __last);
3615 pair<value_type*, ptrdiff_t> __p(0, 0);
3616 unique_ptr<value_type, __return_temporary_buffer> __h;
3617 if (__len >= __alloc_limit)
3619 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3620 __h.reset(__p.first);
3622 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3623 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3626 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3627 _BidirectionalIterator
3628 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3629 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3631 // *__first is known to be false
3632 // *__last is known to be true
3636 swap(*__first, *__last);
3641 _BidirectionalIterator __m = __first;
3644 swap(*__first, *__m);
3645 swap(*__m, *__last);
3648 swap(*__m, *__last);
3649 swap(*__first, *__m);
3652 if (__len <= __p.second)
3653 { // The buffer is big enough to use
3654 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3655 __destruct_n __d(0);
3656 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3657 // Move the falses into the temporary buffer, and the trues to the front of the line
3658 // Update __first to always point to the end of the trues
3659 value_type* __t = __p.first;
3660 ::new ((void*)__t) value_type(_VSTD::move(*__first));
3661 __d.template __incr<value_type>();
3663 _BidirectionalIterator __i = __first;
3664 while (++__i != __last)
3668 *__first = _VSTD::move(*__i);
3673 ::new ((void*)__t) value_type(_VSTD::move(*__i));
3674 __d.template __incr<value_type>();
3678 // move *__last, known to be true
3679 *__first = _VSTD::move(*__i);
3681 // All trues now at start of range, all falses in buffer
3682 // Move falses back into range, but don't mess up __first which points to first false
3683 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3684 *__i = _VSTD::move(*__t2);
3685 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3688 // Else not enough buffer, do in place
3690 _BidirectionalIterator __m = __first;
3691 _Distance __len2 = __len / 2; // __len2 >= 2
3692 _VSTD::advance(__m, __len2);
3693 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3694 // F????????????????T
3696 _BidirectionalIterator __m1 = __m;
3697 _BidirectionalIterator __first_false = __first;
3698 _Distance __len_half = __len2;
3699 while (!__pred(*--__m1))
3701 if (__m1 == __first)
3702 goto __first_half_done;
3705 // F???TFFF?????????T
3707 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3708 __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3710 // TTTFFFFF?????????T
3712 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3714 _BidirectionalIterator __second_false = __last;
3716 __len_half = __len - __len2;
3717 while (__pred(*__m1))
3719 if (++__m1 == __last)
3720 goto __second_half_done;
3723 // TTTFFFFFTTTF?????T
3725 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3727 // TTTFFFFFTTTTTFFFFF
3729 return _VSTD::rotate(__first_false, __m, __second_false);
3730 // TTTTTTTTFFFFFFFFFF
3734 template <class _Predicate, class _BidirectionalIterator>
3735 _BidirectionalIterator
3736 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3737 bidirectional_iterator_tag)
3739 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3740 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3741 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3742 // Either prove all true and return __first or point to first false
3745 if (__first == __last)
3747 if (!__pred(*__first))
3751 // __first points to first false, everything prior to __first is already set.
3752 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3755 if (__first == --__last)
3757 } while (!__pred(*__last));
3758 // We now have a reduced range [__first, __last]
3759 // *__first is known to be false
3760 // *__last is known to be true
3762 difference_type __len = _VSTD::distance(__first, __last) + 1;
3763 pair<value_type*, ptrdiff_t> __p(0, 0);
3764 unique_ptr<value_type, __return_temporary_buffer> __h;
3765 if (__len >= __alloc_limit)
3767 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3768 __h.reset(__p.first);
3770 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3771 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3774 template <class _ForwardIterator, class _Predicate>
3775 inline _LIBCPP_INLINE_VISIBILITY
3777 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3779 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
3780 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3785 template <class _ForwardIterator, class _Compare>
3786 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3787 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3789 if (__first != __last)
3791 _ForwardIterator __i = __first;
3792 while (++__i != __last)
3794 if (__comp(*__i, *__first))
3802 template<class _ForwardIterator>
3803 _LIBCPP_NODISCARD_EXT inline
3804 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3806 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3808 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3813 template <class _ForwardIterator, class _Compare>
3814 _LIBCPP_NODISCARD_EXT inline
3815 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3817 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3819 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3822 template<class _ForwardIterator>
3823 _LIBCPP_NODISCARD_EXT inline
3824 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3826 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3828 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3833 // stable, 2-3 compares, 0-2 swaps
3835 template <class _Compare, class _ForwardIterator>
3837 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3840 if (!__c(*__y, *__x)) // if x <= y
3842 if (!__c(*__z, *__y)) // if y <= z
3843 return __r; // x <= y && y <= z
3845 swap(*__y, *__z); // x <= z && y < z
3847 if (__c(*__y, *__x)) // if x > y
3849 swap(*__x, *__y); // x < y && y <= z
3852 return __r; // x <= y && y < z
3854 if (__c(*__z, *__y)) // x > y, if y > z
3856 swap(*__x, *__z); // x < y && y < z
3860 swap(*__x, *__y); // x > y && y <= z
3861 __r = 1; // x < y && x <= z
3862 if (__c(*__z, *__y)) // if y > z
3864 swap(*__y, *__z); // x <= y && y < z
3868 } // x <= y && y <= z
3870 // stable, 3-6 compares, 0-5 swaps
3872 template <class _Compare, class _ForwardIterator>
3874 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3875 _ForwardIterator __x4, _Compare __c)
3877 unsigned __r = _VSTD::__sort3<_Compare>(__x1, __x2, __x3, __c);
3878 if (__c(*__x4, *__x3))
3882 if (__c(*__x3, *__x2))
3886 if (__c(*__x2, *__x1))
3896 // stable, 4-10 compares, 0-9 swaps
3898 template <class _Compare, class _ForwardIterator>
3901 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3902 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3904 unsigned __r = _VSTD::__sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3905 if (__c(*__x5, *__x4))
3909 if (__c(*__x4, *__x3))
3913 if (__c(*__x3, *__x2))
3917 if (__c(*__x2, *__x1))
3929 template <class _Compare, class _BidirectionalIterator>
3931 __selection_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
3933 _BidirectionalIterator __lm1 = __last;
3934 for (--__lm1; __first != __lm1; ++__first)
3936 _BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator,
3937 typename add_lvalue_reference<_Compare>::type>
3938 (__first, __last, __comp);
3940 swap(*__first, *__i);
3944 template <class _Compare, class _BidirectionalIterator>
3946 __insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
3948 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3949 if (__first != __last)
3951 _BidirectionalIterator __i = __first;
3952 for (++__i; __i != __last; ++__i)
3954 _BidirectionalIterator __j = __i;
3955 value_type __t(_VSTD::move(*__j));
3956 for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3957 *__j = _VSTD::move(*__k);
3958 *__j = _VSTD::move(__t);
3963 template <class _Compare, class _RandomAccessIterator>
3965 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3967 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3968 _RandomAccessIterator __j = __first+2;
3969 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
3970 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3972 if (__comp(*__i, *__j))
3974 value_type __t(_VSTD::move(*__i));
3975 _RandomAccessIterator __k = __j;
3979 *__j = _VSTD::move(*__k);
3981 } while (__j != __first && __comp(__t, *--__k));
3982 *__j = _VSTD::move(__t);
3988 template <class _Compare, class _RandomAccessIterator>
3990 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3992 switch (__last - __first)
3998 if (__comp(*--__last, *__first))
3999 swap(*__first, *__last);
4002 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
4005 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
4008 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
4011 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4012 _RandomAccessIterator __j = __first+2;
4013 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
4014 const unsigned __limit = 8;
4015 unsigned __count = 0;
4016 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
4018 if (__comp(*__i, *__j))
4020 value_type __t(_VSTD::move(*__i));
4021 _RandomAccessIterator __k = __j;
4025 *__j = _VSTD::move(*__k);
4027 } while (__j != __first && __comp(__t, *--__k));
4028 *__j = _VSTD::move(__t);
4029 if (++__count == __limit)
4030 return ++__i == __last;
4037 template <class _Compare, class _BidirectionalIterator>
4039 __insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterator __last1,
4040 typename iterator_traits<_BidirectionalIterator>::value_type* __first2, _Compare __comp)
4042 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4043 if (__first1 != __last1)
4045 __destruct_n __d(0);
4046 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
4047 value_type* __last2 = __first2;
4048 ::new ((void*)__last2) value_type(_VSTD::move(*__first1));
4049 __d.template __incr<value_type>();
4050 for (++__last2; ++__first1 != __last1; ++__last2)
4052 value_type* __j2 = __last2;
4053 value_type* __i2 = __j2;
4054 if (__comp(*__first1, *--__i2))
4056 ::new ((void*)__j2) value_type(_VSTD::move(*__i2));
4057 __d.template __incr<value_type>();
4058 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
4059 *__j2 = _VSTD::move(*__i2);
4060 *__j2 = _VSTD::move(*__first1);
4064 ::new ((void*)__j2) value_type(_VSTD::move(*__first1));
4065 __d.template __incr<value_type>();
4072 template <class _Compare, class _RandomAccessIterator>
4074 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4076 // _Compare is known to be a reference type
4077 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4078 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4079 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4080 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
4084 difference_type __len = __last - __first;
4091 if (__comp(*--__last, *__first))
4092 swap(*__first, *__last);
4095 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
4098 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
4101 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
4104 if (__len <= __limit)
4106 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
4110 _RandomAccessIterator __m = __first;
4111 _RandomAccessIterator __lm1 = __last;
4115 difference_type __delta;
4121 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
4127 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
4131 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4132 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4133 _RandomAccessIterator __i = __first;
4134 _RandomAccessIterator __j = __lm1;
4135 // j points beyond range to be tested, *__m is known to be <= *__lm1
4136 // The search going up is known to be guarded but the search coming down isn't.
4137 // Prime the downward search with a guard.
4138 if (!__comp(*__i, *__m)) // if *__first == *__m
4140 // *__first == *__m, *__first doesn't go in first part
4141 // manually guard downward moving __j against __i
4146 // *__first == *__m, *__m <= all other elements
4147 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4148 ++__i; // __first + 1
4150 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4155 return; // [__first, __last) all equivalent elements
4156 if (__comp(*__first, *__i))
4166 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4171 while (!__comp(*__first, *__i))
4173 while (__comp(*__first, *--__j))
4181 // [__first, __i) == *__first and *__first < [__i, __last)
4182 // The first part is sorted, sort the second part
4183 // _VSTD::__sort<_Compare>(__i, __last, __comp);
4187 if (__comp(*__j, *__m))
4191 break; // found guard for downward moving __j, now use unguarded partition
4195 // It is known that *__i < *__m
4197 // j points beyond range to be tested, *__m is known to be <= *__lm1
4198 // if not yet partitioned...
4201 // known that *(__i - 1) < *__m
4202 // known that __i <= __m
4205 // __m still guards upward moving __i
4206 while (__comp(*__i, *__m))
4208 // It is now known that a guard exists for downward moving __j
4209 while (!__comp(*--__j, *__m))
4215 // It is known that __m != __j
4216 // If __m just moved, follow it
4222 // [__first, __i) < *__m and *__m <= [__i, __last)
4223 if (__i != __m && __comp(*__m, *__i))
4228 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4229 // If we were given a perfect partition, see if insertion sort is quick...
4232 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4233 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4249 // sort smaller range with recursive call and larger with tail recursion elimination
4250 if (__i - __first < __last - __i)
4252 _VSTD::__sort<_Compare>(__first, __i, __comp);
4253 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4258 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4259 // _VSTD::__sort<_Compare>(__first, __i, __comp);
4265 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4266 template <class _RandomAccessIterator, class _Compare>
4267 inline _LIBCPP_INLINE_VISIBILITY
4269 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4271 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4272 _VSTD::__sort<_Comp_ref>(__first, __last, _Comp_ref(__comp));
4275 template <class _RandomAccessIterator>
4276 inline _LIBCPP_INLINE_VISIBILITY
4278 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4280 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4283 template <class _Tp>
4284 inline _LIBCPP_INLINE_VISIBILITY
4286 sort(_Tp** __first, _Tp** __last)
4288 _VSTD::sort((uintptr_t*)__first, (uintptr_t*)__last, __less<uintptr_t>());
4291 template <class _Tp>
4292 inline _LIBCPP_INLINE_VISIBILITY
4294 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4296 _VSTD::sort(__first.base(), __last.base());
4299 template <class _Tp, class _Compare>
4300 inline _LIBCPP_INLINE_VISIBILITY
4302 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4304 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4305 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4308 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4309 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4310 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4311 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4312 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4313 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4314 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4315 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4316 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4317 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4318 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4319 _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>&))
4320 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4321 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4322 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4324 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4325 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4326 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4327 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4328 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4329 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4330 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4331 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4332 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4333 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4334 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4335 _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>&))
4336 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4337 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4338 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4340 _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>&))
4344 template <class _Compare, class _ForwardIterator, class _Tp>
4345 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4346 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4348 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4349 difference_type __len = _VSTD::distance(__first, __last);
4352 difference_type __l2 = _VSTD::__half_positive(__len);
4353 _ForwardIterator __m = __first;
4354 _VSTD::advance(__m, __l2);
4355 if (__comp(*__m, __value_))
4366 template <class _ForwardIterator, class _Tp, class _Compare>
4367 _LIBCPP_NODISCARD_EXT inline
4368 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4370 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4372 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4373 return _VSTD::__lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4376 template <class _ForwardIterator, class _Tp>
4377 _LIBCPP_NODISCARD_EXT inline
4378 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4380 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4382 return _VSTD::lower_bound(__first, __last, __value_,
4383 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4388 template <class _Compare, class _ForwardIterator, class _Tp>
4389 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4390 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4392 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4393 difference_type __len = _VSTD::distance(__first, __last);
4396 difference_type __l2 = _VSTD::__half_positive(__len);
4397 _ForwardIterator __m = __first;
4398 _VSTD::advance(__m, __l2);
4399 if (__comp(__value_, *__m))
4410 template <class _ForwardIterator, class _Tp, class _Compare>
4411 _LIBCPP_NODISCARD_EXT inline
4412 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4414 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4416 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4417 return _VSTD::__upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4420 template <class _ForwardIterator, class _Tp>
4421 _LIBCPP_NODISCARD_EXT inline
4422 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4424 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4426 return _VSTD::upper_bound(__first, __last, __value_,
4427 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4432 template <class _Compare, class _ForwardIterator, class _Tp>
4433 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
4434 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4436 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4437 difference_type __len = _VSTD::distance(__first, __last);
4440 difference_type __l2 = _VSTD::__half_positive(__len);
4441 _ForwardIterator __m = __first;
4442 _VSTD::advance(__m, __l2);
4443 if (__comp(*__m, __value_))
4448 else if (__comp(__value_, *__m))
4455 _ForwardIterator __mp1 = __m;
4456 return pair<_ForwardIterator, _ForwardIterator>
4458 _VSTD::__lower_bound<_Compare>(__first, __m, __value_, __comp),
4459 _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4463 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4466 template <class _ForwardIterator, class _Tp, class _Compare>
4467 _LIBCPP_NODISCARD_EXT inline
4468 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4469 pair<_ForwardIterator, _ForwardIterator>
4470 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4472 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4473 return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4476 template <class _ForwardIterator, class _Tp>
4477 _LIBCPP_NODISCARD_EXT inline
4478 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4479 pair<_ForwardIterator, _ForwardIterator>
4480 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4482 return _VSTD::equal_range(__first, __last, __value_,
4483 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4488 template <class _Compare, class _ForwardIterator, class _Tp>
4489 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4491 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4493 __first = _VSTD::__lower_bound<_Compare>(__first, __last, __value_, __comp);
4494 return __first != __last && !__comp(__value_, *__first);
4497 template <class _ForwardIterator, class _Tp, class _Compare>
4498 _LIBCPP_NODISCARD_EXT inline
4499 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4501 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4503 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4504 return _VSTD::__binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4507 template <class _ForwardIterator, class _Tp>
4508 _LIBCPP_NODISCARD_EXT inline
4509 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4511 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4513 return _VSTD::binary_search(__first, __last, __value_,
4514 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4519 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4520 _LIBCPP_CONSTEXPR_AFTER_CXX17
4522 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4523 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4525 for (; __first1 != __last1; ++__result)
4527 if (__first2 == __last2)
4528 return _VSTD::copy(__first1, __last1, __result);
4529 if (__comp(*__first2, *__first1))
4531 *__result = *__first2;
4536 *__result = *__first1;
4540 return _VSTD::copy(__first2, __last2, __result);
4543 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4544 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4546 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4547 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4549 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4550 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4553 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4554 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4556 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4557 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4559 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4560 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4561 return _VSTD::merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4566 template <class _Compare, class _InputIterator1, class _InputIterator2,
4567 class _OutputIterator>
4568 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4569 _InputIterator2 __first2, _InputIterator2 __last2,
4570 _OutputIterator __result, _Compare __comp)
4572 for (; __first1 != __last1; ++__result)
4574 if (__first2 == __last2)
4576 _VSTD::move(__first1, __last1, __result);
4580 if (__comp(*__first2, *__first1))
4582 *__result = _VSTD::move(*__first2);
4587 *__result = _VSTD::move(*__first1);
4591 // __first2 through __last2 are already in the right spot.
4594 template <class _Compare, class _BidirectionalIterator>
4596 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4597 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4598 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4599 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4601 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4602 __destruct_n __d(0);
4603 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4604 if (__len1 <= __len2)
4606 value_type* __p = __buff;
4607 for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
4608 ::new ((void*)__p) value_type(_VSTD::move(*__i));
4609 _VSTD::__half_inplace_merge<_Compare>(__buff, __p, __middle, __last, __first, __comp);
4613 value_type* __p = __buff;
4614 for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
4615 ::new ((void*)__p) value_type(_VSTD::move(*__i));
4616 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4617 typedef reverse_iterator<value_type*> _Rv;
4618 typedef __invert<_Compare> _Inverted;
4619 _VSTD::__half_inplace_merge<_Inverted>(_Rv(__p), _Rv(__buff),
4620 _RBi(__middle), _RBi(__first),
4621 _RBi(__last), _Inverted(__comp));
4625 template <class _Compare, class _BidirectionalIterator>
4627 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4628 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4629 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4630 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4632 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4635 // if __middle == __last, we're done
4638 if (__len1 <= __buff_size || __len2 <= __buff_size)
4639 return _VSTD::__buffered_inplace_merge<_Compare>
4640 (__first, __middle, __last, __comp, __len1, __len2, __buff);
4641 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4642 for (; true; ++__first, (void) --__len1)
4646 if (__comp(*__middle, *__first))
4649 // __first < __middle < __last
4650 // *__first > *__middle
4651 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4653 // [__first, __m1) <= [__middle, __m2)
4654 // [__middle, __m2) < [__m1, __middle)
4655 // [__m1, __middle) <= [__m2, __last)
4656 // and __m1 or __m2 is in the middle of its range
4657 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4658 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4659 difference_type __len11; // distance(__first, __m1)
4660 difference_type __len21; // distance(__middle, __m2)
4661 // binary search smaller range
4662 if (__len1 < __len2)
4663 { // __len >= 1, __len2 >= 2
4664 __len21 = __len2 / 2;
4666 _VSTD::advance(__m2, __len21);
4667 __m1 = _VSTD::__upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4668 __len11 = _VSTD::distance(__first, __m1);
4673 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4674 // It is known *__first > *__middle
4675 swap(*__first, *__middle);
4678 // __len1 >= 2, __len2 >= 1
4679 __len11 = __len1 / 2;
4681 _VSTD::advance(__m1, __len11);
4682 __m2 = _VSTD::__lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4683 __len21 = _VSTD::distance(__middle, __m2);
4685 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4686 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4687 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4688 // swap middle two partitions
4689 __middle = _VSTD::rotate(__m1, __middle, __m2);
4690 // __len12 and __len21 now have swapped meanings
4691 // merge smaller range with recursive call and larger with tail recursion elimination
4692 if (__len11 + __len21 < __len12 + __len22)
4694 _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4695 // _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4703 _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4704 // _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4713 template <class _BidirectionalIterator, class _Compare>
4714 inline _LIBCPP_INLINE_VISIBILITY
4716 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4719 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4720 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4721 difference_type __len1 = _VSTD::distance(__first, __middle);
4722 difference_type __len2 = _VSTD::distance(__middle, __last);
4723 difference_type __buf_size = _VSTD::min(__len1, __len2);
4724 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4725 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4726 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4727 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4728 __buf.first, __buf.second);
4731 template <class _BidirectionalIterator>
4732 inline _LIBCPP_INLINE_VISIBILITY
4734 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4736 _VSTD::inplace_merge(__first, __middle, __last,
4737 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4742 template <class _Compare, class _InputIterator1, class _InputIterator2>
4744 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4745 _InputIterator2 __first2, _InputIterator2 __last2,
4746 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4748 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4749 __destruct_n __d(0);
4750 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4751 for (; true; ++__result)
4753 if (__first1 == __last1)
4755 for (; __first2 != __last2; ++__first2, ++__result, (void)__d.template __incr<value_type>())
4756 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
4760 if (__first2 == __last2)
4762 for (; __first1 != __last1; ++__first1, ++__result, (void)__d.template __incr<value_type>())
4763 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
4767 if (__comp(*__first2, *__first1))
4769 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
4770 __d.template __incr<value_type>();
4775 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
4776 __d.template __incr<value_type>();
4782 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4784 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4785 _InputIterator2 __first2, _InputIterator2 __last2,
4786 _OutputIterator __result, _Compare __comp)
4788 for (; __first1 != __last1; ++__result)
4790 if (__first2 == __last2)
4792 for (; __first1 != __last1; ++__first1, (void) ++__result)
4793 *__result = _VSTD::move(*__first1);
4796 if (__comp(*__first2, *__first1))
4798 *__result = _VSTD::move(*__first2);
4803 *__result = _VSTD::move(*__first1);
4807 for (; __first2 != __last2; ++__first2, (void) ++__result)
4808 *__result = _VSTD::move(*__first2);
4811 template <class _Compare, class _RandomAccessIterator>
4813 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4814 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4815 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4817 template <class _Compare, class _RandomAccessIterator>
4819 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4820 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4821 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4823 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4829 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4832 __destruct_n __d(0);
4833 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4834 if (__comp(*--__last1, *__first1))
4836 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
4837 __d.template __incr<value_type>();
4839 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4843 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
4844 __d.template __incr<value_type>();
4846 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
4853 _VSTD::__insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4856 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4857 _RandomAccessIterator __m = __first1 + __l2;
4858 _VSTD::__stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4859 _VSTD::__stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4860 _VSTD::__merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4863 template <class _Tp>
4864 struct __stable_sort_switch
4866 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4869 template <class _Compare, class _RandomAccessIterator>
4871 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4872 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4873 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4875 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4876 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4883 if (__comp(*--__last, *__first))
4884 swap(*__first, *__last);
4887 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4889 _VSTD::__insertion_sort<_Compare>(__first, __last, __comp);
4892 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4893 _RandomAccessIterator __m = __first + __l2;
4894 if (__len <= __buff_size)
4896 __destruct_n __d(0);
4897 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4898 _VSTD::__stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4899 __d.__set(__l2, (value_type*)nullptr);
4900 _VSTD::__stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4901 __d.__set(__len, (value_type*)nullptr);
4902 _VSTD::__merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4903 // _VSTD::__merge<_Compare>(move_iterator<value_type*>(__buff),
4904 // move_iterator<value_type*>(__buff + __l2),
4905 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4906 // move_iterator<_RandomAccessIterator>(__buff + __len),
4907 // __first, __comp);
4910 _VSTD::__stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4911 _VSTD::__stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4912 _VSTD::__inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4915 template <class _RandomAccessIterator, class _Compare>
4916 inline _LIBCPP_INLINE_VISIBILITY
4918 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4920 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4921 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4922 difference_type __len = __last - __first;
4923 pair<value_type*, ptrdiff_t> __buf(0, 0);
4924 unique_ptr<value_type, __return_temporary_buffer> __h;
4925 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4927 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4928 __h.reset(__buf.first);
4930 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4931 _VSTD::__stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4934 template <class _RandomAccessIterator>
4935 inline _LIBCPP_INLINE_VISIBILITY
4937 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4939 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4944 template <class _RandomAccessIterator, class _Compare>
4945 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
4946 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4948 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4949 difference_type __len = __last - __first;
4950 difference_type __p = 0;
4951 difference_type __c = 1;
4952 _RandomAccessIterator __pp = __first;
4955 _RandomAccessIterator __cp = __first + __c;
4956 if (__comp(*__pp, *__cp))
4962 if (__comp(*__pp, *__cp))
4971 template<class _RandomAccessIterator>
4972 _LIBCPP_NODISCARD_EXT inline
4973 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4974 _RandomAccessIterator
4975 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4977 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4982 template <class _RandomAccessIterator, class _Compare>
4983 _LIBCPP_NODISCARD_EXT inline
4984 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4986 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4988 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4991 template<class _RandomAccessIterator>
4992 _LIBCPP_NODISCARD_EXT inline
4993 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4995 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4997 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5002 template <class _Compare, class _RandomAccessIterator>
5004 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5005 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5007 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5010 __len = (__len - 2) / 2;
5011 _RandomAccessIterator __ptr = __first + __len;
5012 if (__comp(*__ptr, *--__last))
5014 value_type __t(_VSTD::move(*__last));
5017 *__last = _VSTD::move(*__ptr);
5021 __len = (__len - 1) / 2;
5022 __ptr = __first + __len;
5023 } while (__comp(*__ptr, __t));
5024 *__last = _VSTD::move(__t);
5029 template <class _RandomAccessIterator, class _Compare>
5030 inline _LIBCPP_INLINE_VISIBILITY
5032 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5034 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5035 _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
5038 template <class _RandomAccessIterator>
5039 inline _LIBCPP_INLINE_VISIBILITY
5041 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5043 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5048 template <class _Compare, class _RandomAccessIterator>
5050 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
5052 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5053 _RandomAccessIterator __start)
5055 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5056 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5057 // left-child of __start is at 2 * __start + 1
5058 // right-child of __start is at 2 * __start + 2
5059 difference_type __child = __start - __first;
5061 if (__len < 2 || (__len - 2) / 2 < __child)
5064 __child = 2 * __child + 1;
5065 _RandomAccessIterator __child_i = __first + __child;
5067 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5068 // right-child exists and is greater than left-child
5073 // check if we are in heap-order
5074 if (__comp(*__child_i, *__start))
5075 // we are, __start is larger than it's largest child
5078 value_type __top(_VSTD::move(*__start));
5081 // we are not in heap-order, swap the parent with it's largest child
5082 *__start = _VSTD::move(*__child_i);
5083 __start = __child_i;
5085 if ((__len - 2) / 2 < __child)
5088 // recompute the child based off of the updated parent
5089 __child = 2 * __child + 1;
5090 __child_i = __first + __child;
5092 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5093 // right-child exists and is greater than left-child
5098 // check if we are in heap-order
5099 } while (!__comp(*__child_i, __top));
5100 *__start = _VSTD::move(__top);
5103 template <class _Compare, class _RandomAccessIterator>
5104 inline _LIBCPP_INLINE_VISIBILITY
5106 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5107 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5111 swap(*__first, *--__last);
5112 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
5116 template <class _RandomAccessIterator, class _Compare>
5117 inline _LIBCPP_INLINE_VISIBILITY
5119 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5121 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5122 _VSTD::__pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
5125 template <class _RandomAccessIterator>
5126 inline _LIBCPP_INLINE_VISIBILITY
5128 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5130 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5135 template <class _Compare, class _RandomAccessIterator>
5137 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5139 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5140 difference_type __n = __last - __first;
5143 // start from the first parent, there is no need to consider children
5144 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5146 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5151 template <class _RandomAccessIterator, class _Compare>
5152 inline _LIBCPP_INLINE_VISIBILITY
5154 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5156 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5157 _VSTD::__make_heap<_Comp_ref>(__first, __last, __comp);
5160 template <class _RandomAccessIterator>
5161 inline _LIBCPP_INLINE_VISIBILITY
5163 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5165 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5170 template <class _Compare, class _RandomAccessIterator>
5172 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5174 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5175 for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
5176 _VSTD::__pop_heap<_Compare>(__first, __last, __comp, __n);
5179 template <class _RandomAccessIterator, class _Compare>
5180 inline _LIBCPP_INLINE_VISIBILITY
5182 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5184 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5185 _VSTD::__sort_heap<_Comp_ref>(__first, __last, __comp);
5188 template <class _RandomAccessIterator>
5189 inline _LIBCPP_INLINE_VISIBILITY
5191 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5193 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5198 template <class _Compare, class _RandomAccessIterator>
5200 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5203 _VSTD::__make_heap<_Compare>(__first, __middle, __comp);
5204 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5205 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5207 if (__comp(*__i, *__first))
5209 swap(*__i, *__first);
5210 _VSTD::__sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5213 _VSTD::__sort_heap<_Compare>(__first, __middle, __comp);
5216 template <class _RandomAccessIterator, class _Compare>
5217 inline _LIBCPP_INLINE_VISIBILITY
5219 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5222 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5223 _VSTD::__partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5226 template <class _RandomAccessIterator>
5227 inline _LIBCPP_INLINE_VISIBILITY
5229 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5231 _VSTD::partial_sort(__first, __middle, __last,
5232 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5235 // partial_sort_copy
5237 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5238 _RandomAccessIterator
5239 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5240 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5242 _RandomAccessIterator __r = __result_first;
5243 if (__r != __result_last)
5245 for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
5247 _VSTD::__make_heap<_Compare>(__result_first, __r, __comp);
5248 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5249 for (; __first != __last; ++__first)
5250 if (__comp(*__first, *__result_first))
5252 *__result_first = *__first;
5253 _VSTD::__sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5255 _VSTD::__sort_heap<_Compare>(__result_first, __r, __comp);
5260 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5261 inline _LIBCPP_INLINE_VISIBILITY
5262 _RandomAccessIterator
5263 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5264 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5266 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5267 return _VSTD::__partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5270 template <class _InputIterator, class _RandomAccessIterator>
5271 inline _LIBCPP_INLINE_VISIBILITY
5272 _RandomAccessIterator
5273 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5274 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5276 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5277 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5282 template <class _Compare, class _RandomAccessIterator>
5284 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5286 // _Compare is known to be a reference type
5287 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5288 const difference_type __limit = 7;
5292 if (__nth == __last)
5294 difference_type __len = __last - __first;
5301 if (__comp(*--__last, *__first))
5302 swap(*__first, *__last);
5306 _RandomAccessIterator __m = __first;
5307 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5311 if (__len <= __limit)
5313 _VSTD::__selection_sort<_Compare>(__first, __last, __comp);
5316 // __len > __limit >= 3
5317 _RandomAccessIterator __m = __first + __len/2;
5318 _RandomAccessIterator __lm1 = __last;
5319 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5321 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5322 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5323 _RandomAccessIterator __i = __first;
5324 _RandomAccessIterator __j = __lm1;
5325 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5326 // The search going up is known to be guarded but the search coming down isn't.
5327 // Prime the downward search with a guard.
5328 if (!__comp(*__i, *__m)) // if *__first == *__m
5330 // *__first == *__m, *__first doesn't go in first part
5331 // manually guard downward moving __j against __i
5336 // *__first == *__m, *__m <= all other elements
5337 // Partition instead into [__first, __i) == *__first and *__first < [__i, __last)
5338 ++__i; // __first + 1
5340 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5345 return; // [__first, __last) all equivalent elements
5346 if (__comp(*__first, *__i))
5356 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5361 while (!__comp(*__first, *__i))
5363 while (__comp(*__first, *--__j))
5371 // [__first, __i) == *__first and *__first < [__i, __last)
5372 // The first part is sorted,
5375 // __nth_element the second part
5376 // _VSTD::__nth_element<_Compare>(__i, __nth, __last, __comp);
5380 if (__comp(*__j, *__m))
5384 break; // found guard for downward moving __j, now use unguarded partition
5389 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5390 // if not yet partitioned...
5393 // known that *(__i - 1) < *__m
5396 // __m still guards upward moving __i
5397 while (__comp(*__i, *__m))
5399 // It is now known that a guard exists for downward moving __j
5400 while (!__comp(*--__j, *__m))
5406 // It is known that __m != __j
5407 // If __m just moved, follow it
5413 // [__first, __i) < *__m and *__m <= [__i, __last)
5414 if (__i != __m && __comp(*__m, *__i))
5419 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5424 // We were given a perfectly partitioned sequence. Coincidence?
5427 // Check for [__first, __i) already sorted
5428 __j = __m = __first;
5429 while (++__j != __i)
5431 if (__comp(*__j, *__m))
5432 // not yet sorted, so sort
5436 // [__first, __i) sorted
5441 // Check for [__i, __last) already sorted
5443 while (++__j != __last)
5445 if (__comp(*__j, *__m))
5446 // not yet sorted, so sort
5450 // [__i, __last) sorted
5455 // __nth_element on range containing __nth
5458 // _VSTD::__nth_element<_Compare>(__first, __nth, __i, __comp);
5463 // _VSTD::__nth_element<_Compare>(__i+1, __nth, __last, __comp);
5469 template <class _RandomAccessIterator, class _Compare>
5470 inline _LIBCPP_INLINE_VISIBILITY
5472 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5474 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5475 _VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5478 template <class _RandomAccessIterator>
5479 inline _LIBCPP_INLINE_VISIBILITY
5481 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5483 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5488 template <class _Compare, class _InputIterator1, class _InputIterator2>
5489 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5490 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5493 for (; __first2 != __last2; ++__first1)
5495 if (__first1 == __last1 || __comp(*__first2, *__first1))
5497 if (!__comp(*__first1, *__first2))
5503 template <class _InputIterator1, class _InputIterator2, class _Compare>
5504 _LIBCPP_NODISCARD_EXT inline
5505 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5507 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5510 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5511 return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5514 template <class _InputIterator1, class _InputIterator2>
5515 _LIBCPP_NODISCARD_EXT inline
5516 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5518 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5520 return _VSTD::includes(__first1, __last1, __first2, __last2,
5521 __less<typename iterator_traits<_InputIterator1>::value_type,
5522 typename iterator_traits<_InputIterator2>::value_type>());
5527 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5528 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5529 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5530 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5532 for (; __first1 != __last1; ++__result)
5534 if (__first2 == __last2)
5535 return _VSTD::copy(__first1, __last1, __result);
5536 if (__comp(*__first2, *__first1))
5538 *__result = *__first2;
5543 if (!__comp(*__first1, *__first2))
5545 *__result = *__first1;
5549 return _VSTD::copy(__first2, __last2, __result);
5552 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5553 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5555 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5556 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5558 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5559 return _VSTD::__set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5562 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5563 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5565 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5566 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5568 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5569 __less<typename iterator_traits<_InputIterator1>::value_type,
5570 typename iterator_traits<_InputIterator2>::value_type>());
5575 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5576 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5577 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5578 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5580 while (__first1 != __last1 && __first2 != __last2)
5582 if (__comp(*__first1, *__first2))
5586 if (!__comp(*__first2, *__first1))
5588 *__result = *__first1;
5598 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5599 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5601 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5602 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5604 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5605 return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5608 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5609 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5611 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5612 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5614 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5615 __less<typename iterator_traits<_InputIterator1>::value_type,
5616 typename iterator_traits<_InputIterator2>::value_type>());
5621 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5622 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5623 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5624 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5626 while (__first1 != __last1)
5628 if (__first2 == __last2)
5629 return _VSTD::copy(__first1, __last1, __result);
5630 if (__comp(*__first1, *__first2))
5632 *__result = *__first1;
5638 if (!__comp(*__first2, *__first1))
5646 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5647 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5649 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5650 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5652 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5653 return _VSTD::__set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5656 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5657 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5659 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5660 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5662 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5663 __less<typename iterator_traits<_InputIterator1>::value_type,
5664 typename iterator_traits<_InputIterator2>::value_type>());
5667 // set_symmetric_difference
5669 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5670 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5671 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5672 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5674 while (__first1 != __last1)
5676 if (__first2 == __last2)
5677 return _VSTD::copy(__first1, __last1, __result);
5678 if (__comp(*__first1, *__first2))
5680 *__result = *__first1;
5686 if (__comp(*__first2, *__first1))
5688 *__result = *__first2;
5696 return _VSTD::copy(__first2, __last2, __result);
5699 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5700 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5702 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5703 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5705 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5706 return _VSTD::__set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5709 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5710 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5712 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5713 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5715 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5716 __less<typename iterator_traits<_InputIterator1>::value_type,
5717 typename iterator_traits<_InputIterator2>::value_type>());
5720 // lexicographical_compare
5722 template <class _Compare, class _InputIterator1, class _InputIterator2>
5723 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5724 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5725 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5727 for (; __first2 != __last2; ++__first1, (void) ++__first2)
5729 if (__first1 == __last1 || __comp(*__first1, *__first2))
5731 if (__comp(*__first2, *__first1))
5737 template <class _InputIterator1, class _InputIterator2, class _Compare>
5738 _LIBCPP_NODISCARD_EXT inline
5739 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5741 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5742 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5744 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5745 return _VSTD::__lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5748 template <class _InputIterator1, class _InputIterator2>
5749 _LIBCPP_NODISCARD_EXT inline
5750 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5752 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5753 _InputIterator2 __first2, _InputIterator2 __last2)
5755 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5756 __less<typename iterator_traits<_InputIterator1>::value_type,
5757 typename iterator_traits<_InputIterator2>::value_type>());
5762 template <class _Compare, class _BidirectionalIterator>
5763 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5764 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5766 _BidirectionalIterator __i = __last;
5767 if (__first == __last || __first == --__i)
5771 _BidirectionalIterator __ip1 = __i;
5772 if (__comp(*--__i, *__ip1))
5774 _BidirectionalIterator __j = __last;
5775 while (!__comp(*__i, *--__j))
5778 _VSTD::reverse(__ip1, __last);
5783 _VSTD::reverse(__first, __last);
5789 template <class _BidirectionalIterator, class _Compare>
5790 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5792 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5794 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5795 return _VSTD::__next_permutation<_Comp_ref>(__first, __last, __comp);
5798 template <class _BidirectionalIterator>
5799 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5801 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5803 return _VSTD::next_permutation(__first, __last,
5804 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5809 template <class _Compare, class _BidirectionalIterator>
5810 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5811 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5813 _BidirectionalIterator __i = __last;
5814 if (__first == __last || __first == --__i)
5818 _BidirectionalIterator __ip1 = __i;
5819 if (__comp(*__ip1, *--__i))
5821 _BidirectionalIterator __j = __last;
5822 while (!__comp(*--__j, *__i))
5825 _VSTD::reverse(__ip1, __last);
5830 _VSTD::reverse(__first, __last);
5836 template <class _BidirectionalIterator, class _Compare>
5837 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5839 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5841 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5842 return _VSTD::__prev_permutation<_Comp_ref>(__first, __last, __comp);
5845 template <class _BidirectionalIterator>
5846 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5848 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5850 return _VSTD::prev_permutation(__first, __last,
5851 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5854 _LIBCPP_END_NAMESPACE_STD
5858 #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5859 # include <__pstl_algorithm>
5862 #endif // _LIBCPP_ALGORITHM