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>
24 all_of(InputIterator first, InputIterator last, Predicate pred);
26 template <class InputIterator, class Predicate>
28 any_of(InputIterator first, InputIterator last, Predicate pred);
30 template <class InputIterator, class Predicate>
32 none_of(InputIterator first, InputIterator last, Predicate pred);
34 template <class InputIterator, class Function>
36 for_each(InputIterator first, InputIterator last, Function f);
38 template<class InputIterator, class Size, class Function>
39 InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17
41 template <class InputIterator, class T>
43 find(InputIterator first, InputIterator last, const T& value);
45 template <class InputIterator, class Predicate>
47 find_if(InputIterator first, InputIterator last, Predicate pred);
49 template<class InputIterator, class Predicate>
51 find_if_not(InputIterator first, InputIterator last, Predicate pred);
53 template <class ForwardIterator1, class ForwardIterator2>
55 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
58 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
60 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
63 template <class ForwardIterator1, class ForwardIterator2>
65 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
68 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
70 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
73 template <class ForwardIterator>
75 adjacent_find(ForwardIterator first, ForwardIterator last);
77 template <class ForwardIterator, class BinaryPredicate>
79 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
81 template <class InputIterator, class T>
82 typename iterator_traits<InputIterator>::difference_type
83 count(InputIterator first, InputIterator last, const T& value);
85 template <class InputIterator, class Predicate>
86 typename iterator_traits<InputIterator>::difference_type
87 count_if(InputIterator first, InputIterator last, Predicate pred);
89 template <class InputIterator1, class InputIterator2>
90 pair<InputIterator1, InputIterator2>
91 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
93 template <class InputIterator1, class InputIterator2>
94 pair<InputIterator1, InputIterator2>
95 mismatch(InputIterator1 first1, InputIterator1 last1,
96 InputIterator2 first2, InputIterator2 last2); // **C++14**
98 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
99 pair<InputIterator1, InputIterator2>
100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
103 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
104 pair<InputIterator1, InputIterator2>
105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
109 template <class InputIterator1, class InputIterator2>
111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
113 template <class InputIterator1, class InputIterator2>
115 equal(InputIterator1 first1, InputIterator1 last1,
116 InputIterator2 first2, InputIterator2 last2); // **C++14**
118 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
123 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
129 template<class ForwardIterator1, class ForwardIterator2>
131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
134 template<class ForwardIterator1, class ForwardIterator2>
136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
139 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
144 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
150 template <class ForwardIterator1, class ForwardIterator2>
152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
155 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
160 template <class ForwardIterator, class Size, class T>
162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
164 template <class ForwardIterator, class Size, class T, class BinaryPredicate>
166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
169 template <class InputIterator, class OutputIterator>
171 copy(InputIterator first, InputIterator last, OutputIterator result);
173 template<class InputIterator, class OutputIterator, class Predicate>
175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
178 template<class InputIterator, class Size, class OutputIterator>
180 copy_n(InputIterator first, Size n, OutputIterator result);
182 template <class BidirectionalIterator1, class BidirectionalIterator2>
183 BidirectionalIterator2
184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
187 template <class ForwardIterator1, class ForwardIterator2>
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
191 template <class ForwardIterator1, class ForwardIterator2>
193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
195 template <class InputIterator, class OutputIterator, class UnaryOperation>
197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
199 template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
204 template <class ForwardIterator, class T>
206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
208 template <class ForwardIterator, class Predicate, class T>
210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
212 template <class InputIterator, class OutputIterator, class T>
214 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 const T& old_value, const T& new_value);
217 template <class InputIterator, class OutputIterator, class Predicate, class T>
219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
221 template <class ForwardIterator, class T>
223 fill(ForwardIterator first, ForwardIterator last, const T& value);
225 template <class OutputIterator, class Size, class T>
227 fill_n(OutputIterator first, Size n, const T& value);
229 template <class ForwardIterator, class Generator>
231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
233 template <class OutputIterator, class Size, class Generator>
235 generate_n(OutputIterator first, Size n, Generator gen);
237 template <class ForwardIterator, class T>
239 remove(ForwardIterator first, ForwardIterator last, const T& value);
241 template <class ForwardIterator, class Predicate>
243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
245 template <class InputIterator, class OutputIterator, class T>
247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
249 template <class InputIterator, class OutputIterator, class Predicate>
251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
253 template <class ForwardIterator>
255 unique(ForwardIterator first, ForwardIterator last);
257 template <class ForwardIterator, class BinaryPredicate>
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
261 template <class InputIterator, class OutputIterator>
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
265 template <class InputIterator, class OutputIterator, class BinaryPredicate>
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
269 template <class BidirectionalIterator>
271 reverse(BidirectionalIterator first, BidirectionalIterator last);
273 template <class BidirectionalIterator, class OutputIterator>
275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
277 template <class ForwardIterator>
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
281 template <class ForwardIterator, class OutputIterator>
283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
285 template <class RandomAccessIterator>
287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
289 template <class RandomAccessIterator, class RandomNumberGenerator>
291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
294 template<class PopulationIterator, class SampleIterator,
295 class Distance, class UniformRandomBitGenerator>
296 SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 SampleIterator out, Distance n,
298 UniformRandomBitGenerator&& g); // C++17
300 template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
302 UniformRandomNumberGenerator&& g);
304 template <class InputIterator, class Predicate>
306 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
308 template <class ForwardIterator, class Predicate>
310 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
312 template <class InputIterator, class OutputIterator1,
313 class OutputIterator2, class Predicate>
314 pair<OutputIterator1, OutputIterator2>
315 partition_copy(InputIterator first, InputIterator last,
316 OutputIterator1 out_true, OutputIterator2 out_false,
319 template <class ForwardIterator, class Predicate>
321 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
323 template<class ForwardIterator, class Predicate>
325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
327 template <class ForwardIterator>
329 is_sorted(ForwardIterator first, ForwardIterator last);
331 template <class ForwardIterator, class Compare>
333 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
335 template<class ForwardIterator>
337 is_sorted_until(ForwardIterator first, ForwardIterator last);
339 template <class ForwardIterator, class Compare>
341 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
343 template <class RandomAccessIterator>
345 sort(RandomAccessIterator first, RandomAccessIterator last);
347 template <class RandomAccessIterator, class Compare>
349 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
351 template <class RandomAccessIterator>
353 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
355 template <class RandomAccessIterator, class Compare>
357 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
359 template <class RandomAccessIterator>
361 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
363 template <class RandomAccessIterator, class Compare>
365 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
367 template <class InputIterator, class RandomAccessIterator>
369 partial_sort_copy(InputIterator first, InputIterator last,
370 RandomAccessIterator result_first, RandomAccessIterator result_last);
372 template <class InputIterator, class RandomAccessIterator, class Compare>
374 partial_sort_copy(InputIterator first, InputIterator last,
375 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
377 template <class RandomAccessIterator>
379 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
381 template <class RandomAccessIterator, class Compare>
383 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
385 template <class ForwardIterator, class T>
387 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
389 template <class ForwardIterator, class T, class Compare>
391 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
393 template <class ForwardIterator, class T>
395 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
397 template <class ForwardIterator, class T, class Compare>
399 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
401 template <class ForwardIterator, class T>
402 pair<ForwardIterator, ForwardIterator>
403 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
405 template <class ForwardIterator, class T, class Compare>
406 pair<ForwardIterator, ForwardIterator>
407 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
409 template <class ForwardIterator, class T>
411 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
413 template <class ForwardIterator, class T, class Compare>
415 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
417 template <class InputIterator1, class InputIterator2, class OutputIterator>
419 merge(InputIterator1 first1, InputIterator1 last1,
420 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
422 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
424 merge(InputIterator1 first1, InputIterator1 last1,
425 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
427 template <class BidirectionalIterator>
429 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
431 template <class BidirectionalIterator, class Compare>
433 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
435 template <class InputIterator1, class InputIterator2>
437 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
439 template <class InputIterator1, class InputIterator2, class Compare>
441 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
443 template <class InputIterator1, class InputIterator2, class OutputIterator>
445 set_union(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 set_union(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453 template <class InputIterator1, class InputIterator2, class OutputIterator>
455 set_intersection(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460 set_intersection(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463 template <class InputIterator1, class InputIterator2, class OutputIterator>
465 set_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 set_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473 template <class InputIterator1, class InputIterator2, class OutputIterator>
475 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
478 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
480 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
483 template <class RandomAccessIterator>
485 push_heap(RandomAccessIterator first, RandomAccessIterator last);
487 template <class RandomAccessIterator, class Compare>
489 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
491 template <class RandomAccessIterator>
493 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
495 template <class RandomAccessIterator, class Compare>
497 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
499 template <class RandomAccessIterator>
501 make_heap(RandomAccessIterator first, RandomAccessIterator last);
503 template <class RandomAccessIterator, class Compare>
505 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
507 template <class RandomAccessIterator>
509 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
511 template <class RandomAccessIterator, class Compare>
513 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
515 template <class RandomAccessIterator>
517 is_heap(RandomAccessIterator first, RandomAccessiterator last);
519 template <class RandomAccessIterator, class Compare>
521 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
523 template <class RandomAccessIterator>
525 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
527 template <class RandomAccessIterator, class Compare>
529 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
531 template <class ForwardIterator>
533 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
535 template <class ForwardIterator, class Compare>
537 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
541 min(const T& a, const T& b); // constexpr in C++14
543 template <class T, class Compare>
545 min(const T& a, const T& b, Compare comp); // constexpr in C++14
549 min(initializer_list<T> t); // constexpr in C++14
551 template<class T, class Compare>
553 min(initializer_list<T> t, Compare comp); // constexpr in C++14
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
558 template<class T, class Compare>
559 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
561 template <class ForwardIterator>
563 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
565 template <class ForwardIterator, class Compare>
567 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
571 max(const T& a, const T& b); // constexpr in C++14
573 template <class T, class Compare>
575 max(const T& a, const T& b, Compare comp); // constexpr in C++14
579 max(initializer_list<T> t); // constexpr in C++14
581 template<class T, class Compare>
583 max(initializer_list<T> t, Compare comp); // constexpr in C++14
585 template<class ForwardIterator>
586 pair<ForwardIterator, ForwardIterator>
587 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
589 template<class ForwardIterator, class Compare>
590 pair<ForwardIterator, ForwardIterator>
591 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
594 pair<const T&, const T&>
595 minmax(const T& a, const T& b); // constexpr in C++14
597 template<class T, class Compare>
598 pair<const T&, const T&>
599 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
603 minmax(initializer_list<T> t); // constexpr in C++14
605 template<class T, class Compare>
607 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
609 template <class InputIterator1, class InputIterator2>
611 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
613 template <class InputIterator1, class InputIterator2, class Compare>
615 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
616 InputIterator2 first2, InputIterator2 last2, Compare comp);
618 template <class BidirectionalIterator>
620 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
622 template <class BidirectionalIterator, class Compare>
624 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
626 template <class BidirectionalIterator>
628 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
630 template <class BidirectionalIterator, class Compare>
632 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
639 #include <initializer_list>
640 #include <type_traits>
642 #include <utility> // needed to provide swap_ranges.
647 #if defined(__IBMCPP__)
648 #include "support/ibm/support.h"
650 #if defined(_LIBCPP_COMPILER_MSVC)
656 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
657 #pragma GCC system_header
661 #include <__undef_macros>
664 _LIBCPP_BEGIN_NAMESPACE_STD
666 // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
667 // * That only works with C++14 and later, and
668 // * We haven't included <functional> here.
669 template <class _T1, class _T2 = _T1>
672 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
673 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
674 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
675 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
679 struct __equal_to<_T1, _T1>
681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
682 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
686 struct __equal_to<const _T1, _T1>
688 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
693 struct __equal_to<_T1, const _T1>
695 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
696 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
699 template <class _T1, class _T2 = _T1>
702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
703 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
705 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
706 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
711 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
712 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
716 struct __less<_T1, _T1>
718 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
719 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
723 struct __less<const _T1, _T1>
725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
726 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
730 struct __less<_T1, const _T1>
732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
733 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
736 template <class _Predicate>
737 class __invert // invert the sense of a comparison
742 _LIBCPP_INLINE_VISIBILITY __invert() {}
744 _LIBCPP_INLINE_VISIBILITY
745 explicit __invert(_Predicate __p) : __p_(__p) {}
748 _LIBCPP_INLINE_VISIBILITY
749 bool operator()(const _T1& __x) {return !__p_(__x);}
751 template <class _T1, class _T2>
752 _LIBCPP_INLINE_VISIBILITY
753 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
758 template <class _Compare>
762 __debug_less(_Compare& __c) : __comp_(__c) {}
764 template <class _Tp, class _Up>
765 bool operator()(const _Tp& __x, const _Up& __y)
767 bool __r = __comp_(__x, __y);
769 __do_compare_assert(0, __y, __x);
773 template <class _LHS, class _RHS>
774 inline _LIBCPP_INLINE_VISIBILITY
775 decltype((void)_VSTD::declval<_Compare&>()(
776 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
777 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
778 _LIBCPP_ASSERT(!__comp_(__l, __r),
779 "Comparator does not induce a strict weak ordering");
782 template <class _LHS, class _RHS>
783 inline _LIBCPP_INLINE_VISIBILITY
784 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
787 #endif // _LIBCPP_DEBUG
789 // Precondition: __x != 0
790 inline _LIBCPP_INLINE_VISIBILITY
791 unsigned __ctz(unsigned __x) {
792 #ifndef _LIBCPP_COMPILER_MSVC
793 return static_cast<unsigned>(__builtin_ctz(__x));
795 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
796 static_assert(sizeof(unsigned long) == 4, "");
798 // Search from LSB to MSB for first set bit.
799 // Returns zero if no set bit is found.
800 if (_BitScanForward(&where, __x))
806 inline _LIBCPP_INLINE_VISIBILITY
807 unsigned long __ctz(unsigned long __x) {
808 #ifndef _LIBCPP_COMPILER_MSVC
809 return static_cast<unsigned long>(__builtin_ctzl(__x));
811 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
812 return __ctz(static_cast<unsigned>(__x));
816 inline _LIBCPP_INLINE_VISIBILITY
817 unsigned long long __ctz(unsigned long long __x) {
818 #ifndef _LIBCPP_COMPILER_MSVC
819 return static_cast<unsigned long long>(__builtin_ctzll(__x));
822 // Search from LSB to MSB for first set bit.
823 // Returns zero if no set bit is found.
824 #if defined(_LIBCPP_HAS_BITSCAN64)
825 (defined(_M_AMD64) || defined(__x86_64__))
826 if (_BitScanForward64(&where, __x))
827 return static_cast<int>(where);
829 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
830 // Scan the Low Word.
831 if (_BitScanForward(&where, static_cast<unsigned long>(__x)))
833 // Scan the High Word.
834 if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
835 return where + 32; // Create a bit offset from the LSB.
838 #endif // _LIBCPP_COMPILER_MSVC
841 // Precondition: __x != 0
842 inline _LIBCPP_INLINE_VISIBILITY
843 unsigned __clz(unsigned __x) {
844 #ifndef _LIBCPP_COMPILER_MSVC
845 return static_cast<unsigned>(__builtin_clz(__x));
847 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
848 static_assert(sizeof(unsigned long) == 4, "");
850 // Search from LSB to MSB for first set bit.
851 // Returns zero if no set bit is found.
852 if (_BitScanReverse(&where, __x))
854 return 32; // Undefined Behavior.
858 inline _LIBCPP_INLINE_VISIBILITY
859 unsigned long __clz(unsigned long __x) {
860 #ifndef _LIBCPP_COMPILER_MSVC
861 return static_cast<unsigned long>(__builtin_clzl (__x));
863 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
864 return __clz(static_cast<unsigned>(__x));
868 inline _LIBCPP_INLINE_VISIBILITY
869 unsigned long long __clz(unsigned long long __x) {
870 #ifndef _LIBCPP_COMPILER_MSVC
871 return static_cast<unsigned long long>(__builtin_clzll(__x));
874 // BitScanReverse scans from MSB to LSB for first set bit.
875 // Returns 0 if no set bit is found.
876 #if defined(_LIBCPP_HAS_BITSCAN64)
877 if (_BitScanReverse64(&where, __x))
878 return static_cast<int>(63 - where);
880 // Scan the high 32 bits.
881 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
882 return 63 - (where + 32); // Create a bit offset from the MSB.
883 // Scan the low 32 bits.
884 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
887 return 64; // Undefined Behavior.
888 #endif // _LIBCPP_COMPILER_MSVC
891 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
892 #ifndef _LIBCPP_COMPILER_MSVC
893 return __builtin_popcount (__x);
895 static_assert(sizeof(unsigned) == 4, "");
896 return __popcnt(__x);
900 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
901 #ifndef _LIBCPP_COMPILER_MSVC
902 return __builtin_popcountl (__x);
904 static_assert(sizeof(unsigned long) == 4, "");
905 return __popcnt(__x);
909 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
910 #ifndef _LIBCPP_COMPILER_MSVC
911 return __builtin_popcountll(__x);
913 static_assert(sizeof(unsigned long long) == 8, "");
914 return __popcnt64(__x);
920 template <class _InputIterator, class _Predicate>
921 inline _LIBCPP_INLINE_VISIBILITY
923 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
925 for (; __first != __last; ++__first)
926 if (!__pred(*__first))
933 template <class _InputIterator, class _Predicate>
934 inline _LIBCPP_INLINE_VISIBILITY
936 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
938 for (; __first != __last; ++__first)
939 if (__pred(*__first))
946 template <class _InputIterator, class _Predicate>
947 inline _LIBCPP_INLINE_VISIBILITY
949 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
951 for (; __first != __last; ++__first)
952 if (__pred(*__first))
959 template <class _InputIterator, class _Function>
960 inline _LIBCPP_INLINE_VISIBILITY
962 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
964 for (; __first != __last; ++__first)
969 #if _LIBCPP_STD_VER > 14
972 template <class _InputIterator, class _Size, class _Function>
973 inline _LIBCPP_INLINE_VISIBILITY
975 for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
977 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
978 _IntegralSize __n = __orig_n;
991 template <class _InputIterator, class _Tp>
992 inline _LIBCPP_INLINE_VISIBILITY
994 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
996 for (; __first != __last; ++__first)
997 if (*__first == __value_)
1004 template <class _InputIterator, class _Predicate>
1005 inline _LIBCPP_INLINE_VISIBILITY
1007 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1009 for (; __first != __last; ++__first)
1010 if (__pred(*__first))
1017 template<class _InputIterator, class _Predicate>
1018 inline _LIBCPP_INLINE_VISIBILITY
1020 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1022 for (; __first != __last; ++__first)
1023 if (!__pred(*__first))
1030 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1032 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1033 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1034 forward_iterator_tag, forward_iterator_tag)
1036 // modeled after search algorithm
1037 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1038 if (__first2 == __last2)
1044 if (__first1 == __last1) // if source exhausted return last correct answer
1045 return __r; // (or __last1 if never found)
1046 if (__pred(*__first1, *__first2))
1050 // *__first1 matches *__first2, now match elements after here
1051 _ForwardIterator1 __m1 = __first1;
1052 _ForwardIterator2 __m2 = __first2;
1055 if (++__m2 == __last2)
1056 { // Pattern exhaused, record answer and search for another one
1061 if (++__m1 == __last1) // Source exhausted, return last answer
1063 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1067 } // else there is a match, check next elements
1072 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1073 _BidirectionalIterator1
1074 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1075 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1076 bidirectional_iterator_tag, bidirectional_iterator_tag)
1078 // modeled after search algorithm (in reverse)
1079 if (__first2 == __last2)
1080 return __last1; // Everything matches an empty sequence
1081 _BidirectionalIterator1 __l1 = __last1;
1082 _BidirectionalIterator2 __l2 = __last2;
1086 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1089 if (__first1 == __l1) // return __last1 if no element matches *__first2
1091 if (__pred(*--__l1, *__l2))
1094 // *__l1 matches *__l2, now match elements before here
1095 _BidirectionalIterator1 __m1 = __l1;
1096 _BidirectionalIterator2 __m2 = __l2;
1099 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1101 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1103 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1106 } // else there is a match, check next elements
1111 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1112 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1113 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1114 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1115 random_access_iterator_tag, random_access_iterator_tag)
1117 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1118 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1121 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1122 if (__len1 < __len2)
1124 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1125 _RandomAccessIterator1 __l1 = __last1;
1126 _RandomAccessIterator2 __l2 = __last2;
1134 if (__pred(*--__l1, *__l2))
1137 _RandomAccessIterator1 __m1 = __l1;
1138 _RandomAccessIterator2 __m2 = __l2;
1141 if (__m2 == __first2)
1143 // no need to check range on __m1 because __s guarantees we have enough source
1144 if (!__pred(*--__m1, *--__m2))
1152 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1153 inline _LIBCPP_INLINE_VISIBILITY
1155 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1156 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1158 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1159 (__first1, __last1, __first2, __last2, __pred,
1160 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1161 typename iterator_traits<_ForwardIterator2>::iterator_category());
1164 template <class _ForwardIterator1, class _ForwardIterator2>
1165 inline _LIBCPP_INLINE_VISIBILITY
1167 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1168 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1170 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1171 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1172 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1177 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1178 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1179 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1180 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1182 for (; __first1 != __last1; ++__first1)
1183 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1184 if (__pred(*__first1, *__j))
1190 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1191 inline _LIBCPP_INLINE_VISIBILITY
1193 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1194 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1196 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1199 template <class _ForwardIterator1, class _ForwardIterator2>
1200 inline _LIBCPP_INLINE_VISIBILITY
1202 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1203 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1205 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1206 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1207 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1212 template <class _ForwardIterator, class _BinaryPredicate>
1213 inline _LIBCPP_INLINE_VISIBILITY
1215 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1217 if (__first != __last)
1219 _ForwardIterator __i = __first;
1220 while (++__i != __last)
1222 if (__pred(*__first, *__i))
1230 template <class _ForwardIterator>
1231 inline _LIBCPP_INLINE_VISIBILITY
1233 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1235 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1236 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1241 template <class _InputIterator, class _Tp>
1242 inline _LIBCPP_INLINE_VISIBILITY
1243 typename iterator_traits<_InputIterator>::difference_type
1244 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1246 typename iterator_traits<_InputIterator>::difference_type __r(0);
1247 for (; __first != __last; ++__first)
1248 if (*__first == __value_)
1255 template <class _InputIterator, class _Predicate>
1256 inline _LIBCPP_INLINE_VISIBILITY
1257 typename iterator_traits<_InputIterator>::difference_type
1258 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1260 typename iterator_traits<_InputIterator>::difference_type __r(0);
1261 for (; __first != __last; ++__first)
1262 if (__pred(*__first))
1269 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1270 inline _LIBCPP_INLINE_VISIBILITY
1271 pair<_InputIterator1, _InputIterator2>
1272 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1273 _InputIterator2 __first2, _BinaryPredicate __pred)
1275 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1276 if (!__pred(*__first1, *__first2))
1278 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1281 template <class _InputIterator1, class _InputIterator2>
1282 inline _LIBCPP_INLINE_VISIBILITY
1283 pair<_InputIterator1, _InputIterator2>
1284 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1286 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1287 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1288 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1291 #if _LIBCPP_STD_VER > 11
1292 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1293 inline _LIBCPP_INLINE_VISIBILITY
1294 pair<_InputIterator1, _InputIterator2>
1295 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1296 _InputIterator2 __first2, _InputIterator2 __last2,
1297 _BinaryPredicate __pred)
1299 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1300 if (!__pred(*__first1, *__first2))
1302 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1305 template <class _InputIterator1, class _InputIterator2>
1306 inline _LIBCPP_INLINE_VISIBILITY
1307 pair<_InputIterator1, _InputIterator2>
1308 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1309 _InputIterator2 __first2, _InputIterator2 __last2)
1311 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1312 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1313 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1319 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1320 inline _LIBCPP_INLINE_VISIBILITY
1322 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1324 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1325 if (!__pred(*__first1, *__first2))
1330 template <class _InputIterator1, class _InputIterator2>
1331 inline _LIBCPP_INLINE_VISIBILITY
1333 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1335 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1336 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1337 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1340 #if _LIBCPP_STD_VER > 11
1341 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1342 inline _LIBCPP_INLINE_VISIBILITY
1344 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1345 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1346 input_iterator_tag, input_iterator_tag )
1348 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1349 if (!__pred(*__first1, *__first2))
1351 return __first1 == __last1 && __first2 == __last2;
1354 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1355 inline _LIBCPP_INLINE_VISIBILITY
1357 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1358 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1359 random_access_iterator_tag, random_access_iterator_tag )
1361 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1363 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1364 typename add_lvalue_reference<_BinaryPredicate>::type>
1365 (__first1, __last1, __first2, __pred );
1368 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1369 inline _LIBCPP_INLINE_VISIBILITY
1371 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1372 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1374 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1375 (__first1, __last1, __first2, __last2, __pred,
1376 typename iterator_traits<_InputIterator1>::iterator_category(),
1377 typename iterator_traits<_InputIterator2>::iterator_category());
1380 template <class _InputIterator1, class _InputIterator2>
1381 inline _LIBCPP_INLINE_VISIBILITY
1383 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1384 _InputIterator2 __first2, _InputIterator2 __last2)
1386 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1387 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1388 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1389 typename iterator_traits<_InputIterator1>::iterator_category(),
1390 typename iterator_traits<_InputIterator2>::iterator_category());
1396 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1398 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1399 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1401 // shorten sequences as much as possible by lopping of any equal parts
1402 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1403 if (!__pred(*__first1, *__first2))
1407 // __first1 != __last1 && *__first1 != *__first2
1408 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1409 _D1 __l1 = _VSTD::distance(__first1, __last1);
1412 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1413 // For each element in [f1, l1) see if there are the same number of
1414 // equal elements in [f2, l2)
1415 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1417 // Have we already counted the number of *__i in [f1, l1)?
1418 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1419 if (__pred(*__j, *__i))
1422 // Count number of *__i in [f2, l2)
1424 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1425 if (__pred(*__i, *__j))
1429 // Count number of *__i in [__i, l1) (we can start with 1)
1431 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1432 if (__pred(*__i, *__j))
1442 template<class _ForwardIterator1, class _ForwardIterator2>
1443 inline _LIBCPP_INLINE_VISIBILITY
1445 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1446 _ForwardIterator2 __first2)
1448 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1449 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1450 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1453 #if _LIBCPP_STD_VER > 11
1454 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1456 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1457 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1458 _BinaryPredicate __pred,
1459 forward_iterator_tag, forward_iterator_tag )
1461 // shorten sequences as much as possible by lopping of any equal parts
1462 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1463 if (!__pred(*__first1, *__first2))
1465 return __first1 == __last1 && __first2 == __last2;
1467 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1468 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1469 _D1 __l1 = _VSTD::distance(__first1, __last1);
1471 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1472 _D2 __l2 = _VSTD::distance(__first2, __last2);
1476 // For each element in [f1, l1) see if there are the same number of
1477 // equal elements in [f2, l2)
1478 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1480 // Have we already counted the number of *__i in [f1, l1)?
1481 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1482 if (__pred(*__j, *__i))
1485 // Count number of *__i in [f2, l2)
1487 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1488 if (__pred(*__i, *__j))
1492 // Count number of *__i in [__i, l1) (we can start with 1)
1494 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1495 if (__pred(*__i, *__j))
1505 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1507 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1508 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1509 _BinaryPredicate __pred,
1510 random_access_iterator_tag, random_access_iterator_tag )
1512 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1514 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1515 typename add_lvalue_reference<_BinaryPredicate>::type>
1516 (__first1, __last1, __first2, __pred );
1519 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1520 inline _LIBCPP_INLINE_VISIBILITY
1522 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1523 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1524 _BinaryPredicate __pred )
1526 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1527 (__first1, __last1, __first2, __last2, __pred,
1528 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1529 typename iterator_traits<_ForwardIterator2>::iterator_category());
1532 template<class _ForwardIterator1, class _ForwardIterator2>
1533 inline _LIBCPP_INLINE_VISIBILITY
1535 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1536 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1538 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1539 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1540 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1541 __equal_to<__v1, __v2>(),
1542 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1543 typename iterator_traits<_ForwardIterator2>::iterator_category());
1549 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1550 pair<_ForwardIterator1, _ForwardIterator1>
1551 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1552 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1553 forward_iterator_tag, forward_iterator_tag)
1555 if (__first2 == __last2)
1556 return make_pair(__first1, __first1); // Everything matches an empty sequence
1559 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1562 if (__first1 == __last1) // return __last1 if no element matches *__first2
1563 return make_pair(__last1, __last1);
1564 if (__pred(*__first1, *__first2))
1568 // *__first1 matches *__first2, now match elements after here
1569 _ForwardIterator1 __m1 = __first1;
1570 _ForwardIterator2 __m2 = __first2;
1573 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1574 return make_pair(__first1, __m1);
1575 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1576 return make_pair(__last1, __last1);
1577 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1581 } // else there is a match, check next elements
1586 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1587 _LIBCPP_CONSTEXPR_AFTER_CXX11
1588 pair<_RandomAccessIterator1, _RandomAccessIterator1>
1589 __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1590 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1591 random_access_iterator_tag, random_access_iterator_tag)
1593 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1594 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1595 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1596 const _D2 __len2 = __last2 - __first2;
1598 return make_pair(__first1, __first1);
1599 const _D1 __len1 = __last1 - __first1;
1600 if (__len1 < __len2)
1601 return make_pair(__last1, __last1);
1602 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1608 if (__first1 == __s)
1609 return make_pair(__last1, __last1);
1610 if (__pred(*__first1, *__first2))
1615 _RandomAccessIterator1 __m1 = __first1;
1616 _RandomAccessIterator2 __m2 = __first2;
1619 if (++__m2 == __last2)
1620 return make_pair(__first1, __first1 + __len2);
1621 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1622 if (!__pred(*__m1, *__m2))
1631 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1632 inline _LIBCPP_INLINE_VISIBILITY
1634 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1635 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1637 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1638 (__first1, __last1, __first2, __last2, __pred,
1639 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1640 typename iterator_traits<_ForwardIterator2>::iterator_category())
1644 template <class _ForwardIterator1, class _ForwardIterator2>
1645 inline _LIBCPP_INLINE_VISIBILITY
1647 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1648 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1650 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1651 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1652 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1657 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1659 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1660 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1666 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1669 if (__first == __last) // return __last if no element matches __value_
1671 if (__pred(*__first, __value_))
1675 // *__first matches __value_, now match elements after here
1676 _ForwardIterator __m = __first;
1680 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1682 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1684 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1689 } // else there is a match, check next elements
1694 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1695 _RandomAccessIterator
1696 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1697 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1701 _Size __len = static_cast<_Size>(__last - __first);
1702 if (__len < __count)
1704 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1707 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1710 if (__first >= __s) // return __last if no element matches __value_
1712 if (__pred(*__first, __value_))
1716 // *__first matches __value_, now match elements after here
1717 _RandomAccessIterator __m = __first;
1721 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1723 ++__m; // no need to check range on __m because __s guarantees we have enough source
1724 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1729 } // else there is a match, check next elements
1734 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1735 inline _LIBCPP_INLINE_VISIBILITY
1737 search_n(_ForwardIterator __first, _ForwardIterator __last,
1738 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1740 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1741 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1742 typename iterator_traits<_ForwardIterator>::iterator_category());
1745 template <class _ForwardIterator, class _Size, class _Tp>
1746 inline _LIBCPP_INLINE_VISIBILITY
1748 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1750 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1751 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1752 __value_, __equal_to<__v, _Tp>());
1756 template <class _Iter>
1757 inline _LIBCPP_INLINE_VISIBILITY
1759 __unwrap_iter(_Iter __i)
1764 template <class _Tp>
1765 inline _LIBCPP_INLINE_VISIBILITY
1768 is_trivially_copy_assignable<_Tp>::value,
1771 __unwrap_iter(move_iterator<_Tp*> __i)
1776 #if _LIBCPP_DEBUG_LEVEL < 2
1778 template <class _Tp>
1779 inline _LIBCPP_INLINE_VISIBILITY
1782 is_trivially_copy_assignable<_Tp>::value,
1785 __unwrap_iter(__wrap_iter<_Tp*> __i)
1792 template <class _Tp>
1793 inline _LIBCPP_INLINE_VISIBILITY
1796 is_trivially_copy_assignable<_Tp>::value,
1799 __unwrap_iter(__wrap_iter<_Tp*> __i)
1804 #endif // _LIBCPP_DEBUG_LEVEL < 2
1806 template <class _InputIterator, class _OutputIterator>
1807 inline _LIBCPP_INLINE_VISIBILITY
1809 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1811 for (; __first != __last; ++__first, (void) ++__result)
1812 *__result = *__first;
1816 template <class _Tp, class _Up>
1817 inline _LIBCPP_INLINE_VISIBILITY
1820 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1821 is_trivially_copy_assignable<_Up>::value,
1824 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1826 const size_t __n = static_cast<size_t>(__last - __first);
1828 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1829 return __result + __n;
1832 template <class _InputIterator, class _OutputIterator>
1833 inline _LIBCPP_INLINE_VISIBILITY
1835 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1837 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1842 template <class _BidirectionalIterator, class _OutputIterator>
1843 inline _LIBCPP_INLINE_VISIBILITY
1845 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1847 while (__first != __last)
1848 *--__result = *--__last;
1852 template <class _Tp, class _Up>
1853 inline _LIBCPP_INLINE_VISIBILITY
1856 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1857 is_trivially_copy_assignable<_Up>::value,
1860 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1862 const size_t __n = static_cast<size_t>(__last - __first);
1866 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1871 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1872 inline _LIBCPP_INLINE_VISIBILITY
1873 _BidirectionalIterator2
1874 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1875 _BidirectionalIterator2 __result)
1877 return _VSTD::__copy_backward(__unwrap_iter(__first),
1878 __unwrap_iter(__last),
1879 __unwrap_iter(__result));
1884 template<class _InputIterator, class _OutputIterator, class _Predicate>
1885 inline _LIBCPP_INLINE_VISIBILITY
1887 copy_if(_InputIterator __first, _InputIterator __last,
1888 _OutputIterator __result, _Predicate __pred)
1890 for (; __first != __last; ++__first)
1892 if (__pred(*__first))
1894 *__result = *__first;
1903 template<class _InputIterator, class _Size, class _OutputIterator>
1904 inline _LIBCPP_INLINE_VISIBILITY
1907 __is_input_iterator<_InputIterator>::value &&
1908 !__is_random_access_iterator<_InputIterator>::value,
1911 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1913 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1914 _IntegralSize __n = __orig_n;
1917 *__result = *__first;
1919 for (--__n; __n > 0; --__n)
1922 *__result = *__first;
1929 template<class _InputIterator, class _Size, class _OutputIterator>
1930 inline _LIBCPP_INLINE_VISIBILITY
1933 __is_random_access_iterator<_InputIterator>::value,
1936 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1938 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1939 _IntegralSize __n = __orig_n;
1940 return _VSTD::copy(__first, __first + __n, __result);
1945 template <class _InputIterator, class _OutputIterator>
1946 inline _LIBCPP_INLINE_VISIBILITY
1948 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1950 for (; __first != __last; ++__first, (void) ++__result)
1951 *__result = _VSTD::move(*__first);
1955 template <class _Tp, class _Up>
1956 inline _LIBCPP_INLINE_VISIBILITY
1959 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1960 is_trivially_copy_assignable<_Up>::value,
1963 __move(_Tp* __first, _Tp* __last, _Up* __result)
1965 const size_t __n = static_cast<size_t>(__last - __first);
1967 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1968 return __result + __n;
1971 template <class _InputIterator, class _OutputIterator>
1972 inline _LIBCPP_INLINE_VISIBILITY
1974 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1976 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1981 template <class _InputIterator, class _OutputIterator>
1982 inline _LIBCPP_INLINE_VISIBILITY
1984 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1986 while (__first != __last)
1987 *--__result = _VSTD::move(*--__last);
1991 template <class _Tp, class _Up>
1992 inline _LIBCPP_INLINE_VISIBILITY
1995 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1996 is_trivially_copy_assignable<_Up>::value,
1999 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
2001 const size_t __n = static_cast<size_t>(__last - __first);
2005 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
2010 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
2011 inline _LIBCPP_INLINE_VISIBILITY
2012 _BidirectionalIterator2
2013 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
2014 _BidirectionalIterator2 __result)
2016 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
2021 // moved to <type_traits> for better swap / noexcept support
2025 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
2026 inline _LIBCPP_INLINE_VISIBILITY
2028 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
2030 for (; __first != __last; ++__first, (void) ++__result)
2031 *__result = __op(*__first);
2035 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
2036 inline _LIBCPP_INLINE_VISIBILITY
2038 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
2039 _OutputIterator __result, _BinaryOperation __binary_op)
2041 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
2042 *__result = __binary_op(*__first1, *__first2);
2048 template <class _ForwardIterator, class _Tp>
2049 inline _LIBCPP_INLINE_VISIBILITY
2051 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2053 for (; __first != __last; ++__first)
2054 if (*__first == __old_value)
2055 *__first = __new_value;
2060 template <class _ForwardIterator, class _Predicate, class _Tp>
2061 inline _LIBCPP_INLINE_VISIBILITY
2063 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2065 for (; __first != __last; ++__first)
2066 if (__pred(*__first))
2067 *__first = __new_value;
2072 template <class _InputIterator, class _OutputIterator, class _Tp>
2073 inline _LIBCPP_INLINE_VISIBILITY
2075 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2076 const _Tp& __old_value, const _Tp& __new_value)
2078 for (; __first != __last; ++__first, (void) ++__result)
2079 if (*__first == __old_value)
2080 *__result = __new_value;
2082 *__result = *__first;
2088 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2089 inline _LIBCPP_INLINE_VISIBILITY
2091 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2092 _Predicate __pred, const _Tp& __new_value)
2094 for (; __first != __last; ++__first, (void) ++__result)
2095 if (__pred(*__first))
2096 *__result = __new_value;
2098 *__result = *__first;
2104 template <class _OutputIterator, class _Size, class _Tp>
2105 inline _LIBCPP_INLINE_VISIBILITY
2107 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2109 for (; __n > 0; ++__first, (void) --__n)
2110 *__first = __value_;
2114 template <class _Tp, class _Size, class _Up>
2115 inline _LIBCPP_INLINE_VISIBILITY
2118 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2119 !is_same<_Tp, bool>::value &&
2120 is_integral<_Up>::value && sizeof(_Up) == 1,
2123 __fill_n(_Tp* __first, _Size __n,_Up __value_)
2126 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2127 return __first + __n;
2130 template <class _OutputIterator, class _Size, class _Tp>
2131 inline _LIBCPP_INLINE_VISIBILITY
2133 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2135 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
2140 template <class _ForwardIterator, class _Tp>
2141 inline _LIBCPP_INLINE_VISIBILITY
2143 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2145 for (; __first != __last; ++__first)
2146 *__first = __value_;
2149 template <class _RandomAccessIterator, class _Tp>
2150 inline _LIBCPP_INLINE_VISIBILITY
2152 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2154 _VSTD::fill_n(__first, __last - __first, __value_);
2157 template <class _ForwardIterator, class _Tp>
2158 inline _LIBCPP_INLINE_VISIBILITY
2160 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2162 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2167 template <class _ForwardIterator, class _Generator>
2168 inline _LIBCPP_INLINE_VISIBILITY
2170 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2172 for (; __first != __last; ++__first)
2178 template <class _OutputIterator, class _Size, class _Generator>
2179 inline _LIBCPP_INLINE_VISIBILITY
2181 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2183 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2184 _IntegralSize __n = __orig_n;
2185 for (; __n > 0; ++__first, (void) --__n)
2192 template <class _ForwardIterator, class _Tp>
2194 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2196 __first = _VSTD::find(__first, __last, __value_);
2197 if (__first != __last)
2199 _ForwardIterator __i = __first;
2200 while (++__i != __last)
2202 if (!(*__i == __value_))
2204 *__first = _VSTD::move(*__i);
2214 template <class _ForwardIterator, class _Predicate>
2216 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2218 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2219 (__first, __last, __pred);
2220 if (__first != __last)
2222 _ForwardIterator __i = __first;
2223 while (++__i != __last)
2227 *__first = _VSTD::move(*__i);
2237 template <class _InputIterator, class _OutputIterator, class _Tp>
2238 inline _LIBCPP_INLINE_VISIBILITY
2240 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2242 for (; __first != __last; ++__first)
2244 if (!(*__first == __value_))
2246 *__result = *__first;
2255 template <class _InputIterator, class _OutputIterator, class _Predicate>
2256 inline _LIBCPP_INLINE_VISIBILITY
2258 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2260 for (; __first != __last; ++__first)
2262 if (!__pred(*__first))
2264 *__result = *__first;
2273 template <class _ForwardIterator, class _BinaryPredicate>
2275 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2277 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2278 (__first, __last, __pred);
2279 if (__first != __last)
2283 _ForwardIterator __i = __first;
2284 for (++__i; ++__i != __last;)
2285 if (!__pred(*__first, *__i))
2286 *++__first = _VSTD::move(*__i);
2292 template <class _ForwardIterator>
2293 inline _LIBCPP_INLINE_VISIBILITY
2295 unique(_ForwardIterator __first, _ForwardIterator __last)
2297 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2298 return _VSTD::unique(__first, __last, __equal_to<__v>());
2303 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2305 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2306 input_iterator_tag, output_iterator_tag)
2308 if (__first != __last)
2310 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2313 while (++__first != __last)
2315 if (!__pred(__t, *__first))
2326 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2328 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2329 forward_iterator_tag, output_iterator_tag)
2331 if (__first != __last)
2333 _ForwardIterator __i = __first;
2336 while (++__first != __last)
2338 if (!__pred(*__i, *__first))
2340 *__result = *__first;
2349 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2351 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2352 input_iterator_tag, forward_iterator_tag)
2354 if (__first != __last)
2356 *__result = *__first;
2357 while (++__first != __last)
2358 if (!__pred(*__result, *__first))
2359 *++__result = *__first;
2365 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2366 inline _LIBCPP_INLINE_VISIBILITY
2368 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2370 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2371 (__first, __last, __result, __pred,
2372 typename iterator_traits<_InputIterator>::iterator_category(),
2373 typename iterator_traits<_OutputIterator>::iterator_category());
2376 template <class _InputIterator, class _OutputIterator>
2377 inline _LIBCPP_INLINE_VISIBILITY
2379 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2381 typedef typename iterator_traits<_InputIterator>::value_type __v;
2382 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2387 template <class _BidirectionalIterator>
2388 inline _LIBCPP_INLINE_VISIBILITY
2390 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2392 while (__first != __last)
2394 if (__first == --__last)
2396 _VSTD::iter_swap(__first, __last);
2401 template <class _RandomAccessIterator>
2402 inline _LIBCPP_INLINE_VISIBILITY
2404 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2406 if (__first != __last)
2407 for (; __first < --__last; ++__first)
2408 _VSTD::iter_swap(__first, __last);
2411 template <class _BidirectionalIterator>
2412 inline _LIBCPP_INLINE_VISIBILITY
2414 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2416 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2421 template <class _BidirectionalIterator, class _OutputIterator>
2422 inline _LIBCPP_INLINE_VISIBILITY
2424 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2426 for (; __first != __last; ++__result)
2427 *__result = *--__last;
2433 template <class _ForwardIterator>
2435 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2437 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2438 value_type __tmp = _VSTD::move(*__first);
2439 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2440 *__lm1 = _VSTD::move(__tmp);
2444 template <class _BidirectionalIterator>
2445 _BidirectionalIterator
2446 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2448 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2449 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2450 value_type __tmp = _VSTD::move(*__lm1);
2451 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2452 *__first = _VSTD::move(__tmp);
2456 template <class _ForwardIterator>
2458 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2460 _ForwardIterator __i = __middle;
2463 swap(*__first, *__i);
2465 if (++__i == __last)
2467 if (__first == __middle)
2470 _ForwardIterator __r = __first;
2471 if (__first != __middle)
2476 swap(*__first, *__i);
2478 if (++__i == __last)
2480 if (__first == __middle)
2484 else if (__first == __middle)
2491 template<typename _Integral>
2492 inline _LIBCPP_INLINE_VISIBILITY
2494 __algo_gcd(_Integral __x, _Integral __y)
2498 _Integral __t = __x % __y;
2505 template<typename _RandomAccessIterator>
2506 _RandomAccessIterator
2507 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2509 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2510 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2512 const difference_type __m1 = __middle - __first;
2513 const difference_type __m2 = __last - __middle;
2516 _VSTD::swap_ranges(__first, __middle, __middle);
2519 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
2520 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2522 value_type __t(_VSTD::move(*--__p));
2523 _RandomAccessIterator __p1 = __p;
2524 _RandomAccessIterator __p2 = __p1 + __m1;
2527 *__p1 = _VSTD::move(*__p2);
2529 const difference_type __d = __last - __p2;
2533 __p2 = __first + (__m1 - __d);
2534 } while (__p2 != __p);
2535 *__p1 = _VSTD::move(__t);
2537 return __first + __m2;
2540 template <class _ForwardIterator>
2541 inline _LIBCPP_INLINE_VISIBILITY
2543 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2544 _VSTD::forward_iterator_tag)
2546 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2547 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2549 if (_VSTD::next(__first) == __middle)
2550 return _VSTD::__rotate_left(__first, __last);
2552 return _VSTD::__rotate_forward(__first, __middle, __last);
2555 template <class _BidirectionalIterator>
2556 inline _LIBCPP_INLINE_VISIBILITY
2557 _BidirectionalIterator
2558 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2559 _VSTD::bidirectional_iterator_tag)
2561 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2562 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2564 if (_VSTD::next(__first) == __middle)
2565 return _VSTD::__rotate_left(__first, __last);
2566 if (_VSTD::next(__middle) == __last)
2567 return _VSTD::__rotate_right(__first, __last);
2569 return _VSTD::__rotate_forward(__first, __middle, __last);
2572 template <class _RandomAccessIterator>
2573 inline _LIBCPP_INLINE_VISIBILITY
2574 _RandomAccessIterator
2575 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2576 _VSTD::random_access_iterator_tag)
2578 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2579 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2581 if (_VSTD::next(__first) == __middle)
2582 return _VSTD::__rotate_left(__first, __last);
2583 if (_VSTD::next(__middle) == __last)
2584 return _VSTD::__rotate_right(__first, __last);
2585 return _VSTD::__rotate_gcd(__first, __middle, __last);
2587 return _VSTD::__rotate_forward(__first, __middle, __last);
2590 template <class _ForwardIterator>
2591 inline _LIBCPP_INLINE_VISIBILITY
2593 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2595 if (__first == __middle)
2597 if (__middle == __last)
2599 return _VSTD::__rotate(__first, __middle, __last,
2600 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2605 template <class _ForwardIterator, class _OutputIterator>
2606 inline _LIBCPP_INLINE_VISIBILITY
2608 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2610 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2615 template <class _ForwardIterator, class _Compare>
2616 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2618 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2620 if (__first != __last)
2622 _ForwardIterator __i = __first;
2623 while (++__i != __last)
2624 if (__comp(*__i, *__first))
2630 template <class _ForwardIterator>
2631 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2633 min_element(_ForwardIterator __first, _ForwardIterator __last)
2635 return _VSTD::min_element(__first, __last,
2636 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2641 template <class _Tp, class _Compare>
2642 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2644 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2646 return __comp(__b, __a) ? __b : __a;
2649 template <class _Tp>
2650 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2652 min(const _Tp& __a, const _Tp& __b)
2654 return _VSTD::min(__a, __b, __less<_Tp>());
2657 #ifndef _LIBCPP_CXX03_LANG
2659 template<class _Tp, class _Compare>
2660 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2662 min(initializer_list<_Tp> __t, _Compare __comp)
2664 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2668 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2670 min(initializer_list<_Tp> __t)
2672 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2675 #endif // _LIBCPP_CXX03_LANG
2679 template <class _ForwardIterator, class _Compare>
2680 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2682 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2684 if (__first != __last)
2686 _ForwardIterator __i = __first;
2687 while (++__i != __last)
2688 if (__comp(*__first, *__i))
2695 template <class _ForwardIterator>
2696 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2698 max_element(_ForwardIterator __first, _ForwardIterator __last)
2700 return _VSTD::max_element(__first, __last,
2701 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2706 template <class _Tp, class _Compare>
2707 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2709 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2711 return __comp(__a, __b) ? __b : __a;
2714 template <class _Tp>
2715 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2717 max(const _Tp& __a, const _Tp& __b)
2719 return _VSTD::max(__a, __b, __less<_Tp>());
2722 #ifndef _LIBCPP_CXX03_LANG
2724 template<class _Tp, class _Compare>
2725 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2727 max(initializer_list<_Tp> __t, _Compare __comp)
2729 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2733 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2735 max(initializer_list<_Tp> __t)
2737 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2740 #endif // _LIBCPP_CXX03_LANG
2742 #if _LIBCPP_STD_VER > 14
2744 template<class _Tp, class _Compare>
2745 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2747 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2749 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2750 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2755 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2757 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2759 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2765 template <class _ForwardIterator, class _Compare>
2766 _LIBCPP_CONSTEXPR_AFTER_CXX11
2767 std::pair<_ForwardIterator, _ForwardIterator>
2768 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2770 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2771 if (__first != __last)
2773 if (++__first != __last)
2775 if (__comp(*__first, *__result.first))
2776 __result.first = __first;
2778 __result.second = __first;
2779 while (++__first != __last)
2781 _ForwardIterator __i = __first;
2782 if (++__first == __last)
2784 if (__comp(*__i, *__result.first))
2785 __result.first = __i;
2786 else if (!__comp(*__i, *__result.second))
2787 __result.second = __i;
2792 if (__comp(*__first, *__i))
2794 if (__comp(*__first, *__result.first))
2795 __result.first = __first;
2796 if (!__comp(*__i, *__result.second))
2797 __result.second = __i;
2801 if (__comp(*__i, *__result.first))
2802 __result.first = __i;
2803 if (!__comp(*__first, *__result.second))
2804 __result.second = __first;
2813 template <class _ForwardIterator>
2814 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2815 std::pair<_ForwardIterator, _ForwardIterator>
2816 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2818 return _VSTD::minmax_element(__first, __last,
2819 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2824 template<class _Tp, class _Compare>
2825 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2826 pair<const _Tp&, const _Tp&>
2827 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2829 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2830 pair<const _Tp&, const _Tp&>(__a, __b);
2834 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2835 pair<const _Tp&, const _Tp&>
2836 minmax(const _Tp& __a, const _Tp& __b)
2838 return _VSTD::minmax(__a, __b, __less<_Tp>());
2841 #ifndef _LIBCPP_CXX03_LANG
2843 template<class _Tp, class _Compare>
2844 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2846 minmax(initializer_list<_Tp> __t, _Compare __comp)
2848 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2849 _Iter __first = __t.begin();
2850 _Iter __last = __t.end();
2851 std::pair<_Tp, _Tp> __result(*__first, *__first);
2854 if (__t.size() % 2 == 0)
2856 if (__comp(*__first, __result.first))
2857 __result.first = *__first;
2859 __result.second = *__first;
2863 while (__first != __last)
2865 _Tp __prev = *__first++;
2866 if (__comp(*__first, __prev)) {
2867 if ( __comp(*__first, __result.first)) __result.first = *__first;
2868 if (!__comp(__prev, __result.second)) __result.second = __prev;
2871 if ( __comp(__prev, __result.first)) __result.first = __prev;
2872 if (!__comp(*__first, __result.second)) __result.second = *__first;
2881 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2883 minmax(initializer_list<_Tp> __t)
2885 return _VSTD::minmax(__t, __less<_Tp>());
2888 #endif // _LIBCPP_CXX03_LANG
2892 // __independent_bits_engine
2894 template <unsigned long long _Xp, size_t _Rp>
2897 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2898 : __log2_imp<_Xp, _Rp - 1>::value;
2901 template <unsigned long long _Xp>
2902 struct __log2_imp<_Xp, 0>
2904 static const size_t value = 0;
2907 template <size_t _Rp>
2908 struct __log2_imp<0, _Rp>
2910 static const size_t value = _Rp + 1;
2913 template <class _UIntType, _UIntType _Xp>
2916 static const size_t value = __log2_imp<_Xp,
2917 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
2920 template<class _Engine, class _UIntType>
2921 class __independent_bits_engine
2925 typedef _UIntType result_type;
2928 typedef typename _Engine::result_type _Engine_result_type;
2929 typedef typename conditional
2931 sizeof(_Engine_result_type) <= sizeof(result_type),
2934 >::type _Working_result_type;
2941 _Working_result_type __y0_;
2942 _Working_result_type __y1_;
2943 _Engine_result_type __mask0_;
2944 _Engine_result_type __mask1_;
2946 #ifdef _LIBCPP_CXX03_LANG
2947 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2948 + _Working_result_type(1);
2950 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2951 + _Working_result_type(1);
2953 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2954 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2955 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2958 // constructors and seeding functions
2959 __independent_bits_engine(_Engine& __e, size_t __w);
2961 // generating functions
2962 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2965 result_type __eval(false_type);
2966 result_type __eval(true_type);
2969 template<class _Engine, class _UIntType>
2970 __independent_bits_engine<_Engine, _UIntType>
2971 ::__independent_bits_engine(_Engine& __e, size_t __w)
2975 __n_ = __w_ / __m + (__w_ % __m != 0);
2976 __w0_ = __w_ / __n_;
2979 else if (__w0_ < _WDt)
2980 __y0_ = (_Rp >> __w0_) << __w0_;
2983 if (_Rp - __y0_ > __y0_ / __n_)
2986 __w0_ = __w_ / __n_;
2988 __y0_ = (_Rp >> __w0_) << __w0_;
2992 __n0_ = __n_ - __w_ % __n_;
2993 if (__w0_ < _WDt - 1)
2994 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2997 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2998 _Engine_result_type(0);
2999 __mask1_ = __w0_ < _EDt - 1 ?
3000 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
3001 _Engine_result_type(~0);
3004 template<class _Engine, class _UIntType>
3007 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
3009 return static_cast<result_type>(__e_() & __mask0_);
3012 template<class _Engine, class _UIntType>
3014 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
3016 const size_t _WRt = numeric_limits<result_type>::digits;
3017 result_type _Sp = 0;
3018 for (size_t __k = 0; __k < __n0_; ++__k)
3020 _Engine_result_type __u;
3023 __u = __e_() - _Engine::min();
3024 } while (__u >= __y0_);
3029 _Sp += __u & __mask0_;
3031 for (size_t __k = __n0_; __k < __n_; ++__k)
3033 _Engine_result_type __u;
3036 __u = __e_() - _Engine::min();
3037 } while (__u >= __y1_);
3038 if (__w0_ < _WRt - 1)
3042 _Sp += __u & __mask1_;
3047 // uniform_int_distribution
3049 template<class _IntType = int>
3050 class uniform_int_distribution
3054 typedef _IntType result_type;
3061 typedef uniform_int_distribution distribution_type;
3063 explicit param_type(result_type __a = 0,
3064 result_type __b = numeric_limits<result_type>::max())
3065 : __a_(__a), __b_(__b) {}
3067 result_type a() const {return __a_;}
3068 result_type b() const {return __b_;}
3070 friend bool operator==(const param_type& __x, const param_type& __y)
3071 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3072 friend bool operator!=(const param_type& __x, const param_type& __y)
3073 {return !(__x == __y);}
3080 // constructors and reset functions
3081 explicit uniform_int_distribution(result_type __a = 0,
3082 result_type __b = numeric_limits<result_type>::max())
3083 : __p_(param_type(__a, __b)) {}
3084 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3087 // generating functions
3088 template<class _URNG> result_type operator()(_URNG& __g)
3089 {return (*this)(__g, __p_);}
3090 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3092 // property functions
3093 result_type a() const {return __p_.a();}
3094 result_type b() const {return __p_.b();}
3096 param_type param() const {return __p_;}
3097 void param(const param_type& __p) {__p_ = __p;}
3099 result_type min() const {return a();}
3100 result_type max() const {return b();}
3102 friend bool operator==(const uniform_int_distribution& __x,
3103 const uniform_int_distribution& __y)
3104 {return __x.__p_ == __y.__p_;}
3105 friend bool operator!=(const uniform_int_distribution& __x,
3106 const uniform_int_distribution& __y)
3107 {return !(__x == __y);}
3110 template<class _IntType>
3111 template<class _URNG>
3112 typename uniform_int_distribution<_IntType>::result_type
3113 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3115 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3116 uint32_t, uint64_t>::type _UIntType;
3117 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3120 const size_t _Dt = numeric_limits<_UIntType>::digits;
3121 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3123 return static_cast<result_type>(_Eng(__g, _Dt)());
3124 size_t __w = _Dt - __clz(_Rp) - 1;
3125 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3132 } while (__u >= _Rp);
3133 return static_cast<result_type>(__u + __p.a());
3136 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3137 || defined(_LIBCPP_BUILDING_LIBRARY)
3138 class _LIBCPP_TYPE_VIS __rs_default;
3140 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3142 class _LIBCPP_TYPE_VIS __rs_default
3144 static unsigned __c_;
3148 typedef uint_fast32_t result_type;
3150 static const result_type _Min = 0;
3151 static const result_type _Max = 0xFFFFFFFF;
3153 __rs_default(const __rs_default&);
3156 result_type operator()();
3158 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3159 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3161 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3164 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3166 template <class _RandomAccessIterator>
3168 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3170 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3171 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3172 typedef typename _Dp::param_type _Pp;
3173 difference_type __d = __last - __first;
3177 __rs_default __g = __rs_get();
3178 for (--__last, --__d; __first < __last; ++__first, --__d)
3180 difference_type __i = __uid(__g, _Pp(0, __d));
3181 if (__i != difference_type(0))
3182 swap(*__first, *(__first + __i));
3187 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3189 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3190 #ifndef _LIBCPP_CXX03_LANG
3191 _RandomNumberGenerator&& __rand)
3193 _RandomNumberGenerator& __rand)
3196 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3197 difference_type __d = __last - __first;
3200 for (--__last; __first < __last; ++__first, --__d)
3202 difference_type __i = __rand(__d);
3203 swap(*__first, *(__first + __i));
3209 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3210 class _UniformRandomNumberGenerator>
3211 _LIBCPP_INLINE_VISIBILITY
3212 _SampleIterator __sample(_PopulationIterator __first,
3213 _PopulationIterator __last, _SampleIterator __output_iter,
3215 _UniformRandomNumberGenerator & __g,
3216 input_iterator_tag) {
3219 for (; __first != __last && __k < __n; ++__first, (void)++__k)
3220 __output_iter[__k] = *__first;
3221 _Distance __sz = __k;
3222 for (; __first != __last; ++__first, (void)++__k) {
3223 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3225 __output_iter[__r] = *__first;
3227 return __output_iter + _VSTD::min(__n, __k);
3230 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3231 class _UniformRandomNumberGenerator>
3232 _LIBCPP_INLINE_VISIBILITY
3233 _SampleIterator __sample(_PopulationIterator __first,
3234 _PopulationIterator __last, _SampleIterator __output_iter,
3236 _UniformRandomNumberGenerator& __g,
3237 forward_iterator_tag) {
3238 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3239 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3241 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3243 *__output_iter++ = *__first;
3247 return __output_iter;
3250 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3251 class _UniformRandomNumberGenerator>
3252 _LIBCPP_INLINE_VISIBILITY
3253 _SampleIterator __sample(_PopulationIterator __first,
3254 _PopulationIterator __last, _SampleIterator __output_iter,
3255 _Distance __n, _UniformRandomNumberGenerator& __g) {
3256 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3258 typedef typename iterator_traits<_PopulationIterator>::difference_type
3260 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3261 __is_random_access_iterator<_SampleIterator>::value,
3262 "SampleIterator must meet the requirements of RandomAccessIterator");
3263 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3264 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3265 return _VSTD::__sample(
3266 __first, __last, __output_iter, _CommonType(__n),
3267 __g, _PopCategory());
3270 #if _LIBCPP_STD_VER > 14
3271 template <class _PopulationIterator, class _SampleIterator, class _Distance,
3272 class _UniformRandomNumberGenerator>
3273 inline _LIBCPP_INLINE_VISIBILITY
3274 _SampleIterator sample(_PopulationIterator __first,
3275 _PopulationIterator __last, _SampleIterator __output_iter,
3276 _Distance __n, _UniformRandomNumberGenerator&& __g) {
3277 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
3279 #endif // _LIBCPP_STD_VER > 14
3281 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3282 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3283 #ifndef _LIBCPP_CXX03_LANG
3284 _UniformRandomNumberGenerator&& __g)
3286 _UniformRandomNumberGenerator& __g)
3289 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3290 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3291 typedef typename _Dp::param_type _Pp;
3292 difference_type __d = __last - __first;
3296 for (--__last, --__d; __first < __last; ++__first, --__d)
3298 difference_type __i = __uid(__g, _Pp(0, __d));
3299 if (__i != difference_type(0))
3300 swap(*__first, *(__first + __i));
3305 template <class _InputIterator, class _Predicate>
3307 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3309 for (; __first != __last; ++__first)
3310 if (!__pred(*__first))
3312 if ( __first == __last )
3315 for (; __first != __last; ++__first)
3316 if (__pred(*__first))
3323 template <class _Predicate, class _ForwardIterator>
3325 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3329 if (__first == __last)
3331 if (!__pred(*__first))
3335 for (_ForwardIterator __p = __first; ++__p != __last;)
3339 swap(*__first, *__p);
3346 template <class _Predicate, class _BidirectionalIterator>
3347 _BidirectionalIterator
3348 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3349 bidirectional_iterator_tag)
3355 if (__first == __last)
3357 if (!__pred(*__first))
3363 if (__first == --__last)
3365 } while (!__pred(*__last));
3366 swap(*__first, *__last);
3371 template <class _ForwardIterator, class _Predicate>
3372 inline _LIBCPP_INLINE_VISIBILITY
3374 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3376 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3377 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3382 template <class _InputIterator, class _OutputIterator1,
3383 class _OutputIterator2, class _Predicate>
3384 pair<_OutputIterator1, _OutputIterator2>
3385 partition_copy(_InputIterator __first, _InputIterator __last,
3386 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3389 for (; __first != __last; ++__first)
3391 if (__pred(*__first))
3393 *__out_true = *__first;
3398 *__out_false = *__first;
3402 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3407 template<class _ForwardIterator, class _Predicate>
3409 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3411 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3412 difference_type __len = _VSTD::distance(__first, __last);
3415 difference_type __l2 = __len / 2;
3416 _ForwardIterator __m = __first;
3417 _VSTD::advance(__m, __l2);
3431 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3433 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3434 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3436 // *__first is known to be false
3442 _ForwardIterator __m = __first;
3445 swap(*__first, *__m);
3450 if (__len <= __p.second)
3451 { // The buffer is big enough to use
3452 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3453 __destruct_n __d(0);
3454 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3455 // Move the falses into the temporary buffer, and the trues to the front of the line
3456 // Update __first to always point to the end of the trues
3457 value_type* __t = __p.first;
3458 ::new(__t) value_type(_VSTD::move(*__first));
3459 __d.__incr((value_type*)0);
3461 _ForwardIterator __i = __first;
3462 while (++__i != __last)
3466 *__first = _VSTD::move(*__i);
3471 ::new(__t) value_type(_VSTD::move(*__i));
3472 __d.__incr((value_type*)0);
3476 // All trues now at start of range, all falses in buffer
3477 // Move falses back into range, but don't mess up __first which points to first false
3479 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3480 *__i = _VSTD::move(*__t2);
3481 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3484 // Else not enough buffer, do in place
3486 _ForwardIterator __m = __first;
3487 _Distance __len2 = __len / 2; // __len2 >= 2
3488 _VSTD::advance(__m, __len2);
3489 // recurse on [__first, __m), *__first know to be false
3490 // F?????????????????
3492 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3493 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3494 // TTTFFFFF??????????
3496 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3497 _ForwardIterator __m1 = __m;
3498 _ForwardIterator __second_false = __last;
3499 _Distance __len_half = __len - __len2;
3500 while (__pred(*__m1))
3502 if (++__m1 == __last)
3503 goto __second_half_done;
3506 // TTTFFFFFTTTF??????
3508 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3510 // TTTFFFFFTTTTTFFFFF
3512 return _VSTD::rotate(__first_false, __m, __second_false);
3513 // TTTTTTTTFFFFFFFFFF
3517 struct __return_temporary_buffer
3519 template <class _Tp>
3520 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3523 template <class _Predicate, class _ForwardIterator>
3525 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3526 forward_iterator_tag)
3528 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3529 // Either prove all true and return __first or point to first false
3532 if (__first == __last)
3534 if (!__pred(*__first))
3538 // We now have a reduced range [__first, __last)
3539 // *__first is known to be false
3540 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3541 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3542 difference_type __len = _VSTD::distance(__first, __last);
3543 pair<value_type*, ptrdiff_t> __p(0, 0);
3544 unique_ptr<value_type, __return_temporary_buffer> __h;
3545 if (__len >= __alloc_limit)
3547 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3548 __h.reset(__p.first);
3550 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3551 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3554 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3555 _BidirectionalIterator
3556 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3557 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3559 // *__first is known to be false
3560 // *__last is known to be true
3564 swap(*__first, *__last);
3569 _BidirectionalIterator __m = __first;
3572 swap(*__first, *__m);
3573 swap(*__m, *__last);
3576 swap(*__m, *__last);
3577 swap(*__first, *__m);
3580 if (__len <= __p.second)
3581 { // The buffer is big enough to use
3582 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3583 __destruct_n __d(0);
3584 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3585 // Move the falses into the temporary buffer, and the trues to the front of the line
3586 // Update __first to always point to the end of the trues
3587 value_type* __t = __p.first;
3588 ::new(__t) value_type(_VSTD::move(*__first));
3589 __d.__incr((value_type*)0);
3591 _BidirectionalIterator __i = __first;
3592 while (++__i != __last)
3596 *__first = _VSTD::move(*__i);
3601 ::new(__t) value_type(_VSTD::move(*__i));
3602 __d.__incr((value_type*)0);
3606 // move *__last, known to be true
3607 *__first = _VSTD::move(*__i);
3609 // All trues now at start of range, all falses in buffer
3610 // Move falses back into range, but don't mess up __first which points to first false
3611 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3612 *__i = _VSTD::move(*__t2);
3613 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3616 // Else not enough buffer, do in place
3618 _BidirectionalIterator __m = __first;
3619 _Distance __len2 = __len / 2; // __len2 >= 2
3620 _VSTD::advance(__m, __len2);
3621 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3622 // F????????????????T
3624 _BidirectionalIterator __m1 = __m;
3625 _BidirectionalIterator __first_false = __first;
3626 _Distance __len_half = __len2;
3627 while (!__pred(*--__m1))
3629 if (__m1 == __first)
3630 goto __first_half_done;
3633 // F???TFFF?????????T
3635 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3636 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3638 // TTTFFFFF?????????T
3640 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3642 _BidirectionalIterator __second_false = __last;
3644 __len_half = __len - __len2;
3645 while (__pred(*__m1))
3647 if (++__m1 == __last)
3648 goto __second_half_done;
3651 // TTTFFFFFTTTF?????T
3653 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3655 // TTTFFFFFTTTTTFFFFF
3657 return _VSTD::rotate(__first_false, __m, __second_false);
3658 // TTTTTTTTFFFFFFFFFF
3662 template <class _Predicate, class _BidirectionalIterator>
3663 _BidirectionalIterator
3664 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3665 bidirectional_iterator_tag)
3667 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3668 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3669 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3670 // Either prove all true and return __first or point to first false
3673 if (__first == __last)
3675 if (!__pred(*__first))
3679 // __first points to first false, everything prior to __first is already set.
3680 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3683 if (__first == --__last)
3685 } while (!__pred(*__last));
3686 // We now have a reduced range [__first, __last]
3687 // *__first is known to be false
3688 // *__last is known to be true
3690 difference_type __len = _VSTD::distance(__first, __last) + 1;
3691 pair<value_type*, ptrdiff_t> __p(0, 0);
3692 unique_ptr<value_type, __return_temporary_buffer> __h;
3693 if (__len >= __alloc_limit)
3695 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3696 __h.reset(__p.first);
3698 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3699 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3702 template <class _ForwardIterator, class _Predicate>
3703 inline _LIBCPP_INLINE_VISIBILITY
3705 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3707 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3708 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3713 template <class _ForwardIterator, class _Compare>
3715 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3717 if (__first != __last)
3719 _ForwardIterator __i = __first;
3720 while (++__i != __last)
3722 if (__comp(*__i, *__first))
3730 template<class _ForwardIterator>
3731 inline _LIBCPP_INLINE_VISIBILITY
3733 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3735 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3740 template <class _ForwardIterator, class _Compare>
3741 inline _LIBCPP_INLINE_VISIBILITY
3743 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3745 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3748 template<class _ForwardIterator>
3749 inline _LIBCPP_INLINE_VISIBILITY
3751 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3753 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3758 // stable, 2-3 compares, 0-2 swaps
3760 template <class _Compare, class _ForwardIterator>
3762 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3765 if (!__c(*__y, *__x)) // if x <= y
3767 if (!__c(*__z, *__y)) // if y <= z
3768 return __r; // x <= y && y <= z
3770 swap(*__y, *__z); // x <= z && y < z
3772 if (__c(*__y, *__x)) // if x > y
3774 swap(*__x, *__y); // x < y && y <= z
3777 return __r; // x <= y && y < z
3779 if (__c(*__z, *__y)) // x > y, if y > z
3781 swap(*__x, *__z); // x < y && y < z
3785 swap(*__x, *__y); // x > y && y <= z
3786 __r = 1; // x < y && x <= z
3787 if (__c(*__z, *__y)) // if y > z
3789 swap(*__y, *__z); // x <= y && y < z
3793 } // x <= y && y <= z
3795 // stable, 3-6 compares, 0-5 swaps
3797 template <class _Compare, class _ForwardIterator>
3799 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3800 _ForwardIterator __x4, _Compare __c)
3802 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3803 if (__c(*__x4, *__x3))
3807 if (__c(*__x3, *__x2))
3811 if (__c(*__x2, *__x1))
3821 // stable, 4-10 compares, 0-9 swaps
3823 template <class _Compare, class _ForwardIterator>
3825 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3826 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3828 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3829 if (__c(*__x5, *__x4))
3833 if (__c(*__x4, *__x3))
3837 if (__c(*__x3, *__x2))
3841 if (__c(*__x2, *__x1))
3853 template <class _Compare, class _BirdirectionalIterator>
3855 __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3857 _BirdirectionalIterator __lm1 = __last;
3858 for (--__lm1; __first != __lm1; ++__first)
3860 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3861 typename add_lvalue_reference<_Compare>::type>
3862 (__first, __last, __comp);
3864 swap(*__first, *__i);
3868 template <class _Compare, class _BirdirectionalIterator>
3870 __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3872 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3873 if (__first != __last)
3875 _BirdirectionalIterator __i = __first;
3876 for (++__i; __i != __last; ++__i)
3878 _BirdirectionalIterator __j = __i;
3879 value_type __t(_VSTD::move(*__j));
3880 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3881 *__j = _VSTD::move(*__k);
3882 *__j = _VSTD::move(__t);
3887 template <class _Compare, class _RandomAccessIterator>
3889 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3891 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3892 _RandomAccessIterator __j = __first+2;
3893 __sort3<_Compare>(__first, __first+1, __j, __comp);
3894 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3896 if (__comp(*__i, *__j))
3898 value_type __t(_VSTD::move(*__i));
3899 _RandomAccessIterator __k = __j;
3903 *__j = _VSTD::move(*__k);
3905 } while (__j != __first && __comp(__t, *--__k));
3906 *__j = _VSTD::move(__t);
3912 template <class _Compare, class _RandomAccessIterator>
3914 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3916 switch (__last - __first)
3922 if (__comp(*--__last, *__first))
3923 swap(*__first, *__last);
3926 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3929 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3932 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3935 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3936 _RandomAccessIterator __j = __first+2;
3937 __sort3<_Compare>(__first, __first+1, __j, __comp);
3938 const unsigned __limit = 8;
3939 unsigned __count = 0;
3940 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3942 if (__comp(*__i, *__j))
3944 value_type __t(_VSTD::move(*__i));
3945 _RandomAccessIterator __k = __j;
3949 *__j = _VSTD::move(*__k);
3951 } while (__j != __first && __comp(__t, *--__k));
3952 *__j = _VSTD::move(__t);
3953 if (++__count == __limit)
3954 return ++__i == __last;
3961 template <class _Compare, class _BirdirectionalIterator>
3963 __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3964 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3966 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3967 if (__first1 != __last1)
3969 __destruct_n __d(0);
3970 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3971 value_type* __last2 = __first2;
3972 ::new(__last2) value_type(_VSTD::move(*__first1));
3973 __d.__incr((value_type*)0);
3974 for (++__last2; ++__first1 != __last1; ++__last2)
3976 value_type* __j2 = __last2;
3977 value_type* __i2 = __j2;
3978 if (__comp(*__first1, *--__i2))
3980 ::new(__j2) value_type(_VSTD::move(*__i2));
3981 __d.__incr((value_type*)0);
3982 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3983 *__j2 = _VSTD::move(*__i2);
3984 *__j2 = _VSTD::move(*__first1);
3988 ::new(__j2) value_type(_VSTD::move(*__first1));
3989 __d.__incr((value_type*)0);
3996 template <class _Compare, class _RandomAccessIterator>
3998 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4000 // _Compare is known to be a reference type
4001 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4002 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4003 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4004 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
4008 difference_type __len = __last - __first;
4015 if (__comp(*--__last, *__first))
4016 swap(*__first, *__last);
4019 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
4022 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
4025 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
4028 if (__len <= __limit)
4030 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
4034 _RandomAccessIterator __m = __first;
4035 _RandomAccessIterator __lm1 = __last;
4039 difference_type __delta;
4045 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
4051 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
4055 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4056 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4057 _RandomAccessIterator __i = __first;
4058 _RandomAccessIterator __j = __lm1;
4059 // j points beyond range to be tested, *__m is known to be <= *__lm1
4060 // The search going up is known to be guarded but the search coming down isn't.
4061 // Prime the downward search with a guard.
4062 if (!__comp(*__i, *__m)) // if *__first == *__m
4064 // *__first == *__m, *__first doesn't go in first part
4065 // manually guard downward moving __j against __i
4070 // *__first == *__m, *__m <= all other elements
4071 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4072 ++__i; // __first + 1
4074 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4079 return; // [__first, __last) all equivalent elements
4080 if (__comp(*__first, *__i))
4090 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4095 while (!__comp(*__first, *__i))
4097 while (__comp(*__first, *--__j))
4105 // [__first, __i) == *__first and *__first < [__i, __last)
4106 // The first part is sorted, sort the secod part
4107 // _VSTD::__sort<_Compare>(__i, __last, __comp);
4111 if (__comp(*__j, *__m))
4115 break; // found guard for downward moving __j, now use unguarded partition
4119 // It is known that *__i < *__m
4121 // j points beyond range to be tested, *__m is known to be <= *__lm1
4122 // if not yet partitioned...
4125 // known that *(__i - 1) < *__m
4126 // known that __i <= __m
4129 // __m still guards upward moving __i
4130 while (__comp(*__i, *__m))
4132 // It is now known that a guard exists for downward moving __j
4133 while (!__comp(*--__j, *__m))
4139 // It is known that __m != __j
4140 // If __m just moved, follow it
4146 // [__first, __i) < *__m and *__m <= [__i, __last)
4147 if (__i != __m && __comp(*__m, *__i))
4152 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4153 // If we were given a perfect partition, see if insertion sort is quick...
4156 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4157 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4173 // sort smaller range with recursive call and larger with tail recursion elimination
4174 if (__i - __first < __last - __i)
4176 _VSTD::__sort<_Compare>(__first, __i, __comp);
4177 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4182 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4183 // _VSTD::__sort<_Compare>(__first, __i, __comp);
4189 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4190 template <class _RandomAccessIterator, class _Compare>
4191 inline _LIBCPP_INLINE_VISIBILITY
4193 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4195 #ifdef _LIBCPP_DEBUG
4196 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4197 __debug_less<_Compare> __c(__comp);
4198 __sort<_Comp_ref>(__first, __last, __c);
4199 #else // _LIBCPP_DEBUG
4200 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4201 __sort<_Comp_ref>(__first, __last, __comp);
4202 #endif // _LIBCPP_DEBUG
4205 template <class _RandomAccessIterator>
4206 inline _LIBCPP_INLINE_VISIBILITY
4208 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4210 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4213 template <class _Tp>
4214 inline _LIBCPP_INLINE_VISIBILITY
4216 sort(_Tp** __first, _Tp** __last)
4218 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4221 template <class _Tp>
4222 inline _LIBCPP_INLINE_VISIBILITY
4224 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4226 _VSTD::sort(__first.base(), __last.base());
4229 template <class _Tp, class _Compare>
4230 inline _LIBCPP_INLINE_VISIBILITY
4232 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4234 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4235 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4238 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4239 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4240 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4241 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4242 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4243 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4244 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4245 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4246 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4247 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4248 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4249 _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>&))
4250 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4251 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4252 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4254 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4255 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4256 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4257 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4258 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4259 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4260 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4261 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4262 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4263 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4264 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4265 _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>&))
4266 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4267 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4268 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4270 _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>&))
4274 template <class _Compare, class _ForwardIterator, class _Tp>
4276 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4278 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4279 difference_type __len = _VSTD::distance(__first, __last);
4282 difference_type __l2 = __len / 2;
4283 _ForwardIterator __m = __first;
4284 _VSTD::advance(__m, __l2);
4285 if (__comp(*__m, __value_))
4296 template <class _ForwardIterator, class _Tp, class _Compare>
4297 inline _LIBCPP_INLINE_VISIBILITY
4299 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4301 #ifdef _LIBCPP_DEBUG
4302 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4303 __debug_less<_Compare> __c(__comp);
4304 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4305 #else // _LIBCPP_DEBUG
4306 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4307 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4308 #endif // _LIBCPP_DEBUG
4311 template <class _ForwardIterator, class _Tp>
4312 inline _LIBCPP_INLINE_VISIBILITY
4314 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4316 return _VSTD::lower_bound(__first, __last, __value_,
4317 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4322 template <class _Compare, class _ForwardIterator, class _Tp>
4324 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4326 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4327 difference_type __len = _VSTD::distance(__first, __last);
4330 difference_type __l2 = __len / 2;
4331 _ForwardIterator __m = __first;
4332 _VSTD::advance(__m, __l2);
4333 if (__comp(__value_, *__m))
4344 template <class _ForwardIterator, class _Tp, class _Compare>
4345 inline _LIBCPP_INLINE_VISIBILITY
4347 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4349 #ifdef _LIBCPP_DEBUG
4350 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4351 __debug_less<_Compare> __c(__comp);
4352 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4353 #else // _LIBCPP_DEBUG
4354 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4355 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4356 #endif // _LIBCPP_DEBUG
4359 template <class _ForwardIterator, class _Tp>
4360 inline _LIBCPP_INLINE_VISIBILITY
4362 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4364 return _VSTD::upper_bound(__first, __last, __value_,
4365 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4370 template <class _Compare, class _ForwardIterator, class _Tp>
4371 pair<_ForwardIterator, _ForwardIterator>
4372 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4374 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4375 difference_type __len = _VSTD::distance(__first, __last);
4378 difference_type __l2 = __len / 2;
4379 _ForwardIterator __m = __first;
4380 _VSTD::advance(__m, __l2);
4381 if (__comp(*__m, __value_))
4386 else if (__comp(__value_, *__m))
4393 _ForwardIterator __mp1 = __m;
4394 return pair<_ForwardIterator, _ForwardIterator>
4396 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4397 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4401 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4404 template <class _ForwardIterator, class _Tp, class _Compare>
4405 inline _LIBCPP_INLINE_VISIBILITY
4406 pair<_ForwardIterator, _ForwardIterator>
4407 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4409 #ifdef _LIBCPP_DEBUG
4410 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4411 __debug_less<_Compare> __c(__comp);
4412 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4413 #else // _LIBCPP_DEBUG
4414 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4415 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4416 #endif // _LIBCPP_DEBUG
4419 template <class _ForwardIterator, class _Tp>
4420 inline _LIBCPP_INLINE_VISIBILITY
4421 pair<_ForwardIterator, _ForwardIterator>
4422 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4424 return _VSTD::equal_range(__first, __last, __value_,
4425 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4430 template <class _Compare, class _ForwardIterator, class _Tp>
4431 inline _LIBCPP_INLINE_VISIBILITY
4433 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4435 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4436 return __first != __last && !__comp(__value_, *__first);
4439 template <class _ForwardIterator, class _Tp, class _Compare>
4440 inline _LIBCPP_INLINE_VISIBILITY
4442 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4444 #ifdef _LIBCPP_DEBUG
4445 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4446 __debug_less<_Compare> __c(__comp);
4447 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4448 #else // _LIBCPP_DEBUG
4449 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4450 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4451 #endif // _LIBCPP_DEBUG
4454 template <class _ForwardIterator, class _Tp>
4455 inline _LIBCPP_INLINE_VISIBILITY
4457 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4459 return _VSTD::binary_search(__first, __last, __value_,
4460 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4465 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4467 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4468 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4470 for (; __first1 != __last1; ++__result)
4472 if (__first2 == __last2)
4473 return _VSTD::copy(__first1, __last1, __result);
4474 if (__comp(*__first2, *__first1))
4476 *__result = *__first2;
4481 *__result = *__first1;
4485 return _VSTD::copy(__first2, __last2, __result);
4488 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4489 inline _LIBCPP_INLINE_VISIBILITY
4491 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4492 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4494 #ifdef _LIBCPP_DEBUG
4495 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4496 __debug_less<_Compare> __c(__comp);
4497 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4498 #else // _LIBCPP_DEBUG
4499 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4500 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4501 #endif // _LIBCPP_DEBUG
4504 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4505 inline _LIBCPP_INLINE_VISIBILITY
4507 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4508 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4510 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4511 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4512 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4517 template <class _Compare, class _InputIterator1, class _InputIterator2,
4518 class _OutputIterator>
4519 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4520 _InputIterator2 __first2, _InputIterator2 __last2,
4521 _OutputIterator __result, _Compare __comp)
4523 for (; __first1 != __last1; ++__result)
4525 if (__first2 == __last2)
4527 _VSTD::move(__first1, __last1, __result);
4531 if (__comp(*__first2, *__first1))
4533 *__result = _VSTD::move(*__first2);
4538 *__result = _VSTD::move(*__first1);
4542 // __first2 through __last2 are already in the right spot.
4545 template <class _Compare, class _BidirectionalIterator>
4547 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4548 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4549 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4550 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4552 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4553 __destruct_n __d(0);
4554 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4555 if (__len1 <= __len2)
4557 value_type* __p = __buff;
4558 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4559 ::new(__p) value_type(_VSTD::move(*__i));
4560 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
4564 value_type* __p = __buff;
4565 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4566 ::new(__p) value_type(_VSTD::move(*__i));
4567 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4568 typedef reverse_iterator<value_type*> _Rv;
4569 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4570 _RBi(__middle), _RBi(__first),
4571 _RBi(__last), __invert<_Compare>(__comp));
4575 template <class _Compare, class _BidirectionalIterator>
4577 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4578 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4579 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4580 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4582 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4585 // if __middle == __last, we're done
4588 if (__len1 <= __buff_size || __len2 <= __buff_size)
4589 return __buffered_inplace_merge<_Compare>
4590 (__first, __middle, __last, __comp, __len1, __len2, __buff);
4591 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4592 for (; true; ++__first, (void) --__len1)
4596 if (__comp(*__middle, *__first))
4599 // __first < __middle < __last
4600 // *__first > *__middle
4601 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4603 // [__first, __m1) <= [__middle, __m2)
4604 // [__middle, __m2) < [__m1, __middle)
4605 // [__m1, __middle) <= [__m2, __last)
4606 // and __m1 or __m2 is in the middle of its range
4607 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4608 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4609 difference_type __len11; // distance(__first, __m1)
4610 difference_type __len21; // distance(__middle, __m2)
4611 // binary search smaller range
4612 if (__len1 < __len2)
4613 { // __len >= 1, __len2 >= 2
4614 __len21 = __len2 / 2;
4616 _VSTD::advance(__m2, __len21);
4617 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4618 __len11 = _VSTD::distance(__first, __m1);
4623 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4624 // It is known *__first > *__middle
4625 swap(*__first, *__middle);
4628 // __len1 >= 2, __len2 >= 1
4629 __len11 = __len1 / 2;
4631 _VSTD::advance(__m1, __len11);
4632 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4633 __len21 = _VSTD::distance(__middle, __m2);
4635 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4636 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4637 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4638 // swap middle two partitions
4639 __middle = _VSTD::rotate(__m1, __middle, __m2);
4640 // __len12 and __len21 now have swapped meanings
4641 // merge smaller range with recurisve call and larger with tail recursion elimination
4642 if (__len11 + __len21 < __len12 + __len22)
4644 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4645 // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4653 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4654 // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4663 template <class _BidirectionalIterator, class _Compare>
4664 inline _LIBCPP_INLINE_VISIBILITY
4666 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4669 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4670 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4671 difference_type __len1 = _VSTD::distance(__first, __middle);
4672 difference_type __len2 = _VSTD::distance(__middle, __last);
4673 difference_type __buf_size = _VSTD::min(__len1, __len2);
4674 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4675 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4677 #ifdef _LIBCPP_DEBUG
4678 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4679 __debug_less<_Compare> __c(__comp);
4680 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4681 __buf.first, __buf.second);
4682 #else // _LIBCPP_DEBUG
4683 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4684 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4685 __buf.first, __buf.second);
4686 #endif // _LIBCPP_DEBUG
4689 template <class _BidirectionalIterator>
4690 inline _LIBCPP_INLINE_VISIBILITY
4692 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4694 _VSTD::inplace_merge(__first, __middle, __last,
4695 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4700 template <class _Compare, class _InputIterator1, class _InputIterator2>
4702 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4703 _InputIterator2 __first2, _InputIterator2 __last2,
4704 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4706 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4707 __destruct_n __d(0);
4708 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4709 for (; true; ++__result)
4711 if (__first1 == __last1)
4713 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4714 ::new (__result) value_type(_VSTD::move(*__first2));
4718 if (__first2 == __last2)
4720 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4721 ::new (__result) value_type(_VSTD::move(*__first1));
4725 if (__comp(*__first2, *__first1))
4727 ::new (__result) value_type(_VSTD::move(*__first2));
4728 __d.__incr((value_type*)0);
4733 ::new (__result) value_type(_VSTD::move(*__first1));
4734 __d.__incr((value_type*)0);
4740 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4742 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4743 _InputIterator2 __first2, _InputIterator2 __last2,
4744 _OutputIterator __result, _Compare __comp)
4746 for (; __first1 != __last1; ++__result)
4748 if (__first2 == __last2)
4750 for (; __first1 != __last1; ++__first1, ++__result)
4751 *__result = _VSTD::move(*__first1);
4754 if (__comp(*__first2, *__first1))
4756 *__result = _VSTD::move(*__first2);
4761 *__result = _VSTD::move(*__first1);
4765 for (; __first2 != __last2; ++__first2, ++__result)
4766 *__result = _VSTD::move(*__first2);
4769 template <class _Compare, class _RandomAccessIterator>
4771 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4772 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4773 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4775 template <class _Compare, class _RandomAccessIterator>
4777 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4778 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4779 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4781 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4787 ::new(__first2) value_type(_VSTD::move(*__first1));
4790 __destruct_n __d(0);
4791 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4792 if (__comp(*--__last1, *__first1))
4794 ::new(__first2) value_type(_VSTD::move(*__last1));
4795 __d.__incr((value_type*)0);
4797 ::new(__first2) value_type(_VSTD::move(*__first1));
4801 ::new(__first2) value_type(_VSTD::move(*__first1));
4802 __d.__incr((value_type*)0);
4804 ::new(__first2) value_type(_VSTD::move(*__last1));
4811 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4814 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4815 _RandomAccessIterator __m = __first1 + __l2;
4816 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4817 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4818 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4821 template <class _Tp>
4822 struct __stable_sort_switch
4824 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4827 template <class _Compare, class _RandomAccessIterator>
4829 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4830 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4831 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4833 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4834 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4841 if (__comp(*--__last, *__first))
4842 swap(*__first, *__last);
4845 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4847 __insertion_sort<_Compare>(__first, __last, __comp);
4850 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4851 _RandomAccessIterator __m = __first + __l2;
4852 if (__len <= __buff_size)
4854 __destruct_n __d(0);
4855 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4856 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4857 __d.__set(__l2, (value_type*)0);
4858 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4859 __d.__set(__len, (value_type*)0);
4860 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4861 // __merge<_Compare>(move_iterator<value_type*>(__buff),
4862 // move_iterator<value_type*>(__buff + __l2),
4863 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4864 // move_iterator<_RandomAccessIterator>(__buff + __len),
4865 // __first, __comp);
4868 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4869 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4870 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4873 template <class _RandomAccessIterator, class _Compare>
4874 inline _LIBCPP_INLINE_VISIBILITY
4876 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4878 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4879 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4880 difference_type __len = __last - __first;
4881 pair<value_type*, ptrdiff_t> __buf(0, 0);
4882 unique_ptr<value_type, __return_temporary_buffer> __h;
4883 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4885 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4886 __h.reset(__buf.first);
4888 #ifdef _LIBCPP_DEBUG
4889 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4890 __debug_less<_Compare> __c(__comp);
4891 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4892 #else // _LIBCPP_DEBUG
4893 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4894 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4895 #endif // _LIBCPP_DEBUG
4898 template <class _RandomAccessIterator>
4899 inline _LIBCPP_INLINE_VISIBILITY
4901 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4903 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4908 template <class _RandomAccessIterator, class _Compare>
4909 _RandomAccessIterator
4910 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4912 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4913 difference_type __len = __last - __first;
4914 difference_type __p = 0;
4915 difference_type __c = 1;
4916 _RandomAccessIterator __pp = __first;
4919 _RandomAccessIterator __cp = __first + __c;
4920 if (__comp(*__pp, *__cp))
4926 if (__comp(*__pp, *__cp))
4935 template<class _RandomAccessIterator>
4936 inline _LIBCPP_INLINE_VISIBILITY
4937 _RandomAccessIterator
4938 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4940 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4945 template <class _RandomAccessIterator, class _Compare>
4946 inline _LIBCPP_INLINE_VISIBILITY
4948 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4950 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4953 template<class _RandomAccessIterator>
4954 inline _LIBCPP_INLINE_VISIBILITY
4956 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4958 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4963 template <class _Compare, class _RandomAccessIterator>
4965 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4966 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4968 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4971 __len = (__len - 2) / 2;
4972 _RandomAccessIterator __ptr = __first + __len;
4973 if (__comp(*__ptr, *--__last))
4975 value_type __t(_VSTD::move(*__last));
4978 *__last = _VSTD::move(*__ptr);
4982 __len = (__len - 1) / 2;
4983 __ptr = __first + __len;
4984 } while (__comp(*__ptr, __t));
4985 *__last = _VSTD::move(__t);
4990 template <class _RandomAccessIterator, class _Compare>
4991 inline _LIBCPP_INLINE_VISIBILITY
4993 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4995 #ifdef _LIBCPP_DEBUG
4996 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4997 __debug_less<_Compare> __c(__comp);
4998 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
4999 #else // _LIBCPP_DEBUG
5000 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5001 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
5002 #endif // _LIBCPP_DEBUG
5005 template <class _RandomAccessIterator>
5006 inline _LIBCPP_INLINE_VISIBILITY
5008 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5010 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5015 template <class _Compare, class _RandomAccessIterator>
5017 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
5019 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5020 _RandomAccessIterator __start)
5022 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5023 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5024 // left-child of __start is at 2 * __start + 1
5025 // right-child of __start is at 2 * __start + 2
5026 difference_type __child = __start - __first;
5028 if (__len < 2 || (__len - 2) / 2 < __child)
5031 __child = 2 * __child + 1;
5032 _RandomAccessIterator __child_i = __first + __child;
5034 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5035 // right-child exists and is greater than left-child
5040 // check if we are in heap-order
5041 if (__comp(*__child_i, *__start))
5042 // we are, __start is larger than it's largest child
5045 value_type __top(_VSTD::move(*__start));
5048 // we are not in heap-order, swap the parent with it's largest child
5049 *__start = _VSTD::move(*__child_i);
5050 __start = __child_i;
5052 if ((__len - 2) / 2 < __child)
5055 // recompute the child based off of the updated parent
5056 __child = 2 * __child + 1;
5057 __child_i = __first + __child;
5059 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5060 // right-child exists and is greater than left-child
5065 // check if we are in heap-order
5066 } while (!__comp(*__child_i, __top));
5067 *__start = _VSTD::move(__top);
5070 template <class _Compare, class _RandomAccessIterator>
5071 inline _LIBCPP_INLINE_VISIBILITY
5073 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5074 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5078 swap(*__first, *--__last);
5079 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
5083 template <class _RandomAccessIterator, class _Compare>
5084 inline _LIBCPP_INLINE_VISIBILITY
5086 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5088 #ifdef _LIBCPP_DEBUG
5089 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5090 __debug_less<_Compare> __c(__comp);
5091 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
5092 #else // _LIBCPP_DEBUG
5093 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5094 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
5095 #endif // _LIBCPP_DEBUG
5098 template <class _RandomAccessIterator>
5099 inline _LIBCPP_INLINE_VISIBILITY
5101 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5103 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5108 template <class _Compare, class _RandomAccessIterator>
5110 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5112 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5113 difference_type __n = __last - __first;
5116 // start from the first parent, there is no need to consider children
5117 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5119 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5124 template <class _RandomAccessIterator, class _Compare>
5125 inline _LIBCPP_INLINE_VISIBILITY
5127 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5129 #ifdef _LIBCPP_DEBUG
5130 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5131 __debug_less<_Compare> __c(__comp);
5132 __make_heap<_Comp_ref>(__first, __last, __c);
5133 #else // _LIBCPP_DEBUG
5134 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5135 __make_heap<_Comp_ref>(__first, __last, __comp);
5136 #endif // _LIBCPP_DEBUG
5139 template <class _RandomAccessIterator>
5140 inline _LIBCPP_INLINE_VISIBILITY
5142 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5144 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5149 template <class _Compare, class _RandomAccessIterator>
5151 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5153 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5154 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5155 __pop_heap<_Compare>(__first, __last, __comp, __n);
5158 template <class _RandomAccessIterator, class _Compare>
5159 inline _LIBCPP_INLINE_VISIBILITY
5161 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5163 #ifdef _LIBCPP_DEBUG
5164 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5165 __debug_less<_Compare> __c(__comp);
5166 __sort_heap<_Comp_ref>(__first, __last, __c);
5167 #else // _LIBCPP_DEBUG
5168 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5169 __sort_heap<_Comp_ref>(__first, __last, __comp);
5170 #endif // _LIBCPP_DEBUG
5173 template <class _RandomAccessIterator>
5174 inline _LIBCPP_INLINE_VISIBILITY
5176 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5178 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5183 template <class _Compare, class _RandomAccessIterator>
5185 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5188 __make_heap<_Compare>(__first, __middle, __comp);
5189 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5190 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5192 if (__comp(*__i, *__first))
5194 swap(*__i, *__first);
5195 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5198 __sort_heap<_Compare>(__first, __middle, __comp);
5201 template <class _RandomAccessIterator, class _Compare>
5202 inline _LIBCPP_INLINE_VISIBILITY
5204 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5207 #ifdef _LIBCPP_DEBUG
5208 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5209 __debug_less<_Compare> __c(__comp);
5210 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
5211 #else // _LIBCPP_DEBUG
5212 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5213 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5214 #endif // _LIBCPP_DEBUG
5217 template <class _RandomAccessIterator>
5218 inline _LIBCPP_INLINE_VISIBILITY
5220 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5222 _VSTD::partial_sort(__first, __middle, __last,
5223 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5226 // partial_sort_copy
5228 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5229 _RandomAccessIterator
5230 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5231 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5233 _RandomAccessIterator __r = __result_first;
5234 if (__r != __result_last)
5236 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5238 __make_heap<_Compare>(__result_first, __r, __comp);
5239 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5240 for (; __first != __last; ++__first)
5241 if (__comp(*__first, *__result_first))
5243 *__result_first = *__first;
5244 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5246 __sort_heap<_Compare>(__result_first, __r, __comp);
5251 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5252 inline _LIBCPP_INLINE_VISIBILITY
5253 _RandomAccessIterator
5254 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5255 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5257 #ifdef _LIBCPP_DEBUG
5258 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5259 __debug_less<_Compare> __c(__comp);
5260 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
5261 #else // _LIBCPP_DEBUG
5262 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5263 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5264 #endif // _LIBCPP_DEBUG
5267 template <class _InputIterator, class _RandomAccessIterator>
5268 inline _LIBCPP_INLINE_VISIBILITY
5269 _RandomAccessIterator
5270 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5271 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5273 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5274 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5279 template <class _Compare, class _RandomAccessIterator>
5281 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5283 // _Compare is known to be a reference type
5284 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5285 const difference_type __limit = 7;
5289 if (__nth == __last)
5291 difference_type __len = __last - __first;
5298 if (__comp(*--__last, *__first))
5299 swap(*__first, *__last);
5303 _RandomAccessIterator __m = __first;
5304 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5308 if (__len <= __limit)
5310 __selection_sort<_Compare>(__first, __last, __comp);
5313 // __len > __limit >= 3
5314 _RandomAccessIterator __m = __first + __len/2;
5315 _RandomAccessIterator __lm1 = __last;
5316 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5318 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5319 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5320 _RandomAccessIterator __i = __first;
5321 _RandomAccessIterator __j = __lm1;
5322 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5323 // The search going up is known to be guarded but the search coming down isn't.
5324 // Prime the downward search with a guard.
5325 if (!__comp(*__i, *__m)) // if *__first == *__m
5327 // *__first == *__m, *__first doesn't go in first part
5328 // manually guard downward moving __j against __i
5333 // *__first == *__m, *__m <= all other elements
5334 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5335 ++__i; // __first + 1
5337 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5342 return; // [__first, __last) all equivalent elements
5343 if (__comp(*__first, *__i))
5353 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5358 while (!__comp(*__first, *__i))
5360 while (__comp(*__first, *--__j))
5368 // [__first, __i) == *__first and *__first < [__i, __last)
5369 // The first part is sorted,
5372 // __nth_element the secod part
5373 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5377 if (__comp(*__j, *__m))
5381 break; // found guard for downward moving __j, now use unguarded partition
5386 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5387 // if not yet partitioned...
5390 // known that *(__i - 1) < *__m
5393 // __m still guards upward moving __i
5394 while (__comp(*__i, *__m))
5396 // It is now known that a guard exists for downward moving __j
5397 while (!__comp(*--__j, *__m))
5403 // It is known that __m != __j
5404 // If __m just moved, follow it
5410 // [__first, __i) < *__m and *__m <= [__i, __last)
5411 if (__i != __m && __comp(*__m, *__i))
5416 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5421 // We were given a perfectly partitioned sequence. Coincidence?
5424 // Check for [__first, __i) already sorted
5425 __j = __m = __first;
5426 while (++__j != __i)
5428 if (__comp(*__j, *__m))
5429 // not yet sorted, so sort
5433 // [__first, __i) sorted
5438 // Check for [__i, __last) already sorted
5440 while (++__j != __last)
5442 if (__comp(*__j, *__m))
5443 // not yet sorted, so sort
5447 // [__i, __last) sorted
5452 // __nth_element on range containing __nth
5455 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5460 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5466 template <class _RandomAccessIterator, class _Compare>
5467 inline _LIBCPP_INLINE_VISIBILITY
5469 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5471 #ifdef _LIBCPP_DEBUG
5472 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5473 __debug_less<_Compare> __c(__comp);
5474 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5475 #else // _LIBCPP_DEBUG
5476 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5477 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5478 #endif // _LIBCPP_DEBUG
5481 template <class _RandomAccessIterator>
5482 inline _LIBCPP_INLINE_VISIBILITY
5484 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5486 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5491 template <class _Compare, class _InputIterator1, class _InputIterator2>
5493 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5496 for (; __first2 != __last2; ++__first1)
5498 if (__first1 == __last1 || __comp(*__first2, *__first1))
5500 if (!__comp(*__first1, *__first2))
5506 template <class _InputIterator1, class _InputIterator2, class _Compare>
5507 inline _LIBCPP_INLINE_VISIBILITY
5509 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5512 #ifdef _LIBCPP_DEBUG
5513 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5514 __debug_less<_Compare> __c(__comp);
5515 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5516 #else // _LIBCPP_DEBUG
5517 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5518 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5519 #endif // _LIBCPP_DEBUG
5522 template <class _InputIterator1, class _InputIterator2>
5523 inline _LIBCPP_INLINE_VISIBILITY
5525 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5527 return _VSTD::includes(__first1, __last1, __first2, __last2,
5528 __less<typename iterator_traits<_InputIterator1>::value_type,
5529 typename iterator_traits<_InputIterator2>::value_type>());
5534 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5536 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5537 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5539 for (; __first1 != __last1; ++__result)
5541 if (__first2 == __last2)
5542 return _VSTD::copy(__first1, __last1, __result);
5543 if (__comp(*__first2, *__first1))
5545 *__result = *__first2;
5550 if (!__comp(*__first1, *__first2))
5552 *__result = *__first1;
5556 return _VSTD::copy(__first2, __last2, __result);
5559 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5560 inline _LIBCPP_INLINE_VISIBILITY
5562 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5563 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5565 #ifdef _LIBCPP_DEBUG
5566 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5567 __debug_less<_Compare> __c(__comp);
5568 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5569 #else // _LIBCPP_DEBUG
5570 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5571 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5572 #endif // _LIBCPP_DEBUG
5575 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5576 inline _LIBCPP_INLINE_VISIBILITY
5578 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5579 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5581 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5582 __less<typename iterator_traits<_InputIterator1>::value_type,
5583 typename iterator_traits<_InputIterator2>::value_type>());
5588 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5590 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5591 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5593 while (__first1 != __last1 && __first2 != __last2)
5595 if (__comp(*__first1, *__first2))
5599 if (!__comp(*__first2, *__first1))
5601 *__result = *__first1;
5611 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5612 inline _LIBCPP_INLINE_VISIBILITY
5614 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5615 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5617 #ifdef _LIBCPP_DEBUG
5618 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5619 __debug_less<_Compare> __c(__comp);
5620 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5621 #else // _LIBCPP_DEBUG
5622 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5623 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5624 #endif // _LIBCPP_DEBUG
5627 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5628 inline _LIBCPP_INLINE_VISIBILITY
5630 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5631 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5633 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5634 __less<typename iterator_traits<_InputIterator1>::value_type,
5635 typename iterator_traits<_InputIterator2>::value_type>());
5640 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5642 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5643 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5645 while (__first1 != __last1)
5647 if (__first2 == __last2)
5648 return _VSTD::copy(__first1, __last1, __result);
5649 if (__comp(*__first1, *__first2))
5651 *__result = *__first1;
5657 if (!__comp(*__first2, *__first1))
5665 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5666 inline _LIBCPP_INLINE_VISIBILITY
5668 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5669 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5671 #ifdef _LIBCPP_DEBUG
5672 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5673 __debug_less<_Compare> __c(__comp);
5674 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5675 #else // _LIBCPP_DEBUG
5676 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5677 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5678 #endif // _LIBCPP_DEBUG
5681 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5682 inline _LIBCPP_INLINE_VISIBILITY
5684 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5685 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5687 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5688 __less<typename iterator_traits<_InputIterator1>::value_type,
5689 typename iterator_traits<_InputIterator2>::value_type>());
5692 // set_symmetric_difference
5694 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5696 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5697 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5699 while (__first1 != __last1)
5701 if (__first2 == __last2)
5702 return _VSTD::copy(__first1, __last1, __result);
5703 if (__comp(*__first1, *__first2))
5705 *__result = *__first1;
5711 if (__comp(*__first2, *__first1))
5713 *__result = *__first2;
5721 return _VSTD::copy(__first2, __last2, __result);
5724 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5725 inline _LIBCPP_INLINE_VISIBILITY
5727 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5728 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5730 #ifdef _LIBCPP_DEBUG
5731 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5732 __debug_less<_Compare> __c(__comp);
5733 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5734 #else // _LIBCPP_DEBUG
5735 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5736 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5737 #endif // _LIBCPP_DEBUG
5740 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5741 inline _LIBCPP_INLINE_VISIBILITY
5743 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5744 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5746 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5747 __less<typename iterator_traits<_InputIterator1>::value_type,
5748 typename iterator_traits<_InputIterator2>::value_type>());
5751 // lexicographical_compare
5753 template <class _Compare, class _InputIterator1, class _InputIterator2>
5755 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5756 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5758 for (; __first2 != __last2; ++__first1, (void) ++__first2)
5760 if (__first1 == __last1 || __comp(*__first1, *__first2))
5762 if (__comp(*__first2, *__first1))
5768 template <class _InputIterator1, class _InputIterator2, class _Compare>
5769 inline _LIBCPP_INLINE_VISIBILITY
5771 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5772 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5774 #ifdef _LIBCPP_DEBUG
5775 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5776 __debug_less<_Compare> __c(__comp);
5777 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5778 #else // _LIBCPP_DEBUG
5779 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5780 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5781 #endif // _LIBCPP_DEBUG
5784 template <class _InputIterator1, class _InputIterator2>
5785 inline _LIBCPP_INLINE_VISIBILITY
5787 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5788 _InputIterator2 __first2, _InputIterator2 __last2)
5790 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5791 __less<typename iterator_traits<_InputIterator1>::value_type,
5792 typename iterator_traits<_InputIterator2>::value_type>());
5797 template <class _Compare, class _BidirectionalIterator>
5799 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5801 _BidirectionalIterator __i = __last;
5802 if (__first == __last || __first == --__i)
5806 _BidirectionalIterator __ip1 = __i;
5807 if (__comp(*--__i, *__ip1))
5809 _BidirectionalIterator __j = __last;
5810 while (!__comp(*__i, *--__j))
5813 _VSTD::reverse(__ip1, __last);
5818 _VSTD::reverse(__first, __last);
5824 template <class _BidirectionalIterator, class _Compare>
5825 inline _LIBCPP_INLINE_VISIBILITY
5827 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5829 #ifdef _LIBCPP_DEBUG
5830 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5831 __debug_less<_Compare> __c(__comp);
5832 return __next_permutation<_Comp_ref>(__first, __last, __c);
5833 #else // _LIBCPP_DEBUG
5834 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5835 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5836 #endif // _LIBCPP_DEBUG
5839 template <class _BidirectionalIterator>
5840 inline _LIBCPP_INLINE_VISIBILITY
5842 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5844 return _VSTD::next_permutation(__first, __last,
5845 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5850 template <class _Compare, class _BidirectionalIterator>
5852 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5854 _BidirectionalIterator __i = __last;
5855 if (__first == __last || __first == --__i)
5859 _BidirectionalIterator __ip1 = __i;
5860 if (__comp(*__ip1, *--__i))
5862 _BidirectionalIterator __j = __last;
5863 while (!__comp(*--__j, *__i))
5866 _VSTD::reverse(__ip1, __last);
5871 _VSTD::reverse(__first, __last);
5877 template <class _BidirectionalIterator, class _Compare>
5878 inline _LIBCPP_INLINE_VISIBILITY
5880 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5882 #ifdef _LIBCPP_DEBUG
5883 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5884 __debug_less<_Compare> __c(__comp);
5885 return __prev_permutation<_Comp_ref>(__first, __last, __c);
5886 #else // _LIBCPP_DEBUG
5887 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5888 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5889 #endif // _LIBCPP_DEBUG
5892 template <class _BidirectionalIterator>
5893 inline _LIBCPP_INLINE_VISIBILITY
5895 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5897 return _VSTD::prev_permutation(__first, __last,
5898 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5901 _LIBCPP_END_NAMESPACE_STD
5905 #endif // _LIBCPP_ALGORITHM