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 InputIterator // constexpr in C++20
51 find_if_not(InputIterator first, InputIterator last, Predicate pred);
53 template <class ForwardIterator1, class ForwardIterator2>
54 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 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>
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
191 template <class ForwardIterator1, class ForwardIterator2>
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>
255 unique(ForwardIterator first, ForwardIterator last);
257 template <class ForwardIterator, class BinaryPredicate>
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
261 template <class InputIterator, class OutputIterator>
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
265 template <class InputIterator, class OutputIterator, class BinaryPredicate>
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
269 template <class BidirectionalIterator>
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>
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
281 template <class ForwardIterator, class OutputIterator>
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 InputIterator, class Predicate>
305 constexpr bool // constexpr in C++20
306 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
308 template <class ForwardIterator, class Predicate>
310 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
312 template <class InputIterator, class OutputIterator1,
313 class OutputIterator2, class Predicate>
314 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
315 partition_copy(InputIterator first, InputIterator last,
316 OutputIterator1 out_true, OutputIterator2 out_false,
319 template <class ForwardIterator, class Predicate>
321 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
323 template<class ForwardIterator, class Predicate>
324 constexpr ForwardIterator // constexpr in C++20
325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
327 template <class ForwardIterator>
328 constexpr bool // constexpr in C++20
329 is_sorted(ForwardIterator first, ForwardIterator last);
331 template <class ForwardIterator, class Compare>
333 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
335 template<class ForwardIterator>
336 constexpr ForwardIterator // constexpr in C++20
337 is_sorted_until(ForwardIterator first, ForwardIterator last);
339 template <class ForwardIterator, class Compare>
340 constexpr ForwardIterator // constexpr in C++20
341 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
343 template <class RandomAccessIterator>
345 sort(RandomAccessIterator first, RandomAccessIterator last);
347 template <class RandomAccessIterator, class Compare>
349 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
351 template <class RandomAccessIterator>
353 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
355 template <class RandomAccessIterator, class Compare>
357 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
359 template <class RandomAccessIterator>
361 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
363 template <class RandomAccessIterator, class Compare>
365 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
367 template <class InputIterator, class RandomAccessIterator>
369 partial_sort_copy(InputIterator first, InputIterator last,
370 RandomAccessIterator result_first, RandomAccessIterator result_last);
372 template <class InputIterator, class RandomAccessIterator, class Compare>
374 partial_sort_copy(InputIterator first, InputIterator last,
375 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
377 template <class RandomAccessIterator>
379 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
381 template <class RandomAccessIterator, class Compare>
383 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
385 template <class ForwardIterator, class T>
386 constexpr ForwardIterator // constexpr in C++20
387 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
389 template <class ForwardIterator, class T, class Compare>
390 constexpr ForwardIterator // constexpr in C++20
391 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
393 template <class ForwardIterator, class T>
394 constexpr ForwardIterator // constexpr in C++20
395 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
397 template <class ForwardIterator, class T, class Compare>
398 constexpr ForwardIterator // constexpr in C++20
399 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
401 template <class ForwardIterator, class T>
402 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
403 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
405 template <class ForwardIterator, class T, class Compare>
406 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
407 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
409 template <class ForwardIterator, class T>
410 constexpr bool // constexpr in C++20
411 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
413 template <class ForwardIterator, class T, class Compare>
414 constexpr bool // constexpr in C++20
415 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
417 template <class InputIterator1, class InputIterator2, class OutputIterator>
419 merge(InputIterator1 first1, InputIterator1 last1,
420 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
422 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
424 merge(InputIterator1 first1, InputIterator1 last1,
425 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
427 template <class BidirectionalIterator>
429 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
431 template <class BidirectionalIterator, class Compare>
433 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
435 template <class InputIterator1, class InputIterator2>
436 constexpr bool // constexpr in C++20
437 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
439 template <class InputIterator1, class InputIterator2, class Compare>
440 constexpr bool // constexpr in C++20
441 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
443 template <class InputIterator1, class InputIterator2, class OutputIterator>
445 set_union(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 set_union(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453 template <class InputIterator1, class InputIterator2, class OutputIterator>
454 constexpr OutputIterator // constexpr in C++20
455 set_intersection(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_intersection(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463 template <class InputIterator1, class InputIterator2, class OutputIterator>
465 set_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 set_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473 template <class InputIterator1, class InputIterator2, class OutputIterator>
475 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
478 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
480 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
483 template <class RandomAccessIterator>
485 push_heap(RandomAccessIterator first, RandomAccessIterator last);
487 template <class RandomAccessIterator, class Compare>
489 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
491 template <class RandomAccessIterator>
493 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
495 template <class RandomAccessIterator, class Compare>
497 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
499 template <class RandomAccessIterator>
501 make_heap(RandomAccessIterator first, RandomAccessIterator last);
503 template <class RandomAccessIterator, class Compare>
505 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
507 template <class RandomAccessIterator>
509 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
511 template <class RandomAccessIterator, class Compare>
513 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
515 template <class RandomAccessIterator>
516 constexpr bool // constexpr in C++20
517 is_heap(RandomAccessIterator first, RandomAccessiterator last);
519 template <class RandomAccessIterator, class Compare>
520 constexpr bool // constexpr in C++20
521 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
523 template <class RandomAccessIterator>
524 constexpr RandomAccessIterator // constexpr in C++20
525 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
527 template <class RandomAccessIterator, class Compare>
528 constexpr RandomAccessIterator // constexpr in C++20
529 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
531 template <class ForwardIterator>
533 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
535 template <class ForwardIterator, class Compare>
537 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
541 min(const T& a, const T& b); // constexpr in C++14
543 template <class T, class Compare>
545 min(const T& a, const T& b, Compare comp); // constexpr in C++14
549 min(initializer_list<T> t); // constexpr in C++14
551 template<class T, class Compare>
553 min(initializer_list<T> t, Compare comp); // constexpr in C++14
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
558 template<class T, class Compare>
559 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
561 template <class ForwardIterator>
563 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
565 template <class ForwardIterator, class Compare>
567 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
571 max(const T& a, const T& b); // constexpr in C++14
573 template <class T, class Compare>
575 max(const T& a, const T& b, Compare comp); // constexpr in C++14
579 max(initializer_list<T> t); // constexpr in C++14
581 template<class T, class Compare>
583 max(initializer_list<T> t, Compare comp); // constexpr in C++14
585 template<class ForwardIterator>
586 pair<ForwardIterator, ForwardIterator>
587 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
589 template<class ForwardIterator, class Compare>
590 pair<ForwardIterator, ForwardIterator>
591 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
594 pair<const T&, const T&>
595 minmax(const T& a, const T& b); // constexpr in C++14
597 template<class T, class Compare>
598 pair<const T&, const T&>
599 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
603 minmax(initializer_list<T> t); // constexpr in C++14
605 template<class T, class Compare>
607 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
609 template <class InputIterator1, class InputIterator2>
610 constexpr bool // constexpr in C++20
611 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
613 template <class InputIterator1, class InputIterator2, class Compare>
614 constexpr bool // constexpr in C++20
615 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
616 InputIterator2 first2, InputIterator2 last2, Compare comp);
618 template <class BidirectionalIterator>
620 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
622 template <class BidirectionalIterator, class Compare>
624 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
626 template <class BidirectionalIterator>
628 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
630 template <class BidirectionalIterator, class Compare>
632 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
639 #include <initializer_list>
640 #include <type_traits>
642 #include <utility> // needed to provide swap_ranges.
644 #include <functional>
652 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
653 #pragma GCC system_header
657 #include <__undef_macros>
660 _LIBCPP_BEGIN_NAMESPACE_STD
662 // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
663 // * That only works with C++14 and later, and
664 // * We haven't included <functional> here.
665 template <class _T1, class _T2 = _T1>
668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
675 struct __equal_to<_T1, _T1>
677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
678 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
682 struct __equal_to<const _T1, _T1>
684 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
685 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
689 struct __equal_to<_T1, const _T1>
691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
695 template <class _T1, class _T2 = _T1>
698 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
699 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
704 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
705 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
707 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
708 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
712 struct __less<_T1, _T1>
714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
719 struct __less<const _T1, _T1>
721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
722 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
726 struct __less<_T1, const _T1>
728 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
729 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
732 template <class _Predicate>
733 class __invert // invert the sense of a comparison
738 _LIBCPP_INLINE_VISIBILITY __invert() {}
740 _LIBCPP_INLINE_VISIBILITY
741 explicit __invert(_Predicate __p) : __p_(__p) {}
744 _LIBCPP_INLINE_VISIBILITY
745 bool operator()(const _T1& __x) {return !__p_(__x);}
747 template <class _T1, class _T2>
748 _LIBCPP_INLINE_VISIBILITY
749 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
752 // Perform division by two quickly for positive integers (llvm.org/PR39129)
754 template <typename _Integral>
755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
758 is_integral<_Integral>::value,
761 __half_positive(_Integral __value)
763 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
766 template <typename _Tp>
767 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
770 !is_integral<_Tp>::value,
773 __half_positive(_Tp __value)
780 template <class _Compare>
784 _LIBCPP_CONSTEXPR_AFTER_CXX17
785 __debug_less(_Compare& __c) : __comp_(__c) {}
787 template <class _Tp, class _Up>
788 _LIBCPP_CONSTEXPR_AFTER_CXX17
789 bool operator()(const _Tp& __x, const _Up& __y)
791 bool __r = __comp_(__x, __y);
793 __do_compare_assert(0, __y, __x);
797 template <class _Tp, class _Up>
798 _LIBCPP_CONSTEXPR_AFTER_CXX17
799 bool operator()(_Tp& __x, _Up& __y)
801 bool __r = __comp_(__x, __y);
803 __do_compare_assert(0, __y, __x);
807 template <class _LHS, class _RHS>
808 _LIBCPP_CONSTEXPR_AFTER_CXX17
809 inline _LIBCPP_INLINE_VISIBILITY
810 decltype((void)_VSTD::declval<_Compare&>()(
811 _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
812 __do_compare_assert(int, _LHS & __l, _RHS & __r) {
813 _LIBCPP_ASSERT(!__comp_(__l, __r),
814 "Comparator does not induce a strict weak ordering");
817 template <class _LHS, class _RHS>
818 _LIBCPP_CONSTEXPR_AFTER_CXX17
819 inline _LIBCPP_INLINE_VISIBILITY
820 void __do_compare_assert(long, _LHS &, _RHS &) {}
823 #endif // _LIBCPP_DEBUG
825 template <class _Comp>
826 struct __comp_ref_type {
827 // Pass the comparator by lvalue reference. Or in debug mode, using a
828 // debugging wrapper that stores a reference.
829 #ifndef _LIBCPP_DEBUG
830 typedef typename add_lvalue_reference<_Comp>::type type;
832 typedef __debug_less<_Comp> type;
838 template <class _InputIterator, class _Predicate>
839 _LIBCPP_NODISCARD_EXT inline
840 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
842 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
844 for (; __first != __last; ++__first)
845 if (!__pred(*__first))
852 template <class _InputIterator, class _Predicate>
853 _LIBCPP_NODISCARD_EXT inline
854 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
856 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
858 for (; __first != __last; ++__first)
859 if (__pred(*__first))
866 template <class _InputIterator, class _Predicate>
867 _LIBCPP_NODISCARD_EXT inline
868 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
870 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
872 for (; __first != __last; ++__first)
873 if (__pred(*__first))
880 template <class _InputIterator, class _Function>
881 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
883 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
885 for (; __first != __last; ++__first)
890 #if _LIBCPP_STD_VER > 14
893 template <class _InputIterator, class _Size, class _Function>
894 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
896 for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
898 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
899 _IntegralSize __n = __orig_n;
912 template <class _InputIterator, class _Tp>
913 _LIBCPP_NODISCARD_EXT inline
914 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
916 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
918 for (; __first != __last; ++__first)
919 if (*__first == __value_)
926 template <class _InputIterator, class _Predicate>
927 _LIBCPP_NODISCARD_EXT inline
928 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
930 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
932 for (; __first != __last; ++__first)
933 if (__pred(*__first))
940 template<class _InputIterator, class _Predicate>
941 _LIBCPP_NODISCARD_EXT inline
942 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
944 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
946 for (; __first != __last; ++__first)
947 if (!__pred(*__first))
954 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
955 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
956 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
957 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
958 forward_iterator_tag, forward_iterator_tag)
960 // modeled after search algorithm
961 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
962 if (__first2 == __last2)
968 if (__first1 == __last1) // if source exhausted return last correct answer
969 return __r; // (or __last1 if never found)
970 if (__pred(*__first1, *__first2))
974 // *__first1 matches *__first2, now match elements after here
975 _ForwardIterator1 __m1 = __first1;
976 _ForwardIterator2 __m2 = __first2;
979 if (++__m2 == __last2)
980 { // Pattern exhaused, record answer and search for another one
985 if (++__m1 == __last1) // Source exhausted, return last answer
987 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
991 } // else there is a match, check next elements
996 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
997 _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
998 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
999 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1000 bidirectional_iterator_tag, bidirectional_iterator_tag)
1002 // modeled after search algorithm (in reverse)
1003 if (__first2 == __last2)
1004 return __last1; // Everything matches an empty sequence
1005 _BidirectionalIterator1 __l1 = __last1;
1006 _BidirectionalIterator2 __l2 = __last2;
1010 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1013 if (__first1 == __l1) // return __last1 if no element matches *__first2
1015 if (__pred(*--__l1, *__l2))
1018 // *__l1 matches *__l2, now match elements before here
1019 _BidirectionalIterator1 __m1 = __l1;
1020 _BidirectionalIterator2 __m2 = __l2;
1023 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1025 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1027 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1030 } // else there is a match, check next elements
1035 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1036 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1037 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1038 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1039 random_access_iterator_tag, random_access_iterator_tag)
1041 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1042 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1045 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1046 if (__len1 < __len2)
1048 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1049 _RandomAccessIterator1 __l1 = __last1;
1050 _RandomAccessIterator2 __l2 = __last2;
1058 if (__pred(*--__l1, *__l2))
1061 _RandomAccessIterator1 __m1 = __l1;
1062 _RandomAccessIterator2 __m2 = __l2;
1065 if (__m2 == __first2)
1067 // no need to check range on __m1 because __s guarantees we have enough source
1068 if (!__pred(*--__m1, *--__m2))
1076 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1077 _LIBCPP_NODISCARD_EXT inline
1078 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1080 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1081 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1083 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1084 (__first1, __last1, __first2, __last2, __pred,
1085 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1086 typename iterator_traits<_ForwardIterator2>::iterator_category());
1089 template <class _ForwardIterator1, class _ForwardIterator2>
1090 _LIBCPP_NODISCARD_EXT inline
1091 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1093 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1094 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1096 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1097 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1098 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1103 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1104 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1105 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1106 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1108 for (; __first1 != __last1; ++__first1)
1109 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1110 if (__pred(*__first1, *__j))
1116 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1117 _LIBCPP_NODISCARD_EXT inline
1118 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1120 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1121 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1123 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1126 template <class _ForwardIterator1, class _ForwardIterator2>
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)
1133 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1134 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1135 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1140 template <class _ForwardIterator, class _BinaryPredicate>
1141 _LIBCPP_NODISCARD_EXT inline
1142 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1144 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1146 if (__first != __last)
1148 _ForwardIterator __i = __first;
1149 while (++__i != __last)
1151 if (__pred(*__first, *__i))
1159 template <class _ForwardIterator>
1160 _LIBCPP_NODISCARD_EXT inline
1161 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1163 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1165 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1166 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1171 template <class _InputIterator, class _Tp>
1172 _LIBCPP_NODISCARD_EXT inline
1173 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1174 typename iterator_traits<_InputIterator>::difference_type
1175 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1177 typename iterator_traits<_InputIterator>::difference_type __r(0);
1178 for (; __first != __last; ++__first)
1179 if (*__first == __value_)
1186 template <class _InputIterator, class _Predicate>
1187 _LIBCPP_NODISCARD_EXT inline
1188 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1189 typename iterator_traits<_InputIterator>::difference_type
1190 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1192 typename iterator_traits<_InputIterator>::difference_type __r(0);
1193 for (; __first != __last; ++__first)
1194 if (__pred(*__first))
1201 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1202 _LIBCPP_NODISCARD_EXT inline
1203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1204 pair<_InputIterator1, _InputIterator2>
1205 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1206 _InputIterator2 __first2, _BinaryPredicate __pred)
1208 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1209 if (!__pred(*__first1, *__first2))
1211 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1214 template <class _InputIterator1, class _InputIterator2>
1215 _LIBCPP_NODISCARD_EXT inline
1216 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1217 pair<_InputIterator1, _InputIterator2>
1218 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1220 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1221 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1222 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1225 #if _LIBCPP_STD_VER > 11
1226 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1227 _LIBCPP_NODISCARD_EXT inline
1228 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1229 pair<_InputIterator1, _InputIterator2>
1230 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1231 _InputIterator2 __first2, _InputIterator2 __last2,
1232 _BinaryPredicate __pred)
1234 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1235 if (!__pred(*__first1, *__first2))
1237 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1240 template <class _InputIterator1, class _InputIterator2>
1241 _LIBCPP_NODISCARD_EXT inline
1242 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1243 pair<_InputIterator1, _InputIterator2>
1244 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1245 _InputIterator2 __first2, _InputIterator2 __last2)
1247 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1248 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1249 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1255 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1256 _LIBCPP_NODISCARD_EXT inline
1257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1259 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1261 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1262 if (!__pred(*__first1, *__first2))
1267 template <class _InputIterator1, class _InputIterator2>
1268 _LIBCPP_NODISCARD_EXT inline
1269 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1271 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1273 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1274 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1275 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1278 #if _LIBCPP_STD_VER > 11
1279 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1280 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1282 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1283 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1284 input_iterator_tag, input_iterator_tag )
1286 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1287 if (!__pred(*__first1, *__first2))
1289 return __first1 == __last1 && __first2 == __last2;
1292 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1293 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1295 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1296 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1297 random_access_iterator_tag, random_access_iterator_tag )
1299 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1301 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1302 typename add_lvalue_reference<_BinaryPredicate>::type>
1303 (__first1, __last1, __first2, __pred );
1306 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1307 _LIBCPP_NODISCARD_EXT inline
1308 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1310 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1311 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1313 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1314 (__first1, __last1, __first2, __last2, __pred,
1315 typename iterator_traits<_InputIterator1>::iterator_category(),
1316 typename iterator_traits<_InputIterator2>::iterator_category());
1319 template <class _InputIterator1, class _InputIterator2>
1320 _LIBCPP_NODISCARD_EXT inline
1321 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1323 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1324 _InputIterator2 __first2, _InputIterator2 __last2)
1326 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1327 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1328 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1329 typename iterator_traits<_InputIterator1>::iterator_category(),
1330 typename iterator_traits<_InputIterator2>::iterator_category());
1336 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1337 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1338 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1339 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1341 // shorten sequences as much as possible by lopping of any equal prefix
1342 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1343 if (!__pred(*__first1, *__first2))
1345 if (__first1 == __last1)
1348 // __first1 != __last1 && *__first1 != *__first2
1349 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1350 _D1 __l1 = _VSTD::distance(__first1, __last1);
1353 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1354 // For each element in [f1, l1) see if there are the same number of
1355 // equal elements in [f2, l2)
1356 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1358 // Have we already counted the number of *__i in [f1, l1)?
1359 _ForwardIterator1 __match = __first1;
1360 for (; __match != __i; ++__match)
1361 if (__pred(*__match, *__i))
1363 if (__match == __i) {
1364 // Count number of *__i in [f2, l2)
1366 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1367 if (__pred(*__i, *__j))
1371 // Count number of *__i in [__i, l1) (we can start with 1)
1373 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1374 if (__pred(*__i, *__j))
1383 template<class _ForwardIterator1, class _ForwardIterator2>
1384 _LIBCPP_NODISCARD_EXT inline
1385 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1387 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1388 _ForwardIterator2 __first2)
1390 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1391 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1392 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1395 #if _LIBCPP_STD_VER > 11
1396 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1397 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1398 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1399 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1400 _BinaryPredicate __pred,
1401 forward_iterator_tag, forward_iterator_tag )
1403 // shorten sequences as much as possible by lopping of any equal prefix
1404 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1405 if (!__pred(*__first1, *__first2))
1407 if (__first1 == __last1)
1408 return __first2 == __last2;
1409 else if (__first2 == __last2)
1412 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1413 _D1 __l1 = _VSTD::distance(__first1, __last1);
1415 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1416 _D2 __l2 = _VSTD::distance(__first2, __last2);
1420 // For each element in [f1, l1) see if there are the same number of
1421 // equal elements in [f2, l2)
1422 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1424 // Have we already counted the number of *__i in [f1, l1)?
1425 _ForwardIterator1 __match = __first1;
1426 for (; __match != __i; ++__match)
1427 if (__pred(*__match, *__i))
1429 if (__match == __i) {
1430 // Count number of *__i in [f2, l2)
1432 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1433 if (__pred(*__i, *__j))
1437 // Count number of *__i in [__i, l1) (we can start with 1)
1439 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1440 if (__pred(*__i, *__j))
1449 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1450 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1451 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1452 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1453 _BinaryPredicate __pred,
1454 random_access_iterator_tag, random_access_iterator_tag )
1456 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1458 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1459 typename add_lvalue_reference<_BinaryPredicate>::type>
1460 (__first1, __last1, __first2, __pred );
1463 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1464 _LIBCPP_NODISCARD_EXT inline
1465 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1467 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1468 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1469 _BinaryPredicate __pred )
1471 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1472 (__first1, __last1, __first2, __last2, __pred,
1473 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1474 typename iterator_traits<_ForwardIterator2>::iterator_category());
1477 template<class _ForwardIterator1, class _ForwardIterator2>
1478 _LIBCPP_NODISCARD_EXT inline
1479 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1481 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1482 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1484 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1485 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1486 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1487 __equal_to<__v1, __v2>(),
1488 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1489 typename iterator_traits<_ForwardIterator2>::iterator_category());
1494 // __search is in <functional>
1496 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1497 _LIBCPP_NODISCARD_EXT inline
1498 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1500 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1501 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1503 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1504 (__first1, __last1, __first2, __last2, __pred,
1505 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1506 typename iterator_traits<_ForwardIterator2>::iterator_category())
1510 template <class _ForwardIterator1, class _ForwardIterator2>
1511 _LIBCPP_NODISCARD_EXT inline
1512 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1514 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1515 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1517 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1518 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1519 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1523 #if _LIBCPP_STD_VER > 14
1524 template <class _ForwardIterator, class _Searcher>
1525 _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1526 _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1527 { return __s(__f, __l).first; }
1532 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1533 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
1534 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1535 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1541 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1544 if (__first == __last) // return __last if no element matches __value_
1546 if (__pred(*__first, __value_))
1550 // *__first matches __value_, now match elements after here
1551 _ForwardIterator __m = __first;
1555 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1557 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1559 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1564 } // else there is a match, check next elements
1569 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1570 _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
1571 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1572 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1576 _Size __len = static_cast<_Size>(__last - __first);
1577 if (__len < __count)
1579 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1582 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1585 if (__first >= __s) // return __last if no element matches __value_
1587 if (__pred(*__first, __value_))
1591 // *__first matches __value_, now match elements after here
1592 _RandomAccessIterator __m = __first;
1596 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1598 ++__m; // no need to check range on __m because __s guarantees we have enough source
1599 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1604 } // else there is a match, check next elements
1609 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1610 _LIBCPP_NODISCARD_EXT inline
1611 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1613 search_n(_ForwardIterator __first, _ForwardIterator __last,
1614 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1616 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1617 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1618 typename iterator_traits<_ForwardIterator>::iterator_category());
1621 template <class _ForwardIterator, class _Size, class _Tp>
1622 _LIBCPP_NODISCARD_EXT inline
1623 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1625 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1627 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1628 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1629 __value_, __equal_to<__v, _Tp>());
1633 template <class _Iter>
1634 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1636 __unwrap_iter(_Iter __i)
1641 template <class _Tp>
1642 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1645 is_trivially_copy_assignable<_Tp>::value,
1648 __unwrap_iter(move_iterator<_Tp*> __i)
1653 #if _LIBCPP_DEBUG_LEVEL < 2
1655 template <class _Tp>
1656 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1659 is_trivially_copy_assignable<_Tp>::value,
1662 __unwrap_iter(__wrap_iter<_Tp*> __i)
1667 template <class _Tp>
1668 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1671 is_trivially_copy_assignable<_Tp>::value,
1674 __unwrap_iter(__wrap_iter<const _Tp*> __i)
1681 template <class _Tp>
1682 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1685 is_trivially_copy_assignable<_Tp>::value,
1688 __unwrap_iter(__wrap_iter<_Tp*> __i)
1693 #endif // _LIBCPP_DEBUG_LEVEL < 2
1695 template <class _InputIterator, class _OutputIterator>
1696 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1698 __copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1700 for (; __first != __last; ++__first, (void) ++__result)
1701 *__result = *__first;
1705 template <class _InputIterator, class _OutputIterator>
1706 inline _LIBCPP_INLINE_VISIBILITY
1708 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1710 return __copy_constexpr(__first, __last, __result);
1713 template <class _Tp, class _Up>
1714 inline _LIBCPP_INLINE_VISIBILITY
1717 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1718 is_trivially_copy_assignable<_Up>::value,
1721 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1723 const size_t __n = static_cast<size_t>(__last - __first);
1725 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1726 return __result + __n;
1729 template <class _InputIterator, class _OutputIterator>
1730 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17_WITH_IS_CONSTANT_EVALUATED
1732 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1734 if (__libcpp_is_constant_evaluated()) {
1735 return _VSTD::__copy_constexpr(
1736 __unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1738 return _VSTD::__copy(
1739 __unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1745 template <class _BidirectionalIterator, class _OutputIterator>
1746 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1748 __copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1750 while (__first != __last)
1751 *--__result = *--__last;
1755 template <class _BidirectionalIterator, class _OutputIterator>
1756 inline _LIBCPP_INLINE_VISIBILITY
1758 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1760 return __copy_backward_constexpr(__first, __last, __result);
1763 template <class _Tp, class _Up>
1764 inline _LIBCPP_INLINE_VISIBILITY
1767 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1768 is_trivially_copy_assignable<_Up>::value,
1771 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1773 const size_t __n = static_cast<size_t>(__last - __first);
1777 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1782 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1783 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17_WITH_IS_CONSTANT_EVALUATED
1784 _BidirectionalIterator2
1785 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1786 _BidirectionalIterator2 __result)
1788 if (__libcpp_is_constant_evaluated()) {
1789 return _VSTD::__copy_backward_constexpr(__unwrap_iter(__first),
1790 __unwrap_iter(__last),
1791 __unwrap_iter(__result));
1793 return _VSTD::__copy_backward(__unwrap_iter(__first),
1794 __unwrap_iter(__last),
1795 __unwrap_iter(__result));
1801 template<class _InputIterator, class _OutputIterator, class _Predicate>
1802 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1804 copy_if(_InputIterator __first, _InputIterator __last,
1805 _OutputIterator __result, _Predicate __pred)
1807 for (; __first != __last; ++__first)
1809 if (__pred(*__first))
1811 *__result = *__first;
1820 template<class _InputIterator, class _Size, class _OutputIterator>
1821 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17_WITH_IS_CONSTANT_EVALUATED
1824 __is_cpp17_input_iterator<_InputIterator>::value &&
1825 !__is_cpp17_random_access_iterator<_InputIterator>::value,
1828 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1830 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1831 _IntegralSize __n = __orig_n;
1834 *__result = *__first;
1836 for (--__n; __n > 0; --__n)
1839 *__result = *__first;
1846 template<class _InputIterator, class _Size, class _OutputIterator>
1847 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17_WITH_IS_CONSTANT_EVALUATED
1850 __is_cpp17_random_access_iterator<_InputIterator>::value,
1853 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1855 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1856 _IntegralSize __n = __orig_n;
1857 return _VSTD::copy(__first, __first + __n, __result);
1862 template <class _InputIterator, class _OutputIterator>
1863 inline _LIBCPP_INLINE_VISIBILITY
1865 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1867 for (; __first != __last; ++__first, (void) ++__result)
1868 *__result = _VSTD::move(*__first);
1872 template <class _Tp, class _Up>
1873 inline _LIBCPP_INLINE_VISIBILITY
1876 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1877 is_trivially_copy_assignable<_Up>::value,
1880 __move(_Tp* __first, _Tp* __last, _Up* __result)
1882 const size_t __n = static_cast<size_t>(__last - __first);
1884 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1885 return __result + __n;
1888 template <class _InputIterator, class _OutputIterator>
1889 inline _LIBCPP_INLINE_VISIBILITY
1891 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1893 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1898 template <class _InputIterator, class _OutputIterator>
1899 inline _LIBCPP_INLINE_VISIBILITY
1901 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1903 while (__first != __last)
1904 *--__result = _VSTD::move(*--__last);
1908 template <class _Tp, class _Up>
1909 inline _LIBCPP_INLINE_VISIBILITY
1912 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1913 is_trivially_copy_assignable<_Up>::value,
1916 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1918 const size_t __n = static_cast<size_t>(__last - __first);
1922 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1927 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1928 inline _LIBCPP_INLINE_VISIBILITY
1929 _BidirectionalIterator2
1930 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1931 _BidirectionalIterator2 __result)
1933 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1938 // moved to <type_traits> for better swap / noexcept support
1942 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1943 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1945 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1947 for (; __first != __last; ++__first, (void) ++__result)
1948 *__result = __op(*__first);
1952 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1953 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1955 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1956 _OutputIterator __result, _BinaryOperation __binary_op)
1958 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1959 *__result = __binary_op(*__first1, *__first2);
1965 template <class _ForwardIterator, class _Tp>
1966 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1968 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1970 for (; __first != __last; ++__first)
1971 if (*__first == __old_value)
1972 *__first = __new_value;
1977 template <class _ForwardIterator, class _Predicate, class _Tp>
1978 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1980 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1982 for (; __first != __last; ++__first)
1983 if (__pred(*__first))
1984 *__first = __new_value;
1989 template <class _InputIterator, class _OutputIterator, class _Tp>
1990 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1992 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1993 const _Tp& __old_value, const _Tp& __new_value)
1995 for (; __first != __last; ++__first, (void) ++__result)
1996 if (*__first == __old_value)
1997 *__result = __new_value;
1999 *__result = *__first;
2005 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2006 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2008 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2009 _Predicate __pred, const _Tp& __new_value)
2011 for (; __first != __last; ++__first, (void) ++__result)
2012 if (__pred(*__first))
2013 *__result = __new_value;
2015 *__result = *__first;
2021 template <class _OutputIterator, class _Size, class _Tp>
2022 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2024 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2026 for (; __n > 0; ++__first, (void) --__n)
2027 *__first = __value_;
2031 template <class _OutputIterator, class _Size, class _Tp>
2032 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2034 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2036 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
2041 template <class _ForwardIterator, class _Tp>
2042 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2044 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2046 for (; __first != __last; ++__first)
2047 *__first = __value_;
2050 template <class _RandomAccessIterator, class _Tp>
2051 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2053 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2055 _VSTD::fill_n(__first, __last - __first, __value_);
2058 template <class _ForwardIterator, class _Tp>
2059 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2061 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2063 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2068 template <class _ForwardIterator, class _Generator>
2069 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2071 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2073 for (; __first != __last; ++__first)
2079 template <class _OutputIterator, class _Size, class _Generator>
2080 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2082 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2084 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2085 _IntegralSize __n = __orig_n;
2086 for (; __n > 0; ++__first, (void) --__n)
2093 template <class _ForwardIterator, class _Tp>
2094 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2095 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2097 __first = _VSTD::find(__first, __last, __value_);
2098 if (__first != __last)
2100 _ForwardIterator __i = __first;
2101 while (++__i != __last)
2103 if (!(*__i == __value_))
2105 *__first = _VSTD::move(*__i);
2115 template <class _ForwardIterator, class _Predicate>
2116 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2117 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2119 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2120 (__first, __last, __pred);
2121 if (__first != __last)
2123 _ForwardIterator __i = __first;
2124 while (++__i != __last)
2128 *__first = _VSTD::move(*__i);
2138 template <class _InputIterator, class _OutputIterator, class _Tp>
2139 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2141 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2143 for (; __first != __last; ++__first)
2145 if (!(*__first == __value_))
2147 *__result = *__first;
2156 template <class _InputIterator, class _OutputIterator, class _Predicate>
2157 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2159 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2161 for (; __first != __last; ++__first)
2163 if (!__pred(*__first))
2165 *__result = *__first;
2174 template <class _ForwardIterator, class _BinaryPredicate>
2175 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2176 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2178 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2179 (__first, __last, __pred);
2180 if (__first != __last)
2184 _ForwardIterator __i = __first;
2185 for (++__i; ++__i != __last;)
2186 if (!__pred(*__first, *__i))
2187 *++__first = _VSTD::move(*__i);
2193 template <class _ForwardIterator>
2194 _LIBCPP_NODISCARD_EXT inline
2195 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2197 unique(_ForwardIterator __first, _ForwardIterator __last)
2199 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2200 return _VSTD::unique(__first, __last, __equal_to<__v>());
2205 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2206 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2207 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2208 input_iterator_tag, output_iterator_tag)
2210 if (__first != __last)
2212 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2215 while (++__first != __last)
2217 if (!__pred(__t, *__first))
2228 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2229 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2230 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2231 forward_iterator_tag, output_iterator_tag)
2233 if (__first != __last)
2235 _ForwardIterator __i = __first;
2238 while (++__first != __last)
2240 if (!__pred(*__i, *__first))
2242 *__result = *__first;
2251 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2252 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2253 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2254 input_iterator_tag, forward_iterator_tag)
2256 if (__first != __last)
2258 *__result = *__first;
2259 while (++__first != __last)
2260 if (!__pred(*__result, *__first))
2261 *++__result = *__first;
2267 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2268 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2270 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2272 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2273 (__first, __last, __result, __pred,
2274 typename iterator_traits<_InputIterator>::iterator_category(),
2275 typename iterator_traits<_OutputIterator>::iterator_category());
2278 template <class _InputIterator, class _OutputIterator>
2279 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2281 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2283 typedef typename iterator_traits<_InputIterator>::value_type __v;
2284 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2289 template <class _BidirectionalIterator>
2290 inline _LIBCPP_INLINE_VISIBILITY
2292 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2294 while (__first != __last)
2296 if (__first == --__last)
2298 _VSTD::iter_swap(__first, __last);
2303 template <class _RandomAccessIterator>
2304 inline _LIBCPP_INLINE_VISIBILITY
2306 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2308 if (__first != __last)
2309 for (; __first < --__last; ++__first)
2310 _VSTD::iter_swap(__first, __last);
2313 template <class _BidirectionalIterator>
2314 inline _LIBCPP_INLINE_VISIBILITY
2316 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2318 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2323 template <class _BidirectionalIterator, class _OutputIterator>
2324 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2326 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2328 for (; __first != __last; ++__result)
2329 *__result = *--__last;
2335 template <class _ForwardIterator>
2337 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2339 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2340 value_type __tmp = _VSTD::move(*__first);
2341 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2342 *__lm1 = _VSTD::move(__tmp);
2346 template <class _BidirectionalIterator>
2347 _BidirectionalIterator
2348 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2350 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2351 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2352 value_type __tmp = _VSTD::move(*__lm1);
2353 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2354 *__first = _VSTD::move(__tmp);
2358 template <class _ForwardIterator>
2360 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2362 _ForwardIterator __i = __middle;
2365 swap(*__first, *__i);
2367 if (++__i == __last)
2369 if (__first == __middle)
2372 _ForwardIterator __r = __first;
2373 if (__first != __middle)
2378 swap(*__first, *__i);
2380 if (++__i == __last)
2382 if (__first == __middle)
2386 else if (__first == __middle)
2393 template<typename _Integral>
2394 inline _LIBCPP_INLINE_VISIBILITY
2396 __algo_gcd(_Integral __x, _Integral __y)
2400 _Integral __t = __x % __y;
2407 template<typename _RandomAccessIterator>
2408 _RandomAccessIterator
2409 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2411 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2412 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2414 const difference_type __m1 = __middle - __first;
2415 const difference_type __m2 = __last - __middle;
2418 _VSTD::swap_ranges(__first, __middle, __middle);
2421 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
2422 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2424 value_type __t(_VSTD::move(*--__p));
2425 _RandomAccessIterator __p1 = __p;
2426 _RandomAccessIterator __p2 = __p1 + __m1;
2429 *__p1 = _VSTD::move(*__p2);
2431 const difference_type __d = __last - __p2;
2435 __p2 = __first + (__m1 - __d);
2436 } while (__p2 != __p);
2437 *__p1 = _VSTD::move(__t);
2439 return __first + __m2;
2442 template <class _ForwardIterator>
2443 inline _LIBCPP_INLINE_VISIBILITY
2445 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2446 _VSTD::forward_iterator_tag)
2448 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2449 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2451 if (_VSTD::next(__first) == __middle)
2452 return _VSTD::__rotate_left(__first, __last);
2454 return _VSTD::__rotate_forward(__first, __middle, __last);
2457 template <class _BidirectionalIterator>
2458 inline _LIBCPP_INLINE_VISIBILITY
2459 _BidirectionalIterator
2460 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2461 _VSTD::bidirectional_iterator_tag)
2463 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2464 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2466 if (_VSTD::next(__first) == __middle)
2467 return _VSTD::__rotate_left(__first, __last);
2468 if (_VSTD::next(__middle) == __last)
2469 return _VSTD::__rotate_right(__first, __last);
2471 return _VSTD::__rotate_forward(__first, __middle, __last);
2474 template <class _RandomAccessIterator>
2475 inline _LIBCPP_INLINE_VISIBILITY
2476 _RandomAccessIterator
2477 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2478 _VSTD::random_access_iterator_tag)
2480 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2481 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2483 if (_VSTD::next(__first) == __middle)
2484 return _VSTD::__rotate_left(__first, __last);
2485 if (_VSTD::next(__middle) == __last)
2486 return _VSTD::__rotate_right(__first, __last);
2487 return _VSTD::__rotate_gcd(__first, __middle, __last);
2489 return _VSTD::__rotate_forward(__first, __middle, __last);
2492 template <class _ForwardIterator>
2493 inline _LIBCPP_INLINE_VISIBILITY
2495 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2497 if (__first == __middle)
2499 if (__middle == __last)
2501 return _VSTD::__rotate(__first, __middle, __last,
2502 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2507 template <class _ForwardIterator, class _OutputIterator>
2508 inline _LIBCPP_INLINE_VISIBILITY
2510 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2512 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2517 template <class _ForwardIterator, class _Compare>
2518 _LIBCPP_NODISCARD_EXT inline
2519 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2521 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2523 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2524 "std::min_element requires a ForwardIterator");
2525 if (__first != __last)
2527 _ForwardIterator __i = __first;
2528 while (++__i != __last)
2529 if (__comp(*__i, *__first))
2535 template <class _ForwardIterator>
2536 _LIBCPP_NODISCARD_EXT inline
2537 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2539 min_element(_ForwardIterator __first, _ForwardIterator __last)
2541 return _VSTD::min_element(__first, __last,
2542 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2547 template <class _Tp, class _Compare>
2548 _LIBCPP_NODISCARD_EXT inline
2549 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2551 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2553 return __comp(__b, __a) ? __b : __a;
2556 template <class _Tp>
2557 _LIBCPP_NODISCARD_EXT inline
2558 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2560 min(const _Tp& __a, const _Tp& __b)
2562 return _VSTD::min(__a, __b, __less<_Tp>());
2565 #ifndef _LIBCPP_CXX03_LANG
2567 template<class _Tp, class _Compare>
2568 _LIBCPP_NODISCARD_EXT inline
2569 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2571 min(initializer_list<_Tp> __t, _Compare __comp)
2573 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2577 _LIBCPP_NODISCARD_EXT inline
2578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2580 min(initializer_list<_Tp> __t)
2582 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2585 #endif // _LIBCPP_CXX03_LANG
2589 template <class _ForwardIterator, class _Compare>
2590 _LIBCPP_NODISCARD_EXT inline
2591 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2593 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2595 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2596 "std::max_element requires a ForwardIterator");
2597 if (__first != __last)
2599 _ForwardIterator __i = __first;
2600 while (++__i != __last)
2601 if (__comp(*__first, *__i))
2608 template <class _ForwardIterator>
2609 _LIBCPP_NODISCARD_EXT inline
2610 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2612 max_element(_ForwardIterator __first, _ForwardIterator __last)
2614 return _VSTD::max_element(__first, __last,
2615 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2620 template <class _Tp, class _Compare>
2621 _LIBCPP_NODISCARD_EXT inline
2622 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2624 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2626 return __comp(__a, __b) ? __b : __a;
2629 template <class _Tp>
2630 _LIBCPP_NODISCARD_EXT inline
2631 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2633 max(const _Tp& __a, const _Tp& __b)
2635 return _VSTD::max(__a, __b, __less<_Tp>());
2638 #ifndef _LIBCPP_CXX03_LANG
2640 template<class _Tp, class _Compare>
2641 _LIBCPP_NODISCARD_EXT inline
2642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2644 max(initializer_list<_Tp> __t, _Compare __comp)
2646 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2650 _LIBCPP_NODISCARD_EXT inline
2651 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2653 max(initializer_list<_Tp> __t)
2655 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2658 #endif // _LIBCPP_CXX03_LANG
2660 #if _LIBCPP_STD_VER > 14
2662 template<class _Tp, class _Compare>
2663 _LIBCPP_NODISCARD_EXT inline
2664 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2666 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2668 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2669 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2674 _LIBCPP_NODISCARD_EXT inline
2675 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2677 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2679 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2685 template <class _ForwardIterator, class _Compare>
2686 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
2687 std::pair<_ForwardIterator, _ForwardIterator>
2688 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2690 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
2691 "std::minmax_element requires a ForwardIterator");
2692 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2693 if (__first != __last)
2695 if (++__first != __last)
2697 if (__comp(*__first, *__result.first))
2698 __result.first = __first;
2700 __result.second = __first;
2701 while (++__first != __last)
2703 _ForwardIterator __i = __first;
2704 if (++__first == __last)
2706 if (__comp(*__i, *__result.first))
2707 __result.first = __i;
2708 else if (!__comp(*__i, *__result.second))
2709 __result.second = __i;
2714 if (__comp(*__first, *__i))
2716 if (__comp(*__first, *__result.first))
2717 __result.first = __first;
2718 if (!__comp(*__i, *__result.second))
2719 __result.second = __i;
2723 if (__comp(*__i, *__result.first))
2724 __result.first = __i;
2725 if (!__comp(*__first, *__result.second))
2726 __result.second = __first;
2735 template <class _ForwardIterator>
2736 _LIBCPP_NODISCARD_EXT inline
2737 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2738 std::pair<_ForwardIterator, _ForwardIterator>
2739 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2741 return _VSTD::minmax_element(__first, __last,
2742 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2747 template<class _Tp, class _Compare>
2748 _LIBCPP_NODISCARD_EXT inline
2749 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2750 pair<const _Tp&, const _Tp&>
2751 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2753 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2754 pair<const _Tp&, const _Tp&>(__a, __b);
2758 _LIBCPP_NODISCARD_EXT inline
2759 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2760 pair<const _Tp&, const _Tp&>
2761 minmax(const _Tp& __a, const _Tp& __b)
2763 return _VSTD::minmax(__a, __b, __less<_Tp>());
2766 #ifndef _LIBCPP_CXX03_LANG
2768 template<class _Tp, class _Compare>
2769 _LIBCPP_NODISCARD_EXT inline
2770 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2772 minmax(initializer_list<_Tp> __t, _Compare __comp)
2774 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2775 _Iter __first = __t.begin();
2776 _Iter __last = __t.end();
2777 std::pair<_Tp, _Tp> __result(*__first, *__first);
2780 if (__t.size() % 2 == 0)
2782 if (__comp(*__first, __result.first))
2783 __result.first = *__first;
2785 __result.second = *__first;
2789 while (__first != __last)
2791 _Tp __prev = *__first++;
2792 if (__comp(*__first, __prev)) {
2793 if ( __comp(*__first, __result.first)) __result.first = *__first;
2794 if (!__comp(__prev, __result.second)) __result.second = __prev;
2797 if ( __comp(__prev, __result.first)) __result.first = __prev;
2798 if (!__comp(*__first, __result.second)) __result.second = *__first;
2807 _LIBCPP_NODISCARD_EXT inline
2808 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2810 minmax(initializer_list<_Tp> __t)
2812 return _VSTD::minmax(__t, __less<_Tp>());
2815 #endif // _LIBCPP_CXX03_LANG
2819 // __independent_bits_engine
2821 template <unsigned long long _Xp, size_t _Rp>
2824 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2825 : __log2_imp<_Xp, _Rp - 1>::value;
2828 template <unsigned long long _Xp>
2829 struct __log2_imp<_Xp, 0>
2831 static const size_t value = 0;
2834 template <size_t _Rp>
2835 struct __log2_imp<0, _Rp>
2837 static const size_t value = _Rp + 1;
2840 template <class _UIntType, _UIntType _Xp>
2843 static const size_t value = __log2_imp<_Xp,
2844 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
2847 template<class _Engine, class _UIntType>
2848 class __independent_bits_engine
2852 typedef _UIntType result_type;
2855 typedef typename _Engine::result_type _Engine_result_type;
2856 typedef typename conditional
2858 sizeof(_Engine_result_type) <= sizeof(result_type),
2861 >::type _Working_result_type;
2868 _Working_result_type __y0_;
2869 _Working_result_type __y1_;
2870 _Engine_result_type __mask0_;
2871 _Engine_result_type __mask1_;
2873 #ifdef _LIBCPP_CXX03_LANG
2874 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2875 + _Working_result_type(1);
2877 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2878 + _Working_result_type(1);
2880 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2881 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2882 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2885 // constructors and seeding functions
2886 __independent_bits_engine(_Engine& __e, size_t __w);
2888 // generating functions
2889 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2892 result_type __eval(false_type);
2893 result_type __eval(true_type);
2896 template<class _Engine, class _UIntType>
2897 __independent_bits_engine<_Engine, _UIntType>
2898 ::__independent_bits_engine(_Engine& __e, size_t __w)
2902 __n_ = __w_ / __m + (__w_ % __m != 0);
2903 __w0_ = __w_ / __n_;
2906 else if (__w0_ < _WDt)
2907 __y0_ = (_Rp >> __w0_) << __w0_;
2910 if (_Rp - __y0_ > __y0_ / __n_)
2913 __w0_ = __w_ / __n_;
2915 __y0_ = (_Rp >> __w0_) << __w0_;
2919 __n0_ = __n_ - __w_ % __n_;
2920 if (__w0_ < _WDt - 1)
2921 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2924 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2925 _Engine_result_type(0);
2926 __mask1_ = __w0_ < _EDt - 1 ?
2927 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2928 _Engine_result_type(~0);
2931 template<class _Engine, class _UIntType>
2934 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2936 return static_cast<result_type>(__e_() & __mask0_);
2939 template<class _Engine, class _UIntType>
2941 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2943 const size_t _WRt = numeric_limits<result_type>::digits;
2944 result_type _Sp = 0;
2945 for (size_t __k = 0; __k < __n0_; ++__k)
2947 _Engine_result_type __u;
2950 __u = __e_() - _Engine::min();
2951 } while (__u >= __y0_);
2956 _Sp += __u & __mask0_;
2958 for (size_t __k = __n0_; __k < __n_; ++__k)
2960 _Engine_result_type __u;
2963 __u = __e_() - _Engine::min();
2964 } while (__u >= __y1_);
2965 if (__w0_ < _WRt - 1)
2969 _Sp += __u & __mask1_;
2974 // uniform_int_distribution
2976 template<class _IntType = int>
2977 class uniform_int_distribution
2981 typedef _IntType result_type;
2988 typedef uniform_int_distribution distribution_type;
2990 explicit param_type(result_type __a = 0,
2991 result_type __b = numeric_limits<result_type>::max())
2992 : __a_(__a), __b_(__b) {}
2994 result_type a() const {return __a_;}
2995 result_type b() const {return __b_;}
2997 friend bool operator==(const param_type& __x, const param_type& __y)
2998 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2999 friend bool operator!=(const param_type& __x, const param_type& __y)
3000 {return !(__x == __y);}
3007 // constructors and reset functions
3008 explicit uniform_int_distribution(result_type __a = 0,
3009 result_type __b = numeric_limits<result_type>::max())
3010 : __p_(param_type(__a, __b)) {}
3011 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3014 // generating functions
3015 template<class _URNG> result_type operator()(_URNG& __g)
3016 {return (*this)(__g, __p_);}
3017 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3019 // property functions
3020 result_type a() const {return __p_.a();}
3021 result_type b() const {return __p_.b();}
3023 param_type param() const {return __p_;}
3024 void param(const param_type& __p) {__p_ = __p;}
3026 result_type min() const {return a();}
3027 result_type max() const {return b();}
3029 friend bool operator==(const uniform_int_distribution& __x,
3030 const uniform_int_distribution& __y)
3031 {return __x.__p_ == __y.__p_;}
3032 friend bool operator!=(const uniform_int_distribution& __x,
3033 const uniform_int_distribution& __y)
3034 {return !(__x == __y);}
3037 template<class _IntType>
3038 template<class _URNG>
3039 typename uniform_int_distribution<_IntType>::result_type
3040 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3041 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
3043 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3044 uint32_t, uint64_t>::type _UIntType;
3045 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
3048 const size_t _Dt = numeric_limits<_UIntType>::digits;
3049 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3051 return static_cast<result_type>(_Eng(__g, _Dt)());
3052 size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
3053 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3060 } while (__u >= _Rp);
3061 return static_cast<result_type>(__u + __p.a());
3064 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3065 || defined(_LIBCPP_BUILDING_LIBRARY)
3066 class _LIBCPP_TYPE_VIS __rs_default;
3068 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3070 class _LIBCPP_TYPE_VIS __rs_default
3072 static unsigned __c_;
3076 typedef uint_fast32_t result_type;
3078 static const result_type _Min = 0;
3079 static const result_type _Max = 0xFFFFFFFF;
3081 __rs_default(const __rs_default&);
3084 result_type operator()();
3086 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3087 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3089 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3092 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3094 template <class _RandomAccessIterator>
3095 _LIBCPP_DEPRECATED_IN_CXX14 void
3096 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3098 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3099 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3100 typedef typename _Dp::param_type _Pp;
3101 difference_type __d = __last - __first;
3105 __rs_default __g = __rs_get();
3106 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3108 difference_type __i = __uid(__g, _Pp(0, __d));
3109 if (__i != difference_type(0))
3110 swap(*__first, *(__first + __i));
3115 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3116 _LIBCPP_DEPRECATED_IN_CXX14 void
3117 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3118 #ifndef _LIBCPP_CXX03_LANG
3119 _RandomNumberGenerator&& __rand)
3121 _RandomNumberGenerator& __rand)
3124 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3125 difference_type __d = __last - __first;
3128 for (--__last; __first < __last; ++__first, (void) --__d)
3130 difference_type __i = __rand(__d);
3131 if (__i != difference_type(0))
3132 swap(*__first, *(__first + __i));
3138 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3139 class _UniformRandomNumberGenerator>
3140 _LIBCPP_INLINE_VISIBILITY
3141 _SampleIterator __sample(_PopulationIterator __first,
3142 _PopulationIterator __last, _SampleIterator __output_iter,
3144 _UniformRandomNumberGenerator & __g,
3145 input_iterator_tag) {
3148 for (; __first != __last && __k < __n; ++__first, (void) ++__k)
3149 __output_iter[__k] = *__first;
3150 _Distance __sz = __k;
3151 for (; __first != __last; ++__first, (void) ++__k) {
3152 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3154 __output_iter[__r] = *__first;
3156 return __output_iter + _VSTD::min(__n, __k);
3159 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3160 class _UniformRandomNumberGenerator>
3161 _LIBCPP_INLINE_VISIBILITY
3162 _SampleIterator __sample(_PopulationIterator __first,
3163 _PopulationIterator __last, _SampleIterator __output_iter,
3165 _UniformRandomNumberGenerator& __g,
3166 forward_iterator_tag) {
3167 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3168 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3170 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3172 *__output_iter++ = *__first;
3176 return __output_iter;
3179 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3180 class _UniformRandomNumberGenerator>
3181 _LIBCPP_INLINE_VISIBILITY
3182 _SampleIterator __sample(_PopulationIterator __first,
3183 _PopulationIterator __last, _SampleIterator __output_iter,
3184 _Distance __n, _UniformRandomNumberGenerator& __g) {
3185 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3187 typedef typename iterator_traits<_PopulationIterator>::difference_type
3189 static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
3190 __is_cpp17_random_access_iterator<_SampleIterator>::value,
3191 "SampleIterator must meet the requirements of RandomAccessIterator");
3192 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3193 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3194 return _VSTD::__sample(
3195 __first, __last, __output_iter, _CommonType(__n),
3196 __g, _PopCategory());
3199 #if _LIBCPP_STD_VER > 14
3200 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3201 class _UniformRandomNumberGenerator>
3202 inline _LIBCPP_INLINE_VISIBILITY
3203 _SampleIterator sample(_PopulationIterator __first,
3204 _PopulationIterator __last, _SampleIterator __output_iter,
3205 _Distance __n, _UniformRandomNumberGenerator&& __g) {
3206 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
3208 #endif // _LIBCPP_STD_VER > 14
3210 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3211 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3212 _UniformRandomNumberGenerator&& __g)
3214 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3215 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3216 typedef typename _Dp::param_type _Pp;
3217 difference_type __d = __last - __first;
3221 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
3223 difference_type __i = __uid(__g, _Pp(0, __d));
3224 if (__i != difference_type(0))
3225 swap(*__first, *(__first + __i));
3230 template <class _InputIterator, class _Predicate>
3231 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
3232 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3234 for (; __first != __last; ++__first)
3235 if (!__pred(*__first))
3237 if ( __first == __last )
3240 for (; __first != __last; ++__first)
3241 if (__pred(*__first))
3248 template <class _Predicate, class _ForwardIterator>
3250 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3254 if (__first == __last)
3256 if (!__pred(*__first))
3260 for (_ForwardIterator __p = __first; ++__p != __last;)
3264 swap(*__first, *__p);
3271 template <class _Predicate, class _BidirectionalIterator>
3272 _BidirectionalIterator
3273 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3274 bidirectional_iterator_tag)
3280 if (__first == __last)
3282 if (!__pred(*__first))
3288 if (__first == --__last)
3290 } while (!__pred(*__last));
3291 swap(*__first, *__last);
3296 template <class _ForwardIterator, class _Predicate>
3297 inline _LIBCPP_INLINE_VISIBILITY
3299 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3301 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3302 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3307 template <class _InputIterator, class _OutputIterator1,
3308 class _OutputIterator2, class _Predicate>
3309 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
3310 partition_copy(_InputIterator __first, _InputIterator __last,
3311 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3314 for (; __first != __last; ++__first)
3316 if (__pred(*__first))
3318 *__out_true = *__first;
3323 *__out_false = *__first;
3327 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3332 template<class _ForwardIterator, class _Predicate>
3333 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3334 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3336 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3337 difference_type __len = _VSTD::distance(__first, __last);
3340 difference_type __l2 = _VSTD::__half_positive(__len);
3341 _ForwardIterator __m = __first;
3342 _VSTD::advance(__m, __l2);
3356 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3358 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3359 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3361 // *__first is known to be false
3367 _ForwardIterator __m = __first;
3370 swap(*__first, *__m);
3375 if (__len <= __p.second)
3376 { // The buffer is big enough to use
3377 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3378 __destruct_n __d(0);
3379 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3380 // Move the falses into the temporary buffer, and the trues to the front of the line
3381 // Update __first to always point to the end of the trues
3382 value_type* __t = __p.first;
3383 ::new(__t) value_type(_VSTD::move(*__first));
3384 __d.__incr((value_type*)0);
3386 _ForwardIterator __i = __first;
3387 while (++__i != __last)
3391 *__first = _VSTD::move(*__i);
3396 ::new(__t) value_type(_VSTD::move(*__i));
3397 __d.__incr((value_type*)0);
3401 // All trues now at start of range, all falses in buffer
3402 // Move falses back into range, but don't mess up __first which points to first false
3404 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3405 *__i = _VSTD::move(*__t2);
3406 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3409 // Else not enough buffer, do in place
3411 _ForwardIterator __m = __first;
3412 _Distance __len2 = __len / 2; // __len2 >= 2
3413 _VSTD::advance(__m, __len2);
3414 // recurse on [__first, __m), *__first know to be false
3415 // F?????????????????
3417 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3418 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3419 // TTTFFFFF??????????
3421 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3422 _ForwardIterator __m1 = __m;
3423 _ForwardIterator __second_false = __last;
3424 _Distance __len_half = __len - __len2;
3425 while (__pred(*__m1))
3427 if (++__m1 == __last)
3428 goto __second_half_done;
3431 // TTTFFFFFTTTF??????
3433 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3435 // TTTFFFFFTTTTTFFFFF
3437 return _VSTD::rotate(__first_false, __m, __second_false);
3438 // TTTTTTTTFFFFFFFFFF
3442 struct __return_temporary_buffer
3444 template <class _Tp>
3445 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3448 template <class _Predicate, class _ForwardIterator>
3450 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3451 forward_iterator_tag)
3453 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3454 // Either prove all true and return __first or point to first false
3457 if (__first == __last)
3459 if (!__pred(*__first))
3463 // We now have a reduced range [__first, __last)
3464 // *__first is known to be false
3465 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3466 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3467 difference_type __len = _VSTD::distance(__first, __last);
3468 pair<value_type*, ptrdiff_t> __p(0, 0);
3469 unique_ptr<value_type, __return_temporary_buffer> __h;
3470 if (__len >= __alloc_limit)
3472 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3473 __h.reset(__p.first);
3475 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3476 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3479 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3480 _BidirectionalIterator
3481 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3482 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3484 // *__first is known to be false
3485 // *__last is known to be true
3489 swap(*__first, *__last);
3494 _BidirectionalIterator __m = __first;
3497 swap(*__first, *__m);
3498 swap(*__m, *__last);
3501 swap(*__m, *__last);
3502 swap(*__first, *__m);
3505 if (__len <= __p.second)
3506 { // The buffer is big enough to use
3507 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3508 __destruct_n __d(0);
3509 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3510 // Move the falses into the temporary buffer, and the trues to the front of the line
3511 // Update __first to always point to the end of the trues
3512 value_type* __t = __p.first;
3513 ::new(__t) value_type(_VSTD::move(*__first));
3514 __d.__incr((value_type*)0);
3516 _BidirectionalIterator __i = __first;
3517 while (++__i != __last)
3521 *__first = _VSTD::move(*__i);
3526 ::new(__t) value_type(_VSTD::move(*__i));
3527 __d.__incr((value_type*)0);
3531 // move *__last, known to be true
3532 *__first = _VSTD::move(*__i);
3534 // All trues now at start of range, all falses in buffer
3535 // Move falses back into range, but don't mess up __first which points to first false
3536 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
3537 *__i = _VSTD::move(*__t2);
3538 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3541 // Else not enough buffer, do in place
3543 _BidirectionalIterator __m = __first;
3544 _Distance __len2 = __len / 2; // __len2 >= 2
3545 _VSTD::advance(__m, __len2);
3546 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3547 // F????????????????T
3549 _BidirectionalIterator __m1 = __m;
3550 _BidirectionalIterator __first_false = __first;
3551 _Distance __len_half = __len2;
3552 while (!__pred(*--__m1))
3554 if (__m1 == __first)
3555 goto __first_half_done;
3558 // F???TFFF?????????T
3560 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3561 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3563 // TTTFFFFF?????????T
3565 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3567 _BidirectionalIterator __second_false = __last;
3569 __len_half = __len - __len2;
3570 while (__pred(*__m1))
3572 if (++__m1 == __last)
3573 goto __second_half_done;
3576 // TTTFFFFFTTTF?????T
3578 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3580 // TTTFFFFFTTTTTFFFFF
3582 return _VSTD::rotate(__first_false, __m, __second_false);
3583 // TTTTTTTTFFFFFFFFFF
3587 template <class _Predicate, class _BidirectionalIterator>
3588 _BidirectionalIterator
3589 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3590 bidirectional_iterator_tag)
3592 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3593 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3594 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3595 // Either prove all true and return __first or point to first false
3598 if (__first == __last)
3600 if (!__pred(*__first))
3604 // __first points to first false, everything prior to __first is already set.
3605 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3608 if (__first == --__last)
3610 } while (!__pred(*__last));
3611 // We now have a reduced range [__first, __last]
3612 // *__first is known to be false
3613 // *__last is known to be true
3615 difference_type __len = _VSTD::distance(__first, __last) + 1;
3616 pair<value_type*, ptrdiff_t> __p(0, 0);
3617 unique_ptr<value_type, __return_temporary_buffer> __h;
3618 if (__len >= __alloc_limit)
3620 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3621 __h.reset(__p.first);
3623 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3624 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3627 template <class _ForwardIterator, class _Predicate>
3628 inline _LIBCPP_INLINE_VISIBILITY
3630 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3632 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3633 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3638 template <class _ForwardIterator, class _Compare>
3639 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3640 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3642 if (__first != __last)
3644 _ForwardIterator __i = __first;
3645 while (++__i != __last)
3647 if (__comp(*__i, *__first))
3655 template<class _ForwardIterator>
3656 _LIBCPP_NODISCARD_EXT inline
3657 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3659 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3661 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3666 template <class _ForwardIterator, class _Compare>
3667 _LIBCPP_NODISCARD_EXT inline
3668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3670 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3672 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3675 template<class _ForwardIterator>
3676 _LIBCPP_NODISCARD_EXT inline
3677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3679 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3681 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3686 // stable, 2-3 compares, 0-2 swaps
3688 template <class _Compare, class _ForwardIterator>
3690 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3693 if (!__c(*__y, *__x)) // if x <= y
3695 if (!__c(*__z, *__y)) // if y <= z
3696 return __r; // x <= y && y <= z
3698 swap(*__y, *__z); // x <= z && y < z
3700 if (__c(*__y, *__x)) // if x > y
3702 swap(*__x, *__y); // x < y && y <= z
3705 return __r; // x <= y && y < z
3707 if (__c(*__z, *__y)) // x > y, if y > z
3709 swap(*__x, *__z); // x < y && y < z
3713 swap(*__x, *__y); // x > y && y <= z
3714 __r = 1; // x < y && x <= z
3715 if (__c(*__z, *__y)) // if y > z
3717 swap(*__y, *__z); // x <= y && y < z
3721 } // x <= y && y <= z
3723 // stable, 3-6 compares, 0-5 swaps
3725 template <class _Compare, class _ForwardIterator>
3727 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3728 _ForwardIterator __x4, _Compare __c)
3730 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3731 if (__c(*__x4, *__x3))
3735 if (__c(*__x3, *__x2))
3739 if (__c(*__x2, *__x1))
3749 // stable, 4-10 compares, 0-9 swaps
3751 template <class _Compare, class _ForwardIterator>
3754 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3755 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3757 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3758 if (__c(*__x5, *__x4))
3762 if (__c(*__x4, *__x3))
3766 if (__c(*__x3, *__x2))
3770 if (__c(*__x2, *__x1))
3782 template <class _Compare, class _BirdirectionalIterator>
3784 __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3786 _BirdirectionalIterator __lm1 = __last;
3787 for (--__lm1; __first != __lm1; ++__first)
3789 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3790 typename add_lvalue_reference<_Compare>::type>
3791 (__first, __last, __comp);
3793 swap(*__first, *__i);
3797 template <class _Compare, class _BirdirectionalIterator>
3799 __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3801 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3802 if (__first != __last)
3804 _BirdirectionalIterator __i = __first;
3805 for (++__i; __i != __last; ++__i)
3807 _BirdirectionalIterator __j = __i;
3808 value_type __t(_VSTD::move(*__j));
3809 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3810 *__j = _VSTD::move(*__k);
3811 *__j = _VSTD::move(__t);
3816 template <class _Compare, class _RandomAccessIterator>
3818 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3820 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3821 _RandomAccessIterator __j = __first+2;
3822 __sort3<_Compare>(__first, __first+1, __j, __comp);
3823 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3825 if (__comp(*__i, *__j))
3827 value_type __t(_VSTD::move(*__i));
3828 _RandomAccessIterator __k = __j;
3832 *__j = _VSTD::move(*__k);
3834 } while (__j != __first && __comp(__t, *--__k));
3835 *__j = _VSTD::move(__t);
3841 template <class _Compare, class _RandomAccessIterator>
3843 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3845 switch (__last - __first)
3851 if (__comp(*--__last, *__first))
3852 swap(*__first, *__last);
3855 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3858 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3861 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3864 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3865 _RandomAccessIterator __j = __first+2;
3866 __sort3<_Compare>(__first, __first+1, __j, __comp);
3867 const unsigned __limit = 8;
3868 unsigned __count = 0;
3869 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3871 if (__comp(*__i, *__j))
3873 value_type __t(_VSTD::move(*__i));
3874 _RandomAccessIterator __k = __j;
3878 *__j = _VSTD::move(*__k);
3880 } while (__j != __first && __comp(__t, *--__k));
3881 *__j = _VSTD::move(__t);
3882 if (++__count == __limit)
3883 return ++__i == __last;
3890 template <class _Compare, class _BirdirectionalIterator>
3892 __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3893 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3895 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3896 if (__first1 != __last1)
3898 __destruct_n __d(0);
3899 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3900 value_type* __last2 = __first2;
3901 ::new(__last2) value_type(_VSTD::move(*__first1));
3902 __d.__incr((value_type*)0);
3903 for (++__last2; ++__first1 != __last1; ++__last2)
3905 value_type* __j2 = __last2;
3906 value_type* __i2 = __j2;
3907 if (__comp(*__first1, *--__i2))
3909 ::new(__j2) value_type(_VSTD::move(*__i2));
3910 __d.__incr((value_type*)0);
3911 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3912 *__j2 = _VSTD::move(*__i2);
3913 *__j2 = _VSTD::move(*__first1);
3917 ::new(__j2) value_type(_VSTD::move(*__first1));
3918 __d.__incr((value_type*)0);
3925 template <class _Compare, class _RandomAccessIterator>
3927 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3929 // _Compare is known to be a reference type
3930 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3931 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3932 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3933 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3937 difference_type __len = __last - __first;
3944 if (__comp(*--__last, *__first))
3945 swap(*__first, *__last);
3948 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3951 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3954 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3957 if (__len <= __limit)
3959 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3963 _RandomAccessIterator __m = __first;
3964 _RandomAccessIterator __lm1 = __last;
3968 difference_type __delta;
3974 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3980 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3984 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3985 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3986 _RandomAccessIterator __i = __first;
3987 _RandomAccessIterator __j = __lm1;
3988 // j points beyond range to be tested, *__m is known to be <= *__lm1
3989 // The search going up is known to be guarded but the search coming down isn't.
3990 // Prime the downward search with a guard.
3991 if (!__comp(*__i, *__m)) // if *__first == *__m
3993 // *__first == *__m, *__first doesn't go in first part
3994 // manually guard downward moving __j against __i
3999 // *__first == *__m, *__m <= all other elements
4000 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4001 ++__i; // __first + 1
4003 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4008 return; // [__first, __last) all equivalent elements
4009 if (__comp(*__first, *__i))
4019 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4024 while (!__comp(*__first, *__i))
4026 while (__comp(*__first, *--__j))
4034 // [__first, __i) == *__first and *__first < [__i, __last)
4035 // The first part is sorted, sort the secod part
4036 // _VSTD::__sort<_Compare>(__i, __last, __comp);
4040 if (__comp(*__j, *__m))
4044 break; // found guard for downward moving __j, now use unguarded partition
4048 // It is known that *__i < *__m
4050 // j points beyond range to be tested, *__m is known to be <= *__lm1
4051 // if not yet partitioned...
4054 // known that *(__i - 1) < *__m
4055 // known that __i <= __m
4058 // __m still guards upward moving __i
4059 while (__comp(*__i, *__m))
4061 // It is now known that a guard exists for downward moving __j
4062 while (!__comp(*--__j, *__m))
4068 // It is known that __m != __j
4069 // If __m just moved, follow it
4075 // [__first, __i) < *__m and *__m <= [__i, __last)
4076 if (__i != __m && __comp(*__m, *__i))
4081 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4082 // If we were given a perfect partition, see if insertion sort is quick...
4085 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4086 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4102 // sort smaller range with recursive call and larger with tail recursion elimination
4103 if (__i - __first < __last - __i)
4105 _VSTD::__sort<_Compare>(__first, __i, __comp);
4106 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4111 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4112 // _VSTD::__sort<_Compare>(__first, __i, __comp);
4118 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4119 template <class _RandomAccessIterator, class _Compare>
4120 inline _LIBCPP_INLINE_VISIBILITY
4122 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4124 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4125 _VSTD::__sort<_Comp_ref>(__first, __last, _Comp_ref(__comp));
4128 template <class _RandomAccessIterator>
4129 inline _LIBCPP_INLINE_VISIBILITY
4131 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4133 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4136 template <class _Tp>
4137 inline _LIBCPP_INLINE_VISIBILITY
4139 sort(_Tp** __first, _Tp** __last)
4141 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4144 template <class _Tp>
4145 inline _LIBCPP_INLINE_VISIBILITY
4147 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4149 _VSTD::sort(__first.base(), __last.base());
4152 template <class _Tp, class _Compare>
4153 inline _LIBCPP_INLINE_VISIBILITY
4155 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4157 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4158 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4161 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4162 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4163 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4164 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4165 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4166 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4167 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4168 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4169 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4170 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4171 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4172 _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>&))
4173 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4174 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4175 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4177 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4178 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4179 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4180 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4181 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4182 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4183 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4184 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4185 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4186 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4187 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4188 _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>&))
4189 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4190 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4191 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4193 _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>&))
4197 template <class _Compare, class _ForwardIterator, class _Tp>
4198 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4199 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4201 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4202 difference_type __len = _VSTD::distance(__first, __last);
4205 difference_type __l2 = _VSTD::__half_positive(__len);
4206 _ForwardIterator __m = __first;
4207 _VSTD::advance(__m, __l2);
4208 if (__comp(*__m, __value_))
4219 template <class _ForwardIterator, class _Tp, class _Compare>
4220 _LIBCPP_NODISCARD_EXT inline
4221 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4223 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4225 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4226 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4229 template <class _ForwardIterator, class _Tp>
4230 _LIBCPP_NODISCARD_EXT inline
4231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4233 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4235 return _VSTD::lower_bound(__first, __last, __value_,
4236 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4241 template <class _Compare, class _ForwardIterator, class _Tp>
4242 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4243 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4245 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4246 difference_type __len = _VSTD::distance(__first, __last);
4249 difference_type __l2 = _VSTD::__half_positive(__len);
4250 _ForwardIterator __m = __first;
4251 _VSTD::advance(__m, __l2);
4252 if (__comp(__value_, *__m))
4263 template <class _ForwardIterator, class _Tp, class _Compare>
4264 _LIBCPP_NODISCARD_EXT inline
4265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4267 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4269 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4270 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4273 template <class _ForwardIterator, class _Tp>
4274 _LIBCPP_NODISCARD_EXT inline
4275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4277 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4279 return _VSTD::upper_bound(__first, __last, __value_,
4280 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4285 template <class _Compare, class _ForwardIterator, class _Tp>
4286 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
4287 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4289 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4290 difference_type __len = _VSTD::distance(__first, __last);
4293 difference_type __l2 = _VSTD::__half_positive(__len);
4294 _ForwardIterator __m = __first;
4295 _VSTD::advance(__m, __l2);
4296 if (__comp(*__m, __value_))
4301 else if (__comp(__value_, *__m))
4308 _ForwardIterator __mp1 = __m;
4309 return pair<_ForwardIterator, _ForwardIterator>
4311 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4312 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4316 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4319 template <class _ForwardIterator, class _Tp, class _Compare>
4320 _LIBCPP_NODISCARD_EXT inline
4321 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4322 pair<_ForwardIterator, _ForwardIterator>
4323 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4325 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4326 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4329 template <class _ForwardIterator, class _Tp>
4330 _LIBCPP_NODISCARD_EXT inline
4331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4332 pair<_ForwardIterator, _ForwardIterator>
4333 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4335 return _VSTD::equal_range(__first, __last, __value_,
4336 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4341 template <class _Compare, class _ForwardIterator, class _Tp>
4342 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4344 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4346 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4347 return __first != __last && !__comp(__value_, *__first);
4350 template <class _ForwardIterator, class _Tp, class _Compare>
4351 _LIBCPP_NODISCARD_EXT inline
4352 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4354 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4356 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4357 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4360 template <class _ForwardIterator, class _Tp>
4361 _LIBCPP_NODISCARD_EXT inline
4362 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4364 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4366 return _VSTD::binary_search(__first, __last, __value_,
4367 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4372 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4374 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4375 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4377 for (; __first1 != __last1; ++__result)
4379 if (__first2 == __last2)
4380 return _VSTD::copy(__first1, __last1, __result);
4381 if (__comp(*__first2, *__first1))
4383 *__result = *__first2;
4388 *__result = *__first1;
4392 return _VSTD::copy(__first2, __last2, __result);
4395 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4396 inline _LIBCPP_INLINE_VISIBILITY
4398 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4399 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4401 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4402 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4405 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4406 inline _LIBCPP_INLINE_VISIBILITY
4408 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4409 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4411 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4412 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4413 return _VSTD::merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4418 template <class _Compare, class _InputIterator1, class _InputIterator2,
4419 class _OutputIterator>
4420 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4421 _InputIterator2 __first2, _InputIterator2 __last2,
4422 _OutputIterator __result, _Compare __comp)
4424 for (; __first1 != __last1; ++__result)
4426 if (__first2 == __last2)
4428 _VSTD::move(__first1, __last1, __result);
4432 if (__comp(*__first2, *__first1))
4434 *__result = _VSTD::move(*__first2);
4439 *__result = _VSTD::move(*__first1);
4443 // __first2 through __last2 are already in the right spot.
4446 template <class _Compare, class _BidirectionalIterator>
4448 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4449 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4450 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4451 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4453 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4454 __destruct_n __d(0);
4455 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4456 if (__len1 <= __len2)
4458 value_type* __p = __buff;
4459 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, (void) ++__p)
4460 ::new(__p) value_type(_VSTD::move(*__i));
4461 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
4465 value_type* __p = __buff;
4466 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, (void) ++__p)
4467 ::new(__p) value_type(_VSTD::move(*__i));
4468 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4469 typedef reverse_iterator<value_type*> _Rv;
4470 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4471 _RBi(__middle), _RBi(__first),
4472 _RBi(__last), __invert<_Compare>(__comp));
4476 template <class _Compare, class _BidirectionalIterator>
4478 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4479 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4480 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4481 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4483 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4486 // if __middle == __last, we're done
4489 if (__len1 <= __buff_size || __len2 <= __buff_size)
4490 return __buffered_inplace_merge<_Compare>
4491 (__first, __middle, __last, __comp, __len1, __len2, __buff);
4492 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4493 for (; true; ++__first, (void) --__len1)
4497 if (__comp(*__middle, *__first))
4500 // __first < __middle < __last
4501 // *__first > *__middle
4502 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4504 // [__first, __m1) <= [__middle, __m2)
4505 // [__middle, __m2) < [__m1, __middle)
4506 // [__m1, __middle) <= [__m2, __last)
4507 // and __m1 or __m2 is in the middle of its range
4508 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4509 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4510 difference_type __len11; // distance(__first, __m1)
4511 difference_type __len21; // distance(__middle, __m2)
4512 // binary search smaller range
4513 if (__len1 < __len2)
4514 { // __len >= 1, __len2 >= 2
4515 __len21 = __len2 / 2;
4517 _VSTD::advance(__m2, __len21);
4518 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4519 __len11 = _VSTD::distance(__first, __m1);
4524 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4525 // It is known *__first > *__middle
4526 swap(*__first, *__middle);
4529 // __len1 >= 2, __len2 >= 1
4530 __len11 = __len1 / 2;
4532 _VSTD::advance(__m1, __len11);
4533 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4534 __len21 = _VSTD::distance(__middle, __m2);
4536 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4537 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4538 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4539 // swap middle two partitions
4540 __middle = _VSTD::rotate(__m1, __middle, __m2);
4541 // __len12 and __len21 now have swapped meanings
4542 // merge smaller range with recurisve call and larger with tail recursion elimination
4543 if (__len11 + __len21 < __len12 + __len22)
4545 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4546 // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4554 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4555 // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4564 template <class _BidirectionalIterator, class _Compare>
4565 inline _LIBCPP_INLINE_VISIBILITY
4567 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4570 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4571 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4572 difference_type __len1 = _VSTD::distance(__first, __middle);
4573 difference_type __len2 = _VSTD::distance(__middle, __last);
4574 difference_type __buf_size = _VSTD::min(__len1, __len2);
4575 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4576 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4577 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4578 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4579 __buf.first, __buf.second);
4582 template <class _BidirectionalIterator>
4583 inline _LIBCPP_INLINE_VISIBILITY
4585 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4587 _VSTD::inplace_merge(__first, __middle, __last,
4588 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4593 template <class _Compare, class _InputIterator1, class _InputIterator2>
4595 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4596 _InputIterator2 __first2, _InputIterator2 __last2,
4597 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4599 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4600 __destruct_n __d(0);
4601 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4602 for (; true; ++__result)
4604 if (__first1 == __last1)
4606 for (; __first2 != __last2; ++__first2, ++__result, (void) __d.__incr((value_type*)0))
4607 ::new (__result) value_type(_VSTD::move(*__first2));
4611 if (__first2 == __last2)
4613 for (; __first1 != __last1; ++__first1, ++__result, (void) __d.__incr((value_type*)0))
4614 ::new (__result) value_type(_VSTD::move(*__first1));
4618 if (__comp(*__first2, *__first1))
4620 ::new (__result) value_type(_VSTD::move(*__first2));
4621 __d.__incr((value_type*)0);
4626 ::new (__result) value_type(_VSTD::move(*__first1));
4627 __d.__incr((value_type*)0);
4633 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4635 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4636 _InputIterator2 __first2, _InputIterator2 __last2,
4637 _OutputIterator __result, _Compare __comp)
4639 for (; __first1 != __last1; ++__result)
4641 if (__first2 == __last2)
4643 for (; __first1 != __last1; ++__first1, (void) ++__result)
4644 *__result = _VSTD::move(*__first1);
4647 if (__comp(*__first2, *__first1))
4649 *__result = _VSTD::move(*__first2);
4654 *__result = _VSTD::move(*__first1);
4658 for (; __first2 != __last2; ++__first2, (void) ++__result)
4659 *__result = _VSTD::move(*__first2);
4662 template <class _Compare, class _RandomAccessIterator>
4664 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4665 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4666 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4668 template <class _Compare, class _RandomAccessIterator>
4670 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4671 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4672 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4674 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4680 ::new(__first2) value_type(_VSTD::move(*__first1));
4683 __destruct_n __d(0);
4684 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4685 if (__comp(*--__last1, *__first1))
4687 ::new(__first2) value_type(_VSTD::move(*__last1));
4688 __d.__incr((value_type*)0);
4690 ::new(__first2) value_type(_VSTD::move(*__first1));
4694 ::new(__first2) value_type(_VSTD::move(*__first1));
4695 __d.__incr((value_type*)0);
4697 ::new(__first2) value_type(_VSTD::move(*__last1));
4704 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4707 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4708 _RandomAccessIterator __m = __first1 + __l2;
4709 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4710 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4711 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4714 template <class _Tp>
4715 struct __stable_sort_switch
4717 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4720 template <class _Compare, class _RandomAccessIterator>
4722 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4723 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4724 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4726 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4727 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4734 if (__comp(*--__last, *__first))
4735 swap(*__first, *__last);
4738 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4740 __insertion_sort<_Compare>(__first, __last, __comp);
4743 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4744 _RandomAccessIterator __m = __first + __l2;
4745 if (__len <= __buff_size)
4747 __destruct_n __d(0);
4748 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4749 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4750 __d.__set(__l2, (value_type*)0);
4751 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4752 __d.__set(__len, (value_type*)0);
4753 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4754 // __merge<_Compare>(move_iterator<value_type*>(__buff),
4755 // move_iterator<value_type*>(__buff + __l2),
4756 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4757 // move_iterator<_RandomAccessIterator>(__buff + __len),
4758 // __first, __comp);
4761 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4762 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4763 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4766 template <class _RandomAccessIterator, class _Compare>
4767 inline _LIBCPP_INLINE_VISIBILITY
4769 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4771 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4772 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4773 difference_type __len = __last - __first;
4774 pair<value_type*, ptrdiff_t> __buf(0, 0);
4775 unique_ptr<value_type, __return_temporary_buffer> __h;
4776 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4778 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4779 __h.reset(__buf.first);
4781 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4782 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4785 template <class _RandomAccessIterator>
4786 inline _LIBCPP_INLINE_VISIBILITY
4788 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4790 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4795 template <class _RandomAccessIterator, class _Compare>
4796 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
4797 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4799 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4800 difference_type __len = __last - __first;
4801 difference_type __p = 0;
4802 difference_type __c = 1;
4803 _RandomAccessIterator __pp = __first;
4806 _RandomAccessIterator __cp = __first + __c;
4807 if (__comp(*__pp, *__cp))
4813 if (__comp(*__pp, *__cp))
4822 template<class _RandomAccessIterator>
4823 _LIBCPP_NODISCARD_EXT inline
4824 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4825 _RandomAccessIterator
4826 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4828 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4833 template <class _RandomAccessIterator, class _Compare>
4834 _LIBCPP_NODISCARD_EXT inline
4835 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4837 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4839 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4842 template<class _RandomAccessIterator>
4843 _LIBCPP_NODISCARD_EXT inline
4844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4846 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4848 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4853 template <class _Compare, class _RandomAccessIterator>
4855 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4856 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4858 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4861 __len = (__len - 2) / 2;
4862 _RandomAccessIterator __ptr = __first + __len;
4863 if (__comp(*__ptr, *--__last))
4865 value_type __t(_VSTD::move(*__last));
4868 *__last = _VSTD::move(*__ptr);
4872 __len = (__len - 1) / 2;
4873 __ptr = __first + __len;
4874 } while (__comp(*__ptr, __t));
4875 *__last = _VSTD::move(__t);
4880 template <class _RandomAccessIterator, class _Compare>
4881 inline _LIBCPP_INLINE_VISIBILITY
4883 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4885 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4886 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
4889 template <class _RandomAccessIterator>
4890 inline _LIBCPP_INLINE_VISIBILITY
4892 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4894 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4899 template <class _Compare, class _RandomAccessIterator>
4901 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4903 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4904 _RandomAccessIterator __start)
4906 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4907 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4908 // left-child of __start is at 2 * __start + 1
4909 // right-child of __start is at 2 * __start + 2
4910 difference_type __child = __start - __first;
4912 if (__len < 2 || (__len - 2) / 2 < __child)
4915 __child = 2 * __child + 1;
4916 _RandomAccessIterator __child_i = __first + __child;
4918 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4919 // right-child exists and is greater than left-child
4924 // check if we are in heap-order
4925 if (__comp(*__child_i, *__start))
4926 // we are, __start is larger than it's largest child
4929 value_type __top(_VSTD::move(*__start));
4932 // we are not in heap-order, swap the parent with it's largest child
4933 *__start = _VSTD::move(*__child_i);
4934 __start = __child_i;
4936 if ((__len - 2) / 2 < __child)
4939 // recompute the child based off of the updated parent
4940 __child = 2 * __child + 1;
4941 __child_i = __first + __child;
4943 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4944 // right-child exists and is greater than left-child
4949 // check if we are in heap-order
4950 } while (!__comp(*__child_i, __top));
4951 *__start = _VSTD::move(__top);
4954 template <class _Compare, class _RandomAccessIterator>
4955 inline _LIBCPP_INLINE_VISIBILITY
4957 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4958 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4962 swap(*__first, *--__last);
4963 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
4967 template <class _RandomAccessIterator, class _Compare>
4968 inline _LIBCPP_INLINE_VISIBILITY
4970 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4972 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4973 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4976 template <class _RandomAccessIterator>
4977 inline _LIBCPP_INLINE_VISIBILITY
4979 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4981 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4986 template <class _Compare, class _RandomAccessIterator>
4988 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4990 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4991 difference_type __n = __last - __first;
4994 // start from the first parent, there is no need to consider children
4995 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4997 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5002 template <class _RandomAccessIterator, class _Compare>
5003 inline _LIBCPP_INLINE_VISIBILITY
5005 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5007 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5008 __make_heap<_Comp_ref>(__first, __last, __comp);
5011 template <class _RandomAccessIterator>
5012 inline _LIBCPP_INLINE_VISIBILITY
5014 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5016 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5021 template <class _Compare, class _RandomAccessIterator>
5023 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5025 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5026 for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
5027 __pop_heap<_Compare>(__first, __last, __comp, __n);
5030 template <class _RandomAccessIterator, class _Compare>
5031 inline _LIBCPP_INLINE_VISIBILITY
5033 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5035 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5036 __sort_heap<_Comp_ref>(__first, __last, __comp);
5039 template <class _RandomAccessIterator>
5040 inline _LIBCPP_INLINE_VISIBILITY
5042 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5044 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5049 template <class _Compare, class _RandomAccessIterator>
5051 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5054 __make_heap<_Compare>(__first, __middle, __comp);
5055 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5056 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5058 if (__comp(*__i, *__first))
5060 swap(*__i, *__first);
5061 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5064 __sort_heap<_Compare>(__first, __middle, __comp);
5067 template <class _RandomAccessIterator, class _Compare>
5068 inline _LIBCPP_INLINE_VISIBILITY
5070 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5073 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5074 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5077 template <class _RandomAccessIterator>
5078 inline _LIBCPP_INLINE_VISIBILITY
5080 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5082 _VSTD::partial_sort(__first, __middle, __last,
5083 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5086 // partial_sort_copy
5088 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5089 _RandomAccessIterator
5090 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5091 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5093 _RandomAccessIterator __r = __result_first;
5094 if (__r != __result_last)
5096 for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
5098 __make_heap<_Compare>(__result_first, __r, __comp);
5099 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5100 for (; __first != __last; ++__first)
5101 if (__comp(*__first, *__result_first))
5103 *__result_first = *__first;
5104 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5106 __sort_heap<_Compare>(__result_first, __r, __comp);
5111 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5112 inline _LIBCPP_INLINE_VISIBILITY
5113 _RandomAccessIterator
5114 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5115 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5117 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5118 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5121 template <class _InputIterator, class _RandomAccessIterator>
5122 inline _LIBCPP_INLINE_VISIBILITY
5123 _RandomAccessIterator
5124 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5125 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5127 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5128 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5133 template <class _Compare, class _RandomAccessIterator>
5135 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5137 // _Compare is known to be a reference type
5138 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5139 const difference_type __limit = 7;
5143 if (__nth == __last)
5145 difference_type __len = __last - __first;
5152 if (__comp(*--__last, *__first))
5153 swap(*__first, *__last);
5157 _RandomAccessIterator __m = __first;
5158 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5162 if (__len <= __limit)
5164 __selection_sort<_Compare>(__first, __last, __comp);
5167 // __len > __limit >= 3
5168 _RandomAccessIterator __m = __first + __len/2;
5169 _RandomAccessIterator __lm1 = __last;
5170 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5172 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5173 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5174 _RandomAccessIterator __i = __first;
5175 _RandomAccessIterator __j = __lm1;
5176 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5177 // The search going up is known to be guarded but the search coming down isn't.
5178 // Prime the downward search with a guard.
5179 if (!__comp(*__i, *__m)) // if *__first == *__m
5181 // *__first == *__m, *__first doesn't go in first part
5182 // manually guard downward moving __j against __i
5187 // *__first == *__m, *__m <= all other elements
5188 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5189 ++__i; // __first + 1
5191 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5196 return; // [__first, __last) all equivalent elements
5197 if (__comp(*__first, *__i))
5207 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5212 while (!__comp(*__first, *__i))
5214 while (__comp(*__first, *--__j))
5222 // [__first, __i) == *__first and *__first < [__i, __last)
5223 // The first part is sorted,
5226 // __nth_element the secod part
5227 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5231 if (__comp(*__j, *__m))
5235 break; // found guard for downward moving __j, now use unguarded partition
5240 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5241 // if not yet partitioned...
5244 // known that *(__i - 1) < *__m
5247 // __m still guards upward moving __i
5248 while (__comp(*__i, *__m))
5250 // It is now known that a guard exists for downward moving __j
5251 while (!__comp(*--__j, *__m))
5257 // It is known that __m != __j
5258 // If __m just moved, follow it
5264 // [__first, __i) < *__m and *__m <= [__i, __last)
5265 if (__i != __m && __comp(*__m, *__i))
5270 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5275 // We were given a perfectly partitioned sequence. Coincidence?
5278 // Check for [__first, __i) already sorted
5279 __j = __m = __first;
5280 while (++__j != __i)
5282 if (__comp(*__j, *__m))
5283 // not yet sorted, so sort
5287 // [__first, __i) sorted
5292 // Check for [__i, __last) already sorted
5294 while (++__j != __last)
5296 if (__comp(*__j, *__m))
5297 // not yet sorted, so sort
5301 // [__i, __last) sorted
5306 // __nth_element on range containing __nth
5309 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5314 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5320 template <class _RandomAccessIterator, class _Compare>
5321 inline _LIBCPP_INLINE_VISIBILITY
5323 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5325 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5326 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5329 template <class _RandomAccessIterator>
5330 inline _LIBCPP_INLINE_VISIBILITY
5332 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5334 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5339 template <class _Compare, class _InputIterator1, class _InputIterator2>
5340 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5341 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5344 for (; __first2 != __last2; ++__first1)
5346 if (__first1 == __last1 || __comp(*__first2, *__first1))
5348 if (!__comp(*__first1, *__first2))
5354 template <class _InputIterator1, class _InputIterator2, class _Compare>
5355 _LIBCPP_NODISCARD_EXT inline
5356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5358 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5361 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5362 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5365 template <class _InputIterator1, class _InputIterator2>
5366 _LIBCPP_NODISCARD_EXT inline
5367 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5369 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5371 return _VSTD::includes(__first1, __last1, __first2, __last2,
5372 __less<typename iterator_traits<_InputIterator1>::value_type,
5373 typename iterator_traits<_InputIterator2>::value_type>());
5378 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5380 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5381 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5383 for (; __first1 != __last1; ++__result)
5385 if (__first2 == __last2)
5386 return _VSTD::copy(__first1, __last1, __result);
5387 if (__comp(*__first2, *__first1))
5389 *__result = *__first2;
5394 if (!__comp(*__first1, *__first2))
5396 *__result = *__first1;
5400 return _VSTD::copy(__first2, __last2, __result);
5403 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5404 inline _LIBCPP_INLINE_VISIBILITY
5406 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5407 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5409 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5410 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5413 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5414 inline _LIBCPP_INLINE_VISIBILITY
5416 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5417 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5419 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5420 __less<typename iterator_traits<_InputIterator1>::value_type,
5421 typename iterator_traits<_InputIterator2>::value_type>());
5426 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5427 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5428 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5429 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5431 while (__first1 != __last1 && __first2 != __last2)
5433 if (__comp(*__first1, *__first2))
5437 if (!__comp(*__first2, *__first1))
5439 *__result = *__first1;
5449 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5450 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5452 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5453 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5455 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5456 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5459 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5460 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5462 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5463 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5465 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5466 __less<typename iterator_traits<_InputIterator1>::value_type,
5467 typename iterator_traits<_InputIterator2>::value_type>());
5472 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5474 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5475 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5477 while (__first1 != __last1)
5479 if (__first2 == __last2)
5480 return _VSTD::copy(__first1, __last1, __result);
5481 if (__comp(*__first1, *__first2))
5483 *__result = *__first1;
5489 if (!__comp(*__first2, *__first1))
5497 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5498 inline _LIBCPP_INLINE_VISIBILITY
5500 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5501 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5503 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5504 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5507 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5508 inline _LIBCPP_INLINE_VISIBILITY
5510 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5511 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5513 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5514 __less<typename iterator_traits<_InputIterator1>::value_type,
5515 typename iterator_traits<_InputIterator2>::value_type>());
5518 // set_symmetric_difference
5520 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5522 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5523 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5525 while (__first1 != __last1)
5527 if (__first2 == __last2)
5528 return _VSTD::copy(__first1, __last1, __result);
5529 if (__comp(*__first1, *__first2))
5531 *__result = *__first1;
5537 if (__comp(*__first2, *__first1))
5539 *__result = *__first2;
5547 return _VSTD::copy(__first2, __last2, __result);
5550 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5551 inline _LIBCPP_INLINE_VISIBILITY
5553 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5554 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5556 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5557 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5560 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5561 inline _LIBCPP_INLINE_VISIBILITY
5563 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5564 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5566 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5567 __less<typename iterator_traits<_InputIterator1>::value_type,
5568 typename iterator_traits<_InputIterator2>::value_type>());
5571 // lexicographical_compare
5573 template <class _Compare, class _InputIterator1, class _InputIterator2>
5574 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5575 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5576 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5578 for (; __first2 != __last2; ++__first1, (void) ++__first2)
5580 if (__first1 == __last1 || __comp(*__first1, *__first2))
5582 if (__comp(*__first2, *__first1))
5588 template <class _InputIterator1, class _InputIterator2, class _Compare>
5589 _LIBCPP_NODISCARD_EXT inline
5590 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5592 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5593 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5595 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5596 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5599 template <class _InputIterator1, class _InputIterator2>
5600 _LIBCPP_NODISCARD_EXT inline
5601 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5603 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5604 _InputIterator2 __first2, _InputIterator2 __last2)
5606 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5607 __less<typename iterator_traits<_InputIterator1>::value_type,
5608 typename iterator_traits<_InputIterator2>::value_type>());
5613 template <class _Compare, class _BidirectionalIterator>
5615 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5617 _BidirectionalIterator __i = __last;
5618 if (__first == __last || __first == --__i)
5622 _BidirectionalIterator __ip1 = __i;
5623 if (__comp(*--__i, *__ip1))
5625 _BidirectionalIterator __j = __last;
5626 while (!__comp(*__i, *--__j))
5629 _VSTD::reverse(__ip1, __last);
5634 _VSTD::reverse(__first, __last);
5640 template <class _BidirectionalIterator, class _Compare>
5641 inline _LIBCPP_INLINE_VISIBILITY
5643 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5645 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5646 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5649 template <class _BidirectionalIterator>
5650 inline _LIBCPP_INLINE_VISIBILITY
5652 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5654 return _VSTD::next_permutation(__first, __last,
5655 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5660 template <class _Compare, class _BidirectionalIterator>
5662 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5664 _BidirectionalIterator __i = __last;
5665 if (__first == __last || __first == --__i)
5669 _BidirectionalIterator __ip1 = __i;
5670 if (__comp(*__ip1, *--__i))
5672 _BidirectionalIterator __j = __last;
5673 while (!__comp(*--__j, *__i))
5676 _VSTD::reverse(__ip1, __last);
5681 _VSTD::reverse(__first, __last);
5687 template <class _BidirectionalIterator, class _Compare>
5688 inline _LIBCPP_INLINE_VISIBILITY
5690 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5692 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5693 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5696 template <class _BidirectionalIterator>
5697 inline _LIBCPP_INLINE_VISIBILITY
5699 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5701 return _VSTD::prev_permutation(__first, __last,
5702 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5705 _LIBCPP_END_NAMESPACE_STD
5709 #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5710 # include <__pstl_algorithm>
5713 #endif // _LIBCPP_ALGORITHM