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