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 T>
40 find(InputIterator first, InputIterator last, const T& value);
42 template <class InputIterator, class Predicate>
44 find_if(InputIterator first, InputIterator last, Predicate pred);
46 template<class InputIterator, class Predicate>
48 find_if_not(InputIterator first, InputIterator last, Predicate pred);
50 template <class ForwardIterator1, class ForwardIterator2>
52 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53 ForwardIterator2 first2, ForwardIterator2 last2);
55 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
57 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
60 template <class ForwardIterator1, class ForwardIterator2>
62 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63 ForwardIterator2 first2, ForwardIterator2 last2);
65 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
67 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
70 template <class ForwardIterator>
72 adjacent_find(ForwardIterator first, ForwardIterator last);
74 template <class ForwardIterator, class BinaryPredicate>
76 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
78 template <class InputIterator, class T>
79 typename iterator_traits<InputIterator>::difference_type
80 count(InputIterator first, InputIterator last, const T& value);
82 template <class InputIterator, class Predicate>
83 typename iterator_traits<InputIterator>::difference_type
84 count_if(InputIterator first, InputIterator last, Predicate pred);
86 template <class InputIterator1, class InputIterator2>
87 pair<InputIterator1, InputIterator2>
88 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
90 template <class InputIterator1, class InputIterator2>
91 pair<InputIterator1, InputIterator2>
92 mismatch(InputIterator1 first1, InputIterator1 last1,
93 InputIterator2 first2, InputIterator2 last2); // **C++14**
95 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
96 pair<InputIterator1, InputIterator2>
97 mismatch(InputIterator1 first1, InputIterator1 last1,
98 InputIterator2 first2, BinaryPredicate pred);
100 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
101 pair<InputIterator1, InputIterator2>
102 mismatch(InputIterator1 first1, InputIterator1 last1,
103 InputIterator2 first2, InputIterator2 last2,
104 BinaryPredicate pred); // **C++14**
106 template <class InputIterator1, class InputIterator2>
108 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
110 template <class InputIterator1, class InputIterator2>
112 equal(InputIterator1 first1, InputIterator1 last1,
113 InputIterator2 first2, InputIterator2 last2); // **C++14**
115 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
117 equal(InputIterator1 first1, InputIterator1 last1,
118 InputIterator2 first2, BinaryPredicate pred);
120 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
122 equal(InputIterator1 first1, InputIterator1 last1,
123 InputIterator2 first2, InputIterator2 last2,
124 BinaryPredicate pred); // **C++14**
126 template<class ForwardIterator1, class ForwardIterator2>
128 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129 ForwardIterator2 first2);
131 template<class ForwardIterator1, class ForwardIterator2>
133 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
136 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
138 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139 ForwardIterator2 first2, BinaryPredicate pred);
141 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
143 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144 ForwardIterator2 first2, ForwardIterator2 last2,
145 BinaryPredicate pred); // **C++14**
147 template <class ForwardIterator1, class ForwardIterator2>
149 search(ForwardIterator1 first1, ForwardIterator1 last1,
150 ForwardIterator2 first2, ForwardIterator2 last2);
152 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
154 search(ForwardIterator1 first1, ForwardIterator1 last1,
155 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
157 template <class ForwardIterator, class Size, class T>
159 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
161 template <class ForwardIterator, class Size, class T, class BinaryPredicate>
163 search_n(ForwardIterator first, ForwardIterator last,
164 Size count, const T& value, BinaryPredicate pred);
166 template <class InputIterator, class OutputIterator>
168 copy(InputIterator first, InputIterator last, OutputIterator result);
170 template<class InputIterator, class OutputIterator, class Predicate>
172 copy_if(InputIterator first, InputIterator last,
173 OutputIterator result, Predicate pred);
175 template<class InputIterator, class Size, class OutputIterator>
177 copy_n(InputIterator first, Size n, OutputIterator result);
179 template <class BidirectionalIterator1, class BidirectionalIterator2>
180 BidirectionalIterator2
181 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182 BidirectionalIterator2 result);
184 template <class ForwardIterator1, class ForwardIterator2>
186 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
188 template <class ForwardIterator1, class ForwardIterator2>
190 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
192 template <class InputIterator, class OutputIterator, class UnaryOperation>
194 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
196 template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
198 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199 OutputIterator result, BinaryOperation binary_op);
201 template <class ForwardIterator, class T>
203 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
205 template <class ForwardIterator, class Predicate, class T>
207 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
209 template <class InputIterator, class OutputIterator, class T>
211 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212 const T& old_value, const T& new_value);
214 template <class InputIterator, class OutputIterator, class Predicate, class T>
216 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
218 template <class ForwardIterator, class T>
220 fill(ForwardIterator first, ForwardIterator last, const T& value);
222 template <class OutputIterator, class Size, class T>
224 fill_n(OutputIterator first, Size n, const T& value);
226 template <class ForwardIterator, class Generator>
228 generate(ForwardIterator first, ForwardIterator last, Generator gen);
230 template <class OutputIterator, class Size, class Generator>
232 generate_n(OutputIterator first, Size n, Generator gen);
234 template <class ForwardIterator, class T>
236 remove(ForwardIterator first, ForwardIterator last, const T& value);
238 template <class ForwardIterator, class Predicate>
240 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
242 template <class InputIterator, class OutputIterator, class T>
244 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
246 template <class InputIterator, class OutputIterator, class Predicate>
248 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
250 template <class ForwardIterator>
252 unique(ForwardIterator first, ForwardIterator last);
254 template <class ForwardIterator, class BinaryPredicate>
256 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
258 template <class InputIterator, class OutputIterator>
260 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
262 template <class InputIterator, class OutputIterator, class BinaryPredicate>
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
266 template <class BidirectionalIterator>
268 reverse(BidirectionalIterator first, BidirectionalIterator last);
270 template <class BidirectionalIterator, class OutputIterator>
272 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
274 template <class ForwardIterator>
276 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
278 template <class ForwardIterator, class OutputIterator>
280 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
282 template <class RandomAccessIterator>
284 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
286 template <class RandomAccessIterator, class RandomNumberGenerator>
288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289 RandomNumberGenerator& rand); // deprecated in C++14
291 template<class RandomAccessIterator, class UniformRandomNumberGenerator>
292 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
293 UniformRandomNumberGenerator&& g);
295 template <class InputIterator, class Predicate>
297 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
299 template <class ForwardIterator, class Predicate>
301 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
303 template <class InputIterator, class OutputIterator1,
304 class OutputIterator2, class Predicate>
305 pair<OutputIterator1, OutputIterator2>
306 partition_copy(InputIterator first, InputIterator last,
307 OutputIterator1 out_true, OutputIterator2 out_false,
310 template <class ForwardIterator, class Predicate>
312 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
314 template<class ForwardIterator, class Predicate>
316 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
318 template <class ForwardIterator>
320 is_sorted(ForwardIterator first, ForwardIterator last);
322 template <class ForwardIterator, class Compare>
324 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
326 template<class ForwardIterator>
328 is_sorted_until(ForwardIterator first, ForwardIterator last);
330 template <class ForwardIterator, class Compare>
332 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
334 template <class RandomAccessIterator>
336 sort(RandomAccessIterator first, RandomAccessIterator last);
338 template <class RandomAccessIterator, class Compare>
340 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
342 template <class RandomAccessIterator>
344 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
346 template <class RandomAccessIterator, class Compare>
348 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
350 template <class RandomAccessIterator>
352 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
354 template <class RandomAccessIterator, class Compare>
356 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
358 template <class InputIterator, class RandomAccessIterator>
360 partial_sort_copy(InputIterator first, InputIterator last,
361 RandomAccessIterator result_first, RandomAccessIterator result_last);
363 template <class InputIterator, class RandomAccessIterator, class Compare>
365 partial_sort_copy(InputIterator first, InputIterator last,
366 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
368 template <class RandomAccessIterator>
370 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
372 template <class RandomAccessIterator, class Compare>
374 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
376 template <class ForwardIterator, class T>
378 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
380 template <class ForwardIterator, class T, class Compare>
382 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
384 template <class ForwardIterator, class T>
386 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
388 template <class ForwardIterator, class T, class Compare>
390 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
392 template <class ForwardIterator, class T>
393 pair<ForwardIterator, ForwardIterator>
394 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
396 template <class ForwardIterator, class T, class Compare>
397 pair<ForwardIterator, ForwardIterator>
398 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
400 template <class ForwardIterator, class T>
402 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
404 template <class ForwardIterator, class T, class Compare>
406 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
408 template <class InputIterator1, class InputIterator2, class OutputIterator>
410 merge(InputIterator1 first1, InputIterator1 last1,
411 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
413 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
415 merge(InputIterator1 first1, InputIterator1 last1,
416 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
418 template <class BidirectionalIterator>
420 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
422 template <class BidirectionalIterator, class Compare>
424 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
426 template <class InputIterator1, class InputIterator2>
428 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
430 template <class InputIterator1, class InputIterator2, class Compare>
432 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
434 template <class InputIterator1, class InputIterator2, class OutputIterator>
436 set_union(InputIterator1 first1, InputIterator1 last1,
437 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
439 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
441 set_union(InputIterator1 first1, InputIterator1 last1,
442 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
444 template <class InputIterator1, class InputIterator2, class OutputIterator>
446 set_intersection(InputIterator1 first1, InputIterator1 last1,
447 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
449 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
451 set_intersection(InputIterator1 first1, InputIterator1 last1,
452 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
454 template <class InputIterator1, class InputIterator2, class OutputIterator>
456 set_difference(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
459 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
461 set_difference(InputIterator1 first1, InputIterator1 last1,
462 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
464 template <class InputIterator1, class InputIterator2, class OutputIterator>
466 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
467 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
469 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
471 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
472 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
474 template <class RandomAccessIterator>
476 push_heap(RandomAccessIterator first, RandomAccessIterator last);
478 template <class RandomAccessIterator, class Compare>
480 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
482 template <class RandomAccessIterator>
484 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
486 template <class RandomAccessIterator, class Compare>
488 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
490 template <class RandomAccessIterator>
492 make_heap(RandomAccessIterator first, RandomAccessIterator last);
494 template <class RandomAccessIterator, class Compare>
496 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
498 template <class RandomAccessIterator>
500 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
502 template <class RandomAccessIterator, class Compare>
504 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
506 template <class RandomAccessIterator>
508 is_heap(RandomAccessIterator first, RandomAccessiterator last);
510 template <class RandomAccessIterator, class Compare>
512 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
514 template <class RandomAccessIterator>
516 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
518 template <class RandomAccessIterator, class Compare>
520 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
522 template <class ForwardIterator>
524 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
526 template <class ForwardIterator, class Compare>
528 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
532 min(const T& a, const T& b); // constexpr in C++14
534 template <class T, class Compare>
536 min(const T& a, const T& b, Compare comp); // constexpr in C++14
540 min(initializer_list<T> t); // constexpr in C++14
542 template<class T, class Compare>
544 min(initializer_list<T> t, Compare comp); // constexpr in C++14
546 template <class ForwardIterator>
548 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
550 template <class ForwardIterator, class Compare>
552 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
556 max(const T& a, const T& b); // constexpr in C++14
558 template <class T, class Compare>
560 max(const T& a, const T& b, Compare comp); // constexpr in C++14
564 max(initializer_list<T> t); // constexpr in C++14
566 template<class T, class Compare>
568 max(initializer_list<T> t, Compare comp); // constexpr in C++14
570 template<class ForwardIterator>
571 pair<ForwardIterator, ForwardIterator>
572 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
574 template<class ForwardIterator, class Compare>
575 pair<ForwardIterator, ForwardIterator>
576 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
579 pair<const T&, const T&>
580 minmax(const T& a, const T& b); // constexpr in C++14
582 template<class T, class Compare>
583 pair<const T&, const T&>
584 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
588 minmax(initializer_list<T> t); // constexpr in C++14
590 template<class T, class Compare>
592 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
594 template <class InputIterator1, class InputIterator2>
596 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
598 template <class InputIterator1, class InputIterator2, class Compare>
600 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
601 InputIterator2 first2, InputIterator2 last2, Compare comp);
603 template <class BidirectionalIterator>
605 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
607 template <class BidirectionalIterator, class Compare>
609 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
611 template <class BidirectionalIterator>
613 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
615 template <class BidirectionalIterator, class Compare>
617 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
624 #include <initializer_list>
625 #include <type_traits>
632 #if defined(__IBMCPP__)
633 #include "support/ibm/support.h"
635 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
636 #include "support/win32/support.h"
639 #include <__undef_min_max>
643 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
644 #pragma GCC system_header
647 _LIBCPP_BEGIN_NAMESPACE_STD
649 // I'd like to replace these with _VSTD::equal_to<void>, but can't because:
650 // * That only works with C++14 and later, and
651 // * We haven't included <functional> here.
652 template <class _T1, class _T2 = _T1>
655 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
656 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
657 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
658 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
662 struct __equal_to<_T1, _T1>
664 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
665 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
669 struct __equal_to<const _T1, _T1>
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
672 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
676 struct __equal_to<_T1, const _T1>
678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
682 template <class _T1, class _T2 = _T1>
685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
688 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
699 struct __less<_T1, _T1>
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
706 struct __less<const _T1, _T1>
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
713 struct __less<_T1, const _T1>
715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
719 template <class _Predicate>
725 _LIBCPP_INLINE_VISIBILITY __negate() {}
727 _LIBCPP_INLINE_VISIBILITY
728 explicit __negate(_Predicate __p) : __p_(__p) {}
731 _LIBCPP_INLINE_VISIBILITY
732 bool operator()(const _T1& __x) {return !__p_(__x);}
734 template <class _T1, class _T2>
735 _LIBCPP_INLINE_VISIBILITY
736 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
741 template <class _Compare>
745 __debug_less(_Compare& __c) : __comp_(__c) {}
746 template <class _Tp, class _Up>
747 bool operator()(const _Tp& __x, const _Up& __y)
749 bool __r = __comp_(__x, __y);
751 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
756 #endif // _LIBCPP_DEBUG
758 // Precondition: __x != 0
759 inline _LIBCPP_INLINE_VISIBILITY
763 return static_cast<unsigned>(__builtin_ctz(__x));
766 inline _LIBCPP_INLINE_VISIBILITY
768 __ctz(unsigned long __x)
770 return static_cast<unsigned long>(__builtin_ctzl(__x));
773 inline _LIBCPP_INLINE_VISIBILITY
775 __ctz(unsigned long long __x)
777 return static_cast<unsigned long long>(__builtin_ctzll(__x));
780 // Precondition: __x != 0
781 inline _LIBCPP_INLINE_VISIBILITY
785 return static_cast<unsigned>(__builtin_clz(__x));
788 inline _LIBCPP_INLINE_VISIBILITY
790 __clz(unsigned long __x)
792 return static_cast<unsigned long>(__builtin_clzl (__x));
795 inline _LIBCPP_INLINE_VISIBILITY
797 __clz(unsigned long long __x)
799 return static_cast<unsigned long long>(__builtin_clzll(__x));
802 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
803 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
804 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
808 template <class _InputIterator, class _Predicate>
809 inline _LIBCPP_INLINE_VISIBILITY
811 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
813 for (; __first != __last; ++__first)
814 if (!__pred(*__first))
821 template <class _InputIterator, class _Predicate>
822 inline _LIBCPP_INLINE_VISIBILITY
824 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
826 for (; __first != __last; ++__first)
827 if (__pred(*__first))
834 template <class _InputIterator, class _Predicate>
835 inline _LIBCPP_INLINE_VISIBILITY
837 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
839 for (; __first != __last; ++__first)
840 if (__pred(*__first))
847 template <class _InputIterator, class _Function>
848 inline _LIBCPP_INLINE_VISIBILITY
850 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
852 for (; __first != __last; ++__first)
854 return _LIBCPP_EXPLICIT_MOVE(__f); // explicitly moved for (emulated) C++03
859 template <class _InputIterator, class _Tp>
860 inline _LIBCPP_INLINE_VISIBILITY
862 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
864 for (; __first != __last; ++__first)
865 if (*__first == __value_)
872 template <class _InputIterator, class _Predicate>
873 inline _LIBCPP_INLINE_VISIBILITY
875 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
877 for (; __first != __last; ++__first)
878 if (__pred(*__first))
885 template<class _InputIterator, class _Predicate>
886 inline _LIBCPP_INLINE_VISIBILITY
888 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
890 for (; __first != __last; ++__first)
891 if (!__pred(*__first))
898 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
900 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
901 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
902 forward_iterator_tag, forward_iterator_tag)
904 // modeled after search algorithm
905 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
906 if (__first2 == __last2)
912 if (__first1 == __last1) // if source exhausted return last correct answer
913 return __r; // (or __last1 if never found)
914 if (__pred(*__first1, *__first2))
918 // *__first1 matches *__first2, now match elements after here
919 _ForwardIterator1 __m1 = __first1;
920 _ForwardIterator2 __m2 = __first2;
923 if (++__m2 == __last2)
924 { // Pattern exhaused, record answer and search for another one
929 if (++__m1 == __last1) // Source exhausted, return last answer
931 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
935 } // else there is a match, check next elements
940 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
941 _BidirectionalIterator1
942 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
943 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
944 bidirectional_iterator_tag, bidirectional_iterator_tag)
946 // modeled after search algorithm (in reverse)
947 if (__first2 == __last2)
948 return __last1; // Everything matches an empty sequence
949 _BidirectionalIterator1 __l1 = __last1;
950 _BidirectionalIterator2 __l2 = __last2;
954 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
957 if (__first1 == __l1) // return __last1 if no element matches *__first2
959 if (__pred(*--__l1, *__l2))
962 // *__l1 matches *__l2, now match elements before here
963 _BidirectionalIterator1 __m1 = __l1;
964 _BidirectionalIterator2 __m2 = __l2;
967 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
969 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
971 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
974 } // else there is a match, check next elements
979 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
980 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
981 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
982 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
983 random_access_iterator_tag, random_access_iterator_tag)
985 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
986 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
989 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
992 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
993 _RandomAccessIterator1 __l1 = __last1;
994 _RandomAccessIterator2 __l2 = __last2;
1002 if (__pred(*--__l1, *__l2))
1005 _RandomAccessIterator1 __m1 = __l1;
1006 _RandomAccessIterator2 __m2 = __l2;
1009 if (__m2 == __first2)
1011 // no need to check range on __m1 because __s guarantees we have enough source
1012 if (!__pred(*--__m1, *--__m2))
1020 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1021 inline _LIBCPP_INLINE_VISIBILITY
1023 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1024 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1026 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1027 (__first1, __last1, __first2, __last2, __pred,
1028 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1029 typename iterator_traits<_ForwardIterator2>::iterator_category());
1032 template <class _ForwardIterator1, class _ForwardIterator2>
1033 inline _LIBCPP_INLINE_VISIBILITY
1035 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1036 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1038 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1039 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1040 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1045 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1046 _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1048 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1050 for (; __first1 != __last1; ++__first1)
1051 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1052 if (__pred(*__first1, *__j))
1058 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1059 inline _LIBCPP_INLINE_VISIBILITY
1061 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1064 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1067 template <class _ForwardIterator1, class _ForwardIterator2>
1068 inline _LIBCPP_INLINE_VISIBILITY
1070 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1073 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1074 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1075 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1080 template <class _ForwardIterator, class _BinaryPredicate>
1081 inline _LIBCPP_INLINE_VISIBILITY
1083 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1085 if (__first != __last)
1087 _ForwardIterator __i = __first;
1088 while (++__i != __last)
1090 if (__pred(*__first, *__i))
1098 template <class _ForwardIterator>
1099 inline _LIBCPP_INLINE_VISIBILITY
1101 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1103 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1104 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1109 template <class _InputIterator, class _Tp>
1110 inline _LIBCPP_INLINE_VISIBILITY
1111 typename iterator_traits<_InputIterator>::difference_type
1112 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1114 typename iterator_traits<_InputIterator>::difference_type __r(0);
1115 for (; __first != __last; ++__first)
1116 if (*__first == __value_)
1123 template <class _InputIterator, class _Predicate>
1124 inline _LIBCPP_INLINE_VISIBILITY
1125 typename iterator_traits<_InputIterator>::difference_type
1126 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1128 typename iterator_traits<_InputIterator>::difference_type __r(0);
1129 for (; __first != __last; ++__first)
1130 if (__pred(*__first))
1137 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1138 inline _LIBCPP_INLINE_VISIBILITY
1139 pair<_InputIterator1, _InputIterator2>
1140 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141 _InputIterator2 __first2, _BinaryPredicate __pred)
1143 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1144 if (!__pred(*__first1, *__first2))
1146 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1149 template <class _InputIterator1, class _InputIterator2>
1150 inline _LIBCPP_INLINE_VISIBILITY
1151 pair<_InputIterator1, _InputIterator2>
1152 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1154 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1155 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1156 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1159 #if _LIBCPP_STD_VER > 11
1160 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1161 inline _LIBCPP_INLINE_VISIBILITY
1162 pair<_InputIterator1, _InputIterator2>
1163 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1164 _InputIterator2 __first2, _InputIterator2 __last2,
1165 _BinaryPredicate __pred)
1167 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1168 if (!__pred(*__first1, *__first2))
1170 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1173 template <class _InputIterator1, class _InputIterator2>
1174 inline _LIBCPP_INLINE_VISIBILITY
1175 pair<_InputIterator1, _InputIterator2>
1176 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1177 _InputIterator2 __first2, _InputIterator2 __last2)
1179 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1181 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1187 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1188 inline _LIBCPP_INLINE_VISIBILITY
1190 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1192 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1193 if (!__pred(*__first1, *__first2))
1198 template <class _InputIterator1, class _InputIterator2>
1199 inline _LIBCPP_INLINE_VISIBILITY
1201 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1203 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1204 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1205 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1208 #if _LIBCPP_STD_VER > 11
1209 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1210 inline _LIBCPP_INLINE_VISIBILITY
1212 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1213 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1214 input_iterator_tag, input_iterator_tag )
1216 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1217 if (!__pred(*__first1, *__first2))
1219 return __first1 == __last1 && __first2 == __last2;
1222 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1223 inline _LIBCPP_INLINE_VISIBILITY
1225 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1226 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1227 random_access_iterator_tag, random_access_iterator_tag )
1229 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1231 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1232 typename add_lvalue_reference<_BinaryPredicate>::type>
1233 (__first1, __last1, __first2, __pred );
1236 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237 inline _LIBCPP_INLINE_VISIBILITY
1239 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1240 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1242 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1243 (__first1, __last1, __first2, __last2, __pred,
1244 typename iterator_traits<_InputIterator1>::iterator_category(),
1245 typename iterator_traits<_InputIterator2>::iterator_category());
1248 template <class _InputIterator1, class _InputIterator2>
1249 inline _LIBCPP_INLINE_VISIBILITY
1251 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1252 _InputIterator2 __first2, _InputIterator2 __last2)
1254 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1255 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1256 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1257 typename iterator_traits<_InputIterator1>::iterator_category(),
1258 typename iterator_traits<_InputIterator2>::iterator_category());
1264 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1266 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1267 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1269 // shorten sequences as much as possible by lopping of any equal parts
1270 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1271 if (!__pred(*__first1, *__first2))
1275 // __first1 != __last1 && *__first1 != *__first2
1276 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1277 _D1 __l1 = _VSTD::distance(__first1, __last1);
1280 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1281 // For each element in [f1, l1) see if there are the same number of
1282 // equal elements in [f2, l2)
1283 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1285 // Have we already counted the number of *__i in [f1, l1)?
1286 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1287 if (__pred(*__j, *__i))
1290 // Count number of *__i in [f2, l2)
1292 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1293 if (__pred(*__i, *__j))
1297 // Count number of *__i in [__i, l1) (we can start with 1)
1299 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1300 if (__pred(*__i, *__j))
1310 template<class _ForwardIterator1, class _ForwardIterator2>
1311 inline _LIBCPP_INLINE_VISIBILITY
1313 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1314 _ForwardIterator2 __first2)
1316 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1317 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1318 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1321 #if _LIBCPP_STD_VER > 11
1322 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1324 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1325 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1326 _BinaryPredicate __pred,
1327 forward_iterator_tag, forward_iterator_tag )
1329 // shorten sequences as much as possible by lopping of any equal parts
1330 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1331 if (!__pred(*__first1, *__first2))
1333 return __first1 == __last1 && __first2 == __last2;
1335 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1336 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1337 _D1 __l1 = _VSTD::distance(__first1, __last1);
1339 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1340 _D2 __l2 = _VSTD::distance(__first2, __last2);
1344 // For each element in [f1, l1) see if there are the same number of
1345 // equal elements in [f2, l2)
1346 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1348 // Have we already counted the number of *__i in [f1, l1)?
1349 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1350 if (__pred(*__j, *__i))
1353 // Count number of *__i in [f2, l2)
1355 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1356 if (__pred(*__i, *__j))
1360 // Count number of *__i in [__i, l1) (we can start with 1)
1362 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1363 if (__pred(*__i, *__j))
1373 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1375 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1376 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1377 _BinaryPredicate __pred,
1378 random_access_iterator_tag, random_access_iterator_tag )
1380 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1382 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1383 typename add_lvalue_reference<_BinaryPredicate>::type>
1384 (__first1, __last1, __first2, __pred );
1387 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1388 inline _LIBCPP_INLINE_VISIBILITY
1390 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1391 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1392 _BinaryPredicate __pred )
1394 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1395 (__first1, __last1, __first2, __last2, __pred,
1396 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1397 typename iterator_traits<_ForwardIterator2>::iterator_category());
1400 template<class _ForwardIterator1, class _ForwardIterator2>
1401 inline _LIBCPP_INLINE_VISIBILITY
1403 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1404 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1406 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1407 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1408 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1409 __equal_to<__v1, __v2>(),
1410 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1411 typename iterator_traits<_ForwardIterator2>::iterator_category());
1417 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1419 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1421 forward_iterator_tag, forward_iterator_tag)
1423 if (__first2 == __last2)
1424 return __first1; // Everything matches an empty sequence
1427 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1430 if (__first1 == __last1) // return __last1 if no element matches *__first2
1432 if (__pred(*__first1, *__first2))
1436 // *__first1 matches *__first2, now match elements after here
1437 _ForwardIterator1 __m1 = __first1;
1438 _ForwardIterator2 __m2 = __first2;
1441 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1443 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1445 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1449 } // else there is a match, check next elements
1454 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1455 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1456 __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1457 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1458 random_access_iterator_tag, random_access_iterator_tag)
1460 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1461 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1462 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1463 _D2 __len2 = __last2 - __first2;
1466 _D1 __len1 = __last1 - __first1;
1467 if (__len1 < __len2)
1469 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1472 #if !_LIBCPP_UNROLL_LOOPS
1475 if (__first1 == __s)
1477 if (__pred(*__first1, *__first2))
1481 #else // !_LIBCPP_UNROLL_LOOPS
1482 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1484 if (__pred(*__first1, *__first2))
1486 if (__pred(*++__first1, *__first2))
1488 if (__pred(*++__first1, *__first2))
1490 if (__pred(*++__first1, *__first2))
1494 switch (__s - __first1)
1497 if (__pred(*__first1, *__first2))
1501 if (__pred(*__first1, *__first2))
1505 if (__pred(*__first1, *__first2))
1511 #endif // !_LIBCPP_UNROLL_LOOPS
1512 _RandomAccessIterator1 __m1 = __first1;
1513 _RandomAccessIterator2 __m2 = __first2;
1514 #if !_LIBCPP_UNROLL_LOOPS
1517 if (++__m2 == __last2)
1519 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1520 if (!__pred(*__m1, *__m2))
1526 #else // !_LIBCPP_UNROLL_LOOPS
1529 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1531 if (!__pred(*__m1, *__m2))
1533 if (!__pred(*++__m1, *++__m2))
1535 if (!__pred(*++__m1, *++__m2))
1537 if (!__pred(*++__m1, *++__m2))
1542 switch (__last2 - __m2)
1545 if (!__pred(*__m1, *__m2))
1550 if (!__pred(*__m1, *__m2))
1555 if (!__pred(*__m1, *__m2))
1562 #endif // !_LIBCPP_UNROLL_LOOPS
1566 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1567 inline _LIBCPP_INLINE_VISIBILITY
1569 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1570 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1572 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1573 (__first1, __last1, __first2, __last2, __pred,
1574 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1575 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1578 template <class _ForwardIterator1, class _ForwardIterator2>
1579 inline _LIBCPP_INLINE_VISIBILITY
1581 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1582 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1584 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1585 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1586 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1591 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1593 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1594 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1600 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1603 if (__first == __last) // return __last if no element matches __value_
1605 if (__pred(*__first, __value_))
1609 // *__first matches __value_, now match elements after here
1610 _ForwardIterator __m = __first;
1614 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1616 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1618 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1623 } // else there is a match, check next elements
1628 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1629 _RandomAccessIterator
1630 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1631 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1635 _Size __len = static_cast<_Size>(__last - __first);
1636 if (__len < __count)
1638 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1641 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1644 if (__first >= __s) // return __last if no element matches __value_
1646 if (__pred(*__first, __value_))
1650 // *__first matches __value_, now match elements after here
1651 _RandomAccessIterator __m = __first;
1655 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1657 ++__m; // no need to check range on __m because __s guarantees we have enough source
1658 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1663 } // else there is a match, check next elements
1668 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1669 inline _LIBCPP_INLINE_VISIBILITY
1671 search_n(_ForwardIterator __first, _ForwardIterator __last,
1672 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1674 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1675 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1676 typename iterator_traits<_ForwardIterator>::iterator_category());
1679 template <class _ForwardIterator, class _Size, class _Tp>
1680 inline _LIBCPP_INLINE_VISIBILITY
1682 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1684 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1685 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1686 __value_, __equal_to<__v, _Tp>());
1691 template <class _Iter>
1692 struct __libcpp_is_trivial_iterator
1694 static const bool value = is_pointer<_Iter>::value;
1697 template <class _Iter>
1698 struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1700 static const bool value = is_pointer<_Iter>::value;
1703 template <class _Iter>
1704 struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1706 static const bool value = is_pointer<_Iter>::value;
1709 template <class _Iter>
1710 inline _LIBCPP_INLINE_VISIBILITY
1712 __unwrap_iter(_Iter __i)
1717 template <class _Tp>
1718 inline _LIBCPP_INLINE_VISIBILITY
1721 is_trivially_copy_assignable<_Tp>::value,
1724 __unwrap_iter(move_iterator<_Tp*> __i)
1729 #if _LIBCPP_DEBUG_LEVEL < 2
1731 template <class _Tp>
1732 inline _LIBCPP_INLINE_VISIBILITY
1735 is_trivially_copy_assignable<_Tp>::value,
1738 __unwrap_iter(__wrap_iter<_Tp*> __i)
1743 #endif // _LIBCPP_DEBUG_LEVEL < 2
1745 template <class _InputIterator, class _OutputIterator>
1746 inline _LIBCPP_INLINE_VISIBILITY
1748 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1750 for (; __first != __last; ++__first, (void) ++__result)
1751 *__result = *__first;
1755 template <class _Tp, class _Up>
1756 inline _LIBCPP_INLINE_VISIBILITY
1759 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1760 is_trivially_copy_assignable<_Up>::value,
1763 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1765 const size_t __n = static_cast<size_t>(__last - __first);
1767 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1768 return __result + __n;
1771 template <class _InputIterator, class _OutputIterator>
1772 inline _LIBCPP_INLINE_VISIBILITY
1774 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1776 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1781 template <class _BidirectionalIterator, class _OutputIterator>
1782 inline _LIBCPP_INLINE_VISIBILITY
1784 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1786 while (__first != __last)
1787 *--__result = *--__last;
1791 template <class _Tp, class _Up>
1792 inline _LIBCPP_INLINE_VISIBILITY
1795 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1796 is_trivially_copy_assignable<_Up>::value,
1799 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1801 const size_t __n = static_cast<size_t>(__last - __first);
1805 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1810 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1811 inline _LIBCPP_INLINE_VISIBILITY
1812 _BidirectionalIterator2
1813 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1814 _BidirectionalIterator2 __result)
1816 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1821 template<class _InputIterator, class _OutputIterator, class _Predicate>
1822 inline _LIBCPP_INLINE_VISIBILITY
1824 copy_if(_InputIterator __first, _InputIterator __last,
1825 _OutputIterator __result, _Predicate __pred)
1827 for (; __first != __last; ++__first)
1829 if (__pred(*__first))
1831 *__result = *__first;
1840 template<class _InputIterator, class _Size, class _OutputIterator>
1841 inline _LIBCPP_INLINE_VISIBILITY
1844 __is_input_iterator<_InputIterator>::value &&
1845 !__is_random_access_iterator<_InputIterator>::value,
1848 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1850 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1851 _IntegralSize __n = __orig_n;
1854 *__result = *__first;
1856 for (--__n; __n > 0; --__n)
1859 *__result = *__first;
1866 template<class _InputIterator, class _Size, class _OutputIterator>
1867 inline _LIBCPP_INLINE_VISIBILITY
1870 __is_random_access_iterator<_InputIterator>::value,
1873 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
1875 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1876 _IntegralSize __n = __orig_n;
1877 return _VSTD::copy(__first, __first + __n, __result);
1882 template <class _InputIterator, class _OutputIterator>
1883 inline _LIBCPP_INLINE_VISIBILITY
1885 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1887 for (; __first != __last; ++__first, (void) ++__result)
1888 *__result = _VSTD::move(*__first);
1892 template <class _Tp, class _Up>
1893 inline _LIBCPP_INLINE_VISIBILITY
1896 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1897 is_trivially_copy_assignable<_Up>::value,
1900 __move(_Tp* __first, _Tp* __last, _Up* __result)
1902 const size_t __n = static_cast<size_t>(__last - __first);
1904 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1905 return __result + __n;
1908 template <class _InputIterator, class _OutputIterator>
1909 inline _LIBCPP_INLINE_VISIBILITY
1911 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1913 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1918 template <class _InputIterator, class _OutputIterator>
1919 inline _LIBCPP_INLINE_VISIBILITY
1921 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1923 while (__first != __last)
1924 *--__result = _VSTD::move(*--__last);
1928 template <class _Tp, class _Up>
1929 inline _LIBCPP_INLINE_VISIBILITY
1932 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1933 is_trivially_copy_assignable<_Up>::value,
1936 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1938 const size_t __n = static_cast<size_t>(__last - __first);
1942 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1947 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1948 inline _LIBCPP_INLINE_VISIBILITY
1949 _BidirectionalIterator2
1950 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1951 _BidirectionalIterator2 __result)
1953 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1958 // moved to <type_traits> for better swap / noexcept support
1962 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1963 inline _LIBCPP_INLINE_VISIBILITY
1965 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1967 for (; __first != __last; ++__first, (void) ++__result)
1968 *__result = __op(*__first);
1972 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1973 inline _LIBCPP_INLINE_VISIBILITY
1975 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1976 _OutputIterator __result, _BinaryOperation __binary_op)
1978 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1979 *__result = __binary_op(*__first1, *__first2);
1985 template <class _ForwardIterator, class _Tp>
1986 inline _LIBCPP_INLINE_VISIBILITY
1988 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1990 for (; __first != __last; ++__first)
1991 if (*__first == __old_value)
1992 *__first = __new_value;
1997 template <class _ForwardIterator, class _Predicate, class _Tp>
1998 inline _LIBCPP_INLINE_VISIBILITY
2000 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2002 for (; __first != __last; ++__first)
2003 if (__pred(*__first))
2004 *__first = __new_value;
2009 template <class _InputIterator, class _OutputIterator, class _Tp>
2010 inline _LIBCPP_INLINE_VISIBILITY
2012 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2013 const _Tp& __old_value, const _Tp& __new_value)
2015 for (; __first != __last; ++__first, (void) ++__result)
2016 if (*__first == __old_value)
2017 *__result = __new_value;
2019 *__result = *__first;
2025 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2026 inline _LIBCPP_INLINE_VISIBILITY
2028 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2029 _Predicate __pred, const _Tp& __new_value)
2031 for (; __first != __last; ++__first, (void) ++__result)
2032 if (__pred(*__first))
2033 *__result = __new_value;
2035 *__result = *__first;
2041 template <class _OutputIterator, class _Size, class _Tp>
2042 inline _LIBCPP_INLINE_VISIBILITY
2044 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2046 for (; __n > 0; ++__first, (void) --__n)
2047 *__first = __value_;
2051 template <class _Tp, class _Size, class _Up>
2052 inline _LIBCPP_INLINE_VISIBILITY
2055 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2056 !is_same<_Tp, bool>::value &&
2057 is_integral<_Up>::value && sizeof(_Up) == 1,
2060 __fill_n(_Tp* __first, _Size __n,_Up __value_)
2063 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2064 return __first + __n;
2067 template <class _OutputIterator, class _Size, class _Tp>
2068 inline _LIBCPP_INLINE_VISIBILITY
2070 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2072 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
2077 template <class _ForwardIterator, class _Tp>
2078 inline _LIBCPP_INLINE_VISIBILITY
2080 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2082 for (; __first != __last; ++__first)
2083 *__first = __value_;
2086 template <class _RandomAccessIterator, class _Tp>
2087 inline _LIBCPP_INLINE_VISIBILITY
2089 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2091 _VSTD::fill_n(__first, __last - __first, __value_);
2094 template <class _ForwardIterator, class _Tp>
2095 inline _LIBCPP_INLINE_VISIBILITY
2097 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2099 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2104 template <class _ForwardIterator, class _Generator>
2105 inline _LIBCPP_INLINE_VISIBILITY
2107 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2109 for (; __first != __last; ++__first)
2115 template <class _OutputIterator, class _Size, class _Generator>
2116 inline _LIBCPP_INLINE_VISIBILITY
2118 generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
2120 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2121 _IntegralSize __n = __orig_n;
2122 for (; __n > 0; ++__first, (void) --__n)
2129 template <class _ForwardIterator, class _Tp>
2131 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2133 __first = _VSTD::find(__first, __last, __value_);
2134 if (__first != __last)
2136 _ForwardIterator __i = __first;
2137 while (++__i != __last)
2139 if (!(*__i == __value_))
2141 *__first = _VSTD::move(*__i);
2151 template <class _ForwardIterator, class _Predicate>
2153 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2155 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2156 (__first, __last, __pred);
2157 if (__first != __last)
2159 _ForwardIterator __i = __first;
2160 while (++__i != __last)
2164 *__first = _VSTD::move(*__i);
2174 template <class _InputIterator, class _OutputIterator, class _Tp>
2175 inline _LIBCPP_INLINE_VISIBILITY
2177 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2179 for (; __first != __last; ++__first)
2181 if (!(*__first == __value_))
2183 *__result = *__first;
2192 template <class _InputIterator, class _OutputIterator, class _Predicate>
2193 inline _LIBCPP_INLINE_VISIBILITY
2195 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2197 for (; __first != __last; ++__first)
2199 if (!__pred(*__first))
2201 *__result = *__first;
2210 template <class _ForwardIterator, class _BinaryPredicate>
2212 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2214 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2215 (__first, __last, __pred);
2216 if (__first != __last)
2220 _ForwardIterator __i = __first;
2221 for (++__i; ++__i != __last;)
2222 if (!__pred(*__first, *__i))
2223 *++__first = _VSTD::move(*__i);
2229 template <class _ForwardIterator>
2230 inline _LIBCPP_INLINE_VISIBILITY
2232 unique(_ForwardIterator __first, _ForwardIterator __last)
2234 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2235 return _VSTD::unique(__first, __last, __equal_to<__v>());
2240 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2242 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2243 input_iterator_tag, output_iterator_tag)
2245 if (__first != __last)
2247 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2250 while (++__first != __last)
2252 if (!__pred(__t, *__first))
2263 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2265 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2266 forward_iterator_tag, output_iterator_tag)
2268 if (__first != __last)
2270 _ForwardIterator __i = __first;
2273 while (++__first != __last)
2275 if (!__pred(*__i, *__first))
2277 *__result = *__first;
2286 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2288 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2289 input_iterator_tag, forward_iterator_tag)
2291 if (__first != __last)
2293 *__result = *__first;
2294 while (++__first != __last)
2295 if (!__pred(*__result, *__first))
2296 *++__result = *__first;
2302 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2303 inline _LIBCPP_INLINE_VISIBILITY
2305 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2307 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2308 (__first, __last, __result, __pred,
2309 typename iterator_traits<_InputIterator>::iterator_category(),
2310 typename iterator_traits<_OutputIterator>::iterator_category());
2313 template <class _InputIterator, class _OutputIterator>
2314 inline _LIBCPP_INLINE_VISIBILITY
2316 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2318 typedef typename iterator_traits<_InputIterator>::value_type __v;
2319 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2324 template <class _BidirectionalIterator>
2325 inline _LIBCPP_INLINE_VISIBILITY
2327 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2329 while (__first != __last)
2331 if (__first == --__last)
2333 _VSTD::iter_swap(__first, __last);
2338 template <class _RandomAccessIterator>
2339 inline _LIBCPP_INLINE_VISIBILITY
2341 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2343 if (__first != __last)
2344 for (; __first < --__last; ++__first)
2345 _VSTD::iter_swap(__first, __last);
2348 template <class _BidirectionalIterator>
2349 inline _LIBCPP_INLINE_VISIBILITY
2351 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2353 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2358 template <class _BidirectionalIterator, class _OutputIterator>
2359 inline _LIBCPP_INLINE_VISIBILITY
2361 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2363 for (; __first != __last; ++__result)
2364 *__result = *--__last;
2370 template <class _ForwardIterator>
2372 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2374 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2375 value_type __tmp = _VSTD::move(*__first);
2376 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2377 *__lm1 = _VSTD::move(__tmp);
2381 template <class _BidirectionalIterator>
2382 _BidirectionalIterator
2383 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2385 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2386 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2387 value_type __tmp = _VSTD::move(*__lm1);
2388 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2389 *__first = _VSTD::move(__tmp);
2393 template <class _ForwardIterator>
2395 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2397 _ForwardIterator __i = __middle;
2400 swap(*__first, *__i);
2402 if (++__i == __last)
2404 if (__first == __middle)
2407 _ForwardIterator __r = __first;
2408 if (__first != __middle)
2413 swap(*__first, *__i);
2415 if (++__i == __last)
2417 if (__first == __middle)
2421 else if (__first == __middle)
2428 template<typename _Integral>
2429 inline _LIBCPP_INLINE_VISIBILITY
2431 __gcd(_Integral __x, _Integral __y)
2435 _Integral __t = __x % __y;
2442 template<typename _RandomAccessIterator>
2443 _RandomAccessIterator
2444 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2446 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2447 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2449 const difference_type __m1 = __middle - __first;
2450 const difference_type __m2 = __last - __middle;
2453 _VSTD::swap_ranges(__first, __middle, __middle);
2456 const difference_type __g = _VSTD::__gcd(__m1, __m2);
2457 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2459 value_type __t(_VSTD::move(*--__p));
2460 _RandomAccessIterator __p1 = __p;
2461 _RandomAccessIterator __p2 = __p1 + __m1;
2464 *__p1 = _VSTD::move(*__p2);
2466 const difference_type __d = __last - __p2;
2470 __p2 = __first + (__m1 - __d);
2471 } while (__p2 != __p);
2472 *__p1 = _VSTD::move(__t);
2474 return __first + __m2;
2477 template <class _ForwardIterator>
2478 inline _LIBCPP_INLINE_VISIBILITY
2480 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2481 _VSTD::forward_iterator_tag)
2483 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2484 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2486 if (_VSTD::next(__first) == __middle)
2487 return _VSTD::__rotate_left(__first, __last);
2489 return _VSTD::__rotate_forward(__first, __middle, __last);
2492 template <class _BidirectionalIterator>
2493 inline _LIBCPP_INLINE_VISIBILITY
2494 _BidirectionalIterator
2495 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2496 _VSTD::bidirectional_iterator_tag)
2498 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2499 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2501 if (_VSTD::next(__first) == __middle)
2502 return _VSTD::__rotate_left(__first, __last);
2503 if (_VSTD::next(__middle) == __last)
2504 return _VSTD::__rotate_right(__first, __last);
2506 return _VSTD::__rotate_forward(__first, __middle, __last);
2509 template <class _RandomAccessIterator>
2510 inline _LIBCPP_INLINE_VISIBILITY
2511 _RandomAccessIterator
2512 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2513 _VSTD::random_access_iterator_tag)
2515 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2516 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2518 if (_VSTD::next(__first) == __middle)
2519 return _VSTD::__rotate_left(__first, __last);
2520 if (_VSTD::next(__middle) == __last)
2521 return _VSTD::__rotate_right(__first, __last);
2522 return _VSTD::__rotate_gcd(__first, __middle, __last);
2524 return _VSTD::__rotate_forward(__first, __middle, __last);
2527 template <class _ForwardIterator>
2528 inline _LIBCPP_INLINE_VISIBILITY
2530 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2532 if (__first == __middle)
2534 if (__middle == __last)
2536 return _VSTD::__rotate(__first, __middle, __last,
2537 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2542 template <class _ForwardIterator, class _OutputIterator>
2543 inline _LIBCPP_INLINE_VISIBILITY
2545 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2547 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2552 template <class _ForwardIterator, class _Compare>
2553 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2555 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2557 if (__first != __last)
2559 _ForwardIterator __i = __first;
2560 while (++__i != __last)
2561 if (__comp(*__i, *__first))
2567 template <class _ForwardIterator>
2568 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2570 min_element(_ForwardIterator __first, _ForwardIterator __last)
2572 return _VSTD::min_element(__first, __last,
2573 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2578 template <class _Tp, class _Compare>
2579 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2581 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2583 return __comp(__b, __a) ? __b : __a;
2586 template <class _Tp>
2587 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2589 min(const _Tp& __a, const _Tp& __b)
2591 return _VSTD::min(__a, __b, __less<_Tp>());
2594 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2596 template<class _Tp, class _Compare>
2597 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2599 min(initializer_list<_Tp> __t, _Compare __comp)
2601 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2605 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2607 min(initializer_list<_Tp> __t)
2609 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
2612 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2616 template <class _ForwardIterator, class _Compare>
2617 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2619 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2621 if (__first != __last)
2623 _ForwardIterator __i = __first;
2624 while (++__i != __last)
2625 if (__comp(*__first, *__i))
2632 template <class _ForwardIterator>
2633 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2635 max_element(_ForwardIterator __first, _ForwardIterator __last)
2637 return _VSTD::max_element(__first, __last,
2638 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2643 template <class _Tp, class _Compare>
2644 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2646 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2648 return __comp(__a, __b) ? __b : __a;
2651 template <class _Tp>
2652 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2654 max(const _Tp& __a, const _Tp& __b)
2656 return _VSTD::max(__a, __b, __less<_Tp>());
2659 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2661 template<class _Tp, class _Compare>
2662 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2664 max(initializer_list<_Tp> __t, _Compare __comp)
2666 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2670 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2672 max(initializer_list<_Tp> __t)
2674 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
2677 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2681 template <class _ForwardIterator, class _Compare>
2682 _LIBCPP_CONSTEXPR_AFTER_CXX11
2683 std::pair<_ForwardIterator, _ForwardIterator>
2684 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2686 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2687 if (__first != __last)
2689 if (++__first != __last)
2691 if (__comp(*__first, *__result.first))
2692 __result.first = __first;
2694 __result.second = __first;
2695 while (++__first != __last)
2697 _ForwardIterator __i = __first;
2698 if (++__first == __last)
2700 if (__comp(*__i, *__result.first))
2701 __result.first = __i;
2702 else if (!__comp(*__i, *__result.second))
2703 __result.second = __i;
2708 if (__comp(*__first, *__i))
2710 if (__comp(*__first, *__result.first))
2711 __result.first = __first;
2712 if (!__comp(*__i, *__result.second))
2713 __result.second = __i;
2717 if (__comp(*__i, *__result.first))
2718 __result.first = __i;
2719 if (!__comp(*__first, *__result.second))
2720 __result.second = __first;
2729 template <class _ForwardIterator>
2730 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2731 std::pair<_ForwardIterator, _ForwardIterator>
2732 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2734 return _VSTD::minmax_element(__first, __last,
2735 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2740 template<class _Tp, class _Compare>
2741 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2742 pair<const _Tp&, const _Tp&>
2743 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2745 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2746 pair<const _Tp&, const _Tp&>(__a, __b);
2750 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2751 pair<const _Tp&, const _Tp&>
2752 minmax(const _Tp& __a, const _Tp& __b)
2754 return _VSTD::minmax(__a, __b, __less<_Tp>());
2757 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2759 template<class _Tp, class _Compare>
2760 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2762 minmax(initializer_list<_Tp> __t, _Compare __comp)
2764 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2765 _Iter __first = __t.begin();
2766 _Iter __last = __t.end();
2767 std::pair<_Tp, _Tp> __result(*__first, *__first);
2770 if (__t.size() % 2 == 0)
2772 if (__comp(*__first, __result.first))
2773 __result.first = *__first;
2775 __result.second = *__first;
2779 while (__first != __last)
2781 _Tp __prev = *__first++;
2782 if (__comp(*__first, __prev)) {
2783 if ( __comp(*__first, __result.first)) __result.first = *__first;
2784 if (!__comp(__prev, __result.second)) __result.second = __prev;
2787 if ( __comp(__prev, __result.first)) __result.first = __prev;
2788 if (!__comp(*__first, __result.second)) __result.second = *__first;
2797 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2799 minmax(initializer_list<_Tp> __t)
2801 return _VSTD::minmax(__t, __less<_Tp>());
2804 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2808 // __independent_bits_engine
2810 template <unsigned long long _Xp, size_t _Rp>
2813 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2814 : __log2_imp<_Xp, _Rp - 1>::value;
2817 template <unsigned long long _Xp>
2818 struct __log2_imp<_Xp, 0>
2820 static const size_t value = 0;
2823 template <size_t _Rp>
2824 struct __log2_imp<0, _Rp>
2826 static const size_t value = _Rp + 1;
2829 template <class _UI, _UI _Xp>
2832 static const size_t value = __log2_imp<_Xp,
2833 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2836 template<class _Engine, class _UIntType>
2837 class __independent_bits_engine
2841 typedef _UIntType result_type;
2844 typedef typename _Engine::result_type _Engine_result_type;
2845 typedef typename conditional
2847 sizeof(_Engine_result_type) <= sizeof(result_type),
2850 >::type _Working_result_type;
2857 _Working_result_type __y0_;
2858 _Working_result_type __y1_;
2859 _Engine_result_type __mask0_;
2860 _Engine_result_type __mask1_;
2862 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
2863 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2864 + _Working_result_type(1);
2866 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2867 + _Working_result_type(1);
2869 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2870 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2871 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2874 // constructors and seeding functions
2875 __independent_bits_engine(_Engine& __e, size_t __w);
2877 // generating functions
2878 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2881 result_type __eval(false_type);
2882 result_type __eval(true_type);
2885 template<class _Engine, class _UIntType>
2886 __independent_bits_engine<_Engine, _UIntType>
2887 ::__independent_bits_engine(_Engine& __e, size_t __w)
2891 __n_ = __w_ / __m + (__w_ % __m != 0);
2892 __w0_ = __w_ / __n_;
2895 else if (__w0_ < _WDt)
2896 __y0_ = (_Rp >> __w0_) << __w0_;
2899 if (_Rp - __y0_ > __y0_ / __n_)
2902 __w0_ = __w_ / __n_;
2904 __y0_ = (_Rp >> __w0_) << __w0_;
2908 __n0_ = __n_ - __w_ % __n_;
2909 if (__w0_ < _WDt - 1)
2910 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2913 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2914 _Engine_result_type(0);
2915 __mask1_ = __w0_ < _EDt - 1 ?
2916 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2917 _Engine_result_type(~0);
2920 template<class _Engine, class _UIntType>
2923 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2925 return static_cast<result_type>(__e_() & __mask0_);
2928 template<class _Engine, class _UIntType>
2930 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2932 result_type _Sp = 0;
2933 for (size_t __k = 0; __k < __n0_; ++__k)
2935 _Engine_result_type __u;
2938 __u = __e_() - _Engine::min();
2939 } while (__u >= __y0_);
2944 _Sp += __u & __mask0_;
2946 for (size_t __k = __n0_; __k < __n_; ++__k)
2948 _Engine_result_type __u;
2951 __u = __e_() - _Engine::min();
2952 } while (__u >= __y1_);
2953 if (__w0_ < _WDt - 1)
2957 _Sp += __u & __mask1_;
2962 // uniform_int_distribution
2964 template<class _IntType = int>
2965 class uniform_int_distribution
2969 typedef _IntType result_type;
2976 typedef uniform_int_distribution distribution_type;
2978 explicit param_type(result_type __a = 0,
2979 result_type __b = numeric_limits<result_type>::max())
2980 : __a_(__a), __b_(__b) {}
2982 result_type a() const {return __a_;}
2983 result_type b() const {return __b_;}
2985 friend bool operator==(const param_type& __x, const param_type& __y)
2986 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2987 friend bool operator!=(const param_type& __x, const param_type& __y)
2988 {return !(__x == __y);}
2995 // constructors and reset functions
2996 explicit uniform_int_distribution(result_type __a = 0,
2997 result_type __b = numeric_limits<result_type>::max())
2998 : __p_(param_type(__a, __b)) {}
2999 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3002 // generating functions
3003 template<class _URNG> result_type operator()(_URNG& __g)
3004 {return (*this)(__g, __p_);}
3005 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3007 // property functions
3008 result_type a() const {return __p_.a();}
3009 result_type b() const {return __p_.b();}
3011 param_type param() const {return __p_;}
3012 void param(const param_type& __p) {__p_ = __p;}
3014 result_type min() const {return a();}
3015 result_type max() const {return b();}
3017 friend bool operator==(const uniform_int_distribution& __x,
3018 const uniform_int_distribution& __y)
3019 {return __x.__p_ == __y.__p_;}
3020 friend bool operator!=(const uniform_int_distribution& __x,
3021 const uniform_int_distribution& __y)
3022 {return !(__x == __y);}
3025 template<class _IntType>
3026 template<class _URNG>
3027 typename uniform_int_distribution<_IntType>::result_type
3028 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3030 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3031 uint32_t, uint64_t>::type _UIntType;
3032 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3035 const size_t _Dt = numeric_limits<_UIntType>::digits;
3036 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3038 return static_cast<result_type>(_Eng(__g, _Dt)());
3039 size_t __w = _Dt - __clz(_Rp) - 1;
3040 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
3047 } while (__u >= _Rp);
3048 return static_cast<result_type>(__u + __p.a());
3051 class _LIBCPP_TYPE_VIS __rs_default;
3053 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3055 class _LIBCPP_TYPE_VIS __rs_default
3057 static unsigned __c_;
3061 typedef uint_fast32_t result_type;
3063 static const result_type _Min = 0;
3064 static const result_type _Max = 0xFFFFFFFF;
3066 __rs_default(const __rs_default&);
3069 result_type operator()();
3071 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3072 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3074 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3077 _LIBCPP_FUNC_VIS __rs_default __rs_get();
3079 template <class _RandomAccessIterator>
3081 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3083 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3084 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3085 typedef typename _Dp::param_type _Pp;
3086 difference_type __d = __last - __first;
3090 __rs_default __g = __rs_get();
3091 for (--__last, --__d; __first < __last; ++__first, --__d)
3093 difference_type __i = __uid(__g, _Pp(0, __d));
3094 if (__i != difference_type(0))
3095 swap(*__first, *(__first + __i));
3100 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3102 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3103 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3104 _RandomNumberGenerator&& __rand)
3106 _RandomNumberGenerator& __rand)
3109 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3110 difference_type __d = __last - __first;
3113 for (--__last; __first < __last; ++__first, --__d)
3115 difference_type __i = __rand(__d);
3116 swap(*__first, *(__first + __i));
3121 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3122 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3123 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3124 _UniformRandomNumberGenerator&& __g)
3126 _UniformRandomNumberGenerator& __g)
3129 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3130 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3131 typedef typename _Dp::param_type _Pp;
3132 difference_type __d = __last - __first;
3136 for (--__last, --__d; __first < __last; ++__first, --__d)
3138 difference_type __i = __uid(__g, _Pp(0, __d));
3139 if (__i != difference_type(0))
3140 swap(*__first, *(__first + __i));
3145 template <class _InputIterator, class _Predicate>
3147 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3149 for (; __first != __last; ++__first)
3150 if (!__pred(*__first))
3152 if ( __first == __last )
3155 for (; __first != __last; ++__first)
3156 if (__pred(*__first))
3163 template <class _Predicate, class _ForwardIterator>
3165 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3169 if (__first == __last)
3171 if (!__pred(*__first))
3175 for (_ForwardIterator __p = __first; ++__p != __last;)
3179 swap(*__first, *__p);
3186 template <class _Predicate, class _BidirectionalIterator>
3187 _BidirectionalIterator
3188 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3189 bidirectional_iterator_tag)
3195 if (__first == __last)
3197 if (!__pred(*__first))
3203 if (__first == --__last)
3205 } while (!__pred(*__last));
3206 swap(*__first, *__last);
3211 template <class _ForwardIterator, class _Predicate>
3212 inline _LIBCPP_INLINE_VISIBILITY
3214 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3216 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3217 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3222 template <class _InputIterator, class _OutputIterator1,
3223 class _OutputIterator2, class _Predicate>
3224 pair<_OutputIterator1, _OutputIterator2>
3225 partition_copy(_InputIterator __first, _InputIterator __last,
3226 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3229 for (; __first != __last; ++__first)
3231 if (__pred(*__first))
3233 *__out_true = *__first;
3238 *__out_false = *__first;
3242 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3247 template<class _ForwardIterator, class _Predicate>
3249 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3251 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3252 difference_type __len = _VSTD::distance(__first, __last);
3255 difference_type __l2 = __len / 2;
3256 _ForwardIterator __m = __first;
3257 _VSTD::advance(__m, __l2);
3271 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3273 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3274 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3276 // *__first is known to be false
3282 _ForwardIterator __m = __first;
3285 swap(*__first, *__m);
3290 if (__len <= __p.second)
3291 { // The buffer is big enough to use
3292 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3293 __destruct_n __d(0);
3294 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3295 // Move the falses into the temporary buffer, and the trues to the front of the line
3296 // Update __first to always point to the end of the trues
3297 value_type* __t = __p.first;
3298 ::new(__t) value_type(_VSTD::move(*__first));
3299 __d.__incr((value_type*)0);
3301 _ForwardIterator __i = __first;
3302 while (++__i != __last)
3306 *__first = _VSTD::move(*__i);
3311 ::new(__t) value_type(_VSTD::move(*__i));
3312 __d.__incr((value_type*)0);
3316 // All trues now at start of range, all falses in buffer
3317 // Move falses back into range, but don't mess up __first which points to first false
3319 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3320 *__i = _VSTD::move(*__t2);
3321 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3324 // Else not enough buffer, do in place
3326 _ForwardIterator __m = __first;
3327 _Distance __len2 = __len / 2; // __len2 >= 2
3328 _VSTD::advance(__m, __len2);
3329 // recurse on [__first, __m), *__first know to be false
3330 // F?????????????????
3332 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3333 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3334 // TTTFFFFF??????????
3336 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3337 _ForwardIterator __m1 = __m;
3338 _ForwardIterator __second_false = __last;
3339 _Distance __len_half = __len - __len2;
3340 while (__pred(*__m1))
3342 if (++__m1 == __last)
3343 goto __second_half_done;
3346 // TTTFFFFFTTTF??????
3348 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3350 // TTTFFFFFTTTTTFFFFF
3352 return _VSTD::rotate(__first_false, __m, __second_false);
3353 // TTTTTTTTFFFFFFFFFF
3357 struct __return_temporary_buffer
3359 template <class _Tp>
3360 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3363 template <class _Predicate, class _ForwardIterator>
3365 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3366 forward_iterator_tag)
3368 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3369 // Either prove all true and return __first or point to first false
3372 if (__first == __last)
3374 if (!__pred(*__first))
3378 // We now have a reduced range [__first, __last)
3379 // *__first is known to be false
3380 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3381 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3382 difference_type __len = _VSTD::distance(__first, __last);
3383 pair<value_type*, ptrdiff_t> __p(0, 0);
3384 unique_ptr<value_type, __return_temporary_buffer> __h;
3385 if (__len >= __alloc_limit)
3387 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3388 __h.reset(__p.first);
3390 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3391 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3394 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3395 _BidirectionalIterator
3396 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3397 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3399 // *__first is known to be false
3400 // *__last is known to be true
3404 swap(*__first, *__last);
3409 _BidirectionalIterator __m = __first;
3412 swap(*__first, *__m);
3413 swap(*__m, *__last);
3416 swap(*__m, *__last);
3417 swap(*__first, *__m);
3420 if (__len <= __p.second)
3421 { // The buffer is big enough to use
3422 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3423 __destruct_n __d(0);
3424 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3425 // Move the falses into the temporary buffer, and the trues to the front of the line
3426 // Update __first to always point to the end of the trues
3427 value_type* __t = __p.first;
3428 ::new(__t) value_type(_VSTD::move(*__first));
3429 __d.__incr((value_type*)0);
3431 _BidirectionalIterator __i = __first;
3432 while (++__i != __last)
3436 *__first = _VSTD::move(*__i);
3441 ::new(__t) value_type(_VSTD::move(*__i));
3442 __d.__incr((value_type*)0);
3446 // move *__last, known to be true
3447 *__first = _VSTD::move(*__i);
3449 // All trues now at start of range, all falses in buffer
3450 // Move falses back into range, but don't mess up __first which points to first false
3451 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3452 *__i = _VSTD::move(*__t2);
3453 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3456 // Else not enough buffer, do in place
3458 _BidirectionalIterator __m = __first;
3459 _Distance __len2 = __len / 2; // __len2 >= 2
3460 _VSTD::advance(__m, __len2);
3461 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3462 // F????????????????T
3464 _BidirectionalIterator __m1 = __m;
3465 _BidirectionalIterator __first_false = __first;
3466 _Distance __len_half = __len2;
3467 while (!__pred(*--__m1))
3469 if (__m1 == __first)
3470 goto __first_half_done;
3473 // F???TFFF?????????T
3475 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3476 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3478 // TTTFFFFF?????????T
3480 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3482 _BidirectionalIterator __second_false = __last;
3484 __len_half = __len - __len2;
3485 while (__pred(*__m1))
3487 if (++__m1 == __last)
3488 goto __second_half_done;
3491 // TTTFFFFFTTTF?????T
3493 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3495 // TTTFFFFFTTTTTFFFFF
3497 return _VSTD::rotate(__first_false, __m, __second_false);
3498 // TTTTTTTTFFFFFFFFFF
3502 template <class _Predicate, class _BidirectionalIterator>
3503 _BidirectionalIterator
3504 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3505 bidirectional_iterator_tag)
3507 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3508 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3509 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3510 // Either prove all true and return __first or point to first false
3513 if (__first == __last)
3515 if (!__pred(*__first))
3519 // __first points to first false, everything prior to __first is already set.
3520 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3523 if (__first == --__last)
3525 } while (!__pred(*__last));
3526 // We now have a reduced range [__first, __last]
3527 // *__first is known to be false
3528 // *__last is known to be true
3530 difference_type __len = _VSTD::distance(__first, __last) + 1;
3531 pair<value_type*, ptrdiff_t> __p(0, 0);
3532 unique_ptr<value_type, __return_temporary_buffer> __h;
3533 if (__len >= __alloc_limit)
3535 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3536 __h.reset(__p.first);
3538 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3539 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3542 template <class _ForwardIterator, class _Predicate>
3543 inline _LIBCPP_INLINE_VISIBILITY
3545 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3547 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3548 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3553 template <class _ForwardIterator, class _Compare>
3555 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3557 if (__first != __last)
3559 _ForwardIterator __i = __first;
3560 while (++__i != __last)
3562 if (__comp(*__i, *__first))
3570 template<class _ForwardIterator>
3571 inline _LIBCPP_INLINE_VISIBILITY
3573 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3575 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3580 template <class _ForwardIterator, class _Compare>
3581 inline _LIBCPP_INLINE_VISIBILITY
3583 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3585 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3588 template<class _ForwardIterator>
3589 inline _LIBCPP_INLINE_VISIBILITY
3591 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3593 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3598 // stable, 2-3 compares, 0-2 swaps
3600 template <class _Compare, class _ForwardIterator>
3602 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3605 if (!__c(*__y, *__x)) // if x <= y
3607 if (!__c(*__z, *__y)) // if y <= z
3608 return __r; // x <= y && y <= z
3610 swap(*__y, *__z); // x <= z && y < z
3612 if (__c(*__y, *__x)) // if x > y
3614 swap(*__x, *__y); // x < y && y <= z
3617 return __r; // x <= y && y < z
3619 if (__c(*__z, *__y)) // x > y, if y > z
3621 swap(*__x, *__z); // x < y && y < z
3625 swap(*__x, *__y); // x > y && y <= z
3626 __r = 1; // x < y && x <= z
3627 if (__c(*__z, *__y)) // if y > z
3629 swap(*__y, *__z); // x <= y && y < z
3633 } // x <= y && y <= z
3635 // stable, 3-6 compares, 0-5 swaps
3637 template <class _Compare, class _ForwardIterator>
3639 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3640 _ForwardIterator __x4, _Compare __c)
3642 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3643 if (__c(*__x4, *__x3))
3647 if (__c(*__x3, *__x2))
3651 if (__c(*__x2, *__x1))
3661 // stable, 4-10 compares, 0-9 swaps
3663 template <class _Compare, class _ForwardIterator>
3665 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3666 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3668 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3669 if (__c(*__x5, *__x4))
3673 if (__c(*__x4, *__x3))
3677 if (__c(*__x3, *__x2))
3681 if (__c(*__x2, *__x1))
3693 template <class _Compare, class _BirdirectionalIterator>
3695 __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3697 _BirdirectionalIterator __lm1 = __last;
3698 for (--__lm1; __first != __lm1; ++__first)
3700 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3701 typename add_lvalue_reference<_Compare>::type>
3702 (__first, __last, __comp);
3704 swap(*__first, *__i);
3708 template <class _Compare, class _BirdirectionalIterator>
3710 __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3712 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3713 if (__first != __last)
3715 _BirdirectionalIterator __i = __first;
3716 for (++__i; __i != __last; ++__i)
3718 _BirdirectionalIterator __j = __i;
3719 value_type __t(_VSTD::move(*__j));
3720 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3721 *__j = _VSTD::move(*__k);
3722 *__j = _VSTD::move(__t);
3727 template <class _Compare, class _RandomAccessIterator>
3729 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3731 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3732 _RandomAccessIterator __j = __first+2;
3733 __sort3<_Compare>(__first, __first+1, __j, __comp);
3734 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3736 if (__comp(*__i, *__j))
3738 value_type __t(_VSTD::move(*__i));
3739 _RandomAccessIterator __k = __j;
3743 *__j = _VSTD::move(*__k);
3745 } while (__j != __first && __comp(__t, *--__k));
3746 *__j = _VSTD::move(__t);
3752 template <class _Compare, class _RandomAccessIterator>
3754 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3756 switch (__last - __first)
3762 if (__comp(*--__last, *__first))
3763 swap(*__first, *__last);
3766 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3769 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3772 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3775 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3776 _RandomAccessIterator __j = __first+2;
3777 __sort3<_Compare>(__first, __first+1, __j, __comp);
3778 const unsigned __limit = 8;
3779 unsigned __count = 0;
3780 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3782 if (__comp(*__i, *__j))
3784 value_type __t(_VSTD::move(*__i));
3785 _RandomAccessIterator __k = __j;
3789 *__j = _VSTD::move(*__k);
3791 } while (__j != __first && __comp(__t, *--__k));
3792 *__j = _VSTD::move(__t);
3793 if (++__count == __limit)
3794 return ++__i == __last;
3801 template <class _Compare, class _BirdirectionalIterator>
3803 __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3804 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3806 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3807 if (__first1 != __last1)
3809 __destruct_n __d(0);
3810 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3811 value_type* __last2 = __first2;
3812 ::new(__last2) value_type(_VSTD::move(*__first1));
3813 __d.__incr((value_type*)0);
3814 for (++__last2; ++__first1 != __last1; ++__last2)
3816 value_type* __j2 = __last2;
3817 value_type* __i2 = __j2;
3818 if (__comp(*__first1, *--__i2))
3820 ::new(__j2) value_type(_VSTD::move(*__i2));
3821 __d.__incr((value_type*)0);
3822 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3823 *__j2 = _VSTD::move(*__i2);
3824 *__j2 = _VSTD::move(*__first1);
3828 ::new(__j2) value_type(_VSTD::move(*__first1));
3829 __d.__incr((value_type*)0);
3836 template <class _Compare, class _RandomAccessIterator>
3838 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3840 // _Compare is known to be a reference type
3841 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3842 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3843 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3844 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3848 difference_type __len = __last - __first;
3855 if (__comp(*--__last, *__first))
3856 swap(*__first, *__last);
3859 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3862 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3865 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3868 if (__len <= __limit)
3870 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3874 _RandomAccessIterator __m = __first;
3875 _RandomAccessIterator __lm1 = __last;
3879 difference_type __delta;
3885 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3891 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3895 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3896 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3897 _RandomAccessIterator __i = __first;
3898 _RandomAccessIterator __j = __lm1;
3899 // j points beyond range to be tested, *__m is known to be <= *__lm1
3900 // The search going up is known to be guarded but the search coming down isn't.
3901 // Prime the downward search with a guard.
3902 if (!__comp(*__i, *__m)) // if *__first == *__m
3904 // *__first == *__m, *__first doesn't go in first part
3905 // manually guard downward moving __j against __i
3910 // *__first == *__m, *__m <= all other elements
3911 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3912 ++__i; // __first + 1
3914 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3919 return; // [__first, __last) all equivalent elements
3920 if (__comp(*__first, *__i))
3930 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3935 while (!__comp(*__first, *__i))
3937 while (__comp(*__first, *--__j))
3945 // [__first, __i) == *__first and *__first < [__i, __last)
3946 // The first part is sorted, sort the secod part
3947 // _VSTD::__sort<_Compare>(__i, __last, __comp);
3951 if (__comp(*__j, *__m))
3955 break; // found guard for downward moving __j, now use unguarded partition
3959 // It is known that *__i < *__m
3961 // j points beyond range to be tested, *__m is known to be <= *__lm1
3962 // if not yet partitioned...
3965 // known that *(__i - 1) < *__m
3966 // known that __i <= __m
3969 // __m still guards upward moving __i
3970 while (__comp(*__i, *__m))
3972 // It is now known that a guard exists for downward moving __j
3973 while (!__comp(*--__j, *__m))
3979 // It is known that __m != __j
3980 // If __m just moved, follow it
3986 // [__first, __i) < *__m and *__m <= [__i, __last)
3987 if (__i != __m && __comp(*__m, *__i))
3992 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3993 // If we were given a perfect partition, see if insertion sort is quick...
3996 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3997 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
4013 // sort smaller range with recursive call and larger with tail recursion elimination
4014 if (__i - __first < __last - __i)
4016 _VSTD::__sort<_Compare>(__first, __i, __comp);
4017 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4022 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4023 // _VSTD::__sort<_Compare>(__first, __i, __comp);
4029 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4030 template <class _RandomAccessIterator, class _Compare>
4031 inline _LIBCPP_INLINE_VISIBILITY
4033 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4035 #ifdef _LIBCPP_DEBUG
4036 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4037 __debug_less<_Compare> __c(__comp);
4038 __sort<_Comp_ref>(__first, __last, __c);
4039 #else // _LIBCPP_DEBUG
4040 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4041 __sort<_Comp_ref>(__first, __last, __comp);
4042 #endif // _LIBCPP_DEBUG
4045 template <class _RandomAccessIterator>
4046 inline _LIBCPP_INLINE_VISIBILITY
4048 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4050 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4053 template <class _Tp>
4054 inline _LIBCPP_INLINE_VISIBILITY
4056 sort(_Tp** __first, _Tp** __last)
4058 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4061 template <class _Tp>
4062 inline _LIBCPP_INLINE_VISIBILITY
4064 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4066 _VSTD::sort(__first.base(), __last.base());
4069 template <class _Tp, class _Compare>
4070 inline _LIBCPP_INLINE_VISIBILITY
4072 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4074 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4075 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4079 #pragma warning( push )
4080 #pragma warning( disable: 4231)
4081 #endif // _LIBCPP_MSVC
4082 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4083 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4084 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4085 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4086 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4087 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4088 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4089 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4090 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4091 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4092 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4093 _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>&))
4094 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4095 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4096 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4098 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4099 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4100 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4101 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4102 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4103 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4104 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4105 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4106 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4107 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4108 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4109 _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>&))
4110 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4111 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4112 _LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4114 _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>&))
4116 #pragma warning( pop )
4117 #endif // _LIBCPP_MSVC
4121 template <class _Compare, class _ForwardIterator, class _Tp>
4123 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4125 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4126 difference_type __len = _VSTD::distance(__first, __last);
4129 difference_type __l2 = __len / 2;
4130 _ForwardIterator __m = __first;
4131 _VSTD::advance(__m, __l2);
4132 if (__comp(*__m, __value_))
4143 template <class _ForwardIterator, class _Tp, class _Compare>
4144 inline _LIBCPP_INLINE_VISIBILITY
4146 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4148 #ifdef _LIBCPP_DEBUG
4149 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4150 __debug_less<_Compare> __c(__comp);
4151 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4152 #else // _LIBCPP_DEBUG
4153 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4154 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4155 #endif // _LIBCPP_DEBUG
4158 template <class _ForwardIterator, class _Tp>
4159 inline _LIBCPP_INLINE_VISIBILITY
4161 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4163 return _VSTD::lower_bound(__first, __last, __value_,
4164 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4169 template <class _Compare, class _ForwardIterator, class _Tp>
4171 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4173 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4174 difference_type __len = _VSTD::distance(__first, __last);
4177 difference_type __l2 = __len / 2;
4178 _ForwardIterator __m = __first;
4179 _VSTD::advance(__m, __l2);
4180 if (__comp(__value_, *__m))
4191 template <class _ForwardIterator, class _Tp, class _Compare>
4192 inline _LIBCPP_INLINE_VISIBILITY
4194 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4196 #ifdef _LIBCPP_DEBUG
4197 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4198 __debug_less<_Compare> __c(__comp);
4199 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4200 #else // _LIBCPP_DEBUG
4201 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4202 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4203 #endif // _LIBCPP_DEBUG
4206 template <class _ForwardIterator, class _Tp>
4207 inline _LIBCPP_INLINE_VISIBILITY
4209 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4211 return _VSTD::upper_bound(__first, __last, __value_,
4212 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4217 template <class _Compare, class _ForwardIterator, class _Tp>
4218 pair<_ForwardIterator, _ForwardIterator>
4219 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4221 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4222 difference_type __len = _VSTD::distance(__first, __last);
4225 difference_type __l2 = __len / 2;
4226 _ForwardIterator __m = __first;
4227 _VSTD::advance(__m, __l2);
4228 if (__comp(*__m, __value_))
4233 else if (__comp(__value_, *__m))
4240 _ForwardIterator __mp1 = __m;
4241 return pair<_ForwardIterator, _ForwardIterator>
4243 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4244 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4248 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4251 template <class _ForwardIterator, class _Tp, class _Compare>
4252 inline _LIBCPP_INLINE_VISIBILITY
4253 pair<_ForwardIterator, _ForwardIterator>
4254 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4256 #ifdef _LIBCPP_DEBUG
4257 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4258 __debug_less<_Compare> __c(__comp);
4259 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4260 #else // _LIBCPP_DEBUG
4261 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4262 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4263 #endif // _LIBCPP_DEBUG
4266 template <class _ForwardIterator, class _Tp>
4267 inline _LIBCPP_INLINE_VISIBILITY
4268 pair<_ForwardIterator, _ForwardIterator>
4269 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4271 return _VSTD::equal_range(__first, __last, __value_,
4272 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4277 template <class _Compare, class _ForwardIterator, class _Tp>
4278 inline _LIBCPP_INLINE_VISIBILITY
4280 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4282 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4283 return __first != __last && !__comp(__value_, *__first);
4286 template <class _ForwardIterator, class _Tp, class _Compare>
4287 inline _LIBCPP_INLINE_VISIBILITY
4289 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4291 #ifdef _LIBCPP_DEBUG
4292 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4293 __debug_less<_Compare> __c(__comp);
4294 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4295 #else // _LIBCPP_DEBUG
4296 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4297 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4298 #endif // _LIBCPP_DEBUG
4301 template <class _ForwardIterator, class _Tp>
4302 inline _LIBCPP_INLINE_VISIBILITY
4304 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4306 return _VSTD::binary_search(__first, __last, __value_,
4307 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4312 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4314 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4315 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4317 for (; __first1 != __last1; ++__result)
4319 if (__first2 == __last2)
4320 return _VSTD::copy(__first1, __last1, __result);
4321 if (__comp(*__first2, *__first1))
4323 *__result = *__first2;
4328 *__result = *__first1;
4332 return _VSTD::copy(__first2, __last2, __result);
4335 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4336 inline _LIBCPP_INLINE_VISIBILITY
4338 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4339 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4341 #ifdef _LIBCPP_DEBUG
4342 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4343 __debug_less<_Compare> __c(__comp);
4344 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4345 #else // _LIBCPP_DEBUG
4346 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4347 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4348 #endif // _LIBCPP_DEBUG
4351 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4352 inline _LIBCPP_INLINE_VISIBILITY
4354 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4355 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4357 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4358 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4359 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4364 template <class _Compare, class _InputIterator1, class _InputIterator2,
4365 class _OutputIterator>
4366 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4367 _InputIterator2 __first2, _InputIterator2 __last2,
4368 _OutputIterator __result, _Compare __comp)
4370 for (; __first1 != __last1; ++__result)
4372 if (__first2 == __last2)
4374 _VSTD::move(__first1, __last1, __result);
4378 if (__comp(*__first2, *__first1))
4380 *__result = _VSTD::move(*__first2);
4385 *__result = _VSTD::move(*__first1);
4389 // __first2 through __last2 are already in the right spot.
4392 template <class _Compare, class _BidirectionalIterator>
4394 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4395 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4396 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4397 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4399 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4400 __destruct_n __d(0);
4401 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4402 if (__len1 <= __len2)
4404 value_type* __p = __buff;
4405 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4406 ::new(__p) value_type(_VSTD::move(*__i));
4407 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
4411 value_type* __p = __buff;
4412 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4413 ::new(__p) value_type(_VSTD::move(*__i));
4414 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4415 typedef reverse_iterator<value_type*> _Rv;
4416 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4417 _RBi(__middle), _RBi(__first),
4418 _RBi(__last), __negate<_Compare>(__comp));
4422 template <class _Compare, class _BidirectionalIterator>
4424 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4425 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4426 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4427 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4429 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4432 // if __middle == __last, we're done
4435 if (__len1 <= __buff_size || __len2 <= __buff_size)
4436 return __buffered_inplace_merge<_Compare>
4437 (__first, __middle, __last, __comp, __len1, __len2, __buff);
4438 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4439 for (; true; ++__first, (void) --__len1)
4443 if (__comp(*__middle, *__first))
4446 // __first < __middle < __last
4447 // *__first > *__middle
4448 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4450 // [__first, __m1) <= [__middle, __m2)
4451 // [__middle, __m2) < [__m1, __middle)
4452 // [__m1, __middle) <= [__m2, __last)
4453 // and __m1 or __m2 is in the middle of its range
4454 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4455 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4456 difference_type __len11; // distance(__first, __m1)
4457 difference_type __len21; // distance(__middle, __m2)
4458 // binary search smaller range
4459 if (__len1 < __len2)
4460 { // __len >= 1, __len2 >= 2
4461 __len21 = __len2 / 2;
4463 _VSTD::advance(__m2, __len21);
4464 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4465 __len11 = _VSTD::distance(__first, __m1);
4470 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4471 // It is known *__first > *__middle
4472 swap(*__first, *__middle);
4475 // __len1 >= 2, __len2 >= 1
4476 __len11 = __len1 / 2;
4478 _VSTD::advance(__m1, __len11);
4479 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4480 __len21 = _VSTD::distance(__middle, __m2);
4482 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4483 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4484 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4485 // swap middle two partitions
4486 __middle = _VSTD::rotate(__m1, __middle, __m2);
4487 // __len12 and __len21 now have swapped meanings
4488 // merge smaller range with recurisve call and larger with tail recursion elimination
4489 if (__len11 + __len21 < __len12 + __len22)
4491 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4492 // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4500 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4501 // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4510 template <class _BidirectionalIterator, class _Compare>
4511 inline _LIBCPP_INLINE_VISIBILITY
4513 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4516 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4517 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4518 difference_type __len1 = _VSTD::distance(__first, __middle);
4519 difference_type __len2 = _VSTD::distance(__middle, __last);
4520 difference_type __buf_size = _VSTD::min(__len1, __len2);
4521 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4522 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4524 #ifdef _LIBCPP_DEBUG
4525 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4526 __debug_less<_Compare> __c(__comp);
4527 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4528 __buf.first, __buf.second);
4529 #else // _LIBCPP_DEBUG
4530 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4531 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4532 __buf.first, __buf.second);
4533 #endif // _LIBCPP_DEBUG
4536 template <class _BidirectionalIterator>
4537 inline _LIBCPP_INLINE_VISIBILITY
4539 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4541 _VSTD::inplace_merge(__first, __middle, __last,
4542 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4547 template <class _Compare, class _InputIterator1, class _InputIterator2>
4549 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4550 _InputIterator2 __first2, _InputIterator2 __last2,
4551 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4553 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4554 __destruct_n __d(0);
4555 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4556 for (; true; ++__result)
4558 if (__first1 == __last1)
4560 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4561 ::new (__result) value_type(_VSTD::move(*__first2));
4565 if (__first2 == __last2)
4567 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4568 ::new (__result) value_type(_VSTD::move(*__first1));
4572 if (__comp(*__first2, *__first1))
4574 ::new (__result) value_type(_VSTD::move(*__first2));
4575 __d.__incr((value_type*)0);
4580 ::new (__result) value_type(_VSTD::move(*__first1));
4581 __d.__incr((value_type*)0);
4587 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4589 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4590 _InputIterator2 __first2, _InputIterator2 __last2,
4591 _OutputIterator __result, _Compare __comp)
4593 for (; __first1 != __last1; ++__result)
4595 if (__first2 == __last2)
4597 for (; __first1 != __last1; ++__first1, ++__result)
4598 *__result = _VSTD::move(*__first1);
4601 if (__comp(*__first2, *__first1))
4603 *__result = _VSTD::move(*__first2);
4608 *__result = _VSTD::move(*__first1);
4612 for (; __first2 != __last2; ++__first2, ++__result)
4613 *__result = _VSTD::move(*__first2);
4616 template <class _Compare, class _RandomAccessIterator>
4618 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4619 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4620 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4622 template <class _Compare, class _RandomAccessIterator>
4624 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4625 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4626 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4628 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4634 ::new(__first2) value_type(_VSTD::move(*__first1));
4637 __destruct_n __d(0);
4638 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4639 if (__comp(*--__last1, *__first1))
4641 ::new(__first2) value_type(_VSTD::move(*__last1));
4642 __d.__incr((value_type*)0);
4644 ::new(__first2) value_type(_VSTD::move(*__first1));
4648 ::new(__first2) value_type(_VSTD::move(*__first1));
4649 __d.__incr((value_type*)0);
4651 ::new(__first2) value_type(_VSTD::move(*__last1));
4658 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4661 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4662 _RandomAccessIterator __m = __first1 + __l2;
4663 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4664 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4665 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4668 template <class _Tp>
4669 struct __stable_sort_switch
4671 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4674 template <class _Compare, class _RandomAccessIterator>
4676 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4677 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4678 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4680 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4681 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4688 if (__comp(*--__last, *__first))
4689 swap(*__first, *__last);
4692 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4694 __insertion_sort<_Compare>(__first, __last, __comp);
4697 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4698 _RandomAccessIterator __m = __first + __l2;
4699 if (__len <= __buff_size)
4701 __destruct_n __d(0);
4702 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4703 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4704 __d.__set(__l2, (value_type*)0);
4705 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4706 __d.__set(__len, (value_type*)0);
4707 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4708 // __merge<_Compare>(move_iterator<value_type*>(__buff),
4709 // move_iterator<value_type*>(__buff + __l2),
4710 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4711 // move_iterator<_RandomAccessIterator>(__buff + __len),
4712 // __first, __comp);
4715 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4716 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4717 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4720 template <class _RandomAccessIterator, class _Compare>
4721 inline _LIBCPP_INLINE_VISIBILITY
4723 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4725 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4726 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4727 difference_type __len = __last - __first;
4728 pair<value_type*, ptrdiff_t> __buf(0, 0);
4729 unique_ptr<value_type, __return_temporary_buffer> __h;
4730 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4732 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4733 __h.reset(__buf.first);
4735 #ifdef _LIBCPP_DEBUG
4736 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4737 __debug_less<_Compare> __c(__comp);
4738 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4739 #else // _LIBCPP_DEBUG
4740 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4741 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4742 #endif // _LIBCPP_DEBUG
4745 template <class _RandomAccessIterator>
4746 inline _LIBCPP_INLINE_VISIBILITY
4748 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4750 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4755 template <class _RandomAccessIterator, class _Compare>
4756 _RandomAccessIterator
4757 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4759 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4760 difference_type __len = __last - __first;
4761 difference_type __p = 0;
4762 difference_type __c = 1;
4763 _RandomAccessIterator __pp = __first;
4766 _RandomAccessIterator __cp = __first + __c;
4767 if (__comp(*__pp, *__cp))
4773 if (__comp(*__pp, *__cp))
4782 template<class _RandomAccessIterator>
4783 inline _LIBCPP_INLINE_VISIBILITY
4784 _RandomAccessIterator
4785 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4787 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4792 template <class _RandomAccessIterator, class _Compare>
4793 inline _LIBCPP_INLINE_VISIBILITY
4795 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4797 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4800 template<class _RandomAccessIterator>
4801 inline _LIBCPP_INLINE_VISIBILITY
4803 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4805 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4810 template <class _Compare, class _RandomAccessIterator>
4812 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4813 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4815 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4818 __len = (__len - 2) / 2;
4819 _RandomAccessIterator __ptr = __first + __len;
4820 if (__comp(*__ptr, *--__last))
4822 value_type __t(_VSTD::move(*__last));
4825 *__last = _VSTD::move(*__ptr);
4829 __len = (__len - 1) / 2;
4830 __ptr = __first + __len;
4831 } while (__comp(*__ptr, __t));
4832 *__last = _VSTD::move(__t);
4837 template <class _RandomAccessIterator, class _Compare>
4838 inline _LIBCPP_INLINE_VISIBILITY
4840 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4842 #ifdef _LIBCPP_DEBUG
4843 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4844 __debug_less<_Compare> __c(__comp);
4845 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
4846 #else // _LIBCPP_DEBUG
4847 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4848 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
4849 #endif // _LIBCPP_DEBUG
4852 template <class _RandomAccessIterator>
4853 inline _LIBCPP_INLINE_VISIBILITY
4855 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4857 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4862 template <class _Compare, class _RandomAccessIterator>
4864 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4865 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4866 _RandomAccessIterator __start)
4868 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4869 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4870 // left-child of __start is at 2 * __start + 1
4871 // right-child of __start is at 2 * __start + 2
4872 difference_type __child = __start - __first;
4874 if (__len < 2 || (__len - 2) / 2 < __child)
4877 __child = 2 * __child + 1;
4878 _RandomAccessIterator __child_i = __first + __child;
4880 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4881 // right-child exists and is greater than left-child
4886 // check if we are in heap-order
4887 if (__comp(*__child_i, *__start))
4888 // we are, __start is larger than it's largest child
4891 value_type __top(_VSTD::move(*__start));
4894 // we are not in heap-order, swap the parent with it's largest child
4895 *__start = _VSTD::move(*__child_i);
4896 __start = __child_i;
4898 if ((__len - 2) / 2 < __child)
4901 // recompute the child based off of the updated parent
4902 __child = 2 * __child + 1;
4903 __child_i = __first + __child;
4905 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4906 // right-child exists and is greater than left-child
4911 // check if we are in heap-order
4912 } while (!__comp(*__child_i, __top));
4913 *__start = _VSTD::move(__top);
4916 template <class _Compare, class _RandomAccessIterator>
4917 inline _LIBCPP_INLINE_VISIBILITY
4919 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4920 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4924 swap(*__first, *--__last);
4925 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
4929 template <class _RandomAccessIterator, class _Compare>
4930 inline _LIBCPP_INLINE_VISIBILITY
4932 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4934 #ifdef _LIBCPP_DEBUG
4935 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4936 __debug_less<_Compare> __c(__comp);
4937 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4938 #else // _LIBCPP_DEBUG
4939 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4940 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4941 #endif // _LIBCPP_DEBUG
4944 template <class _RandomAccessIterator>
4945 inline _LIBCPP_INLINE_VISIBILITY
4947 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4949 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4954 template <class _Compare, class _RandomAccessIterator>
4956 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4958 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4959 difference_type __n = __last - __first;
4962 // start from the first parent, there is no need to consider children
4963 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4965 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4970 template <class _RandomAccessIterator, class _Compare>
4971 inline _LIBCPP_INLINE_VISIBILITY
4973 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4975 #ifdef _LIBCPP_DEBUG
4976 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4977 __debug_less<_Compare> __c(__comp);
4978 __make_heap<_Comp_ref>(__first, __last, __c);
4979 #else // _LIBCPP_DEBUG
4980 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4981 __make_heap<_Comp_ref>(__first, __last, __comp);
4982 #endif // _LIBCPP_DEBUG
4985 template <class _RandomAccessIterator>
4986 inline _LIBCPP_INLINE_VISIBILITY
4988 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4990 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4995 template <class _Compare, class _RandomAccessIterator>
4997 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4999 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5000 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5001 __pop_heap<_Compare>(__first, __last, __comp, __n);
5004 template <class _RandomAccessIterator, class _Compare>
5005 inline _LIBCPP_INLINE_VISIBILITY
5007 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5009 #ifdef _LIBCPP_DEBUG
5010 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5011 __debug_less<_Compare> __c(__comp);
5012 __sort_heap<_Comp_ref>(__first, __last, __c);
5013 #else // _LIBCPP_DEBUG
5014 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5015 __sort_heap<_Comp_ref>(__first, __last, __comp);
5016 #endif // _LIBCPP_DEBUG
5019 template <class _RandomAccessIterator>
5020 inline _LIBCPP_INLINE_VISIBILITY
5022 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5024 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5029 template <class _Compare, class _RandomAccessIterator>
5031 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5034 __make_heap<_Compare>(__first, __middle, __comp);
5035 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5036 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5038 if (__comp(*__i, *__first))
5040 swap(*__i, *__first);
5041 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5044 __sort_heap<_Compare>(__first, __middle, __comp);
5047 template <class _RandomAccessIterator, class _Compare>
5048 inline _LIBCPP_INLINE_VISIBILITY
5050 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5053 #ifdef _LIBCPP_DEBUG
5054 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5055 __debug_less<_Compare> __c(__comp);
5056 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
5057 #else // _LIBCPP_DEBUG
5058 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5059 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5060 #endif // _LIBCPP_DEBUG
5063 template <class _RandomAccessIterator>
5064 inline _LIBCPP_INLINE_VISIBILITY
5066 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5068 _VSTD::partial_sort(__first, __middle, __last,
5069 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5072 // partial_sort_copy
5074 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5075 _RandomAccessIterator
5076 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
5077 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5079 _RandomAccessIterator __r = __result_first;
5080 if (__r != __result_last)
5082 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5084 __make_heap<_Compare>(__result_first, __r, __comp);
5085 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5086 for (; __first != __last; ++__first)
5087 if (__comp(*__first, *__result_first))
5089 *__result_first = *__first;
5090 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5092 __sort_heap<_Compare>(__result_first, __r, __comp);
5097 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5098 inline _LIBCPP_INLINE_VISIBILITY
5099 _RandomAccessIterator
5100 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5101 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5103 #ifdef _LIBCPP_DEBUG
5104 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5105 __debug_less<_Compare> __c(__comp);
5106 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
5107 #else // _LIBCPP_DEBUG
5108 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5109 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5110 #endif // _LIBCPP_DEBUG
5113 template <class _InputIterator, class _RandomAccessIterator>
5114 inline _LIBCPP_INLINE_VISIBILITY
5115 _RandomAccessIterator
5116 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5117 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5119 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5120 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5125 template <class _Compare, class _RandomAccessIterator>
5127 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5129 // _Compare is known to be a reference type
5130 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5131 const difference_type __limit = 7;
5135 if (__nth == __last)
5137 difference_type __len = __last - __first;
5144 if (__comp(*--__last, *__first))
5145 swap(*__first, *__last);
5149 _RandomAccessIterator __m = __first;
5150 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5154 if (__len <= __limit)
5156 __selection_sort<_Compare>(__first, __last, __comp);
5159 // __len > __limit >= 3
5160 _RandomAccessIterator __m = __first + __len/2;
5161 _RandomAccessIterator __lm1 = __last;
5162 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5164 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5165 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5166 _RandomAccessIterator __i = __first;
5167 _RandomAccessIterator __j = __lm1;
5168 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5169 // The search going up is known to be guarded but the search coming down isn't.
5170 // Prime the downward search with a guard.
5171 if (!__comp(*__i, *__m)) // if *__first == *__m
5173 // *__first == *__m, *__first doesn't go in first part
5174 // manually guard downward moving __j against __i
5179 // *__first == *__m, *__m <= all other elements
5180 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5181 ++__i; // __first + 1
5183 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5188 return; // [__first, __last) all equivalent elements
5189 if (__comp(*__first, *__i))
5199 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5204 while (!__comp(*__first, *__i))
5206 while (__comp(*__first, *--__j))
5214 // [__first, __i) == *__first and *__first < [__i, __last)
5215 // The first part is sorted,
5218 // __nth_element the secod part
5219 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5223 if (__comp(*__j, *__m))
5227 break; // found guard for downward moving __j, now use unguarded partition
5232 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5233 // if not yet partitioned...
5236 // known that *(__i - 1) < *__m
5239 // __m still guards upward moving __i
5240 while (__comp(*__i, *__m))
5242 // It is now known that a guard exists for downward moving __j
5243 while (!__comp(*--__j, *__m))
5249 // It is known that __m != __j
5250 // If __m just moved, follow it
5256 // [__first, __i) < *__m and *__m <= [__i, __last)
5257 if (__i != __m && __comp(*__m, *__i))
5262 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5267 // We were given a perfectly partitioned sequence. Coincidence?
5270 // Check for [__first, __i) already sorted
5271 __j = __m = __first;
5272 while (++__j != __i)
5274 if (__comp(*__j, *__m))
5275 // not yet sorted, so sort
5279 // [__first, __i) sorted
5284 // Check for [__i, __last) already sorted
5286 while (++__j != __last)
5288 if (__comp(*__j, *__m))
5289 // not yet sorted, so sort
5293 // [__i, __last) sorted
5298 // __nth_element on range containing __nth
5301 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5306 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5312 template <class _RandomAccessIterator, class _Compare>
5313 inline _LIBCPP_INLINE_VISIBILITY
5315 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5317 #ifdef _LIBCPP_DEBUG
5318 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5319 __debug_less<_Compare> __c(__comp);
5320 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5321 #else // _LIBCPP_DEBUG
5322 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5323 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5324 #endif // _LIBCPP_DEBUG
5327 template <class _RandomAccessIterator>
5328 inline _LIBCPP_INLINE_VISIBILITY
5330 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5332 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5337 template <class _Compare, class _InputIterator1, class _InputIterator2>
5339 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5342 for (; __first2 != __last2; ++__first1)
5344 if (__first1 == __last1 || __comp(*__first2, *__first1))
5346 if (!__comp(*__first1, *__first2))
5352 template <class _InputIterator1, class _InputIterator2, class _Compare>
5353 inline _LIBCPP_INLINE_VISIBILITY
5355 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5358 #ifdef _LIBCPP_DEBUG
5359 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5360 __debug_less<_Compare> __c(__comp);
5361 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5362 #else // _LIBCPP_DEBUG
5363 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5364 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5365 #endif // _LIBCPP_DEBUG
5368 template <class _InputIterator1, class _InputIterator2>
5369 inline _LIBCPP_INLINE_VISIBILITY
5371 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5373 return _VSTD::includes(__first1, __last1, __first2, __last2,
5374 __less<typename iterator_traits<_InputIterator1>::value_type,
5375 typename iterator_traits<_InputIterator2>::value_type>());
5380 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5382 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5383 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5385 for (; __first1 != __last1; ++__result)
5387 if (__first2 == __last2)
5388 return _VSTD::copy(__first1, __last1, __result);
5389 if (__comp(*__first2, *__first1))
5391 *__result = *__first2;
5396 *__result = *__first1;
5397 if (!__comp(*__first1, *__first2))
5402 return _VSTD::copy(__first2, __last2, __result);
5405 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5406 inline _LIBCPP_INLINE_VISIBILITY
5408 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5409 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5411 #ifdef _LIBCPP_DEBUG
5412 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5413 __debug_less<_Compare> __c(__comp);
5414 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5415 #else // _LIBCPP_DEBUG
5416 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5417 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5418 #endif // _LIBCPP_DEBUG
5421 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5422 inline _LIBCPP_INLINE_VISIBILITY
5424 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5425 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5427 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5428 __less<typename iterator_traits<_InputIterator1>::value_type,
5429 typename iterator_traits<_InputIterator2>::value_type>());
5434 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5436 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5437 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5439 while (__first1 != __last1 && __first2 != __last2)
5441 if (__comp(*__first1, *__first2))
5445 if (!__comp(*__first2, *__first1))
5447 *__result = *__first1;
5457 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5458 inline _LIBCPP_INLINE_VISIBILITY
5460 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5461 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5463 #ifdef _LIBCPP_DEBUG
5464 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5465 __debug_less<_Compare> __c(__comp);
5466 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5467 #else // _LIBCPP_DEBUG
5468 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5469 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5470 #endif // _LIBCPP_DEBUG
5473 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5474 inline _LIBCPP_INLINE_VISIBILITY
5476 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5477 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5479 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5480 __less<typename iterator_traits<_InputIterator1>::value_type,
5481 typename iterator_traits<_InputIterator2>::value_type>());
5486 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5488 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5489 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5491 while (__first1 != __last1)
5493 if (__first2 == __last2)
5494 return _VSTD::copy(__first1, __last1, __result);
5495 if (__comp(*__first1, *__first2))
5497 *__result = *__first1;
5503 if (!__comp(*__first2, *__first1))
5511 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5512 inline _LIBCPP_INLINE_VISIBILITY
5514 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5515 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5517 #ifdef _LIBCPP_DEBUG
5518 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5519 __debug_less<_Compare> __c(__comp);
5520 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5521 #else // _LIBCPP_DEBUG
5522 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5523 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5524 #endif // _LIBCPP_DEBUG
5527 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5528 inline _LIBCPP_INLINE_VISIBILITY
5530 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5531 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5533 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5534 __less<typename iterator_traits<_InputIterator1>::value_type,
5535 typename iterator_traits<_InputIterator2>::value_type>());
5538 // set_symmetric_difference
5540 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5542 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5543 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5545 while (__first1 != __last1)
5547 if (__first2 == __last2)
5548 return _VSTD::copy(__first1, __last1, __result);
5549 if (__comp(*__first1, *__first2))
5551 *__result = *__first1;
5557 if (__comp(*__first2, *__first1))
5559 *__result = *__first2;
5567 return _VSTD::copy(__first2, __last2, __result);
5570 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5571 inline _LIBCPP_INLINE_VISIBILITY
5573 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5574 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5576 #ifdef _LIBCPP_DEBUG
5577 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5578 __debug_less<_Compare> __c(__comp);
5579 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5580 #else // _LIBCPP_DEBUG
5581 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5582 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5583 #endif // _LIBCPP_DEBUG
5586 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5587 inline _LIBCPP_INLINE_VISIBILITY
5589 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5590 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5592 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5593 __less<typename iterator_traits<_InputIterator1>::value_type,
5594 typename iterator_traits<_InputIterator2>::value_type>());
5597 // lexicographical_compare
5599 template <class _Compare, class _InputIterator1, class _InputIterator2>
5601 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5602 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5604 for (; __first2 != __last2; ++__first1, (void) ++__first2)
5606 if (__first1 == __last1 || __comp(*__first1, *__first2))
5608 if (__comp(*__first2, *__first1))
5614 template <class _InputIterator1, class _InputIterator2, class _Compare>
5615 inline _LIBCPP_INLINE_VISIBILITY
5617 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5618 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5620 #ifdef _LIBCPP_DEBUG
5621 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5622 __debug_less<_Compare> __c(__comp);
5623 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5624 #else // _LIBCPP_DEBUG
5625 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5626 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5627 #endif // _LIBCPP_DEBUG
5630 template <class _InputIterator1, class _InputIterator2>
5631 inline _LIBCPP_INLINE_VISIBILITY
5633 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5634 _InputIterator2 __first2, _InputIterator2 __last2)
5636 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5637 __less<typename iterator_traits<_InputIterator1>::value_type,
5638 typename iterator_traits<_InputIterator2>::value_type>());
5643 template <class _Compare, class _BidirectionalIterator>
5645 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5647 _BidirectionalIterator __i = __last;
5648 if (__first == __last || __first == --__i)
5652 _BidirectionalIterator __ip1 = __i;
5653 if (__comp(*--__i, *__ip1))
5655 _BidirectionalIterator __j = __last;
5656 while (!__comp(*__i, *--__j))
5659 _VSTD::reverse(__ip1, __last);
5664 _VSTD::reverse(__first, __last);
5670 template <class _BidirectionalIterator, class _Compare>
5671 inline _LIBCPP_INLINE_VISIBILITY
5673 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5675 #ifdef _LIBCPP_DEBUG
5676 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5677 __debug_less<_Compare> __c(__comp);
5678 return __next_permutation<_Comp_ref>(__first, __last, __c);
5679 #else // _LIBCPP_DEBUG
5680 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5681 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5682 #endif // _LIBCPP_DEBUG
5685 template <class _BidirectionalIterator>
5686 inline _LIBCPP_INLINE_VISIBILITY
5688 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5690 return _VSTD::next_permutation(__first, __last,
5691 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5696 template <class _Compare, class _BidirectionalIterator>
5698 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5700 _BidirectionalIterator __i = __last;
5701 if (__first == __last || __first == --__i)
5705 _BidirectionalIterator __ip1 = __i;
5706 if (__comp(*__ip1, *--__i))
5708 _BidirectionalIterator __j = __last;
5709 while (!__comp(*--__j, *__i))
5712 _VSTD::reverse(__ip1, __last);
5717 _VSTD::reverse(__first, __last);
5723 template <class _BidirectionalIterator, class _Compare>
5724 inline _LIBCPP_INLINE_VISIBILITY
5726 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5728 #ifdef _LIBCPP_DEBUG
5729 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5730 __debug_less<_Compare> __c(__comp);
5731 return __prev_permutation<_Comp_ref>(__first, __last, __c);
5732 #else // _LIBCPP_DEBUG
5733 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5734 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5735 #endif // _LIBCPP_DEBUG
5738 template <class _BidirectionalIterator>
5739 inline _LIBCPP_INLINE_VISIBILITY
5741 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5743 return _VSTD::prev_permutation(__first, __last,
5744 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5747 template <class _Tp>
5748 inline _LIBCPP_INLINE_VISIBILITY
5751 is_integral<_Tp>::value,
5754 __rotate_left(_Tp __t, _Tp __n = 1)
5756 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5758 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5761 template <class _Tp>
5762 inline _LIBCPP_INLINE_VISIBILITY
5765 is_integral<_Tp>::value,
5768 __rotate_right(_Tp __t, _Tp __n = 1)
5770 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5772 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5775 _LIBCPP_END_NAMESPACE_STD
5777 #endif // _LIBCPP_ALGORITHM