1// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15    algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
23    bool
24    all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
27    bool
28    any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
31    bool
32    none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
35    Function
36    for_each(InputIterator first, InputIterator last, Function f);
37
38template <class InputIterator, class T>
39    InputIterator
40    find(InputIterator first, InputIterator last, const T& value);
41
42template <class InputIterator, class Predicate>
43    InputIterator
44    find_if(InputIterator first, InputIterator last, Predicate pred);
45
46template<class InputIterator, class Predicate>
47    InputIterator
48    find_if_not(InputIterator first, InputIterator last, Predicate pred);
49
50template <class ForwardIterator1, class ForwardIterator2>
51    ForwardIterator1
52    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53             ForwardIterator2 first2, ForwardIterator2 last2);
54
55template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
56    ForwardIterator1
57    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58             ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
59
60template <class ForwardIterator1, class ForwardIterator2>
61    ForwardIterator1
62    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63                  ForwardIterator2 first2, ForwardIterator2 last2);
64
65template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
66    ForwardIterator1
67    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68                  ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
69
70template <class ForwardIterator>
71    ForwardIterator
72    adjacent_find(ForwardIterator first, ForwardIterator last);
73
74template <class ForwardIterator, class BinaryPredicate>
75    ForwardIterator
76    adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
77
78template <class InputIterator, class T>
79    typename iterator_traits<InputIterator>::difference_type
80    count(InputIterator first, InputIterator last, const T& value);
81
82template <class InputIterator, class Predicate>
83    typename iterator_traits<InputIterator>::difference_type
84    count_if(InputIterator first, InputIterator last, Predicate pred);
85
86template <class InputIterator1, class InputIterator2>
87    pair<InputIterator1, InputIterator2>
88    mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
89
90template <class InputIterator1, class InputIterator2>
91    pair<InputIterator1, InputIterator2>
92    mismatch(InputIterator1 first1, InputIterator1 last1,
93             InputIterator2 first2, InputIterator2 last2); // **C++14**
94
95template <class InputIterator1, class InputIterator2, class BinaryPredicate>
96    pair<InputIterator1, InputIterator2>
97    mismatch(InputIterator1 first1, InputIterator1 last1,
98             InputIterator2 first2, BinaryPredicate pred);
99
100template <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**
105
106template <class InputIterator1, class InputIterator2>
107    bool
108    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
109
110template <class InputIterator1, class InputIterator2>
111    bool
112    equal(InputIterator1 first1, InputIterator1 last1,
113          InputIterator2 first2, InputIterator2 last2); // **C++14**
114
115template <class InputIterator1, class InputIterator2, class BinaryPredicate>
116    bool
117    equal(InputIterator1 first1, InputIterator1 last1,
118          InputIterator2 first2, BinaryPredicate pred);
119
120template <class InputIterator1, class InputIterator2, class BinaryPredicate>
121    bool
122    equal(InputIterator1 first1, InputIterator1 last1,
123          InputIterator2 first2, InputIterator2 last2,
124          BinaryPredicate pred); // **C++14**
125
126template<class ForwardIterator1, class ForwardIterator2>
127    bool
128    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129                   ForwardIterator2 first2);
130
131template<class ForwardIterator1, class ForwardIterator2>
132    bool
133    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134                   ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
135
136template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
137    bool
138    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139                   ForwardIterator2 first2, BinaryPredicate pred);
140
141template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
142    bool
143    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144                   ForwardIterator2 first2, ForwardIterator2 last2,
145                   BinaryPredicate pred);  // **C++14**
146
147template <class ForwardIterator1, class ForwardIterator2>
148    ForwardIterator1
149    search(ForwardIterator1 first1, ForwardIterator1 last1,
150           ForwardIterator2 first2, ForwardIterator2 last2);
151
152template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
153    ForwardIterator1
154    search(ForwardIterator1 first1, ForwardIterator1 last1,
155           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
156
157template <class ForwardIterator, class Size, class T>
158    ForwardIterator
159    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
160
161template <class ForwardIterator, class Size, class T, class BinaryPredicate>
162    ForwardIterator
163    search_n(ForwardIterator first, ForwardIterator last,
164             Size count, const T& value, BinaryPredicate pred);
165
166template <class InputIterator, class OutputIterator>
167    OutputIterator
168    copy(InputIterator first, InputIterator last, OutputIterator result);
169
170template<class InputIterator, class OutputIterator, class Predicate>
171    OutputIterator
172    copy_if(InputIterator first, InputIterator last,
173            OutputIterator result, Predicate pred);
174
175template<class InputIterator, class Size, class OutputIterator>
176    OutputIterator
177    copy_n(InputIterator first, Size n, OutputIterator result);
178
179template <class BidirectionalIterator1, class BidirectionalIterator2>
180    BidirectionalIterator2
181    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182                  BidirectionalIterator2 result);
183
184template <class ForwardIterator1, class ForwardIterator2>
185    ForwardIterator2
186    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
187
188template <class ForwardIterator1, class ForwardIterator2>
189    void
190    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
191
192template <class InputIterator, class OutputIterator, class UnaryOperation>
193    OutputIterator
194    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
195
196template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
197    OutputIterator
198    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199              OutputIterator result, BinaryOperation binary_op);
200
201template <class ForwardIterator, class T>
202    void
203    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
204
205template <class ForwardIterator, class Predicate, class T>
206    void
207    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
208
209template <class InputIterator, class OutputIterator, class T>
210    OutputIterator
211    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212                 const T& old_value, const T& new_value);
213
214template <class InputIterator, class OutputIterator, class Predicate, class T>
215    OutputIterator
216    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
217
218template <class ForwardIterator, class T>
219    void
220    fill(ForwardIterator first, ForwardIterator last, const T& value);
221
222template <class OutputIterator, class Size, class T>
223    OutputIterator
224    fill_n(OutputIterator first, Size n, const T& value);
225
226template <class ForwardIterator, class Generator>
227    void
228    generate(ForwardIterator first, ForwardIterator last, Generator gen);
229
230template <class OutputIterator, class Size, class Generator>
231    OutputIterator
232    generate_n(OutputIterator first, Size n, Generator gen);
233
234template <class ForwardIterator, class T>
235    ForwardIterator
236    remove(ForwardIterator first, ForwardIterator last, const T& value);
237
238template <class ForwardIterator, class Predicate>
239    ForwardIterator
240    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
241
242template <class InputIterator, class OutputIterator, class T>
243    OutputIterator
244    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
245
246template <class InputIterator, class OutputIterator, class Predicate>
247    OutputIterator
248    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
249
250template <class ForwardIterator>
251    ForwardIterator
252    unique(ForwardIterator first, ForwardIterator last);
253
254template <class ForwardIterator, class BinaryPredicate>
255    ForwardIterator
256    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
257
258template <class InputIterator, class OutputIterator>
259    OutputIterator
260    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
261
262template <class InputIterator, class OutputIterator, class BinaryPredicate>
263    OutputIterator
264    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
265
266template <class BidirectionalIterator>
267    void
268    reverse(BidirectionalIterator first, BidirectionalIterator last);
269
270template <class BidirectionalIterator, class OutputIterator>
271    OutputIterator
272    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
273
274template <class ForwardIterator>
275    ForwardIterator
276    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
277
278template <class ForwardIterator, class OutputIterator>
279    OutputIterator
280    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
281
282template <class RandomAccessIterator>
283    void
284    random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
285
286template <class RandomAccessIterator, class RandomNumberGenerator>
287    void
288    random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289                   RandomNumberGenerator& rand);  // deprecated in C++14
290
291template<class RandomAccessIterator, class UniformRandomNumberGenerator>
292    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
293                 UniformRandomNumberGenerator&& g);
294
295template <class InputIterator, class Predicate>
296    bool
297    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
298
299template <class ForwardIterator, class Predicate>
300    ForwardIterator
301    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
302
303template <class InputIterator, class OutputIterator1,
304          class OutputIterator2, class Predicate>
305    pair<OutputIterator1, OutputIterator2>
306    partition_copy(InputIterator first, InputIterator last,
307                   OutputIterator1 out_true, OutputIterator2 out_false,
308                   Predicate pred);
309
310template <class ForwardIterator, class Predicate>
311    ForwardIterator
312    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313
314template<class ForwardIterator, class Predicate>
315    ForwardIterator
316    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
317
318template <class ForwardIterator>
319    bool
320    is_sorted(ForwardIterator first, ForwardIterator last);
321
322template <class ForwardIterator, class Compare>
323    bool
324    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
325
326template<class ForwardIterator>
327    ForwardIterator
328    is_sorted_until(ForwardIterator first, ForwardIterator last);
329
330template <class ForwardIterator, class Compare>
331    ForwardIterator
332    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
333
334template <class RandomAccessIterator>
335    void
336    sort(RandomAccessIterator first, RandomAccessIterator last);
337
338template <class RandomAccessIterator, class Compare>
339    void
340    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
341
342template <class RandomAccessIterator>
343    void
344    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
345
346template <class RandomAccessIterator, class Compare>
347    void
348    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
349
350template <class RandomAccessIterator>
351    void
352    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
353
354template <class RandomAccessIterator, class Compare>
355    void
356    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
357
358template <class InputIterator, class RandomAccessIterator>
359    RandomAccessIterator
360    partial_sort_copy(InputIterator first, InputIterator last,
361                      RandomAccessIterator result_first, RandomAccessIterator result_last);
362
363template <class InputIterator, class RandomAccessIterator, class Compare>
364    RandomAccessIterator
365    partial_sort_copy(InputIterator first, InputIterator last,
366                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
367
368template <class RandomAccessIterator>
369    void
370    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
371
372template <class RandomAccessIterator, class Compare>
373    void
374    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
375
376template <class ForwardIterator, class T>
377    ForwardIterator
378    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
379
380template <class ForwardIterator, class T, class Compare>
381    ForwardIterator
382    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
383
384template <class ForwardIterator, class T>
385    ForwardIterator
386    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
387
388template <class ForwardIterator, class T, class Compare>
389    ForwardIterator
390    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
391
392template <class ForwardIterator, class T>
393    pair<ForwardIterator, ForwardIterator>
394    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
395
396template <class ForwardIterator, class T, class Compare>
397    pair<ForwardIterator, ForwardIterator>
398    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
399
400template <class ForwardIterator, class T>
401    bool
402    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
403
404template <class ForwardIterator, class T, class Compare>
405    bool
406    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
407
408template <class InputIterator1, class InputIterator2, class OutputIterator>
409    OutputIterator
410    merge(InputIterator1 first1, InputIterator1 last1,
411          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
412
413template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
414    OutputIterator
415    merge(InputIterator1 first1, InputIterator1 last1,
416          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
417
418template <class BidirectionalIterator>
419    void
420    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
421
422template <class BidirectionalIterator, class Compare>
423    void
424    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
425
426template <class InputIterator1, class InputIterator2>
427    bool
428    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
429
430template <class InputIterator1, class InputIterator2, class Compare>
431    bool
432    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
433
434template <class InputIterator1, class InputIterator2, class OutputIterator>
435    OutputIterator
436    set_union(InputIterator1 first1, InputIterator1 last1,
437              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
438
439template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
440    OutputIterator
441    set_union(InputIterator1 first1, InputIterator1 last1,
442              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
443
444template <class InputIterator1, class InputIterator2, class OutputIterator>
445    OutputIterator
446    set_intersection(InputIterator1 first1, InputIterator1 last1,
447                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448
449template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450    OutputIterator
451    set_intersection(InputIterator1 first1, InputIterator1 last1,
452                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453
454template <class InputIterator1, class InputIterator2, class OutputIterator>
455    OutputIterator
456    set_difference(InputIterator1 first1, InputIterator1 last1,
457                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458
459template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460    OutputIterator
461    set_difference(InputIterator1 first1, InputIterator1 last1,
462                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463
464template <class InputIterator1, class InputIterator2, class OutputIterator>
465    OutputIterator
466    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
467                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468
469template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470    OutputIterator
471    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
472                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473
474template <class RandomAccessIterator>
475    void
476    push_heap(RandomAccessIterator first, RandomAccessIterator last);
477
478template <class RandomAccessIterator, class Compare>
479    void
480    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
481
482template <class RandomAccessIterator>
483    void
484    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
485
486template <class RandomAccessIterator, class Compare>
487    void
488    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
489
490template <class RandomAccessIterator>
491    void
492    make_heap(RandomAccessIterator first, RandomAccessIterator last);
493
494template <class RandomAccessIterator, class Compare>
495    void
496    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
497
498template <class RandomAccessIterator>
499    void
500    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
501
502template <class RandomAccessIterator, class Compare>
503    void
504    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
505
506template <class RandomAccessIterator>
507    bool
508    is_heap(RandomAccessIterator first, RandomAccessiterator last);
509
510template <class RandomAccessIterator, class Compare>
511    bool
512    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
513
514template <class RandomAccessIterator>
515    RandomAccessIterator
516    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
517
518template <class RandomAccessIterator, class Compare>
519    RandomAccessIterator
520    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
521
522template <class ForwardIterator>
523    ForwardIterator
524    min_element(ForwardIterator first, ForwardIterator last);
525
526template <class ForwardIterator, class Compare>
527    ForwardIterator
528    min_element(ForwardIterator first, ForwardIterator last, Compare comp);
529
530template <class T>
531    const T&
532    min(const T& a, const T& b);  // constexpr in C++14
533
534template <class T, class Compare>
535    const T&
536    min(const T& a, const T& b, Compare comp);  // constexpr in C++14
537
538template<class T>
539    T
540    min(initializer_list<T> t);  // constexpr in C++14
541
542template<class T, class Compare>
543    T
544    min(initializer_list<T> t, Compare comp);  // constexpr in C++14
545
546template <class ForwardIterator>
547    ForwardIterator
548    max_element(ForwardIterator first, ForwardIterator last);
549
550template <class ForwardIterator, class Compare>
551    ForwardIterator
552    max_element(ForwardIterator first, ForwardIterator last, Compare comp);
553
554template <class T>
555    const T&
556    max(const T& a, const T& b); // constexpr in C++14
557
558template <class T, class Compare>
559    const T&
560    max(const T& a, const T& b, Compare comp);  // constexpr in C++14
561
562template<class T>
563    T
564    max(initializer_list<T> t);  // constexpr in C++14
565
566template<class T, class Compare>
567    T
568    max(initializer_list<T> t, Compare comp);  // constexpr in C++14
569
570template<class ForwardIterator>
571    pair<ForwardIterator, ForwardIterator>
572    minmax_element(ForwardIterator first, ForwardIterator last);
573
574template<class ForwardIterator, class Compare>
575    pair<ForwardIterator, ForwardIterator>
576    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
577
578template<class T>
579    pair<const T&, const T&>
580    minmax(const T& a, const T& b);  // constexpr in C++14
581
582template<class T, class Compare>
583    pair<const T&, const T&>
584    minmax(const T& a, const T& b, Compare comp);  // constexpr in C++14
585
586template<class T>
587    pair<T, T>
588    minmax(initializer_list<T> t);  // constexpr in C++14
589
590template<class T, class Compare>
591    pair<T, T>
592    minmax(initializer_list<T> t, Compare comp);  // constexpr in C++14
593
594template <class InputIterator1, class InputIterator2>
595    bool
596    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
597
598template <class InputIterator1, class InputIterator2, class Compare>
599    bool
600    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
601                            InputIterator2 first2, InputIterator2 last2, Compare comp);
602
603template <class BidirectionalIterator>
604    bool
605    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
606
607template <class BidirectionalIterator, class Compare>
608    bool
609    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
610
611template <class BidirectionalIterator>
612    bool
613    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
614
615template <class BidirectionalIterator, class Compare>
616    bool
617    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
618
619}  // std
620
621*/
622
623#include <__config>
624#include <initializer_list>
625#include <type_traits>
626#include <cstring>
627#include <utility>
628#include <memory>
629#include <iterator>
630#include <cstddef>
631
632#if defined(__IBMCPP__)
633#include "support/ibm/support.h"
634#endif
635#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
636#include "support/win32/support.h"
637#endif
638
639#include <__undef_min_max>
640
641#include <__debug>
642
643#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
644#pragma GCC system_header
645#endif
646
647_LIBCPP_BEGIN_NAMESPACE_STD
648
649// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
650//   * That only works with C++14 and later, and
651//   * We haven't included <functional> here.
652template <class _T1, class _T2 = _T1>
653struct __equal_to
654{
655    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
656    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
657    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
658    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
659};
660
661template <class _T1>
662struct __equal_to<_T1, _T1>
663{
664    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
665    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
666};
667
668template <class _T1>
669struct __equal_to<const _T1, _T1>
670{
671    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
672    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
673};
674
675template <class _T1>
676struct __equal_to<_T1, const _T1>
677{
678    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
680};
681
682template <class _T1, class _T2 = _T1>
683struct __less
684{
685    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
687
688    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689    bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
690
691    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692    bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
693
694    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695    bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
696};
697
698template <class _T1>
699struct __less<_T1, _T1>
700{
701    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
703};
704
705template <class _T1>
706struct __less<const _T1, _T1>
707{
708    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
710};
711
712template <class _T1>
713struct __less<_T1, const _T1>
714{
715    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
717};
718
719template <class _Predicate>
720class __negate
721{
722private:
723    _Predicate __p_;
724public:
725    _LIBCPP_INLINE_VISIBILITY __negate() {}
726
727    _LIBCPP_INLINE_VISIBILITY
728    explicit __negate(_Predicate __p) : __p_(__p) {}
729
730    template <class _T1>
731    _LIBCPP_INLINE_VISIBILITY
732    bool operator()(const _T1& __x) {return !__p_(__x);}
733
734    template <class _T1, class _T2>
735    _LIBCPP_INLINE_VISIBILITY
736    bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
737};
738
739#ifdef _LIBCPP_DEBUG
740
741template <class _Compare>
742struct __debug_less
743{
744    _Compare __comp_;
745    __debug_less(_Compare& __c) : __comp_(__c) {}
746    template <class _Tp, class _Up>
747    bool operator()(const _Tp& __x, const _Up& __y)
748    {
749        bool __r = __comp_(__x, __y);
750        if (__r)
751            _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
752        return __r;
753    }
754};
755
756#endif  // _LIBCPP_DEBUG
757
758// Precondition:  __x != 0
759inline _LIBCPP_INLINE_VISIBILITY
760unsigned
761__ctz(unsigned __x)
762{
763    return static_cast<unsigned>(__builtin_ctz(__x));
764}
765
766inline _LIBCPP_INLINE_VISIBILITY
767unsigned long
768__ctz(unsigned long __x)
769{
770    return static_cast<unsigned long>(__builtin_ctzl(__x));
771}
772
773inline _LIBCPP_INLINE_VISIBILITY
774unsigned long long
775__ctz(unsigned long long __x)
776{
777    return static_cast<unsigned long long>(__builtin_ctzll(__x));
778}
779
780// Precondition:  __x != 0
781inline _LIBCPP_INLINE_VISIBILITY
782unsigned
783__clz(unsigned __x)
784{
785    return static_cast<unsigned>(__builtin_clz(__x));
786}
787
788inline _LIBCPP_INLINE_VISIBILITY
789unsigned long
790__clz(unsigned long __x)
791{
792    return static_cast<unsigned long>(__builtin_clzl (__x));
793}
794
795inline _LIBCPP_INLINE_VISIBILITY
796unsigned long long
797__clz(unsigned long long __x)
798{
799    return static_cast<unsigned long long>(__builtin_clzll(__x));
800}
801
802inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned           __x) {return __builtin_popcount  (__x);}
803inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned      long __x) {return __builtin_popcountl (__x);}
804inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
805
806// all_of
807
808template <class _InputIterator, class _Predicate>
809inline _LIBCPP_INLINE_VISIBILITY
810bool
811all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
812{
813    for (; __first != __last; ++__first)
814        if (!__pred(*__first))
815            return false;
816    return true;
817}
818
819// any_of
820
821template <class _InputIterator, class _Predicate>
822inline _LIBCPP_INLINE_VISIBILITY
823bool
824any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
825{
826    for (; __first != __last; ++__first)
827        if (__pred(*__first))
828            return true;
829    return false;
830}
831
832// none_of
833
834template <class _InputIterator, class _Predicate>
835inline _LIBCPP_INLINE_VISIBILITY
836bool
837none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
838{
839    for (; __first != __last; ++__first)
840        if (__pred(*__first))
841            return false;
842    return true;
843}
844
845// for_each
846
847template <class _InputIterator, class _Function>
848inline _LIBCPP_INLINE_VISIBILITY
849_Function
850for_each(_InputIterator __first, _InputIterator __last, _Function __f)
851{
852    for (; __first != __last; ++__first)
853        __f(*__first);
854    return _VSTD::move(__f);  // explicitly moved for (emulated) C++03
855}
856
857// find
858
859template <class _InputIterator, class _Tp>
860inline _LIBCPP_INLINE_VISIBILITY
861_InputIterator
862find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
863{
864    for (; __first != __last; ++__first)
865        if (*__first == __value_)
866            break;
867    return __first;
868}
869
870// find_if
871
872template <class _InputIterator, class _Predicate>
873inline _LIBCPP_INLINE_VISIBILITY
874_InputIterator
875find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
876{
877    for (; __first != __last; ++__first)
878        if (__pred(*__first))
879            break;
880    return __first;
881}
882
883// find_if_not
884
885template<class _InputIterator, class _Predicate>
886inline _LIBCPP_INLINE_VISIBILITY
887_InputIterator
888find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
889{
890    for (; __first != __last; ++__first)
891        if (!__pred(*__first))
892            break;
893    return __first;
894}
895
896// find_end
897
898template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
899_ForwardIterator1
900__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
901           _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
902           forward_iterator_tag, forward_iterator_tag)
903{
904    // modeled after search algorithm
905    _ForwardIterator1 __r = __last1;  // __last1 is the "default" answer
906    if (__first2 == __last2)
907        return __r;
908    while (true)
909    {
910        while (true)
911        {
912            if (__first1 == __last1)         // if source exhausted return last correct answer
913                return __r;                  //    (or __last1 if never found)
914            if (__pred(*__first1, *__first2))
915                break;
916            ++__first1;
917        }
918        // *__first1 matches *__first2, now match elements after here
919        _ForwardIterator1 __m1 = __first1;
920        _ForwardIterator2 __m2 = __first2;
921        while (true)
922        {
923            if (++__m2 == __last2)
924            {                         // Pattern exhaused, record answer and search for another one
925                __r = __first1;
926                ++__first1;
927                break;
928            }
929            if (++__m1 == __last1)     // Source exhausted, return last answer
930                return __r;
931            if (!__pred(*__m1, *__m2))  // mismatch, restart with a new __first
932            {
933                ++__first1;
934                break;
935            }  // else there is a match, check next elements
936        }
937    }
938}
939
940template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
941_BidirectionalIterator1
942__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
943           _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
944           bidirectional_iterator_tag, bidirectional_iterator_tag)
945{
946    // modeled after search algorithm (in reverse)
947    if (__first2 == __last2)
948        return __last1;  // Everything matches an empty sequence
949    _BidirectionalIterator1 __l1 = __last1;
950    _BidirectionalIterator2 __l2 = __last2;
951    --__l2;
952    while (true)
953    {
954        // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
955        while (true)
956        {
957            if (__first1 == __l1)  // return __last1 if no element matches *__first2
958                return __last1;
959            if (__pred(*--__l1, *__l2))
960                break;
961        }
962        // *__l1 matches *__l2, now match elements before here
963        _BidirectionalIterator1 __m1 = __l1;
964        _BidirectionalIterator2 __m2 = __l2;
965        while (true)
966        {
967            if (__m2 == __first2)  // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
968                return __m1;
969            if (__m1 == __first1)  // Otherwise if source exhaused, pattern not found
970                return __last1;
971            if (!__pred(*--__m1, *--__m2))  // if there is a mismatch, restart with a new __l1
972            {
973                break;
974            }  // else there is a match, check next elements
975        }
976    }
977}
978
979template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
980_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
981__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
982           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
983           random_access_iterator_tag, random_access_iterator_tag)
984{
985    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
986    typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
987    if (__len2 == 0)
988        return __last1;
989    typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
990    if (__len1 < __len2)
991        return __last1;
992    const _RandomAccessIterator1 __s = __first1 + (__len2 - 1);  // End of pattern match can't go before here
993    _RandomAccessIterator1 __l1 = __last1;
994    _RandomAccessIterator2 __l2 = __last2;
995    --__l2;
996    while (true)
997    {
998        while (true)
999        {
1000            if (__s == __l1)
1001                return __last1;
1002            if (__pred(*--__l1, *__l2))
1003                break;
1004        }
1005        _RandomAccessIterator1 __m1 = __l1;
1006        _RandomAccessIterator2 __m2 = __l2;
1007        while (true)
1008        {
1009            if (__m2 == __first2)
1010                return __m1;
1011                                 // no need to check range on __m1 because __s guarantees we have enough source
1012            if (!__pred(*--__m1, *--__m2))
1013            {
1014                break;
1015            }
1016        }
1017    }
1018}
1019
1020template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1021inline _LIBCPP_INLINE_VISIBILITY
1022_ForwardIterator1
1023find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1024         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1025{
1026    return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1027                         (__first1, __last1, __first2, __last2, __pred,
1028                          typename iterator_traits<_ForwardIterator1>::iterator_category(),
1029                          typename iterator_traits<_ForwardIterator2>::iterator_category());
1030}
1031
1032template <class _ForwardIterator1, class _ForwardIterator2>
1033inline _LIBCPP_INLINE_VISIBILITY
1034_ForwardIterator1
1035find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1036         _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1037{
1038    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1039    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1040    return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1041}
1042
1043// find_first_of
1044
1045template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1046_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1048              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1049{
1050    for (; __first1 != __last1; ++__first1)
1051        for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1052            if (__pred(*__first1, *__j))
1053                return __first1;
1054    return __last1;
1055}
1056
1057
1058template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1059inline _LIBCPP_INLINE_VISIBILITY
1060_ForwardIterator1
1061find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1063{
1064    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1065}
1066
1067template <class _ForwardIterator1, class _ForwardIterator2>
1068inline _LIBCPP_INLINE_VISIBILITY
1069_ForwardIterator1
1070find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071              _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1072{
1073    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1074    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1075    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1076}
1077
1078// adjacent_find
1079
1080template <class _ForwardIterator, class _BinaryPredicate>
1081inline _LIBCPP_INLINE_VISIBILITY
1082_ForwardIterator
1083adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1084{
1085    if (__first != __last)
1086    {
1087        _ForwardIterator __i = __first;
1088        while (++__i != __last)
1089        {
1090            if (__pred(*__first, *__i))
1091                return __first;
1092            __first = __i;
1093        }
1094    }
1095    return __last;
1096}
1097
1098template <class _ForwardIterator>
1099inline _LIBCPP_INLINE_VISIBILITY
1100_ForwardIterator
1101adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1102{
1103    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1104    return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1105}
1106
1107// count
1108
1109template <class _InputIterator, class _Tp>
1110inline _LIBCPP_INLINE_VISIBILITY
1111typename iterator_traits<_InputIterator>::difference_type
1112count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1113{
1114    typename iterator_traits<_InputIterator>::difference_type __r(0);
1115    for (; __first != __last; ++__first)
1116        if (*__first == __value_)
1117            ++__r;
1118    return __r;
1119}
1120
1121// count_if
1122
1123template <class _InputIterator, class _Predicate>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename iterator_traits<_InputIterator>::difference_type
1126count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1127{
1128    typename iterator_traits<_InputIterator>::difference_type __r(0);
1129    for (; __first != __last; ++__first)
1130        if (__pred(*__first))
1131            ++__r;
1132    return __r;
1133}
1134
1135// mismatch
1136
1137template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1138inline _LIBCPP_INLINE_VISIBILITY
1139pair<_InputIterator1, _InputIterator2>
1140mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141         _InputIterator2 __first2, _BinaryPredicate __pred)
1142{
1143    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1144        if (!__pred(*__first1, *__first2))
1145            break;
1146    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1147}
1148
1149template <class _InputIterator1, class _InputIterator2>
1150inline _LIBCPP_INLINE_VISIBILITY
1151pair<_InputIterator1, _InputIterator2>
1152mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1153{
1154    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1155    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1156    return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1157}
1158
1159#if _LIBCPP_STD_VER > 11
1160template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1161inline _LIBCPP_INLINE_VISIBILITY
1162pair<_InputIterator1, _InputIterator2>
1163mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1164         _InputIterator2 __first2, _InputIterator2 __last2,
1165         _BinaryPredicate __pred)
1166{
1167    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1168        if (!__pred(*__first1, *__first2))
1169            break;
1170    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1171}
1172
1173template <class _InputIterator1, class _InputIterator2>
1174inline _LIBCPP_INLINE_VISIBILITY
1175pair<_InputIterator1, _InputIterator2>
1176mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1177         _InputIterator2 __first2, _InputIterator2 __last2)
1178{
1179    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1181    return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1182}
1183#endif
1184
1185// equal
1186
1187template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1188inline _LIBCPP_INLINE_VISIBILITY
1189bool
1190equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1191{
1192    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1193        if (!__pred(*__first1, *__first2))
1194            return false;
1195    return true;
1196}
1197
1198template <class _InputIterator1, class _InputIterator2>
1199inline _LIBCPP_INLINE_VISIBILITY
1200bool
1201equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1202{
1203    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1204    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1205    return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1206}
1207
1208#if _LIBCPP_STD_VER > 11
1209template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1210inline _LIBCPP_INLINE_VISIBILITY
1211bool
1212__equal(_InputIterator1 __first1, _InputIterator1 __last1,
1213        _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1214        input_iterator_tag, input_iterator_tag )
1215{
1216    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1217        if (!__pred(*__first1, *__first2))
1218            return false;
1219    return __first1 == __last1 && __first2 == __last2;
1220}
1221
1222template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1223inline _LIBCPP_INLINE_VISIBILITY
1224bool
1225__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1226        _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1227      random_access_iterator_tag, random_access_iterator_tag )
1228{
1229    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1230        return false;
1231    return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1232                        typename add_lvalue_reference<_BinaryPredicate>::type>
1233                       (__first1, __last1, __first2, __pred );
1234}
1235
1236template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237inline _LIBCPP_INLINE_VISIBILITY
1238bool
1239equal(_InputIterator1 __first1, _InputIterator1 __last1,
1240      _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1241{
1242    return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1243       (__first1, __last1, __first2, __last2, __pred,
1244        typename iterator_traits<_InputIterator1>::iterator_category(),
1245        typename iterator_traits<_InputIterator2>::iterator_category());
1246}
1247
1248template <class _InputIterator1, class _InputIterator2>
1249inline _LIBCPP_INLINE_VISIBILITY
1250bool
1251equal(_InputIterator1 __first1, _InputIterator1 __last1,
1252      _InputIterator2 __first2, _InputIterator2 __last2)
1253{
1254    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1255    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1256    return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1257        typename iterator_traits<_InputIterator1>::iterator_category(),
1258        typename iterator_traits<_InputIterator2>::iterator_category());
1259}
1260#endif
1261
1262// is_permutation
1263
1264template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1265bool
1266is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1267               _ForwardIterator2 __first2, _BinaryPredicate __pred)
1268{
1269    // shorten sequences as much as possible by lopping of any equal parts
1270    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1271        if (!__pred(*__first1, *__first2))
1272            goto __not_done;
1273    return true;
1274__not_done:
1275    // __first1 != __last1 && *__first1 != *__first2
1276    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1277    _D1 __l1 = _VSTD::distance(__first1, __last1);
1278    if (__l1 == _D1(1))
1279        return false;
1280    _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1281    // For each element in [f1, l1) see if there are the same number of
1282    //    equal elements in [f2, l2)
1283    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1284    {
1285        // Have we already counted the number of *__i in [f1, l1)?
1286        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1287            if (__pred(*__j, *__i))
1288                goto __next_iter;
1289        {
1290            // Count number of *__i in [f2, l2)
1291            _D1 __c2 = 0;
1292            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1293                if (__pred(*__i, *__j))
1294                    ++__c2;
1295            if (__c2 == 0)
1296                return false;
1297            // Count number of *__i in [__i, l1) (we can start with 1)
1298            _D1 __c1 = 1;
1299            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1300                if (__pred(*__i, *__j))
1301                    ++__c1;
1302            if (__c1 != __c2)
1303                return false;
1304        }
1305__next_iter:;
1306    }
1307    return true;
1308}
1309
1310template<class _ForwardIterator1, class _ForwardIterator2>
1311inline _LIBCPP_INLINE_VISIBILITY
1312bool
1313is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1314               _ForwardIterator2 __first2)
1315{
1316    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1317    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1318    return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1319}
1320
1321#if _LIBCPP_STD_VER > 11
1322template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1323bool
1324__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1325                 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1326                 _BinaryPredicate __pred,
1327                 forward_iterator_tag, forward_iterator_tag )
1328{
1329    // shorten sequences as much as possible by lopping of any equal parts
1330    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1331        if (!__pred(*__first1, *__first2))
1332            goto __not_done;
1333    return __first1 == __last1 && __first2 == __last2;
1334__not_done:
1335    // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1336    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1337    _D1 __l1 = _VSTD::distance(__first1, __last1);
1338
1339    typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1340    _D2 __l2 = _VSTD::distance(__first2, __last2);
1341    if (__l1 != __l2)
1342        return false;
1343
1344    // For each element in [f1, l1) see if there are the same number of
1345    //    equal elements in [f2, l2)
1346    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1347    {
1348        // Have we already counted the number of *__i in [f1, l1)?
1349        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1350            if (__pred(*__j, *__i))
1351                goto __next_iter;
1352        {
1353            // Count number of *__i in [f2, l2)
1354            _D1 __c2 = 0;
1355            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1356                if (__pred(*__i, *__j))
1357                    ++__c2;
1358            if (__c2 == 0)
1359                return false;
1360            // Count number of *__i in [__i, l1) (we can start with 1)
1361            _D1 __c1 = 1;
1362            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1363                if (__pred(*__i, *__j))
1364                    ++__c1;
1365            if (__c1 != __c2)
1366                return false;
1367        }
1368__next_iter:;
1369    }
1370    return true;
1371}
1372
1373template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1374bool
1375__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1376               _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1377               _BinaryPredicate __pred,
1378               random_access_iterator_tag, random_access_iterator_tag )
1379{
1380    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1381        return false;
1382    return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1383                                 typename add_lvalue_reference<_BinaryPredicate>::type>
1384                                (__first1, __last1, __first2, __pred );
1385}
1386
1387template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1388inline _LIBCPP_INLINE_VISIBILITY
1389bool
1390is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1391               _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1392               _BinaryPredicate __pred )
1393{
1394    return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1395       (__first1, __last1, __first2, __last2, __pred,
1396        typename iterator_traits<_ForwardIterator1>::iterator_category(),
1397        typename iterator_traits<_ForwardIterator2>::iterator_category());
1398}
1399
1400template<class _ForwardIterator1, class _ForwardIterator2>
1401inline _LIBCPP_INLINE_VISIBILITY
1402bool
1403is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1404               _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1405{
1406    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1407    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1408    return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1409        __equal_to<__v1, __v2>(),
1410        typename iterator_traits<_ForwardIterator1>::iterator_category(),
1411        typename iterator_traits<_ForwardIterator2>::iterator_category());
1412}
1413#endif
1414
1415// search
1416
1417template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1418_ForwardIterator1
1419__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1421         forward_iterator_tag, forward_iterator_tag)
1422{
1423    if (__first2 == __last2)
1424        return __first1;  // Everything matches an empty sequence
1425    while (true)
1426    {
1427        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1428        while (true)
1429        {
1430            if (__first1 == __last1)  // return __last1 if no element matches *__first2
1431                return __last1;
1432            if (__pred(*__first1, *__first2))
1433                break;
1434            ++__first1;
1435        }
1436        // *__first1 matches *__first2, now match elements after here
1437        _ForwardIterator1 __m1 = __first1;
1438        _ForwardIterator2 __m2 = __first2;
1439        while (true)
1440        {
1441            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1442                return __first1;
1443            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
1444                return __last1;
1445            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
1446            {
1447                ++__first1;
1448                break;
1449            }  // else there is a match, check next elements
1450        }
1451    }
1452}
1453
1454template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1455_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1456__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1457           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1458           random_access_iterator_tag, random_access_iterator_tag)
1459{
1460    typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1461    typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1462    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
1463    _D2 __len2 = __last2 - __first2;
1464    if (__len2 == 0)
1465        return __first1;
1466    _D1 __len1 = __last1 - __first1;
1467    if (__len1 < __len2)
1468        return __last1;
1469    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
1470    while (true)
1471    {
1472#if !_LIBCPP_UNROLL_LOOPS
1473        while (true)
1474        {
1475            if (__first1 == __s)
1476                return __last1;
1477            if (__pred(*__first1, *__first2))
1478                break;
1479            ++__first1;
1480        }
1481#else  // !_LIBCPP_UNROLL_LOOPS
1482        for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1483        {
1484            if (__pred(*__first1, *__first2))
1485                goto __phase2;
1486            if (__pred(*++__first1, *__first2))
1487                goto __phase2;
1488            if (__pred(*++__first1, *__first2))
1489                goto __phase2;
1490            if (__pred(*++__first1, *__first2))
1491                goto __phase2;
1492            ++__first1;
1493        }
1494        switch (__s - __first1)
1495        {
1496        case 3:
1497            if (__pred(*__first1, *__first2))
1498                break;
1499            ++__first1;
1500        case 2:
1501            if (__pred(*__first1, *__first2))
1502                break;
1503            ++__first1;
1504        case 1:
1505            if (__pred(*__first1, *__first2))
1506                break;
1507        case 0:
1508            return __last1;
1509        }
1510    __phase2:
1511#endif  // !_LIBCPP_UNROLL_LOOPS
1512        _RandomAccessIterator1 __m1 = __first1;
1513        _RandomAccessIterator2 __m2 = __first2;
1514#if !_LIBCPP_UNROLL_LOOPS
1515         while (true)
1516         {
1517             if (++__m2 == __last2)
1518                 return __first1;
1519             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
1520             if (!__pred(*__m1, *__m2))
1521             {
1522                 ++__first1;
1523                 break;
1524             }
1525         }
1526#else  // !_LIBCPP_UNROLL_LOOPS
1527        ++__m2;
1528        ++__m1;
1529        for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1530        {
1531            if (!__pred(*__m1, *__m2))
1532                goto __continue;
1533            if (!__pred(*++__m1, *++__m2))
1534                goto __continue;
1535            if (!__pred(*++__m1, *++__m2))
1536                goto __continue;
1537            if (!__pred(*++__m1, *++__m2))
1538                goto __continue;
1539            ++__m1;
1540            ++__m2;
1541        }
1542        switch (__last2 - __m2)
1543        {
1544        case 3:
1545            if (!__pred(*__m1, *__m2))
1546                break;
1547            ++__m1;
1548            ++__m2;
1549        case 2:
1550            if (!__pred(*__m1, *__m2))
1551                break;
1552            ++__m1;
1553            ++__m2;
1554        case 1:
1555            if (!__pred(*__m1, *__m2))
1556                break;
1557        case 0:
1558            return __first1;
1559        }
1560    __continue:
1561        ++__first1;
1562#endif  // !_LIBCPP_UNROLL_LOOPS
1563    }
1564}
1565
1566template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1567inline _LIBCPP_INLINE_VISIBILITY
1568_ForwardIterator1
1569search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1570       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1571{
1572    return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1573                         (__first1, __last1, __first2, __last2, __pred,
1574                          typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1575                          typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1576}
1577
1578template <class _ForwardIterator1, class _ForwardIterator2>
1579inline _LIBCPP_INLINE_VISIBILITY
1580_ForwardIterator1
1581search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1582       _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1583{
1584    typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1585    typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1586    return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1587}
1588
1589// search_n
1590
1591template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1592_ForwardIterator
1593__search_n(_ForwardIterator __first, _ForwardIterator __last,
1594           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1595{
1596    if (__count <= 0)
1597        return __first;
1598    while (true)
1599    {
1600        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1601        while (true)
1602        {
1603            if (__first == __last)  // return __last if no element matches __value_
1604                return __last;
1605            if (__pred(*__first, __value_))
1606                break;
1607            ++__first;
1608        }
1609        // *__first matches __value_, now match elements after here
1610        _ForwardIterator __m = __first;
1611        _Size __c(0);
1612        while (true)
1613        {
1614            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1615                return __first;
1616            if (++__m == __last)  // Otherwise if source exhaused, pattern not found
1617                return __last;
1618            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1619            {
1620                __first = __m;
1621                ++__first;
1622                break;
1623            }  // else there is a match, check next elements
1624        }
1625    }
1626}
1627
1628template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1629_RandomAccessIterator
1630__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1631           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1632{
1633    if (__count <= 0)
1634        return __first;
1635    _Size __len = static_cast<_Size>(__last - __first);
1636    if (__len < __count)
1637        return __last;
1638    const _RandomAccessIterator __s = __last - (__count - 1);  // Start of pattern match can't go beyond here
1639    while (true)
1640    {
1641        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1642        while (true)
1643        {
1644            if (__first >= __s)  // return __last if no element matches __value_
1645                return __last;
1646            if (__pred(*__first, __value_))
1647                break;
1648            ++__first;
1649        }
1650        // *__first matches __value_, now match elements after here
1651        _RandomAccessIterator __m = __first;
1652        _Size __c(0);
1653        while (true)
1654        {
1655            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1656                return __first;
1657             ++__m;          // no need to check range on __m because __s guarantees we have enough source
1658            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1659            {
1660                __first = __m;
1661                ++__first;
1662                break;
1663            }  // else there is a match, check next elements
1664        }
1665    }
1666}
1667
1668template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1669inline _LIBCPP_INLINE_VISIBILITY
1670_ForwardIterator
1671search_n(_ForwardIterator __first, _ForwardIterator __last,
1672         _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1673{
1674    return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1675           (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1676}
1677
1678template <class _ForwardIterator, class _Size, class _Tp>
1679inline _LIBCPP_INLINE_VISIBILITY
1680_ForwardIterator
1681search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1682{
1683    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1684    return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
1685}
1686
1687// copy
1688
1689template <class _Iter>
1690struct __libcpp_is_trivial_iterator
1691{
1692    static const bool value = is_pointer<_Iter>::value;
1693};
1694
1695template <class _Iter>
1696struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1697{
1698    static const bool value = is_pointer<_Iter>::value;
1699};
1700
1701template <class _Iter>
1702struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1703{
1704    static const bool value = is_pointer<_Iter>::value;
1705};
1706
1707template <class _Iter>
1708inline _LIBCPP_INLINE_VISIBILITY
1709_Iter
1710__unwrap_iter(_Iter __i)
1711{
1712    return __i;
1713}
1714
1715template <class _Tp>
1716inline _LIBCPP_INLINE_VISIBILITY
1717typename enable_if
1718<
1719    is_trivially_copy_assignable<_Tp>::value,
1720    _Tp*
1721>::type
1722__unwrap_iter(move_iterator<_Tp*> __i)
1723{
1724    return __i.base();
1725}
1726
1727#if _LIBCPP_DEBUG_LEVEL < 2
1728
1729template <class _Tp>
1730inline _LIBCPP_INLINE_VISIBILITY
1731typename enable_if
1732<
1733    is_trivially_copy_assignable<_Tp>::value,
1734    _Tp*
1735>::type
1736__unwrap_iter(__wrap_iter<_Tp*> __i)
1737{
1738    return __i.base();
1739}
1740
1741#endif  // _LIBCPP_DEBUG_LEVEL < 2
1742
1743template <class _InputIterator, class _OutputIterator>
1744inline _LIBCPP_INLINE_VISIBILITY
1745_OutputIterator
1746__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1747{
1748    for (; __first != __last; ++__first, (void) ++__result)
1749        *__result = *__first;
1750    return __result;
1751}
1752
1753template <class _Tp, class _Up>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
1757    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1758    is_trivially_copy_assignable<_Up>::value,
1759    _Up*
1760>::type
1761__copy(_Tp* __first, _Tp* __last, _Up* __result)
1762{
1763    const size_t __n = static_cast<size_t>(__last - __first);
1764    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1765    return __result + __n;
1766}
1767
1768template <class _InputIterator, class _OutputIterator>
1769inline _LIBCPP_INLINE_VISIBILITY
1770_OutputIterator
1771copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1772{
1773    return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1774}
1775
1776// copy_backward
1777
1778template <class _BidirectionalIterator, class _OutputIterator>
1779inline _LIBCPP_INLINE_VISIBILITY
1780_OutputIterator
1781__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1782{
1783    while (__first != __last)
1784        *--__result = *--__last;
1785    return __result;
1786}
1787
1788template <class _Tp, class _Up>
1789inline _LIBCPP_INLINE_VISIBILITY
1790typename enable_if
1791<
1792    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1793    is_trivially_copy_assignable<_Up>::value,
1794    _Up*
1795>::type
1796__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1797{
1798    const size_t __n = static_cast<size_t>(__last - __first);
1799    __result -= __n;
1800    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1801    return __result;
1802}
1803
1804template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1805inline _LIBCPP_INLINE_VISIBILITY
1806_BidirectionalIterator2
1807copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1808              _BidirectionalIterator2 __result)
1809{
1810    return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1811}
1812
1813// copy_if
1814
1815template<class _InputIterator, class _OutputIterator, class _Predicate>
1816inline _LIBCPP_INLINE_VISIBILITY
1817_OutputIterator
1818copy_if(_InputIterator __first, _InputIterator __last,
1819        _OutputIterator __result, _Predicate __pred)
1820{
1821    for (; __first != __last; ++__first)
1822    {
1823        if (__pred(*__first))
1824        {
1825            *__result = *__first;
1826            ++__result;
1827        }
1828    }
1829    return __result;
1830}
1831
1832// copy_n
1833
1834template<class _InputIterator, class _Size, class _OutputIterator>
1835inline _LIBCPP_INLINE_VISIBILITY
1836typename enable_if
1837<
1838    __is_input_iterator<_InputIterator>::value &&
1839   !__is_random_access_iterator<_InputIterator>::value,
1840    _OutputIterator
1841>::type
1842copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1843{
1844    if (__n > 0)
1845    {
1846        *__result = *__first;
1847        ++__result;
1848        for (--__n; __n > 0; --__n)
1849        {
1850            ++__first;
1851            *__result = *__first;
1852            ++__result;
1853        }
1854    }
1855    return __result;
1856}
1857
1858template<class _InputIterator, class _Size, class _OutputIterator>
1859inline _LIBCPP_INLINE_VISIBILITY
1860typename enable_if
1861<
1862    __is_random_access_iterator<_InputIterator>::value,
1863    _OutputIterator
1864>::type
1865copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1866{
1867    return _VSTD::copy(__first, __first + __n, __result);
1868}
1869
1870// move
1871
1872template <class _InputIterator, class _OutputIterator>
1873inline _LIBCPP_INLINE_VISIBILITY
1874_OutputIterator
1875__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1876{
1877    for (; __first != __last; ++__first, (void) ++__result)
1878        *__result = _VSTD::move(*__first);
1879    return __result;
1880}
1881
1882template <class _Tp, class _Up>
1883inline _LIBCPP_INLINE_VISIBILITY
1884typename enable_if
1885<
1886    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1887    is_trivially_copy_assignable<_Up>::value,
1888    _Up*
1889>::type
1890__move(_Tp* __first, _Tp* __last, _Up* __result)
1891{
1892    const size_t __n = static_cast<size_t>(__last - __first);
1893    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1894    return __result + __n;
1895}
1896
1897template <class _InputIterator, class _OutputIterator>
1898inline _LIBCPP_INLINE_VISIBILITY
1899_OutputIterator
1900move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1901{
1902    return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1903}
1904
1905// move_backward
1906
1907template <class _InputIterator, class _OutputIterator>
1908inline _LIBCPP_INLINE_VISIBILITY
1909_OutputIterator
1910__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1911{
1912    while (__first != __last)
1913        *--__result = _VSTD::move(*--__last);
1914    return __result;
1915}
1916
1917template <class _Tp, class _Up>
1918inline _LIBCPP_INLINE_VISIBILITY
1919typename enable_if
1920<
1921    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1922    is_trivially_copy_assignable<_Up>::value,
1923    _Up*
1924>::type
1925__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1926{
1927    const size_t __n = static_cast<size_t>(__last - __first);
1928    __result -= __n;
1929    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1930    return __result;
1931}
1932
1933template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1934inline _LIBCPP_INLINE_VISIBILITY
1935_BidirectionalIterator2
1936move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1937              _BidirectionalIterator2 __result)
1938{
1939    return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1940}
1941
1942// iter_swap
1943
1944// moved to <type_traits> for better swap / noexcept support
1945
1946// transform
1947
1948template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1949inline _LIBCPP_INLINE_VISIBILITY
1950_OutputIterator
1951transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1952{
1953    for (; __first != __last; ++__first, (void) ++__result)
1954        *__result = __op(*__first);
1955    return __result;
1956}
1957
1958template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1959inline _LIBCPP_INLINE_VISIBILITY
1960_OutputIterator
1961transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1962          _OutputIterator __result, _BinaryOperation __binary_op)
1963{
1964    for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1965        *__result = __binary_op(*__first1, *__first2);
1966    return __result;
1967}
1968
1969// replace
1970
1971template <class _ForwardIterator, class _Tp>
1972inline _LIBCPP_INLINE_VISIBILITY
1973void
1974replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1975{
1976    for (; __first != __last; ++__first)
1977        if (*__first == __old_value)
1978            *__first = __new_value;
1979}
1980
1981// replace_if
1982
1983template <class _ForwardIterator, class _Predicate, class _Tp>
1984inline _LIBCPP_INLINE_VISIBILITY
1985void
1986replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1987{
1988    for (; __first != __last; ++__first)
1989        if (__pred(*__first))
1990            *__first = __new_value;
1991}
1992
1993// replace_copy
1994
1995template <class _InputIterator, class _OutputIterator, class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
1997_OutputIterator
1998replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1999             const _Tp& __old_value, const _Tp& __new_value)
2000{
2001    for (; __first != __last; ++__first, (void) ++__result)
2002        if (*__first == __old_value)
2003            *__result = __new_value;
2004        else
2005            *__result = *__first;
2006    return __result;
2007}
2008
2009// replace_copy_if
2010
2011template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2012inline _LIBCPP_INLINE_VISIBILITY
2013_OutputIterator
2014replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2015                _Predicate __pred, const _Tp& __new_value)
2016{
2017    for (; __first != __last; ++__first, (void) ++__result)
2018        if (__pred(*__first))
2019            *__result = __new_value;
2020        else
2021            *__result = *__first;
2022    return __result;
2023}
2024
2025// fill_n
2026
2027template <class _OutputIterator, class _Size, class _Tp>
2028inline _LIBCPP_INLINE_VISIBILITY
2029_OutputIterator
2030__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2031{
2032    for (; __n > 0; ++__first, (void) --__n)
2033        *__first = __value_;
2034    return __first;
2035}
2036
2037template <class _Tp, class _Size, class _Up>
2038inline _LIBCPP_INLINE_VISIBILITY
2039typename enable_if
2040<
2041    is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2042    !is_same<_Tp, bool>::value &&
2043    is_integral<_Up>::value && sizeof(_Up) == 1,
2044    _Tp*
2045>::type
2046__fill_n(_Tp* __first, _Size __n,_Up __value_)
2047{
2048    if (__n > 0)
2049        _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2050    return __first + __n;
2051}
2052
2053template <class _OutputIterator, class _Size, class _Tp>
2054inline _LIBCPP_INLINE_VISIBILITY
2055_OutputIterator
2056fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2057{
2058   return _VSTD::__fill_n(__first, __n, __value_);
2059}
2060
2061// fill
2062
2063template <class _ForwardIterator, class _Tp>
2064inline _LIBCPP_INLINE_VISIBILITY
2065void
2066__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2067{
2068    for (; __first != __last; ++__first)
2069        *__first = __value_;
2070}
2071
2072template <class _RandomAccessIterator, class _Tp>
2073inline _LIBCPP_INLINE_VISIBILITY
2074void
2075__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2076{
2077    _VSTD::fill_n(__first, __last - __first, __value_);
2078}
2079
2080template <class _ForwardIterator, class _Tp>
2081inline _LIBCPP_INLINE_VISIBILITY
2082void
2083fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2084{
2085    _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2086}
2087
2088// generate
2089
2090template <class _ForwardIterator, class _Generator>
2091inline _LIBCPP_INLINE_VISIBILITY
2092void
2093generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2094{
2095    for (; __first != __last; ++__first)
2096        *__first = __gen();
2097}
2098
2099// generate_n
2100
2101template <class _OutputIterator, class _Size, class _Generator>
2102inline _LIBCPP_INLINE_VISIBILITY
2103_OutputIterator
2104generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
2105{
2106    for (; __n > 0; ++__first, (void) --__n)
2107        *__first = __gen();
2108    return __first;
2109}
2110
2111// remove
2112
2113template <class _ForwardIterator, class _Tp>
2114_ForwardIterator
2115remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2116{
2117    __first = _VSTD::find(__first, __last, __value_);
2118    if (__first != __last)
2119    {
2120        _ForwardIterator __i = __first;
2121        while (++__i != __last)
2122        {
2123            if (!(*__i == __value_))
2124            {
2125                *__first = _VSTD::move(*__i);
2126                ++__first;
2127            }
2128        }
2129    }
2130    return __first;
2131}
2132
2133// remove_if
2134
2135template <class _ForwardIterator, class _Predicate>
2136_ForwardIterator
2137remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2138{
2139    __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2140                           (__first, __last, __pred);
2141    if (__first != __last)
2142    {
2143        _ForwardIterator __i = __first;
2144        while (++__i != __last)
2145        {
2146            if (!__pred(*__i))
2147            {
2148                *__first = _VSTD::move(*__i);
2149                ++__first;
2150            }
2151        }
2152    }
2153    return __first;
2154}
2155
2156// remove_copy
2157
2158template <class _InputIterator, class _OutputIterator, class _Tp>
2159inline _LIBCPP_INLINE_VISIBILITY
2160_OutputIterator
2161remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2162{
2163    for (; __first != __last; ++__first)
2164    {
2165        if (!(*__first == __value_))
2166        {
2167            *__result = *__first;
2168            ++__result;
2169        }
2170    }
2171    return __result;
2172}
2173
2174// remove_copy_if
2175
2176template <class _InputIterator, class _OutputIterator, class _Predicate>
2177inline _LIBCPP_INLINE_VISIBILITY
2178_OutputIterator
2179remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2180{
2181    for (; __first != __last; ++__first)
2182    {
2183        if (!__pred(*__first))
2184        {
2185            *__result = *__first;
2186            ++__result;
2187        }
2188    }
2189    return __result;
2190}
2191
2192// unique
2193
2194template <class _ForwardIterator, class _BinaryPredicate>
2195_ForwardIterator
2196unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2197{
2198    __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2199                                 (__first, __last, __pred);
2200    if (__first != __last)
2201    {
2202        // ...  a  a  ?  ...
2203        //      f     i
2204        _ForwardIterator __i = __first;
2205        for (++__i; ++__i != __last;)
2206            if (!__pred(*__first, *__i))
2207                *++__first = _VSTD::move(*__i);
2208        ++__first;
2209    }
2210    return __first;
2211}
2212
2213template <class _ForwardIterator>
2214inline _LIBCPP_INLINE_VISIBILITY
2215_ForwardIterator
2216unique(_ForwardIterator __first, _ForwardIterator __last)
2217{
2218    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2219    return _VSTD::unique(__first, __last, __equal_to<__v>());
2220}
2221
2222// unique_copy
2223
2224template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2225_OutputIterator
2226__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2227              input_iterator_tag, output_iterator_tag)
2228{
2229    if (__first != __last)
2230    {
2231        typename iterator_traits<_InputIterator>::value_type __t(*__first);
2232        *__result = __t;
2233        ++__result;
2234        while (++__first != __last)
2235        {
2236            if (!__pred(__t, *__first))
2237            {
2238                __t = *__first;
2239                *__result = __t;
2240                ++__result;
2241            }
2242        }
2243    }
2244    return __result;
2245}
2246
2247template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2248_OutputIterator
2249__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2250              forward_iterator_tag, output_iterator_tag)
2251{
2252    if (__first != __last)
2253    {
2254        _ForwardIterator __i = __first;
2255        *__result = *__i;
2256        ++__result;
2257        while (++__first != __last)
2258        {
2259            if (!__pred(*__i, *__first))
2260            {
2261                *__result = *__first;
2262                ++__result;
2263                __i = __first;
2264            }
2265        }
2266    }
2267    return __result;
2268}
2269
2270template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2271_ForwardIterator
2272__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2273              input_iterator_tag, forward_iterator_tag)
2274{
2275    if (__first != __last)
2276    {
2277        *__result = *__first;
2278        while (++__first != __last)
2279            if (!__pred(*__result, *__first))
2280                *++__result = *__first;
2281        ++__result;
2282    }
2283    return __result;
2284}
2285
2286template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2287inline _LIBCPP_INLINE_VISIBILITY
2288_OutputIterator
2289unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2290{
2291    return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2292                              (__first, __last, __result, __pred,
2293                               typename iterator_traits<_InputIterator>::iterator_category(),
2294                               typename iterator_traits<_OutputIterator>::iterator_category());
2295}
2296
2297template <class _InputIterator, class _OutputIterator>
2298inline _LIBCPP_INLINE_VISIBILITY
2299_OutputIterator
2300unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2301{
2302    typedef typename iterator_traits<_InputIterator>::value_type __v;
2303    return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2304}
2305
2306// reverse
2307
2308template <class _BidirectionalIterator>
2309inline _LIBCPP_INLINE_VISIBILITY
2310void
2311__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2312{
2313    while (__first != __last)
2314    {
2315        if (__first == --__last)
2316            break;
2317        swap(*__first, *__last);
2318        ++__first;
2319    }
2320}
2321
2322template <class _RandomAccessIterator>
2323inline _LIBCPP_INLINE_VISIBILITY
2324void
2325__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2326{
2327    if (__first != __last)
2328        for (; __first < --__last; ++__first)
2329            swap(*__first, *__last);
2330}
2331
2332template <class _BidirectionalIterator>
2333inline _LIBCPP_INLINE_VISIBILITY
2334void
2335reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2336{
2337    _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2338}
2339
2340// reverse_copy
2341
2342template <class _BidirectionalIterator, class _OutputIterator>
2343inline _LIBCPP_INLINE_VISIBILITY
2344_OutputIterator
2345reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2346{
2347    for (; __first != __last; ++__result)
2348        *__result = *--__last;
2349    return __result;
2350}
2351
2352// rotate
2353
2354template <class _ForwardIterator>
2355_ForwardIterator
2356__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2357{
2358    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2359    value_type __tmp = _VSTD::move(*__first);
2360    _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2361    *__lm1 = _VSTD::move(__tmp);
2362    return __lm1;
2363}
2364
2365template <class _BidirectionalIterator>
2366_BidirectionalIterator
2367__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2368{
2369    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2370    _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2371    value_type __tmp = _VSTD::move(*__lm1);
2372    _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2373    *__first = _VSTD::move(__tmp);
2374    return __fp1;
2375}
2376
2377template <class _ForwardIterator>
2378_ForwardIterator
2379__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2380{
2381    _ForwardIterator __i = __middle;
2382    while (true)
2383    {
2384        swap(*__first, *__i);
2385        ++__first;
2386        if (++__i == __last)
2387            break;
2388        if (__first == __middle)
2389            __middle = __i;
2390    }
2391    _ForwardIterator __r = __first;
2392    if (__first != __middle)
2393    {
2394        __i = __middle;
2395        while (true)
2396        {
2397            swap(*__first, *__i);
2398            ++__first;
2399            if (++__i == __last)
2400            {
2401                if (__first == __middle)
2402                    break;
2403                __i = __middle;
2404            }
2405            else if (__first == __middle)
2406                __middle = __i;
2407        }
2408    }
2409    return __r;
2410}
2411
2412template<typename _Integral>
2413inline _LIBCPP_INLINE_VISIBILITY
2414_Integral
2415__gcd(_Integral __x, _Integral __y)
2416{
2417    do
2418    {
2419        _Integral __t = __x % __y;
2420        __x = __y;
2421        __y = __t;
2422    } while (__y);
2423    return __x;
2424}
2425
2426template<typename _RandomAccessIterator>
2427_RandomAccessIterator
2428__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2429{
2430    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2431    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2432
2433    const difference_type __m1 = __middle - __first;
2434    const difference_type __m2 = __last - __middle;
2435    if (__m1 == __m2)
2436    {
2437        _VSTD::swap_ranges(__first, __middle, __middle);
2438        return __middle;
2439    }
2440    const difference_type __g = _VSTD::__gcd(__m1, __m2);
2441    for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2442    {
2443        value_type __t(_VSTD::move(*--__p));
2444        _RandomAccessIterator __p1 = __p;
2445        _RandomAccessIterator __p2 = __p1 + __m1;
2446        do
2447        {
2448            *__p1 = _VSTD::move(*__p2);
2449            __p1 = __p2;
2450            const difference_type __d = __last - __p2;
2451            if (__m1 < __d)
2452                __p2 += __m1;
2453            else
2454                __p2 = __first + (__m1 - __d);
2455        } while (__p2 != __p);
2456        *__p1 = _VSTD::move(__t);
2457    }
2458    return __first + __m2;
2459}
2460
2461template <class _ForwardIterator>
2462inline _LIBCPP_INLINE_VISIBILITY
2463_ForwardIterator
2464__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2465         _VSTD::forward_iterator_tag)
2466{
2467    typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2468    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2469    {
2470        if (_VSTD::next(__first) == __middle)
2471            return _VSTD::__rotate_left(__first, __last);
2472    }
2473    return _VSTD::__rotate_forward(__first, __middle, __last);
2474}
2475
2476template <class _BidirectionalIterator>
2477inline _LIBCPP_INLINE_VISIBILITY
2478_BidirectionalIterator
2479__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2480         _VSTD::bidirectional_iterator_tag)
2481{
2482    typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2483    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2484    {
2485        if (_VSTD::next(__first) == __middle)
2486            return _VSTD::__rotate_left(__first, __last);
2487        if (_VSTD::next(__middle) == __last)
2488            return _VSTD::__rotate_right(__first, __last);
2489    }
2490    return _VSTD::__rotate_forward(__first, __middle, __last);
2491}
2492
2493template <class _RandomAccessIterator>
2494inline _LIBCPP_INLINE_VISIBILITY
2495_RandomAccessIterator
2496__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2497         _VSTD::random_access_iterator_tag)
2498{
2499    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2500    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2501    {
2502        if (_VSTD::next(__first) == __middle)
2503            return _VSTD::__rotate_left(__first, __last);
2504        if (_VSTD::next(__middle) == __last)
2505            return _VSTD::__rotate_right(__first, __last);
2506        return _VSTD::__rotate_gcd(__first, __middle, __last);
2507    }
2508    return _VSTD::__rotate_forward(__first, __middle, __last);
2509}
2510
2511template <class _ForwardIterator>
2512inline _LIBCPP_INLINE_VISIBILITY
2513_ForwardIterator
2514rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2515{
2516    if (__first == __middle)
2517        return __last;
2518    if (__middle == __last)
2519        return __first;
2520    return _VSTD::__rotate(__first, __middle, __last,
2521                           typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2522}
2523
2524// rotate_copy
2525
2526template <class _ForwardIterator, class _OutputIterator>
2527inline _LIBCPP_INLINE_VISIBILITY
2528_OutputIterator
2529rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2530{
2531    return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2532}
2533
2534// min_element
2535
2536template <class _ForwardIterator, class _Compare>
2537inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2538_ForwardIterator
2539__min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2540{
2541    if (__first != __last)
2542    {
2543        _ForwardIterator __i = __first;
2544        while (++__i != __last)
2545            if (__comp(*__i, *__first))
2546                __first = __i;
2547    }
2548    return __first;
2549}
2550
2551template <class _ForwardIterator, class _Compare>
2552inline _LIBCPP_INLINE_VISIBILITY
2553_ForwardIterator
2554min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2555{
2556    return __min_element(__first, __last, __comp);
2557}
2558
2559template <class _ForwardIterator>
2560inline _LIBCPP_INLINE_VISIBILITY
2561_ForwardIterator
2562min_element(_ForwardIterator __first, _ForwardIterator __last)
2563{
2564    return __min_element(__first, __last,
2565              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2566}
2567
2568// min
2569
2570template <class _Tp, class _Compare>
2571inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2572const _Tp&
2573min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2574{
2575    return __comp(__b, __a) ? __b : __a;
2576}
2577
2578template <class _Tp>
2579inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2580const _Tp&
2581min(const _Tp& __a, const _Tp& __b)
2582{
2583    return _VSTD::min(__a, __b, __less<_Tp>());
2584}
2585
2586#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2587
2588template<class _Tp, class _Compare>
2589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2590_Tp
2591min(initializer_list<_Tp> __t, _Compare __comp)
2592{
2593    return *__min_element(__t.begin(), __t.end(), __comp);
2594}
2595
2596template<class _Tp>
2597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2598_Tp
2599min(initializer_list<_Tp> __t)
2600{
2601    return *__min_element(__t.begin(), __t.end(), __less<_Tp>());
2602}
2603
2604#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2605
2606// max_element
2607
2608template <class _ForwardIterator, class _Compare>
2609inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2610_ForwardIterator
2611__max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2612{
2613    if (__first != __last)
2614    {
2615        _ForwardIterator __i = __first;
2616        while (++__i != __last)
2617            if (__comp(*__first, *__i))
2618                __first = __i;
2619    }
2620    return __first;
2621}
2622
2623
2624template <class _ForwardIterator, class _Compare>
2625inline _LIBCPP_INLINE_VISIBILITY
2626_ForwardIterator
2627max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2628{
2629    return __max_element(__first, __last, __comp);
2630}
2631
2632template <class _ForwardIterator>
2633inline _LIBCPP_INLINE_VISIBILITY
2634_ForwardIterator
2635max_element(_ForwardIterator __first, _ForwardIterator __last)
2636{
2637    return __max_element(__first, __last,
2638              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2639}
2640
2641// max
2642
2643template <class _Tp, class _Compare>
2644inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2645const _Tp&
2646max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2647{
2648    return __comp(__a, __b) ? __b : __a;
2649}
2650
2651template <class _Tp>
2652inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2653const _Tp&
2654max(const _Tp& __a, const _Tp& __b)
2655{
2656    return _VSTD::max(__a, __b, __less<_Tp>());
2657}
2658
2659#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2660
2661template<class _Tp, class _Compare>
2662inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2663_Tp
2664max(initializer_list<_Tp> __t, _Compare __comp)
2665{
2666    return *__max_element(__t.begin(), __t.end(), __comp);
2667}
2668
2669template<class _Tp>
2670inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2671_Tp
2672max(initializer_list<_Tp> __t)
2673{
2674    return *__max_element(__t.begin(), __t.end(), __less<_Tp>());
2675}
2676
2677#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2678
2679// minmax_element
2680
2681template <class _ForwardIterator, class _Compare>
2682std::pair<_ForwardIterator, _ForwardIterator>
2683minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2684{
2685  std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2686  if (__first != __last)
2687  {
2688      if (++__first != __last)
2689      {
2690          if (__comp(*__first, *__result.first))
2691              __result.first = __first;
2692          else
2693              __result.second = __first;
2694          while (++__first != __last)
2695          {
2696              _ForwardIterator __i = __first;
2697              if (++__first == __last)
2698              {
2699                  if (__comp(*__i, *__result.first))
2700                      __result.first = __i;
2701                  else if (!__comp(*__i, *__result.second))
2702                      __result.second = __i;
2703                  break;
2704              }
2705              else
2706              {
2707                  if (__comp(*__first, *__i))
2708                  {
2709                      if (__comp(*__first, *__result.first))
2710                          __result.first = __first;
2711                      if (!__comp(*__i, *__result.second))
2712                          __result.second = __i;
2713                  }
2714                  else
2715                  {
2716                      if (__comp(*__i, *__result.first))
2717                          __result.first = __i;
2718                      if (!__comp(*__first, *__result.second))
2719                          __result.second = __first;
2720                  }
2721              }
2722          }
2723      }
2724  }
2725  return __result;
2726}
2727
2728template <class _ForwardIterator>
2729inline _LIBCPP_INLINE_VISIBILITY
2730std::pair<_ForwardIterator, _ForwardIterator>
2731minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2732{
2733    return _VSTD::minmax_element(__first, __last,
2734              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2735}
2736
2737// minmax
2738
2739template<class _Tp, class _Compare>
2740inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2741pair<const _Tp&, const _Tp&>
2742minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2743{
2744    return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2745                              pair<const _Tp&, const _Tp&>(__a, __b);
2746}
2747
2748template<class _Tp>
2749inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2750pair<const _Tp&, const _Tp&>
2751minmax(const _Tp& __a, const _Tp& __b)
2752{
2753    return _VSTD::minmax(__a, __b, __less<_Tp>());
2754}
2755
2756#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2757
2758template<class _Tp, class _Compare>
2759inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2760pair<_Tp, _Tp>
2761minmax(initializer_list<_Tp> __t, _Compare __comp)
2762{
2763    typedef typename initializer_list<_Tp>::const_iterator _Iter;
2764    _Iter __first = __t.begin();
2765    _Iter __last  = __t.end();
2766    std::pair<_Tp, _Tp> __result ( *__first, *__first );
2767
2768    ++__first;
2769    if (__t.size() % 2 == 0)
2770    {
2771        if (__comp(*__first,  __result.first))
2772            __result.first  = *__first;
2773        else
2774            __result.second = *__first;
2775        ++__first;
2776    }
2777
2778    while (__first != __last)
2779    {
2780        _Tp __prev = *__first++;
2781        if (__comp(__prev, *__first)) {
2782            if (__comp(__prev, __result.first))    __result.first  = __prev;
2783            if (__comp(__result.second, *__first)) __result.second = *__first;
2784            }
2785        else {
2786            if (__comp(*__first, __result.first)) __result.first  = *__first;
2787            if (__comp(__result.second, __prev))  __result.second = __prev;
2788            }
2789
2790        __first++;
2791    }
2792    return __result;
2793}
2794
2795template<class _Tp>
2796inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2797pair<_Tp, _Tp>
2798minmax(initializer_list<_Tp> __t)
2799{
2800    return _VSTD::minmax(__t, __less<_Tp>());
2801}
2802
2803#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2804
2805// random_shuffle
2806
2807// __independent_bits_engine
2808
2809template <unsigned long long _Xp, size_t _Rp>
2810struct __log2_imp
2811{
2812    static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2813                                           : __log2_imp<_Xp, _Rp - 1>::value;
2814};
2815
2816template <unsigned long long _Xp>
2817struct __log2_imp<_Xp, 0>
2818{
2819    static const size_t value = 0;
2820};
2821
2822template <size_t _Rp>
2823struct __log2_imp<0, _Rp>
2824{
2825    static const size_t value = _Rp + 1;
2826};
2827
2828template <class _UI, _UI _Xp>
2829struct __log2
2830{
2831    static const size_t value = __log2_imp<_Xp,
2832                                         sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2833};
2834
2835template<class _Engine, class _UIntType>
2836class __independent_bits_engine
2837{
2838public:
2839    // types
2840    typedef _UIntType result_type;
2841
2842private:
2843    typedef typename _Engine::result_type _Engine_result_type;
2844    typedef typename conditional
2845        <
2846            sizeof(_Engine_result_type) <= sizeof(result_type),
2847                result_type,
2848                _Engine_result_type
2849        >::type _Working_result_type;
2850
2851    _Engine& __e_;
2852    size_t __w_;
2853    size_t __w0_;
2854    size_t __n_;
2855    size_t __n0_;
2856    _Working_result_type __y0_;
2857    _Working_result_type __y1_;
2858    _Engine_result_type __mask0_;
2859    _Engine_result_type __mask1_;
2860
2861#ifdef _LIBCPP_HAS_NO_CONSTEXPR
2862    static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2863                                          + _Working_result_type(1);
2864#else
2865    static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2866                                                      + _Working_result_type(1);
2867#endif
2868    static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2869    static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2870    static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2871
2872public:
2873    // constructors and seeding functions
2874    __independent_bits_engine(_Engine& __e, size_t __w);
2875
2876    // generating functions
2877    result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2878
2879private:
2880    result_type __eval(false_type);
2881    result_type __eval(true_type);
2882};
2883
2884template<class _Engine, class _UIntType>
2885__independent_bits_engine<_Engine, _UIntType>
2886    ::__independent_bits_engine(_Engine& __e, size_t __w)
2887        : __e_(__e),
2888          __w_(__w)
2889{
2890    __n_ = __w_ / __m + (__w_ % __m != 0);
2891    __w0_ = __w_ / __n_;
2892    if (_Rp == 0)
2893        __y0_ = _Rp;
2894    else if (__w0_ < _WDt)
2895        __y0_ = (_Rp >> __w0_) << __w0_;
2896    else
2897        __y0_ = 0;
2898    if (_Rp - __y0_ > __y0_ / __n_)
2899    {
2900        ++__n_;
2901        __w0_ = __w_ / __n_;
2902        if (__w0_ < _WDt)
2903            __y0_ = (_Rp >> __w0_) << __w0_;
2904        else
2905            __y0_ = 0;
2906    }
2907    __n0_ = __n_ - __w_ % __n_;
2908    if (__w0_ < _WDt - 1)
2909        __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2910    else
2911        __y1_ = 0;
2912    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2913                          _Engine_result_type(0);
2914    __mask1_ = __w0_ < _EDt - 1 ?
2915                               _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2916                               _Engine_result_type(~0);
2917}
2918
2919template<class _Engine, class _UIntType>
2920inline
2921_UIntType
2922__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2923{
2924    return static_cast<result_type>(__e_() & __mask0_);
2925}
2926
2927template<class _Engine, class _UIntType>
2928_UIntType
2929__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2930{
2931    result_type _Sp = 0;
2932    for (size_t __k = 0; __k < __n0_; ++__k)
2933    {
2934        _Engine_result_type __u;
2935        do
2936        {
2937            __u = __e_() - _Engine::min();
2938        } while (__u >= __y0_);
2939        if (__w0_ < _WDt)
2940            _Sp <<= __w0_;
2941        else
2942            _Sp = 0;
2943        _Sp += __u & __mask0_;
2944    }
2945    for (size_t __k = __n0_; __k < __n_; ++__k)
2946    {
2947        _Engine_result_type __u;
2948        do
2949        {
2950            __u = __e_() - _Engine::min();
2951        } while (__u >= __y1_);
2952        if (__w0_ < _WDt - 1)
2953            _Sp <<= __w0_ + 1;
2954        else
2955            _Sp = 0;
2956        _Sp += __u & __mask1_;
2957    }
2958    return _Sp;
2959}
2960
2961// uniform_int_distribution
2962
2963template<class _IntType = int>
2964class uniform_int_distribution
2965{
2966public:
2967    // types
2968    typedef _IntType result_type;
2969
2970    class param_type
2971    {
2972        result_type __a_;
2973        result_type __b_;
2974    public:
2975        typedef uniform_int_distribution distribution_type;
2976
2977        explicit param_type(result_type __a = 0,
2978                            result_type __b = numeric_limits<result_type>::max())
2979            : __a_(__a), __b_(__b) {}
2980
2981        result_type a() const {return __a_;}
2982        result_type b() const {return __b_;}
2983
2984        friend bool operator==(const param_type& __x, const param_type& __y)
2985            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2986        friend bool operator!=(const param_type& __x, const param_type& __y)
2987            {return !(__x == __y);}
2988    };
2989
2990private:
2991    param_type __p_;
2992
2993public:
2994    // constructors and reset functions
2995    explicit uniform_int_distribution(result_type __a = 0,
2996                                      result_type __b = numeric_limits<result_type>::max())
2997        : __p_(param_type(__a, __b)) {}
2998    explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2999    void reset() {}
3000
3001    // generating functions
3002    template<class _URNG> result_type operator()(_URNG& __g)
3003        {return (*this)(__g, __p_);}
3004    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3005
3006    // property functions
3007    result_type a() const {return __p_.a();}
3008    result_type b() const {return __p_.b();}
3009
3010    param_type param() const {return __p_;}
3011    void param(const param_type& __p) {__p_ = __p;}
3012
3013    result_type min() const {return a();}
3014    result_type max() const {return b();}
3015
3016    friend bool operator==(const uniform_int_distribution& __x,
3017                           const uniform_int_distribution& __y)
3018        {return __x.__p_ == __y.__p_;}
3019    friend bool operator!=(const uniform_int_distribution& __x,
3020                           const uniform_int_distribution& __y)
3021            {return !(__x == __y);}
3022};
3023
3024template<class _IntType>
3025template<class _URNG>
3026typename uniform_int_distribution<_IntType>::result_type
3027uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3028{
3029    typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3030                                            uint32_t, uint64_t>::type _UIntType;
3031    const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3032    if (_Rp == 1)
3033        return __p.a();
3034    const size_t _Dt = numeric_limits<_UIntType>::digits;
3035    typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3036    if (_Rp == 0)
3037        return static_cast<result_type>(_Eng(__g, _Dt)());
3038    size_t __w = _Dt - __clz(_Rp) - 1;
3039    if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
3040        ++__w;
3041    _Eng __e(__g, __w);
3042    _UIntType __u;
3043    do
3044    {
3045        __u = __e();
3046    } while (__u >= _Rp);
3047    return static_cast<result_type>(__u + __p.a());
3048}
3049
3050class _LIBCPP_TYPE_VIS __rs_default;
3051
3052_LIBCPP_FUNC_VIS __rs_default __rs_get();
3053
3054class _LIBCPP_TYPE_VIS __rs_default
3055{
3056    static unsigned __c_;
3057
3058    __rs_default();
3059public:
3060    typedef uint_fast32_t result_type;
3061
3062    static const result_type _Min = 0;
3063    static const result_type _Max = 0xFFFFFFFF;
3064
3065    __rs_default(const __rs_default&);
3066    ~__rs_default();
3067
3068    result_type operator()();
3069
3070    static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3071    static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3072
3073    friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3074};
3075
3076_LIBCPP_FUNC_VIS __rs_default __rs_get();
3077
3078template <class _RandomAccessIterator>
3079void
3080random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3081{
3082    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3083    typedef uniform_int_distribution<ptrdiff_t> _Dp;
3084    typedef typename _Dp::param_type _Pp;
3085    difference_type __d = __last - __first;
3086    if (__d > 1)
3087    {
3088        _Dp __uid;
3089        __rs_default __g = __rs_get();
3090        for (--__last, --__d; __first < __last; ++__first, --__d)
3091        {
3092            difference_type __i = __uid(__g, _Pp(0, __d));
3093            if (__i != difference_type(0))
3094                swap(*__first, *(__first + __i));
3095        }
3096    }
3097}
3098
3099template <class _RandomAccessIterator, class _RandomNumberGenerator>
3100void
3101random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3102#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3103               _RandomNumberGenerator&& __rand)
3104#else
3105               _RandomNumberGenerator& __rand)
3106#endif
3107{
3108    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3109    difference_type __d = __last - __first;
3110    if (__d > 1)
3111    {
3112        for (--__last; __first < __last; ++__first, --__d)
3113        {
3114            difference_type __i = __rand(__d);
3115            swap(*__first, *(__first + __i));
3116        }
3117    }
3118}
3119
3120template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3121    void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3122#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3123                 _UniformRandomNumberGenerator&& __g)
3124#else
3125                 _UniformRandomNumberGenerator& __g)
3126#endif
3127{
3128    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3129    typedef uniform_int_distribution<ptrdiff_t> _Dp;
3130    typedef typename _Dp::param_type _Pp;
3131    difference_type __d = __last - __first;
3132    if (__d > 1)
3133    {
3134        _Dp __uid;
3135        for (--__last, --__d; __first < __last; ++__first, --__d)
3136        {
3137            difference_type __i = __uid(__g, _Pp(0, __d));
3138            if (__i != difference_type(0))
3139                swap(*__first, *(__first + __i));
3140        }
3141    }
3142}
3143
3144template <class _InputIterator, class _Predicate>
3145bool
3146is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3147{
3148    for (; __first != __last; ++__first)
3149        if (!__pred(*__first))
3150            break;
3151    if ( __first == __last )
3152        return true;
3153    ++__first;
3154    for (; __first != __last; ++__first)
3155        if (__pred(*__first))
3156            return false;
3157    return true;
3158}
3159
3160// partition
3161
3162template <class _Predicate, class _ForwardIterator>
3163_ForwardIterator
3164__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3165{
3166    while (true)
3167    {
3168        if (__first == __last)
3169            return __first;
3170        if (!__pred(*__first))
3171            break;
3172        ++__first;
3173    }
3174    for (_ForwardIterator __p = __first; ++__p != __last;)
3175    {
3176        if (__pred(*__p))
3177        {
3178            swap(*__first, *__p);
3179            ++__first;
3180        }
3181    }
3182    return __first;
3183}
3184
3185template <class _Predicate, class _BidirectionalIterator>
3186_BidirectionalIterator
3187__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3188            bidirectional_iterator_tag)
3189{
3190    while (true)
3191    {
3192        while (true)
3193        {
3194            if (__first == __last)
3195                return __first;
3196            if (!__pred(*__first))
3197                break;
3198            ++__first;
3199        }
3200        do
3201        {
3202            if (__first == --__last)
3203                return __first;
3204        } while (!__pred(*__last));
3205        swap(*__first, *__last);
3206        ++__first;
3207    }
3208}
3209
3210template <class _ForwardIterator, class _Predicate>
3211inline _LIBCPP_INLINE_VISIBILITY
3212_ForwardIterator
3213partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3214{
3215    return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3216                            (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3217}
3218
3219// partition_copy
3220
3221template <class _InputIterator, class _OutputIterator1,
3222          class _OutputIterator2, class _Predicate>
3223pair<_OutputIterator1, _OutputIterator2>
3224partition_copy(_InputIterator __first, _InputIterator __last,
3225               _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3226               _Predicate __pred)
3227{
3228    for (; __first != __last; ++__first)
3229    {
3230        if (__pred(*__first))
3231        {
3232            *__out_true = *__first;
3233            ++__out_true;
3234        }
3235        else
3236        {
3237            *__out_false = *__first;
3238            ++__out_false;
3239        }
3240    }
3241    return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3242}
3243
3244// partition_point
3245
3246template<class _ForwardIterator, class _Predicate>
3247_ForwardIterator
3248partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3249{
3250    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3251    difference_type __len = _VSTD::distance(__first, __last);
3252    while (__len != 0)
3253    {
3254        difference_type __l2 = __len / 2;
3255        _ForwardIterator __m = __first;
3256        _VSTD::advance(__m, __l2);
3257        if (__pred(*__m))
3258        {
3259            __first = ++__m;
3260            __len -= __l2 + 1;
3261        }
3262        else
3263            __len = __l2;
3264    }
3265    return __first;
3266}
3267
3268// stable_partition
3269
3270template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3271_ForwardIterator
3272__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3273                   _Distance __len, _Pair __p, forward_iterator_tag __fit)
3274{
3275    // *__first is known to be false
3276    // __len >= 1
3277    if (__len == 1)
3278        return __first;
3279    if (__len == 2)
3280    {
3281        _ForwardIterator __m = __first;
3282        if (__pred(*++__m))
3283        {
3284            swap(*__first, *__m);
3285            return __m;
3286        }
3287        return __first;
3288    }
3289    if (__len <= __p.second)
3290    {   // The buffer is big enough to use
3291        typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3292        __destruct_n __d(0);
3293        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3294        // Move the falses into the temporary buffer, and the trues to the front of the line
3295        // Update __first to always point to the end of the trues
3296        value_type* __t = __p.first;
3297        ::new(__t) value_type(_VSTD::move(*__first));
3298        __d.__incr((value_type*)0);
3299        ++__t;
3300        _ForwardIterator __i = __first;
3301        while (++__i != __last)
3302        {
3303            if (__pred(*__i))
3304            {
3305                *__first = _VSTD::move(*__i);
3306                ++__first;
3307            }
3308            else
3309            {
3310                ::new(__t) value_type(_VSTD::move(*__i));
3311                __d.__incr((value_type*)0);
3312                ++__t;
3313            }
3314        }
3315        // All trues now at start of range, all falses in buffer
3316        // Move falses back into range, but don't mess up __first which points to first false
3317        __i = __first;
3318        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3319            *__i = _VSTD::move(*__t2);
3320        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3321        return __first;
3322    }
3323    // Else not enough buffer, do in place
3324    // __len >= 3
3325    _ForwardIterator __m = __first;
3326    _Distance __len2 = __len / 2;  // __len2 >= 2
3327    _VSTD::advance(__m, __len2);
3328    // recurse on [__first, __m), *__first know to be false
3329    // F?????????????????
3330    // f       m         l
3331    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3332    _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3333    // TTTFFFFF??????????
3334    // f  ff   m         l
3335    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3336    _ForwardIterator __m1 = __m;
3337    _ForwardIterator __second_false = __last;
3338    _Distance __len_half = __len - __len2;
3339    while (__pred(*__m1))
3340    {
3341        if (++__m1 == __last)
3342            goto __second_half_done;
3343        --__len_half;
3344    }
3345    // TTTFFFFFTTTF??????
3346    // f  ff   m  m1     l
3347    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3348__second_half_done:
3349    // TTTFFFFFTTTTTFFFFF
3350    // f  ff   m    sf   l
3351    return _VSTD::rotate(__first_false, __m, __second_false);
3352    // TTTTTTTTFFFFFFFFFF
3353    //         |
3354}
3355
3356struct __return_temporary_buffer
3357{
3358    template <class _Tp>
3359    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3360};
3361
3362template <class _Predicate, class _ForwardIterator>
3363_ForwardIterator
3364__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3365                   forward_iterator_tag)
3366{
3367    const unsigned __alloc_limit = 3;  // might want to make this a function of trivial assignment
3368    // Either prove all true and return __first or point to first false
3369    while (true)
3370    {
3371        if (__first == __last)
3372            return __first;
3373        if (!__pred(*__first))
3374            break;
3375        ++__first;
3376    }
3377    // We now have a reduced range [__first, __last)
3378    // *__first is known to be false
3379    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3380    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3381    difference_type __len = _VSTD::distance(__first, __last);
3382    pair<value_type*, ptrdiff_t> __p(0, 0);
3383    unique_ptr<value_type, __return_temporary_buffer> __h;
3384    if (__len >= __alloc_limit)
3385    {
3386        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3387        __h.reset(__p.first);
3388    }
3389    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3390                             (__first, __last, __pred, __len, __p, forward_iterator_tag());
3391}
3392
3393template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3394_BidirectionalIterator
3395__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3396                   _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3397{
3398    // *__first is known to be false
3399    // *__last is known to be true
3400    // __len >= 2
3401    if (__len == 2)
3402    {
3403        swap(*__first, *__last);
3404        return __last;
3405    }
3406    if (__len == 3)
3407    {
3408        _BidirectionalIterator __m = __first;
3409        if (__pred(*++__m))
3410        {
3411            swap(*__first, *__m);
3412            swap(*__m, *__last);
3413            return __last;
3414        }
3415        swap(*__m, *__last);
3416        swap(*__first, *__m);
3417        return __m;
3418    }
3419    if (__len <= __p.second)
3420    {   // The buffer is big enough to use
3421        typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3422        __destruct_n __d(0);
3423        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3424        // Move the falses into the temporary buffer, and the trues to the front of the line
3425        // Update __first to always point to the end of the trues
3426        value_type* __t = __p.first;
3427        ::new(__t) value_type(_VSTD::move(*__first));
3428        __d.__incr((value_type*)0);
3429        ++__t;
3430        _BidirectionalIterator __i = __first;
3431        while (++__i != __last)
3432        {
3433            if (__pred(*__i))
3434            {
3435                *__first = _VSTD::move(*__i);
3436                ++__first;
3437            }
3438            else
3439            {
3440                ::new(__t) value_type(_VSTD::move(*__i));
3441                __d.__incr((value_type*)0);
3442                ++__t;
3443            }
3444        }
3445        // move *__last, known to be true
3446        *__first = _VSTD::move(*__i);
3447        __i = ++__first;
3448        // All trues now at start of range, all falses in buffer
3449        // Move falses back into range, but don't mess up __first which points to first false
3450        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3451            *__i = _VSTD::move(*__t2);
3452        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3453        return __first;
3454    }
3455    // Else not enough buffer, do in place
3456    // __len >= 4
3457    _BidirectionalIterator __m = __first;
3458    _Distance __len2 = __len / 2;  // __len2 >= 2
3459    _VSTD::advance(__m, __len2);
3460    // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3461    // F????????????????T
3462    // f       m        l
3463    _BidirectionalIterator __m1 = __m;
3464    _BidirectionalIterator __first_false = __first;
3465    _Distance __len_half = __len2;
3466    while (!__pred(*--__m1))
3467    {
3468        if (__m1 == __first)
3469            goto __first_half_done;
3470        --__len_half;
3471    }
3472    // F???TFFF?????????T
3473    // f   m1  m        l
3474    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3475    __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3476__first_half_done:
3477    // TTTFFFFF?????????T
3478    // f  ff   m        l
3479    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3480    __m1 = __m;
3481    _BidirectionalIterator __second_false = __last;
3482    ++__second_false;
3483    __len_half = __len - __len2;
3484    while (__pred(*__m1))
3485    {
3486        if (++__m1 == __last)
3487            goto __second_half_done;
3488        --__len_half;
3489    }
3490    // TTTFFFFFTTTF?????T
3491    // f  ff   m  m1    l
3492    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3493__second_half_done:
3494    // TTTFFFFFTTTTTFFFFF
3495    // f  ff   m    sf  l
3496    return _VSTD::rotate(__first_false, __m, __second_false);
3497    // TTTTTTTTFFFFFFFFFF
3498    //         |
3499}
3500
3501template <class _Predicate, class _BidirectionalIterator>
3502_BidirectionalIterator
3503__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3504                   bidirectional_iterator_tag)
3505{
3506    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3507    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3508    const difference_type __alloc_limit = 4;  // might want to make this a function of trivial assignment
3509    // Either prove all true and return __first or point to first false
3510    while (true)
3511    {
3512        if (__first == __last)
3513            return __first;
3514        if (!__pred(*__first))
3515            break;
3516        ++__first;
3517    }
3518    // __first points to first false, everything prior to __first is already set.
3519    // Either prove [__first, __last) is all false and return __first, or point __last to last true
3520    do
3521    {
3522        if (__first == --__last)
3523            return __first;
3524    } while (!__pred(*__last));
3525    // We now have a reduced range [__first, __last]
3526    // *__first is known to be false
3527    // *__last is known to be true
3528    // __len >= 2
3529    difference_type __len = _VSTD::distance(__first, __last) + 1;
3530    pair<value_type*, ptrdiff_t> __p(0, 0);
3531    unique_ptr<value_type, __return_temporary_buffer> __h;
3532    if (__len >= __alloc_limit)
3533    {
3534        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3535        __h.reset(__p.first);
3536    }
3537    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3538                             (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3539}
3540
3541template <class _ForwardIterator, class _Predicate>
3542inline _LIBCPP_INLINE_VISIBILITY
3543_ForwardIterator
3544stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3545{
3546    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3547                             (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3548}
3549
3550// is_sorted_until
3551
3552template <class _ForwardIterator, class _Compare>
3553_ForwardIterator
3554is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3555{
3556    if (__first != __last)
3557    {
3558        _ForwardIterator __i = __first;
3559        while (++__i != __last)
3560        {
3561            if (__comp(*__i, *__first))
3562                return __i;
3563            __first = __i;
3564        }
3565    }
3566    return __last;
3567}
3568
3569template<class _ForwardIterator>
3570inline _LIBCPP_INLINE_VISIBILITY
3571_ForwardIterator
3572is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3573{
3574    return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3575}
3576
3577// is_sorted
3578
3579template <class _ForwardIterator, class _Compare>
3580inline _LIBCPP_INLINE_VISIBILITY
3581bool
3582is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3583{
3584    return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3585}
3586
3587template<class _ForwardIterator>
3588inline _LIBCPP_INLINE_VISIBILITY
3589bool
3590is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3591{
3592    return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3593}
3594
3595// sort
3596
3597// stable, 2-3 compares, 0-2 swaps
3598
3599template <class _Compare, class _ForwardIterator>
3600unsigned
3601__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3602{
3603    unsigned __r = 0;
3604    if (!__c(*__y, *__x))          // if x <= y
3605    {
3606        if (!__c(*__z, *__y))      // if y <= z
3607            return __r;            // x <= y && y <= z
3608                                   // x <= y && y > z
3609        swap(*__y, *__z);          // x <= z && y < z
3610        __r = 1;
3611        if (__c(*__y, *__x))       // if x > y
3612        {
3613            swap(*__x, *__y);      // x < y && y <= z
3614            __r = 2;
3615        }
3616        return __r;                // x <= y && y < z
3617    }
3618    if (__c(*__z, *__y))           // x > y, if y > z
3619    {
3620        swap(*__x, *__z);          // x < y && y < z
3621        __r = 1;
3622        return __r;
3623    }
3624    swap(*__x, *__y);              // x > y && y <= z
3625    __r = 1;                       // x < y && x <= z
3626    if (__c(*__z, *__y))           // if y > z
3627    {
3628        swap(*__y, *__z);          // x <= y && y < z
3629        __r = 2;
3630    }
3631    return __r;
3632}                                  // x <= y && y <= z
3633
3634// stable, 3-6 compares, 0-5 swaps
3635
3636template <class _Compare, class _ForwardIterator>
3637unsigned
3638__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3639            _ForwardIterator __x4, _Compare __c)
3640{
3641    unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3642    if (__c(*__x4, *__x3))
3643    {
3644        swap(*__x3, *__x4);
3645        ++__r;
3646        if (__c(*__x3, *__x2))
3647        {
3648            swap(*__x2, *__x3);
3649            ++__r;
3650            if (__c(*__x2, *__x1))
3651            {
3652                swap(*__x1, *__x2);
3653                ++__r;
3654            }
3655        }
3656    }
3657    return __r;
3658}
3659
3660// stable, 4-10 compares, 0-9 swaps
3661
3662template <class _Compare, class _ForwardIterator>
3663unsigned
3664__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3665            _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3666{
3667    unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3668    if (__c(*__x5, *__x4))
3669    {
3670        swap(*__x4, *__x5);
3671        ++__r;
3672        if (__c(*__x4, *__x3))
3673        {
3674            swap(*__x3, *__x4);
3675            ++__r;
3676            if (__c(*__x3, *__x2))
3677            {
3678                swap(*__x2, *__x3);
3679                ++__r;
3680                if (__c(*__x2, *__x1))
3681                {
3682                    swap(*__x1, *__x2);
3683                    ++__r;
3684                }
3685            }
3686        }
3687    }
3688    return __r;
3689}
3690
3691// Assumes size > 0
3692template <class _Compare, class _BirdirectionalIterator>
3693void
3694__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3695{
3696    _BirdirectionalIterator __lm1 = __last;
3697    for (--__lm1; __first != __lm1; ++__first)
3698    {
3699        _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3700                                                        typename add_lvalue_reference<_Compare>::type>
3701                                                       (__first, __last, __comp);
3702        if (__i != __first)
3703            swap(*__first, *__i);
3704    }
3705}
3706
3707template <class _Compare, class _BirdirectionalIterator>
3708void
3709__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3710{
3711    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3712    if (__first != __last)
3713    {
3714        _BirdirectionalIterator __i = __first;
3715        for (++__i; __i != __last; ++__i)
3716        {
3717            _BirdirectionalIterator __j = __i;
3718            value_type __t(_VSTD::move(*__j));
3719            for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t,  *--__k); --__j)
3720                *__j = _VSTD::move(*__k);
3721            *__j = _VSTD::move(__t);
3722        }
3723    }
3724}
3725
3726template <class _Compare, class _RandomAccessIterator>
3727void
3728__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3729{
3730    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3731    _RandomAccessIterator __j = __first+2;
3732    __sort3<_Compare>(__first, __first+1, __j, __comp);
3733    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3734    {
3735        if (__comp(*__i, *__j))
3736        {
3737            value_type __t(_VSTD::move(*__i));
3738            _RandomAccessIterator __k = __j;
3739            __j = __i;
3740            do
3741            {
3742                *__j = _VSTD::move(*__k);
3743                __j = __k;
3744            } while (__j != __first && __comp(__t, *--__k));
3745            *__j = _VSTD::move(__t);
3746        }
3747        __j = __i;
3748    }
3749}
3750
3751template <class _Compare, class _RandomAccessIterator>
3752bool
3753__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3754{
3755    switch (__last - __first)
3756    {
3757    case 0:
3758    case 1:
3759        return true;
3760    case 2:
3761        if (__comp(*--__last, *__first))
3762            swap(*__first, *__last);
3763        return true;
3764    case 3:
3765        _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3766        return true;
3767    case 4:
3768        _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3769        return true;
3770    case 5:
3771        _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3772        return true;
3773    }
3774    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3775    _RandomAccessIterator __j = __first+2;
3776    __sort3<_Compare>(__first, __first+1, __j, __comp);
3777    const unsigned __limit = 8;
3778    unsigned __count = 0;
3779    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3780    {
3781        if (__comp(*__i, *__j))
3782        {
3783            value_type __t(_VSTD::move(*__i));
3784            _RandomAccessIterator __k = __j;
3785            __j = __i;
3786            do
3787            {
3788                *__j = _VSTD::move(*__k);
3789                __j = __k;
3790            } while (__j != __first && __comp(__t, *--__k));
3791            *__j = _VSTD::move(__t);
3792            if (++__count == __limit)
3793                return ++__i == __last;
3794        }
3795        __j = __i;
3796    }
3797    return true;
3798}
3799
3800template <class _Compare, class _BirdirectionalIterator>
3801void
3802__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3803                      typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3804{
3805    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3806    if (__first1 != __last1)
3807    {
3808        __destruct_n __d(0);
3809        unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3810        value_type* __last2 = __first2;
3811        ::new(__last2) value_type(_VSTD::move(*__first1));
3812        __d.__incr((value_type*)0);
3813        for (++__last2; ++__first1 != __last1; ++__last2)
3814        {
3815            value_type* __j2 = __last2;
3816            value_type* __i2 = __j2;
3817            if (__comp(*__first1, *--__i2))
3818            {
3819                ::new(__j2) value_type(_VSTD::move(*__i2));
3820                __d.__incr((value_type*)0);
3821                for (--__j2; __i2 != __first2 && __comp(*__first1,  *--__i2); --__j2)
3822                    *__j2 = _VSTD::move(*__i2);
3823                *__j2 = _VSTD::move(*__first1);
3824            }
3825            else
3826            {
3827                ::new(__j2) value_type(_VSTD::move(*__first1));
3828                __d.__incr((value_type*)0);
3829            }
3830        }
3831        __h.release();
3832    }
3833}
3834
3835template <class _Compare, class _RandomAccessIterator>
3836void
3837__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3838{
3839    // _Compare is known to be a reference type
3840    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3841    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3842    const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3843                                    is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3844    while (true)
3845    {
3846    __restart:
3847        difference_type __len = __last - __first;
3848        switch (__len)
3849        {
3850        case 0:
3851        case 1:
3852            return;
3853        case 2:
3854            if (__comp(*--__last, *__first))
3855                swap(*__first, *__last);
3856            return;
3857        case 3:
3858            _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3859            return;
3860        case 4:
3861            _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3862            return;
3863        case 5:
3864            _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3865            return;
3866        }
3867        if (__len <= __limit)
3868        {
3869            _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3870            return;
3871        }
3872        // __len > 5
3873        _RandomAccessIterator __m = __first;
3874        _RandomAccessIterator __lm1 = __last;
3875        --__lm1;
3876        unsigned __n_swaps;
3877        {
3878        difference_type __delta;
3879        if (__len >= 1000)
3880        {
3881            __delta = __len/2;
3882            __m += __delta;
3883            __delta /= 2;
3884            __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3885        }
3886        else
3887        {
3888            __delta = __len/2;
3889            __m += __delta;
3890            __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3891        }
3892        }
3893        // *__m is median
3894        // partition [__first, __m) < *__m and *__m <= [__m, __last)
3895        // (this inhibits tossing elements equivalent to __m around unnecessarily)
3896        _RandomAccessIterator __i = __first;
3897        _RandomAccessIterator __j = __lm1;
3898        // j points beyond range to be tested, *__m is known to be <= *__lm1
3899        // The search going up is known to be guarded but the search coming down isn't.
3900        // Prime the downward search with a guard.
3901        if (!__comp(*__i, *__m))  // if *__first == *__m
3902        {
3903            // *__first == *__m, *__first doesn't go in first part
3904            // manually guard downward moving __j against __i
3905            while (true)
3906            {
3907                if (__i == --__j)
3908                {
3909                    // *__first == *__m, *__m <= all other elements
3910                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3911                    ++__i;  // __first + 1
3912                    __j = __last;
3913                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
3914                    {
3915                        while (true)
3916                        {
3917                            if (__i == __j)
3918                                return;  // [__first, __last) all equivalent elements
3919                            if (__comp(*__first, *__i))
3920                            {
3921                                swap(*__i, *__j);
3922                                ++__n_swaps;
3923                                ++__i;
3924                                break;
3925                            }
3926                            ++__i;
3927                        }
3928                    }
3929                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3930                    if (__i == __j)
3931                        return;
3932                    while (true)
3933                    {
3934                        while (!__comp(*__first, *__i))
3935                            ++__i;
3936                        while (__comp(*__first, *--__j))
3937                            ;
3938                        if (__i >= __j)
3939                            break;
3940                        swap(*__i, *__j);
3941                        ++__n_swaps;
3942                        ++__i;
3943                    }
3944                    // [__first, __i) == *__first and *__first < [__i, __last)
3945                    // The first part is sorted, sort the secod part
3946                    // _VSTD::__sort<_Compare>(__i, __last, __comp);
3947                    __first = __i;
3948                    goto __restart;
3949                }
3950                if (__comp(*__j, *__m))
3951                {
3952                    swap(*__i, *__j);
3953                    ++__n_swaps;
3954                    break;  // found guard for downward moving __j, now use unguarded partition
3955                }
3956            }
3957        }
3958        // It is known that *__i < *__m
3959        ++__i;
3960        // j points beyond range to be tested, *__m is known to be <= *__lm1
3961        // if not yet partitioned...
3962        if (__i < __j)
3963        {
3964            // known that *(__i - 1) < *__m
3965            // known that __i <= __m
3966            while (true)
3967            {
3968                // __m still guards upward moving __i
3969                while (__comp(*__i, *__m))
3970                    ++__i;
3971                // It is now known that a guard exists for downward moving __j
3972                while (!__comp(*--__j, *__m))
3973                    ;
3974                if (__i > __j)
3975                    break;
3976                swap(*__i, *__j);
3977                ++__n_swaps;
3978                // It is known that __m != __j
3979                // If __m just moved, follow it
3980                if (__m == __i)
3981                    __m = __j;
3982                ++__i;
3983            }
3984        }
3985        // [__first, __i) < *__m and *__m <= [__i, __last)
3986        if (__i != __m && __comp(*__m, *__i))
3987        {
3988            swap(*__i, *__m);
3989            ++__n_swaps;
3990        }
3991        // [__first, __i) < *__i and *__i <= [__i+1, __last)
3992        // If we were given a perfect partition, see if insertion sort is quick...
3993        if (__n_swaps == 0)
3994        {
3995            bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3996            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
3997            {
3998                if (__fs)
3999                    return;
4000                __last = __i;
4001                continue;
4002            }
4003            else
4004            {
4005                if (__fs)
4006                {
4007                    __first = ++__i;
4008                    continue;
4009                }
4010            }
4011        }
4012        // sort smaller range with recursive call and larger with tail recursion elimination
4013        if (__i - __first < __last - __i)
4014        {
4015            _VSTD::__sort<_Compare>(__first, __i, __comp);
4016            // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4017            __first = ++__i;
4018        }
4019        else
4020        {
4021            _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4022            // _VSTD::__sort<_Compare>(__first, __i, __comp);
4023            __last = __i;
4024        }
4025    }
4026}
4027
4028// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4029template <class _RandomAccessIterator, class _Compare>
4030inline _LIBCPP_INLINE_VISIBILITY
4031void
4032sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4033{
4034#ifdef _LIBCPP_DEBUG
4035    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4036    __debug_less<_Compare> __c(__comp);
4037    __sort<_Comp_ref>(__first, __last, __c);
4038#else  // _LIBCPP_DEBUG
4039    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4040    __sort<_Comp_ref>(__first, __last, __comp);
4041#endif  // _LIBCPP_DEBUG
4042}
4043
4044template <class _RandomAccessIterator>
4045inline _LIBCPP_INLINE_VISIBILITY
4046void
4047sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4048{
4049    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4050}
4051
4052template <class _Tp>
4053inline _LIBCPP_INLINE_VISIBILITY
4054void
4055sort(_Tp** __first, _Tp** __last)
4056{
4057    _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4058}
4059
4060template <class _Tp>
4061inline _LIBCPP_INLINE_VISIBILITY
4062void
4063sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4064{
4065    _VSTD::sort(__first.base(), __last.base());
4066}
4067
4068template <class _Tp, class _Compare>
4069inline _LIBCPP_INLINE_VISIBILITY
4070void
4071sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4072{
4073    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4074    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4075}
4076
4077#ifdef _LIBCPP_MSVC
4078#pragma warning( push )
4079#pragma warning( disable: 4231)
4080#endif // _LIBCPP_MSVC
4081_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4082_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4084_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4085_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4086_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4087_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4088_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4089_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4090_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4091_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4092_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4093_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4094_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4095_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4096
4097_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4098_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4099_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4100_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4101_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4102_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4103_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4104_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4105_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4106_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4107_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4108_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4109_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4110_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4111_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4112
4113_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
4114#ifdef _LIBCPP_MSVC
4115#pragma warning( pop )
4116#endif  // _LIBCPP_MSVC
4117
4118// lower_bound
4119
4120template <class _Compare, class _ForwardIterator, class _Tp>
4121_ForwardIterator
4122__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4123{
4124    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4125    difference_type __len = _VSTD::distance(__first, __last);
4126    while (__len != 0)
4127    {
4128        difference_type __l2 = __len / 2;
4129        _ForwardIterator __m = __first;
4130        _VSTD::advance(__m, __l2);
4131        if (__comp(*__m, __value_))
4132        {
4133            __first = ++__m;
4134            __len -= __l2 + 1;
4135        }
4136        else
4137            __len = __l2;
4138    }
4139    return __first;
4140}
4141
4142template <class _ForwardIterator, class _Tp, class _Compare>
4143inline _LIBCPP_INLINE_VISIBILITY
4144_ForwardIterator
4145lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4146{
4147#ifdef _LIBCPP_DEBUG
4148    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4149    __debug_less<_Compare> __c(__comp);
4150    return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4151#else  // _LIBCPP_DEBUG
4152    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4153    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4154#endif  // _LIBCPP_DEBUG
4155}
4156
4157template <class _ForwardIterator, class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159_ForwardIterator
4160lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4161{
4162    return _VSTD::lower_bound(__first, __last, __value_,
4163                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4164}
4165
4166// upper_bound
4167
4168template <class _Compare, class _ForwardIterator, class _Tp>
4169_ForwardIterator
4170__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4171{
4172    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4173    difference_type __len = _VSTD::distance(__first, __last);
4174    while (__len != 0)
4175    {
4176        difference_type __l2 = __len / 2;
4177        _ForwardIterator __m = __first;
4178        _VSTD::advance(__m, __l2);
4179        if (__comp(__value_, *__m))
4180            __len = __l2;
4181        else
4182        {
4183            __first = ++__m;
4184            __len -= __l2 + 1;
4185        }
4186    }
4187    return __first;
4188}
4189
4190template <class _ForwardIterator, class _Tp, class _Compare>
4191inline _LIBCPP_INLINE_VISIBILITY
4192_ForwardIterator
4193upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4194{
4195#ifdef _LIBCPP_DEBUG
4196    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4197    __debug_less<_Compare> __c(__comp);
4198    return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4199#else  // _LIBCPP_DEBUG
4200    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4201    return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4202#endif  // _LIBCPP_DEBUG
4203}
4204
4205template <class _ForwardIterator, class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207_ForwardIterator
4208upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4209{
4210    return _VSTD::upper_bound(__first, __last, __value_,
4211                             __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4212}
4213
4214// equal_range
4215
4216template <class _Compare, class _ForwardIterator, class _Tp>
4217pair<_ForwardIterator, _ForwardIterator>
4218__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4219{
4220    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4221    difference_type __len = _VSTD::distance(__first, __last);
4222    while (__len != 0)
4223    {
4224        difference_type __l2 = __len / 2;
4225        _ForwardIterator __m = __first;
4226        _VSTD::advance(__m, __l2);
4227        if (__comp(*__m, __value_))
4228        {
4229            __first = ++__m;
4230            __len -= __l2 + 1;
4231        }
4232        else if (__comp(__value_, *__m))
4233        {
4234            __last = __m;
4235            __len = __l2;
4236        }
4237        else
4238        {
4239            _ForwardIterator __mp1 = __m;
4240            return pair<_ForwardIterator, _ForwardIterator>
4241                   (
4242                      __lower_bound<_Compare>(__first, __m, __value_, __comp),
4243                      __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4244                   );
4245        }
4246    }
4247    return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4248}
4249
4250template <class _ForwardIterator, class _Tp, class _Compare>
4251inline _LIBCPP_INLINE_VISIBILITY
4252pair<_ForwardIterator, _ForwardIterator>
4253equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4254{
4255#ifdef _LIBCPP_DEBUG
4256    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4257    __debug_less<_Compare> __c(__comp);
4258    return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4259#else  // _LIBCPP_DEBUG
4260    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4261    return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4262#endif  // _LIBCPP_DEBUG
4263}
4264
4265template <class _ForwardIterator, class _Tp>
4266inline _LIBCPP_INLINE_VISIBILITY
4267pair<_ForwardIterator, _ForwardIterator>
4268equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4269{
4270    return _VSTD::equal_range(__first, __last, __value_,
4271                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4272}
4273
4274// binary_search
4275
4276template <class _Compare, class _ForwardIterator, class _Tp>
4277inline _LIBCPP_INLINE_VISIBILITY
4278bool
4279__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4280{
4281    __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4282    return __first != __last && !__comp(__value_, *__first);
4283}
4284
4285template <class _ForwardIterator, class _Tp, class _Compare>
4286inline _LIBCPP_INLINE_VISIBILITY
4287bool
4288binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4289{
4290#ifdef _LIBCPP_DEBUG
4291    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4292    __debug_less<_Compare> __c(__comp);
4293    return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4294#else  // _LIBCPP_DEBUG
4295    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4296    return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4297#endif  // _LIBCPP_DEBUG
4298}
4299
4300template <class _ForwardIterator, class _Tp>
4301inline _LIBCPP_INLINE_VISIBILITY
4302bool
4303binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4304{
4305    return _VSTD::binary_search(__first, __last, __value_,
4306                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4307}
4308
4309// merge
4310
4311template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4312_OutputIterator
4313__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4314        _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4315{
4316    for (; __first1 != __last1; ++__result)
4317    {
4318        if (__first2 == __last2)
4319            return _VSTD::copy(__first1, __last1, __result);
4320        if (__comp(*__first2, *__first1))
4321        {
4322            *__result = *__first2;
4323            ++__first2;
4324        }
4325        else
4326        {
4327            *__result = *__first1;
4328            ++__first1;
4329        }
4330    }
4331    return _VSTD::copy(__first2, __last2, __result);
4332}
4333
4334template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4335inline _LIBCPP_INLINE_VISIBILITY
4336_OutputIterator
4337merge(_InputIterator1 __first1, _InputIterator1 __last1,
4338      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4339{
4340#ifdef _LIBCPP_DEBUG
4341    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4342    __debug_less<_Compare> __c(__comp);
4343    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4344#else  // _LIBCPP_DEBUG
4345    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4346    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4347#endif  // _LIBCPP_DEBUG
4348}
4349
4350template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4351inline _LIBCPP_INLINE_VISIBILITY
4352_OutputIterator
4353merge(_InputIterator1 __first1, _InputIterator1 __last1,
4354      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4355{
4356    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4357    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4358    return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4359}
4360
4361// inplace_merge
4362
4363template <class _Compare, class _BidirectionalIterator>
4364void
4365__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4366                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4367                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4368                typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4369{
4370    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4371    __destruct_n __d(0);
4372    unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4373    if (__len1 <= __len2)
4374    {
4375        value_type* __p = __buff;
4376        for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4377            ::new(__p) value_type(_VSTD::move(*__i));
4378        __merge<_Compare>(move_iterator<value_type*>(__buff),
4379                          move_iterator<value_type*>(__p),
4380                          move_iterator<_BidirectionalIterator>(__middle),
4381                          move_iterator<_BidirectionalIterator>(__last),
4382                          __first, __comp);
4383    }
4384    else
4385    {
4386        value_type* __p = __buff;
4387        for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4388            ::new(__p) value_type(_VSTD::move(*__i));
4389        typedef reverse_iterator<_BidirectionalIterator> _RBi;
4390        typedef reverse_iterator<value_type*> _Rv;
4391        __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
4392                move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
4393                _RBi(__last), __negate<_Compare>(__comp));
4394    }
4395}
4396
4397template <class _Compare, class _BidirectionalIterator>
4398void
4399__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4400                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4401                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4402                typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4403{
4404    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4405    while (true)
4406    {
4407        // if __middle == __last, we're done
4408        if (__len2 == 0)
4409            return;
4410        if (__len1 <= __buff_size || __len2 <= __buff_size)
4411            return __buffered_inplace_merge<_Compare>
4412                   (__first, __middle, __last, __comp, __len1, __len2, __buff);
4413        // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4414        for (; true; ++__first, (void) --__len1)
4415        {
4416            if (__len1 == 0)
4417                return;
4418            if (__comp(*__middle, *__first))
4419                break;
4420        }
4421        // __first < __middle < __last
4422        // *__first > *__middle
4423        // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4424        //     all elements in:
4425        //         [__first, __m1)  <= [__middle, __m2)
4426        //         [__middle, __m2) <  [__m1, __middle)
4427        //         [__m1, __middle) <= [__m2, __last)
4428        //     and __m1 or __m2 is in the middle of its range
4429        _BidirectionalIterator __m1;  // "median" of [__first, __middle)
4430        _BidirectionalIterator __m2;  // "median" of [__middle, __last)
4431        difference_type __len11;      // distance(__first, __m1)
4432        difference_type __len21;      // distance(__middle, __m2)
4433        // binary search smaller range
4434        if (__len1 < __len2)
4435        {   // __len >= 1, __len2 >= 2
4436            __len21 = __len2 / 2;
4437            __m2 = __middle;
4438            _VSTD::advance(__m2, __len21);
4439            __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4440            __len11 = _VSTD::distance(__first, __m1);
4441        }
4442        else
4443        {
4444            if (__len1 == 1)
4445            {   // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4446                // It is known *__first > *__middle
4447                swap(*__first, *__middle);
4448                return;
4449            }
4450            // __len1 >= 2, __len2 >= 1
4451            __len11 = __len1 / 2;
4452            __m1 = __first;
4453            _VSTD::advance(__m1, __len11);
4454            __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4455            __len21 = _VSTD::distance(__middle, __m2);
4456        }
4457        difference_type __len12 = __len1 - __len11;  // distance(__m1, __middle)
4458        difference_type __len22 = __len2 - __len21;  // distance(__m2, __last)
4459        // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4460        // swap middle two partitions
4461        __middle = _VSTD::rotate(__m1, __middle, __m2);
4462        // __len12 and __len21 now have swapped meanings
4463        // merge smaller range with recurisve call and larger with tail recursion elimination
4464        if (__len11 + __len21 < __len12 + __len22)
4465        {
4466            __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4467//          __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4468            __first = __middle;
4469            __middle = __m2;
4470            __len1 = __len12;
4471            __len2 = __len22;
4472        }
4473        else
4474        {
4475            __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4476//          __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4477            __last = __middle;
4478            __middle = __m1;
4479            __len1 = __len11;
4480            __len2 = __len21;
4481        }
4482    }
4483}
4484
4485template <class _BidirectionalIterator, class _Compare>
4486inline _LIBCPP_INLINE_VISIBILITY
4487void
4488inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4489              _Compare __comp)
4490{
4491    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4492    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4493    difference_type __len1 = _VSTD::distance(__first, __middle);
4494    difference_type __len2 = _VSTD::distance(__middle, __last);
4495    difference_type __buf_size = _VSTD::min(__len1, __len2);
4496    pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4497    unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4498
4499#ifdef _LIBCPP_DEBUG
4500    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4501    __debug_less<_Compare> __c(__comp);
4502    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4503                                            __buf.first, __buf.second);
4504#else  // _LIBCPP_DEBUG
4505    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4506    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4507                                            __buf.first, __buf.second);
4508#endif  // _LIBCPP_DEBUG
4509}
4510
4511template <class _BidirectionalIterator>
4512inline _LIBCPP_INLINE_VISIBILITY
4513void
4514inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4515{
4516    _VSTD::inplace_merge(__first, __middle, __last,
4517                        __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4518}
4519
4520// stable_sort
4521
4522template <class _Compare, class _InputIterator1, class _InputIterator2>
4523void
4524__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4525        _InputIterator2 __first2, _InputIterator2 __last2,
4526        typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4527{
4528    typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4529    __destruct_n __d(0);
4530    unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4531    for (; true; ++__result)
4532    {
4533        if (__first1 == __last1)
4534        {
4535            for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4536                ::new (__result) value_type(_VSTD::move(*__first2));
4537            __h.release();
4538            return;
4539        }
4540        if (__first2 == __last2)
4541        {
4542            for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4543                ::new (__result) value_type(_VSTD::move(*__first1));
4544            __h.release();
4545            return;
4546        }
4547        if (__comp(*__first2, *__first1))
4548        {
4549            ::new (__result) value_type(_VSTD::move(*__first2));
4550            __d.__incr((value_type*)0);
4551            ++__first2;
4552        }
4553        else
4554        {
4555            ::new (__result) value_type(_VSTD::move(*__first1));
4556            __d.__incr((value_type*)0);
4557            ++__first1;
4558        }
4559    }
4560}
4561
4562template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4563void
4564__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4565        _InputIterator2 __first2, _InputIterator2 __last2,
4566        _OutputIterator __result, _Compare __comp)
4567{
4568    for (; __first1 != __last1; ++__result)
4569    {
4570        if (__first2 == __last2)
4571        {
4572            for (; __first1 != __last1; ++__first1, ++__result)
4573                *__result = _VSTD::move(*__first1);
4574            return;
4575        }
4576        if (__comp(*__first2, *__first1))
4577        {
4578            *__result = _VSTD::move(*__first2);
4579            ++__first2;
4580        }
4581        else
4582        {
4583            *__result = _VSTD::move(*__first1);
4584            ++__first1;
4585        }
4586    }
4587    for (; __first2 != __last2; ++__first2, ++__result)
4588        *__result = _VSTD::move(*__first2);
4589}
4590
4591template <class _Compare, class _RandomAccessIterator>
4592void
4593__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4594              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4595              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4596
4597template <class _Compare, class _RandomAccessIterator>
4598void
4599__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4600                   typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4601                   typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4602{
4603    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4604    switch (__len)
4605    {
4606    case 0:
4607        return;
4608    case 1:
4609        ::new(__first2) value_type(_VSTD::move(*__first1));
4610        return;
4611    case 2:
4612       __destruct_n __d(0);
4613        unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4614         if (__comp(*--__last1, *__first1))
4615        {
4616            ::new(__first2) value_type(_VSTD::move(*__last1));
4617            __d.__incr((value_type*)0);
4618            ++__first2;
4619            ::new(__first2) value_type(_VSTD::move(*__first1));
4620        }
4621        else
4622        {
4623            ::new(__first2) value_type(_VSTD::move(*__first1));
4624            __d.__incr((value_type*)0);
4625            ++__first2;
4626            ::new(__first2) value_type(_VSTD::move(*__last1));
4627        }
4628        __h2.release();
4629        return;
4630    }
4631    if (__len <= 8)
4632    {
4633        __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4634        return;
4635    }
4636    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4637    _RandomAccessIterator __m = __first1 + __l2;
4638    __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4639    __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4640    __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4641}
4642
4643template <class _Tp>
4644struct __stable_sort_switch
4645{
4646    static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4647};
4648
4649template <class _Compare, class _RandomAccessIterator>
4650void
4651__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4652              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4653              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4654{
4655    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4656    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4657    switch (__len)
4658    {
4659    case 0:
4660    case 1:
4661        return;
4662    case 2:
4663        if (__comp(*--__last, *__first))
4664            swap(*__first, *__last);
4665        return;
4666    }
4667    if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4668    {
4669        __insertion_sort<_Compare>(__first, __last, __comp);
4670        return;
4671    }
4672    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4673    _RandomAccessIterator __m = __first + __l2;
4674    if (__len <= __buff_size)
4675    {
4676        __destruct_n __d(0);
4677        unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4678        __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4679        __d.__set(__l2, (value_type*)0);
4680        __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4681        __d.__set(__len, (value_type*)0);
4682        __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4683//         __merge<_Compare>(move_iterator<value_type*>(__buff),
4684//                           move_iterator<value_type*>(__buff + __l2),
4685//                           move_iterator<_RandomAccessIterator>(__buff + __l2),
4686//                           move_iterator<_RandomAccessIterator>(__buff + __len),
4687//                           __first, __comp);
4688        return;
4689    }
4690    __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4691    __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4692    __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4693}
4694
4695template <class _RandomAccessIterator, class _Compare>
4696inline _LIBCPP_INLINE_VISIBILITY
4697void
4698stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4699{
4700    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4701    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4702    difference_type __len = __last - __first;
4703    pair<value_type*, ptrdiff_t> __buf(0, 0);
4704    unique_ptr<value_type, __return_temporary_buffer> __h;
4705    if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4706    {
4707        __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4708        __h.reset(__buf.first);
4709    }
4710#ifdef _LIBCPP_DEBUG
4711    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4712    __debug_less<_Compare> __c(__comp);
4713    __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4714#else  // _LIBCPP_DEBUG
4715    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4716    __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4717#endif  // _LIBCPP_DEBUG
4718}
4719
4720template <class _RandomAccessIterator>
4721inline _LIBCPP_INLINE_VISIBILITY
4722void
4723stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4724{
4725    _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4726}
4727
4728// is_heap_until
4729
4730template <class _RandomAccessIterator, class _Compare>
4731_RandomAccessIterator
4732is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4733{
4734    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4735    difference_type __len = __last - __first;
4736    difference_type __p = 0;
4737    difference_type __c = 1;
4738    _RandomAccessIterator __pp = __first;
4739    while (__c < __len)
4740    {
4741        _RandomAccessIterator __cp = __first + __c;
4742        if (__comp(*__pp, *__cp))
4743            return __cp;
4744        ++__c;
4745        ++__cp;
4746        if (__c == __len)
4747            return __last;
4748        if (__comp(*__pp, *__cp))
4749            return __cp;
4750        ++__p;
4751        ++__pp;
4752        __c = 2 * __p + 1;
4753    }
4754    return __last;
4755}
4756
4757template<class _RandomAccessIterator>
4758inline _LIBCPP_INLINE_VISIBILITY
4759_RandomAccessIterator
4760is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4761{
4762    return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4763}
4764
4765// is_heap
4766
4767template <class _RandomAccessIterator, class _Compare>
4768inline _LIBCPP_INLINE_VISIBILITY
4769bool
4770is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4771{
4772    return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4773}
4774
4775template<class _RandomAccessIterator>
4776inline _LIBCPP_INLINE_VISIBILITY
4777bool
4778is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4779{
4780    return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4781}
4782
4783// push_heap
4784
4785template <class _Compare, class _RandomAccessIterator>
4786void
4787__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4788          typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4789{
4790    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4791    if (__len > 1)
4792    {
4793        __len = (__len - 2) / 2;
4794        _RandomAccessIterator __ptr = __first + __len;
4795        if (__comp(*__ptr, *--__last))
4796        {
4797            value_type __t(_VSTD::move(*__last));
4798            do
4799            {
4800                *__last = _VSTD::move(*__ptr);
4801                __last = __ptr;
4802                if (__len == 0)
4803                    break;
4804                __len = (__len - 1) / 2;
4805                __ptr = __first + __len;
4806            } while (__comp(*__ptr, __t));
4807            *__last = _VSTD::move(__t);
4808        }
4809    }
4810}
4811
4812template <class _RandomAccessIterator, class _Compare>
4813inline _LIBCPP_INLINE_VISIBILITY
4814void
4815push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4816{
4817#ifdef _LIBCPP_DEBUG
4818    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4819    __debug_less<_Compare> __c(__comp);
4820    __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
4821#else  // _LIBCPP_DEBUG
4822    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4823    __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
4824#endif  // _LIBCPP_DEBUG
4825}
4826
4827template <class _RandomAccessIterator>
4828inline _LIBCPP_INLINE_VISIBILITY
4829void
4830push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4831{
4832    _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4833}
4834
4835// pop_heap
4836
4837template <class _Compare, class _RandomAccessIterator>
4838void
4839__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4840            typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4841            _RandomAccessIterator __start)
4842{
4843    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4844    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4845    // left-child of __start is at 2 * __start + 1
4846    // right-child of __start is at 2 * __start + 2
4847    difference_type __child = __start - __first;
4848
4849    if (__len < 2 || (__len - 2) / 2 < __child)
4850        return;
4851
4852    __child = 2 * __child + 1;
4853    _RandomAccessIterator __child_i = __first + __child;
4854
4855    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4856        // right-child exists and is greater than left-child
4857        ++__child_i;
4858        ++__child;
4859    }
4860
4861    // check if we are in heap-order
4862    if (__comp(*__child_i, *__start))
4863        // we are, __start is larger than it's largest child
4864        return;
4865
4866    value_type __top(_VSTD::move(*__start));
4867    do
4868    {
4869        // we are not in heap-order, swap the parent with it's largest child
4870        *__start = _VSTD::move(*__child_i);
4871        __start = __child_i;
4872
4873        if ((__len - 2) / 2 < __child)
4874            break;
4875
4876        // recompute the child based off of the updated parent
4877        __child = 2 * __child + 1;
4878        __child_i = __first + __child;
4879
4880        if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4881            // right-child exists and is greater than left-child
4882            ++__child_i;
4883            ++__child;
4884        }
4885
4886        // check if we are in heap-order
4887    } while (!__comp(*__child_i, __top));
4888    *__start = _VSTD::move(__top);
4889}
4890
4891template <class _Compare, class _RandomAccessIterator>
4892inline _LIBCPP_INLINE_VISIBILITY
4893void
4894__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4895           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4896{
4897    if (__len > 1)
4898    {
4899        swap(*__first, *--__last);
4900        __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
4901    }
4902}
4903
4904template <class _RandomAccessIterator, class _Compare>
4905inline _LIBCPP_INLINE_VISIBILITY
4906void
4907pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4908{
4909#ifdef _LIBCPP_DEBUG
4910    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4911    __debug_less<_Compare> __c(__comp);
4912    __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4913#else  // _LIBCPP_DEBUG
4914    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4915    __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4916#endif  // _LIBCPP_DEBUG
4917}
4918
4919template <class _RandomAccessIterator>
4920inline _LIBCPP_INLINE_VISIBILITY
4921void
4922pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4923{
4924    _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4925}
4926
4927// make_heap
4928
4929template <class _Compare, class _RandomAccessIterator>
4930void
4931__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4932{
4933    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4934    difference_type __n = __last - __first;
4935    if (__n > 1)
4936    {
4937        // start from the first parent, there is no need to consider children
4938        for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4939        {
4940            __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4941        }
4942    }
4943}
4944
4945template <class _RandomAccessIterator, class _Compare>
4946inline _LIBCPP_INLINE_VISIBILITY
4947void
4948make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4949{
4950#ifdef _LIBCPP_DEBUG
4951    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4952    __debug_less<_Compare> __c(__comp);
4953    __make_heap<_Comp_ref>(__first, __last, __c);
4954#else  // _LIBCPP_DEBUG
4955    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4956    __make_heap<_Comp_ref>(__first, __last, __comp);
4957#endif  // _LIBCPP_DEBUG
4958}
4959
4960template <class _RandomAccessIterator>
4961inline _LIBCPP_INLINE_VISIBILITY
4962void
4963make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4964{
4965    _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4966}
4967
4968// sort_heap
4969
4970template <class _Compare, class _RandomAccessIterator>
4971void
4972__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4973{
4974    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4975    for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4976        __pop_heap<_Compare>(__first, __last, __comp, __n);
4977}
4978
4979template <class _RandomAccessIterator, class _Compare>
4980inline _LIBCPP_INLINE_VISIBILITY
4981void
4982sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4983{
4984#ifdef _LIBCPP_DEBUG
4985    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4986    __debug_less<_Compare> __c(__comp);
4987    __sort_heap<_Comp_ref>(__first, __last, __c);
4988#else  // _LIBCPP_DEBUG
4989    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4990    __sort_heap<_Comp_ref>(__first, __last, __comp);
4991#endif  // _LIBCPP_DEBUG
4992}
4993
4994template <class _RandomAccessIterator>
4995inline _LIBCPP_INLINE_VISIBILITY
4996void
4997sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4998{
4999    _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5000}
5001
5002// partial_sort
5003
5004template <class _Compare, class _RandomAccessIterator>
5005void
5006__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5007             _Compare __comp)
5008{
5009    __make_heap<_Compare>(__first, __middle, __comp);
5010    typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5011    for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5012    {
5013        if (__comp(*__i, *__first))
5014        {
5015            swap(*__i, *__first);
5016            __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5017        }
5018    }
5019    __sort_heap<_Compare>(__first, __middle, __comp);
5020}
5021
5022template <class _RandomAccessIterator, class _Compare>
5023inline _LIBCPP_INLINE_VISIBILITY
5024void
5025partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5026             _Compare __comp)
5027{
5028#ifdef _LIBCPP_DEBUG
5029    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5030    __debug_less<_Compare> __c(__comp);
5031    __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
5032#else  // _LIBCPP_DEBUG
5033    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5034    __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5035#endif  // _LIBCPP_DEBUG
5036}
5037
5038template <class _RandomAccessIterator>
5039inline _LIBCPP_INLINE_VISIBILITY
5040void
5041partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5042{
5043    _VSTD::partial_sort(__first, __middle, __last,
5044                       __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5045}
5046
5047// partial_sort_copy
5048
5049template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5050_RandomAccessIterator
5051__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5052                    _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5053{
5054    _RandomAccessIterator __r = __result_first;
5055    if (__r != __result_last)
5056    {
5057        for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5058            *__r = *__first;
5059        __make_heap<_Compare>(__result_first, __r, __comp);
5060        typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5061        for (; __first != __last; ++__first)
5062            if (__comp(*__first, *__result_first))
5063            {
5064                *__result_first = *__first;
5065                __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5066            }
5067        __sort_heap<_Compare>(__result_first, __r, __comp);
5068    }
5069    return __r;
5070}
5071
5072template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5073inline _LIBCPP_INLINE_VISIBILITY
5074_RandomAccessIterator
5075partial_sort_copy(_InputIterator __first, _InputIterator __last,
5076                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5077{
5078#ifdef _LIBCPP_DEBUG
5079    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5080    __debug_less<_Compare> __c(__comp);
5081    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
5082#else  // _LIBCPP_DEBUG
5083    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5084    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5085#endif  // _LIBCPP_DEBUG
5086}
5087
5088template <class _InputIterator, class _RandomAccessIterator>
5089inline _LIBCPP_INLINE_VISIBILITY
5090_RandomAccessIterator
5091partial_sort_copy(_InputIterator __first, _InputIterator __last,
5092                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5093{
5094    return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5095                                   __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5096}
5097
5098// nth_element
5099
5100template <class _Compare, class _RandomAccessIterator>
5101void
5102__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5103{
5104    // _Compare is known to be a reference type
5105    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5106    const difference_type __limit = 7;
5107    while (true)
5108    {
5109    __restart:
5110        if (__nth == __last)
5111            return;
5112        difference_type __len = __last - __first;
5113        switch (__len)
5114        {
5115        case 0:
5116        case 1:
5117            return;
5118        case 2:
5119            if (__comp(*--__last, *__first))
5120                swap(*__first, *__last);
5121            return;
5122        case 3:
5123            {
5124            _RandomAccessIterator __m = __first;
5125            _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5126            return;
5127            }
5128        }
5129        if (__len <= __limit)
5130        {
5131            __selection_sort<_Compare>(__first, __last, __comp);
5132            return;
5133        }
5134        // __len > __limit >= 3
5135        _RandomAccessIterator __m = __first + __len/2;
5136        _RandomAccessIterator __lm1 = __last;
5137        unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5138        // *__m is median
5139        // partition [__first, __m) < *__m and *__m <= [__m, __last)
5140        // (this inhibits tossing elements equivalent to __m around unnecessarily)
5141        _RandomAccessIterator __i = __first;
5142        _RandomAccessIterator __j = __lm1;
5143        // j points beyond range to be tested, *__lm1 is known to be <= *__m
5144        // The search going up is known to be guarded but the search coming down isn't.
5145        // Prime the downward search with a guard.
5146        if (!__comp(*__i, *__m))  // if *__first == *__m
5147        {
5148            // *__first == *__m, *__first doesn't go in first part
5149            // manually guard downward moving __j against __i
5150            while (true)
5151            {
5152                if (__i == --__j)
5153                {
5154                    // *__first == *__m, *__m <= all other elements
5155                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5156                    ++__i;  // __first + 1
5157                    __j = __last;
5158                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
5159                    {
5160                        while (true)
5161                        {
5162                            if (__i == __j)
5163                                return;  // [__first, __last) all equivalent elements
5164                            if (__comp(*__first, *__i))
5165                            {
5166                                swap(*__i, *__j);
5167                                ++__n_swaps;
5168                                ++__i;
5169                                break;
5170                            }
5171                            ++__i;
5172                        }
5173                    }
5174                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5175                    if (__i == __j)
5176                        return;
5177                    while (true)
5178                    {
5179                        while (!__comp(*__first, *__i))
5180                            ++__i;
5181                        while (__comp(*__first, *--__j))
5182                            ;
5183                        if (__i >= __j)
5184                            break;
5185                        swap(*__i, *__j);
5186                        ++__n_swaps;
5187                        ++__i;
5188                    }
5189                    // [__first, __i) == *__first and *__first < [__i, __last)
5190                    // The first part is sorted,
5191                    if (__nth < __i)
5192                        return;
5193                    // __nth_element the secod part
5194                    // __nth_element<_Compare>(__i, __nth, __last, __comp);
5195                    __first = __i;
5196                    goto __restart;
5197                }
5198                if (__comp(*__j, *__m))
5199                {
5200                    swap(*__i, *__j);
5201                    ++__n_swaps;
5202                    break;  // found guard for downward moving __j, now use unguarded partition
5203                }
5204            }
5205        }
5206        ++__i;
5207        // j points beyond range to be tested, *__lm1 is known to be <= *__m
5208        // if not yet partitioned...
5209        if (__i < __j)
5210        {
5211            // known that *(__i - 1) < *__m
5212            while (true)
5213            {
5214                // __m still guards upward moving __i
5215                while (__comp(*__i, *__m))
5216                    ++__i;
5217                // It is now known that a guard exists for downward moving __j
5218                while (!__comp(*--__j, *__m))
5219                    ;
5220                if (__i >= __j)
5221                    break;
5222                swap(*__i, *__j);
5223                ++__n_swaps;
5224                // It is known that __m != __j
5225                // If __m just moved, follow it
5226                if (__m == __i)
5227                    __m = __j;
5228                ++__i;
5229            }
5230        }
5231        // [__first, __i) < *__m and *__m <= [__i, __last)
5232        if (__i != __m && __comp(*__m, *__i))
5233        {
5234            swap(*__i, *__m);
5235            ++__n_swaps;
5236        }
5237        // [__first, __i) < *__i and *__i <= [__i+1, __last)
5238        if (__nth == __i)
5239            return;
5240        if (__n_swaps == 0)
5241        {
5242            // We were given a perfectly partitioned sequence.  Coincidence?
5243            if (__nth < __i)
5244            {
5245                // Check for [__first, __i) already sorted
5246                __j = __m = __first;
5247                while (++__j != __i)
5248                {
5249                    if (__comp(*__j, *__m))
5250                        // not yet sorted, so sort
5251                        goto not_sorted;
5252                    __m = __j;
5253                }
5254                // [__first, __i) sorted
5255                return;
5256            }
5257            else
5258            {
5259                // Check for [__i, __last) already sorted
5260                __j = __m = __i;
5261                while (++__j != __last)
5262                {
5263                    if (__comp(*__j, *__m))
5264                        // not yet sorted, so sort
5265                        goto not_sorted;
5266                    __m = __j;
5267                }
5268                // [__i, __last) sorted
5269                return;
5270            }
5271        }
5272not_sorted:
5273        // __nth_element on range containing __nth
5274        if (__nth < __i)
5275        {
5276            // __nth_element<_Compare>(__first, __nth, __i, __comp);
5277            __last = __i;
5278        }
5279        else
5280        {
5281            // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5282            __first = ++__i;
5283        }
5284    }
5285}
5286
5287template <class _RandomAccessIterator, class _Compare>
5288inline _LIBCPP_INLINE_VISIBILITY
5289void
5290nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5291{
5292#ifdef _LIBCPP_DEBUG
5293    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5294    __debug_less<_Compare> __c(__comp);
5295    __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5296#else  // _LIBCPP_DEBUG
5297    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5298    __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5299#endif  // _LIBCPP_DEBUG
5300}
5301
5302template <class _RandomAccessIterator>
5303inline _LIBCPP_INLINE_VISIBILITY
5304void
5305nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5306{
5307    _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5308}
5309
5310// includes
5311
5312template <class _Compare, class _InputIterator1, class _InputIterator2>
5313bool
5314__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5315           _Compare __comp)
5316{
5317    for (; __first2 != __last2; ++__first1)
5318    {
5319        if (__first1 == __last1 || __comp(*__first2, *__first1))
5320            return false;
5321        if (!__comp(*__first1, *__first2))
5322            ++__first2;
5323    }
5324    return true;
5325}
5326
5327template <class _InputIterator1, class _InputIterator2, class _Compare>
5328inline _LIBCPP_INLINE_VISIBILITY
5329bool
5330includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5331         _Compare __comp)
5332{
5333#ifdef _LIBCPP_DEBUG
5334    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5335    __debug_less<_Compare> __c(__comp);
5336    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5337#else  // _LIBCPP_DEBUG
5338    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5339    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5340#endif  // _LIBCPP_DEBUG
5341}
5342
5343template <class _InputIterator1, class _InputIterator2>
5344inline _LIBCPP_INLINE_VISIBILITY
5345bool
5346includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5347{
5348    return _VSTD::includes(__first1, __last1, __first2, __last2,
5349                          __less<typename iterator_traits<_InputIterator1>::value_type,
5350                                 typename iterator_traits<_InputIterator2>::value_type>());
5351}
5352
5353// set_union
5354
5355template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5356_OutputIterator
5357__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5358            _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5359{
5360    for (; __first1 != __last1; ++__result)
5361    {
5362        if (__first2 == __last2)
5363            return _VSTD::copy(__first1, __last1, __result);
5364        if (__comp(*__first2, *__first1))
5365        {
5366            *__result = *__first2;
5367            ++__first2;
5368        }
5369        else
5370        {
5371            *__result = *__first1;
5372            if (!__comp(*__first1, *__first2))
5373                ++__first2;
5374            ++__first1;
5375        }
5376    }
5377    return _VSTD::copy(__first2, __last2, __result);
5378}
5379
5380template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5381inline _LIBCPP_INLINE_VISIBILITY
5382_OutputIterator
5383set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5384          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5385{
5386#ifdef _LIBCPP_DEBUG
5387    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5388    __debug_less<_Compare> __c(__comp);
5389    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5390#else  // _LIBCPP_DEBUG
5391    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5392    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5393#endif  // _LIBCPP_DEBUG
5394}
5395
5396template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5397inline _LIBCPP_INLINE_VISIBILITY
5398_OutputIterator
5399set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5400          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5401{
5402    return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5403                          __less<typename iterator_traits<_InputIterator1>::value_type,
5404                                 typename iterator_traits<_InputIterator2>::value_type>());
5405}
5406
5407// set_intersection
5408
5409template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5410_OutputIterator
5411__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5412                   _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5413{
5414    while (__first1 != __last1 && __first2 != __last2)
5415    {
5416        if (__comp(*__first1, *__first2))
5417            ++__first1;
5418        else
5419        {
5420            if (!__comp(*__first2, *__first1))
5421            {
5422                *__result = *__first1;
5423                ++__result;
5424                ++__first1;
5425            }
5426            ++__first2;
5427        }
5428    }
5429    return __result;
5430}
5431
5432template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5433inline _LIBCPP_INLINE_VISIBILITY
5434_OutputIterator
5435set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5436                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5437{
5438#ifdef _LIBCPP_DEBUG
5439    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5440    __debug_less<_Compare> __c(__comp);
5441    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5442#else  // _LIBCPP_DEBUG
5443    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5444    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5445#endif  // _LIBCPP_DEBUG
5446}
5447
5448template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5449inline _LIBCPP_INLINE_VISIBILITY
5450_OutputIterator
5451set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5452                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5453{
5454    return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5455                                  __less<typename iterator_traits<_InputIterator1>::value_type,
5456                                         typename iterator_traits<_InputIterator2>::value_type>());
5457}
5458
5459// set_difference
5460
5461template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5462_OutputIterator
5463__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5464                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5465{
5466    while (__first1 != __last1)
5467    {
5468        if (__first2 == __last2)
5469            return _VSTD::copy(__first1, __last1, __result);
5470        if (__comp(*__first1, *__first2))
5471        {
5472            *__result = *__first1;
5473            ++__result;
5474            ++__first1;
5475        }
5476        else
5477        {
5478            if (!__comp(*__first2, *__first1))
5479                ++__first1;
5480            ++__first2;
5481        }
5482    }
5483    return __result;
5484}
5485
5486template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5487inline _LIBCPP_INLINE_VISIBILITY
5488_OutputIterator
5489set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5490               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5491{
5492#ifdef _LIBCPP_DEBUG
5493    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5494    __debug_less<_Compare> __c(__comp);
5495    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5496#else  // _LIBCPP_DEBUG
5497    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5498    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5499#endif  // _LIBCPP_DEBUG
5500}
5501
5502template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5503inline _LIBCPP_INLINE_VISIBILITY
5504_OutputIterator
5505set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5506               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5507{
5508    return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5509                                __less<typename iterator_traits<_InputIterator1>::value_type,
5510                                       typename iterator_traits<_InputIterator2>::value_type>());
5511}
5512
5513// set_symmetric_difference
5514
5515template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5516_OutputIterator
5517__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5518                           _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5519{
5520    while (__first1 != __last1)
5521    {
5522        if (__first2 == __last2)
5523            return _VSTD::copy(__first1, __last1, __result);
5524        if (__comp(*__first1, *__first2))
5525        {
5526            *__result = *__first1;
5527            ++__result;
5528            ++__first1;
5529        }
5530        else
5531        {
5532            if (__comp(*__first2, *__first1))
5533            {
5534                *__result = *__first2;
5535                ++__result;
5536            }
5537            else
5538                ++__first1;
5539            ++__first2;
5540        }
5541    }
5542    return _VSTD::copy(__first2, __last2, __result);
5543}
5544
5545template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5546inline _LIBCPP_INLINE_VISIBILITY
5547_OutputIterator
5548set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5549                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5550{
5551#ifdef _LIBCPP_DEBUG
5552    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5553    __debug_less<_Compare> __c(__comp);
5554    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5555#else  // _LIBCPP_DEBUG
5556    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5557    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5558#endif  // _LIBCPP_DEBUG
5559}
5560
5561template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5562inline _LIBCPP_INLINE_VISIBILITY
5563_OutputIterator
5564set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5565                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5566{
5567    return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5568                                          __less<typename iterator_traits<_InputIterator1>::value_type,
5569                                                 typename iterator_traits<_InputIterator2>::value_type>());
5570}
5571
5572// lexicographical_compare
5573
5574template <class _Compare, class _InputIterator1, class _InputIterator2>
5575bool
5576__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5577                          _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5578{
5579    for (; __first2 != __last2; ++__first1, (void) ++__first2)
5580    {
5581        if (__first1 == __last1 || __comp(*__first1, *__first2))
5582            return true;
5583        if (__comp(*__first2, *__first1))
5584            return false;
5585    }
5586    return false;
5587}
5588
5589template <class _InputIterator1, class _InputIterator2, class _Compare>
5590inline _LIBCPP_INLINE_VISIBILITY
5591bool
5592lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5593                        _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5594{
5595#ifdef _LIBCPP_DEBUG
5596    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5597    __debug_less<_Compare> __c(__comp);
5598    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5599#else  // _LIBCPP_DEBUG
5600    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5601    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5602#endif  // _LIBCPP_DEBUG
5603}
5604
5605template <class _InputIterator1, class _InputIterator2>
5606inline _LIBCPP_INLINE_VISIBILITY
5607bool
5608lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5609                        _InputIterator2 __first2, _InputIterator2 __last2)
5610{
5611    return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5612                                         __less<typename iterator_traits<_InputIterator1>::value_type,
5613                                                typename iterator_traits<_InputIterator2>::value_type>());
5614}
5615
5616// next_permutation
5617
5618template <class _Compare, class _BidirectionalIterator>
5619bool
5620__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5621{
5622    _BidirectionalIterator __i = __last;
5623    if (__first == __last || __first == --__i)
5624        return false;
5625    while (true)
5626    {
5627        _BidirectionalIterator __ip1 = __i;
5628        if (__comp(*--__i, *__ip1))
5629        {
5630            _BidirectionalIterator __j = __last;
5631            while (!__comp(*__i, *--__j))
5632                ;
5633            swap(*__i, *__j);
5634            _VSTD::reverse(__ip1, __last);
5635            return true;
5636        }
5637        if (__i == __first)
5638        {
5639            _VSTD::reverse(__first, __last);
5640            return false;
5641        }
5642    }
5643}
5644
5645template <class _BidirectionalIterator, class _Compare>
5646inline _LIBCPP_INLINE_VISIBILITY
5647bool
5648next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5649{
5650#ifdef _LIBCPP_DEBUG
5651    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5652    __debug_less<_Compare> __c(__comp);
5653    return __next_permutation<_Comp_ref>(__first, __last, __c);
5654#else  // _LIBCPP_DEBUG
5655    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5656    return __next_permutation<_Comp_ref>(__first, __last, __comp);
5657#endif  // _LIBCPP_DEBUG
5658}
5659
5660template <class _BidirectionalIterator>
5661inline _LIBCPP_INLINE_VISIBILITY
5662bool
5663next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5664{
5665    return _VSTD::next_permutation(__first, __last,
5666                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5667}
5668
5669// prev_permutation
5670
5671template <class _Compare, class _BidirectionalIterator>
5672bool
5673__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5674{
5675    _BidirectionalIterator __i = __last;
5676    if (__first == __last || __first == --__i)
5677        return false;
5678    while (true)
5679    {
5680        _BidirectionalIterator __ip1 = __i;
5681        if (__comp(*__ip1, *--__i))
5682        {
5683            _BidirectionalIterator __j = __last;
5684            while (!__comp(*--__j, *__i))
5685                ;
5686            swap(*__i, *__j);
5687            _VSTD::reverse(__ip1, __last);
5688            return true;
5689        }
5690        if (__i == __first)
5691        {
5692            _VSTD::reverse(__first, __last);
5693            return false;
5694        }
5695    }
5696}
5697
5698template <class _BidirectionalIterator, class _Compare>
5699inline _LIBCPP_INLINE_VISIBILITY
5700bool
5701prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5702{
5703#ifdef _LIBCPP_DEBUG
5704    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5705    __debug_less<_Compare> __c(__comp);
5706    return __prev_permutation<_Comp_ref>(__first, __last, __c);
5707#else  // _LIBCPP_DEBUG
5708    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5709    return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5710#endif  // _LIBCPP_DEBUG
5711}
5712
5713template <class _BidirectionalIterator>
5714inline _LIBCPP_INLINE_VISIBILITY
5715bool
5716prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5717{
5718    return _VSTD::prev_permutation(__first, __last,
5719                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5720}
5721
5722template <class _Tp>
5723inline _LIBCPP_INLINE_VISIBILITY
5724typename enable_if
5725<
5726    is_integral<_Tp>::value,
5727    _Tp
5728>::type
5729__rotate_left(_Tp __t, _Tp __n = 1)
5730{
5731    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5732    __n &= __bits;
5733    return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5734}
5735
5736template <class _Tp>
5737inline _LIBCPP_INLINE_VISIBILITY
5738typename enable_if
5739<
5740    is_integral<_Tp>::value,
5741    _Tp
5742>::type
5743__rotate_right(_Tp __t, _Tp __n = 1)
5744{
5745    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5746    __n &= __bits;
5747    return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5748}
5749
5750_LIBCPP_END_NAMESPACE_STD
5751
5752#endif  // _LIBCPP_ALGORITHM
5753