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);
286 template <class RandomAccessIterator, class RandomNumberGenerator>
288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand);
290 template<class RandomAccessIterator, class UniformRandomNumberGenerator>
291 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
292 UniformRandomNumberGenerator&& g);
294 template <class InputIterator, class Predicate>
296 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
298 template <class ForwardIterator, class Predicate>
300 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
302 template <class InputIterator, class OutputIterator1,
303 class OutputIterator2, class Predicate>
304 pair<OutputIterator1, OutputIterator2>
305 partition_copy(InputIterator first, InputIterator last,
306 OutputIterator1 out_true, OutputIterator2 out_false,
309 template <class ForwardIterator, class Predicate>
311 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313 template<class ForwardIterator, class Predicate>
315 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
317 template <class ForwardIterator>
319 is_sorted(ForwardIterator first, ForwardIterator last);
321 template <class ForwardIterator, class Compare>
323 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
325 template<class ForwardIterator>
327 is_sorted_until(ForwardIterator first, ForwardIterator last);
329 template <class ForwardIterator, class Compare>
331 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
333 template <class RandomAccessIterator>
335 sort(RandomAccessIterator first, RandomAccessIterator last);
337 template <class RandomAccessIterator, class Compare>
339 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
341 template <class RandomAccessIterator>
343 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
345 template <class RandomAccessIterator, class Compare>
347 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
349 template <class RandomAccessIterator>
351 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
353 template <class RandomAccessIterator, class Compare>
355 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
357 template <class InputIterator, class RandomAccessIterator>
359 partial_sort_copy(InputIterator first, InputIterator last,
360 RandomAccessIterator result_first, RandomAccessIterator result_last);
362 template <class InputIterator, class RandomAccessIterator, class Compare>
364 partial_sort_copy(InputIterator first, InputIterator last,
365 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
367 template <class RandomAccessIterator>
369 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
371 template <class RandomAccessIterator, class Compare>
373 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
375 template <class ForwardIterator, class T>
377 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
379 template <class ForwardIterator, class T, class Compare>
381 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
383 template <class ForwardIterator, class T>
385 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
387 template <class ForwardIterator, class T, class Compare>
389 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
391 template <class ForwardIterator, class T>
392 pair<ForwardIterator, ForwardIterator>
393 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
395 template <class ForwardIterator, class T, class Compare>
396 pair<ForwardIterator, ForwardIterator>
397 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
399 template <class ForwardIterator, class T>
401 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
403 template <class ForwardIterator, class T, class Compare>
405 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
407 template <class InputIterator1, class InputIterator2, class OutputIterator>
409 merge(InputIterator1 first1, InputIterator1 last1,
410 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
412 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
414 merge(InputIterator1 first1, InputIterator1 last1,
415 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
417 template <class BidirectionalIterator>
419 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
421 template <class BidirectionalIterator, class Compare>
423 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
425 template <class InputIterator1, class InputIterator2>
427 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
429 template <class InputIterator1, class InputIterator2, class Compare>
431 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
433 template <class InputIterator1, class InputIterator2, class OutputIterator>
435 set_union(InputIterator1 first1, InputIterator1 last1,
436 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
438 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
440 set_union(InputIterator1 first1, InputIterator1 last1,
441 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
443 template <class InputIterator1, class InputIterator2, class OutputIterator>
445 set_intersection(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 set_intersection(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453 template <class InputIterator1, class InputIterator2, class OutputIterator>
455 set_difference(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460 set_difference(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463 template <class InputIterator1, class InputIterator2, class OutputIterator>
465 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473 template <class RandomAccessIterator>
475 push_heap(RandomAccessIterator first, RandomAccessIterator last);
477 template <class RandomAccessIterator, class Compare>
479 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
481 template <class RandomAccessIterator>
483 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
485 template <class RandomAccessIterator, class Compare>
487 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
489 template <class RandomAccessIterator>
491 make_heap(RandomAccessIterator first, RandomAccessIterator last);
493 template <class RandomAccessIterator, class Compare>
495 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
497 template <class RandomAccessIterator>
499 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
501 template <class RandomAccessIterator, class Compare>
503 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
505 template <class RandomAccessIterator>
507 is_heap(RandomAccessIterator first, RandomAccessiterator last);
509 template <class RandomAccessIterator, class Compare>
511 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
513 template <class RandomAccessIterator>
515 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
517 template <class RandomAccessIterator, class Compare>
519 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
521 template <class ForwardIterator>
523 min_element(ForwardIterator first, ForwardIterator last);
525 template <class ForwardIterator, class Compare>
527 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
531 min(const T& a, const T& b);
533 template <class T, class Compare>
535 min(const T& a, const T& b, Compare comp);
539 min(initializer_list<T> t);
541 template<class T, class Compare>
543 min(initializer_list<T> t, Compare comp);
545 template <class ForwardIterator>
547 max_element(ForwardIterator first, ForwardIterator last);
549 template <class ForwardIterator, class Compare>
551 max_element(ForwardIterator first, ForwardIterator last, Compare comp);
555 max(const T& a, const T& b);
557 template <class T, class Compare>
559 max(const T& a, const T& b, Compare comp);
563 max(initializer_list<T> t);
565 template<class T, class Compare>
567 max(initializer_list<T> t, Compare comp);
569 template<class ForwardIterator>
570 pair<ForwardIterator, ForwardIterator>
571 minmax_element(ForwardIterator first, ForwardIterator last);
573 template<class ForwardIterator, class Compare>
574 pair<ForwardIterator, ForwardIterator>
575 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
578 pair<const T&, const T&>
579 minmax(const T& a, const T& b);
581 template<class T, class Compare>
582 pair<const T&, const T&>
583 minmax(const T& a, const T& b, Compare comp);
587 minmax(initializer_list<T> t);
589 template<class T, class Compare>
591 minmax(initializer_list<T> t, Compare comp);
593 template <class InputIterator1, class InputIterator2>
595 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
597 template <class InputIterator1, class InputIterator2, class Compare>
599 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
600 InputIterator2 first2, InputIterator2 last2, Compare comp);
602 template <class BidirectionalIterator>
604 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
606 template <class BidirectionalIterator, class Compare>
608 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
610 template <class BidirectionalIterator>
612 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
614 template <class BidirectionalIterator, class Compare>
616 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
623 #include <initializer_list>
624 #include <type_traits>
631 #include <__undef_min_max>
633 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
634 #pragma GCC system_header
637 _LIBCPP_BEGIN_NAMESPACE_STD
639 template <class _T1, class _T2 = _T1>
642 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
643 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
644 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
645 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
649 struct __equal_to<_T1, _T1>
651 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
655 struct __equal_to<const _T1, _T1>
657 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
661 struct __equal_to<_T1, const _T1>
663 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
666 template <class _T1, class _T2 = _T1>
669 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
670 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
671 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
672 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
676 struct __less<_T1, _T1>
678 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
682 struct __less<const _T1, _T1>
684 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
688 struct __less<_T1, const _T1>
690 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
693 template <class _Predicate>
699 _LIBCPP_INLINE_VISIBILITY __negate() {}
701 _LIBCPP_INLINE_VISIBILITY
702 explicit __negate(_Predicate __p) : __p_(__p) {}
705 _LIBCPP_INLINE_VISIBILITY
706 bool operator()(const _T1& __x) {return !__p_(__x);}
708 template <class _T1, class _T2>
709 _LIBCPP_INLINE_VISIBILITY
710 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
713 #ifdef _LIBCPP_DEBUG2
715 template <class _Compare>
719 __debug_less(_Compare& __c) : __comp_(__c) {}
720 template <class _Tp, class _Up>
721 bool operator()(const _Tp& __x, const _Up& __y)
723 bool __r = __comp_(__x, __y);
725 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
730 #endif // _LIBCPP_DEBUG2
732 // Precondition: __x != 0
733 inline _LIBCPP_INLINE_VISIBILITY
737 return static_cast<unsigned>(__builtin_ctz(__x));
740 inline _LIBCPP_INLINE_VISIBILITY
742 __ctz(unsigned long __x)
744 return static_cast<unsigned long>(__builtin_ctzl(__x));
747 inline _LIBCPP_INLINE_VISIBILITY
749 __ctz(unsigned long long __x)
751 return static_cast<unsigned long long>(__builtin_ctzll(__x));
754 // Precondition: __x != 0
755 inline _LIBCPP_INLINE_VISIBILITY
759 return static_cast<unsigned>(__builtin_clz(__x));
762 inline _LIBCPP_INLINE_VISIBILITY
764 __clz(unsigned long __x)
766 return static_cast<unsigned long>(__builtin_clzl (__x));
769 inline _LIBCPP_INLINE_VISIBILITY
771 __clz(unsigned long long __x)
773 return static_cast<unsigned long long>(__builtin_clzll(__x));
776 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
777 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
778 inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
782 template <class _InputIterator, class _Predicate>
783 inline _LIBCPP_INLINE_VISIBILITY
785 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
787 for (; __first != __last; ++__first)
788 if (!__pred(*__first))
795 template <class _InputIterator, class _Predicate>
796 inline _LIBCPP_INLINE_VISIBILITY
798 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
800 for (; __first != __last; ++__first)
801 if (__pred(*__first))
808 template <class _InputIterator, class _Predicate>
809 inline _LIBCPP_INLINE_VISIBILITY
811 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
813 for (; __first != __last; ++__first)
814 if (__pred(*__first))
821 template <class _InputIterator, class _Function>
822 inline _LIBCPP_INLINE_VISIBILITY
824 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
826 for (; __first != __last; ++__first)
828 return _VSTD::move(__f);
833 template <class _InputIterator, class _Tp>
834 inline _LIBCPP_INLINE_VISIBILITY
836 find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
838 for (; __first != __last; ++__first)
839 if (*__first == __value_)
846 template <class _InputIterator, class _Predicate>
847 inline _LIBCPP_INLINE_VISIBILITY
849 find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
851 for (; __first != __last; ++__first)
852 if (__pred(*__first))
859 template<class _InputIterator, class _Predicate>
860 inline _LIBCPP_INLINE_VISIBILITY
862 find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
864 for (; __first != __last; ++__first)
865 if (!__pred(*__first))
872 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
874 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
875 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
876 forward_iterator_tag, forward_iterator_tag)
878 // modeled after search algorithm
879 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
880 if (__first2 == __last2)
886 if (__first1 == __last1) // if source exhausted return last correct answer
887 return __r; // (or __last1 if never found)
888 if (__pred(*__first1, *__first2))
892 // *__first1 matches *__first2, now match elements after here
893 _ForwardIterator1 __m1 = __first1;
894 _ForwardIterator2 __m2 = __first2;
897 if (++__m2 == __last2)
898 { // Pattern exhaused, record answer and search for another one
903 if (++__m1 == __last1) // Source exhausted, return last answer
905 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
909 } // else there is a match, check next elements
914 template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
915 _BidirectionalIterator1
916 __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
917 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
918 bidirectional_iterator_tag, bidirectional_iterator_tag)
920 // modeled after search algorithm (in reverse)
921 if (__first2 == __last2)
922 return __last1; // Everything matches an empty sequence
923 _BidirectionalIterator1 __l1 = __last1;
924 _BidirectionalIterator2 __l2 = __last2;
928 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
931 if (__first1 == __l1) // return __last1 if no element matches *__first2
933 if (__pred(*--__l1, *__l2))
936 // *__l1 matches *__l2, now match elements before here
937 _BidirectionalIterator1 __m1 = __l1;
938 _BidirectionalIterator2 __m2 = __l2;
941 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
943 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
945 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
948 } // else there is a match, check next elements
953 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
954 _RandomAccessIterator1
955 __find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
956 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
957 random_access_iterator_tag, random_access_iterator_tag)
959 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
960 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
963 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
966 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
967 _RandomAccessIterator1 __l1 = __last1;
968 _RandomAccessIterator2 __l2 = __last2;
976 if (__pred(*--__l1, *__l2))
979 _RandomAccessIterator1 __m1 = __l1;
980 _RandomAccessIterator2 __m2 = __l2;
983 if (__m2 == __first2)
985 // no need to check range on __m1 because __s guarantees we have enough source
986 if (!__pred(*--__m1, *--__m2))
994 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
995 inline _LIBCPP_INLINE_VISIBILITY
997 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
998 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1000 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1001 (__first1, __last1, __first2, __last2, __pred,
1002 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1003 typename iterator_traits<_ForwardIterator2>::iterator_category());
1006 template <class _ForwardIterator1, class _ForwardIterator2>
1007 inline _LIBCPP_INLINE_VISIBILITY
1009 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1010 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1012 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1013 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1014 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1019 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1021 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1022 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1024 for (; __first1 != __last1; ++__first1)
1025 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1026 if (__pred(*__first1, *__j))
1031 template <class _ForwardIterator1, class _ForwardIterator2>
1032 inline _LIBCPP_INLINE_VISIBILITY
1034 find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1035 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1037 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1038 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1039 return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1044 template <class _ForwardIterator, class _BinaryPredicate>
1045 inline _LIBCPP_INLINE_VISIBILITY
1047 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1049 if (__first != __last)
1051 _ForwardIterator __i = __first;
1052 while (++__i != __last)
1054 if (__pred(*__first, *__i))
1062 template <class _ForwardIterator>
1063 inline _LIBCPP_INLINE_VISIBILITY
1065 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1067 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1068 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1073 template <class _InputIterator, class _Tp>
1074 inline _LIBCPP_INLINE_VISIBILITY
1075 typename iterator_traits<_InputIterator>::difference_type
1076 count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1078 typename iterator_traits<_InputIterator>::difference_type __r(0);
1079 for (; __first != __last; ++__first)
1080 if (*__first == __value_)
1087 template <class _InputIterator, class _Predicate>
1088 inline _LIBCPP_INLINE_VISIBILITY
1089 typename iterator_traits<_InputIterator>::difference_type
1090 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1092 typename iterator_traits<_InputIterator>::difference_type __r(0);
1093 for (; __first != __last; ++__first)
1094 if (__pred(*__first))
1101 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1102 inline _LIBCPP_INLINE_VISIBILITY
1103 pair<_InputIterator1, _InputIterator2>
1104 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1105 _InputIterator2 __first2, _BinaryPredicate __pred)
1107 for (; __first1 != __last1; ++__first1, ++__first2)
1108 if (!__pred(*__first1, *__first2))
1110 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1113 template <class _InputIterator1, class _InputIterator2>
1114 inline _LIBCPP_INLINE_VISIBILITY
1115 pair<_InputIterator1, _InputIterator2>
1116 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1118 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1119 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1120 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1123 #if _LIBCPP_STD_VER > 11
1124 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1125 inline _LIBCPP_INLINE_VISIBILITY
1126 pair<_InputIterator1, _InputIterator2>
1127 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1128 _InputIterator2 __first2, _InputIterator2 __last2,
1129 _BinaryPredicate __pred)
1131 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1132 if (!__pred(*__first1, *__first2))
1134 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1137 template <class _InputIterator1, class _InputIterator2>
1138 inline _LIBCPP_INLINE_VISIBILITY
1139 pair<_InputIterator1, _InputIterator2>
1140 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141 _InputIterator2 __first2, _InputIterator2 __last2)
1143 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1144 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1145 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1151 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1152 inline _LIBCPP_INLINE_VISIBILITY
1154 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1156 for (; __first1 != __last1; ++__first1, ++__first2)
1157 if (!__pred(*__first1, *__first2))
1162 template <class _InputIterator1, class _InputIterator2>
1163 inline _LIBCPP_INLINE_VISIBILITY
1165 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1167 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1168 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1169 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1172 #if _LIBCPP_STD_VER > 11
1173 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1174 inline _LIBCPP_INLINE_VISIBILITY
1176 __equal(_InputIterator1 __first1, _InputIterator1 __last1,
1177 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1178 input_iterator_tag, input_iterator_tag )
1180 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1181 if (!__pred(*__first1, *__first2))
1183 return __first1 == __last1 && __first2 == __last2;
1186 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1187 inline _LIBCPP_INLINE_VISIBILITY
1189 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1190 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1191 random_access_iterator_tag, random_access_iterator_tag )
1193 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1195 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1196 typename add_lvalue_reference<_BinaryPredicate>::type>
1197 (__first1, __last1, __first2, __pred );
1200 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1201 inline _LIBCPP_INLINE_VISIBILITY
1203 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1204 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1206 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1207 (__first1, __last1, __first2, __last2, __pred,
1208 typename iterator_traits<_InputIterator1>::iterator_category(),
1209 typename iterator_traits<_InputIterator2>::iterator_category());
1212 template <class _InputIterator1, class _InputIterator2>
1213 inline _LIBCPP_INLINE_VISIBILITY
1215 equal(_InputIterator1 __first1, _InputIterator1 __last1,
1216 _InputIterator2 __first2, _InputIterator2 __last2)
1218 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1219 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1220 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1221 typename iterator_traits<_InputIterator1>::iterator_category(),
1222 typename iterator_traits<_InputIterator2>::iterator_category());
1228 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1230 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1231 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1233 // shorten sequences as much as possible by lopping of any equal parts
1234 for (; __first1 != __last1; ++__first1, ++__first2)
1235 if (!__pred(*__first1, *__first2))
1239 // __first1 != __last1 && *__first1 != *__first2
1240 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1241 _D1 __l1 = _VSTD::distance(__first1, __last1);
1244 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1245 // For each element in [f1, l1) see if there are the same number of
1246 // equal elements in [f2, l2)
1247 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1249 // Have we already counted the number of *__i in [f1, l1)?
1250 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1251 if (__pred(*__j, *__i))
1254 // Count number of *__i in [f2, l2)
1256 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1257 if (__pred(*__i, *__j))
1261 // Count number of *__i in [__i, l1) (we can start with 1)
1263 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1264 if (__pred(*__i, *__j))
1274 template<class _ForwardIterator1, class _ForwardIterator2>
1275 inline _LIBCPP_INLINE_VISIBILITY
1277 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1278 _ForwardIterator2 __first2)
1280 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1281 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1282 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1285 #if _LIBCPP_STD_VER > 11
1286 template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1288 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1289 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1290 _BinaryPredicate __pred,
1291 forward_iterator_tag, forward_iterator_tag )
1293 // shorten sequences as much as possible by lopping of any equal parts
1294 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1295 if (!__pred(*__first1, *__first2))
1297 return __first1 == __last1 && __first2 == __last2;
1299 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1300 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1301 _D1 __l1 = _VSTD::distance(__first1, __last1);
1303 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1304 _D2 __l2 = _VSTD::distance(__first2, __last2);
1308 // For each element in [f1, l1) see if there are the same number of
1309 // equal elements in [f2, l2)
1310 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1312 // Have we already counted the number of *__i in [f1, l1)?
1313 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1314 if (__pred(*__j, *__i))
1317 // Count number of *__i in [f2, l2)
1319 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1320 if (__pred(*__i, *__j))
1324 // Count number of *__i in [__i, l1) (we can start with 1)
1326 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1327 if (__pred(*__i, *__j))
1337 template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1339 __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1340 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1341 _BinaryPredicate __pred,
1342 random_access_iterator_tag, random_access_iterator_tag )
1344 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1346 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1347 typename add_lvalue_reference<_BinaryPredicate>::type>
1348 (__first1, __last1, __first2, __pred );
1351 template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1352 inline _LIBCPP_INLINE_VISIBILITY
1354 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1355 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1356 _BinaryPredicate __pred )
1358 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1359 (__first1, __last1, __first2, __last2, __pred,
1360 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1361 typename iterator_traits<_ForwardIterator2>::iterator_category());
1364 template<class _ForwardIterator1, class _ForwardIterator2>
1365 inline _LIBCPP_INLINE_VISIBILITY
1367 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1368 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1370 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1371 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1372 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1373 __equal_to<__v1, __v2>(),
1374 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1375 typename iterator_traits<_ForwardIterator2>::iterator_category());
1381 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1383 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1384 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1385 forward_iterator_tag, forward_iterator_tag)
1387 if (__first2 == __last2)
1388 return __first1; // Everything matches an empty sequence
1391 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1394 if (__first1 == __last1) // return __last1 if no element matches *__first2
1396 if (__pred(*__first1, *__first2))
1400 // *__first1 matches *__first2, now match elements after here
1401 _ForwardIterator1 __m1 = __first1;
1402 _ForwardIterator2 __m2 = __first2;
1405 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1407 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1409 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1413 } // else there is a match, check next elements
1418 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1419 _RandomAccessIterator1
1420 __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1421 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1422 random_access_iterator_tag, random_access_iterator_tag)
1424 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1425 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1426 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1427 _D2 __len2 = __last2 - __first2;
1430 _D1 __len1 = __last1 - __first1;
1431 if (__len1 < __len2)
1433 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1436 #if !_LIBCPP_UNROLL_LOOPS
1439 if (__first1 == __s)
1441 if (__pred(*__first1, *__first2))
1445 #else // !_LIBCPP_UNROLL_LOOPS
1446 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1448 if (__pred(*__first1, *__first2))
1450 if (__pred(*++__first1, *__first2))
1452 if (__pred(*++__first1, *__first2))
1454 if (__pred(*++__first1, *__first2))
1458 switch (__s - __first1)
1461 if (__pred(*__first1, *__first2))
1465 if (__pred(*__first1, *__first2))
1469 if (__pred(*__first1, *__first2))
1475 #endif // !_LIBCPP_UNROLL_LOOPS
1476 _RandomAccessIterator1 __m1 = __first1;
1477 _RandomAccessIterator2 __m2 = __first2;
1478 #if !_LIBCPP_UNROLL_LOOPS
1481 if (++__m2 == __last2)
1483 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1484 if (!__pred(*__m1, *__m2))
1490 #else // !_LIBCPP_UNROLL_LOOPS
1493 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1495 if (!__pred(*__m1, *__m2))
1497 if (!__pred(*++__m1, *++__m2))
1499 if (!__pred(*++__m1, *++__m2))
1501 if (!__pred(*++__m1, *++__m2))
1506 switch (__last2 - __m2)
1509 if (!__pred(*__m1, *__m2))
1514 if (!__pred(*__m1, *__m2))
1519 if (!__pred(*__m1, *__m2))
1526 #endif // !_LIBCPP_UNROLL_LOOPS
1530 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1531 inline _LIBCPP_INLINE_VISIBILITY
1533 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1534 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1536 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1537 (__first1, __last1, __first2, __last2, __pred,
1538 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1539 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1542 template <class _ForwardIterator1, class _ForwardIterator2>
1543 inline _LIBCPP_INLINE_VISIBILITY
1545 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1546 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1548 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1549 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1550 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1555 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1557 __search_n(_ForwardIterator __first, _ForwardIterator __last,
1558 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1564 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1567 if (__first == __last) // return __last if no element matches __value_
1569 if (__pred(*__first, __value_))
1573 // *__first matches __value_, now match elements after here
1574 _ForwardIterator __m = __first;
1578 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1580 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1582 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1587 } // else there is a match, check next elements
1592 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1593 _RandomAccessIterator
1594 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1595 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1599 _Size __len = static_cast<_Size>(__last - __first);
1600 if (__len < __count)
1602 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1605 // Find first element in sequence that matchs __value_, with a mininum of loop checks
1608 if (__first >= __s) // return __last if no element matches __value_
1610 if (__pred(*__first, __value_))
1614 // *__first matches __value_, now match elements after here
1615 _RandomAccessIterator __m = __first;
1619 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1621 ++__m; // no need to check range on __m because __s guarantees we have enough source
1622 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
1627 } // else there is a match, check next elements
1632 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1633 inline _LIBCPP_INLINE_VISIBILITY
1635 search_n(_ForwardIterator __first, _ForwardIterator __last,
1636 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1638 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1639 (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1642 template <class _ForwardIterator, class _Size, class _Tp>
1643 inline _LIBCPP_INLINE_VISIBILITY
1645 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1647 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1648 return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
1653 template <class _Iter>
1654 struct __libcpp_is_trivial_iterator
1656 static const bool value = is_pointer<_Iter>::value;
1659 template <class _Iter>
1660 struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1662 static const bool value = is_pointer<_Iter>::value;
1665 template <class _Iter>
1666 struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1668 static const bool value = is_pointer<_Iter>::value;
1671 template <class _Iter>
1672 inline _LIBCPP_INLINE_VISIBILITY
1674 __unwrap_iter(_Iter __i)
1679 template <class _Tp>
1680 inline _LIBCPP_INLINE_VISIBILITY
1683 is_trivially_copy_assignable<_Tp>::value,
1686 __unwrap_iter(move_iterator<_Tp*> __i)
1691 template <class _Tp>
1692 inline _LIBCPP_INLINE_VISIBILITY
1695 is_trivially_copy_assignable<_Tp>::value,
1698 __unwrap_iter(__wrap_iter<_Tp*> __i)
1703 template <class _InputIterator, class _OutputIterator>
1704 inline _LIBCPP_INLINE_VISIBILITY
1706 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1708 for (; __first != __last; ++__first, ++__result)
1709 *__result = *__first;
1713 template <class _Tp, class _Up>
1714 inline _LIBCPP_INLINE_VISIBILITY
1717 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1718 is_trivially_copy_assignable<_Up>::value,
1721 __copy(_Tp* __first, _Tp* __last, _Up* __result)
1723 const size_t __n = static_cast<size_t>(__last - __first);
1724 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1725 return __result + __n;
1728 template <class _InputIterator, class _OutputIterator>
1729 inline _LIBCPP_INLINE_VISIBILITY
1731 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1733 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1738 template <class _BidirectionalIterator, class _OutputIterator>
1739 inline _LIBCPP_INLINE_VISIBILITY
1741 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1743 while (__first != __last)
1744 *--__result = *--__last;
1748 template <class _Tp, class _Up>
1749 inline _LIBCPP_INLINE_VISIBILITY
1752 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1753 is_trivially_copy_assignable<_Up>::value,
1756 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1758 const size_t __n = static_cast<size_t>(__last - __first);
1760 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1764 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1765 inline _LIBCPP_INLINE_VISIBILITY
1766 _BidirectionalIterator2
1767 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1768 _BidirectionalIterator2 __result)
1770 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1775 template<class _InputIterator, class _OutputIterator, class _Predicate>
1776 inline _LIBCPP_INLINE_VISIBILITY
1778 copy_if(_InputIterator __first, _InputIterator __last,
1779 _OutputIterator __result, _Predicate __pred)
1781 for (; __first != __last; ++__first)
1783 if (__pred(*__first))
1785 *__result = *__first;
1794 template<class _InputIterator, class _Size, class _OutputIterator>
1795 inline _LIBCPP_INLINE_VISIBILITY
1798 __is_input_iterator<_InputIterator>::value &&
1799 !__is_random_access_iterator<_InputIterator>::value,
1802 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1806 *__result = *__first;
1808 for (--__n; __n > 0; --__n)
1811 *__result = *__first;
1818 template<class _InputIterator, class _Size, class _OutputIterator>
1819 inline _LIBCPP_INLINE_VISIBILITY
1822 __is_random_access_iterator<_InputIterator>::value,
1825 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1827 return _VSTD::copy(__first, __first + __n, __result);
1832 template <class _InputIterator, class _OutputIterator>
1833 inline _LIBCPP_INLINE_VISIBILITY
1835 __move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1837 for (; __first != __last; ++__first, ++__result)
1838 *__result = _VSTD::move(*__first);
1842 template <class _Tp, class _Up>
1843 inline _LIBCPP_INLINE_VISIBILITY
1846 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1847 is_trivially_copy_assignable<_Up>::value,
1850 __move(_Tp* __first, _Tp* __last, _Up* __result)
1852 const size_t __n = static_cast<size_t>(__last - __first);
1853 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1854 return __result + __n;
1857 template <class _InputIterator, class _OutputIterator>
1858 inline _LIBCPP_INLINE_VISIBILITY
1860 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1862 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1867 template <class _InputIterator, class _OutputIterator>
1868 inline _LIBCPP_INLINE_VISIBILITY
1870 __move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1872 while (__first != __last)
1873 *--__result = _VSTD::move(*--__last);
1877 template <class _Tp, class _Up>
1878 inline _LIBCPP_INLINE_VISIBILITY
1881 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1882 is_trivially_copy_assignable<_Up>::value,
1885 __move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1887 const size_t __n = static_cast<size_t>(__last - __first);
1889 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1893 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1894 inline _LIBCPP_INLINE_VISIBILITY
1895 _BidirectionalIterator2
1896 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1897 _BidirectionalIterator2 __result)
1899 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1904 // moved to <type_traits> for better swap / noexcept support
1908 template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1909 inline _LIBCPP_INLINE_VISIBILITY
1911 transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1913 for (; __first != __last; ++__first, ++__result)
1914 *__result = __op(*__first);
1918 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1919 inline _LIBCPP_INLINE_VISIBILITY
1921 transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1922 _OutputIterator __result, _BinaryOperation __binary_op)
1924 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
1925 *__result = __binary_op(*__first1, *__first2);
1931 template <class _ForwardIterator, class _Tp>
1932 inline _LIBCPP_INLINE_VISIBILITY
1934 replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1936 for (; __first != __last; ++__first)
1937 if (*__first == __old_value)
1938 *__first = __new_value;
1943 template <class _ForwardIterator, class _Predicate, class _Tp>
1944 inline _LIBCPP_INLINE_VISIBILITY
1946 replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1948 for (; __first != __last; ++__first)
1949 if (__pred(*__first))
1950 *__first = __new_value;
1955 template <class _InputIterator, class _OutputIterator, class _Tp>
1956 inline _LIBCPP_INLINE_VISIBILITY
1958 replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1959 const _Tp& __old_value, const _Tp& __new_value)
1961 for (; __first != __last; ++__first, ++__result)
1962 if (*__first == __old_value)
1963 *__result = __new_value;
1965 *__result = *__first;
1971 template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
1972 inline _LIBCPP_INLINE_VISIBILITY
1974 replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1975 _Predicate __pred, const _Tp& __new_value)
1977 for (; __first != __last; ++__first, ++__result)
1978 if (__pred(*__first))
1979 *__result = __new_value;
1981 *__result = *__first;
1987 template <class _OutputIterator, class _Size, class _Tp>
1988 inline _LIBCPP_INLINE_VISIBILITY
1990 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, false_type)
1992 for (; __n > 0; ++__first, --__n)
1993 *__first = __value_;
1997 template <class _OutputIterator, class _Size, class _Tp>
1998 inline _LIBCPP_INLINE_VISIBILITY
2000 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, true_type)
2003 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2004 return __first + __n;
2007 template <class _OutputIterator, class _Size, class _Tp>
2008 inline _LIBCPP_INLINE_VISIBILITY
2010 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2012 return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool,
2013 is_pointer<_OutputIterator>::value &&
2014 is_trivially_copy_assignable<_Tp>::value &&
2015 sizeof(_Tp) == 1>());
2020 template <class _ForwardIterator, class _Tp>
2021 inline _LIBCPP_INLINE_VISIBILITY
2023 __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2025 for (; __first != __last; ++__first)
2026 *__first = __value_;
2029 template <class _RandomAccessIterator, class _Tp>
2030 inline _LIBCPP_INLINE_VISIBILITY
2032 __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2034 _VSTD::fill_n(__first, __last - __first, __value_);
2037 template <class _ForwardIterator, class _Tp>
2038 inline _LIBCPP_INLINE_VISIBILITY
2040 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2042 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2047 template <class _ForwardIterator, class _Generator>
2048 inline _LIBCPP_INLINE_VISIBILITY
2050 generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2052 for (; __first != __last; ++__first)
2058 template <class _OutputIterator, class _Size, class _Generator>
2059 inline _LIBCPP_INLINE_VISIBILITY
2061 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
2063 for (; __n > 0; ++__first, --__n)
2070 template <class _ForwardIterator, class _Tp>
2072 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2074 __first = _VSTD::find(__first, __last, __value_);
2075 if (__first != __last)
2077 _ForwardIterator __i = __first;
2078 while (++__i != __last)
2080 if (!(*__i == __value_))
2082 *__first = _VSTD::move(*__i);
2092 template <class _ForwardIterator, class _Predicate>
2094 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2096 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2097 (__first, __last, __pred);
2098 if (__first != __last)
2100 _ForwardIterator __i = __first;
2101 while (++__i != __last)
2105 *__first = _VSTD::move(*__i);
2115 template <class _InputIterator, class _OutputIterator, class _Tp>
2116 inline _LIBCPP_INLINE_VISIBILITY
2118 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2120 for (; __first != __last; ++__first)
2122 if (!(*__first == __value_))
2124 *__result = *__first;
2133 template <class _InputIterator, class _OutputIterator, class _Predicate>
2134 inline _LIBCPP_INLINE_VISIBILITY
2136 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2138 for (; __first != __last; ++__first)
2140 if (!__pred(*__first))
2142 *__result = *__first;
2151 template <class _ForwardIterator, class _BinaryPredicate>
2153 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2155 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2156 (__first, __last, __pred);
2157 if (__first != __last)
2161 _ForwardIterator __i = __first;
2162 for (++__i; ++__i != __last;)
2163 if (!__pred(*__first, *__i))
2164 *++__first = _VSTD::move(*__i);
2170 template <class _ForwardIterator>
2171 inline _LIBCPP_INLINE_VISIBILITY
2173 unique(_ForwardIterator __first, _ForwardIterator __last)
2175 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2176 return _VSTD::unique(__first, __last, __equal_to<__v>());
2181 template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2183 __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2184 input_iterator_tag, output_iterator_tag)
2186 if (__first != __last)
2188 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2191 while (++__first != __last)
2193 if (!__pred(__t, *__first))
2204 template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2206 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2207 forward_iterator_tag, output_iterator_tag)
2209 if (__first != __last)
2211 _ForwardIterator __i = __first;
2214 while (++__first != __last)
2216 if (!__pred(*__i, *__first))
2218 *__result = *__first;
2227 template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2229 __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2230 input_iterator_tag, forward_iterator_tag)
2232 if (__first != __last)
2234 *__result = *__first;
2235 while (++__first != __last)
2236 if (!__pred(*__result, *__first))
2237 *++__result = *__first;
2243 template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2244 inline _LIBCPP_INLINE_VISIBILITY
2246 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2248 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2249 (__first, __last, __result, __pred,
2250 typename iterator_traits<_InputIterator>::iterator_category(),
2251 typename iterator_traits<_OutputIterator>::iterator_category());
2254 template <class _InputIterator, class _OutputIterator>
2255 inline _LIBCPP_INLINE_VISIBILITY
2257 unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2259 typedef typename iterator_traits<_InputIterator>::value_type __v;
2260 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2265 template <class _BidirectionalIterator>
2266 inline _LIBCPP_INLINE_VISIBILITY
2268 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2270 while (__first != __last)
2272 if (__first == --__last)
2274 swap(*__first, *__last);
2279 template <class _RandomAccessIterator>
2280 inline _LIBCPP_INLINE_VISIBILITY
2282 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2284 if (__first != __last)
2285 for (; __first < --__last; ++__first)
2286 swap(*__first, *__last);
2289 template <class _BidirectionalIterator>
2290 inline _LIBCPP_INLINE_VISIBILITY
2292 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2294 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2299 template <class _BidirectionalIterator, class _OutputIterator>
2300 inline _LIBCPP_INLINE_VISIBILITY
2302 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2304 for (; __first != __last; ++__result)
2305 *__result = *--__last;
2311 template <class _ForwardIterator>
2313 __rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2315 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2316 value_type __tmp = _VSTD::move(*__first);
2317 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2318 *__lm1 = _VSTD::move(__tmp);
2322 template <class _BidirectionalIterator>
2323 _BidirectionalIterator
2324 __rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2326 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2327 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2328 value_type __tmp = _VSTD::move(*__lm1);
2329 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2330 *__first = _VSTD::move(__tmp);
2334 template <class _ForwardIterator>
2336 __rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2338 _ForwardIterator __i = __middle;
2341 swap(*__first, *__i);
2343 if (++__i == __last)
2345 if (__first == __middle)
2348 _ForwardIterator __r = __first;
2349 if (__first != __middle)
2354 swap(*__first, *__i);
2356 if (++__i == __last)
2358 if (__first == __middle)
2362 else if (__first == __middle)
2369 template<typename _Integral>
2370 inline _LIBCPP_INLINE_VISIBILITY
2372 __gcd(_Integral __x, _Integral __y)
2376 _Integral __t = __x % __y;
2383 template<typename _RandomAccessIterator>
2384 _RandomAccessIterator
2385 __rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2387 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2388 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2390 const difference_type __m1 = __middle - __first;
2391 const difference_type __m2 = __last - __middle;
2394 _VSTD::swap_ranges(__first, __middle, __middle);
2397 const difference_type __g = _VSTD::__gcd(__m1, __m2);
2398 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2400 value_type __t(_VSTD::move(*--__p));
2401 _RandomAccessIterator __p1 = __p;
2402 _RandomAccessIterator __p2 = __p1 + __m1;
2405 *__p1 = _VSTD::move(*__p2);
2407 const difference_type __d = __last - __p2;
2411 __p2 = __first + (__m1 - __d);
2412 } while (__p2 != __p);
2413 *__p1 = _VSTD::move(__t);
2415 return __first + __m2;
2418 template <class _ForwardIterator>
2419 inline _LIBCPP_INLINE_VISIBILITY
2421 __rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2422 _VSTD::forward_iterator_tag)
2424 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2425 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2427 if (_VSTD::next(__first) == __middle)
2428 return _VSTD::__rotate_left(__first, __last);
2430 return _VSTD::__rotate_forward(__first, __middle, __last);
2433 template <class _BidirectionalIterator>
2434 inline _LIBCPP_INLINE_VISIBILITY
2435 _BidirectionalIterator
2436 __rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2437 _VSTD::bidirectional_iterator_tag)
2439 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2440 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2442 if (_VSTD::next(__first) == __middle)
2443 return _VSTD::__rotate_left(__first, __last);
2444 if (_VSTD::next(__middle) == __last)
2445 return _VSTD::__rotate_right(__first, __last);
2447 return _VSTD::__rotate_forward(__first, __middle, __last);
2450 template <class _RandomAccessIterator>
2451 inline _LIBCPP_INLINE_VISIBILITY
2452 _RandomAccessIterator
2453 __rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2454 _VSTD::random_access_iterator_tag)
2456 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2457 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2459 if (_VSTD::next(__first) == __middle)
2460 return _VSTD::__rotate_left(__first, __last);
2461 if (_VSTD::next(__middle) == __last)
2462 return _VSTD::__rotate_right(__first, __last);
2463 return _VSTD::__rotate_gcd(__first, __middle, __last);
2465 return _VSTD::__rotate_forward(__first, __middle, __last);
2468 template <class _ForwardIterator>
2469 inline _LIBCPP_INLINE_VISIBILITY
2471 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2473 if (__first == __middle)
2475 if (__middle == __last)
2477 return _VSTD::__rotate(__first, __middle, __last,
2478 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2483 template <class _ForwardIterator, class _OutputIterator>
2484 inline _LIBCPP_INLINE_VISIBILITY
2486 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2488 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2493 template <class _ForwardIterator, class _Compare>
2494 inline _LIBCPP_INLINE_VISIBILITY
2496 min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2498 if (__first != __last)
2500 _ForwardIterator __i = __first;
2501 while (++__i != __last)
2502 if (__comp(*__i, *__first))
2508 template <class _ForwardIterator>
2509 inline _LIBCPP_INLINE_VISIBILITY
2511 min_element(_ForwardIterator __first, _ForwardIterator __last)
2513 return _VSTD::min_element(__first, __last,
2514 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2519 template <class _Tp, class _Compare>
2520 inline _LIBCPP_INLINE_VISIBILITY
2522 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2524 return __comp(__b, __a) ? __b : __a;
2527 template <class _Tp>
2528 inline _LIBCPP_INLINE_VISIBILITY
2530 min(const _Tp& __a, const _Tp& __b)
2532 return _VSTD::min(__a, __b, __less<_Tp>());
2535 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2537 template<class _Tp, class _Compare>
2538 inline _LIBCPP_INLINE_VISIBILITY
2540 min(initializer_list<_Tp> __t, _Compare __comp)
2542 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2546 inline _LIBCPP_INLINE_VISIBILITY
2548 min(initializer_list<_Tp> __t)
2550 return *_VSTD::min_element(__t.begin(), __t.end());
2553 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2557 template <class _ForwardIterator, class _Compare>
2558 inline _LIBCPP_INLINE_VISIBILITY
2560 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2562 if (__first != __last)
2564 _ForwardIterator __i = __first;
2565 while (++__i != __last)
2566 if (__comp(*__first, *__i))
2572 template <class _ForwardIterator>
2573 inline _LIBCPP_INLINE_VISIBILITY
2575 max_element(_ForwardIterator __first, _ForwardIterator __last)
2577 return _VSTD::max_element(__first, __last,
2578 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2583 template <class _Tp, class _Compare>
2584 inline _LIBCPP_INLINE_VISIBILITY
2586 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2588 return __comp(__a, __b) ? __b : __a;
2591 template <class _Tp>
2592 inline _LIBCPP_INLINE_VISIBILITY
2594 max(const _Tp& __a, const _Tp& __b)
2596 return _VSTD::max(__a, __b, __less<_Tp>());
2599 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2601 template<class _Tp, class _Compare>
2602 inline _LIBCPP_INLINE_VISIBILITY
2604 max(initializer_list<_Tp> __t, _Compare __comp)
2606 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2610 inline _LIBCPP_INLINE_VISIBILITY
2612 max(initializer_list<_Tp> __t)
2614 return *_VSTD::max_element(__t.begin(), __t.end());
2617 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2621 template <class _ForwardIterator, class _Compare>
2622 std::pair<_ForwardIterator, _ForwardIterator>
2623 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2625 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2626 if (__first != __last)
2628 if (++__first != __last)
2630 if (__comp(*__first, *__result.first))
2631 __result.first = __first;
2633 __result.second = __first;
2634 while (++__first != __last)
2636 _ForwardIterator __i = __first;
2637 if (++__first == __last)
2639 if (__comp(*__i, *__result.first))
2640 __result.first = __i;
2641 else if (!__comp(*__i, *__result.second))
2642 __result.second = __i;
2647 if (__comp(*__first, *__i))
2649 if (__comp(*__first, *__result.first))
2650 __result.first = __first;
2651 if (!__comp(*__i, *__result.second))
2652 __result.second = __i;
2656 if (__comp(*__i, *__result.first))
2657 __result.first = __i;
2658 if (!__comp(*__first, *__result.second))
2659 __result.second = __first;
2668 template <class _ForwardIterator>
2669 inline _LIBCPP_INLINE_VISIBILITY
2670 std::pair<_ForwardIterator, _ForwardIterator>
2671 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2673 return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
2678 template<class _Tp, class _Compare>
2679 inline _LIBCPP_INLINE_VISIBILITY
2680 pair<const _Tp&, const _Tp&>
2681 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2683 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2684 pair<const _Tp&, const _Tp&>(__a, __b);
2688 inline _LIBCPP_INLINE_VISIBILITY
2689 pair<const _Tp&, const _Tp&>
2690 minmax(const _Tp& __a, const _Tp& __b)
2692 return _VSTD::minmax(__a, __b, __less<_Tp>());
2695 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2698 inline _LIBCPP_INLINE_VISIBILITY
2700 minmax(initializer_list<_Tp> __t)
2702 pair<const _Tp*, const _Tp*> __p =
2703 _VSTD::minmax_element(__t.begin(), __t.end());
2704 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2707 template<class _Tp, class _Compare>
2708 inline _LIBCPP_INLINE_VISIBILITY
2710 minmax(initializer_list<_Tp> __t, _Compare __comp)
2712 pair<const _Tp*, const _Tp*> __p =
2713 _VSTD::minmax_element(__t.begin(), __t.end(), __comp);
2714 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2717 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2721 // __independent_bits_engine
2723 template <unsigned long long _Xp, size_t _Rp>
2726 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2727 : __log2_imp<_Xp, _Rp - 1>::value;
2730 template <unsigned long long _Xp>
2731 struct __log2_imp<_Xp, 0>
2733 static const size_t value = 0;
2736 template <size_t _Rp>
2737 struct __log2_imp<0, _Rp>
2739 static const size_t value = _Rp + 1;
2742 template <class _UI, _UI _Xp>
2745 static const size_t value = __log2_imp<_Xp,
2746 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2749 template<class _Engine, class _UIntType>
2750 class __independent_bits_engine
2754 typedef _UIntType result_type;
2757 typedef typename _Engine::result_type _Engine_result_type;
2758 typedef typename conditional
2760 sizeof(_Engine_result_type) <= sizeof(result_type),
2763 >::type _Working_result_type;
2770 _Working_result_type __y0_;
2771 _Working_result_type __y1_;
2772 _Engine_result_type __mask0_;
2773 _Engine_result_type __mask1_;
2775 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
2776 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2777 + _Working_result_type(1);
2779 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2780 + _Working_result_type(1);
2782 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2783 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2784 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2787 // constructors and seeding functions
2788 __independent_bits_engine(_Engine& __e, size_t __w);
2790 // generating functions
2791 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2794 result_type __eval(false_type);
2795 result_type __eval(true_type);
2798 template<class _Engine, class _UIntType>
2799 __independent_bits_engine<_Engine, _UIntType>
2800 ::__independent_bits_engine(_Engine& __e, size_t __w)
2804 __n_ = __w_ / __m + (__w_ % __m != 0);
2805 __w0_ = __w_ / __n_;
2808 else if (__w0_ < _WDt)
2809 __y0_ = (_Rp >> __w0_) << __w0_;
2812 if (_Rp - __y0_ > __y0_ / __n_)
2815 __w0_ = __w_ / __n_;
2817 __y0_ = (_Rp >> __w0_) << __w0_;
2821 __n0_ = __n_ - __w_ % __n_;
2822 if (__w0_ < _WDt - 1)
2823 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2826 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2827 _Engine_result_type(0);
2828 __mask1_ = __w0_ < _EDt - 1 ?
2829 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2830 _Engine_result_type(~0);
2833 template<class _Engine, class _UIntType>
2836 __independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2838 return static_cast<result_type>(__e_() & __mask0_);
2841 template<class _Engine, class _UIntType>
2843 __independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2845 result_type _Sp = 0;
2846 for (size_t __k = 0; __k < __n0_; ++__k)
2848 _Engine_result_type __u;
2851 __u = __e_() - _Engine::min();
2852 } while (__u >= __y0_);
2857 _Sp += __u & __mask0_;
2859 for (size_t __k = __n0_; __k < __n_; ++__k)
2861 _Engine_result_type __u;
2864 __u = __e_() - _Engine::min();
2865 } while (__u >= __y1_);
2866 if (__w0_ < _WDt - 1)
2870 _Sp += __u & __mask1_;
2875 // uniform_int_distribution
2877 template<class _IntType = int>
2878 class uniform_int_distribution
2882 typedef _IntType result_type;
2889 typedef uniform_int_distribution distribution_type;
2891 explicit param_type(result_type __a = 0,
2892 result_type __b = numeric_limits<result_type>::max())
2893 : __a_(__a), __b_(__b) {}
2895 result_type a() const {return __a_;}
2896 result_type b() const {return __b_;}
2898 friend bool operator==(const param_type& __x, const param_type& __y)
2899 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2900 friend bool operator!=(const param_type& __x, const param_type& __y)
2901 {return !(__x == __y);}
2908 // constructors and reset functions
2909 explicit uniform_int_distribution(result_type __a = 0,
2910 result_type __b = numeric_limits<result_type>::max())
2911 : __p_(param_type(__a, __b)) {}
2912 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2915 // generating functions
2916 template<class _URNG> result_type operator()(_URNG& __g)
2917 {return (*this)(__g, __p_);}
2918 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2920 // property functions
2921 result_type a() const {return __p_.a();}
2922 result_type b() const {return __p_.b();}
2924 param_type param() const {return __p_;}
2925 void param(const param_type& __p) {__p_ = __p;}
2927 result_type min() const {return a();}
2928 result_type max() const {return b();}
2930 friend bool operator==(const uniform_int_distribution& __x,
2931 const uniform_int_distribution& __y)
2932 {return __x.__p_ == __y.__p_;}
2933 friend bool operator!=(const uniform_int_distribution& __x,
2934 const uniform_int_distribution& __y)
2935 {return !(__x == __y);}
2938 template<class _IntType>
2939 template<class _URNG>
2940 typename uniform_int_distribution<_IntType>::result_type
2941 uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2943 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2944 uint32_t, uint64_t>::type _UIntType;
2945 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
2948 const size_t _Dt = numeric_limits<_UIntType>::digits;
2949 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2951 return static_cast<result_type>(_Eng(__g, _Dt)());
2952 size_t __w = _Dt - __clz(_Rp) - 1;
2953 if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
2960 } while (__u >= _Rp);
2961 return static_cast<result_type>(__u + __p.a());
2966 __rs_default __rs_get();
2970 static unsigned __c_;
2974 typedef uint_fast32_t result_type;
2976 static const result_type _Min = 0;
2977 static const result_type _Max = 0xFFFFFFFF;
2979 __rs_default(const __rs_default&);
2982 result_type operator()();
2984 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2985 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
2987 friend __rs_default __rs_get();
2990 __rs_default __rs_get();
2992 template <class _RandomAccessIterator>
2994 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2996 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2997 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2998 typedef typename _Dp::param_type _Pp;
2999 difference_type __d = __last - __first;
3003 __rs_default __g = __rs_get();
3004 for (--__last, --__d; __first < __last; ++__first, --__d)
3006 difference_type __i = __uid(__g, _Pp(0, __d));
3007 if (__i != difference_type(0))
3008 swap(*__first, *(__first + __i));
3013 template <class _RandomAccessIterator, class _RandomNumberGenerator>
3015 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3016 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3017 _RandomNumberGenerator&& __rand)
3019 _RandomNumberGenerator& __rand)
3022 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3023 difference_type __d = __last - __first;
3026 for (--__last; __first < __last; ++__first, --__d)
3028 difference_type __i = __rand(__d);
3029 swap(*__first, *(__first + __i));
3034 template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3035 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3036 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3037 _UniformRandomNumberGenerator&& __g)
3039 _UniformRandomNumberGenerator& __g)
3042 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3043 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3044 typedef typename _Dp::param_type _Pp;
3045 difference_type __d = __last - __first;
3049 for (--__last, --__d; __first < __last; ++__first, --__d)
3051 difference_type __i = __uid(__g, _Pp(0, __d));
3052 if (__i != difference_type(0))
3053 swap(*__first, *(__first + __i));
3058 template <class _InputIterator, class _Predicate>
3060 is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3062 for (; __first != __last; ++__first)
3063 if (!__pred(*__first))
3065 for (; __first != __last; ++__first)
3066 if (__pred(*__first))
3073 template <class _Predicate, class _ForwardIterator>
3075 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3079 if (__first == __last)
3081 if (!__pred(*__first))
3085 for (_ForwardIterator __p = __first; ++__p != __last;)
3089 swap(*__first, *__p);
3096 template <class _Predicate, class _BidirectionalIterator>
3097 _BidirectionalIterator
3098 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3099 bidirectional_iterator_tag)
3105 if (__first == __last)
3107 if (!__pred(*__first))
3113 if (__first == --__last)
3115 } while (!__pred(*__last));
3116 swap(*__first, *__last);
3121 template <class _ForwardIterator, class _Predicate>
3122 inline _LIBCPP_INLINE_VISIBILITY
3124 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3126 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3127 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3132 template <class _InputIterator, class _OutputIterator1,
3133 class _OutputIterator2, class _Predicate>
3134 pair<_OutputIterator1, _OutputIterator2>
3135 partition_copy(_InputIterator __first, _InputIterator __last,
3136 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3139 for (; __first != __last; ++__first)
3141 if (__pred(*__first))
3143 *__out_true = *__first;
3148 *__out_false = *__first;
3152 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3157 template<class _ForwardIterator, class _Predicate>
3159 partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3161 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3162 difference_type __len = _VSTD::distance(__first, __last);
3165 difference_type __l2 = __len / 2;
3166 _ForwardIterator __m = __first;
3167 _VSTD::advance(__m, __l2);
3181 template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3183 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3184 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3186 // *__first is known to be false
3192 _ForwardIterator __m = __first;
3195 swap(*__first, *__m);
3200 if (__len <= __p.second)
3201 { // The buffer is big enough to use
3202 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3203 __destruct_n __d(0);
3204 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3205 // Move the falses into the temporary buffer, and the trues to the front of the line
3206 // Update __first to always point to the end of the trues
3207 value_type* __t = __p.first;
3208 ::new(__t) value_type(_VSTD::move(*__first));
3209 __d.__incr((value_type*)0);
3211 _ForwardIterator __i = __first;
3212 while (++__i != __last)
3216 *__first = _VSTD::move(*__i);
3221 ::new(__t) value_type(_VSTD::move(*__i));
3222 __d.__incr((value_type*)0);
3226 // All trues now at start of range, all falses in buffer
3227 // Move falses back into range, but don't mess up __first which points to first false
3229 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3230 *__i = _VSTD::move(*__t2);
3231 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3234 // Else not enough buffer, do in place
3236 _ForwardIterator __m = __first;
3237 _Distance __len2 = __len / 2; // __len2 >= 2
3238 _VSTD::advance(__m, __len2);
3239 // recurse on [__first, __m), *__first know to be false
3240 // F?????????????????
3242 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3243 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3244 // TTTFFFFF??????????
3246 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3247 _ForwardIterator __m1 = __m;
3248 _ForwardIterator __second_false = __last;
3249 _Distance __len_half = __len - __len2;
3250 while (__pred(*__m1))
3252 if (++__m1 == __last)
3253 goto __second_half_done;
3256 // TTTFFFFFTTTF??????
3258 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3260 // TTTFFFFFTTTTTFFFFF
3262 return _VSTD::rotate(__first_false, __m, __second_false);
3263 // TTTTTTTTFFFFFFFFFF
3267 struct __return_temporary_buffer
3269 template <class _Tp>
3270 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3273 template <class _Predicate, class _ForwardIterator>
3275 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3276 forward_iterator_tag)
3278 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3279 // Either prove all true and return __first or point to first false
3282 if (__first == __last)
3284 if (!__pred(*__first))
3288 // We now have a reduced range [__first, __last)
3289 // *__first is known to be false
3290 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3291 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3292 difference_type __len = _VSTD::distance(__first, __last);
3293 pair<value_type*, ptrdiff_t> __p(0, 0);
3294 unique_ptr<value_type, __return_temporary_buffer> __h;
3295 if (__len >= __alloc_limit)
3297 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3298 __h.reset(__p.first);
3300 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3301 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3304 template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3305 _BidirectionalIterator
3306 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3307 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3309 // *__first is known to be false
3310 // *__last is known to be true
3314 swap(*__first, *__last);
3319 _BidirectionalIterator __m = __first;
3322 swap(*__first, *__m);
3323 swap(*__m, *__last);
3326 swap(*__m, *__last);
3327 swap(*__first, *__m);
3330 if (__len <= __p.second)
3331 { // The buffer is big enough to use
3332 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3333 __destruct_n __d(0);
3334 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3335 // Move the falses into the temporary buffer, and the trues to the front of the line
3336 // Update __first to always point to the end of the trues
3337 value_type* __t = __p.first;
3338 ::new(__t) value_type(_VSTD::move(*__first));
3339 __d.__incr((value_type*)0);
3341 _BidirectionalIterator __i = __first;
3342 while (++__i != __last)
3346 *__first = _VSTD::move(*__i);
3351 ::new(__t) value_type(_VSTD::move(*__i));
3352 __d.__incr((value_type*)0);
3356 // move *__last, known to be true
3357 *__first = _VSTD::move(*__i);
3359 // All trues now at start of range, all falses in buffer
3360 // Move falses back into range, but don't mess up __first which points to first false
3361 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3362 *__i = _VSTD::move(*__t2);
3363 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3366 // Else not enough buffer, do in place
3368 _BidirectionalIterator __m = __first;
3369 _Distance __len2 = __len / 2; // __len2 >= 2
3370 _VSTD::advance(__m, __len2);
3371 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3372 // F????????????????T
3374 _BidirectionalIterator __m1 = __m;
3375 _BidirectionalIterator __first_false = __first;
3376 _Distance __len_half = __len2;
3377 while (!__pred(*--__m1))
3379 if (__m1 == __first)
3380 goto __first_half_done;
3383 // F???TFFF?????????T
3385 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3386 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3388 // TTTFFFFF?????????T
3390 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3392 _BidirectionalIterator __second_false = __last;
3394 __len_half = __len - __len2;
3395 while (__pred(*__m1))
3397 if (++__m1 == __last)
3398 goto __second_half_done;
3401 // TTTFFFFFTTTF?????T
3403 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3405 // TTTFFFFFTTTTTFFFFF
3407 return _VSTD::rotate(__first_false, __m, __second_false);
3408 // TTTTTTTTFFFFFFFFFF
3412 template <class _Predicate, class _BidirectionalIterator>
3413 _BidirectionalIterator
3414 __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3415 bidirectional_iterator_tag)
3417 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3418 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3419 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3420 // Either prove all true and return __first or point to first false
3423 if (__first == __last)
3425 if (!__pred(*__first))
3429 // __first points to first false, everything prior to __first is already set.
3430 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3433 if (__first == --__last)
3435 } while (!__pred(*__last));
3436 // We now have a reduced range [__first, __last]
3437 // *__first is known to be false
3438 // *__last is known to be true
3440 difference_type __len = _VSTD::distance(__first, __last) + 1;
3441 pair<value_type*, ptrdiff_t> __p(0, 0);
3442 unique_ptr<value_type, __return_temporary_buffer> __h;
3443 if (__len >= __alloc_limit)
3445 __p = _VSTD::get_temporary_buffer<value_type>(__len);
3446 __h.reset(__p.first);
3448 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3449 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3452 template <class _ForwardIterator, class _Predicate>
3453 inline _LIBCPP_INLINE_VISIBILITY
3455 stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3457 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3458 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3463 template <class _ForwardIterator, class _Compare>
3465 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3467 if (__first != __last)
3469 _ForwardIterator __i = __first;
3470 while (++__i != __last)
3472 if (__comp(*__i, *__first))
3480 template<class _ForwardIterator>
3481 inline _LIBCPP_INLINE_VISIBILITY
3483 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3485 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3490 template <class _ForwardIterator, class _Compare>
3491 inline _LIBCPP_INLINE_VISIBILITY
3493 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3495 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3498 template<class _ForwardIterator>
3499 inline _LIBCPP_INLINE_VISIBILITY
3501 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3503 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3508 // stable, 2-3 compares, 0-2 swaps
3510 template <class _Compare, class _ForwardIterator>
3512 __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3515 if (!__c(*__y, *__x)) // if x <= y
3517 if (!__c(*__z, *__y)) // if y <= z
3518 return __r; // x <= y && y <= z
3520 swap(*__y, *__z); // x <= z && y < z
3522 if (__c(*__y, *__x)) // if x > y
3524 swap(*__x, *__y); // x < y && y <= z
3527 return __r; // x <= y && y < z
3529 if (__c(*__z, *__y)) // x > y, if y > z
3531 swap(*__x, *__z); // x < y && y < z
3535 swap(*__x, *__y); // x > y && y <= z
3536 __r = 1; // x < y && x <= z
3537 if (__c(*__z, *__y)) // if y > z
3539 swap(*__y, *__z); // x <= y && y < z
3543 } // x <= y && y <= z
3545 // stable, 3-6 compares, 0-5 swaps
3547 template <class _Compare, class _ForwardIterator>
3549 __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3550 _ForwardIterator __x4, _Compare __c)
3552 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3553 if (__c(*__x4, *__x3))
3557 if (__c(*__x3, *__x2))
3561 if (__c(*__x2, *__x1))
3571 // stable, 4-10 compares, 0-9 swaps
3573 template <class _Compare, class _ForwardIterator>
3575 __sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3576 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3578 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3579 if (__c(*__x5, *__x4))
3583 if (__c(*__x4, *__x3))
3587 if (__c(*__x3, *__x2))
3591 if (__c(*__x2, *__x1))
3603 template <class _Compare, class _BirdirectionalIterator>
3605 __selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3607 _BirdirectionalIterator __lm1 = __last;
3608 for (--__lm1; __first != __lm1; ++__first)
3610 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3611 typename add_lvalue_reference<_Compare>::type>
3612 (__first, __last, __comp);
3614 swap(*__first, *__i);
3618 template <class _Compare, class _BirdirectionalIterator>
3620 __insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3622 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3623 if (__first != __last)
3625 _BirdirectionalIterator __i = __first;
3626 for (++__i; __i != __last; ++__i)
3628 _BirdirectionalIterator __j = __i;
3629 value_type __t(_VSTD::move(*__j));
3630 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3631 *__j = _VSTD::move(*__k);
3632 *__j = _VSTD::move(__t);
3637 template <class _Compare, class _RandomAccessIterator>
3639 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3641 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3642 _RandomAccessIterator __j = __first+2;
3643 __sort3<_Compare>(__first, __first+1, __j, __comp);
3644 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3646 if (__comp(*__i, *__j))
3648 value_type __t(_VSTD::move(*__i));
3649 _RandomAccessIterator __k = __j;
3653 *__j = _VSTD::move(*__k);
3655 } while (__j != __first && __comp(__t, *--__k));
3656 *__j = _VSTD::move(__t);
3662 template <class _Compare, class _RandomAccessIterator>
3664 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3666 switch (__last - __first)
3672 if (__comp(*--__last, *__first))
3673 swap(*__first, *__last);
3676 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3679 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3682 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3685 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3686 _RandomAccessIterator __j = __first+2;
3687 __sort3<_Compare>(__first, __first+1, __j, __comp);
3688 const unsigned __limit = 8;
3689 unsigned __count = 0;
3690 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3692 if (__comp(*__i, *__j))
3694 value_type __t(_VSTD::move(*__i));
3695 _RandomAccessIterator __k = __j;
3699 *__j = _VSTD::move(*__k);
3701 } while (__j != __first && __comp(__t, *--__k));
3702 *__j = _VSTD::move(__t);
3703 if (++__count == __limit)
3704 return ++__i == __last;
3711 template <class _Compare, class _BirdirectionalIterator>
3713 __insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3714 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3716 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3717 if (__first1 != __last1)
3719 __destruct_n __d(0);
3720 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3721 value_type* __last2 = __first2;
3722 ::new(__last2) value_type(_VSTD::move(*__first1));
3723 __d.__incr((value_type*)0);
3724 for (++__last2; ++__first1 != __last1; ++__last2)
3726 value_type* __j2 = __last2;
3727 value_type* __i2 = __j2;
3728 if (__comp(*__first1, *--__i2))
3730 ::new(__j2) value_type(_VSTD::move(*__i2));
3731 __d.__incr((value_type*)0);
3732 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3733 *__j2 = _VSTD::move(*__i2);
3734 *__j2 = _VSTD::move(*__first1);
3738 ::new(__j2) value_type(_VSTD::move(*__first1));
3739 __d.__incr((value_type*)0);
3746 template <class _Compare, class _RandomAccessIterator>
3748 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3750 // _Compare is known to be a reference type
3751 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3752 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3753 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3754 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3758 difference_type __len = __last - __first;
3765 if (__comp(*--__last, *__first))
3766 swap(*__first, *__last);
3769 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3772 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3775 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3778 if (__len <= __limit)
3780 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3784 _RandomAccessIterator __m = __first;
3785 _RandomAccessIterator __lm1 = __last;
3789 difference_type __delta;
3795 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3801 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3805 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3806 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3807 _RandomAccessIterator __i = __first;
3808 _RandomAccessIterator __j = __lm1;
3809 // j points beyond range to be tested, *__m is known to be <= *__lm1
3810 // The search going up is known to be guarded but the search coming down isn't.
3811 // Prime the downward search with a guard.
3812 if (!__comp(*__i, *__m)) // if *__first == *__m
3814 // *__first == *__m, *__first doesn't go in first part
3815 // manually guard downward moving __j against __i
3820 // *__first == *__m, *__m <= all other elements
3821 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3822 ++__i; // __first + 1
3824 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3829 return; // [__first, __last) all equivalent elements
3830 if (__comp(*__first, *__i))
3840 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3845 while (!__comp(*__first, *__i))
3847 while (__comp(*__first, *--__j))
3855 // [__first, __i) == *__first and *__first < [__i, __last)
3856 // The first part is sorted, sort the secod part
3857 // _VSTD::__sort<_Compare>(__i, __last, __comp);
3861 if (__comp(*__j, *__m))
3865 break; // found guard for downward moving __j, now use unguarded partition
3869 // It is known that *__i < *__m
3871 // j points beyond range to be tested, *__m is known to be <= *__lm1
3872 // if not yet partitioned...
3875 // known that *(__i - 1) < *__m
3876 // known that __i <= __m
3879 // __m still guards upward moving __i
3880 while (__comp(*__i, *__m))
3882 // It is now known that a guard exists for downward moving __j
3883 while (!__comp(*--__j, *__m))
3889 // It is known that __m != __j
3890 // If __m just moved, follow it
3896 // [__first, __i) < *__m and *__m <= [__i, __last)
3897 if (__i != __m && __comp(*__m, *__i))
3902 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3903 // If we were given a perfect partition, see if insertion sort is quick...
3906 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3907 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
3923 // sort smaller range with recursive call and larger with tail recursion elimination
3924 if (__i - __first < __last - __i)
3926 _VSTD::__sort<_Compare>(__first, __i, __comp);
3927 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3932 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3933 // _VSTD::__sort<_Compare>(__first, __i, __comp);
3939 // This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3940 template <class _RandomAccessIterator, class _Compare>
3941 inline _LIBCPP_INLINE_VISIBILITY
3943 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3945 #ifdef _LIBCPP_DEBUG2
3946 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3947 __debug_less<_Compare> __c(__comp);
3948 __sort<_Comp_ref>(__first, __last, __c);
3949 #else // _LIBCPP_DEBUG2
3950 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3951 __sort<_Comp_ref>(__first, __last, __comp);
3952 #endif // _LIBCPP_DEBUG2
3955 template <class _RandomAccessIterator>
3956 inline _LIBCPP_INLINE_VISIBILITY
3958 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3960 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
3963 template <class _Tp>
3964 inline _LIBCPP_INLINE_VISIBILITY
3966 sort(_Tp** __first, _Tp** __last)
3968 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
3971 template <class _Tp>
3972 inline _LIBCPP_INLINE_VISIBILITY
3974 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
3976 _VSTD::sort(__first.base(), __last.base());
3979 template <class _Tp, class _Compare>
3980 inline _LIBCPP_INLINE_VISIBILITY
3982 sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
3984 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3985 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
3989 #pragma warning( push )
3990 #pragma warning( disable: 4231)
3992 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
3993 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
3994 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
3995 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
3996 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
3997 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
3998 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
3999 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4000 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4001 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4002 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4003 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4004 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4005 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4006 _LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4008 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4009 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4010 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4011 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4012 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4013 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4014 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4015 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4016 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4017 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4018 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4019 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4020 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4021 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4022 _LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4024 _LIBCPP_EXTERN_TEMPLATE(unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
4026 #pragma warning( pop )
4031 template <class _Compare, class _ForwardIterator, class _Tp>
4033 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4035 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4036 difference_type __len = _VSTD::distance(__first, __last);
4039 difference_type __l2 = __len / 2;
4040 _ForwardIterator __m = __first;
4041 _VSTD::advance(__m, __l2);
4042 if (__comp(*__m, __value_))
4053 template <class _ForwardIterator, class _Tp, class _Compare>
4054 inline _LIBCPP_INLINE_VISIBILITY
4056 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4058 #ifdef _LIBCPP_DEBUG2
4059 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4060 __debug_less<_Compare> __c(__comp);
4061 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4062 #else // _LIBCPP_DEBUG2
4063 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4064 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4065 #endif // _LIBCPP_DEBUG2
4068 template <class _ForwardIterator, class _Tp>
4069 inline _LIBCPP_INLINE_VISIBILITY
4071 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4073 return _VSTD::lower_bound(__first, __last, __value_,
4074 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4079 template <class _Compare, class _ForwardIterator, class _Tp>
4081 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4083 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4084 difference_type __len = _VSTD::distance(__first, __last);
4087 difference_type __l2 = __len / 2;
4088 _ForwardIterator __m = __first;
4089 _VSTD::advance(__m, __l2);
4090 if (__comp(__value_, *__m))
4101 template <class _ForwardIterator, class _Tp, class _Compare>
4102 inline _LIBCPP_INLINE_VISIBILITY
4104 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4106 #ifdef _LIBCPP_DEBUG2
4107 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4108 __debug_less<_Compare> __c(__comp);
4109 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4110 #else // _LIBCPP_DEBUG2
4111 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4112 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4113 #endif // _LIBCPP_DEBUG2
4116 template <class _ForwardIterator, class _Tp>
4117 inline _LIBCPP_INLINE_VISIBILITY
4119 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4121 return _VSTD::upper_bound(__first, __last, __value_,
4122 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4127 template <class _Compare, class _ForwardIterator, class _Tp>
4128 pair<_ForwardIterator, _ForwardIterator>
4129 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4131 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4132 difference_type __len = _VSTD::distance(__first, __last);
4135 difference_type __l2 = __len / 2;
4136 _ForwardIterator __m = __first;
4137 _VSTD::advance(__m, __l2);
4138 if (__comp(*__m, __value_))
4143 else if (__comp(__value_, *__m))
4150 _ForwardIterator __mp1 = __m;
4151 return pair<_ForwardIterator, _ForwardIterator>
4153 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4154 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4158 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4161 template <class _ForwardIterator, class _Tp, class _Compare>
4162 inline _LIBCPP_INLINE_VISIBILITY
4163 pair<_ForwardIterator, _ForwardIterator>
4164 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4166 #ifdef _LIBCPP_DEBUG2
4167 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4168 __debug_less<_Compare> __c(__comp);
4169 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4170 #else // _LIBCPP_DEBUG2
4171 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4172 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4173 #endif // _LIBCPP_DEBUG2
4176 template <class _ForwardIterator, class _Tp>
4177 inline _LIBCPP_INLINE_VISIBILITY
4178 pair<_ForwardIterator, _ForwardIterator>
4179 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4181 return _VSTD::equal_range(__first, __last, __value_,
4182 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4187 template <class _Compare, class _ForwardIterator, class _Tp>
4188 inline _LIBCPP_INLINE_VISIBILITY
4190 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4192 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4193 return __first != __last && !__comp(__value_, *__first);
4196 template <class _ForwardIterator, class _Tp, class _Compare>
4197 inline _LIBCPP_INLINE_VISIBILITY
4199 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4201 #ifdef _LIBCPP_DEBUG2
4202 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4203 __debug_less<_Compare> __c(__comp);
4204 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4205 #else // _LIBCPP_DEBUG2
4206 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4207 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4208 #endif // _LIBCPP_DEBUG2
4211 template <class _ForwardIterator, class _Tp>
4212 inline _LIBCPP_INLINE_VISIBILITY
4214 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4216 return _VSTD::binary_search(__first, __last, __value_,
4217 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4222 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4224 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4225 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4227 for (; __first1 != __last1; ++__result)
4229 if (__first2 == __last2)
4230 return _VSTD::copy(__first1, __last1, __result);
4231 if (__comp(*__first2, *__first1))
4233 *__result = *__first2;
4238 *__result = *__first1;
4242 return _VSTD::copy(__first2, __last2, __result);
4245 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4246 inline _LIBCPP_INLINE_VISIBILITY
4248 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4249 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4251 #ifdef _LIBCPP_DEBUG2
4252 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4253 __debug_less<_Compare> __c(__comp);
4254 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4255 #else // _LIBCPP_DEBUG2
4256 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4257 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4258 #endif // _LIBCPP_DEBUG2
4261 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4262 inline _LIBCPP_INLINE_VISIBILITY
4264 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4265 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4267 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4268 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4269 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4274 template <class _Compare, class _BidirectionalIterator>
4276 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4277 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4278 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4279 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4281 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4282 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4283 typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
4284 __destruct_n __d(0);
4285 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4286 if (__len1 <= __len2)
4288 value_type* __p = __buff;
4289 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
4290 ::new(__p) value_type(_VSTD::move(*__i));
4291 __merge<_Compare>(move_iterator<value_type*>(__buff),
4292 move_iterator<value_type*>(__p),
4293 move_iterator<_BidirectionalIterator>(__middle),
4294 move_iterator<_BidirectionalIterator>(__last),
4299 value_type* __p = __buff;
4300 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
4301 ::new(__p) value_type(_VSTD::move(*__i));
4302 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4303 typedef reverse_iterator<value_type*> _Rv;
4304 __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
4305 move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
4306 _RBi(__last), __negate<_Compare>(__comp));
4310 template <class _Compare, class _BidirectionalIterator>
4312 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4313 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4314 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4315 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4317 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4318 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4321 // if __middle == __last, we're done
4324 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4325 for (; true; ++__first, --__len1)
4329 if (__comp(*__middle, *__first))
4332 if (__len1 <= __buff_size || __len2 <= __buff_size)
4334 __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
4337 // __first < __middle < __last
4338 // *__first > *__middle
4339 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4341 // [__first, __m1) <= [__middle, __m2)
4342 // [__middle, __m2) < [__m1, __middle)
4343 // [__m1, __middle) <= [__m2, __last)
4344 // and __m1 or __m2 is in the middle of its range
4345 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4346 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4347 difference_type __len11; // distance(__first, __m1)
4348 difference_type __len21; // distance(__middle, __m2)
4349 // binary search smaller range
4350 if (__len1 < __len2)
4351 { // __len >= 1, __len2 >= 2
4352 __len21 = __len2 / 2;
4354 _VSTD::advance(__m2, __len21);
4355 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4356 __len11 = _VSTD::distance(__first, __m1);
4361 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4362 // It is known *__first > *__middle
4363 swap(*__first, *__middle);
4366 // __len1 >= 2, __len2 >= 1
4367 __len11 = __len1 / 2;
4369 _VSTD::advance(__m1, __len11);
4370 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4371 __len21 = _VSTD::distance(__middle, __m2);
4373 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4374 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4375 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4376 // swap middle two partitions
4377 __middle = _VSTD::rotate(__m1, __middle, __m2);
4378 // __len12 and __len21 now have swapped meanings
4379 // merge smaller range with recurisve call and larger with tail recursion elimination
4380 if (__len11 + __len21 < __len12 + __len22)
4382 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4383 // __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4391 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4392 // __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4401 template <class _Tp>
4402 struct __inplace_merge_switch
4404 static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
4407 template <class _BidirectionalIterator, class _Compare>
4408 inline _LIBCPP_INLINE_VISIBILITY
4410 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4413 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4414 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4415 difference_type __len1 = _VSTD::distance(__first, __middle);
4416 difference_type __len2 = _VSTD::distance(__middle, __last);
4417 difference_type __buf_size = _VSTD::min(__len1, __len2);
4418 pair<value_type*, ptrdiff_t> __buf(0, 0);
4419 unique_ptr<value_type, __return_temporary_buffer> __h;
4420 if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4422 __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4423 __h.reset(__buf.first);
4425 #ifdef _LIBCPP_DEBUG2
4426 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4427 __debug_less<_Compare> __c(__comp);
4428 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4429 __buf.first, __buf.second);
4430 #else // _LIBCPP_DEBUG2
4431 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4432 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4433 __buf.first, __buf.second);
4434 #endif // _LIBCPP_DEBUG2
4437 template <class _BidirectionalIterator>
4438 inline _LIBCPP_INLINE_VISIBILITY
4440 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4442 _VSTD::inplace_merge(__first, __middle, __last,
4443 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4448 template <class _Compare, class _InputIterator1, class _InputIterator2>
4450 __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4451 _InputIterator2 __first2, _InputIterator2 __last2,
4452 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4454 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4455 __destruct_n __d(0);
4456 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4457 for (; true; ++__result)
4459 if (__first1 == __last1)
4461 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4462 ::new (__result) value_type(_VSTD::move(*__first2));
4466 if (__first2 == __last2)
4468 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4469 ::new (__result) value_type(_VSTD::move(*__first1));
4473 if (__comp(*__first2, *__first1))
4475 ::new (__result) value_type(_VSTD::move(*__first2));
4476 __d.__incr((value_type*)0);
4481 ::new (__result) value_type(_VSTD::move(*__first1));
4482 __d.__incr((value_type*)0);
4488 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4490 __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4491 _InputIterator2 __first2, _InputIterator2 __last2,
4492 _OutputIterator __result, _Compare __comp)
4494 for (; __first1 != __last1; ++__result)
4496 if (__first2 == __last2)
4498 for (; __first1 != __last1; ++__first1, ++__result)
4499 *__result = _VSTD::move(*__first1);
4502 if (__comp(*__first2, *__first1))
4504 *__result = _VSTD::move(*__first2);
4509 *__result = _VSTD::move(*__first1);
4513 for (; __first2 != __last2; ++__first2, ++__result)
4514 *__result = _VSTD::move(*__first2);
4517 template <class _Compare, class _RandomAccessIterator>
4519 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4520 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4521 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4523 template <class _Compare, class _RandomAccessIterator>
4525 __stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4526 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4527 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4529 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4535 ::new(__first2) value_type(_VSTD::move(*__first1));
4538 __destruct_n __d(0);
4539 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4540 if (__comp(*--__last1, *__first1))
4542 ::new(__first2) value_type(_VSTD::move(*__last1));
4543 __d.__incr((value_type*)0);
4545 ::new(__first2) value_type(_VSTD::move(*__first1));
4549 ::new(__first2) value_type(_VSTD::move(*__first1));
4550 __d.__incr((value_type*)0);
4552 ::new(__first2) value_type(_VSTD::move(*__last1));
4559 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4562 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4563 _RandomAccessIterator __m = __first1 + __l2;
4564 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4565 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4566 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4569 template <class _Tp>
4570 struct __stable_sort_switch
4572 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4575 template <class _Compare, class _RandomAccessIterator>
4577 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4578 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4579 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4581 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4582 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4589 if (__comp(*--__last, *__first))
4590 swap(*__first, *__last);
4593 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4595 __insertion_sort<_Compare>(__first, __last, __comp);
4598 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4599 _RandomAccessIterator __m = __first + __l2;
4600 if (__len <= __buff_size)
4602 __destruct_n __d(0);
4603 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4604 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4605 __d.__set(__l2, (value_type*)0);
4606 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4607 __d.__set(__len, (value_type*)0);
4608 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4609 // __merge<_Compare>(move_iterator<value_type*>(__buff),
4610 // move_iterator<value_type*>(__buff + __l2),
4611 // move_iterator<_RandomAccessIterator>(__buff + __l2),
4612 // move_iterator<_RandomAccessIterator>(__buff + __len),
4613 // __first, __comp);
4616 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4617 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4618 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4621 template <class _RandomAccessIterator, class _Compare>
4622 inline _LIBCPP_INLINE_VISIBILITY
4624 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4626 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4627 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4628 difference_type __len = __last - __first;
4629 pair<value_type*, ptrdiff_t> __buf(0, 0);
4630 unique_ptr<value_type, __return_temporary_buffer> __h;
4631 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4633 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4634 __h.reset(__buf.first);
4636 #ifdef _LIBCPP_DEBUG2
4637 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4638 __debug_less<_Compare> __c(__comp);
4639 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4640 #else // _LIBCPP_DEBUG2
4641 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4642 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4643 #endif // _LIBCPP_DEBUG2
4646 template <class _RandomAccessIterator>
4647 inline _LIBCPP_INLINE_VISIBILITY
4649 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4651 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4656 template <class _RandomAccessIterator, class _Compare>
4657 _RandomAccessIterator
4658 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4660 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4661 difference_type __len = __last - __first;
4662 difference_type __p = 0;
4663 difference_type __c = 1;
4664 _RandomAccessIterator __pp = __first;
4667 _RandomAccessIterator __cp = __first + __c;
4668 if (__comp(*__pp, *__cp))
4674 if (__comp(*__pp, *__cp))
4683 template<class _RandomAccessIterator>
4684 inline _LIBCPP_INLINE_VISIBILITY
4685 _RandomAccessIterator
4686 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4688 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4693 template <class _RandomAccessIterator, class _Compare>
4694 inline _LIBCPP_INLINE_VISIBILITY
4696 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4698 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4701 template<class _RandomAccessIterator>
4702 inline _LIBCPP_INLINE_VISIBILITY
4704 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4706 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4711 template <class _Compare, class _RandomAccessIterator>
4713 __push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
4714 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4716 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4717 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4720 difference_type __p = 0;
4721 _RandomAccessIterator __pp = __first;
4722 difference_type __c = 2;
4723 _RandomAccessIterator __cp = __first + __c;
4724 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4729 if (__comp(*__pp, *__cp))
4731 value_type __t(_VSTD::move(*__pp));
4734 *__pp = _VSTD::move(*__cp);
4737 __c = (__p + 1) * 2;
4740 __cp = __first + __c;
4741 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4746 } while (__comp(__t, *__cp));
4747 *__pp = _VSTD::move(__t);
4752 template <class _Compare, class _RandomAccessIterator>
4754 __push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4755 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4757 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4758 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4761 __len = (__len - 2) / 2;
4762 _RandomAccessIterator __ptr = __first + __len;
4763 if (__comp(*__ptr, *--__last))
4765 value_type __t(_VSTD::move(*__last));
4768 *__last = _VSTD::move(*__ptr);
4772 __len = (__len - 1) / 2;
4773 __ptr = __first + __len;
4774 } while (__comp(*__ptr, __t));
4775 *__last = _VSTD::move(__t);
4780 template <class _RandomAccessIterator, class _Compare>
4781 inline _LIBCPP_INLINE_VISIBILITY
4783 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4785 #ifdef _LIBCPP_DEBUG2
4786 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4787 __debug_less<_Compare> __c(__comp);
4788 __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
4789 #else // _LIBCPP_DEBUG2
4790 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4791 __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
4792 #endif // _LIBCPP_DEBUG2
4795 template <class _RandomAccessIterator>
4796 inline _LIBCPP_INLINE_VISIBILITY
4798 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4800 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4805 template <class _Compare, class _RandomAccessIterator>
4806 inline _LIBCPP_INLINE_VISIBILITY
4808 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4809 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4813 swap(*__first, *--__last);
4814 __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
4818 template <class _RandomAccessIterator, class _Compare>
4819 inline _LIBCPP_INLINE_VISIBILITY
4821 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4823 #ifdef _LIBCPP_DEBUG2
4824 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4825 __debug_less<_Compare> __c(__comp);
4826 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4827 #else // _LIBCPP_DEBUG2
4828 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4829 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4830 #endif // _LIBCPP_DEBUG2
4833 template <class _RandomAccessIterator>
4834 inline _LIBCPP_INLINE_VISIBILITY
4836 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4838 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4843 template <class _Compare, class _RandomAccessIterator>
4845 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4847 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4848 difference_type __n = __last - __first;
4853 for (difference_type __i = 1; __i < __n;)
4854 __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
4858 template <class _RandomAccessIterator, class _Compare>
4859 inline _LIBCPP_INLINE_VISIBILITY
4861 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4863 #ifdef _LIBCPP_DEBUG2
4864 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4865 __debug_less<_Compare> __c(__comp);
4866 __make_heap<_Comp_ref>(__first, __last, __c);
4867 #else // _LIBCPP_DEBUG2
4868 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4869 __make_heap<_Comp_ref>(__first, __last, __comp);
4870 #endif // _LIBCPP_DEBUG2
4873 template <class _RandomAccessIterator>
4874 inline _LIBCPP_INLINE_VISIBILITY
4876 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4878 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4883 template <class _Compare, class _RandomAccessIterator>
4885 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4887 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4888 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4889 __pop_heap<_Compare>(__first, __last, __comp, __n);
4892 template <class _RandomAccessIterator, class _Compare>
4893 inline _LIBCPP_INLINE_VISIBILITY
4895 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4897 #ifdef _LIBCPP_DEBUG2
4898 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4899 __debug_less<_Compare> __c(__comp);
4900 __sort_heap<_Comp_ref>(__first, __last, __c);
4901 #else // _LIBCPP_DEBUG2
4902 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4903 __sort_heap<_Comp_ref>(__first, __last, __comp);
4904 #endif // _LIBCPP_DEBUG2
4907 template <class _RandomAccessIterator>
4908 inline _LIBCPP_INLINE_VISIBILITY
4910 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4912 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4917 template <class _Compare, class _RandomAccessIterator>
4919 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4922 __make_heap<_Compare>(__first, __middle, __comp);
4923 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4924 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4926 if (__comp(*__i, *__first))
4928 swap(*__i, *__first);
4929 __push_heap_front<_Compare>(__first, __middle, __comp, __len);
4932 __sort_heap<_Compare>(__first, __middle, __comp);
4935 template <class _RandomAccessIterator, class _Compare>
4936 inline _LIBCPP_INLINE_VISIBILITY
4938 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4941 #ifdef _LIBCPP_DEBUG2
4942 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4943 __debug_less<_Compare> __c(__comp);
4944 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
4945 #else // _LIBCPP_DEBUG2
4946 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4947 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
4948 #endif // _LIBCPP_DEBUG2
4951 template <class _RandomAccessIterator>
4952 inline _LIBCPP_INLINE_VISIBILITY
4954 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
4956 _VSTD::partial_sort(__first, __middle, __last,
4957 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4960 // partial_sort_copy
4962 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
4963 _RandomAccessIterator
4964 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
4965 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4967 _RandomAccessIterator __r = __result_first;
4968 if (__r != __result_last)
4970 typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
4971 for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
4973 __make_heap<_Compare>(__result_first, __r, __comp);
4974 for (; __first != __last; ++__first)
4975 if (__comp(*__first, *__result_first))
4977 *__result_first = *__first;
4978 __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
4980 __sort_heap<_Compare>(__result_first, __r, __comp);
4985 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
4986 inline _LIBCPP_INLINE_VISIBILITY
4987 _RandomAccessIterator
4988 partial_sort_copy(_InputIterator __first, _InputIterator __last,
4989 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4991 #ifdef _LIBCPP_DEBUG2
4992 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4993 __debug_less<_Compare> __c(__comp);
4994 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
4995 #else // _LIBCPP_DEBUG2
4996 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4997 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
4998 #endif // _LIBCPP_DEBUG2
5001 template <class _InputIterator, class _RandomAccessIterator>
5002 inline _LIBCPP_INLINE_VISIBILITY
5003 _RandomAccessIterator
5004 partial_sort_copy(_InputIterator __first, _InputIterator __last,
5005 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5007 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5008 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5013 template <class _Compare, class _RandomAccessIterator>
5015 __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5017 // _Compare is known to be a reference type
5018 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5019 const difference_type __limit = 7;
5023 if (__nth == __last)
5025 difference_type __len = __last - __first;
5032 if (__comp(*--__last, *__first))
5033 swap(*__first, *__last);
5037 _RandomAccessIterator __m = __first;
5038 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5042 if (__len <= __limit)
5044 __selection_sort<_Compare>(__first, __last, __comp);
5047 // __len > __limit >= 3
5048 _RandomAccessIterator __m = __first + __len/2;
5049 _RandomAccessIterator __lm1 = __last;
5050 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5052 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5053 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5054 _RandomAccessIterator __i = __first;
5055 _RandomAccessIterator __j = __lm1;
5056 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5057 // The search going up is known to be guarded but the search coming down isn't.
5058 // Prime the downward search with a guard.
5059 if (!__comp(*__i, *__m)) // if *__first == *__m
5061 // *__first == *__m, *__first doesn't go in first part
5062 // manually guard downward moving __j against __i
5067 // *__first == *__m, *__m <= all other elements
5068 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5069 ++__i; // __first + 1
5071 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5076 return; // [__first, __last) all equivalent elements
5077 if (__comp(*__first, *__i))
5087 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5092 while (!__comp(*__first, *__i))
5094 while (__comp(*__first, *--__j))
5102 // [__first, __i) == *__first and *__first < [__i, __last)
5103 // The first part is sorted,
5106 // __nth_element the secod part
5107 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5111 if (__comp(*__j, *__m))
5115 break; // found guard for downward moving __j, now use unguarded partition
5120 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5121 // if not yet partitioned...
5124 // known that *(__i - 1) < *__m
5127 // __m still guards upward moving __i
5128 while (__comp(*__i, *__m))
5130 // It is now known that a guard exists for downward moving __j
5131 while (!__comp(*--__j, *__m))
5137 // It is known that __m != __j
5138 // If __m just moved, follow it
5144 // [__first, __i) < *__m and *__m <= [__i, __last)
5145 if (__i != __m && __comp(*__m, *__i))
5150 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5155 // We were given a perfectly partitioned sequence. Coincidence?
5158 // Check for [__first, __i) already sorted
5159 __j = __m = __first;
5160 while (++__j != __i)
5162 if (__comp(*__j, *__m))
5163 // not yet sorted, so sort
5167 // [__first, __i) sorted
5172 // Check for [__i, __last) already sorted
5174 while (++__j != __last)
5176 if (__comp(*__j, *__m))
5177 // not yet sorted, so sort
5181 // [__i, __last) sorted
5186 // __nth_element on range containing __nth
5189 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5194 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5200 template <class _RandomAccessIterator, class _Compare>
5201 inline _LIBCPP_INLINE_VISIBILITY
5203 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5205 #ifdef _LIBCPP_DEBUG2
5206 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5207 __debug_less<_Compare> __c(__comp);
5208 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5209 #else // _LIBCPP_DEBUG2
5210 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5211 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5212 #endif // _LIBCPP_DEBUG2
5215 template <class _RandomAccessIterator>
5216 inline _LIBCPP_INLINE_VISIBILITY
5218 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5220 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5225 template <class _Compare, class _InputIterator1, class _InputIterator2>
5227 __includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5230 for (; __first2 != __last2; ++__first1)
5232 if (__first1 == __last1 || __comp(*__first2, *__first1))
5234 if (!__comp(*__first1, *__first2))
5240 template <class _InputIterator1, class _InputIterator2, class _Compare>
5241 inline _LIBCPP_INLINE_VISIBILITY
5243 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5246 #ifdef _LIBCPP_DEBUG2
5247 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5248 __debug_less<_Compare> __c(__comp);
5249 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5250 #else // _LIBCPP_DEBUG2
5251 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5252 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5253 #endif // _LIBCPP_DEBUG2
5256 template <class _InputIterator1, class _InputIterator2>
5257 inline _LIBCPP_INLINE_VISIBILITY
5259 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5261 return _VSTD::includes(__first1, __last1, __first2, __last2,
5262 __less<typename iterator_traits<_InputIterator1>::value_type,
5263 typename iterator_traits<_InputIterator2>::value_type>());
5268 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5270 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5271 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5273 for (; __first1 != __last1; ++__result)
5275 if (__first2 == __last2)
5276 return _VSTD::copy(__first1, __last1, __result);
5277 if (__comp(*__first2, *__first1))
5279 *__result = *__first2;
5284 *__result = *__first1;
5285 if (!__comp(*__first1, *__first2))
5290 return _VSTD::copy(__first2, __last2, __result);
5293 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5294 inline _LIBCPP_INLINE_VISIBILITY
5296 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5297 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5299 #ifdef _LIBCPP_DEBUG2
5300 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5301 __debug_less<_Compare> __c(__comp);
5302 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5303 #else // _LIBCPP_DEBUG2
5304 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5305 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5306 #endif // _LIBCPP_DEBUG2
5309 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5310 inline _LIBCPP_INLINE_VISIBILITY
5312 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5313 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5315 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5316 __less<typename iterator_traits<_InputIterator1>::value_type,
5317 typename iterator_traits<_InputIterator2>::value_type>());
5322 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5324 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5325 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5327 while (__first1 != __last1 && __first2 != __last2)
5329 if (__comp(*__first1, *__first2))
5333 if (!__comp(*__first2, *__first1))
5335 *__result = *__first1;
5345 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5346 inline _LIBCPP_INLINE_VISIBILITY
5348 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5349 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5351 #ifdef _LIBCPP_DEBUG2
5352 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5353 __debug_less<_Compare> __c(__comp);
5354 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5355 #else // _LIBCPP_DEBUG2
5356 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5357 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5358 #endif // _LIBCPP_DEBUG2
5361 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5362 inline _LIBCPP_INLINE_VISIBILITY
5364 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5365 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5367 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5368 __less<typename iterator_traits<_InputIterator1>::value_type,
5369 typename iterator_traits<_InputIterator2>::value_type>());
5374 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5376 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5377 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5379 while (__first1 != __last1)
5381 if (__first2 == __last2)
5382 return _VSTD::copy(__first1, __last1, __result);
5383 if (__comp(*__first1, *__first2))
5385 *__result = *__first1;
5391 if (!__comp(*__first2, *__first1))
5399 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5400 inline _LIBCPP_INLINE_VISIBILITY
5402 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5403 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5405 #ifdef _LIBCPP_DEBUG2
5406 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5407 __debug_less<_Compare> __c(__comp);
5408 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5409 #else // _LIBCPP_DEBUG2
5410 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5411 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5412 #endif // _LIBCPP_DEBUG2
5415 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5416 inline _LIBCPP_INLINE_VISIBILITY
5418 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5419 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5421 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5422 __less<typename iterator_traits<_InputIterator1>::value_type,
5423 typename iterator_traits<_InputIterator2>::value_type>());
5426 // set_symmetric_difference
5428 template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5430 __set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5431 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5433 while (__first1 != __last1)
5435 if (__first2 == __last2)
5436 return _VSTD::copy(__first1, __last1, __result);
5437 if (__comp(*__first1, *__first2))
5439 *__result = *__first1;
5445 if (__comp(*__first2, *__first1))
5447 *__result = *__first2;
5455 return _VSTD::copy(__first2, __last2, __result);
5458 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5459 inline _LIBCPP_INLINE_VISIBILITY
5461 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5462 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5464 #ifdef _LIBCPP_DEBUG2
5465 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5466 __debug_less<_Compare> __c(__comp);
5467 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5468 #else // _LIBCPP_DEBUG2
5469 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5470 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5471 #endif // _LIBCPP_DEBUG2
5474 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5475 inline _LIBCPP_INLINE_VISIBILITY
5477 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5478 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5480 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5481 __less<typename iterator_traits<_InputIterator1>::value_type,
5482 typename iterator_traits<_InputIterator2>::value_type>());
5485 // lexicographical_compare
5487 template <class _Compare, class _InputIterator1, class _InputIterator2>
5489 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5490 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5492 for (; __first2 != __last2; ++__first1, ++__first2)
5494 if (__first1 == __last1 || __comp(*__first1, *__first2))
5496 if (__comp(*__first2, *__first1))
5502 template <class _InputIterator1, class _InputIterator2, class _Compare>
5503 inline _LIBCPP_INLINE_VISIBILITY
5505 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5506 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5508 #ifdef _LIBCPP_DEBUG2
5509 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5510 __debug_less<_Compare> __c(__comp);
5511 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5512 #else // _LIBCPP_DEBUG2
5513 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5514 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5515 #endif // _LIBCPP_DEBUG2
5518 template <class _InputIterator1, class _InputIterator2>
5519 inline _LIBCPP_INLINE_VISIBILITY
5521 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5522 _InputIterator2 __first2, _InputIterator2 __last2)
5524 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5525 __less<typename iterator_traits<_InputIterator1>::value_type,
5526 typename iterator_traits<_InputIterator2>::value_type>());
5531 template <class _Compare, class _BidirectionalIterator>
5533 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5535 _BidirectionalIterator __i = __last;
5536 if (__first == __last || __first == --__i)
5540 _BidirectionalIterator __ip1 = __i;
5541 if (__comp(*--__i, *__ip1))
5543 _BidirectionalIterator __j = __last;
5544 while (!__comp(*__i, *--__j))
5547 _VSTD::reverse(__ip1, __last);
5552 _VSTD::reverse(__first, __last);
5558 template <class _BidirectionalIterator, class _Compare>
5559 inline _LIBCPP_INLINE_VISIBILITY
5561 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5563 #ifdef _LIBCPP_DEBUG2
5564 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5565 __debug_less<_Compare> __c(__comp);
5566 return __next_permutation<_Comp_ref>(__first, __last, __c);
5567 #else // _LIBCPP_DEBUG2
5568 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5569 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5570 #endif // _LIBCPP_DEBUG2
5573 template <class _BidirectionalIterator>
5574 inline _LIBCPP_INLINE_VISIBILITY
5576 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5578 return _VSTD::next_permutation(__first, __last,
5579 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5584 template <class _Compare, class _BidirectionalIterator>
5586 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5588 _BidirectionalIterator __i = __last;
5589 if (__first == __last || __first == --__i)
5593 _BidirectionalIterator __ip1 = __i;
5594 if (__comp(*__ip1, *--__i))
5596 _BidirectionalIterator __j = __last;
5597 while (!__comp(*--__j, *__i))
5600 _VSTD::reverse(__ip1, __last);
5605 _VSTD::reverse(__first, __last);
5611 template <class _BidirectionalIterator, class _Compare>
5612 inline _LIBCPP_INLINE_VISIBILITY
5614 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5616 #ifdef _LIBCPP_DEBUG2
5617 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5618 __debug_less<_Compare> __c(__comp);
5619 return __prev_permutation<_Comp_ref>(__first, __last, __c);
5620 #else // _LIBCPP_DEBUG2
5621 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5622 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5623 #endif // _LIBCPP_DEBUG2
5626 template <class _BidirectionalIterator>
5627 inline _LIBCPP_INLINE_VISIBILITY
5629 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5631 return _VSTD::prev_permutation(__first, __last,
5632 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5635 template <class _Tp>
5636 inline _LIBCPP_INLINE_VISIBILITY
5639 is_integral<_Tp>::value,
5642 __rotate_left(_Tp __t, _Tp __n = 1)
5644 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5646 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5649 template <class _Tp>
5650 inline _LIBCPP_INLINE_VISIBILITY
5653 is_integral<_Tp>::value,
5656 __rotate_right(_Tp __t, _Tp __n = 1)
5658 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5660 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5663 _LIBCPP_END_NAMESPACE_STD
5665 #endif // _LIBCPP_ALGORITHM