2 //===-------------------------- algorithm ---------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_ALGORITHM
12 #define _LIBCPP_ALGORITHM
17 #include <initializer_list>
22 template <class InputIterator, class Predicate>
23 constexpr bool // constexpr in C++20
24 all_of(InputIterator first, InputIterator last, Predicate pred);
26 template <class InputIterator, class Predicate>
27 constexpr bool // constexpr in C++20
28 any_of(InputIterator first, InputIterator last, Predicate pred);
30 template <class InputIterator, class Predicate>
31 constexpr bool // constexpr in C++20
32 none_of(InputIterator first, InputIterator last, Predicate pred);
34 template <class InputIterator, class Function>
35 constexpr Function // constexpr in C++20
36 for_each(InputIterator first, InputIterator last, Function f);
38 template<class InputIterator, class Size, class Function>
39 constexpr InputIterator // constexpr in C++20
40 for_each_n(InputIterator first, Size n, Function f); // C++17
42 template <class InputIterator, class T>
43 constexpr InputIterator // constexpr in C++20
44 find(InputIterator first, InputIterator last, const T& value);
46 template <class InputIterator, class Predicate>
47 constexpr InputIterator // constexpr in C++20
48 find_if(InputIterator first, InputIterator last, Predicate pred);
50 template<class InputIterator, class Predicate>
51 InputIterator // constexpr in C++20
52 find_if_not(InputIterator first, InputIterator last, Predicate pred);
54 template <class ForwardIterator1, class ForwardIterator2>
55 ForwardIterator1 // constexpr in C++20
56 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
57 ForwardIterator2 first2, ForwardIterator2 last2);
59 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
60 ForwardIterator1 // constexpr in C++20
61 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
62 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
64 template <class ForwardIterator1, class ForwardIterator2>
65 constexpr ForwardIterator1 // constexpr in C++20
66 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
67 ForwardIterator2 first2, ForwardIterator2 last2);
69 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
70 constexpr ForwardIterator1 // constexpr in C++20
71 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
72 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
74 template <class ForwardIterator>
75 constexpr ForwardIterator // constexpr in C++20
76 adjacent_find(ForwardIterator first, ForwardIterator last);
78 template <class ForwardIterator, class BinaryPredicate>
79 constexpr ForwardIterator // constexpr in C++20
80 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
82 template <class InputIterator, class T>
83 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
84 count(InputIterator first, InputIterator last, const T& value);
86 template <class InputIterator, class Predicate>
87 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
88 count_if(InputIterator first, InputIterator last, Predicate pred);
90 template <class InputIterator1, class InputIterator2>
91 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
92 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
94 template <class InputIterator1, class InputIterator2>
95 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
96 mismatch(InputIterator1 first1, InputIterator1 last1,
97 InputIterator2 first2, InputIterator2 last2); // **C++14**
99 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
100 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
101 mismatch(InputIterator1 first1, InputIterator1 last1,
102 InputIterator2 first2, BinaryPredicate pred);
104 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
105 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
106 mismatch(InputIterator1 first1, InputIterator1 last1,
107 InputIterator2 first2, InputIterator2 last2,
108 BinaryPredicate pred); // **C++14**
110 template <class InputIterator1, class InputIterator2>
111 constexpr bool // constexpr in C++20
112 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
114 template <class InputIterator1, class InputIterator2>
115 constexpr bool // constexpr in C++20
116 equal(InputIterator1 first1, InputIterator1 last1,
117 InputIterator2 first2, InputIterator2 last2); // **C++14**
119 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
120 constexpr bool // constexpr in C++20
121 equal(InputIterator1 first1, InputIterator1 last1,
122 InputIterator2 first2, BinaryPredicate pred);
124 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
125 constexpr bool // constexpr in C++20
126 equal(InputIterator1 first1, InputIterator1 last1,
127 InputIterator2 first2, InputIterator2 last2,
128 BinaryPredicate pred); // **C++14**
130 template<class ForwardIterator1, class ForwardIterator2>
131 constexpr bool // constexpr in C++20
132 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
133 ForwardIterator2 first2);
135 template<class ForwardIterator1, class ForwardIterator2>
136 constexpr bool // constexpr in C++20
137 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
138 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
140 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
141 constexpr bool // constexpr in C++20
142 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
143 ForwardIterator2 first2, BinaryPredicate pred);
145 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
146 constexpr bool // constexpr in C++20
147 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
148 ForwardIterator2 first2, ForwardIterator2 last2,
149 BinaryPredicate pred); // **C++14**
151 template <class ForwardIterator1, class ForwardIterator2>
152 constexpr ForwardIterator1 // constexpr in C++20
153 search(ForwardIterator1 first1, ForwardIterator1 last1,
154 ForwardIterator2 first2, ForwardIterator2 last2);
156 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
157 constexpr ForwardIterator1 // constexpr in C++20
158 search(ForwardIterator1 first1, ForwardIterator1 last1,
159 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
161 template <class ForwardIterator, class Size, class T>
162 constexpr ForwardIterator // constexpr in C++20
163 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
165 template <class ForwardIterator, class Size, class T, class BinaryPredicate>
166 constexpr ForwardIterator // constexpr in C++20
167 search_n(ForwardIterator first, ForwardIterator last,
168 Size count, const T& value, BinaryPredicate pred);
170 template <class InputIterator, class OutputIterator>
172 copy(InputIterator first, InputIterator last, OutputIterator result);
174 template<class InputIterator, class OutputIterator, class Predicate>
176 copy_if(InputIterator first, InputIterator last,
177 OutputIterator result, Predicate pred);
179 template<class InputIterator, class Size, class OutputIterator>
181 copy_n(InputIterator first, Size n, OutputIterator result);
183 template <class BidirectionalIterator1, class BidirectionalIterator2>
184 BidirectionalIterator2
185 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
186 BidirectionalIterator2 result);
188 template <class ForwardIterator1, class ForwardIterator2>
190 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
192 template <class ForwardIterator1, class ForwardIterator2>
194 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
196 template <class InputIterator, class OutputIterator, class UnaryOperation>
197 constexpr OutputIterator // constexpr in C++20
198 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
200 template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
201 constexpr OutputIterator // constexpr in C++20
202 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
203 OutputIterator result, BinaryOperation binary_op);
205 template <class ForwardIterator, class T>
206 constexpr void // constexpr in C++20
207 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
209 template <class ForwardIterator, class Predicate, class T>
210 constexpr void // constexpr in C++20
211 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
213 template <class InputIterator, class OutputIterator, class T>
214 constexpr OutputIterator // constexpr in C++20
215 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
216 const T& old_value, const T& new_value);
218 template <class InputIterator, class OutputIterator, class Predicate, class T>
219 constexpr OutputIterator // constexpr in C++20
220 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
222 template <class ForwardIterator, class T>
223 constexpr void // constexpr in C++20
224 fill(ForwardIterator first, ForwardIterator last, const T& value);
226 template <class OutputIterator, class Size, class T>
227 constexpr OutputIterator // constexpr in C++20
228 fill_n(OutputIterator first, Size n, const T& value);
230 template <class ForwardIterator, class Generator>
231 constexpr void // constexpr in C++20
232 generate(ForwardIterator first, ForwardIterator last, Generator gen);
234 template <class OutputIterator, class Size, class Generator>
235 constexpr OutputIterator // constexpr in C++20
236 generate_n(OutputIterator first, Size n, Generator gen);
238 template <class ForwardIterator, class T>
239 constexpr ForwardIterator // constexpr in C++20
240 remove(ForwardIterator first, ForwardIterator last, const T& value);
242 template <class ForwardIterator, class Predicate>
243 constexpr ForwardIterator // constexpr in C++20
244 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
246 template <class InputIterator, class OutputIterator, class T>
247 constexpr OutputIterator // constexpr in C++20
248 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
250 template <class InputIterator, class OutputIterator, class Predicate>
251 constexpr OutputIterator // constexpr in C++20
252 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
254 template <class ForwardIterator>
256 unique(ForwardIterator first, ForwardIterator last);
258 template <class ForwardIterator, class BinaryPredicate>
260 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
262 template <class InputIterator, class OutputIterator>
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
266 template <class InputIterator, class OutputIterator, class BinaryPredicate>
268 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
270 template <class BidirectionalIterator>
272 reverse(BidirectionalIterator first, BidirectionalIterator last);
274 template <class BidirectionalIterator, class OutputIterator>
275 constexpr OutputIterator // constexpr in C++20
276 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
278 template <class ForwardIterator>
280 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
282 template <class ForwardIterator, class OutputIterator>
284 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
286 template <class RandomAccessIterator>
288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
290 template <class RandomAccessIterator, class RandomNumberGenerator>
292 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
293 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
295 template<class PopulationIterator, class SampleIterator,
296 class Distance, class UniformRandomBitGenerator>
297 SampleIterator sample(PopulationIterator first, PopulationIterator last,
298 SampleIterator out, Distance n,
299 UniformRandomBitGenerator&& g); // C++17
301 template<class RandomAccessIterator, class UniformRandomNumberGenerator>
302 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
303 UniformRandomNumberGenerator&& g);
305 template <class InputIterator, class Predicate>
306 constexpr bool // constexpr in C++20
307 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
309 template <class ForwardIterator, class Predicate>
311 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313 template <class InputIterator, class OutputIterator1,
314 class OutputIterator2, class Predicate>
315 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
316 partition_copy(InputIterator first, InputIterator last,
317 OutputIterator1 out_true, OutputIterator2 out_false,
320 template <class ForwardIterator, class Predicate>
322 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
324 template<class ForwardIterator, class Predicate>
325 constexpr ForwardIterator // constexpr in C++20
326 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
328 template <class ForwardIterator>
329 constexpr bool // constexpr in C++20
330 is_sorted(ForwardIterator first, ForwardIterator last);
332 template <class ForwardIterator, class Compare>
334 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
336 template<class ForwardIterator>
337 constexpr ForwardIterator // constexpr in C++20
338 is_sorted_until(ForwardIterator first, ForwardIterator last);
340 template <class ForwardIterator, class Compare>
341 constexpr ForwardIterator // constexpr in C++20
342 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
344 template <class RandomAccessIterator>
346 sort(RandomAccessIterator first, RandomAccessIterator last);
348 template <class RandomAccessIterator, class Compare>
350 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
352 template <class RandomAccessIterator>
354 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
356 template <class RandomAccessIterator, class Compare>
358 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
360 template <class RandomAccessIterator>
362 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
364 template <class RandomAccessIterator, class Compare>
366 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
368 template <class InputIterator, class RandomAccessIterator>
370 partial_sort_copy(InputIterator first, InputIterator last,
371 RandomAccessIterator result_first, RandomAccessIterator result_last);
373 template <class InputIterator, class RandomAccessIterator, class Compare>
375 partial_sort_copy(InputIterator first, InputIterator last,
376 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
378 template <class RandomAccessIterator>
380 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
382 template <class RandomAccessIterator, class Compare>
384 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
386 template <class ForwardIterator, class T>
387 constexpr ForwardIterator // constexpr in C++20
388 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
390 template <class ForwardIterator, class T, class Compare>
391 constexpr ForwardIterator // constexpr in C++20
392 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
394 template <class ForwardIterator, class T>
395 constexpr ForwardIterator // constexpr in C++20
396 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
398 template <class ForwardIterator, class T, class Compare>
399 constexpr ForwardIterator // constexpr in C++20
400 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
402 template <class ForwardIterator, class T>
403 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
404 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
406 template <class ForwardIterator, class T, class Compare>
407 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
408 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
410 template <class ForwardIterator, class T>
411 constexpr bool // constexpr in C++20
412 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
414 template <class ForwardIterator, class T, class Compare>
415 constexpr bool // constexpr in C++20
416 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
418 template <class InputIterator1, class InputIterator2, class OutputIterator>
420 merge(InputIterator1 first1, InputIterator1 last1,
421 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
423 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
425 merge(InputIterator1 first1, InputIterator1 last1,
426 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
428 template <class BidirectionalIterator>
430 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
432 template <class BidirectionalIterator, class Compare>
434 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
436 template <class InputIterator1, class InputIterator2>
437 constexpr bool // constexpr in C++20
438 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
440 template <class InputIterator1, class InputIterator2, class Compare>
441 constexpr bool // constexpr in C++20
442 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
444 template <class InputIterator1, class InputIterator2, class OutputIterator>
446 set_union(InputIterator1 first1, InputIterator1 last1,
447 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
449 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
451 set_union(InputIterator1 first1, InputIterator1 last1,
452 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
454 template <class InputIterator1, class InputIterator2, class OutputIterator>
455 constexpr OutputIterator // constexpr in C++20
456 set_intersection(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
459 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460 constexpr OutputIterator // constexpr in C++20
461 set_intersection(InputIterator1 first1, InputIterator1 last1,
462 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
464 template <class InputIterator1, class InputIterator2, class OutputIterator>
466 set_difference(InputIterator1 first1, InputIterator1 last1,
467 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
469 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
471 set_difference(InputIterator1 first1, InputIterator1 last1,
472 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
474 template <class InputIterator1, class InputIterator2, class OutputIterator>
476 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
477 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
479 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
481 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
482 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
484 template <class RandomAccessIterator>
486 push_heap(RandomAccessIterator first, RandomAccessIterator last);
488 template <class RandomAccessIterator, class Compare>
490 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
492 template <class RandomAccessIterator>
494 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
496 template <class RandomAccessIterator, class Compare>
498 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
500 template <class RandomAccessIterator>
502 make_heap(RandomAccessIterator first, RandomAccessIterator last);
504 template <class RandomAccessIterator, class Compare>
506 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
508 template <class RandomAccessIterator>
510 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
512 template <class RandomAccessIterator, class Compare>
514 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
516 template <class RandomAccessIterator>
517 constexpr bool // constexpr in C++20
518 is_heap(RandomAccessIterator first, RandomAccessiterator last);
520 template <class RandomAccessIterator, class Compare>
521 constexpr bool // constexpr in C++20
522 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
524 template <class RandomAccessIterator>
525 constexpr RandomAccessIterator // constexpr in C++20
526 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
528 template <class RandomAccessIterator, class Compare>
529 constexpr RandomAccessIterator // constexpr in C++20
530 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
532 template <class ForwardIterator>
534 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
536 template <class ForwardIterator, class Compare>
538 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
542 min(const T& a, const T& b); // constexpr in C++14
544 template <class T, class Compare>
546 min(const T& a, const T& b, Compare comp); // constexpr in C++14
550 min(initializer_list<T> t); // constexpr in C++14
552 template<class T, class Compare>
554 min(initializer_list<T> t, Compare comp); // constexpr in C++14
557 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
559 template<class T, class Compare>
560 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
562 template <class ForwardIterator>
564 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
566 template <class ForwardIterator, class Compare>
568 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
572 max(const T& a, const T& b); // constexpr in C++14
574 template <class T, class Compare>
576 max(const T& a, const T& b, Compare comp); // constexpr in C++14
580 max(initializer_list<T> t); // constexpr in C++14
582 template<class T, class Compare>
584 max(initializer_list<T> t, Compare comp); // constexpr in C++14
586 template<class ForwardIterator>
587 pair<ForwardIterator, ForwardIterator>
588 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
590 template<class ForwardIterator, class Compare>
591 pair<ForwardIterator, ForwardIterator>
592 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
595 pair<const T&, const T&>
596 minmax(const T& a, const T& b); // constexpr in C++14
598 template<class T, class Compare>
599 pair<const T&, const T&>
600 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
604 minmax(initializer_list<T> t); // constexpr in C++14
606 template<class T, class Compare>
608 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
610 template <class InputIterator1, class InputIterator2>
611 constexpr bool // constexpr in C++20
612 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
614 template <class InputIterator1, class InputIterator2, class Compare>
615 constexpr bool // constexpr in C++20
616 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
617 InputIterator2 first2, InputIterator2 last2, Compare comp);
619 template <class BidirectionalIterator>
621 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
623 template <class BidirectionalIterator, class Compare>
625 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
627 template <class BidirectionalIterator>
629 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
631 template <class BidirectionalIterator, class Compare>
633 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
640 #include <initializer_list>
641 #include <type_traits>
643 #include <utility> // needed to provide swap_ranges.
645 #include <functional>
649 #if defined(__IBMCPP__)
650 #include "support/ibm/support.h"
652 #if defined(_LIBCPP_COMPILER_MSVC)
658 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
659 #pragma GCC system_header
663 #include <__undef_macros>
666 _LIBCPP_BEGIN_NAMESPACE_STD
668 // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
669 // * That only works with C++14 and later, and
670 // * We haven't included <functional> here.
671 template <class _T1, class _T2 = _T1>
674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
675 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
676 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
681 struct __equal_to<_T1, _T1>
683 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
684 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
688 struct __equal_to<const _T1, _T1>
690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
691 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
695 struct __equal_to<_T1, const _T1>
697 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
698 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
701 template <class _T1, class _T2 = _T1>
704 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
705 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
707 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
708 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
710 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
711 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
713 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
714 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
718 struct __less<_T1, _T1>
720 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
721 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
725 struct __less<const _T1, _T1>
727 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
728 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
732 struct __less<_T1, const _T1>
734 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
735 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
738 template <class _Predicate>
739 class __invert // invert the sense of a comparison
744 _LIBCPP_INLINE_VISIBILITY __invert() {}
746 _LIBCPP_INLINE_VISIBILITY
747 explicit __invert(_Predicate __p) : __p_(__p) {}
750 _LIBCPP_INLINE_VISIBILITY
751 bool operator()(const _T1& __x) {return !__p_(__x);}
753 template <class _T1, class _T2>
754 _LIBCPP_INLINE_VISIBILITY
755 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
760 template <class _Compare>
764 __debug_less(_Compare& __c) : __comp_(__c) {}
766 template <class _Tp, class _Up>
767 bool operator()(const _Tp& __x, const _Up& __y)
769 bool __r = __comp_(__x, __y);
771 __do_compare_assert(0, __y, __x);
775 template <class _LHS, class _RHS>
776 inline _LIBCPP_INLINE_VISIBILITY
777 decltype((void)_VSTD::declval<_Compare&>()(
778 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
779 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
780 _LIBCPP_ASSERT(!__comp_(__l, __r),
781 "Comparator does not induce a strict weak ordering");
784 template <class _LHS, class _RHS>
785 inline _LIBCPP_INLINE_VISIBILITY
786 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
789 #endif // _LIBCPP_DEBUG
791 // Precondition: __x != 0
792 inline _LIBCPP_INLINE_VISIBILITY
793 unsigned __ctz(unsigned __x) {
794 #ifndef _LIBCPP_COMPILER_MSVC
795 return static_cast<unsigned>(__builtin_ctz(__x));
797 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
798 static_assert(sizeof(unsigned long) == 4, "");
800 // Search from LSB to MSB for first set bit.
801 // Returns zero if no set bit is found.
802 if (_BitScanForward(&where, __x))
808 inline _LIBCPP_INLINE_VISIBILITY
809 unsigned long __ctz(unsigned long __x) {
810 #ifndef _LIBCPP_COMPILER_MSVC
811 return static_cast<unsigned long>(__builtin_ctzl(__x));
813 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
814 return __ctz(static_cast<unsigned>(__x));
818 inline _LIBCPP_INLINE_VISIBILITY
819 unsigned long long __ctz(unsigned long long __x) {
820 #ifndef _LIBCPP_COMPILER_MSVC
821 return static_cast<unsigned long long>(__builtin_ctzll(__x));
824 // Search from LSB to MSB for first set bit.
825 // Returns zero if no set bit is found.
826 #if defined(_LIBCPP_HAS_BITSCAN64)
827 (defined(_M_AMD64) || defined(__x86_64__))
828 if (_BitScanForward64(&where, __x))
829 return static_cast<int>(where);
831 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
832 // Scan the Low Word.
833 if (_BitScanForward(&where, static_cast<unsigned long>(__x)))
835 // Scan the High Word.
836 if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
837 return where + 32; // Create a bit offset from the LSB.
840 #endif // _LIBCPP_COMPILER_MSVC
843 // Precondition: __x != 0
844 inline _LIBCPP_INLINE_VISIBILITY
845 unsigned __clz(unsigned __x) {
846 #ifndef _LIBCPP_COMPILER_MSVC
847 return static_cast<unsigned>(__builtin_clz(__x));
849 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
850 static_assert(sizeof(unsigned long) == 4, "");
852 // Search from LSB to MSB for first set bit.
853 // Returns zero if no set bit is found.
854 if (_BitScanReverse(&where, __x))
856 return 32; // Undefined Behavior.
860 inline _LIBCPP_INLINE_VISIBILITY
861 unsigned long __clz(unsigned long __x) {
862 #ifndef _LIBCPP_COMPILER_MSVC
863 return static_cast<unsigned long>(__builtin_clzl (__x));
865 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
866 return __clz(static_cast<unsigned>(__x));
870 inline _LIBCPP_INLINE_VISIBILITY
871 unsigned long long __clz(unsigned long long __x) {
872 #ifndef _LIBCPP_COMPILER_MSVC
873 return static_cast<unsigned long long>(__builtin_clzll(__x));
876 // BitScanReverse scans from MSB to LSB for first set bit.
877 // Returns 0 if no set bit is found.
878 #if defined(_LIBCPP_HAS_BITSCAN64)
879 if (_BitScanReverse64(&where, __x))
880 return static_cast<int>(63 - where);
882 // Scan the high 32 bits.
883 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
884 return 63 - (where + 32); // Create a bit offset from the MSB.
885 // Scan the low 32 bits.
886 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
889 return 64; // Undefined Behavior.
890 #endif // _LIBCPP_COMPILER_MSVC
893 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
894 #ifndef _LIBCPP_COMPILER_MSVC
895 return __builtin_popcount (__x);
897 static_assert(sizeof(unsigned) == 4, "");
898 return __popcnt(__x);
902 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
903 #ifndef _LIBCPP_COMPILER_MSVC
904 return __builtin_popcountl (__x);
906 static_assert(sizeof(unsigned long) == 4, "");
907 return __popcnt(__x);
911 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
912 #ifndef _LIBCPP_COMPILER_MSVC
913 return __builtin_popcountll(__x);
915 static_assert(sizeof(unsigned long long) == 8, "");
916 return __popcnt64(__x);
922 template <class _InputIterator, class _Predicate>
923 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
925 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
927 for (; __first != __last; ++__first)
928 if (!__pred(*__first))
935 template <class _InputIterator, class _Predicate>
936 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
938 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
940 for (; __first != __last; ++__first)
941 if (__pred(*__first))
948 template <class _InputIterator, class _Predicate>
949 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
951 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
953 for (; __first != __last; ++__first)
954 if (__pred(*__first))
961 template <class _InputIterator, class _Function>
962 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
964 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
966 for (; __first != __last; ++__first)
971 #if _LIBCPP_STD_VER > 14
974 template <class _InputIterator, class _Size, class _Function>
975 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
977 for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
979 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
980 _IntegralSize __n = __orig_n;
993 template <class _InputIterator, class _Tp>
994 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
996 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
998 for (; __first != __last; ++__first)
999 if (*__first == __value_)
1006 template <class _InputIterator, class _Predicate>
1007 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1009 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1011 for (; __first != __last; ++__first)
1012 if (__pred(*__first))
1019 template<class _InputIterator, class _Predicate>
1020 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1022 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1024 for (; __first != __last; ++__first)
1025 if (!__pred(*__first))
1032 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1033 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
1034 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1035 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1036 forward_iterator_tag, forward_iterator_tag)
1038 // modeled after search algorithm
1039 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1040 if (__first2 == __last2)
1046 if (__first1 == __last1) // if source exhausted return last correct answer
1047 return __r; // (or __last1 if never found)
1048 if (__pred(*__first1, *__first2))
1052 // *__first1 matches *__first2, now match elements after here
1053 _ForwardIterator1 __m1 = __first1;
1054 _ForwardIterator2 __m2 = __first2;
1057 if (++__m2 == __last2)
1058 { // Pattern exhaused, record answer and search for another one
1063 if (++__m1 == __last1) // Source exhausted, return last answer
1065 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1069 } // else there is a match, check next elements
1074 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1075 _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
1076 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1077 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1078 bidirectional_iterator_tag, bidirectional_iterator_tag)
1080 // modeled after search algorithm (in reverse)
1081 if (__first2 == __last2)
1082 return __last1; // Everything matches an empty sequence
1083 _BidirectionalIterator1 __l1 = __last1;
1084 _BidirectionalIterator2 __l2 = __last2;
1088 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1091 if (__first1 == __l1) // return __last1 if no element matches *__first2
1093 if (__pred(*--__l1, *__l2))
1096 // *__l1 matches *__l2, now match elements before here
1097 _BidirectionalIterator1 __m1 = __l1;
1098 _BidirectionalIterator2 __m2 = __l2;
1101 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1103 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1105 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1108 } // else there is a match, check next elements
1113 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1114 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1115 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1116 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1117 random_access_iterator_tag, random_access_iterator_tag)
1119 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1120 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1123 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1124 if (__len1 < __len2)
1126 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1127 _RandomAccessIterator1 __l1 = __last1;
1128 _RandomAccessIterator2 __l2 = __last2;
1136 if (__pred(*--__l1, *__l2))
1139 _RandomAccessIterator1 __m1 = __l1;
1140 _RandomAccessIterator2 __m2 = __l2;
1143 if (__m2 == __first2)
1145 // no need to check range on __m1 because __s guarantees we have enough source
1146 if (!__pred(*--__m1, *--__m2))
1154 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1155 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1157 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1158 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1160 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1161 (__first1, __last1, __first2, __last2, __pred,
1162 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1163 typename iterator_traits<_ForwardIterator2>::iterator_category());
1166 template <class _ForwardIterator1, class _ForwardIterator2>
1167 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1169 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1170 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1172 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1173 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1174 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1179 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1180 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1181 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1182 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1184 for (; __first1 != __last1; ++__first1)
1185 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1186 if (__pred(*__first1, *__j))
1192 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1193 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1195 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1196 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1198 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1201 template <class _ForwardIterator1, class _ForwardIterator2>
1202 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1204 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1205 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1207 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1208 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1209 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1214 template <class _ForwardIterator, class _BinaryPredicate>
1215 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1217 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1219 if (__first != __last)
1221 _ForwardIterator __i = __first;
1222 while (++__i != __last)
1224 if (__pred(*__first, *__i))
1232 template <class _ForwardIterator>
1233 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1235 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1237 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1238 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1243 template <class _InputIterator, class _Tp>
1244 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1245 typename iterator_traits<_InputIterator>::difference_type
1246 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1248 typename iterator_traits<_InputIterator>::difference_type __r(0);
1249 for (; __first != __last; ++__first)
1250 if (*__first == __value_)
1257 template <class _InputIterator, class _Predicate>
1258 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1259 typename iterator_traits<_InputIterator>::difference_type
1260 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1262 typename iterator_traits<_InputIterator>::difference_type __r(0);
1263 for (; __first != __last; ++__first)
1264 if (__pred(*__first))
1271 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1272 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1273 pair<_InputIterator1, _InputIterator2>
1274 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1275 _InputIterator2 __first2, _BinaryPredicate __pred)
1277 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1278 if (!__pred(*__first1, *__first2))
1280 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1283 template <class _InputIterator1, class _InputIterator2>
1284 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1285 pair<_InputIterator1, _InputIterator2>
1286 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1288 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1289 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1290 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1293 #if _LIBCPP_STD_VER > 11
1294 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1295 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1296 pair<_InputIterator1, _InputIterator2>
1297 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1298 _InputIterator2 __first2, _InputIterator2 __last2,
1299 _BinaryPredicate __pred)
1301 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1302 if (!__pred(*__first1, *__first2))
1304 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1307 template <class _InputIterator1, class _InputIterator2>
1308 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1309 pair<_InputIterator1, _InputIterator2>
1310 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1311 _InputIterator2 __first2, _InputIterator2 __last2)
1313 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1314 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1315 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1321 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1322 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1324 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1326 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1327 if (!__pred(*__first1, *__first2))
1332 template <class _InputIterator1, class _InputIterator2>
1333 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1335 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1337 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1338 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1339 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1342 #if _LIBCPP_STD_VER > 11
1343 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1344 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1346 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1347 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1348 input_iterator_tag, input_iterator_tag )
1350 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1351 if (!__pred(*__first1, *__first2))
1353 return __first1 == __last1 && __first2 == __last2;
1356 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1357 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1359 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1360 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1361 random_access_iterator_tag, random_access_iterator_tag )
1363 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1365 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1366 typename add_lvalue_reference<_BinaryPredicate>::type>
1367 (__first1, __last1, __first2, __pred );
1370 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1371 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1373 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1374 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1376 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1377 (__first1, __last1, __first2, __last2, __pred,
1378 typename iterator_traits<_InputIterator1>::iterator_category(),
1379 typename iterator_traits<_InputIterator2>::iterator_category());
1382 template <class _InputIterator1, class _InputIterator2>
1383 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1385 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1386 _InputIterator2 __first2, _InputIterator2 __last2)
1388 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1389 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1390 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1391 typename iterator_traits<_InputIterator1>::iterator_category(),
1392 typename iterator_traits<_InputIterator2>::iterator_category());
1398 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1399 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1400 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1401 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1403 // shorten sequences as much as possible by lopping of any equal prefix
1404 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1405 if (!__pred(*__first1, *__first2))
1407 if (__first1 == __last1)
1410 // __first1 != __last1 && *__first1 != *__first2
1411 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1412 _D1 __l1 = _VSTD::distance(__first1, __last1);
1415 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1416 // For each element in [f1, l1) see if there are the same number of
1417 // equal elements in [f2, l2)
1418 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1420 // Have we already counted the number of *__i in [f1, l1)?
1421 _ForwardIterator1 __match = __first1;
1422 for (; __match != __i; ++__match)
1423 if (__pred(*__match, *__i))
1425 if (__match == __i) {
1426 // Count number of *__i in [f2, l2)
1428 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1429 if (__pred(*__i, *__j))
1433 // Count number of *__i in [__i, l1) (we can start with 1)
1435 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1436 if (__pred(*__i, *__j))
1445 template<class _ForwardIterator1, class _ForwardIterator2>
1446 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1448 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1449 _ForwardIterator2 __first2)
1451 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1452 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1453 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1456 #if _LIBCPP_STD_VER > 11
1457 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1458 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1459 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1460 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1461 _BinaryPredicate __pred,
1462 forward_iterator_tag, forward_iterator_tag )
1464 // shorten sequences as much as possible by lopping of any equal prefix
1465 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1466 if (!__pred(*__first1, *__first2))
1468 if (__first1 == __last1)
1469 return __first2 == __last2;
1470 else if (__first2 == __last2)
1473 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1474 _D1 __l1 = _VSTD::distance(__first1, __last1);
1476 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1477 _D2 __l2 = _VSTD::distance(__first2, __last2);
1481 // For each element in [f1, l1) see if there are the same number of
1482 // equal elements in [f2, l2)
1483 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1485 // Have we already counted the number of *__i in [f1, l1)?
1486 _ForwardIterator1 __match = __first1;
1487 for (; __match != __i; ++__match)
1488 if (__pred(*__match, *__i))
1490 if (__match == __i) {
1491 // Count number of *__i in [f2, l2)
1493 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1494 if (__pred(*__i, *__j))
1498 // Count number of *__i in [__i, l1) (we can start with 1)
1500 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1501 if (__pred(*__i, *__j))
1510 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1511 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
1512 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1513 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1514 _BinaryPredicate __pred,
1515 random_access_iterator_tag, random_access_iterator_tag )
1517 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1519 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1520 typename add_lvalue_reference<_BinaryPredicate>::type>
1521 (__first1, __last1, __first2, __pred );
1524 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1525 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1527 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1528 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1529 _BinaryPredicate __pred )
1531 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1532 (__first1, __last1, __first2, __last2, __pred,
1533 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1534 typename iterator_traits<_ForwardIterator2>::iterator_category());
1537 template<class _ForwardIterator1, class _ForwardIterator2>
1538 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1540 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1541 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1543 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1544 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1545 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1546 __equal_to<__v1, __v2>(),
1547 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1548 typename iterator_traits<_ForwardIterator2>::iterator_category());
1553 // __search is in <functional>
1555 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1556 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1558 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1559 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1561 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1562 (__first1, __last1, __first2, __last2, __pred,
1563 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1564 typename iterator_traits<_ForwardIterator2>::iterator_category())
1568 template <class _ForwardIterator1, class _ForwardIterator2>
1569 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1571 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1572 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1574 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1575 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1576 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1580 #if _LIBCPP_STD_VER > 14
1581 template <class _ForwardIterator, class _Searcher>
1582 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1583 _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1584 { return __s(__f, __l).first; }
1589 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1590 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
1591 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1592 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1598 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1601 if (__first == __last) // return __last if no element matches __value_
1603 if (__pred(*__first, __value_))
1607 // *__first matches __value_, now match elements after here
1608 _ForwardIterator __m = __first;
1612 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1614 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1616 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1621 } // else there is a match, check next elements
1626 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1627 _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
1628 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1629 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1633 _Size __len = static_cast<_Size>(__last - __first);
1634 if (__len < __count)
1636 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1639 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1642 if (__first >= __s) // return __last if no element matches __value_
1644 if (__pred(*__first, __value_))
1648 // *__first matches __value_, now match elements after here
1649 _RandomAccessIterator __m = __first;
1653 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1655 ++__m; // no need to check range on __m because __s guarantees we have enough source
1656 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1661 } // else there is a match, check next elements
1666 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1667 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1669 search_n(_ForwardIterator __first, _ForwardIterator __last,
1670 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1672 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1673 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1674 typename iterator_traits<_ForwardIterator>::iterator_category());
1677 template <class _ForwardIterator, class _Size, class _Tp>
1678 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1680 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1682 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1683 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1684 __value_, __equal_to<__v, _Tp>());
1688 template <class _Iter>
1689 inline _LIBCPP_INLINE_VISIBILITY
1691 __unwrap_iter(_Iter __i)
1696 template <class _Tp>
1697 inline _LIBCPP_INLINE_VISIBILITY
1700 is_trivially_copy_assignable<_Tp>::value,
1703 __unwrap_iter(move_iterator<_Tp*> __i)
1708 #if _LIBCPP_DEBUG_LEVEL < 2
1710 template <class _Tp>
1711 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1714 is_trivially_copy_assignable<_Tp>::value,
1717 __unwrap_iter(__wrap_iter<_Tp*> __i)
1724 template <class _Tp>
1725 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1728 is_trivially_copy_assignable<_Tp>::value,
1731 __unwrap_iter(__wrap_iter<_Tp*> __i)
1736 #endif // _LIBCPP_DEBUG_LEVEL < 2
1738 template <class _InputIterator, class _OutputIterator>
1739 inline _LIBCPP_INLINE_VISIBILITY
1741 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1743 for (; __first != __last; ++__first, (void) ++__result)
1744 *__result = *__first;
1748 template <class _Tp, class _Up>
1749 inline _LIBCPP_INLINE_VISIBILITY
1752 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1753 is_trivially_copy_assignable<_Up>::value,
1756 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1758 const size_t __n = static_cast<size_t>(__last - __first);
1760 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1761 return __result + __n;
1764 template <class _InputIterator, class _OutputIterator>
1765 inline _LIBCPP_INLINE_VISIBILITY
1767 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1769 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1774 template <class _BidirectionalIterator, class _OutputIterator>
1775 inline _LIBCPP_INLINE_VISIBILITY
1777 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1779 while (__first != __last)
1780 *--__result = *--__last;
1784 template <class _Tp, class _Up>
1785 inline _LIBCPP_INLINE_VISIBILITY
1788 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1789 is_trivially_copy_assignable<_Up>::value,
1792 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1794 const size_t __n = static_cast<size_t>(__last - __first);
1798 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1803 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1804 inline _LIBCPP_INLINE_VISIBILITY
1805 _BidirectionalIterator2
1806 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1807 _BidirectionalIterator2 __result)
1809 return _VSTD::__copy_backward(__unwrap_iter(__first),
1810 __unwrap_iter(__last),
1811 __unwrap_iter(__result));
1816 template<class _InputIterator, class _OutputIterator, class _Predicate>
1817 inline _LIBCPP_INLINE_VISIBILITY
1819 copy_if(_InputIterator __first, _InputIterator __last,
1820 _OutputIterator __result, _Predicate __pred)
1822 for (; __first != __last; ++__first)
1824 if (__pred(*__first))
1826 *__result = *__first;
1835 template<class _InputIterator, class _Size, class _OutputIterator>
1836 inline _LIBCPP_INLINE_VISIBILITY
1839 __is_input_iterator<_InputIterator>::value &&
1840 !__is_random_access_iterator<_InputIterator>::value,
1843 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1845 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1846 _IntegralSize __n = __orig_n;
1849 *__result = *__first;
1851 for (--__n; __n > 0; --__n)
1854 *__result = *__first;
1861 template<class _InputIterator, class _Size, class _OutputIterator>
1862 inline _LIBCPP_INLINE_VISIBILITY
1865 __is_random_access_iterator<_InputIterator>::value,
1868 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1870 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1871 _IntegralSize __n = __orig_n;
1872 return _VSTD::copy(__first, __first + __n, __result);
1877 template <class _InputIterator, class _OutputIterator>
1878 inline _LIBCPP_INLINE_VISIBILITY
1880 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1882 for (; __first != __last; ++__first, (void) ++__result)
1883 *__result = _VSTD::move(*__first);
1887 template <class _Tp, class _Up>
1888 inline _LIBCPP_INLINE_VISIBILITY
1891 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1892 is_trivially_copy_assignable<_Up>::value,
1895 __move(_Tp* __first, _Tp* __last, _Up* __result)
1897 const size_t __n = static_cast<size_t>(__last - __first);
1899 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1900 return __result + __n;
1903 template <class _InputIterator, class _OutputIterator>
1904 inline _LIBCPP_INLINE_VISIBILITY
1906 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1908 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1913 template <class _InputIterator, class _OutputIterator>
1914 inline _LIBCPP_INLINE_VISIBILITY
1916 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1918 while (__first != __last)
1919 *--__result = _VSTD::move(*--__last);
1923 template <class _Tp, class _Up>
1924 inline _LIBCPP_INLINE_VISIBILITY
1927 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1928 is_trivially_copy_assignable<_Up>::value,
1931 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1933 const size_t __n = static_cast<size_t>(__last - __first);
1937 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1942 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1943 inline _LIBCPP_INLINE_VISIBILITY
1944 _BidirectionalIterator2
1945 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1946 _BidirectionalIterator2 __result)
1948 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1953 // moved to <type_traits> for better swap / noexcept support
1957 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1958 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1960 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1962 for (; __first != __last; ++__first, (void) ++__result)
1963 *__result = __op(*__first);
1967 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1968 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1970 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1971 _OutputIterator __result, _BinaryOperation __binary_op)
1973 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1974 *__result = __binary_op(*__first1, *__first2);
1980 template <class _ForwardIterator, class _Tp>
1981 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1983 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1985 for (; __first != __last; ++__first)
1986 if (*__first == __old_value)
1987 *__first = __new_value;
1992 template <class _ForwardIterator, class _Predicate, class _Tp>
1993 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1995 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1997 for (; __first != __last; ++__first)
1998 if (__pred(*__first))
1999 *__first = __new_value;
2004 template <class _InputIterator, class _OutputIterator, class _Tp>
2005 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2007 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2008 const _Tp& __old_value, const _Tp& __new_value)
2010 for (; __first != __last; ++__first, (void) ++__result)
2011 if (*__first == __old_value)
2012 *__result = __new_value;
2014 *__result = *__first;
2020 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2021 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2023 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2024 _Predicate __pred, const _Tp& __new_value)
2026 for (; __first != __last; ++__first, (void) ++__result)
2027 if (__pred(*__first))
2028 *__result = __new_value;
2030 *__result = *__first;
2036 template <class _OutputIterator, class _Size, class _Tp>
2037 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2039 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2041 for (; __n > 0; ++__first, (void) --__n)
2042 *__first = __value_;
2046 template <class _OutputIterator, class _Size, class _Tp>
2047 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2049 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2051 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
2056 template <class _ForwardIterator, class _Tp>
2057 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2059 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2061 for (; __first != __last; ++__first)
2062 *__first = __value_;
2065 template <class _RandomAccessIterator, class _Tp>
2066 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2068 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2070 _VSTD::fill_n(__first, __last - __first, __value_);
2073 template <class _ForwardIterator, class _Tp>
2074 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2076 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2078 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2083 template <class _ForwardIterator, class _Generator>
2084 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2086 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2088 for (; __first != __last; ++__first)
2094 template <class _OutputIterator, class _Size, class _Generator>
2095 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2097 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2099 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2100 _IntegralSize __n = __orig_n;
2101 for (; __n > 0; ++__first, (void) --__n)
2108 template <class _ForwardIterator, class _Tp>
2109 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2110 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2112 __first = _VSTD::find(__first, __last, __value_);
2113 if (__first != __last)
2115 _ForwardIterator __i = __first;
2116 while (++__i != __last)
2118 if (!(*__i == __value_))
2120 *__first = _VSTD::move(*__i);
2130 template <class _ForwardIterator, class _Predicate>
2131 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2132 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2134 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2135 (__first, __last, __pred);
2136 if (__first != __last)
2138 _ForwardIterator __i = __first;
2139 while (++__i != __last)
2143 *__first = _VSTD::move(*__i);
2153 template <class _InputIterator, class _OutputIterator, class _Tp>
2154 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2156 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2158 for (; __first != __last; ++__first)
2160 if (!(*__first == __value_))
2162 *__result = *__first;
2171 template <class _InputIterator, class _OutputIterator, class _Predicate>
2172 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2174 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2176 for (; __first != __last; ++__first)
2178 if (!__pred(*__first))
2180 *__result = *__first;
2189 template <class _ForwardIterator, class _BinaryPredicate>
2190 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2191 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2193 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2194 (__first, __last, __pred);
2195 if (__first != __last)
2199 _ForwardIterator __i = __first;
2200 for (++__i; ++__i != __last;)
2201 if (!__pred(*__first, *__i))
2202 *++__first = _VSTD::move(*__i);
2208 template <class _ForwardIterator>
2209 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2211 unique(_ForwardIterator __first, _ForwardIterator __last)
2213 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2214 return _VSTD::unique(__first, __last, __equal_to<__v>());
2219 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2220 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2221 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2222 input_iterator_tag, output_iterator_tag)
2224 if (__first != __last)
2226 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2229 while (++__first != __last)
2231 if (!__pred(__t, *__first))
2242 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2243 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
2244 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2245 forward_iterator_tag, output_iterator_tag)
2247 if (__first != __last)
2249 _ForwardIterator __i = __first;
2252 while (++__first != __last)
2254 if (!__pred(*__i, *__first))
2256 *__result = *__first;
2265 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2266 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
2267 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2268 input_iterator_tag, forward_iterator_tag)
2270 if (__first != __last)
2272 *__result = *__first;
2273 while (++__first != __last)
2274 if (!__pred(*__result, *__first))
2275 *++__result = *__first;
2281 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2282 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2284 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2286 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2287 (__first, __last, __result, __pred,
2288 typename iterator_traits<_InputIterator>::iterator_category(),
2289 typename iterator_traits<_OutputIterator>::iterator_category());
2292 template <class _InputIterator, class _OutputIterator>
2293 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2295 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2297 typedef typename iterator_traits<_InputIterator>::value_type __v;
2298 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2303 template <class _BidirectionalIterator>
2304 inline _LIBCPP_INLINE_VISIBILITY
2306 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2308 while (__first != __last)
2310 if (__first == --__last)
2312 _VSTD::iter_swap(__first, __last);
2317 template <class _RandomAccessIterator>
2318 inline _LIBCPP_INLINE_VISIBILITY
2320 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2322 if (__first != __last)
2323 for (; __first < --__last; ++__first)
2324 _VSTD::iter_swap(__first, __last);
2327 template <class _BidirectionalIterator>
2328 inline _LIBCPP_INLINE_VISIBILITY
2330 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2332 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2337 template <class _BidirectionalIterator, class _OutputIterator>
2338 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2340 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2342 for (; __first != __last; ++__result)
2343 *__result = *--__last;
2349 template <class _ForwardIterator>
2351 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2353 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2354 value_type __tmp = _VSTD::move(*__first);
2355 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2356 *__lm1 = _VSTD::move(__tmp);
2360 template <class _BidirectionalIterator>
2361 _BidirectionalIterator
2362 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2364 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2365 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2366 value_type __tmp = _VSTD::move(*__lm1);
2367 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2368 *__first = _VSTD::move(__tmp);
2372 template <class _ForwardIterator>
2374 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2376 _ForwardIterator __i = __middle;
2379 swap(*__first, *__i);
2381 if (++__i == __last)
2383 if (__first == __middle)
2386 _ForwardIterator __r = __first;
2387 if (__first != __middle)
2392 swap(*__first, *__i);
2394 if (++__i == __last)
2396 if (__first == __middle)
2400 else if (__first == __middle)
2407 template<typename _Integral>
2408 inline _LIBCPP_INLINE_VISIBILITY
2410 __algo_gcd(_Integral __x, _Integral __y)
2414 _Integral __t = __x % __y;
2421 template<typename _RandomAccessIterator>
2422 _RandomAccessIterator
2423 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2425 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2426 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2428 const difference_type __m1 = __middle - __first;
2429 const difference_type __m2 = __last - __middle;
2432 _VSTD::swap_ranges(__first, __middle, __middle);
2435 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
2436 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2438 value_type __t(_VSTD::move(*--__p));
2439 _RandomAccessIterator __p1 = __p;
2440 _RandomAccessIterator __p2 = __p1 + __m1;
2443 *__p1 = _VSTD::move(*__p2);
2445 const difference_type __d = __last - __p2;
2449 __p2 = __first + (__m1 - __d);
2450 } while (__p2 != __p);
2451 *__p1 = _VSTD::move(__t);
2453 return __first + __m2;
2456 template <class _ForwardIterator>
2457 inline _LIBCPP_INLINE_VISIBILITY
2459 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2460 _VSTD::forward_iterator_tag)
2462 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2463 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2465 if (_VSTD::next(__first) == __middle)
2466 return _VSTD::__rotate_left(__first, __last);
2468 return _VSTD::__rotate_forward(__first, __middle, __last);
2471 template <class _BidirectionalIterator>
2472 inline _LIBCPP_INLINE_VISIBILITY
2473 _BidirectionalIterator
2474 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2475 _VSTD::bidirectional_iterator_tag)
2477 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2478 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2480 if (_VSTD::next(__first) == __middle)
2481 return _VSTD::__rotate_left(__first, __last);
2482 if (_VSTD::next(__middle) == __last)
2483 return _VSTD::__rotate_right(__first, __last);
2485 return _VSTD::__rotate_forward(__first, __middle, __last);
2488 template <class _RandomAccessIterator>
2489 inline _LIBCPP_INLINE_VISIBILITY
2490 _RandomAccessIterator
2491 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2492 _VSTD::random_access_iterator_tag)
2494 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2495 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2497 if (_VSTD::next(__first) == __middle)
2498 return _VSTD::__rotate_left(__first, __last);
2499 if (_VSTD::next(__middle) == __last)
2500 return _VSTD::__rotate_right(__first, __last);
2501 return _VSTD::__rotate_gcd(__first, __middle, __last);
2503 return _VSTD::__rotate_forward(__first, __middle, __last);
2506 template <class _ForwardIterator>
2507 inline _LIBCPP_INLINE_VISIBILITY
2509 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2511 if (__first == __middle)
2513 if (__middle == __last)
2515 return _VSTD::__rotate(__first, __middle, __last,
2516 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2521 template <class _ForwardIterator, class _OutputIterator>
2522 inline _LIBCPP_INLINE_VISIBILITY
2524 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2526 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2531 template <class _ForwardIterator, class _Compare>
2532 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2534 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2536 if (__first != __last)
2538 _ForwardIterator __i = __first;
2539 while (++__i != __last)
2540 if (__comp(*__i, *__first))
2546 template <class _ForwardIterator>
2547 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2549 min_element(_ForwardIterator __first, _ForwardIterator __last)
2551 return _VSTD::min_element(__first, __last,
2552 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2557 template <class _Tp, class _Compare>
2558 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2560 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2562 return __comp(__b, __a) ? __b : __a;
2565 template <class _Tp>
2566 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2568 min(const _Tp& __a, const _Tp& __b)
2570 return _VSTD::min(__a, __b, __less<_Tp>());
2573 #ifndef _LIBCPP_CXX03_LANG
2575 template<class _Tp, class _Compare>
2576 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2578 min(initializer_list<_Tp> __t, _Compare __comp)
2580 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2584 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2586 min(initializer_list<_Tp> __t)
2588 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2591 #endif // _LIBCPP_CXX03_LANG
2595 template <class _ForwardIterator, class _Compare>
2596 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2598 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2600 if (__first != __last)
2602 _ForwardIterator __i = __first;
2603 while (++__i != __last)
2604 if (__comp(*__first, *__i))
2611 template <class _ForwardIterator>
2612 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2614 max_element(_ForwardIterator __first, _ForwardIterator __last)
2616 return _VSTD::max_element(__first, __last,
2617 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2622 template <class _Tp, class _Compare>
2623 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2625 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2627 return __comp(__a, __b) ? __b : __a;
2630 template <class _Tp>
2631 inline _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 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2643 max(initializer_list<_Tp> __t, _Compare __comp)
2645 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2649 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2651 max(initializer_list<_Tp> __t)
2653 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2656 #endif // _LIBCPP_CXX03_LANG
2658 #if _LIBCPP_STD_VER > 14
2660 template<class _Tp, class _Compare>
2661 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2663 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2665 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2666 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2671 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2673 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2675 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2681 template <class _ForwardIterator, class _Compare>
2682 _LIBCPP_CONSTEXPR_AFTER_CXX11
2683 std::pair<_ForwardIterator, _ForwardIterator>
2684 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2686 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2687 if (__first != __last)
2689 if (++__first != __last)
2691 if (__comp(*__first, *__result.first))
2692 __result.first = __first;
2694 __result.second = __first;
2695 while (++__first != __last)
2697 _ForwardIterator __i = __first;
2698 if (++__first == __last)
2700 if (__comp(*__i, *__result.first))
2701 __result.first = __i;
2702 else if (!__comp(*__i, *__result.second))
2703 __result.second = __i;
2708 if (__comp(*__first, *__i))
2710 if (__comp(*__first, *__result.first))
2711 __result.first = __first;
2712 if (!__comp(*__i, *__result.second))
2713 __result.second = __i;
2717 if (__comp(*__i, *__result.first))
2718 __result.first = __i;
2719 if (!__comp(*__first, *__result.second))
2720 __result.second = __first;
2729 template <class _ForwardIterator>
2730 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2731 std::pair<_ForwardIterator, _ForwardIterator>
2732 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2734 return _VSTD::minmax_element(__first, __last,
2735 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2740 template<class _Tp, class _Compare>
2741 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2742 pair<const _Tp&, const _Tp&>
2743 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2745 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2746 pair<const _Tp&, const _Tp&>(__a, __b);
2750 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2751 pair<const _Tp&, const _Tp&>
2752 minmax(const _Tp& __a, const _Tp& __b)
2754 return _VSTD::minmax(__a, __b, __less<_Tp>());
2757 #ifndef _LIBCPP_CXX03_LANG
2759 template<class _Tp, class _Compare>
2760 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2762 minmax(initializer_list<_Tp> __t, _Compare __comp)
2764 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2765 _Iter __first = __t.begin();
2766 _Iter __last = __t.end();
2767 std::pair<_Tp, _Tp> __result(*__first, *__first);
2770 if (__t.size() % 2 == 0)
2772 if (__comp(*__first, __result.first))
2773 __result.first = *__first;
2775 __result.second = *__first;
2779 while (__first != __last)
2781 _Tp __prev = *__first++;
2782 if (__comp(*__first, __prev)) {
2783 if ( __comp(*__first, __result.first)) __result.first = *__first;
2784 if (!__comp(__prev, __result.second)) __result.second = __prev;
2787 if ( __comp(__prev, __result.first)) __result.first = __prev;
2788 if (!__comp(*__first, __result.second)) __result.second = *__first;
2797 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2799 minmax(initializer_list<_Tp> __t)
2801 return _VSTD::minmax(__t, __less<_Tp>());
2804 #endif // _LIBCPP_CXX03_LANG
2808 // __independent_bits_engine
2810 template <unsigned long long _Xp, size_t _Rp>
2813 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2814 : __log2_imp<_Xp, _Rp - 1>::value;
2817 template <unsigned long long _Xp>
2818 struct __log2_imp<_Xp, 0>
2820 static const size_t value = 0;
2823 template <size_t _Rp>
2824 struct __log2_imp<0, _Rp>
2826 static const size_t value = _Rp + 1;
2829 template <class _UIntType, _UIntType _Xp>
2832 static const size_t value = __log2_imp<_Xp,
2833 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
2836 template<class _Engine, class _UIntType>
2837 class __independent_bits_engine
2841 typedef _UIntType result_type;
2844 typedef typename _Engine::result_type _Engine_result_type;
2845 typedef typename conditional
2847 sizeof(_Engine_result_type) <= sizeof(result_type),
2850 >::type _Working_result_type;
2857 _Working_result_type __y0_;
2858 _Working_result_type __y1_;
2859 _Engine_result_type __mask0_;
2860 _Engine_result_type __mask1_;
2862 #ifdef _LIBCPP_CXX03_LANG
2863 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2864 + _Working_result_type(1);
2866 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2867 + _Working_result_type(1);
2869 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2870 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2871 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2874 // constructors and seeding functions
2875 __independent_bits_engine(_Engine& __e, size_t __w);
2877 // generating functions
2878 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2881 result_type __eval(false_type);
2882 result_type __eval(true_type);
2885 template<class _Engine, class _UIntType>
2886 __independent_bits_engine<_Engine, _UIntType>
2887 ::__independent_bits_engine(_Engine& __e, size_t __w)
2891 __n_ = __w_ / __m + (__w_ % __m != 0);
2892 __w0_ = __w_ / __n_;
2895 else if (__w0_ < _WDt)
2896 __y0_ = (_Rp >> __w0_) << __w0_;
2899 if (_Rp - __y0_ > __y0_ / __n_)
2902 __w0_ = __w_ / __n_;
2904 __y0_ = (_Rp >> __w0_) << __w0_;
2908 __n0_ = __n_ - __w_ % __n_;
2909 if (__w0_ < _WDt - 1)
2910 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2913 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2914 _Engine_result_type(0);
2915 __mask1_ = __w0_ < _EDt - 1 ?
2916 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2917 _Engine_result_type(~0);
2920 template<class _Engine, class _UIntType>
2923 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2925 return static_cast<result_type>(__e_() & __mask0_);
2928 template<class _Engine, class _UIntType>
2930 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2932 const size_t _WRt = numeric_limits<result_type>::digits;
2933 result_type _Sp = 0;
2934 for (size_t __k = 0; __k < __n0_; ++__k)
2936 _Engine_result_type __u;
2939 __u = __e_() - _Engine::min();
2940 } while (__u >= __y0_);
2945 _Sp += __u & __mask0_;
2947 for (size_t __k = __n0_; __k < __n_; ++__k)
2949 _Engine_result_type __u;
2952 __u = __e_() - _Engine::min();
2953 } while (__u >= __y1_);
2954 if (__w0_ < _WRt - 1)
2958 _Sp += __u & __mask1_;
2963 // uniform_int_distribution
2965 template<class _IntType = int>
2966 class uniform_int_distribution
2970 typedef _IntType result_type;
2977 typedef uniform_int_distribution distribution_type;
2979 explicit param_type(result_type __a = 0,
2980 result_type __b = numeric_limits<result_type>::max())
2981 : __a_(__a), __b_(__b) {}
2983 result_type a() const {return __a_;}
2984 result_type b() const {return __b_;}
2986 friend bool operator==(const param_type& __x, const param_type& __y)
2987 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2988 friend bool operator!=(const param_type& __x, const param_type& __y)
2989 {return !(__x == __y);}
2996 // constructors and reset functions
2997 explicit uniform_int_distribution(result_type __a = 0,
2998 result_type __b = numeric_limits<result_type>::max())
2999 : __p_(param_type(__a, __b)) {}
3000 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3003 // generating functions
3004 template<class _URNG> result_type operator()(_URNG& __g)
3005 {return (*this)(__g, __p_);}
3006 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3008 // property functions
3009 result_type a() const {return __p_.a();}
3010 result_type b() const {return __p_.b();}
3012 param_type param() const {return __p_;}
3013 void param(const param_type& __p) {__p_ = __p;}
3015 result_type min() const {return a();}
3016 result_type max() const {return b();}
3018 friend bool operator==(const uniform_int_distribution& __x,
3019 const uniform_int_distribution& __y)
3020 {return __x.__p_ == __y.__p_;}
3021 friend bool operator!=(const uniform_int_distribution& __x,
3022 const uniform_int_distribution& __y)
3023 {return !(__x == __y);}
3026 template<class _IntType>
3027 template<class _URNG>
3028 typename uniform_int_distribution<_IntType>::result_type
3029 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3031 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3032 uint32_t, uint64_t>::type _UIntType;
3033 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3036 const size_t _Dt = numeric_limits<_UIntType>::digits;
3037 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3039 return static_cast<result_type>(_Eng(__g, _Dt)());
3040 size_t __w = _Dt - __clz(_Rp) - 1;
3041 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3048 } while (__u >= _Rp);
3049 return static_cast<result_type>(__u + __p.a());
3052 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3053 || defined(_LIBCPP_BUILDING_LIBRARY)
3054 class _LIBCPP_TYPE_VIS __rs_default;
3056 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3058 class _LIBCPP_TYPE_VIS __rs_default
3060 static unsigned __c_;
3064 typedef uint_fast32_t result_type;
3066 static const result_type _Min = 0;
3067 static const result_type _Max = 0xFFFFFFFF;
3069 __rs_default(const __rs_default&);
3072 result_type operator()();
3074 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3075 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3077 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3080 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3082 template <class _RandomAccessIterator>
3084 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3086 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3087 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3088 typedef typename _Dp::param_type _Pp;
3089 difference_type __d = __last - __first;
3093 __rs_default __g = __rs_get();
3094 for (--__last, --__d; __first < __last; ++__first, --__d)
3096 difference_type __i = __uid(__g, _Pp(0, __d));
3097 if (__i != difference_type(0))
3098 swap(*__first, *(__first + __i));
3103 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3105 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3106 #ifndef _LIBCPP_CXX03_LANG
3107 _RandomNumberGenerator&& __rand)
3109 _RandomNumberGenerator& __rand)
3112 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3113 difference_type __d = __last - __first;
3116 for (--__last; __first < __last; ++__first, --__d)
3118 difference_type __i = __rand(__d);
3119 swap(*__first, *(__first + __i));
3125 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3126 class _UniformRandomNumberGenerator>
3127 _LIBCPP_INLINE_VISIBILITY
3128 _SampleIterator __sample(_PopulationIterator __first,
3129 _PopulationIterator __last, _SampleIterator __output_iter,
3131 _UniformRandomNumberGenerator & __g,
3132 input_iterator_tag) {
3135 for (; __first != __last && __k < __n; ++__first, (void)++__k)
3136 __output_iter[__k] = *__first;
3137 _Distance __sz = __k;
3138 for (; __first != __last; ++__first, (void)++__k) {
3139 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3141 __output_iter[__r] = *__first;
3143 return __output_iter + _VSTD::min(__n, __k);
3146 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3147 class _UniformRandomNumberGenerator>
3148 _LIBCPP_INLINE_VISIBILITY
3149 _SampleIterator __sample(_PopulationIterator __first,
3150 _PopulationIterator __last, _SampleIterator __output_iter,
3152 _UniformRandomNumberGenerator& __g,
3153 forward_iterator_tag) {
3154 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3155 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3157 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3159 *__output_iter++ = *__first;
3163 return __output_iter;
3166 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3167 class _UniformRandomNumberGenerator>
3168 _LIBCPP_INLINE_VISIBILITY
3169 _SampleIterator __sample(_PopulationIterator __first,
3170 _PopulationIterator __last, _SampleIterator __output_iter,
3171 _Distance __n, _UniformRandomNumberGenerator& __g) {
3172 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3174 typedef typename iterator_traits<_PopulationIterator>::difference_type
3176 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3177 __is_random_access_iterator<_SampleIterator>::value,
3178 "SampleIterator must meet the requirements of RandomAccessIterator");
3179 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3180 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3181 return _VSTD::__sample(
3182 __first, __last, __output_iter, _CommonType(__n),
3183 __g, _PopCategory());
3186 #if _LIBCPP_STD_VER > 14
3187 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3188 class _UniformRandomNumberGenerator>
3189 inline _LIBCPP_INLINE_VISIBILITY
3190 _SampleIterator sample(_PopulationIterator __first,
3191 _PopulationIterator __last, _SampleIterator __output_iter,
3192 _Distance __n, _UniformRandomNumberGenerator&& __g) {
3193 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
3195 #endif // _LIBCPP_STD_VER > 14
3197 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3198 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3199 #ifndef _LIBCPP_CXX03_LANG
3200 _UniformRandomNumberGenerator&& __g)
3202 _UniformRandomNumberGenerator& __g)
3205 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3206 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3207 typedef typename _Dp::param_type _Pp;
3208 difference_type __d = __last - __first;
3212 for (--__last, --__d; __first < __last; ++__first, --__d)
3214 difference_type __i = __uid(__g, _Pp(0, __d));
3215 if (__i != difference_type(0))
3216 swap(*__first, *(__first + __i));
3221 template <class _InputIterator, class _Predicate>
3222 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
3223 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3225 for (; __first != __last; ++__first)
3226 if (!__pred(*__first))
3228 if ( __first == __last )
3231 for (; __first != __last; ++__first)
3232 if (__pred(*__first))
3239 template <class _Predicate, class _ForwardIterator>
3241 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3245 if (__first == __last)
3247 if (!__pred(*__first))
3251 for (_ForwardIterator __p = __first; ++__p != __last;)
3255 swap(*__first, *__p);
3262 template <class _Predicate, class _BidirectionalIterator>
3263 _BidirectionalIterator
3264 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3265 bidirectional_iterator_tag)
3271 if (__first == __last)
3273 if (!__pred(*__first))
3279 if (__first == --__last)
3281 } while (!__pred(*__last));
3282 swap(*__first, *__last);
3287 template <class _ForwardIterator, class _Predicate>
3288 inline _LIBCPP_INLINE_VISIBILITY
3290 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3292 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3293 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3298 template <class _InputIterator, class _OutputIterator1,
3299 class _OutputIterator2, class _Predicate>
3300 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
3301 partition_copy(_InputIterator __first, _InputIterator __last,
3302 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3305 for (; __first != __last; ++__first)
3307 if (__pred(*__first))
3309 *__out_true = *__first;
3314 *__out_false = *__first;
3318 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3323 template<class _ForwardIterator, class _Predicate>
3324 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3325 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3327 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3328 difference_type __len = _VSTD::distance(__first, __last);
3331 difference_type __l2 = __len / 2;
3332 _ForwardIterator __m = __first;
3333 _VSTD::advance(__m, __l2);
3347 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3349 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3350 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3352 // *__first is known to be false
3358 _ForwardIterator __m = __first;
3361 swap(*__first, *__m);
3366 if (__len <= __p.second)
3367 { // The buffer is big enough to use
3368 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3369 __destruct_n __d(0);
3370 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3371 // Move the falses into the temporary buffer, and the trues to the front of the line
3372 // Update __first to always point to the end of the trues
3373 value_type* __t = __p.first;
3374 ::new(__t) value_type(_VSTD::move(*__first));
3375 __d.__incr((value_type*)0);
3377 _ForwardIterator __i = __first;
3378 while (++__i != __last)
3382 *__first = _VSTD::move(*__i);
3387 ::new(__t) value_type(_VSTD::move(*__i));
3388 __d.__incr((value_type*)0);
3392 // All trues now at start of range, all falses in buffer
3393 // Move falses back into range, but don't mess up __first which points to first false
3395 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3396 *__i = _VSTD::move(*__t2);
3397 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3400 // Else not enough buffer, do in place
3402 _ForwardIterator __m = __first;
3403 _Distance __len2 = __len / 2; // __len2 >= 2
3404 _VSTD::advance(__m, __len2);
3405 // recurse on [__first, __m), *__first know to be false
3406 // F?????????????????
3408 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3409 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3410 // TTTFFFFF??????????
3412 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3413 _ForwardIterator __m1 = __m;
3414 _ForwardIterator __second_false = __last;
3415 _Distance __len_half = __len - __len2;
3416 while (__pred(*__m1))
3418 if (++__m1 == __last)
3419 goto __second_half_done;
3422 // TTTFFFFFTTTF??????
3424 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3426 // TTTFFFFFTTTTTFFFFF
3428 return _VSTD::rotate(__first_false, __m, __second_false);
3429 // TTTTTTTTFFFFFFFFFF
3433 struct __return_temporary_buffer
3435 template <class _Tp>
3436 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3439 template <class _Predicate, class _ForwardIterator>
3441 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3442 forward_iterator_tag)
3444 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3445 // Either prove all true and return __first or point to first false
3448 if (__first == __last)
3450 if (!__pred(*__first))
3454 // We now have a reduced range [__first, __last)
3455 // *__first is known to be false
3456 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3457 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3458 difference_type __len = _VSTD::distance(__first, __last);
3459 pair<value_type*, ptrdiff_t> __p(0, 0);
3460 unique_ptr<value_type, __return_temporary_buffer> __h;
3461 if (__len >= __alloc_limit)
3463 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3464 __h.reset(__p.first);
3466 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3467 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3470 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3471 _BidirectionalIterator
3472 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3473 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3475 // *__first is known to be false
3476 // *__last is known to be true
3480 swap(*__first, *__last);
3485 _BidirectionalIterator __m = __first;
3488 swap(*__first, *__m);
3489 swap(*__m, *__last);
3492 swap(*__m, *__last);
3493 swap(*__first, *__m);
3496 if (__len <= __p.second)
3497 { // The buffer is big enough to use
3498 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3499 __destruct_n __d(0);
3500 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3501 // Move the falses into the temporary buffer, and the trues to the front of the line
3502 // Update __first to always point to the end of the trues
3503 value_type* __t = __p.first;
3504 ::new(__t) value_type(_VSTD::move(*__first));
3505 __d.__incr((value_type*)0);
3507 _BidirectionalIterator __i = __first;
3508 while (++__i != __last)
3512 *__first = _VSTD::move(*__i);
3517 ::new(__t) value_type(_VSTD::move(*__i));
3518 __d.__incr((value_type*)0);
3522 // move *__last, known to be true
3523 *__first = _VSTD::move(*__i);
3525 // All trues now at start of range, all falses in buffer
3526 // Move falses back into range, but don't mess up __first which points to first false
3527 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3528 *__i = _VSTD::move(*__t2);
3529 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3532 // Else not enough buffer, do in place
3534 _BidirectionalIterator __m = __first;
3535 _Distance __len2 = __len / 2; // __len2 >= 2
3536 _VSTD::advance(__m, __len2);
3537 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3538 // F????????????????T
3540 _BidirectionalIterator __m1 = __m;
3541 _BidirectionalIterator __first_false = __first;
3542 _Distance __len_half = __len2;
3543 while (!__pred(*--__m1))
3545 if (__m1 == __first)
3546 goto __first_half_done;
3549 // F???TFFF?????????T
3551 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3552 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3554 // TTTFFFFF?????????T
3556 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3558 _BidirectionalIterator __second_false = __last;
3560 __len_half = __len - __len2;
3561 while (__pred(*__m1))
3563 if (++__m1 == __last)
3564 goto __second_half_done;
3567 // TTTFFFFFTTTF?????T
3569 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3571 // TTTFFFFFTTTTTFFFFF
3573 return _VSTD::rotate(__first_false, __m, __second_false);
3574 // TTTTTTTTFFFFFFFFFF
3578 template <class _Predicate, class _BidirectionalIterator>
3579 _BidirectionalIterator
3580 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3581 bidirectional_iterator_tag)
3583 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3584 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3585 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3586 // Either prove all true and return __first or point to first false
3589 if (__first == __last)
3591 if (!__pred(*__first))
3595 // __first points to first false, everything prior to __first is already set.
3596 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3599 if (__first == --__last)
3601 } while (!__pred(*__last));
3602 // We now have a reduced range [__first, __last]
3603 // *__first is known to be false
3604 // *__last is known to be true
3606 difference_type __len = _VSTD::distance(__first, __last) + 1;
3607 pair<value_type*, ptrdiff_t> __p(0, 0);
3608 unique_ptr<value_type, __return_temporary_buffer> __h;
3609 if (__len >= __alloc_limit)
3611 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3612 __h.reset(__p.first);
3614 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3615 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3618 template <class _ForwardIterator, class _Predicate>
3619 inline _LIBCPP_INLINE_VISIBILITY
3621 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3623 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3624 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3629 template <class _ForwardIterator, class _Compare>
3630 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
3631 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3633 if (__first != __last)
3635 _ForwardIterator __i = __first;
3636 while (++__i != __last)
3638 if (__comp(*__i, *__first))
3646 template<class _ForwardIterator>
3647 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3649 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3651 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3656 template <class _ForwardIterator, class _Compare>
3657 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3659 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3661 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3664 template<class _ForwardIterator>
3665 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
3667 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3669 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3674 // stable, 2-3 compares, 0-2 swaps
3676 template <class _Compare, class _ForwardIterator>
3678 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3681 if (!__c(*__y, *__x)) // if x <= y
3683 if (!__c(*__z, *__y)) // if y <= z
3684 return __r; // x <= y && y <= z
3686 swap(*__y, *__z); // x <= z && y < z
3688 if (__c(*__y, *__x)) // if x > y
3690 swap(*__x, *__y); // x < y && y <= z
3693 return __r; // x <= y && y < z
3695 if (__c(*__z, *__y)) // x > y, if y > z
3697 swap(*__x, *__z); // x < y && y < z
3701 swap(*__x, *__y); // x > y && y <= z
3702 __r = 1; // x < y && x <= z
3703 if (__c(*__z, *__y)) // if y > z
3705 swap(*__y, *__z); // x <= y && y < z
3709 } // x <= y && y <= z
3711 // stable, 3-6 compares, 0-5 swaps
3713 template <class _Compare, class _ForwardIterator>
3715 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3716 _ForwardIterator __x4, _Compare __c)
3718 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3719 if (__c(*__x4, *__x3))
3723 if (__c(*__x3, *__x2))
3727 if (__c(*__x2, *__x1))
3737 // stable, 4-10 compares, 0-9 swaps
3739 template <class _Compare, class _ForwardIterator>
3741 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3742 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3744 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3745 if (__c(*__x5, *__x4))
3749 if (__c(*__x4, *__x3))
3753 if (__c(*__x3, *__x2))
3757 if (__c(*__x2, *__x1))
3769 template <class _Compare, class _BirdirectionalIterator>
3771 __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3773 _BirdirectionalIterator __lm1 = __last;
3774 for (--__lm1; __first != __lm1; ++__first)
3776 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3777 typename add_lvalue_reference<_Compare>::type>
3778 (__first, __last, __comp);
3780 swap(*__first, *__i);
3784 template <class _Compare, class _BirdirectionalIterator>
3786 __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3788 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3789 if (__first != __last)
3791 _BirdirectionalIterator __i = __first;
3792 for (++__i; __i != __last; ++__i)
3794 _BirdirectionalIterator __j = __i;
3795 value_type __t(_VSTD::move(*__j));
3796 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3797 *__j = _VSTD::move(*__k);
3798 *__j = _VSTD::move(__t);
3803 template <class _Compare, class _RandomAccessIterator>
3805 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3807 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3808 _RandomAccessIterator __j = __first+2;
3809 __sort3<_Compare>(__first, __first+1, __j, __comp);
3810 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3812 if (__comp(*__i, *__j))
3814 value_type __t(_VSTD::move(*__i));
3815 _RandomAccessIterator __k = __j;
3819 *__j = _VSTD::move(*__k);
3821 } while (__j != __first && __comp(__t, *--__k));
3822 *__j = _VSTD::move(__t);
3828 template <class _Compare, class _RandomAccessIterator>
3830 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3832 switch (__last - __first)
3838 if (__comp(*--__last, *__first))
3839 swap(*__first, *__last);
3842 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3845 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3848 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3851 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3852 _RandomAccessIterator __j = __first+2;
3853 __sort3<_Compare>(__first, __first+1, __j, __comp);
3854 const unsigned __limit = 8;
3855 unsigned __count = 0;
3856 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3858 if (__comp(*__i, *__j))
3860 value_type __t(_VSTD::move(*__i));
3861 _RandomAccessIterator __k = __j;
3865 *__j = _VSTD::move(*__k);
3867 } while (__j != __first && __comp(__t, *--__k));
3868 *__j = _VSTD::move(__t);
3869 if (++__count == __limit)
3870 return ++__i == __last;
3877 template <class _Compare, class _BirdirectionalIterator>
3879 __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3880 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3882 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3883 if (__first1 != __last1)
3885 __destruct_n __d(0);
3886 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3887 value_type* __last2 = __first2;
3888 ::new(__last2) value_type(_VSTD::move(*__first1));
3889 __d.__incr((value_type*)0);
3890 for (++__last2; ++__first1 != __last1; ++__last2)
3892 value_type* __j2 = __last2;
3893 value_type* __i2 = __j2;
3894 if (__comp(*__first1, *--__i2))
3896 ::new(__j2) value_type(_VSTD::move(*__i2));
3897 __d.__incr((value_type*)0);
3898 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3899 *__j2 = _VSTD::move(*__i2);
3900 *__j2 = _VSTD::move(*__first1);
3904 ::new(__j2) value_type(_VSTD::move(*__first1));
3905 __d.__incr((value_type*)0);
3912 template <class _Compare, class _RandomAccessIterator>
3914 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3916 // _Compare is known to be a reference type
3917 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3918 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3919 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3920 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3924 difference_type __len = __last - __first;
3931 if (__comp(*--__last, *__first))
3932 swap(*__first, *__last);
3935 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3938 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3941 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3944 if (__len <= __limit)
3946 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3950 _RandomAccessIterator __m = __first;
3951 _RandomAccessIterator __lm1 = __last;
3955 difference_type __delta;
3961 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3967 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3971 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3972 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3973 _RandomAccessIterator __i = __first;
3974 _RandomAccessIterator __j = __lm1;
3975 // j points beyond range to be tested, *__m is known to be <= *__lm1
3976 // The search going up is known to be guarded but the search coming down isn't.
3977 // Prime the downward search with a guard.
3978 if (!__comp(*__i, *__m)) // if *__first == *__m
3980 // *__first == *__m, *__first doesn't go in first part
3981 // manually guard downward moving __j against __i
3986 // *__first == *__m, *__m <= all other elements
3987 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3988 ++__i; // __first + 1
3990 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3995 return; // [__first, __last) all equivalent elements
3996 if (__comp(*__first, *__i))
4006 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4011 while (!__comp(*__first, *__i))
4013 while (__comp(*__first, *--__j))
4021 // [__first, __i) == *__first and *__first < [__i, __last)
4022 // The first part is sorted, sort the secod part
4023 // _VSTD::__sort<_Compare>(__i, __last, __comp);
4027 if (__comp(*__j, *__m))
4031 break; // found guard for downward moving __j, now use unguarded partition
4035 // It is known that *__i < *__m
4037 // j points beyond range to be tested, *__m is known to be <= *__lm1
4038 // if not yet partitioned...
4041 // known that *(__i - 1) < *__m
4042 // known that __i <= __m
4045 // __m still guards upward moving __i
4046 while (__comp(*__i, *__m))
4048 // It is now known that a guard exists for downward moving __j
4049 while (!__comp(*--__j, *__m))
4055 // It is known that __m != __j
4056 // If __m just moved, follow it
4062 // [__first, __i) < *__m and *__m <= [__i, __last)
4063 if (__i != __m && __comp(*__m, *__i))
4068 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4069 // If we were given a perfect partition, see if insertion sort is quick...
4072 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4073 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4089 // sort smaller range with recursive call and larger with tail recursion elimination
4090 if (__i - __first < __last - __i)
4092 _VSTD::__sort<_Compare>(__first, __i, __comp);
4093 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4098 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4099 // _VSTD::__sort<_Compare>(__first, __i, __comp);
4105 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4106 template <class _RandomAccessIterator, class _Compare>
4107 inline _LIBCPP_INLINE_VISIBILITY
4109 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4111 #ifdef _LIBCPP_DEBUG
4112 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4113 __debug_less<_Compare> __c(__comp);
4114 __sort<_Comp_ref>(__first, __last, __c);
4115 #else // _LIBCPP_DEBUG
4116 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4117 __sort<_Comp_ref>(__first, __last, __comp);
4118 #endif // _LIBCPP_DEBUG
4121 template <class _RandomAccessIterator>
4122 inline _LIBCPP_INLINE_VISIBILITY
4124 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4126 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4129 template <class _Tp>
4130 inline _LIBCPP_INLINE_VISIBILITY
4132 sort(_Tp** __first, _Tp** __last)
4134 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4137 template <class _Tp>
4138 inline _LIBCPP_INLINE_VISIBILITY
4140 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4142 _VSTD::sort(__first.base(), __last.base());
4145 template <class _Tp, class _Compare>
4146 inline _LIBCPP_INLINE_VISIBILITY
4148 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4150 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4151 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4154 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4155 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4156 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4157 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4158 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4159 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4160 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4161 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4162 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4163 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4164 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4165 _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>&))
4166 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4167 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4168 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4170 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4171 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4172 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4173 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4174 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4175 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4176 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4177 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4178 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4179 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4180 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4181 _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>&))
4182 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4183 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4184 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4186 _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>&))
4190 template <class _Compare, class _ForwardIterator, class _Tp>
4191 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4192 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4194 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4195 difference_type __len = _VSTD::distance(__first, __last);
4198 difference_type __l2 = __len / 2;
4199 _ForwardIterator __m = __first;
4200 _VSTD::advance(__m, __l2);
4201 if (__comp(*__m, __value_))
4212 template <class _ForwardIterator, class _Tp, class _Compare>
4213 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4215 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4217 #ifdef _LIBCPP_DEBUG
4218 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4219 __debug_less<_Compare> __c(__comp);
4220 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4221 #else // _LIBCPP_DEBUG
4222 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4223 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4224 #endif // _LIBCPP_DEBUG
4227 template <class _ForwardIterator, class _Tp>
4228 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4230 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4232 return _VSTD::lower_bound(__first, __last, __value_,
4233 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4238 template <class _Compare, class _ForwardIterator, class _Tp>
4239 _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
4240 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4242 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4243 difference_type __len = _VSTD::distance(__first, __last);
4246 difference_type __l2 = __len / 2;
4247 _ForwardIterator __m = __first;
4248 _VSTD::advance(__m, __l2);
4249 if (__comp(__value_, *__m))
4260 template <class _ForwardIterator, class _Tp, class _Compare>
4261 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4263 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4265 #ifdef _LIBCPP_DEBUG
4266 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4267 __debug_less<_Compare> __c(__comp);
4268 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4269 #else // _LIBCPP_DEBUG
4270 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4271 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4272 #endif // _LIBCPP_DEBUG
4275 template <class _ForwardIterator, class _Tp>
4276 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4278 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4280 return _VSTD::upper_bound(__first, __last, __value_,
4281 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4286 template <class _Compare, class _ForwardIterator, class _Tp>
4287 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
4288 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4290 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4291 difference_type __len = _VSTD::distance(__first, __last);
4294 difference_type __l2 = __len / 2;
4295 _ForwardIterator __m = __first;
4296 _VSTD::advance(__m, __l2);
4297 if (__comp(*__m, __value_))
4302 else if (__comp(__value_, *__m))
4309 _ForwardIterator __mp1 = __m;
4310 return pair<_ForwardIterator, _ForwardIterator>
4312 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4313 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4317 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4320 template <class _ForwardIterator, class _Tp, class _Compare>
4321 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4322 pair<_ForwardIterator, _ForwardIterator>
4323 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4325 #ifdef _LIBCPP_DEBUG
4326 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4327 __debug_less<_Compare> __c(__comp);
4328 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4329 #else // _LIBCPP_DEBUG
4330 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4331 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4332 #endif // _LIBCPP_DEBUG
4335 template <class _ForwardIterator, class _Tp>
4336 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4337 pair<_ForwardIterator, _ForwardIterator>
4338 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4340 return _VSTD::equal_range(__first, __last, __value_,
4341 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4346 template <class _Compare, class _ForwardIterator, class _Tp>
4347 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4349 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4351 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4352 return __first != __last && !__comp(__value_, *__first);
4355 template <class _ForwardIterator, class _Tp, class _Compare>
4356 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4358 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4360 #ifdef _LIBCPP_DEBUG
4361 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4362 __debug_less<_Compare> __c(__comp);
4363 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4364 #else // _LIBCPP_DEBUG
4365 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4366 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4367 #endif // _LIBCPP_DEBUG
4370 template <class _ForwardIterator, class _Tp>
4371 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4373 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4375 return _VSTD::binary_search(__first, __last, __value_,
4376 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4381 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4383 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4384 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4386 for (; __first1 != __last1; ++__result)
4388 if (__first2 == __last2)
4389 return _VSTD::copy(__first1, __last1, __result);
4390 if (__comp(*__first2, *__first1))
4392 *__result = *__first2;
4397 *__result = *__first1;
4401 return _VSTD::copy(__first2, __last2, __result);
4404 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4405 inline _LIBCPP_INLINE_VISIBILITY
4407 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4408 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4410 #ifdef _LIBCPP_DEBUG
4411 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4412 __debug_less<_Compare> __c(__comp);
4413 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4414 #else // _LIBCPP_DEBUG
4415 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4416 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4417 #endif // _LIBCPP_DEBUG
4420 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4421 inline _LIBCPP_INLINE_VISIBILITY
4423 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4424 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4426 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4427 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4428 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4433 template <class _Compare, class _InputIterator1, class _InputIterator2,
4434 class _OutputIterator>
4435 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4436 _InputIterator2 __first2, _InputIterator2 __last2,
4437 _OutputIterator __result, _Compare __comp)
4439 for (; __first1 != __last1; ++__result)
4441 if (__first2 == __last2)
4443 _VSTD::move(__first1, __last1, __result);
4447 if (__comp(*__first2, *__first1))
4449 *__result = _VSTD::move(*__first2);
4454 *__result = _VSTD::move(*__first1);
4458 // __first2 through __last2 are already in the right spot.
4461 template <class _Compare, class _BidirectionalIterator>
4463 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4464 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4465 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4466 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4468 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4469 __destruct_n __d(0);
4470 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4471 if (__len1 <= __len2)
4473 value_type* __p = __buff;
4474 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4475 ::new(__p) value_type(_VSTD::move(*__i));
4476 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
4480 value_type* __p = __buff;
4481 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4482 ::new(__p) value_type(_VSTD::move(*__i));
4483 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4484 typedef reverse_iterator<value_type*> _Rv;
4485 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4486 _RBi(__middle), _RBi(__first),
4487 _RBi(__last), __invert<_Compare>(__comp));
4491 template <class _Compare, class _BidirectionalIterator>
4493 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4494 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4495 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4496 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4498 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4501 // if __middle == __last, we're done
4504 if (__len1 <= __buff_size || __len2 <= __buff_size)
4505 return __buffered_inplace_merge<_Compare>
4506 (__first, __middle, __last, __comp, __len1, __len2, __buff);
4507 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4508 for (; true; ++__first, (void) --__len1)
4512 if (__comp(*__middle, *__first))
4515 // __first < __middle < __last
4516 // *__first > *__middle
4517 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4519 // [__first, __m1) <= [__middle, __m2)
4520 // [__middle, __m2) < [__m1, __middle)
4521 // [__m1, __middle) <= [__m2, __last)
4522 // and __m1 or __m2 is in the middle of its range
4523 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4524 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4525 difference_type __len11; // distance(__first, __m1)
4526 difference_type __len21; // distance(__middle, __m2)
4527 // binary search smaller range
4528 if (__len1 < __len2)
4529 { // __len >= 1, __len2 >= 2
4530 __len21 = __len2 / 2;
4532 _VSTD::advance(__m2, __len21);
4533 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4534 __len11 = _VSTD::distance(__first, __m1);
4539 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4540 // It is known *__first > *__middle
4541 swap(*__first, *__middle);
4544 // __len1 >= 2, __len2 >= 1
4545 __len11 = __len1 / 2;
4547 _VSTD::advance(__m1, __len11);
4548 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4549 __len21 = _VSTD::distance(__middle, __m2);
4551 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4552 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4553 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4554 // swap middle two partitions
4555 __middle = _VSTD::rotate(__m1, __middle, __m2);
4556 // __len12 and __len21 now have swapped meanings
4557 // merge smaller range with recurisve call and larger with tail recursion elimination
4558 if (__len11 + __len21 < __len12 + __len22)
4560 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4561 // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4569 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4570 // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4579 template <class _BidirectionalIterator, class _Compare>
4580 inline _LIBCPP_INLINE_VISIBILITY
4582 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4585 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4586 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4587 difference_type __len1 = _VSTD::distance(__first, __middle);
4588 difference_type __len2 = _VSTD::distance(__middle, __last);
4589 difference_type __buf_size = _VSTD::min(__len1, __len2);
4590 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4591 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4593 #ifdef _LIBCPP_DEBUG
4594 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4595 __debug_less<_Compare> __c(__comp);
4596 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4597 __buf.first, __buf.second);
4598 #else // _LIBCPP_DEBUG
4599 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4600 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4601 __buf.first, __buf.second);
4602 #endif // _LIBCPP_DEBUG
4605 template <class _BidirectionalIterator>
4606 inline _LIBCPP_INLINE_VISIBILITY
4608 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4610 _VSTD::inplace_merge(__first, __middle, __last,
4611 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4616 template <class _Compare, class _InputIterator1, class _InputIterator2>
4618 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4619 _InputIterator2 __first2, _InputIterator2 __last2,
4620 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4622 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4623 __destruct_n __d(0);
4624 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4625 for (; true; ++__result)
4627 if (__first1 == __last1)
4629 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4630 ::new (__result) value_type(_VSTD::move(*__first2));
4634 if (__first2 == __last2)
4636 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4637 ::new (__result) value_type(_VSTD::move(*__first1));
4641 if (__comp(*__first2, *__first1))
4643 ::new (__result) value_type(_VSTD::move(*__first2));
4644 __d.__incr((value_type*)0);
4649 ::new (__result) value_type(_VSTD::move(*__first1));
4650 __d.__incr((value_type*)0);
4656 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4658 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4659 _InputIterator2 __first2, _InputIterator2 __last2,
4660 _OutputIterator __result, _Compare __comp)
4662 for (; __first1 != __last1; ++__result)
4664 if (__first2 == __last2)
4666 for (; __first1 != __last1; ++__first1, ++__result)
4667 *__result = _VSTD::move(*__first1);
4670 if (__comp(*__first2, *__first1))
4672 *__result = _VSTD::move(*__first2);
4677 *__result = _VSTD::move(*__first1);
4681 for (; __first2 != __last2; ++__first2, ++__result)
4682 *__result = _VSTD::move(*__first2);
4685 template <class _Compare, class _RandomAccessIterator>
4687 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4688 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4689 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4691 template <class _Compare, class _RandomAccessIterator>
4693 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4694 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4695 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4697 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4703 ::new(__first2) value_type(_VSTD::move(*__first1));
4706 __destruct_n __d(0);
4707 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4708 if (__comp(*--__last1, *__first1))
4710 ::new(__first2) value_type(_VSTD::move(*__last1));
4711 __d.__incr((value_type*)0);
4713 ::new(__first2) value_type(_VSTD::move(*__first1));
4717 ::new(__first2) value_type(_VSTD::move(*__first1));
4718 __d.__incr((value_type*)0);
4720 ::new(__first2) value_type(_VSTD::move(*__last1));
4727 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4730 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4731 _RandomAccessIterator __m = __first1 + __l2;
4732 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4733 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4734 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4737 template <class _Tp>
4738 struct __stable_sort_switch
4740 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4743 template <class _Compare, class _RandomAccessIterator>
4745 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4746 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4747 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4749 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4750 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4757 if (__comp(*--__last, *__first))
4758 swap(*__first, *__last);
4761 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4763 __insertion_sort<_Compare>(__first, __last, __comp);
4766 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4767 _RandomAccessIterator __m = __first + __l2;
4768 if (__len <= __buff_size)
4770 __destruct_n __d(0);
4771 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4772 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4773 __d.__set(__l2, (value_type*)0);
4774 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4775 __d.__set(__len, (value_type*)0);
4776 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4777 // __merge<_Compare>(move_iterator<value_type*>(__buff),
4778 // move_iterator<value_type*>(__buff + __l2),
4779 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4780 // move_iterator<_RandomAccessIterator>(__buff + __len),
4781 // __first, __comp);
4784 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4785 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4786 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4789 template <class _RandomAccessIterator, class _Compare>
4790 inline _LIBCPP_INLINE_VISIBILITY
4792 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4794 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4795 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4796 difference_type __len = __last - __first;
4797 pair<value_type*, ptrdiff_t> __buf(0, 0);
4798 unique_ptr<value_type, __return_temporary_buffer> __h;
4799 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4801 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4802 __h.reset(__buf.first);
4804 #ifdef _LIBCPP_DEBUG
4805 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4806 __debug_less<_Compare> __c(__comp);
4807 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4808 #else // _LIBCPP_DEBUG
4809 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4810 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4811 #endif // _LIBCPP_DEBUG
4814 template <class _RandomAccessIterator>
4815 inline _LIBCPP_INLINE_VISIBILITY
4817 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4819 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4824 template <class _RandomAccessIterator, class _Compare>
4825 _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
4826 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4828 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4829 difference_type __len = __last - __first;
4830 difference_type __p = 0;
4831 difference_type __c = 1;
4832 _RandomAccessIterator __pp = __first;
4835 _RandomAccessIterator __cp = __first + __c;
4836 if (__comp(*__pp, *__cp))
4842 if (__comp(*__pp, *__cp))
4851 template<class _RandomAccessIterator>
4852 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4853 _RandomAccessIterator
4854 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4856 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4861 template <class _RandomAccessIterator, class _Compare>
4862 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4864 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4866 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4869 template<class _RandomAccessIterator>
4870 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
4872 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4874 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4879 template <class _Compare, class _RandomAccessIterator>
4881 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4882 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4884 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4887 __len = (__len - 2) / 2;
4888 _RandomAccessIterator __ptr = __first + __len;
4889 if (__comp(*__ptr, *--__last))
4891 value_type __t(_VSTD::move(*__last));
4894 *__last = _VSTD::move(*__ptr);
4898 __len = (__len - 1) / 2;
4899 __ptr = __first + __len;
4900 } while (__comp(*__ptr, __t));
4901 *__last = _VSTD::move(__t);
4906 template <class _RandomAccessIterator, class _Compare>
4907 inline _LIBCPP_INLINE_VISIBILITY
4909 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4911 #ifdef _LIBCPP_DEBUG
4912 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4913 __debug_less<_Compare> __c(__comp);
4914 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
4915 #else // _LIBCPP_DEBUG
4916 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4917 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
4918 #endif // _LIBCPP_DEBUG
4921 template <class _RandomAccessIterator>
4922 inline _LIBCPP_INLINE_VISIBILITY
4924 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4926 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4931 template <class _Compare, class _RandomAccessIterator>
4933 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4935 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4936 _RandomAccessIterator __start)
4938 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4939 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4940 // left-child of __start is at 2 * __start + 1
4941 // right-child of __start is at 2 * __start + 2
4942 difference_type __child = __start - __first;
4944 if (__len < 2 || (__len - 2) / 2 < __child)
4947 __child = 2 * __child + 1;
4948 _RandomAccessIterator __child_i = __first + __child;
4950 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4951 // right-child exists and is greater than left-child
4956 // check if we are in heap-order
4957 if (__comp(*__child_i, *__start))
4958 // we are, __start is larger than it's largest child
4961 value_type __top(_VSTD::move(*__start));
4964 // we are not in heap-order, swap the parent with it's largest child
4965 *__start = _VSTD::move(*__child_i);
4966 __start = __child_i;
4968 if ((__len - 2) / 2 < __child)
4971 // recompute the child based off of the updated parent
4972 __child = 2 * __child + 1;
4973 __child_i = __first + __child;
4975 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4976 // right-child exists and is greater than left-child
4981 // check if we are in heap-order
4982 } while (!__comp(*__child_i, __top));
4983 *__start = _VSTD::move(__top);
4986 template <class _Compare, class _RandomAccessIterator>
4987 inline _LIBCPP_INLINE_VISIBILITY
4989 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4990 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4994 swap(*__first, *--__last);
4995 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
4999 template <class _RandomAccessIterator, class _Compare>
5000 inline _LIBCPP_INLINE_VISIBILITY
5002 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5004 #ifdef _LIBCPP_DEBUG
5005 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5006 __debug_less<_Compare> __c(__comp);
5007 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
5008 #else // _LIBCPP_DEBUG
5009 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5010 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
5011 #endif // _LIBCPP_DEBUG
5014 template <class _RandomAccessIterator>
5015 inline _LIBCPP_INLINE_VISIBILITY
5017 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5019 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5024 template <class _Compare, class _RandomAccessIterator>
5026 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5028 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5029 difference_type __n = __last - __first;
5032 // start from the first parent, there is no need to consider children
5033 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5035 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5040 template <class _RandomAccessIterator, class _Compare>
5041 inline _LIBCPP_INLINE_VISIBILITY
5043 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5045 #ifdef _LIBCPP_DEBUG
5046 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5047 __debug_less<_Compare> __c(__comp);
5048 __make_heap<_Comp_ref>(__first, __last, __c);
5049 #else // _LIBCPP_DEBUG
5050 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5051 __make_heap<_Comp_ref>(__first, __last, __comp);
5052 #endif // _LIBCPP_DEBUG
5055 template <class _RandomAccessIterator>
5056 inline _LIBCPP_INLINE_VISIBILITY
5058 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5060 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5065 template <class _Compare, class _RandomAccessIterator>
5067 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5069 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5070 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5071 __pop_heap<_Compare>(__first, __last, __comp, __n);
5074 template <class _RandomAccessIterator, class _Compare>
5075 inline _LIBCPP_INLINE_VISIBILITY
5077 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5079 #ifdef _LIBCPP_DEBUG
5080 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5081 __debug_less<_Compare> __c(__comp);
5082 __sort_heap<_Comp_ref>(__first, __last, __c);
5083 #else // _LIBCPP_DEBUG
5084 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5085 __sort_heap<_Comp_ref>(__first, __last, __comp);
5086 #endif // _LIBCPP_DEBUG
5089 template <class _RandomAccessIterator>
5090 inline _LIBCPP_INLINE_VISIBILITY
5092 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5094 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5099 template <class _Compare, class _RandomAccessIterator>
5101 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5104 __make_heap<_Compare>(__first, __middle, __comp);
5105 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5106 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5108 if (__comp(*__i, *__first))
5110 swap(*__i, *__first);
5111 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5114 __sort_heap<_Compare>(__first, __middle, __comp);
5117 template <class _RandomAccessIterator, class _Compare>
5118 inline _LIBCPP_INLINE_VISIBILITY
5120 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5123 #ifdef _LIBCPP_DEBUG
5124 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5125 __debug_less<_Compare> __c(__comp);
5126 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
5127 #else // _LIBCPP_DEBUG
5128 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5129 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5130 #endif // _LIBCPP_DEBUG
5133 template <class _RandomAccessIterator>
5134 inline _LIBCPP_INLINE_VISIBILITY
5136 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5138 _VSTD::partial_sort(__first, __middle, __last,
5139 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5142 // partial_sort_copy
5144 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5145 _RandomAccessIterator
5146 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5147 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5149 _RandomAccessIterator __r = __result_first;
5150 if (__r != __result_last)
5152 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5154 __make_heap<_Compare>(__result_first, __r, __comp);
5155 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5156 for (; __first != __last; ++__first)
5157 if (__comp(*__first, *__result_first))
5159 *__result_first = *__first;
5160 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5162 __sort_heap<_Compare>(__result_first, __r, __comp);
5167 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5168 inline _LIBCPP_INLINE_VISIBILITY
5169 _RandomAccessIterator
5170 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5171 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5173 #ifdef _LIBCPP_DEBUG
5174 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5175 __debug_less<_Compare> __c(__comp);
5176 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
5177 #else // _LIBCPP_DEBUG
5178 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5179 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5180 #endif // _LIBCPP_DEBUG
5183 template <class _InputIterator, class _RandomAccessIterator>
5184 inline _LIBCPP_INLINE_VISIBILITY
5185 _RandomAccessIterator
5186 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5187 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5189 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5190 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5195 template <class _Compare, class _RandomAccessIterator>
5197 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5199 // _Compare is known to be a reference type
5200 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5201 const difference_type __limit = 7;
5205 if (__nth == __last)
5207 difference_type __len = __last - __first;
5214 if (__comp(*--__last, *__first))
5215 swap(*__first, *__last);
5219 _RandomAccessIterator __m = __first;
5220 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5224 if (__len <= __limit)
5226 __selection_sort<_Compare>(__first, __last, __comp);
5229 // __len > __limit >= 3
5230 _RandomAccessIterator __m = __first + __len/2;
5231 _RandomAccessIterator __lm1 = __last;
5232 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5234 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5235 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5236 _RandomAccessIterator __i = __first;
5237 _RandomAccessIterator __j = __lm1;
5238 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5239 // The search going up is known to be guarded but the search coming down isn't.
5240 // Prime the downward search with a guard.
5241 if (!__comp(*__i, *__m)) // if *__first == *__m
5243 // *__first == *__m, *__first doesn't go in first part
5244 // manually guard downward moving __j against __i
5249 // *__first == *__m, *__m <= all other elements
5250 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5251 ++__i; // __first + 1
5253 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5258 return; // [__first, __last) all equivalent elements
5259 if (__comp(*__first, *__i))
5269 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5274 while (!__comp(*__first, *__i))
5276 while (__comp(*__first, *--__j))
5284 // [__first, __i) == *__first and *__first < [__i, __last)
5285 // The first part is sorted,
5288 // __nth_element the secod part
5289 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5293 if (__comp(*__j, *__m))
5297 break; // found guard for downward moving __j, now use unguarded partition
5302 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5303 // if not yet partitioned...
5306 // known that *(__i - 1) < *__m
5309 // __m still guards upward moving __i
5310 while (__comp(*__i, *__m))
5312 // It is now known that a guard exists for downward moving __j
5313 while (!__comp(*--__j, *__m))
5319 // It is known that __m != __j
5320 // If __m just moved, follow it
5326 // [__first, __i) < *__m and *__m <= [__i, __last)
5327 if (__i != __m && __comp(*__m, *__i))
5332 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5337 // We were given a perfectly partitioned sequence. Coincidence?
5340 // Check for [__first, __i) already sorted
5341 __j = __m = __first;
5342 while (++__j != __i)
5344 if (__comp(*__j, *__m))
5345 // not yet sorted, so sort
5349 // [__first, __i) sorted
5354 // Check for [__i, __last) already sorted
5356 while (++__j != __last)
5358 if (__comp(*__j, *__m))
5359 // not yet sorted, so sort
5363 // [__i, __last) sorted
5368 // __nth_element on range containing __nth
5371 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5376 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5382 template <class _RandomAccessIterator, class _Compare>
5383 inline _LIBCPP_INLINE_VISIBILITY
5385 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5387 #ifdef _LIBCPP_DEBUG
5388 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5389 __debug_less<_Compare> __c(__comp);
5390 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5391 #else // _LIBCPP_DEBUG
5392 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5393 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5394 #endif // _LIBCPP_DEBUG
5397 template <class _RandomAccessIterator>
5398 inline _LIBCPP_INLINE_VISIBILITY
5400 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5402 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5407 template <class _Compare, class _InputIterator1, class _InputIterator2>
5408 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5409 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5412 for (; __first2 != __last2; ++__first1)
5414 if (__first1 == __last1 || __comp(*__first2, *__first1))
5416 if (!__comp(*__first1, *__first2))
5422 template <class _InputIterator1, class _InputIterator2, class _Compare>
5423 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5425 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5428 #ifdef _LIBCPP_DEBUG
5429 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5430 __debug_less<_Compare> __c(__comp);
5431 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5432 #else // _LIBCPP_DEBUG
5433 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5434 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5435 #endif // _LIBCPP_DEBUG
5438 template <class _InputIterator1, class _InputIterator2>
5439 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5441 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5443 return _VSTD::includes(__first1, __last1, __first2, __last2,
5444 __less<typename iterator_traits<_InputIterator1>::value_type,
5445 typename iterator_traits<_InputIterator2>::value_type>());
5450 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5452 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5453 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5455 for (; __first1 != __last1; ++__result)
5457 if (__first2 == __last2)
5458 return _VSTD::copy(__first1, __last1, __result);
5459 if (__comp(*__first2, *__first1))
5461 *__result = *__first2;
5466 if (!__comp(*__first1, *__first2))
5468 *__result = *__first1;
5472 return _VSTD::copy(__first2, __last2, __result);
5475 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5476 inline _LIBCPP_INLINE_VISIBILITY
5478 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5479 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5481 #ifdef _LIBCPP_DEBUG
5482 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5483 __debug_less<_Compare> __c(__comp);
5484 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5485 #else // _LIBCPP_DEBUG
5486 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5487 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5488 #endif // _LIBCPP_DEBUG
5491 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5492 inline _LIBCPP_INLINE_VISIBILITY
5494 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5495 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5497 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5498 __less<typename iterator_traits<_InputIterator1>::value_type,
5499 typename iterator_traits<_InputIterator2>::value_type>());
5504 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5505 _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
5506 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5507 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5509 while (__first1 != __last1 && __first2 != __last2)
5511 if (__comp(*__first1, *__first2))
5515 if (!__comp(*__first2, *__first1))
5517 *__result = *__first1;
5527 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5528 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5530 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5531 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5533 #ifdef _LIBCPP_DEBUG
5534 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5535 __debug_less<_Compare> __c(__comp);
5536 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5537 #else // _LIBCPP_DEBUG
5538 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5539 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5540 #endif // _LIBCPP_DEBUG
5543 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5544 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5546 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5547 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5549 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5550 __less<typename iterator_traits<_InputIterator1>::value_type,
5551 typename iterator_traits<_InputIterator2>::value_type>());
5556 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5558 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5559 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5561 while (__first1 != __last1)
5563 if (__first2 == __last2)
5564 return _VSTD::copy(__first1, __last1, __result);
5565 if (__comp(*__first1, *__first2))
5567 *__result = *__first1;
5573 if (!__comp(*__first2, *__first1))
5581 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5582 inline _LIBCPP_INLINE_VISIBILITY
5584 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5585 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5587 #ifdef _LIBCPP_DEBUG
5588 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5589 __debug_less<_Compare> __c(__comp);
5590 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5591 #else // _LIBCPP_DEBUG
5592 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5593 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5594 #endif // _LIBCPP_DEBUG
5597 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5598 inline _LIBCPP_INLINE_VISIBILITY
5600 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5601 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5603 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5604 __less<typename iterator_traits<_InputIterator1>::value_type,
5605 typename iterator_traits<_InputIterator2>::value_type>());
5608 // set_symmetric_difference
5610 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5612 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5613 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5615 while (__first1 != __last1)
5617 if (__first2 == __last2)
5618 return _VSTD::copy(__first1, __last1, __result);
5619 if (__comp(*__first1, *__first2))
5621 *__result = *__first1;
5627 if (__comp(*__first2, *__first1))
5629 *__result = *__first2;
5637 return _VSTD::copy(__first2, __last2, __result);
5640 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5641 inline _LIBCPP_INLINE_VISIBILITY
5643 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5644 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5646 #ifdef _LIBCPP_DEBUG
5647 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5648 __debug_less<_Compare> __c(__comp);
5649 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5650 #else // _LIBCPP_DEBUG
5651 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5652 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5653 #endif // _LIBCPP_DEBUG
5656 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5657 inline _LIBCPP_INLINE_VISIBILITY
5659 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5660 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5662 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5663 __less<typename iterator_traits<_InputIterator1>::value_type,
5664 typename iterator_traits<_InputIterator2>::value_type>());
5667 // lexicographical_compare
5669 template <class _Compare, class _InputIterator1, class _InputIterator2>
5670 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
5671 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5672 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5674 for (; __first2 != __last2; ++__first1, (void) ++__first2)
5676 if (__first1 == __last1 || __comp(*__first1, *__first2))
5678 if (__comp(*__first2, *__first1))
5684 template <class _InputIterator1, class _InputIterator2, class _Compare>
5685 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5687 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5688 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5690 #ifdef _LIBCPP_DEBUG
5691 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5692 __debug_less<_Compare> __c(__comp);
5693 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5694 #else // _LIBCPP_DEBUG
5695 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5696 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5697 #endif // _LIBCPP_DEBUG
5700 template <class _InputIterator1, class _InputIterator2>
5701 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5703 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5704 _InputIterator2 __first2, _InputIterator2 __last2)
5706 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5707 __less<typename iterator_traits<_InputIterator1>::value_type,
5708 typename iterator_traits<_InputIterator2>::value_type>());
5713 template <class _Compare, class _BidirectionalIterator>
5715 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5717 _BidirectionalIterator __i = __last;
5718 if (__first == __last || __first == --__i)
5722 _BidirectionalIterator __ip1 = __i;
5723 if (__comp(*--__i, *__ip1))
5725 _BidirectionalIterator __j = __last;
5726 while (!__comp(*__i, *--__j))
5729 _VSTD::reverse(__ip1, __last);
5734 _VSTD::reverse(__first, __last);
5740 template <class _BidirectionalIterator, class _Compare>
5741 inline _LIBCPP_INLINE_VISIBILITY
5743 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5745 #ifdef _LIBCPP_DEBUG
5746 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5747 __debug_less<_Compare> __c(__comp);
5748 return __next_permutation<_Comp_ref>(__first, __last, __c);
5749 #else // _LIBCPP_DEBUG
5750 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5751 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5752 #endif // _LIBCPP_DEBUG
5755 template <class _BidirectionalIterator>
5756 inline _LIBCPP_INLINE_VISIBILITY
5758 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5760 return _VSTD::next_permutation(__first, __last,
5761 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5766 template <class _Compare, class _BidirectionalIterator>
5768 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5770 _BidirectionalIterator __i = __last;
5771 if (__first == __last || __first == --__i)
5775 _BidirectionalIterator __ip1 = __i;
5776 if (__comp(*__ip1, *--__i))
5778 _BidirectionalIterator __j = __last;
5779 while (!__comp(*--__j, *__i))
5782 _VSTD::reverse(__ip1, __last);
5787 _VSTD::reverse(__first, __last);
5793 template <class _BidirectionalIterator, class _Compare>
5794 inline _LIBCPP_INLINE_VISIBILITY
5796 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5798 #ifdef _LIBCPP_DEBUG
5799 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5800 __debug_less<_Compare> __c(__comp);
5801 return __prev_permutation<_Comp_ref>(__first, __last, __c);
5802 #else // _LIBCPP_DEBUG
5803 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5804 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5805 #endif // _LIBCPP_DEBUG
5808 template <class _BidirectionalIterator>
5809 inline _LIBCPP_INLINE_VISIBILITY
5811 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5813 return _VSTD::prev_permutation(__first, __last,
5814 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5817 _LIBCPP_END_NAMESPACE_STD
5821 #endif // _LIBCPP_ALGORITHM