xref: /minix3/external/bsd/libc++/dist/libcxx/include/algorithm (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
14684ddb6SLionel Sambuc// -*- C++ -*-
24684ddb6SLionel Sambuc//===-------------------------- algorithm ---------------------------------===//
34684ddb6SLionel Sambuc//
44684ddb6SLionel Sambuc//                     The LLVM Compiler Infrastructure
54684ddb6SLionel Sambuc//
64684ddb6SLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open
74684ddb6SLionel Sambuc// Source Licenses. See LICENSE.TXT for details.
84684ddb6SLionel Sambuc//
94684ddb6SLionel Sambuc//===----------------------------------------------------------------------===//
104684ddb6SLionel Sambuc
114684ddb6SLionel Sambuc#ifndef _LIBCPP_ALGORITHM
124684ddb6SLionel Sambuc#define _LIBCPP_ALGORITHM
134684ddb6SLionel Sambuc
144684ddb6SLionel Sambuc/*
154684ddb6SLionel Sambuc    algorithm synopsis
164684ddb6SLionel Sambuc
174684ddb6SLionel Sambuc#include <initializer_list>
184684ddb6SLionel Sambuc
194684ddb6SLionel Sambucnamespace std
204684ddb6SLionel Sambuc{
214684ddb6SLionel Sambuc
224684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
234684ddb6SLionel Sambuc    bool
244684ddb6SLionel Sambuc    all_of(InputIterator first, InputIterator last, Predicate pred);
254684ddb6SLionel Sambuc
264684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
274684ddb6SLionel Sambuc    bool
284684ddb6SLionel Sambuc    any_of(InputIterator first, InputIterator last, Predicate pred);
294684ddb6SLionel Sambuc
304684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
314684ddb6SLionel Sambuc    bool
324684ddb6SLionel Sambuc    none_of(InputIterator first, InputIterator last, Predicate pred);
334684ddb6SLionel Sambuc
344684ddb6SLionel Sambuctemplate <class InputIterator, class Function>
354684ddb6SLionel Sambuc    Function
364684ddb6SLionel Sambuc    for_each(InputIterator first, InputIterator last, Function f);
374684ddb6SLionel Sambuc
384684ddb6SLionel Sambuctemplate <class InputIterator, class T>
394684ddb6SLionel Sambuc    InputIterator
404684ddb6SLionel Sambuc    find(InputIterator first, InputIterator last, const T& value);
414684ddb6SLionel Sambuc
424684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
434684ddb6SLionel Sambuc    InputIterator
444684ddb6SLionel Sambuc    find_if(InputIterator first, InputIterator last, Predicate pred);
454684ddb6SLionel Sambuc
464684ddb6SLionel Sambuctemplate<class InputIterator, class Predicate>
474684ddb6SLionel Sambuc    InputIterator
484684ddb6SLionel Sambuc    find_if_not(InputIterator first, InputIterator last, Predicate pred);
494684ddb6SLionel Sambuc
504684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2>
514684ddb6SLionel Sambuc    ForwardIterator1
524684ddb6SLionel Sambuc    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
534684ddb6SLionel Sambuc             ForwardIterator2 first2, ForwardIterator2 last2);
544684ddb6SLionel Sambuc
554684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
564684ddb6SLionel Sambuc    ForwardIterator1
574684ddb6SLionel Sambuc    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
584684ddb6SLionel Sambuc             ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
594684ddb6SLionel Sambuc
604684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2>
614684ddb6SLionel Sambuc    ForwardIterator1
624684ddb6SLionel Sambuc    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
634684ddb6SLionel Sambuc                  ForwardIterator2 first2, ForwardIterator2 last2);
644684ddb6SLionel Sambuc
654684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
664684ddb6SLionel Sambuc    ForwardIterator1
674684ddb6SLionel Sambuc    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
684684ddb6SLionel Sambuc                  ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
694684ddb6SLionel Sambuc
704684ddb6SLionel Sambuctemplate <class ForwardIterator>
714684ddb6SLionel Sambuc    ForwardIterator
724684ddb6SLionel Sambuc    adjacent_find(ForwardIterator first, ForwardIterator last);
734684ddb6SLionel Sambuc
744684ddb6SLionel Sambuctemplate <class ForwardIterator, class BinaryPredicate>
754684ddb6SLionel Sambuc    ForwardIterator
764684ddb6SLionel Sambuc    adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
774684ddb6SLionel Sambuc
784684ddb6SLionel Sambuctemplate <class InputIterator, class T>
794684ddb6SLionel Sambuc    typename iterator_traits<InputIterator>::difference_type
804684ddb6SLionel Sambuc    count(InputIterator first, InputIterator last, const T& value);
814684ddb6SLionel Sambuc
824684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
834684ddb6SLionel Sambuc    typename iterator_traits<InputIterator>::difference_type
844684ddb6SLionel Sambuc    count_if(InputIterator first, InputIterator last, Predicate pred);
854684ddb6SLionel Sambuc
864684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
874684ddb6SLionel Sambuc    pair<InputIterator1, InputIterator2>
884684ddb6SLionel Sambuc    mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
894684ddb6SLionel Sambuc
904684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
914684ddb6SLionel Sambuc    pair<InputIterator1, InputIterator2>
924684ddb6SLionel Sambuc    mismatch(InputIterator1 first1, InputIterator1 last1,
934684ddb6SLionel Sambuc             InputIterator2 first2, InputIterator2 last2); // **C++14**
944684ddb6SLionel Sambuc
954684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
964684ddb6SLionel Sambuc    pair<InputIterator1, InputIterator2>
974684ddb6SLionel Sambuc    mismatch(InputIterator1 first1, InputIterator1 last1,
984684ddb6SLionel Sambuc             InputIterator2 first2, BinaryPredicate pred);
994684ddb6SLionel Sambuc
1004684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
1014684ddb6SLionel Sambuc    pair<InputIterator1, InputIterator2>
1024684ddb6SLionel Sambuc    mismatch(InputIterator1 first1, InputIterator1 last1,
1034684ddb6SLionel Sambuc             InputIterator2 first2, InputIterator2 last2,
1044684ddb6SLionel Sambuc             BinaryPredicate pred); // **C++14**
1054684ddb6SLionel Sambuc
1064684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
1074684ddb6SLionel Sambuc    bool
1084684ddb6SLionel Sambuc    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
1094684ddb6SLionel Sambuc
1104684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
1114684ddb6SLionel Sambuc    bool
1124684ddb6SLionel Sambuc    equal(InputIterator1 first1, InputIterator1 last1,
1134684ddb6SLionel Sambuc          InputIterator2 first2, InputIterator2 last2); // **C++14**
1144684ddb6SLionel Sambuc
1154684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
1164684ddb6SLionel Sambuc    bool
1174684ddb6SLionel Sambuc    equal(InputIterator1 first1, InputIterator1 last1,
1184684ddb6SLionel Sambuc          InputIterator2 first2, BinaryPredicate pred);
1194684ddb6SLionel Sambuc
1204684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
1214684ddb6SLionel Sambuc    bool
1224684ddb6SLionel Sambuc    equal(InputIterator1 first1, InputIterator1 last1,
1234684ddb6SLionel Sambuc          InputIterator2 first2, InputIterator2 last2,
1244684ddb6SLionel Sambuc          BinaryPredicate pred); // **C++14**
1254684ddb6SLionel Sambuc
1264684ddb6SLionel Sambuctemplate<class ForwardIterator1, class ForwardIterator2>
1274684ddb6SLionel Sambuc    bool
1284684ddb6SLionel Sambuc    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
1294684ddb6SLionel Sambuc                   ForwardIterator2 first2);
1304684ddb6SLionel Sambuc
1314684ddb6SLionel Sambuctemplate<class ForwardIterator1, class ForwardIterator2>
1324684ddb6SLionel Sambuc    bool
1334684ddb6SLionel Sambuc    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
1344684ddb6SLionel Sambuc                   ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
1354684ddb6SLionel Sambuc
1364684ddb6SLionel Sambuctemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
1374684ddb6SLionel Sambuc    bool
1384684ddb6SLionel Sambuc    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
1394684ddb6SLionel Sambuc                   ForwardIterator2 first2, BinaryPredicate pred);
1404684ddb6SLionel Sambuc
1414684ddb6SLionel Sambuctemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
1424684ddb6SLionel Sambuc    bool
1434684ddb6SLionel Sambuc    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
1444684ddb6SLionel Sambuc                   ForwardIterator2 first2, ForwardIterator2 last2,
1454684ddb6SLionel Sambuc                   BinaryPredicate pred);  // **C++14**
1464684ddb6SLionel Sambuc
1474684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2>
1484684ddb6SLionel Sambuc    ForwardIterator1
1494684ddb6SLionel Sambuc    search(ForwardIterator1 first1, ForwardIterator1 last1,
1504684ddb6SLionel Sambuc           ForwardIterator2 first2, ForwardIterator2 last2);
1514684ddb6SLionel Sambuc
1524684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
1534684ddb6SLionel Sambuc    ForwardIterator1
1544684ddb6SLionel Sambuc    search(ForwardIterator1 first1, ForwardIterator1 last1,
1554684ddb6SLionel Sambuc           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
1564684ddb6SLionel Sambuc
1574684ddb6SLionel Sambuctemplate <class ForwardIterator, class Size, class T>
1584684ddb6SLionel Sambuc    ForwardIterator
1594684ddb6SLionel Sambuc    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
1604684ddb6SLionel Sambuc
1614684ddb6SLionel Sambuctemplate <class ForwardIterator, class Size, class T, class BinaryPredicate>
1624684ddb6SLionel Sambuc    ForwardIterator
1634684ddb6SLionel Sambuc    search_n(ForwardIterator first, ForwardIterator last,
1644684ddb6SLionel Sambuc             Size count, const T& value, BinaryPredicate pred);
1654684ddb6SLionel Sambuc
1664684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator>
1674684ddb6SLionel Sambuc    OutputIterator
1684684ddb6SLionel Sambuc    copy(InputIterator first, InputIterator last, OutputIterator result);
1694684ddb6SLionel Sambuc
1704684ddb6SLionel Sambuctemplate<class InputIterator, class OutputIterator, class Predicate>
1714684ddb6SLionel Sambuc    OutputIterator
1724684ddb6SLionel Sambuc    copy_if(InputIterator first, InputIterator last,
1734684ddb6SLionel Sambuc            OutputIterator result, Predicate pred);
1744684ddb6SLionel Sambuc
1754684ddb6SLionel Sambuctemplate<class InputIterator, class Size, class OutputIterator>
1764684ddb6SLionel Sambuc    OutputIterator
1774684ddb6SLionel Sambuc    copy_n(InputIterator first, Size n, OutputIterator result);
1784684ddb6SLionel Sambuc
1794684ddb6SLionel Sambuctemplate <class BidirectionalIterator1, class BidirectionalIterator2>
1804684ddb6SLionel Sambuc    BidirectionalIterator2
1814684ddb6SLionel Sambuc    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
1824684ddb6SLionel Sambuc                  BidirectionalIterator2 result);
1834684ddb6SLionel Sambuc
1844684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2>
1854684ddb6SLionel Sambuc    ForwardIterator2
1864684ddb6SLionel Sambuc    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
1874684ddb6SLionel Sambuc
1884684ddb6SLionel Sambuctemplate <class ForwardIterator1, class ForwardIterator2>
1894684ddb6SLionel Sambuc    void
1904684ddb6SLionel Sambuc    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
1914684ddb6SLionel Sambuc
1924684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class UnaryOperation>
1934684ddb6SLionel Sambuc    OutputIterator
1944684ddb6SLionel Sambuc    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
1954684ddb6SLionel Sambuc
1964684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
1974684ddb6SLionel Sambuc    OutputIterator
1984684ddb6SLionel Sambuc    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
1994684ddb6SLionel Sambuc              OutputIterator result, BinaryOperation binary_op);
2004684ddb6SLionel Sambuc
2014684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
2024684ddb6SLionel Sambuc    void
2034684ddb6SLionel Sambuc    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
2044684ddb6SLionel Sambuc
2054684ddb6SLionel Sambuctemplate <class ForwardIterator, class Predicate, class T>
2064684ddb6SLionel Sambuc    void
2074684ddb6SLionel Sambuc    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
2084684ddb6SLionel Sambuc
2094684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class T>
2104684ddb6SLionel Sambuc    OutputIterator
2114684ddb6SLionel Sambuc    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
2124684ddb6SLionel Sambuc                 const T& old_value, const T& new_value);
2134684ddb6SLionel Sambuc
2144684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class Predicate, class T>
2154684ddb6SLionel Sambuc    OutputIterator
2164684ddb6SLionel Sambuc    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
2174684ddb6SLionel Sambuc
2184684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
2194684ddb6SLionel Sambuc    void
2204684ddb6SLionel Sambuc    fill(ForwardIterator first, ForwardIterator last, const T& value);
2214684ddb6SLionel Sambuc
2224684ddb6SLionel Sambuctemplate <class OutputIterator, class Size, class T>
2234684ddb6SLionel Sambuc    OutputIterator
2244684ddb6SLionel Sambuc    fill_n(OutputIterator first, Size n, const T& value);
2254684ddb6SLionel Sambuc
2264684ddb6SLionel Sambuctemplate <class ForwardIterator, class Generator>
2274684ddb6SLionel Sambuc    void
2284684ddb6SLionel Sambuc    generate(ForwardIterator first, ForwardIterator last, Generator gen);
2294684ddb6SLionel Sambuc
2304684ddb6SLionel Sambuctemplate <class OutputIterator, class Size, class Generator>
2314684ddb6SLionel Sambuc    OutputIterator
2324684ddb6SLionel Sambuc    generate_n(OutputIterator first, Size n, Generator gen);
2334684ddb6SLionel Sambuc
2344684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
2354684ddb6SLionel Sambuc    ForwardIterator
2364684ddb6SLionel Sambuc    remove(ForwardIterator first, ForwardIterator last, const T& value);
2374684ddb6SLionel Sambuc
2384684ddb6SLionel Sambuctemplate <class ForwardIterator, class Predicate>
2394684ddb6SLionel Sambuc    ForwardIterator
2404684ddb6SLionel Sambuc    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
2414684ddb6SLionel Sambuc
2424684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class T>
2434684ddb6SLionel Sambuc    OutputIterator
2444684ddb6SLionel Sambuc    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
2454684ddb6SLionel Sambuc
2464684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class Predicate>
2474684ddb6SLionel Sambuc    OutputIterator
2484684ddb6SLionel Sambuc    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
2494684ddb6SLionel Sambuc
2504684ddb6SLionel Sambuctemplate <class ForwardIterator>
2514684ddb6SLionel Sambuc    ForwardIterator
2524684ddb6SLionel Sambuc    unique(ForwardIterator first, ForwardIterator last);
2534684ddb6SLionel Sambuc
2544684ddb6SLionel Sambuctemplate <class ForwardIterator, class BinaryPredicate>
2554684ddb6SLionel Sambuc    ForwardIterator
2564684ddb6SLionel Sambuc    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
2574684ddb6SLionel Sambuc
2584684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator>
2594684ddb6SLionel Sambuc    OutputIterator
2604684ddb6SLionel Sambuc    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
2614684ddb6SLionel Sambuc
2624684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator, class BinaryPredicate>
2634684ddb6SLionel Sambuc    OutputIterator
2644684ddb6SLionel Sambuc    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
2654684ddb6SLionel Sambuc
2664684ddb6SLionel Sambuctemplate <class BidirectionalIterator>
2674684ddb6SLionel Sambuc    void
2684684ddb6SLionel Sambuc    reverse(BidirectionalIterator first, BidirectionalIterator last);
2694684ddb6SLionel Sambuc
2704684ddb6SLionel Sambuctemplate <class BidirectionalIterator, class OutputIterator>
2714684ddb6SLionel Sambuc    OutputIterator
2724684ddb6SLionel Sambuc    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
2734684ddb6SLionel Sambuc
2744684ddb6SLionel Sambuctemplate <class ForwardIterator>
2754684ddb6SLionel Sambuc    ForwardIterator
2764684ddb6SLionel Sambuc    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
2774684ddb6SLionel Sambuc
2784684ddb6SLionel Sambuctemplate <class ForwardIterator, class OutputIterator>
2794684ddb6SLionel Sambuc    OutputIterator
2804684ddb6SLionel Sambuc    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
2814684ddb6SLionel Sambuc
2824684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
2834684ddb6SLionel Sambuc    void
284*0a6a1f1dSLionel Sambuc    random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
2854684ddb6SLionel Sambuc
2864684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class RandomNumberGenerator>
2874684ddb6SLionel Sambuc    void
288*0a6a1f1dSLionel Sambuc    random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289*0a6a1f1dSLionel Sambuc                   RandomNumberGenerator& rand);  // deprecated in C++14
2904684ddb6SLionel Sambuc
2914684ddb6SLionel Sambuctemplate<class RandomAccessIterator, class UniformRandomNumberGenerator>
2924684ddb6SLionel Sambuc    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
2934684ddb6SLionel Sambuc                 UniformRandomNumberGenerator&& g);
2944684ddb6SLionel Sambuc
2954684ddb6SLionel Sambuctemplate <class InputIterator, class Predicate>
2964684ddb6SLionel Sambuc    bool
2974684ddb6SLionel Sambuc    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
2984684ddb6SLionel Sambuc
2994684ddb6SLionel Sambuctemplate <class ForwardIterator, class Predicate>
3004684ddb6SLionel Sambuc    ForwardIterator
3014684ddb6SLionel Sambuc    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
3024684ddb6SLionel Sambuc
3034684ddb6SLionel Sambuctemplate <class InputIterator, class OutputIterator1,
3044684ddb6SLionel Sambuc          class OutputIterator2, class Predicate>
3054684ddb6SLionel Sambuc    pair<OutputIterator1, OutputIterator2>
3064684ddb6SLionel Sambuc    partition_copy(InputIterator first, InputIterator last,
3074684ddb6SLionel Sambuc                   OutputIterator1 out_true, OutputIterator2 out_false,
3084684ddb6SLionel Sambuc                   Predicate pred);
3094684ddb6SLionel Sambuc
3104684ddb6SLionel Sambuctemplate <class ForwardIterator, class Predicate>
3114684ddb6SLionel Sambuc    ForwardIterator
3124684ddb6SLionel Sambuc    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
3134684ddb6SLionel Sambuc
3144684ddb6SLionel Sambuctemplate<class ForwardIterator, class Predicate>
3154684ddb6SLionel Sambuc    ForwardIterator
3164684ddb6SLionel Sambuc    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
3174684ddb6SLionel Sambuc
3184684ddb6SLionel Sambuctemplate <class ForwardIterator>
3194684ddb6SLionel Sambuc    bool
3204684ddb6SLionel Sambuc    is_sorted(ForwardIterator first, ForwardIterator last);
3214684ddb6SLionel Sambuc
3224684ddb6SLionel Sambuctemplate <class ForwardIterator, class Compare>
3234684ddb6SLionel Sambuc    bool
3244684ddb6SLionel Sambuc    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
3254684ddb6SLionel Sambuc
3264684ddb6SLionel Sambuctemplate<class ForwardIterator>
3274684ddb6SLionel Sambuc    ForwardIterator
3284684ddb6SLionel Sambuc    is_sorted_until(ForwardIterator first, ForwardIterator last);
3294684ddb6SLionel Sambuc
3304684ddb6SLionel Sambuctemplate <class ForwardIterator, class Compare>
3314684ddb6SLionel Sambuc    ForwardIterator
3324684ddb6SLionel Sambuc    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
3334684ddb6SLionel Sambuc
3344684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
3354684ddb6SLionel Sambuc    void
3364684ddb6SLionel Sambuc    sort(RandomAccessIterator first, RandomAccessIterator last);
3374684ddb6SLionel Sambuc
3384684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
3394684ddb6SLionel Sambuc    void
3404684ddb6SLionel Sambuc    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
3414684ddb6SLionel Sambuc
3424684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
3434684ddb6SLionel Sambuc    void
3444684ddb6SLionel Sambuc    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
3454684ddb6SLionel Sambuc
3464684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
3474684ddb6SLionel Sambuc    void
3484684ddb6SLionel Sambuc    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
3494684ddb6SLionel Sambuc
3504684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
3514684ddb6SLionel Sambuc    void
3524684ddb6SLionel Sambuc    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
3534684ddb6SLionel Sambuc
3544684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
3554684ddb6SLionel Sambuc    void
3564684ddb6SLionel Sambuc    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
3574684ddb6SLionel Sambuc
3584684ddb6SLionel Sambuctemplate <class InputIterator, class RandomAccessIterator>
3594684ddb6SLionel Sambuc    RandomAccessIterator
3604684ddb6SLionel Sambuc    partial_sort_copy(InputIterator first, InputIterator last,
3614684ddb6SLionel Sambuc                      RandomAccessIterator result_first, RandomAccessIterator result_last);
3624684ddb6SLionel Sambuc
3634684ddb6SLionel Sambuctemplate <class InputIterator, class RandomAccessIterator, class Compare>
3644684ddb6SLionel Sambuc    RandomAccessIterator
3654684ddb6SLionel Sambuc    partial_sort_copy(InputIterator first, InputIterator last,
3664684ddb6SLionel Sambuc                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
3674684ddb6SLionel Sambuc
3684684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
3694684ddb6SLionel Sambuc    void
3704684ddb6SLionel Sambuc    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
3714684ddb6SLionel Sambuc
3724684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
3734684ddb6SLionel Sambuc    void
3744684ddb6SLionel Sambuc    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
3754684ddb6SLionel Sambuc
3764684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
3774684ddb6SLionel Sambuc    ForwardIterator
3784684ddb6SLionel Sambuc    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
3794684ddb6SLionel Sambuc
3804684ddb6SLionel Sambuctemplate <class ForwardIterator, class T, class Compare>
3814684ddb6SLionel Sambuc    ForwardIterator
3824684ddb6SLionel Sambuc    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
3834684ddb6SLionel Sambuc
3844684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
3854684ddb6SLionel Sambuc    ForwardIterator
3864684ddb6SLionel Sambuc    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
3874684ddb6SLionel Sambuc
3884684ddb6SLionel Sambuctemplate <class ForwardIterator, class T, class Compare>
3894684ddb6SLionel Sambuc    ForwardIterator
3904684ddb6SLionel Sambuc    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
3914684ddb6SLionel Sambuc
3924684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
3934684ddb6SLionel Sambuc    pair<ForwardIterator, ForwardIterator>
3944684ddb6SLionel Sambuc    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
3954684ddb6SLionel Sambuc
3964684ddb6SLionel Sambuctemplate <class ForwardIterator, class T, class Compare>
3974684ddb6SLionel Sambuc    pair<ForwardIterator, ForwardIterator>
3984684ddb6SLionel Sambuc    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
3994684ddb6SLionel Sambuc
4004684ddb6SLionel Sambuctemplate <class ForwardIterator, class T>
4014684ddb6SLionel Sambuc    bool
4024684ddb6SLionel Sambuc    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
4034684ddb6SLionel Sambuc
4044684ddb6SLionel Sambuctemplate <class ForwardIterator, class T, class Compare>
4054684ddb6SLionel Sambuc    bool
4064684ddb6SLionel Sambuc    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
4074684ddb6SLionel Sambuc
4084684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator>
4094684ddb6SLionel Sambuc    OutputIterator
4104684ddb6SLionel Sambuc    merge(InputIterator1 first1, InputIterator1 last1,
4114684ddb6SLionel Sambuc          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
4124684ddb6SLionel Sambuc
4134684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
4144684ddb6SLionel Sambuc    OutputIterator
4154684ddb6SLionel Sambuc    merge(InputIterator1 first1, InputIterator1 last1,
4164684ddb6SLionel Sambuc          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
4174684ddb6SLionel Sambuc
4184684ddb6SLionel Sambuctemplate <class BidirectionalIterator>
4194684ddb6SLionel Sambuc    void
4204684ddb6SLionel Sambuc    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
4214684ddb6SLionel Sambuc
4224684ddb6SLionel Sambuctemplate <class BidirectionalIterator, class Compare>
4234684ddb6SLionel Sambuc    void
4244684ddb6SLionel Sambuc    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
4254684ddb6SLionel Sambuc
4264684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
4274684ddb6SLionel Sambuc    bool
4284684ddb6SLionel Sambuc    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
4294684ddb6SLionel Sambuc
4304684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class Compare>
4314684ddb6SLionel Sambuc    bool
4324684ddb6SLionel Sambuc    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
4334684ddb6SLionel Sambuc
4344684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator>
4354684ddb6SLionel Sambuc    OutputIterator
4364684ddb6SLionel Sambuc    set_union(InputIterator1 first1, InputIterator1 last1,
4374684ddb6SLionel Sambuc              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
4384684ddb6SLionel Sambuc
4394684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
4404684ddb6SLionel Sambuc    OutputIterator
4414684ddb6SLionel Sambuc    set_union(InputIterator1 first1, InputIterator1 last1,
4424684ddb6SLionel Sambuc              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
4434684ddb6SLionel Sambuc
4444684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator>
4454684ddb6SLionel Sambuc    OutputIterator
4464684ddb6SLionel Sambuc    set_intersection(InputIterator1 first1, InputIterator1 last1,
4474684ddb6SLionel Sambuc                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
4484684ddb6SLionel Sambuc
4494684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
4504684ddb6SLionel Sambuc    OutputIterator
4514684ddb6SLionel Sambuc    set_intersection(InputIterator1 first1, InputIterator1 last1,
4524684ddb6SLionel Sambuc                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
4534684ddb6SLionel Sambuc
4544684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator>
4554684ddb6SLionel Sambuc    OutputIterator
4564684ddb6SLionel Sambuc    set_difference(InputIterator1 first1, InputIterator1 last1,
4574684ddb6SLionel Sambuc                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
4584684ddb6SLionel Sambuc
4594684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
4604684ddb6SLionel Sambuc    OutputIterator
4614684ddb6SLionel Sambuc    set_difference(InputIterator1 first1, InputIterator1 last1,
4624684ddb6SLionel Sambuc                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
4634684ddb6SLionel Sambuc
4644684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator>
4654684ddb6SLionel Sambuc    OutputIterator
4664684ddb6SLionel Sambuc    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
4674684ddb6SLionel Sambuc                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
4684684ddb6SLionel Sambuc
4694684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
4704684ddb6SLionel Sambuc    OutputIterator
4714684ddb6SLionel Sambuc    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
4724684ddb6SLionel Sambuc                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
4734684ddb6SLionel Sambuc
4744684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
4754684ddb6SLionel Sambuc    void
4764684ddb6SLionel Sambuc    push_heap(RandomAccessIterator first, RandomAccessIterator last);
4774684ddb6SLionel Sambuc
4784684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
4794684ddb6SLionel Sambuc    void
4804684ddb6SLionel Sambuc    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
4814684ddb6SLionel Sambuc
4824684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
4834684ddb6SLionel Sambuc    void
4844684ddb6SLionel Sambuc    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
4854684ddb6SLionel Sambuc
4864684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
4874684ddb6SLionel Sambuc    void
4884684ddb6SLionel Sambuc    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
4894684ddb6SLionel Sambuc
4904684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
4914684ddb6SLionel Sambuc    void
4924684ddb6SLionel Sambuc    make_heap(RandomAccessIterator first, RandomAccessIterator last);
4934684ddb6SLionel Sambuc
4944684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
4954684ddb6SLionel Sambuc    void
4964684ddb6SLionel Sambuc    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
4974684ddb6SLionel Sambuc
4984684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
4994684ddb6SLionel Sambuc    void
5004684ddb6SLionel Sambuc    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
5014684ddb6SLionel Sambuc
5024684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
5034684ddb6SLionel Sambuc    void
5044684ddb6SLionel Sambuc    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
5054684ddb6SLionel Sambuc
5064684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
5074684ddb6SLionel Sambuc    bool
5084684ddb6SLionel Sambuc    is_heap(RandomAccessIterator first, RandomAccessiterator last);
5094684ddb6SLionel Sambuc
5104684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
5114684ddb6SLionel Sambuc    bool
5124684ddb6SLionel Sambuc    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
5134684ddb6SLionel Sambuc
5144684ddb6SLionel Sambuctemplate <class RandomAccessIterator>
5154684ddb6SLionel Sambuc    RandomAccessIterator
5164684ddb6SLionel Sambuc    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
5174684ddb6SLionel Sambuc
5184684ddb6SLionel Sambuctemplate <class RandomAccessIterator, class Compare>
5194684ddb6SLionel Sambuc    RandomAccessIterator
5204684ddb6SLionel Sambuc    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
5214684ddb6SLionel Sambuc
5224684ddb6SLionel Sambuctemplate <class ForwardIterator>
5234684ddb6SLionel Sambuc    ForwardIterator
524*0a6a1f1dSLionel Sambuc    min_element(ForwardIterator first, ForwardIterator last);  // constexpr in C++14
5254684ddb6SLionel Sambuc
5264684ddb6SLionel Sambuctemplate <class ForwardIterator, class Compare>
5274684ddb6SLionel Sambuc    ForwardIterator
528*0a6a1f1dSLionel Sambuc    min_element(ForwardIterator first, ForwardIterator last, Compare comp);  // constexpr in C++14
5294684ddb6SLionel Sambuc
5304684ddb6SLionel Sambuctemplate <class T>
5314684ddb6SLionel Sambuc    const T&
532*0a6a1f1dSLionel Sambuc    min(const T& a, const T& b);  // constexpr in C++14
5334684ddb6SLionel Sambuc
5344684ddb6SLionel Sambuctemplate <class T, class Compare>
5354684ddb6SLionel Sambuc    const T&
536*0a6a1f1dSLionel Sambuc    min(const T& a, const T& b, Compare comp);  // constexpr in C++14
5374684ddb6SLionel Sambuc
5384684ddb6SLionel Sambuctemplate<class T>
5394684ddb6SLionel Sambuc    T
540*0a6a1f1dSLionel Sambuc    min(initializer_list<T> t);  // constexpr in C++14
5414684ddb6SLionel Sambuc
5424684ddb6SLionel Sambuctemplate<class T, class Compare>
5434684ddb6SLionel Sambuc    T
544*0a6a1f1dSLionel Sambuc    min(initializer_list<T> t, Compare comp);  // constexpr in C++14
5454684ddb6SLionel Sambuc
5464684ddb6SLionel Sambuctemplate <class ForwardIterator>
5474684ddb6SLionel Sambuc    ForwardIterator
548*0a6a1f1dSLionel Sambuc    max_element(ForwardIterator first, ForwardIterator last);  // constexpr in C++14
5494684ddb6SLionel Sambuc
5504684ddb6SLionel Sambuctemplate <class ForwardIterator, class Compare>
5514684ddb6SLionel Sambuc    ForwardIterator
552*0a6a1f1dSLionel Sambuc    max_element(ForwardIterator first, ForwardIterator last, Compare comp);  // constexpr in C++14
5534684ddb6SLionel Sambuc
5544684ddb6SLionel Sambuctemplate <class T>
5554684ddb6SLionel Sambuc    const T&
556*0a6a1f1dSLionel Sambuc    max(const T& a, const T& b); // constexpr in C++14
5574684ddb6SLionel Sambuc
5584684ddb6SLionel Sambuctemplate <class T, class Compare>
5594684ddb6SLionel Sambuc    const T&
560*0a6a1f1dSLionel Sambuc    max(const T& a, const T& b, Compare comp);  // constexpr in C++14
5614684ddb6SLionel Sambuc
5624684ddb6SLionel Sambuctemplate<class T>
5634684ddb6SLionel Sambuc    T
564*0a6a1f1dSLionel Sambuc    max(initializer_list<T> t);  // constexpr in C++14
5654684ddb6SLionel Sambuc
5664684ddb6SLionel Sambuctemplate<class T, class Compare>
5674684ddb6SLionel Sambuc    T
568*0a6a1f1dSLionel Sambuc    max(initializer_list<T> t, Compare comp);  // constexpr in C++14
5694684ddb6SLionel Sambuc
5704684ddb6SLionel Sambuctemplate<class ForwardIterator>
5714684ddb6SLionel Sambuc    pair<ForwardIterator, ForwardIterator>
572*0a6a1f1dSLionel Sambuc    minmax_element(ForwardIterator first, ForwardIterator last);   // constexpr in C++14
5734684ddb6SLionel Sambuc
5744684ddb6SLionel Sambuctemplate<class ForwardIterator, class Compare>
5754684ddb6SLionel Sambuc    pair<ForwardIterator, ForwardIterator>
576*0a6a1f1dSLionel Sambuc    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);   // constexpr in C++14
5774684ddb6SLionel Sambuc
5784684ddb6SLionel Sambuctemplate<class T>
5794684ddb6SLionel Sambuc    pair<const T&, const T&>
580*0a6a1f1dSLionel Sambuc    minmax(const T& a, const T& b);  // constexpr in C++14
5814684ddb6SLionel Sambuc
5824684ddb6SLionel Sambuctemplate<class T, class Compare>
5834684ddb6SLionel Sambuc    pair<const T&, const T&>
584*0a6a1f1dSLionel Sambuc    minmax(const T& a, const T& b, Compare comp);  // constexpr in C++14
5854684ddb6SLionel Sambuc
5864684ddb6SLionel Sambuctemplate<class T>
5874684ddb6SLionel Sambuc    pair<T, T>
588*0a6a1f1dSLionel Sambuc    minmax(initializer_list<T> t);  // constexpr in C++14
5894684ddb6SLionel Sambuc
5904684ddb6SLionel Sambuctemplate<class T, class Compare>
5914684ddb6SLionel Sambuc    pair<T, T>
592*0a6a1f1dSLionel Sambuc    minmax(initializer_list<T> t, Compare comp);  // constexpr in C++14
5934684ddb6SLionel Sambuc
5944684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2>
5954684ddb6SLionel Sambuc    bool
5964684ddb6SLionel Sambuc    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
5974684ddb6SLionel Sambuc
5984684ddb6SLionel Sambuctemplate <class InputIterator1, class InputIterator2, class Compare>
5994684ddb6SLionel Sambuc    bool
6004684ddb6SLionel Sambuc    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
6014684ddb6SLionel Sambuc                            InputIterator2 first2, InputIterator2 last2, Compare comp);
6024684ddb6SLionel Sambuc
6034684ddb6SLionel Sambuctemplate <class BidirectionalIterator>
6044684ddb6SLionel Sambuc    bool
6054684ddb6SLionel Sambuc    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
6064684ddb6SLionel Sambuc
6074684ddb6SLionel Sambuctemplate <class BidirectionalIterator, class Compare>
6084684ddb6SLionel Sambuc    bool
6094684ddb6SLionel Sambuc    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
6104684ddb6SLionel Sambuc
6114684ddb6SLionel Sambuctemplate <class BidirectionalIterator>
6124684ddb6SLionel Sambuc    bool
6134684ddb6SLionel Sambuc    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
6144684ddb6SLionel Sambuc
6154684ddb6SLionel Sambuctemplate <class BidirectionalIterator, class Compare>
6164684ddb6SLionel Sambuc    bool
6174684ddb6SLionel Sambuc    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
6184684ddb6SLionel Sambuc
6194684ddb6SLionel Sambuc}  // std
6204684ddb6SLionel Sambuc
6214684ddb6SLionel Sambuc*/
6224684ddb6SLionel Sambuc
6234684ddb6SLionel Sambuc#include <__config>
6244684ddb6SLionel Sambuc#include <initializer_list>
6254684ddb6SLionel Sambuc#include <type_traits>
6264684ddb6SLionel Sambuc#include <cstring>
6274684ddb6SLionel Sambuc#include <utility>
6284684ddb6SLionel Sambuc#include <memory>
6294684ddb6SLionel Sambuc#include <iterator>
6304684ddb6SLionel Sambuc#include <cstddef>
6314684ddb6SLionel Sambuc
6324684ddb6SLionel Sambuc#if defined(__IBMCPP__)
6334684ddb6SLionel Sambuc#include "support/ibm/support.h"
6344684ddb6SLionel Sambuc#endif
6354684ddb6SLionel Sambuc#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6364684ddb6SLionel Sambuc#include "support/win32/support.h"
6374684ddb6SLionel Sambuc#endif
6384684ddb6SLionel Sambuc
6394684ddb6SLionel Sambuc#include <__undef_min_max>
6404684ddb6SLionel Sambuc
641*0a6a1f1dSLionel Sambuc#include <__debug>
642*0a6a1f1dSLionel Sambuc
6434684ddb6SLionel Sambuc#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
6444684ddb6SLionel Sambuc#pragma GCC system_header
6454684ddb6SLionel Sambuc#endif
6464684ddb6SLionel Sambuc
6474684ddb6SLionel Sambuc_LIBCPP_BEGIN_NAMESPACE_STD
6484684ddb6SLionel Sambuc
649*0a6a1f1dSLionel Sambuc// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
650*0a6a1f1dSLionel Sambuc//   * That only works with C++14 and later, and
651*0a6a1f1dSLionel Sambuc//   * We haven't included <functional> here.
6524684ddb6SLionel Sambuctemplate <class _T1, class _T2 = _T1>
6534684ddb6SLionel Sambucstruct __equal_to
6544684ddb6SLionel Sambuc{
6554684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
6564684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
6574684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
6584684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
6594684ddb6SLionel Sambuc};
6604684ddb6SLionel Sambuc
6614684ddb6SLionel Sambuctemplate <class _T1>
6624684ddb6SLionel Sambucstruct __equal_to<_T1, _T1>
6634684ddb6SLionel Sambuc{
664*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
665*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
6664684ddb6SLionel Sambuc};
6674684ddb6SLionel Sambuc
6684684ddb6SLionel Sambuctemplate <class _T1>
6694684ddb6SLionel Sambucstruct __equal_to<const _T1, _T1>
6704684ddb6SLionel Sambuc{
671*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
672*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
6734684ddb6SLionel Sambuc};
6744684ddb6SLionel Sambuc
6754684ddb6SLionel Sambuctemplate <class _T1>
6764684ddb6SLionel Sambucstruct __equal_to<_T1, const _T1>
6774684ddb6SLionel Sambuc{
678*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
6804684ddb6SLionel Sambuc};
6814684ddb6SLionel Sambuc
6824684ddb6SLionel Sambuctemplate <class _T1, class _T2 = _T1>
6834684ddb6SLionel Sambucstruct __less
6844684ddb6SLionel Sambuc{
685*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
687*0a6a1f1dSLionel Sambuc
688*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
690*0a6a1f1dSLionel Sambuc
691*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692*0a6a1f1dSLionel Sambuc    bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
693*0a6a1f1dSLionel Sambuc
694*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695*0a6a1f1dSLionel Sambuc    bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
6964684ddb6SLionel Sambuc};
6974684ddb6SLionel Sambuc
6984684ddb6SLionel Sambuctemplate <class _T1>
6994684ddb6SLionel Sambucstruct __less<_T1, _T1>
7004684ddb6SLionel Sambuc{
701*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
7034684ddb6SLionel Sambuc};
7044684ddb6SLionel Sambuc
7054684ddb6SLionel Sambuctemplate <class _T1>
7064684ddb6SLionel Sambucstruct __less<const _T1, _T1>
7074684ddb6SLionel Sambuc{
708*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
7104684ddb6SLionel Sambuc};
7114684ddb6SLionel Sambuc
7124684ddb6SLionel Sambuctemplate <class _T1>
7134684ddb6SLionel Sambucstruct __less<_T1, const _T1>
7144684ddb6SLionel Sambuc{
715*0a6a1f1dSLionel Sambuc    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716*0a6a1f1dSLionel Sambuc    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
7174684ddb6SLionel Sambuc};
7184684ddb6SLionel Sambuc
7194684ddb6SLionel Sambuctemplate <class _Predicate>
7204684ddb6SLionel Sambucclass __negate
7214684ddb6SLionel Sambuc{
7224684ddb6SLionel Sambucprivate:
7234684ddb6SLionel Sambuc    _Predicate __p_;
7244684ddb6SLionel Sambucpublic:
7254684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY __negate() {}
7264684ddb6SLionel Sambuc
7274684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY
7284684ddb6SLionel Sambuc    explicit __negate(_Predicate __p) : __p_(__p) {}
7294684ddb6SLionel Sambuc
7304684ddb6SLionel Sambuc    template <class _T1>
7314684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY
7324684ddb6SLionel Sambuc    bool operator()(const _T1& __x) {return !__p_(__x);}
7334684ddb6SLionel Sambuc
7344684ddb6SLionel Sambuc    template <class _T1, class _T2>
7354684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY
7364684ddb6SLionel Sambuc    bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
7374684ddb6SLionel Sambuc};
7384684ddb6SLionel Sambuc
7394684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
7404684ddb6SLionel Sambuc
7414684ddb6SLionel Sambuctemplate <class _Compare>
7424684ddb6SLionel Sambucstruct __debug_less
7434684ddb6SLionel Sambuc{
7444684ddb6SLionel Sambuc    _Compare __comp_;
7454684ddb6SLionel Sambuc    __debug_less(_Compare& __c) : __comp_(__c) {}
7464684ddb6SLionel Sambuc    template <class _Tp, class _Up>
7474684ddb6SLionel Sambuc    bool operator()(const _Tp& __x, const _Up& __y)
7484684ddb6SLionel Sambuc    {
7494684ddb6SLionel Sambuc        bool __r = __comp_(__x, __y);
7504684ddb6SLionel Sambuc        if (__r)
7514684ddb6SLionel Sambuc            _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
7524684ddb6SLionel Sambuc        return __r;
7534684ddb6SLionel Sambuc    }
7544684ddb6SLionel Sambuc};
7554684ddb6SLionel Sambuc
7564684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
7574684ddb6SLionel Sambuc
7584684ddb6SLionel Sambuc// Precondition:  __x != 0
7594684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7604684ddb6SLionel Sambucunsigned
7614684ddb6SLionel Sambuc__ctz(unsigned __x)
7624684ddb6SLionel Sambuc{
7634684ddb6SLionel Sambuc    return static_cast<unsigned>(__builtin_ctz(__x));
7644684ddb6SLionel Sambuc}
7654684ddb6SLionel Sambuc
7664684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7674684ddb6SLionel Sambucunsigned long
7684684ddb6SLionel Sambuc__ctz(unsigned long __x)
7694684ddb6SLionel Sambuc{
7704684ddb6SLionel Sambuc    return static_cast<unsigned long>(__builtin_ctzl(__x));
7714684ddb6SLionel Sambuc}
7724684ddb6SLionel Sambuc
7734684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7744684ddb6SLionel Sambucunsigned long long
7754684ddb6SLionel Sambuc__ctz(unsigned long long __x)
7764684ddb6SLionel Sambuc{
7774684ddb6SLionel Sambuc    return static_cast<unsigned long long>(__builtin_ctzll(__x));
7784684ddb6SLionel Sambuc}
7794684ddb6SLionel Sambuc
7804684ddb6SLionel Sambuc// Precondition:  __x != 0
7814684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7824684ddb6SLionel Sambucunsigned
7834684ddb6SLionel Sambuc__clz(unsigned __x)
7844684ddb6SLionel Sambuc{
7854684ddb6SLionel Sambuc    return static_cast<unsigned>(__builtin_clz(__x));
7864684ddb6SLionel Sambuc}
7874684ddb6SLionel Sambuc
7884684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7894684ddb6SLionel Sambucunsigned long
7904684ddb6SLionel Sambuc__clz(unsigned long __x)
7914684ddb6SLionel Sambuc{
7924684ddb6SLionel Sambuc    return static_cast<unsigned long>(__builtin_clzl (__x));
7934684ddb6SLionel Sambuc}
7944684ddb6SLionel Sambuc
7954684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
7964684ddb6SLionel Sambucunsigned long long
7974684ddb6SLionel Sambuc__clz(unsigned long long __x)
7984684ddb6SLionel Sambuc{
7994684ddb6SLionel Sambuc    return static_cast<unsigned long long>(__builtin_clzll(__x));
8004684ddb6SLionel Sambuc}
8014684ddb6SLionel Sambuc
8024684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned           __x) {return __builtin_popcount  (__x);}
8034684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned      long __x) {return __builtin_popcountl (__x);}
8044684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
8054684ddb6SLionel Sambuc
8064684ddb6SLionel Sambuc// all_of
8074684ddb6SLionel Sambuc
8084684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
8094684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8104684ddb6SLionel Sambucbool
8114684ddb6SLionel Sambucall_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
8124684ddb6SLionel Sambuc{
8134684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8144684ddb6SLionel Sambuc        if (!__pred(*__first))
8154684ddb6SLionel Sambuc            return false;
8164684ddb6SLionel Sambuc    return true;
8174684ddb6SLionel Sambuc}
8184684ddb6SLionel Sambuc
8194684ddb6SLionel Sambuc// any_of
8204684ddb6SLionel Sambuc
8214684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
8224684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8234684ddb6SLionel Sambucbool
8244684ddb6SLionel Sambucany_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
8254684ddb6SLionel Sambuc{
8264684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8274684ddb6SLionel Sambuc        if (__pred(*__first))
8284684ddb6SLionel Sambuc            return true;
8294684ddb6SLionel Sambuc    return false;
8304684ddb6SLionel Sambuc}
8314684ddb6SLionel Sambuc
8324684ddb6SLionel Sambuc// none_of
8334684ddb6SLionel Sambuc
8344684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
8354684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8364684ddb6SLionel Sambucbool
8374684ddb6SLionel Sambucnone_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
8384684ddb6SLionel Sambuc{
8394684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8404684ddb6SLionel Sambuc        if (__pred(*__first))
8414684ddb6SLionel Sambuc            return false;
8424684ddb6SLionel Sambuc    return true;
8434684ddb6SLionel Sambuc}
8444684ddb6SLionel Sambuc
8454684ddb6SLionel Sambuc// for_each
8464684ddb6SLionel Sambuc
8474684ddb6SLionel Sambuctemplate <class _InputIterator, class _Function>
8484684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8494684ddb6SLionel Sambuc_Function
8504684ddb6SLionel Sambucfor_each(_InputIterator __first, _InputIterator __last, _Function __f)
8514684ddb6SLionel Sambuc{
8524684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8534684ddb6SLionel Sambuc        __f(*__first);
854*0a6a1f1dSLionel Sambuc    return _LIBCPP_EXPLICIT_MOVE(__f);  // explicitly moved for (emulated) C++03
8554684ddb6SLionel Sambuc}
8564684ddb6SLionel Sambuc
8574684ddb6SLionel Sambuc// find
8584684ddb6SLionel Sambuc
8594684ddb6SLionel Sambuctemplate <class _InputIterator, class _Tp>
8604684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8614684ddb6SLionel Sambuc_InputIterator
8624684ddb6SLionel Sambucfind(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
8634684ddb6SLionel Sambuc{
8644684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8654684ddb6SLionel Sambuc        if (*__first == __value_)
8664684ddb6SLionel Sambuc            break;
8674684ddb6SLionel Sambuc    return __first;
8684684ddb6SLionel Sambuc}
8694684ddb6SLionel Sambuc
8704684ddb6SLionel Sambuc// find_if
8714684ddb6SLionel Sambuc
8724684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
8734684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8744684ddb6SLionel Sambuc_InputIterator
8754684ddb6SLionel Sambucfind_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
8764684ddb6SLionel Sambuc{
8774684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8784684ddb6SLionel Sambuc        if (__pred(*__first))
8794684ddb6SLionel Sambuc            break;
8804684ddb6SLionel Sambuc    return __first;
8814684ddb6SLionel Sambuc}
8824684ddb6SLionel Sambuc
8834684ddb6SLionel Sambuc// find_if_not
8844684ddb6SLionel Sambuc
8854684ddb6SLionel Sambuctemplate<class _InputIterator, class _Predicate>
8864684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
8874684ddb6SLionel Sambuc_InputIterator
8884684ddb6SLionel Sambucfind_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
8894684ddb6SLionel Sambuc{
8904684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
8914684ddb6SLionel Sambuc        if (!__pred(*__first))
8924684ddb6SLionel Sambuc            break;
8934684ddb6SLionel Sambuc    return __first;
8944684ddb6SLionel Sambuc}
8954684ddb6SLionel Sambuc
8964684ddb6SLionel Sambuc// find_end
8974684ddb6SLionel Sambuc
8984684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
8994684ddb6SLionel Sambuc_ForwardIterator1
9004684ddb6SLionel Sambuc__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
9014684ddb6SLionel Sambuc           _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
9024684ddb6SLionel Sambuc           forward_iterator_tag, forward_iterator_tag)
9034684ddb6SLionel Sambuc{
9044684ddb6SLionel Sambuc    // modeled after search algorithm
9054684ddb6SLionel Sambuc    _ForwardIterator1 __r = __last1;  // __last1 is the "default" answer
9064684ddb6SLionel Sambuc    if (__first2 == __last2)
9074684ddb6SLionel Sambuc        return __r;
9084684ddb6SLionel Sambuc    while (true)
9094684ddb6SLionel Sambuc    {
9104684ddb6SLionel Sambuc        while (true)
9114684ddb6SLionel Sambuc        {
9124684ddb6SLionel Sambuc            if (__first1 == __last1)         // if source exhausted return last correct answer
9134684ddb6SLionel Sambuc                return __r;                  //    (or __last1 if never found)
9144684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
9154684ddb6SLionel Sambuc                break;
9164684ddb6SLionel Sambuc            ++__first1;
9174684ddb6SLionel Sambuc        }
9184684ddb6SLionel Sambuc        // *__first1 matches *__first2, now match elements after here
9194684ddb6SLionel Sambuc        _ForwardIterator1 __m1 = __first1;
9204684ddb6SLionel Sambuc        _ForwardIterator2 __m2 = __first2;
9214684ddb6SLionel Sambuc        while (true)
9224684ddb6SLionel Sambuc        {
9234684ddb6SLionel Sambuc            if (++__m2 == __last2)
9244684ddb6SLionel Sambuc            {                         // Pattern exhaused, record answer and search for another one
9254684ddb6SLionel Sambuc                __r = __first1;
9264684ddb6SLionel Sambuc                ++__first1;
9274684ddb6SLionel Sambuc                break;
9284684ddb6SLionel Sambuc            }
9294684ddb6SLionel Sambuc            if (++__m1 == __last1)     // Source exhausted, return last answer
9304684ddb6SLionel Sambuc                return __r;
9314684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))  // mismatch, restart with a new __first
9324684ddb6SLionel Sambuc            {
9334684ddb6SLionel Sambuc                ++__first1;
9344684ddb6SLionel Sambuc                break;
9354684ddb6SLionel Sambuc            }  // else there is a match, check next elements
9364684ddb6SLionel Sambuc        }
9374684ddb6SLionel Sambuc    }
9384684ddb6SLionel Sambuc}
9394684ddb6SLionel Sambuc
9404684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
9414684ddb6SLionel Sambuc_BidirectionalIterator1
9424684ddb6SLionel Sambuc__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
9434684ddb6SLionel Sambuc           _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
9444684ddb6SLionel Sambuc           bidirectional_iterator_tag, bidirectional_iterator_tag)
9454684ddb6SLionel Sambuc{
9464684ddb6SLionel Sambuc    // modeled after search algorithm (in reverse)
9474684ddb6SLionel Sambuc    if (__first2 == __last2)
9484684ddb6SLionel Sambuc        return __last1;  // Everything matches an empty sequence
9494684ddb6SLionel Sambuc    _BidirectionalIterator1 __l1 = __last1;
9504684ddb6SLionel Sambuc    _BidirectionalIterator2 __l2 = __last2;
9514684ddb6SLionel Sambuc    --__l2;
9524684ddb6SLionel Sambuc    while (true)
9534684ddb6SLionel Sambuc    {
9544684ddb6SLionel Sambuc        // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
9554684ddb6SLionel Sambuc        while (true)
9564684ddb6SLionel Sambuc        {
9574684ddb6SLionel Sambuc            if (__first1 == __l1)  // return __last1 if no element matches *__first2
9584684ddb6SLionel Sambuc                return __last1;
9594684ddb6SLionel Sambuc            if (__pred(*--__l1, *__l2))
9604684ddb6SLionel Sambuc                break;
9614684ddb6SLionel Sambuc        }
9624684ddb6SLionel Sambuc        // *__l1 matches *__l2, now match elements before here
9634684ddb6SLionel Sambuc        _BidirectionalIterator1 __m1 = __l1;
9644684ddb6SLionel Sambuc        _BidirectionalIterator2 __m2 = __l2;
9654684ddb6SLionel Sambuc        while (true)
9664684ddb6SLionel Sambuc        {
9674684ddb6SLionel Sambuc            if (__m2 == __first2)  // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
9684684ddb6SLionel Sambuc                return __m1;
9694684ddb6SLionel Sambuc            if (__m1 == __first1)  // Otherwise if source exhaused, pattern not found
9704684ddb6SLionel Sambuc                return __last1;
9714684ddb6SLionel Sambuc            if (!__pred(*--__m1, *--__m2))  // if there is a mismatch, restart with a new __l1
9724684ddb6SLionel Sambuc            {
9734684ddb6SLionel Sambuc                break;
9744684ddb6SLionel Sambuc            }  // else there is a match, check next elements
9754684ddb6SLionel Sambuc        }
9764684ddb6SLionel Sambuc    }
9774684ddb6SLionel Sambuc}
9784684ddb6SLionel Sambuc
9794684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
980*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
9814684ddb6SLionel Sambuc__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
9824684ddb6SLionel Sambuc           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
9834684ddb6SLionel Sambuc           random_access_iterator_tag, random_access_iterator_tag)
9844684ddb6SLionel Sambuc{
9854684ddb6SLionel Sambuc    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
9864684ddb6SLionel Sambuc    typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
9874684ddb6SLionel Sambuc    if (__len2 == 0)
9884684ddb6SLionel Sambuc        return __last1;
9894684ddb6SLionel Sambuc    typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
9904684ddb6SLionel Sambuc    if (__len1 < __len2)
9914684ddb6SLionel Sambuc        return __last1;
9924684ddb6SLionel Sambuc    const _RandomAccessIterator1 __s = __first1 + (__len2 - 1);  // End of pattern match can't go before here
9934684ddb6SLionel Sambuc    _RandomAccessIterator1 __l1 = __last1;
9944684ddb6SLionel Sambuc    _RandomAccessIterator2 __l2 = __last2;
9954684ddb6SLionel Sambuc    --__l2;
9964684ddb6SLionel Sambuc    while (true)
9974684ddb6SLionel Sambuc    {
9984684ddb6SLionel Sambuc        while (true)
9994684ddb6SLionel Sambuc        {
10004684ddb6SLionel Sambuc            if (__s == __l1)
10014684ddb6SLionel Sambuc                return __last1;
10024684ddb6SLionel Sambuc            if (__pred(*--__l1, *__l2))
10034684ddb6SLionel Sambuc                break;
10044684ddb6SLionel Sambuc        }
10054684ddb6SLionel Sambuc        _RandomAccessIterator1 __m1 = __l1;
10064684ddb6SLionel Sambuc        _RandomAccessIterator2 __m2 = __l2;
10074684ddb6SLionel Sambuc        while (true)
10084684ddb6SLionel Sambuc        {
10094684ddb6SLionel Sambuc            if (__m2 == __first2)
10104684ddb6SLionel Sambuc                return __m1;
10114684ddb6SLionel Sambuc                                 // no need to check range on __m1 because __s guarantees we have enough source
10124684ddb6SLionel Sambuc            if (!__pred(*--__m1, *--__m2))
10134684ddb6SLionel Sambuc            {
10144684ddb6SLionel Sambuc                break;
10154684ddb6SLionel Sambuc            }
10164684ddb6SLionel Sambuc        }
10174684ddb6SLionel Sambuc    }
10184684ddb6SLionel Sambuc}
10194684ddb6SLionel Sambuc
10204684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
10214684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
10224684ddb6SLionel Sambuc_ForwardIterator1
10234684ddb6SLionel Sambucfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
10244684ddb6SLionel Sambuc         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
10254684ddb6SLionel Sambuc{
10264684ddb6SLionel Sambuc    return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
10274684ddb6SLionel Sambuc                         (__first1, __last1, __first2, __last2, __pred,
10284684ddb6SLionel Sambuc                          typename iterator_traits<_ForwardIterator1>::iterator_category(),
10294684ddb6SLionel Sambuc                          typename iterator_traits<_ForwardIterator2>::iterator_category());
10304684ddb6SLionel Sambuc}
10314684ddb6SLionel Sambuc
10324684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2>
10334684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
10344684ddb6SLionel Sambuc_ForwardIterator1
10354684ddb6SLionel Sambucfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
10364684ddb6SLionel Sambuc         _ForwardIterator2 __first2, _ForwardIterator2 __last2)
10374684ddb6SLionel Sambuc{
10384684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
10394684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
10404684ddb6SLionel Sambuc    return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
10414684ddb6SLionel Sambuc}
10424684ddb6SLionel Sambuc
10434684ddb6SLionel Sambuc// find_first_of
10444684ddb6SLionel Sambuc
10454684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1046*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047*0a6a1f1dSLionel Sambuc__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
10484684ddb6SLionel Sambuc              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
10494684ddb6SLionel Sambuc{
10504684ddb6SLionel Sambuc    for (; __first1 != __last1; ++__first1)
10514684ddb6SLionel Sambuc        for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
10524684ddb6SLionel Sambuc            if (__pred(*__first1, *__j))
10534684ddb6SLionel Sambuc                return __first1;
10544684ddb6SLionel Sambuc    return __last1;
10554684ddb6SLionel Sambuc}
10564684ddb6SLionel Sambuc
1057*0a6a1f1dSLionel Sambuc
1058*0a6a1f1dSLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1059*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
1060*0a6a1f1dSLionel Sambuc_ForwardIterator1
1061*0a6a1f1dSLionel Sambucfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062*0a6a1f1dSLionel Sambuc              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1063*0a6a1f1dSLionel Sambuc{
1064*0a6a1f1dSLionel Sambuc    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1065*0a6a1f1dSLionel Sambuc}
1066*0a6a1f1dSLionel Sambuc
10674684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2>
10684684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
10694684ddb6SLionel Sambuc_ForwardIterator1
10704684ddb6SLionel Sambucfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
10714684ddb6SLionel Sambuc              _ForwardIterator2 __first2, _ForwardIterator2 __last2)
10724684ddb6SLionel Sambuc{
10734684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
10744684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1075*0a6a1f1dSLionel Sambuc    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
10764684ddb6SLionel Sambuc}
10774684ddb6SLionel Sambuc
10784684ddb6SLionel Sambuc// adjacent_find
10794684ddb6SLionel Sambuc
10804684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _BinaryPredicate>
10814684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
10824684ddb6SLionel Sambuc_ForwardIterator
10834684ddb6SLionel Sambucadjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
10844684ddb6SLionel Sambuc{
10854684ddb6SLionel Sambuc    if (__first != __last)
10864684ddb6SLionel Sambuc    {
10874684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
10884684ddb6SLionel Sambuc        while (++__i != __last)
10894684ddb6SLionel Sambuc        {
10904684ddb6SLionel Sambuc            if (__pred(*__first, *__i))
10914684ddb6SLionel Sambuc                return __first;
10924684ddb6SLionel Sambuc            __first = __i;
10934684ddb6SLionel Sambuc        }
10944684ddb6SLionel Sambuc    }
10954684ddb6SLionel Sambuc    return __last;
10964684ddb6SLionel Sambuc}
10974684ddb6SLionel Sambuc
10984684ddb6SLionel Sambuctemplate <class _ForwardIterator>
10994684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11004684ddb6SLionel Sambuc_ForwardIterator
11014684ddb6SLionel Sambucadjacent_find(_ForwardIterator __first, _ForwardIterator __last)
11024684ddb6SLionel Sambuc{
11034684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
11044684ddb6SLionel Sambuc    return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
11054684ddb6SLionel Sambuc}
11064684ddb6SLionel Sambuc
11074684ddb6SLionel Sambuc// count
11084684ddb6SLionel Sambuc
11094684ddb6SLionel Sambuctemplate <class _InputIterator, class _Tp>
11104684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11114684ddb6SLionel Sambuctypename iterator_traits<_InputIterator>::difference_type
11124684ddb6SLionel Sambuccount(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
11134684ddb6SLionel Sambuc{
11144684ddb6SLionel Sambuc    typename iterator_traits<_InputIterator>::difference_type __r(0);
11154684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
11164684ddb6SLionel Sambuc        if (*__first == __value_)
11174684ddb6SLionel Sambuc            ++__r;
11184684ddb6SLionel Sambuc    return __r;
11194684ddb6SLionel Sambuc}
11204684ddb6SLionel Sambuc
11214684ddb6SLionel Sambuc// count_if
11224684ddb6SLionel Sambuc
11234684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
11244684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11254684ddb6SLionel Sambuctypename iterator_traits<_InputIterator>::difference_type
11264684ddb6SLionel Sambuccount_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
11274684ddb6SLionel Sambuc{
11284684ddb6SLionel Sambuc    typename iterator_traits<_InputIterator>::difference_type __r(0);
11294684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
11304684ddb6SLionel Sambuc        if (__pred(*__first))
11314684ddb6SLionel Sambuc            ++__r;
11324684ddb6SLionel Sambuc    return __r;
11334684ddb6SLionel Sambuc}
11344684ddb6SLionel Sambuc
11354684ddb6SLionel Sambuc// mismatch
11364684ddb6SLionel Sambuc
11374684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
11384684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11394684ddb6SLionel Sambucpair<_InputIterator1, _InputIterator2>
11404684ddb6SLionel Sambucmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
11414684ddb6SLionel Sambuc         _InputIterator2 __first2, _BinaryPredicate __pred)
11424684ddb6SLionel Sambuc{
1143*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1; ++__first1, (void) ++__first2)
11444684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
11454684ddb6SLionel Sambuc            break;
11464684ddb6SLionel Sambuc    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
11474684ddb6SLionel Sambuc}
11484684ddb6SLionel Sambuc
11494684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
11504684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11514684ddb6SLionel Sambucpair<_InputIterator1, _InputIterator2>
11524684ddb6SLionel Sambucmismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
11534684ddb6SLionel Sambuc{
11544684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
11554684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
11564684ddb6SLionel Sambuc    return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
11574684ddb6SLionel Sambuc}
11584684ddb6SLionel Sambuc
11594684ddb6SLionel Sambuc#if _LIBCPP_STD_VER > 11
11604684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
11614684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11624684ddb6SLionel Sambucpair<_InputIterator1, _InputIterator2>
11634684ddb6SLionel Sambucmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
11644684ddb6SLionel Sambuc         _InputIterator2 __first2, _InputIterator2 __last2,
11654684ddb6SLionel Sambuc         _BinaryPredicate __pred)
11664684ddb6SLionel Sambuc{
1167*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
11684684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
11694684ddb6SLionel Sambuc            break;
11704684ddb6SLionel Sambuc    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
11714684ddb6SLionel Sambuc}
11724684ddb6SLionel Sambuc
11734684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
11744684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11754684ddb6SLionel Sambucpair<_InputIterator1, _InputIterator2>
11764684ddb6SLionel Sambucmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
11774684ddb6SLionel Sambuc         _InputIterator2 __first2, _InputIterator2 __last2)
11784684ddb6SLionel Sambuc{
11794684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
11804684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
11814684ddb6SLionel Sambuc    return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
11824684ddb6SLionel Sambuc}
11834684ddb6SLionel Sambuc#endif
11844684ddb6SLionel Sambuc
11854684ddb6SLionel Sambuc// equal
11864684ddb6SLionel Sambuc
11874684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
11884684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
11894684ddb6SLionel Sambucbool
11904684ddb6SLionel Sambucequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
11914684ddb6SLionel Sambuc{
1192*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1; ++__first1, (void) ++__first2)
11934684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
11944684ddb6SLionel Sambuc            return false;
11954684ddb6SLionel Sambuc    return true;
11964684ddb6SLionel Sambuc}
11974684ddb6SLionel Sambuc
11984684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
11994684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
12004684ddb6SLionel Sambucbool
12014684ddb6SLionel Sambucequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
12024684ddb6SLionel Sambuc{
12034684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
12044684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
12054684ddb6SLionel Sambuc    return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
12064684ddb6SLionel Sambuc}
12074684ddb6SLionel Sambuc
12084684ddb6SLionel Sambuc#if _LIBCPP_STD_VER > 11
12094684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
12104684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
12114684ddb6SLionel Sambucbool
12124684ddb6SLionel Sambuc__equal(_InputIterator1 __first1, _InputIterator1 __last1,
12134684ddb6SLionel Sambuc        _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
12144684ddb6SLionel Sambuc        input_iterator_tag, input_iterator_tag )
12154684ddb6SLionel Sambuc{
1216*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
12174684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
12184684ddb6SLionel Sambuc            return false;
12194684ddb6SLionel Sambuc    return __first1 == __last1 && __first2 == __last2;
12204684ddb6SLionel Sambuc}
12214684ddb6SLionel Sambuc
12224684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
12234684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
12244684ddb6SLionel Sambucbool
12254684ddb6SLionel Sambuc__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
12264684ddb6SLionel Sambuc        _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
12274684ddb6SLionel Sambuc      random_access_iterator_tag, random_access_iterator_tag )
12284684ddb6SLionel Sambuc{
12294684ddb6SLionel Sambuc    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
12304684ddb6SLionel Sambuc        return false;
12314684ddb6SLionel Sambuc    return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
12324684ddb6SLionel Sambuc                        typename add_lvalue_reference<_BinaryPredicate>::type>
12334684ddb6SLionel Sambuc                       (__first1, __last1, __first2, __pred );
12344684ddb6SLionel Sambuc}
12354684ddb6SLionel Sambuc
12364684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
12374684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
12384684ddb6SLionel Sambucbool
12394684ddb6SLionel Sambucequal(_InputIterator1 __first1, _InputIterator1 __last1,
12404684ddb6SLionel Sambuc      _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
12414684ddb6SLionel Sambuc{
12424684ddb6SLionel Sambuc    return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
12434684ddb6SLionel Sambuc       (__first1, __last1, __first2, __last2, __pred,
12444684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator1>::iterator_category(),
12454684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator2>::iterator_category());
12464684ddb6SLionel Sambuc}
12474684ddb6SLionel Sambuc
12484684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
12494684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
12504684ddb6SLionel Sambucbool
12514684ddb6SLionel Sambucequal(_InputIterator1 __first1, _InputIterator1 __last1,
12524684ddb6SLionel Sambuc      _InputIterator2 __first2, _InputIterator2 __last2)
12534684ddb6SLionel Sambuc{
12544684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
12554684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
12564684ddb6SLionel Sambuc    return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
12574684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator1>::iterator_category(),
12584684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator2>::iterator_category());
12594684ddb6SLionel Sambuc}
12604684ddb6SLionel Sambuc#endif
12614684ddb6SLionel Sambuc
12624684ddb6SLionel Sambuc// is_permutation
12634684ddb6SLionel Sambuc
12644684ddb6SLionel Sambuctemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
12654684ddb6SLionel Sambucbool
12664684ddb6SLionel Sambucis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
12674684ddb6SLionel Sambuc               _ForwardIterator2 __first2, _BinaryPredicate __pred)
12684684ddb6SLionel Sambuc{
12694684ddb6SLionel Sambuc    // shorten sequences as much as possible by lopping of any equal parts
1270*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1; ++__first1, (void) ++__first2)
12714684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
12724684ddb6SLionel Sambuc            goto __not_done;
12734684ddb6SLionel Sambuc    return true;
12744684ddb6SLionel Sambuc__not_done:
12754684ddb6SLionel Sambuc    // __first1 != __last1 && *__first1 != *__first2
12764684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
12774684ddb6SLionel Sambuc    _D1 __l1 = _VSTD::distance(__first1, __last1);
12784684ddb6SLionel Sambuc    if (__l1 == _D1(1))
12794684ddb6SLionel Sambuc        return false;
12804684ddb6SLionel Sambuc    _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
12814684ddb6SLionel Sambuc    // For each element in [f1, l1) see if there are the same number of
12824684ddb6SLionel Sambuc    //    equal elements in [f2, l2)
12834684ddb6SLionel Sambuc    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
12844684ddb6SLionel Sambuc    {
12854684ddb6SLionel Sambuc        // Have we already counted the number of *__i in [f1, l1)?
12864684ddb6SLionel Sambuc        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
12874684ddb6SLionel Sambuc            if (__pred(*__j, *__i))
12884684ddb6SLionel Sambuc                goto __next_iter;
12894684ddb6SLionel Sambuc        {
12904684ddb6SLionel Sambuc            // Count number of *__i in [f2, l2)
12914684ddb6SLionel Sambuc            _D1 __c2 = 0;
12924684ddb6SLionel Sambuc            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
12934684ddb6SLionel Sambuc                if (__pred(*__i, *__j))
12944684ddb6SLionel Sambuc                    ++__c2;
12954684ddb6SLionel Sambuc            if (__c2 == 0)
12964684ddb6SLionel Sambuc                return false;
12974684ddb6SLionel Sambuc            // Count number of *__i in [__i, l1) (we can start with 1)
12984684ddb6SLionel Sambuc            _D1 __c1 = 1;
12994684ddb6SLionel Sambuc            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
13004684ddb6SLionel Sambuc                if (__pred(*__i, *__j))
13014684ddb6SLionel Sambuc                    ++__c1;
13024684ddb6SLionel Sambuc            if (__c1 != __c2)
13034684ddb6SLionel Sambuc                return false;
13044684ddb6SLionel Sambuc        }
13054684ddb6SLionel Sambuc__next_iter:;
13064684ddb6SLionel Sambuc    }
13074684ddb6SLionel Sambuc    return true;
13084684ddb6SLionel Sambuc}
13094684ddb6SLionel Sambuc
13104684ddb6SLionel Sambuctemplate<class _ForwardIterator1, class _ForwardIterator2>
13114684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
13124684ddb6SLionel Sambucbool
13134684ddb6SLionel Sambucis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
13144684ddb6SLionel Sambuc               _ForwardIterator2 __first2)
13154684ddb6SLionel Sambuc{
13164684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
13174684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
13184684ddb6SLionel Sambuc    return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
13194684ddb6SLionel Sambuc}
13204684ddb6SLionel Sambuc
13214684ddb6SLionel Sambuc#if _LIBCPP_STD_VER > 11
13224684ddb6SLionel Sambuctemplate<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
13234684ddb6SLionel Sambucbool
13244684ddb6SLionel Sambuc__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
13254684ddb6SLionel Sambuc                 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
13264684ddb6SLionel Sambuc                 _BinaryPredicate __pred,
13274684ddb6SLionel Sambuc                 forward_iterator_tag, forward_iterator_tag )
13284684ddb6SLionel Sambuc{
13294684ddb6SLionel Sambuc    // shorten sequences as much as possible by lopping of any equal parts
1330*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
13314684ddb6SLionel Sambuc        if (!__pred(*__first1, *__first2))
13324684ddb6SLionel Sambuc            goto __not_done;
13334684ddb6SLionel Sambuc    return __first1 == __last1 && __first2 == __last2;
13344684ddb6SLionel Sambuc__not_done:
13354684ddb6SLionel Sambuc    // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
13364684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
13374684ddb6SLionel Sambuc    _D1 __l1 = _VSTD::distance(__first1, __last1);
13384684ddb6SLionel Sambuc
13394684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
13404684ddb6SLionel Sambuc    _D2 __l2 = _VSTD::distance(__first2, __last2);
13414684ddb6SLionel Sambuc    if (__l1 != __l2)
13424684ddb6SLionel Sambuc        return false;
13434684ddb6SLionel Sambuc
13444684ddb6SLionel Sambuc    // For each element in [f1, l1) see if there are the same number of
13454684ddb6SLionel Sambuc    //    equal elements in [f2, l2)
13464684ddb6SLionel Sambuc    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
13474684ddb6SLionel Sambuc    {
13484684ddb6SLionel Sambuc        // Have we already counted the number of *__i in [f1, l1)?
13494684ddb6SLionel Sambuc        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
13504684ddb6SLionel Sambuc            if (__pred(*__j, *__i))
13514684ddb6SLionel Sambuc                goto __next_iter;
13524684ddb6SLionel Sambuc        {
13534684ddb6SLionel Sambuc            // Count number of *__i in [f2, l2)
13544684ddb6SLionel Sambuc            _D1 __c2 = 0;
13554684ddb6SLionel Sambuc            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
13564684ddb6SLionel Sambuc                if (__pred(*__i, *__j))
13574684ddb6SLionel Sambuc                    ++__c2;
13584684ddb6SLionel Sambuc            if (__c2 == 0)
13594684ddb6SLionel Sambuc                return false;
13604684ddb6SLionel Sambuc            // Count number of *__i in [__i, l1) (we can start with 1)
13614684ddb6SLionel Sambuc            _D1 __c1 = 1;
13624684ddb6SLionel Sambuc            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
13634684ddb6SLionel Sambuc                if (__pred(*__i, *__j))
13644684ddb6SLionel Sambuc                    ++__c1;
13654684ddb6SLionel Sambuc            if (__c1 != __c2)
13664684ddb6SLionel Sambuc                return false;
13674684ddb6SLionel Sambuc        }
13684684ddb6SLionel Sambuc__next_iter:;
13694684ddb6SLionel Sambuc    }
13704684ddb6SLionel Sambuc    return true;
13714684ddb6SLionel Sambuc}
13724684ddb6SLionel Sambuc
13734684ddb6SLionel Sambuctemplate<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
13744684ddb6SLionel Sambucbool
13754684ddb6SLionel Sambuc__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
13764684ddb6SLionel Sambuc               _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
13774684ddb6SLionel Sambuc               _BinaryPredicate __pred,
13784684ddb6SLionel Sambuc               random_access_iterator_tag, random_access_iterator_tag )
13794684ddb6SLionel Sambuc{
13804684ddb6SLionel Sambuc    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
13814684ddb6SLionel Sambuc        return false;
13824684ddb6SLionel Sambuc    return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
13834684ddb6SLionel Sambuc                                 typename add_lvalue_reference<_BinaryPredicate>::type>
13844684ddb6SLionel Sambuc                                (__first1, __last1, __first2, __pred );
13854684ddb6SLionel Sambuc}
13864684ddb6SLionel Sambuc
13874684ddb6SLionel Sambuctemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
13884684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
13894684ddb6SLionel Sambucbool
13904684ddb6SLionel Sambucis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
13914684ddb6SLionel Sambuc               _ForwardIterator2 __first2, _ForwardIterator2 __last2,
13924684ddb6SLionel Sambuc               _BinaryPredicate __pred )
13934684ddb6SLionel Sambuc{
13944684ddb6SLionel Sambuc    return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
13954684ddb6SLionel Sambuc       (__first1, __last1, __first2, __last2, __pred,
13964684ddb6SLionel Sambuc        typename iterator_traits<_ForwardIterator1>::iterator_category(),
13974684ddb6SLionel Sambuc        typename iterator_traits<_ForwardIterator2>::iterator_category());
13984684ddb6SLionel Sambuc}
13994684ddb6SLionel Sambuc
14004684ddb6SLionel Sambuctemplate<class _ForwardIterator1, class _ForwardIterator2>
14014684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
14024684ddb6SLionel Sambucbool
14034684ddb6SLionel Sambucis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
14044684ddb6SLionel Sambuc               _ForwardIterator2 __first2, _ForwardIterator2 __last2)
14054684ddb6SLionel Sambuc{
14064684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
14074684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
14084684ddb6SLionel Sambuc    return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
14094684ddb6SLionel Sambuc        __equal_to<__v1, __v2>(),
14104684ddb6SLionel Sambuc        typename iterator_traits<_ForwardIterator1>::iterator_category(),
14114684ddb6SLionel Sambuc        typename iterator_traits<_ForwardIterator2>::iterator_category());
14124684ddb6SLionel Sambuc}
14134684ddb6SLionel Sambuc#endif
14144684ddb6SLionel Sambuc
14154684ddb6SLionel Sambuc// search
14164684ddb6SLionel Sambuc
14174684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
14184684ddb6SLionel Sambuc_ForwardIterator1
14194684ddb6SLionel Sambuc__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
14204684ddb6SLionel Sambuc         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
14214684ddb6SLionel Sambuc         forward_iterator_tag, forward_iterator_tag)
14224684ddb6SLionel Sambuc{
14234684ddb6SLionel Sambuc    if (__first2 == __last2)
14244684ddb6SLionel Sambuc        return __first1;  // Everything matches an empty sequence
14254684ddb6SLionel Sambuc    while (true)
14264684ddb6SLionel Sambuc    {
14274684ddb6SLionel Sambuc        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
14284684ddb6SLionel Sambuc        while (true)
14294684ddb6SLionel Sambuc        {
14304684ddb6SLionel Sambuc            if (__first1 == __last1)  // return __last1 if no element matches *__first2
14314684ddb6SLionel Sambuc                return __last1;
14324684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
14334684ddb6SLionel Sambuc                break;
14344684ddb6SLionel Sambuc            ++__first1;
14354684ddb6SLionel Sambuc        }
14364684ddb6SLionel Sambuc        // *__first1 matches *__first2, now match elements after here
14374684ddb6SLionel Sambuc        _ForwardIterator1 __m1 = __first1;
14384684ddb6SLionel Sambuc        _ForwardIterator2 __m2 = __first2;
14394684ddb6SLionel Sambuc        while (true)
14404684ddb6SLionel Sambuc        {
14414684ddb6SLionel Sambuc            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
14424684ddb6SLionel Sambuc                return __first1;
14434684ddb6SLionel Sambuc            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
14444684ddb6SLionel Sambuc                return __last1;
14454684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
14464684ddb6SLionel Sambuc            {
14474684ddb6SLionel Sambuc                ++__first1;
14484684ddb6SLionel Sambuc                break;
14494684ddb6SLionel Sambuc            }  // else there is a match, check next elements
14504684ddb6SLionel Sambuc        }
14514684ddb6SLionel Sambuc    }
14524684ddb6SLionel Sambuc}
14534684ddb6SLionel Sambuc
14544684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1455*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
14564684ddb6SLionel Sambuc__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
14574684ddb6SLionel Sambuc           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
14584684ddb6SLionel Sambuc           random_access_iterator_tag, random_access_iterator_tag)
14594684ddb6SLionel Sambuc{
14604684ddb6SLionel Sambuc    typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
14614684ddb6SLionel Sambuc    typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
14624684ddb6SLionel Sambuc    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
14634684ddb6SLionel Sambuc    _D2 __len2 = __last2 - __first2;
14644684ddb6SLionel Sambuc    if (__len2 == 0)
14654684ddb6SLionel Sambuc        return __first1;
14664684ddb6SLionel Sambuc    _D1 __len1 = __last1 - __first1;
14674684ddb6SLionel Sambuc    if (__len1 < __len2)
14684684ddb6SLionel Sambuc        return __last1;
14694684ddb6SLionel Sambuc    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
14704684ddb6SLionel Sambuc    while (true)
14714684ddb6SLionel Sambuc    {
14724684ddb6SLionel Sambuc#if !_LIBCPP_UNROLL_LOOPS
14734684ddb6SLionel Sambuc        while (true)
14744684ddb6SLionel Sambuc        {
14754684ddb6SLionel Sambuc            if (__first1 == __s)
14764684ddb6SLionel Sambuc                return __last1;
14774684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
14784684ddb6SLionel Sambuc                break;
14794684ddb6SLionel Sambuc            ++__first1;
14804684ddb6SLionel Sambuc        }
14814684ddb6SLionel Sambuc#else  // !_LIBCPP_UNROLL_LOOPS
14824684ddb6SLionel Sambuc        for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
14834684ddb6SLionel Sambuc        {
14844684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
14854684ddb6SLionel Sambuc                goto __phase2;
14864684ddb6SLionel Sambuc            if (__pred(*++__first1, *__first2))
14874684ddb6SLionel Sambuc                goto __phase2;
14884684ddb6SLionel Sambuc            if (__pred(*++__first1, *__first2))
14894684ddb6SLionel Sambuc                goto __phase2;
14904684ddb6SLionel Sambuc            if (__pred(*++__first1, *__first2))
14914684ddb6SLionel Sambuc                goto __phase2;
14924684ddb6SLionel Sambuc            ++__first1;
14934684ddb6SLionel Sambuc        }
14944684ddb6SLionel Sambuc        switch (__s - __first1)
14954684ddb6SLionel Sambuc        {
14964684ddb6SLionel Sambuc        case 3:
14974684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
14984684ddb6SLionel Sambuc                break;
14994684ddb6SLionel Sambuc            ++__first1;
15004684ddb6SLionel Sambuc        case 2:
15014684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
15024684ddb6SLionel Sambuc                break;
15034684ddb6SLionel Sambuc            ++__first1;
15044684ddb6SLionel Sambuc        case 1:
15054684ddb6SLionel Sambuc            if (__pred(*__first1, *__first2))
15064684ddb6SLionel Sambuc                break;
15074684ddb6SLionel Sambuc        case 0:
15084684ddb6SLionel Sambuc            return __last1;
15094684ddb6SLionel Sambuc        }
15104684ddb6SLionel Sambuc    __phase2:
15114684ddb6SLionel Sambuc#endif  // !_LIBCPP_UNROLL_LOOPS
15124684ddb6SLionel Sambuc        _RandomAccessIterator1 __m1 = __first1;
15134684ddb6SLionel Sambuc        _RandomAccessIterator2 __m2 = __first2;
15144684ddb6SLionel Sambuc#if !_LIBCPP_UNROLL_LOOPS
15154684ddb6SLionel Sambuc         while (true)
15164684ddb6SLionel Sambuc         {
15174684ddb6SLionel Sambuc             if (++__m2 == __last2)
15184684ddb6SLionel Sambuc                 return __first1;
15194684ddb6SLionel Sambuc             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
15204684ddb6SLionel Sambuc             if (!__pred(*__m1, *__m2))
15214684ddb6SLionel Sambuc             {
15224684ddb6SLionel Sambuc                 ++__first1;
15234684ddb6SLionel Sambuc                 break;
15244684ddb6SLionel Sambuc             }
15254684ddb6SLionel Sambuc         }
15264684ddb6SLionel Sambuc#else  // !_LIBCPP_UNROLL_LOOPS
15274684ddb6SLionel Sambuc        ++__m2;
15284684ddb6SLionel Sambuc        ++__m1;
15294684ddb6SLionel Sambuc        for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
15304684ddb6SLionel Sambuc        {
15314684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))
15324684ddb6SLionel Sambuc                goto __continue;
15334684ddb6SLionel Sambuc            if (!__pred(*++__m1, *++__m2))
15344684ddb6SLionel Sambuc                goto __continue;
15354684ddb6SLionel Sambuc            if (!__pred(*++__m1, *++__m2))
15364684ddb6SLionel Sambuc                goto __continue;
15374684ddb6SLionel Sambuc            if (!__pred(*++__m1, *++__m2))
15384684ddb6SLionel Sambuc                goto __continue;
15394684ddb6SLionel Sambuc            ++__m1;
15404684ddb6SLionel Sambuc            ++__m2;
15414684ddb6SLionel Sambuc        }
15424684ddb6SLionel Sambuc        switch (__last2 - __m2)
15434684ddb6SLionel Sambuc        {
15444684ddb6SLionel Sambuc        case 3:
15454684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))
15464684ddb6SLionel Sambuc                break;
15474684ddb6SLionel Sambuc            ++__m1;
15484684ddb6SLionel Sambuc            ++__m2;
15494684ddb6SLionel Sambuc        case 2:
15504684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))
15514684ddb6SLionel Sambuc                break;
15524684ddb6SLionel Sambuc            ++__m1;
15534684ddb6SLionel Sambuc            ++__m2;
15544684ddb6SLionel Sambuc        case 1:
15554684ddb6SLionel Sambuc            if (!__pred(*__m1, *__m2))
15564684ddb6SLionel Sambuc                break;
15574684ddb6SLionel Sambuc        case 0:
15584684ddb6SLionel Sambuc            return __first1;
15594684ddb6SLionel Sambuc        }
15604684ddb6SLionel Sambuc    __continue:
15614684ddb6SLionel Sambuc        ++__first1;
15624684ddb6SLionel Sambuc#endif  // !_LIBCPP_UNROLL_LOOPS
15634684ddb6SLionel Sambuc    }
15644684ddb6SLionel Sambuc}
15654684ddb6SLionel Sambuc
15664684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
15674684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
15684684ddb6SLionel Sambuc_ForwardIterator1
15694684ddb6SLionel Sambucsearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
15704684ddb6SLionel Sambuc       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
15714684ddb6SLionel Sambuc{
15724684ddb6SLionel Sambuc    return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
15734684ddb6SLionel Sambuc                         (__first1, __last1, __first2, __last2, __pred,
15744684ddb6SLionel Sambuc                          typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
15754684ddb6SLionel Sambuc                          typename std::iterator_traits<_ForwardIterator2>::iterator_category());
15764684ddb6SLionel Sambuc}
15774684ddb6SLionel Sambuc
15784684ddb6SLionel Sambuctemplate <class _ForwardIterator1, class _ForwardIterator2>
15794684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
15804684ddb6SLionel Sambuc_ForwardIterator1
15814684ddb6SLionel Sambucsearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
15824684ddb6SLionel Sambuc       _ForwardIterator2 __first2, _ForwardIterator2 __last2)
15834684ddb6SLionel Sambuc{
15844684ddb6SLionel Sambuc    typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
15854684ddb6SLionel Sambuc    typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
15864684ddb6SLionel Sambuc    return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
15874684ddb6SLionel Sambuc}
15884684ddb6SLionel Sambuc
15894684ddb6SLionel Sambuc// search_n
15904684ddb6SLionel Sambuc
15914684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
15924684ddb6SLionel Sambuc_ForwardIterator
15934684ddb6SLionel Sambuc__search_n(_ForwardIterator __first, _ForwardIterator __last,
15944684ddb6SLionel Sambuc           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
15954684ddb6SLionel Sambuc{
15964684ddb6SLionel Sambuc    if (__count <= 0)
15974684ddb6SLionel Sambuc        return __first;
15984684ddb6SLionel Sambuc    while (true)
15994684ddb6SLionel Sambuc    {
16004684ddb6SLionel Sambuc        // Find first element in sequence that matchs __value_, with a mininum of loop checks
16014684ddb6SLionel Sambuc        while (true)
16024684ddb6SLionel Sambuc        {
16034684ddb6SLionel Sambuc            if (__first == __last)  // return __last if no element matches __value_
16044684ddb6SLionel Sambuc                return __last;
16054684ddb6SLionel Sambuc            if (__pred(*__first, __value_))
16064684ddb6SLionel Sambuc                break;
16074684ddb6SLionel Sambuc            ++__first;
16084684ddb6SLionel Sambuc        }
16094684ddb6SLionel Sambuc        // *__first matches __value_, now match elements after here
16104684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
16114684ddb6SLionel Sambuc        _Size __c(0);
16124684ddb6SLionel Sambuc        while (true)
16134684ddb6SLionel Sambuc        {
16144684ddb6SLionel Sambuc            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
16154684ddb6SLionel Sambuc                return __first;
16164684ddb6SLionel Sambuc            if (++__m == __last)  // Otherwise if source exhaused, pattern not found
16174684ddb6SLionel Sambuc                return __last;
16184684ddb6SLionel Sambuc            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
16194684ddb6SLionel Sambuc            {
16204684ddb6SLionel Sambuc                __first = __m;
16214684ddb6SLionel Sambuc                ++__first;
16224684ddb6SLionel Sambuc                break;
16234684ddb6SLionel Sambuc            }  // else there is a match, check next elements
16244684ddb6SLionel Sambuc        }
16254684ddb6SLionel Sambuc    }
16264684ddb6SLionel Sambuc}
16274684ddb6SLionel Sambuc
16284684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
16294684ddb6SLionel Sambuc_RandomAccessIterator
16304684ddb6SLionel Sambuc__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
16314684ddb6SLionel Sambuc           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
16324684ddb6SLionel Sambuc{
16334684ddb6SLionel Sambuc    if (__count <= 0)
16344684ddb6SLionel Sambuc        return __first;
16354684ddb6SLionel Sambuc    _Size __len = static_cast<_Size>(__last - __first);
16364684ddb6SLionel Sambuc    if (__len < __count)
16374684ddb6SLionel Sambuc        return __last;
16384684ddb6SLionel Sambuc    const _RandomAccessIterator __s = __last - (__count - 1);  // Start of pattern match can't go beyond here
16394684ddb6SLionel Sambuc    while (true)
16404684ddb6SLionel Sambuc    {
16414684ddb6SLionel Sambuc        // Find first element in sequence that matchs __value_, with a mininum of loop checks
16424684ddb6SLionel Sambuc        while (true)
16434684ddb6SLionel Sambuc        {
16444684ddb6SLionel Sambuc            if (__first >= __s)  // return __last if no element matches __value_
16454684ddb6SLionel Sambuc                return __last;
16464684ddb6SLionel Sambuc            if (__pred(*__first, __value_))
16474684ddb6SLionel Sambuc                break;
16484684ddb6SLionel Sambuc            ++__first;
16494684ddb6SLionel Sambuc        }
16504684ddb6SLionel Sambuc        // *__first matches __value_, now match elements after here
16514684ddb6SLionel Sambuc        _RandomAccessIterator __m = __first;
16524684ddb6SLionel Sambuc        _Size __c(0);
16534684ddb6SLionel Sambuc        while (true)
16544684ddb6SLionel Sambuc        {
16554684ddb6SLionel Sambuc            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
16564684ddb6SLionel Sambuc                return __first;
16574684ddb6SLionel Sambuc             ++__m;          // no need to check range on __m because __s guarantees we have enough source
16584684ddb6SLionel Sambuc            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
16594684ddb6SLionel Sambuc            {
16604684ddb6SLionel Sambuc                __first = __m;
16614684ddb6SLionel Sambuc                ++__first;
16624684ddb6SLionel Sambuc                break;
16634684ddb6SLionel Sambuc            }  // else there is a match, check next elements
16644684ddb6SLionel Sambuc        }
16654684ddb6SLionel Sambuc    }
16664684ddb6SLionel Sambuc}
16674684ddb6SLionel Sambuc
16684684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
16694684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
16704684ddb6SLionel Sambuc_ForwardIterator
16714684ddb6SLionel Sambucsearch_n(_ForwardIterator __first, _ForwardIterator __last,
16724684ddb6SLionel Sambuc         _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
16734684ddb6SLionel Sambuc{
16744684ddb6SLionel Sambuc    return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1675*0a6a1f1dSLionel Sambuc           (__first, __last, __convert_to_integral(__count), __value_, __pred,
1676*0a6a1f1dSLionel Sambuc           typename iterator_traits<_ForwardIterator>::iterator_category());
16774684ddb6SLionel Sambuc}
16784684ddb6SLionel Sambuc
16794684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Size, class _Tp>
16804684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
16814684ddb6SLionel Sambuc_ForwardIterator
16824684ddb6SLionel Sambucsearch_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
16834684ddb6SLionel Sambuc{
16844684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1685*0a6a1f1dSLionel Sambuc    return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1686*0a6a1f1dSLionel Sambuc                           __value_, __equal_to<__v, _Tp>());
16874684ddb6SLionel Sambuc}
16884684ddb6SLionel Sambuc
16894684ddb6SLionel Sambuc// copy
16904684ddb6SLionel Sambuc
16914684ddb6SLionel Sambuctemplate <class _Iter>
16924684ddb6SLionel Sambucstruct __libcpp_is_trivial_iterator
16934684ddb6SLionel Sambuc{
16944684ddb6SLionel Sambuc    static const bool value = is_pointer<_Iter>::value;
16954684ddb6SLionel Sambuc};
16964684ddb6SLionel Sambuc
16974684ddb6SLionel Sambuctemplate <class _Iter>
16984684ddb6SLionel Sambucstruct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
16994684ddb6SLionel Sambuc{
17004684ddb6SLionel Sambuc    static const bool value = is_pointer<_Iter>::value;
17014684ddb6SLionel Sambuc};
17024684ddb6SLionel Sambuc
17034684ddb6SLionel Sambuctemplate <class _Iter>
17044684ddb6SLionel Sambucstruct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
17054684ddb6SLionel Sambuc{
17064684ddb6SLionel Sambuc    static const bool value = is_pointer<_Iter>::value;
17074684ddb6SLionel Sambuc};
17084684ddb6SLionel Sambuc
17094684ddb6SLionel Sambuctemplate <class _Iter>
17104684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17114684ddb6SLionel Sambuc_Iter
17124684ddb6SLionel Sambuc__unwrap_iter(_Iter __i)
17134684ddb6SLionel Sambuc{
17144684ddb6SLionel Sambuc    return __i;
17154684ddb6SLionel Sambuc}
17164684ddb6SLionel Sambuc
17174684ddb6SLionel Sambuctemplate <class _Tp>
17184684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17194684ddb6SLionel Sambuctypename enable_if
17204684ddb6SLionel Sambuc<
17214684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Tp>::value,
17224684ddb6SLionel Sambuc    _Tp*
17234684ddb6SLionel Sambuc>::type
17244684ddb6SLionel Sambuc__unwrap_iter(move_iterator<_Tp*> __i)
17254684ddb6SLionel Sambuc{
17264684ddb6SLionel Sambuc    return __i.base();
17274684ddb6SLionel Sambuc}
17284684ddb6SLionel Sambuc
17294684ddb6SLionel Sambuc#if _LIBCPP_DEBUG_LEVEL < 2
17304684ddb6SLionel Sambuc
17314684ddb6SLionel Sambuctemplate <class _Tp>
17324684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17334684ddb6SLionel Sambuctypename enable_if
17344684ddb6SLionel Sambuc<
17354684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Tp>::value,
17364684ddb6SLionel Sambuc    _Tp*
17374684ddb6SLionel Sambuc>::type
17384684ddb6SLionel Sambuc__unwrap_iter(__wrap_iter<_Tp*> __i)
17394684ddb6SLionel Sambuc{
17404684ddb6SLionel Sambuc    return __i.base();
17414684ddb6SLionel Sambuc}
17424684ddb6SLionel Sambuc
17434684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG_LEVEL < 2
17444684ddb6SLionel Sambuc
17454684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
17464684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17474684ddb6SLionel Sambuc_OutputIterator
17484684ddb6SLionel Sambuc__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
17494684ddb6SLionel Sambuc{
1750*0a6a1f1dSLionel Sambuc    for (; __first != __last; ++__first, (void) ++__result)
17514684ddb6SLionel Sambuc        *__result = *__first;
17524684ddb6SLionel Sambuc    return __result;
17534684ddb6SLionel Sambuc}
17544684ddb6SLionel Sambuc
17554684ddb6SLionel Sambuctemplate <class _Tp, class _Up>
17564684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17574684ddb6SLionel Sambuctypename enable_if
17584684ddb6SLionel Sambuc<
17594684ddb6SLionel Sambuc    is_same<typename remove_const<_Tp>::type, _Up>::value &&
17604684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Up>::value,
17614684ddb6SLionel Sambuc    _Up*
17624684ddb6SLionel Sambuc>::type
17634684ddb6SLionel Sambuc__copy(_Tp* __first, _Tp* __last, _Up* __result)
17644684ddb6SLionel Sambuc{
17654684ddb6SLionel Sambuc    const size_t __n = static_cast<size_t>(__last - __first);
1766*0a6a1f1dSLionel Sambuc    if (__n > 0)
17674684ddb6SLionel Sambuc        _VSTD::memmove(__result, __first, __n * sizeof(_Up));
17684684ddb6SLionel Sambuc    return __result + __n;
17694684ddb6SLionel Sambuc}
17704684ddb6SLionel Sambuc
17714684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
17724684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17734684ddb6SLionel Sambuc_OutputIterator
17744684ddb6SLionel Sambuccopy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
17754684ddb6SLionel Sambuc{
17764684ddb6SLionel Sambuc    return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
17774684ddb6SLionel Sambuc}
17784684ddb6SLionel Sambuc
17794684ddb6SLionel Sambuc// copy_backward
17804684ddb6SLionel Sambuc
17814684ddb6SLionel Sambuctemplate <class _BidirectionalIterator, class _OutputIterator>
17824684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17834684ddb6SLionel Sambuc_OutputIterator
17844684ddb6SLionel Sambuc__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
17854684ddb6SLionel Sambuc{
17864684ddb6SLionel Sambuc    while (__first != __last)
17874684ddb6SLionel Sambuc        *--__result = *--__last;
17884684ddb6SLionel Sambuc    return __result;
17894684ddb6SLionel Sambuc}
17904684ddb6SLionel Sambuc
17914684ddb6SLionel Sambuctemplate <class _Tp, class _Up>
17924684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
17934684ddb6SLionel Sambuctypename enable_if
17944684ddb6SLionel Sambuc<
17954684ddb6SLionel Sambuc    is_same<typename remove_const<_Tp>::type, _Up>::value &&
17964684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Up>::value,
17974684ddb6SLionel Sambuc    _Up*
17984684ddb6SLionel Sambuc>::type
17994684ddb6SLionel Sambuc__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
18004684ddb6SLionel Sambuc{
18014684ddb6SLionel Sambuc    const size_t __n = static_cast<size_t>(__last - __first);
1802*0a6a1f1dSLionel Sambuc    if (__n > 0)
1803*0a6a1f1dSLionel Sambuc    {
18044684ddb6SLionel Sambuc        __result -= __n;
18054684ddb6SLionel Sambuc        _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1806*0a6a1f1dSLionel Sambuc    }
18074684ddb6SLionel Sambuc    return __result;
18084684ddb6SLionel Sambuc}
18094684ddb6SLionel Sambuc
18104684ddb6SLionel Sambuctemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
18114684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18124684ddb6SLionel Sambuc_BidirectionalIterator2
18134684ddb6SLionel Sambuccopy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
18144684ddb6SLionel Sambuc              _BidirectionalIterator2 __result)
18154684ddb6SLionel Sambuc{
18164684ddb6SLionel Sambuc    return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
18174684ddb6SLionel Sambuc}
18184684ddb6SLionel Sambuc
18194684ddb6SLionel Sambuc// copy_if
18204684ddb6SLionel Sambuc
18214684ddb6SLionel Sambuctemplate<class _InputIterator, class _OutputIterator, class _Predicate>
18224684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18234684ddb6SLionel Sambuc_OutputIterator
18244684ddb6SLionel Sambuccopy_if(_InputIterator __first, _InputIterator __last,
18254684ddb6SLionel Sambuc        _OutputIterator __result, _Predicate __pred)
18264684ddb6SLionel Sambuc{
18274684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
18284684ddb6SLionel Sambuc    {
18294684ddb6SLionel Sambuc        if (__pred(*__first))
18304684ddb6SLionel Sambuc        {
18314684ddb6SLionel Sambuc            *__result = *__first;
18324684ddb6SLionel Sambuc            ++__result;
18334684ddb6SLionel Sambuc        }
18344684ddb6SLionel Sambuc    }
18354684ddb6SLionel Sambuc    return __result;
18364684ddb6SLionel Sambuc}
18374684ddb6SLionel Sambuc
18384684ddb6SLionel Sambuc// copy_n
18394684ddb6SLionel Sambuc
18404684ddb6SLionel Sambuctemplate<class _InputIterator, class _Size, class _OutputIterator>
18414684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18424684ddb6SLionel Sambuctypename enable_if
18434684ddb6SLionel Sambuc<
18444684ddb6SLionel Sambuc    __is_input_iterator<_InputIterator>::value &&
18454684ddb6SLionel Sambuc   !__is_random_access_iterator<_InputIterator>::value,
18464684ddb6SLionel Sambuc    _OutputIterator
18474684ddb6SLionel Sambuc>::type
1848*0a6a1f1dSLionel Sambuccopy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
18494684ddb6SLionel Sambuc{
1850*0a6a1f1dSLionel Sambuc    typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1851*0a6a1f1dSLionel Sambuc    _IntegralSize __n = __orig_n;
18524684ddb6SLionel Sambuc    if (__n > 0)
18534684ddb6SLionel Sambuc    {
18544684ddb6SLionel Sambuc        *__result = *__first;
18554684ddb6SLionel Sambuc        ++__result;
18564684ddb6SLionel Sambuc        for (--__n; __n > 0; --__n)
18574684ddb6SLionel Sambuc        {
18584684ddb6SLionel Sambuc            ++__first;
18594684ddb6SLionel Sambuc            *__result = *__first;
18604684ddb6SLionel Sambuc            ++__result;
18614684ddb6SLionel Sambuc        }
18624684ddb6SLionel Sambuc    }
18634684ddb6SLionel Sambuc    return __result;
18644684ddb6SLionel Sambuc}
18654684ddb6SLionel Sambuc
18664684ddb6SLionel Sambuctemplate<class _InputIterator, class _Size, class _OutputIterator>
18674684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18684684ddb6SLionel Sambuctypename enable_if
18694684ddb6SLionel Sambuc<
18704684ddb6SLionel Sambuc    __is_random_access_iterator<_InputIterator>::value,
18714684ddb6SLionel Sambuc    _OutputIterator
18724684ddb6SLionel Sambuc>::type
1873*0a6a1f1dSLionel Sambuccopy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
18744684ddb6SLionel Sambuc{
1875*0a6a1f1dSLionel Sambuc    typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1876*0a6a1f1dSLionel Sambuc    _IntegralSize __n = __orig_n;
18774684ddb6SLionel Sambuc    return _VSTD::copy(__first, __first + __n, __result);
18784684ddb6SLionel Sambuc}
18794684ddb6SLionel Sambuc
18804684ddb6SLionel Sambuc// move
18814684ddb6SLionel Sambuc
18824684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
18834684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18844684ddb6SLionel Sambuc_OutputIterator
18854684ddb6SLionel Sambuc__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
18864684ddb6SLionel Sambuc{
1887*0a6a1f1dSLionel Sambuc    for (; __first != __last; ++__first, (void) ++__result)
18884684ddb6SLionel Sambuc        *__result = _VSTD::move(*__first);
18894684ddb6SLionel Sambuc    return __result;
18904684ddb6SLionel Sambuc}
18914684ddb6SLionel Sambuc
18924684ddb6SLionel Sambuctemplate <class _Tp, class _Up>
18934684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
18944684ddb6SLionel Sambuctypename enable_if
18954684ddb6SLionel Sambuc<
18964684ddb6SLionel Sambuc    is_same<typename remove_const<_Tp>::type, _Up>::value &&
18974684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Up>::value,
18984684ddb6SLionel Sambuc    _Up*
18994684ddb6SLionel Sambuc>::type
19004684ddb6SLionel Sambuc__move(_Tp* __first, _Tp* __last, _Up* __result)
19014684ddb6SLionel Sambuc{
19024684ddb6SLionel Sambuc    const size_t __n = static_cast<size_t>(__last - __first);
1903*0a6a1f1dSLionel Sambuc    if (__n > 0)
19044684ddb6SLionel Sambuc        _VSTD::memmove(__result, __first, __n * sizeof(_Up));
19054684ddb6SLionel Sambuc    return __result + __n;
19064684ddb6SLionel Sambuc}
19074684ddb6SLionel Sambuc
19084684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
19094684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19104684ddb6SLionel Sambuc_OutputIterator
19114684ddb6SLionel Sambucmove(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
19124684ddb6SLionel Sambuc{
19134684ddb6SLionel Sambuc    return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
19144684ddb6SLionel Sambuc}
19154684ddb6SLionel Sambuc
19164684ddb6SLionel Sambuc// move_backward
19174684ddb6SLionel Sambuc
19184684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
19194684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19204684ddb6SLionel Sambuc_OutputIterator
19214684ddb6SLionel Sambuc__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
19224684ddb6SLionel Sambuc{
19234684ddb6SLionel Sambuc    while (__first != __last)
19244684ddb6SLionel Sambuc        *--__result = _VSTD::move(*--__last);
19254684ddb6SLionel Sambuc    return __result;
19264684ddb6SLionel Sambuc}
19274684ddb6SLionel Sambuc
19284684ddb6SLionel Sambuctemplate <class _Tp, class _Up>
19294684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19304684ddb6SLionel Sambuctypename enable_if
19314684ddb6SLionel Sambuc<
19324684ddb6SLionel Sambuc    is_same<typename remove_const<_Tp>::type, _Up>::value &&
19334684ddb6SLionel Sambuc    is_trivially_copy_assignable<_Up>::value,
19344684ddb6SLionel Sambuc    _Up*
19354684ddb6SLionel Sambuc>::type
19364684ddb6SLionel Sambuc__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
19374684ddb6SLionel Sambuc{
19384684ddb6SLionel Sambuc    const size_t __n = static_cast<size_t>(__last - __first);
1939*0a6a1f1dSLionel Sambuc    if (__n > 0)
1940*0a6a1f1dSLionel Sambuc    {
19414684ddb6SLionel Sambuc        __result -= __n;
19424684ddb6SLionel Sambuc        _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1943*0a6a1f1dSLionel Sambuc    }
19444684ddb6SLionel Sambuc    return __result;
19454684ddb6SLionel Sambuc}
19464684ddb6SLionel Sambuc
19474684ddb6SLionel Sambuctemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
19484684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19494684ddb6SLionel Sambuc_BidirectionalIterator2
19504684ddb6SLionel Sambucmove_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
19514684ddb6SLionel Sambuc              _BidirectionalIterator2 __result)
19524684ddb6SLionel Sambuc{
19534684ddb6SLionel Sambuc    return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
19544684ddb6SLionel Sambuc}
19554684ddb6SLionel Sambuc
19564684ddb6SLionel Sambuc// iter_swap
19574684ddb6SLionel Sambuc
19584684ddb6SLionel Sambuc// moved to <type_traits> for better swap / noexcept support
19594684ddb6SLionel Sambuc
19604684ddb6SLionel Sambuc// transform
19614684ddb6SLionel Sambuc
19624684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _UnaryOperation>
19634684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19644684ddb6SLionel Sambuc_OutputIterator
19654684ddb6SLionel Sambuctransform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
19664684ddb6SLionel Sambuc{
1967*0a6a1f1dSLionel Sambuc    for (; __first != __last; ++__first, (void) ++__result)
19684684ddb6SLionel Sambuc        *__result = __op(*__first);
19694684ddb6SLionel Sambuc    return __result;
19704684ddb6SLionel Sambuc}
19714684ddb6SLionel Sambuc
19724684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
19734684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19744684ddb6SLionel Sambuc_OutputIterator
19754684ddb6SLionel Sambuctransform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
19764684ddb6SLionel Sambuc          _OutputIterator __result, _BinaryOperation __binary_op)
19774684ddb6SLionel Sambuc{
1978*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
19794684ddb6SLionel Sambuc        *__result = __binary_op(*__first1, *__first2);
19804684ddb6SLionel Sambuc    return __result;
19814684ddb6SLionel Sambuc}
19824684ddb6SLionel Sambuc
19834684ddb6SLionel Sambuc// replace
19844684ddb6SLionel Sambuc
19854684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
19864684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19874684ddb6SLionel Sambucvoid
19884684ddb6SLionel Sambucreplace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
19894684ddb6SLionel Sambuc{
19904684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
19914684ddb6SLionel Sambuc        if (*__first == __old_value)
19924684ddb6SLionel Sambuc            *__first = __new_value;
19934684ddb6SLionel Sambuc}
19944684ddb6SLionel Sambuc
19954684ddb6SLionel Sambuc// replace_if
19964684ddb6SLionel Sambuc
19974684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Predicate, class _Tp>
19984684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
19994684ddb6SLionel Sambucvoid
20004684ddb6SLionel Sambucreplace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
20014684ddb6SLionel Sambuc{
20024684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
20034684ddb6SLionel Sambuc        if (__pred(*__first))
20044684ddb6SLionel Sambuc            *__first = __new_value;
20054684ddb6SLionel Sambuc}
20064684ddb6SLionel Sambuc
20074684ddb6SLionel Sambuc// replace_copy
20084684ddb6SLionel Sambuc
20094684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _Tp>
20104684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20114684ddb6SLionel Sambuc_OutputIterator
20124684ddb6SLionel Sambucreplace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
20134684ddb6SLionel Sambuc             const _Tp& __old_value, const _Tp& __new_value)
20144684ddb6SLionel Sambuc{
2015*0a6a1f1dSLionel Sambuc    for (; __first != __last; ++__first, (void) ++__result)
20164684ddb6SLionel Sambuc        if (*__first == __old_value)
20174684ddb6SLionel Sambuc            *__result = __new_value;
20184684ddb6SLionel Sambuc        else
20194684ddb6SLionel Sambuc            *__result = *__first;
20204684ddb6SLionel Sambuc    return __result;
20214684ddb6SLionel Sambuc}
20224684ddb6SLionel Sambuc
20234684ddb6SLionel Sambuc// replace_copy_if
20244684ddb6SLionel Sambuc
20254684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
20264684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20274684ddb6SLionel Sambuc_OutputIterator
20284684ddb6SLionel Sambucreplace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
20294684ddb6SLionel Sambuc                _Predicate __pred, const _Tp& __new_value)
20304684ddb6SLionel Sambuc{
2031*0a6a1f1dSLionel Sambuc    for (; __first != __last; ++__first, (void) ++__result)
20324684ddb6SLionel Sambuc        if (__pred(*__first))
20334684ddb6SLionel Sambuc            *__result = __new_value;
20344684ddb6SLionel Sambuc        else
20354684ddb6SLionel Sambuc            *__result = *__first;
20364684ddb6SLionel Sambuc    return __result;
20374684ddb6SLionel Sambuc}
20384684ddb6SLionel Sambuc
20394684ddb6SLionel Sambuc// fill_n
20404684ddb6SLionel Sambuc
20414684ddb6SLionel Sambuctemplate <class _OutputIterator, class _Size, class _Tp>
20424684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20434684ddb6SLionel Sambuc_OutputIterator
20444684ddb6SLionel Sambuc__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
20454684ddb6SLionel Sambuc{
2046*0a6a1f1dSLionel Sambuc    for (; __n > 0; ++__first, (void) --__n)
20474684ddb6SLionel Sambuc        *__first = __value_;
20484684ddb6SLionel Sambuc    return __first;
20494684ddb6SLionel Sambuc}
20504684ddb6SLionel Sambuc
20514684ddb6SLionel Sambuctemplate <class _Tp, class _Size, class _Up>
20524684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20534684ddb6SLionel Sambuctypename enable_if
20544684ddb6SLionel Sambuc<
20554684ddb6SLionel Sambuc    is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
20564684ddb6SLionel Sambuc    !is_same<_Tp, bool>::value &&
20574684ddb6SLionel Sambuc    is_integral<_Up>::value && sizeof(_Up) == 1,
20584684ddb6SLionel Sambuc    _Tp*
20594684ddb6SLionel Sambuc>::type
20604684ddb6SLionel Sambuc__fill_n(_Tp* __first, _Size __n,_Up __value_)
20614684ddb6SLionel Sambuc{
20624684ddb6SLionel Sambuc    if (__n > 0)
20634684ddb6SLionel Sambuc        _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
20644684ddb6SLionel Sambuc    return __first + __n;
20654684ddb6SLionel Sambuc}
20664684ddb6SLionel Sambuc
20674684ddb6SLionel Sambuctemplate <class _OutputIterator, class _Size, class _Tp>
20684684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20694684ddb6SLionel Sambuc_OutputIterator
20704684ddb6SLionel Sambucfill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
20714684ddb6SLionel Sambuc{
2072*0a6a1f1dSLionel Sambuc   return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
20734684ddb6SLionel Sambuc}
20744684ddb6SLionel Sambuc
20754684ddb6SLionel Sambuc// fill
20764684ddb6SLionel Sambuc
20774684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
20784684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20794684ddb6SLionel Sambucvoid
20804684ddb6SLionel Sambuc__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
20814684ddb6SLionel Sambuc{
20824684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
20834684ddb6SLionel Sambuc        *__first = __value_;
20844684ddb6SLionel Sambuc}
20854684ddb6SLionel Sambuc
20864684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Tp>
20874684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20884684ddb6SLionel Sambucvoid
20894684ddb6SLionel Sambuc__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
20904684ddb6SLionel Sambuc{
20914684ddb6SLionel Sambuc    _VSTD::fill_n(__first, __last - __first, __value_);
20924684ddb6SLionel Sambuc}
20934684ddb6SLionel Sambuc
20944684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
20954684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
20964684ddb6SLionel Sambucvoid
20974684ddb6SLionel Sambucfill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
20984684ddb6SLionel Sambuc{
20994684ddb6SLionel Sambuc    _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
21004684ddb6SLionel Sambuc}
21014684ddb6SLionel Sambuc
21024684ddb6SLionel Sambuc// generate
21034684ddb6SLionel Sambuc
21044684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Generator>
21054684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
21064684ddb6SLionel Sambucvoid
21074684ddb6SLionel Sambucgenerate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
21084684ddb6SLionel Sambuc{
21094684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
21104684ddb6SLionel Sambuc        *__first = __gen();
21114684ddb6SLionel Sambuc}
21124684ddb6SLionel Sambuc
21134684ddb6SLionel Sambuc// generate_n
21144684ddb6SLionel Sambuc
21154684ddb6SLionel Sambuctemplate <class _OutputIterator, class _Size, class _Generator>
21164684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
21174684ddb6SLionel Sambuc_OutputIterator
2118*0a6a1f1dSLionel Sambucgenerate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
21194684ddb6SLionel Sambuc{
2120*0a6a1f1dSLionel Sambuc    typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2121*0a6a1f1dSLionel Sambuc    _IntegralSize __n = __orig_n;
2122*0a6a1f1dSLionel Sambuc    for (; __n > 0; ++__first, (void) --__n)
21234684ddb6SLionel Sambuc        *__first = __gen();
21244684ddb6SLionel Sambuc    return __first;
21254684ddb6SLionel Sambuc}
21264684ddb6SLionel Sambuc
21274684ddb6SLionel Sambuc// remove
21284684ddb6SLionel Sambuc
21294684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
21304684ddb6SLionel Sambuc_ForwardIterator
21314684ddb6SLionel Sambucremove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
21324684ddb6SLionel Sambuc{
21334684ddb6SLionel Sambuc    __first = _VSTD::find(__first, __last, __value_);
21344684ddb6SLionel Sambuc    if (__first != __last)
21354684ddb6SLionel Sambuc    {
21364684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
21374684ddb6SLionel Sambuc        while (++__i != __last)
21384684ddb6SLionel Sambuc        {
21394684ddb6SLionel Sambuc            if (!(*__i == __value_))
21404684ddb6SLionel Sambuc            {
21414684ddb6SLionel Sambuc                *__first = _VSTD::move(*__i);
21424684ddb6SLionel Sambuc                ++__first;
21434684ddb6SLionel Sambuc            }
21444684ddb6SLionel Sambuc        }
21454684ddb6SLionel Sambuc    }
21464684ddb6SLionel Sambuc    return __first;
21474684ddb6SLionel Sambuc}
21484684ddb6SLionel Sambuc
21494684ddb6SLionel Sambuc// remove_if
21504684ddb6SLionel Sambuc
21514684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Predicate>
21524684ddb6SLionel Sambuc_ForwardIterator
21534684ddb6SLionel Sambucremove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
21544684ddb6SLionel Sambuc{
21554684ddb6SLionel Sambuc    __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
21564684ddb6SLionel Sambuc                           (__first, __last, __pred);
21574684ddb6SLionel Sambuc    if (__first != __last)
21584684ddb6SLionel Sambuc    {
21594684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
21604684ddb6SLionel Sambuc        while (++__i != __last)
21614684ddb6SLionel Sambuc        {
21624684ddb6SLionel Sambuc            if (!__pred(*__i))
21634684ddb6SLionel Sambuc            {
21644684ddb6SLionel Sambuc                *__first = _VSTD::move(*__i);
21654684ddb6SLionel Sambuc                ++__first;
21664684ddb6SLionel Sambuc            }
21674684ddb6SLionel Sambuc        }
21684684ddb6SLionel Sambuc    }
21694684ddb6SLionel Sambuc    return __first;
21704684ddb6SLionel Sambuc}
21714684ddb6SLionel Sambuc
21724684ddb6SLionel Sambuc// remove_copy
21734684ddb6SLionel Sambuc
21744684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _Tp>
21754684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
21764684ddb6SLionel Sambuc_OutputIterator
21774684ddb6SLionel Sambucremove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
21784684ddb6SLionel Sambuc{
21794684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
21804684ddb6SLionel Sambuc    {
21814684ddb6SLionel Sambuc        if (!(*__first == __value_))
21824684ddb6SLionel Sambuc        {
21834684ddb6SLionel Sambuc            *__result = *__first;
21844684ddb6SLionel Sambuc            ++__result;
21854684ddb6SLionel Sambuc        }
21864684ddb6SLionel Sambuc    }
21874684ddb6SLionel Sambuc    return __result;
21884684ddb6SLionel Sambuc}
21894684ddb6SLionel Sambuc
21904684ddb6SLionel Sambuc// remove_copy_if
21914684ddb6SLionel Sambuc
21924684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _Predicate>
21934684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
21944684ddb6SLionel Sambuc_OutputIterator
21954684ddb6SLionel Sambucremove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
21964684ddb6SLionel Sambuc{
21974684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
21984684ddb6SLionel Sambuc    {
21994684ddb6SLionel Sambuc        if (!__pred(*__first))
22004684ddb6SLionel Sambuc        {
22014684ddb6SLionel Sambuc            *__result = *__first;
22024684ddb6SLionel Sambuc            ++__result;
22034684ddb6SLionel Sambuc        }
22044684ddb6SLionel Sambuc    }
22054684ddb6SLionel Sambuc    return __result;
22064684ddb6SLionel Sambuc}
22074684ddb6SLionel Sambuc
22084684ddb6SLionel Sambuc// unique
22094684ddb6SLionel Sambuc
22104684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _BinaryPredicate>
22114684ddb6SLionel Sambuc_ForwardIterator
22124684ddb6SLionel Sambucunique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
22134684ddb6SLionel Sambuc{
22144684ddb6SLionel Sambuc    __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
22154684ddb6SLionel Sambuc                                 (__first, __last, __pred);
22164684ddb6SLionel Sambuc    if (__first != __last)
22174684ddb6SLionel Sambuc    {
22184684ddb6SLionel Sambuc        // ...  a  a  ?  ...
22194684ddb6SLionel Sambuc        //      f     i
22204684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
22214684ddb6SLionel Sambuc        for (++__i; ++__i != __last;)
22224684ddb6SLionel Sambuc            if (!__pred(*__first, *__i))
22234684ddb6SLionel Sambuc                *++__first = _VSTD::move(*__i);
22244684ddb6SLionel Sambuc        ++__first;
22254684ddb6SLionel Sambuc    }
22264684ddb6SLionel Sambuc    return __first;
22274684ddb6SLionel Sambuc}
22284684ddb6SLionel Sambuc
22294684ddb6SLionel Sambuctemplate <class _ForwardIterator>
22304684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
22314684ddb6SLionel Sambuc_ForwardIterator
22324684ddb6SLionel Sambucunique(_ForwardIterator __first, _ForwardIterator __last)
22334684ddb6SLionel Sambuc{
22344684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
22354684ddb6SLionel Sambuc    return _VSTD::unique(__first, __last, __equal_to<__v>());
22364684ddb6SLionel Sambuc}
22374684ddb6SLionel Sambuc
22384684ddb6SLionel Sambuc// unique_copy
22394684ddb6SLionel Sambuc
22404684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
22414684ddb6SLionel Sambuc_OutputIterator
22424684ddb6SLionel Sambuc__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
22434684ddb6SLionel Sambuc              input_iterator_tag, output_iterator_tag)
22444684ddb6SLionel Sambuc{
22454684ddb6SLionel Sambuc    if (__first != __last)
22464684ddb6SLionel Sambuc    {
22474684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator>::value_type __t(*__first);
22484684ddb6SLionel Sambuc        *__result = __t;
22494684ddb6SLionel Sambuc        ++__result;
22504684ddb6SLionel Sambuc        while (++__first != __last)
22514684ddb6SLionel Sambuc        {
22524684ddb6SLionel Sambuc            if (!__pred(__t, *__first))
22534684ddb6SLionel Sambuc            {
22544684ddb6SLionel Sambuc                __t = *__first;
22554684ddb6SLionel Sambuc                *__result = __t;
22564684ddb6SLionel Sambuc                ++__result;
22574684ddb6SLionel Sambuc            }
22584684ddb6SLionel Sambuc        }
22594684ddb6SLionel Sambuc    }
22604684ddb6SLionel Sambuc    return __result;
22614684ddb6SLionel Sambuc}
22624684ddb6SLionel Sambuc
22634684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
22644684ddb6SLionel Sambuc_OutputIterator
22654684ddb6SLionel Sambuc__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
22664684ddb6SLionel Sambuc              forward_iterator_tag, output_iterator_tag)
22674684ddb6SLionel Sambuc{
22684684ddb6SLionel Sambuc    if (__first != __last)
22694684ddb6SLionel Sambuc    {
22704684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
22714684ddb6SLionel Sambuc        *__result = *__i;
22724684ddb6SLionel Sambuc        ++__result;
22734684ddb6SLionel Sambuc        while (++__first != __last)
22744684ddb6SLionel Sambuc        {
22754684ddb6SLionel Sambuc            if (!__pred(*__i, *__first))
22764684ddb6SLionel Sambuc            {
22774684ddb6SLionel Sambuc                *__result = *__first;
22784684ddb6SLionel Sambuc                ++__result;
22794684ddb6SLionel Sambuc                __i = __first;
22804684ddb6SLionel Sambuc            }
22814684ddb6SLionel Sambuc        }
22824684ddb6SLionel Sambuc    }
22834684ddb6SLionel Sambuc    return __result;
22844684ddb6SLionel Sambuc}
22854684ddb6SLionel Sambuc
22864684ddb6SLionel Sambuctemplate <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
22874684ddb6SLionel Sambuc_ForwardIterator
22884684ddb6SLionel Sambuc__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
22894684ddb6SLionel Sambuc              input_iterator_tag, forward_iterator_tag)
22904684ddb6SLionel Sambuc{
22914684ddb6SLionel Sambuc    if (__first != __last)
22924684ddb6SLionel Sambuc    {
22934684ddb6SLionel Sambuc        *__result = *__first;
22944684ddb6SLionel Sambuc        while (++__first != __last)
22954684ddb6SLionel Sambuc            if (!__pred(*__result, *__first))
22964684ddb6SLionel Sambuc                *++__result = *__first;
22974684ddb6SLionel Sambuc        ++__result;
22984684ddb6SLionel Sambuc    }
22994684ddb6SLionel Sambuc    return __result;
23004684ddb6SLionel Sambuc}
23014684ddb6SLionel Sambuc
23024684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
23034684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23044684ddb6SLionel Sambuc_OutputIterator
23054684ddb6SLionel Sambucunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
23064684ddb6SLionel Sambuc{
23074684ddb6SLionel Sambuc    return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
23084684ddb6SLionel Sambuc                              (__first, __last, __result, __pred,
23094684ddb6SLionel Sambuc                               typename iterator_traits<_InputIterator>::iterator_category(),
23104684ddb6SLionel Sambuc                               typename iterator_traits<_OutputIterator>::iterator_category());
23114684ddb6SLionel Sambuc}
23124684ddb6SLionel Sambuc
23134684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator>
23144684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23154684ddb6SLionel Sambuc_OutputIterator
23164684ddb6SLionel Sambucunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
23174684ddb6SLionel Sambuc{
23184684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator>::value_type __v;
23194684ddb6SLionel Sambuc    return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
23204684ddb6SLionel Sambuc}
23214684ddb6SLionel Sambuc
23224684ddb6SLionel Sambuc// reverse
23234684ddb6SLionel Sambuc
23244684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
23254684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23264684ddb6SLionel Sambucvoid
23274684ddb6SLionel Sambuc__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
23284684ddb6SLionel Sambuc{
23294684ddb6SLionel Sambuc    while (__first != __last)
23304684ddb6SLionel Sambuc    {
23314684ddb6SLionel Sambuc        if (__first == --__last)
23324684ddb6SLionel Sambuc            break;
23334684ddb6SLionel Sambuc        swap(*__first, *__last);
23344684ddb6SLionel Sambuc        ++__first;
23354684ddb6SLionel Sambuc    }
23364684ddb6SLionel Sambuc}
23374684ddb6SLionel Sambuc
23384684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
23394684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23404684ddb6SLionel Sambucvoid
23414684ddb6SLionel Sambuc__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
23424684ddb6SLionel Sambuc{
23434684ddb6SLionel Sambuc    if (__first != __last)
23444684ddb6SLionel Sambuc        for (; __first < --__last; ++__first)
23454684ddb6SLionel Sambuc            swap(*__first, *__last);
23464684ddb6SLionel Sambuc}
23474684ddb6SLionel Sambuc
23484684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
23494684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23504684ddb6SLionel Sambucvoid
23514684ddb6SLionel Sambucreverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
23524684ddb6SLionel Sambuc{
23534684ddb6SLionel Sambuc    _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
23544684ddb6SLionel Sambuc}
23554684ddb6SLionel Sambuc
23564684ddb6SLionel Sambuc// reverse_copy
23574684ddb6SLionel Sambuc
23584684ddb6SLionel Sambuctemplate <class _BidirectionalIterator, class _OutputIterator>
23594684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
23604684ddb6SLionel Sambuc_OutputIterator
23614684ddb6SLionel Sambucreverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
23624684ddb6SLionel Sambuc{
23634684ddb6SLionel Sambuc    for (; __first != __last; ++__result)
23644684ddb6SLionel Sambuc        *__result = *--__last;
23654684ddb6SLionel Sambuc    return __result;
23664684ddb6SLionel Sambuc}
23674684ddb6SLionel Sambuc
23684684ddb6SLionel Sambuc// rotate
23694684ddb6SLionel Sambuc
23704684ddb6SLionel Sambuctemplate <class _ForwardIterator>
23714684ddb6SLionel Sambuc_ForwardIterator
23724684ddb6SLionel Sambuc__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
23734684ddb6SLionel Sambuc{
23744684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
23754684ddb6SLionel Sambuc    value_type __tmp = _VSTD::move(*__first);
23764684ddb6SLionel Sambuc    _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
23774684ddb6SLionel Sambuc    *__lm1 = _VSTD::move(__tmp);
23784684ddb6SLionel Sambuc    return __lm1;
23794684ddb6SLionel Sambuc}
23804684ddb6SLionel Sambuc
23814684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
23824684ddb6SLionel Sambuc_BidirectionalIterator
23834684ddb6SLionel Sambuc__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
23844684ddb6SLionel Sambuc{
23854684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
23864684ddb6SLionel Sambuc    _BidirectionalIterator __lm1 = _VSTD::prev(__last);
23874684ddb6SLionel Sambuc    value_type __tmp = _VSTD::move(*__lm1);
23884684ddb6SLionel Sambuc    _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
23894684ddb6SLionel Sambuc    *__first = _VSTD::move(__tmp);
23904684ddb6SLionel Sambuc    return __fp1;
23914684ddb6SLionel Sambuc}
23924684ddb6SLionel Sambuc
23934684ddb6SLionel Sambuctemplate <class _ForwardIterator>
23944684ddb6SLionel Sambuc_ForwardIterator
23954684ddb6SLionel Sambuc__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
23964684ddb6SLionel Sambuc{
23974684ddb6SLionel Sambuc    _ForwardIterator __i = __middle;
23984684ddb6SLionel Sambuc    while (true)
23994684ddb6SLionel Sambuc    {
24004684ddb6SLionel Sambuc        swap(*__first, *__i);
24014684ddb6SLionel Sambuc        ++__first;
24024684ddb6SLionel Sambuc        if (++__i == __last)
24034684ddb6SLionel Sambuc            break;
24044684ddb6SLionel Sambuc        if (__first == __middle)
24054684ddb6SLionel Sambuc            __middle = __i;
24064684ddb6SLionel Sambuc    }
24074684ddb6SLionel Sambuc    _ForwardIterator __r = __first;
24084684ddb6SLionel Sambuc    if (__first != __middle)
24094684ddb6SLionel Sambuc    {
24104684ddb6SLionel Sambuc        __i = __middle;
24114684ddb6SLionel Sambuc        while (true)
24124684ddb6SLionel Sambuc        {
24134684ddb6SLionel Sambuc            swap(*__first, *__i);
24144684ddb6SLionel Sambuc            ++__first;
24154684ddb6SLionel Sambuc            if (++__i == __last)
24164684ddb6SLionel Sambuc            {
24174684ddb6SLionel Sambuc                if (__first == __middle)
24184684ddb6SLionel Sambuc                    break;
24194684ddb6SLionel Sambuc                __i = __middle;
24204684ddb6SLionel Sambuc            }
24214684ddb6SLionel Sambuc            else if (__first == __middle)
24224684ddb6SLionel Sambuc                __middle = __i;
24234684ddb6SLionel Sambuc        }
24244684ddb6SLionel Sambuc    }
24254684ddb6SLionel Sambuc    return __r;
24264684ddb6SLionel Sambuc}
24274684ddb6SLionel Sambuc
24284684ddb6SLionel Sambuctemplate<typename _Integral>
24294684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
24304684ddb6SLionel Sambuc_Integral
24314684ddb6SLionel Sambuc__gcd(_Integral __x, _Integral __y)
24324684ddb6SLionel Sambuc{
24334684ddb6SLionel Sambuc    do
24344684ddb6SLionel Sambuc    {
24354684ddb6SLionel Sambuc        _Integral __t = __x % __y;
24364684ddb6SLionel Sambuc        __x = __y;
24374684ddb6SLionel Sambuc        __y = __t;
24384684ddb6SLionel Sambuc    } while (__y);
24394684ddb6SLionel Sambuc    return __x;
24404684ddb6SLionel Sambuc}
24414684ddb6SLionel Sambuc
24424684ddb6SLionel Sambuctemplate<typename _RandomAccessIterator>
24434684ddb6SLionel Sambuc_RandomAccessIterator
24444684ddb6SLionel Sambuc__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
24454684ddb6SLionel Sambuc{
24464684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
24474684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
24484684ddb6SLionel Sambuc
24494684ddb6SLionel Sambuc    const difference_type __m1 = __middle - __first;
24504684ddb6SLionel Sambuc    const difference_type __m2 = __last - __middle;
24514684ddb6SLionel Sambuc    if (__m1 == __m2)
24524684ddb6SLionel Sambuc    {
24534684ddb6SLionel Sambuc        _VSTD::swap_ranges(__first, __middle, __middle);
24544684ddb6SLionel Sambuc        return __middle;
24554684ddb6SLionel Sambuc    }
24564684ddb6SLionel Sambuc    const difference_type __g = _VSTD::__gcd(__m1, __m2);
24574684ddb6SLionel Sambuc    for (_RandomAccessIterator __p = __first + __g; __p != __first;)
24584684ddb6SLionel Sambuc    {
24594684ddb6SLionel Sambuc        value_type __t(_VSTD::move(*--__p));
24604684ddb6SLionel Sambuc        _RandomAccessIterator __p1 = __p;
24614684ddb6SLionel Sambuc        _RandomAccessIterator __p2 = __p1 + __m1;
24624684ddb6SLionel Sambuc        do
24634684ddb6SLionel Sambuc        {
24644684ddb6SLionel Sambuc            *__p1 = _VSTD::move(*__p2);
24654684ddb6SLionel Sambuc            __p1 = __p2;
24664684ddb6SLionel Sambuc            const difference_type __d = __last - __p2;
24674684ddb6SLionel Sambuc            if (__m1 < __d)
24684684ddb6SLionel Sambuc                __p2 += __m1;
24694684ddb6SLionel Sambuc            else
24704684ddb6SLionel Sambuc                __p2 = __first + (__m1 - __d);
24714684ddb6SLionel Sambuc        } while (__p2 != __p);
24724684ddb6SLionel Sambuc        *__p1 = _VSTD::move(__t);
24734684ddb6SLionel Sambuc    }
24744684ddb6SLionel Sambuc    return __first + __m2;
24754684ddb6SLionel Sambuc}
24764684ddb6SLionel Sambuc
24774684ddb6SLionel Sambuctemplate <class _ForwardIterator>
24784684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
24794684ddb6SLionel Sambuc_ForwardIterator
24804684ddb6SLionel Sambuc__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
24814684ddb6SLionel Sambuc         _VSTD::forward_iterator_tag)
24824684ddb6SLionel Sambuc{
24834684ddb6SLionel Sambuc    typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
24844684ddb6SLionel Sambuc    if (_VSTD::is_trivially_move_assignable<value_type>::value)
24854684ddb6SLionel Sambuc    {
24864684ddb6SLionel Sambuc        if (_VSTD::next(__first) == __middle)
24874684ddb6SLionel Sambuc            return _VSTD::__rotate_left(__first, __last);
24884684ddb6SLionel Sambuc    }
24894684ddb6SLionel Sambuc    return _VSTD::__rotate_forward(__first, __middle, __last);
24904684ddb6SLionel Sambuc}
24914684ddb6SLionel Sambuc
24924684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
24934684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
24944684ddb6SLionel Sambuc_BidirectionalIterator
24954684ddb6SLionel Sambuc__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
24964684ddb6SLionel Sambuc         _VSTD::bidirectional_iterator_tag)
24974684ddb6SLionel Sambuc{
24984684ddb6SLionel Sambuc    typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
24994684ddb6SLionel Sambuc    if (_VSTD::is_trivially_move_assignable<value_type>::value)
25004684ddb6SLionel Sambuc    {
25014684ddb6SLionel Sambuc        if (_VSTD::next(__first) == __middle)
25024684ddb6SLionel Sambuc            return _VSTD::__rotate_left(__first, __last);
25034684ddb6SLionel Sambuc        if (_VSTD::next(__middle) == __last)
25044684ddb6SLionel Sambuc            return _VSTD::__rotate_right(__first, __last);
25054684ddb6SLionel Sambuc    }
25064684ddb6SLionel Sambuc    return _VSTD::__rotate_forward(__first, __middle, __last);
25074684ddb6SLionel Sambuc}
25084684ddb6SLionel Sambuc
25094684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
25104684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
25114684ddb6SLionel Sambuc_RandomAccessIterator
25124684ddb6SLionel Sambuc__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
25134684ddb6SLionel Sambuc         _VSTD::random_access_iterator_tag)
25144684ddb6SLionel Sambuc{
25154684ddb6SLionel Sambuc    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
25164684ddb6SLionel Sambuc    if (_VSTD::is_trivially_move_assignable<value_type>::value)
25174684ddb6SLionel Sambuc    {
25184684ddb6SLionel Sambuc        if (_VSTD::next(__first) == __middle)
25194684ddb6SLionel Sambuc            return _VSTD::__rotate_left(__first, __last);
25204684ddb6SLionel Sambuc        if (_VSTD::next(__middle) == __last)
25214684ddb6SLionel Sambuc            return _VSTD::__rotate_right(__first, __last);
25224684ddb6SLionel Sambuc        return _VSTD::__rotate_gcd(__first, __middle, __last);
25234684ddb6SLionel Sambuc    }
25244684ddb6SLionel Sambuc    return _VSTD::__rotate_forward(__first, __middle, __last);
25254684ddb6SLionel Sambuc}
25264684ddb6SLionel Sambuc
25274684ddb6SLionel Sambuctemplate <class _ForwardIterator>
25284684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
25294684ddb6SLionel Sambuc_ForwardIterator
25304684ddb6SLionel Sambucrotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
25314684ddb6SLionel Sambuc{
25324684ddb6SLionel Sambuc    if (__first == __middle)
25334684ddb6SLionel Sambuc        return __last;
25344684ddb6SLionel Sambuc    if (__middle == __last)
25354684ddb6SLionel Sambuc        return __first;
25364684ddb6SLionel Sambuc    return _VSTD::__rotate(__first, __middle, __last,
25374684ddb6SLionel Sambuc                           typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
25384684ddb6SLionel Sambuc}
25394684ddb6SLionel Sambuc
25404684ddb6SLionel Sambuc// rotate_copy
25414684ddb6SLionel Sambuc
25424684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _OutputIterator>
25434684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
25444684ddb6SLionel Sambuc_OutputIterator
25454684ddb6SLionel Sambucrotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
25464684ddb6SLionel Sambuc{
25474684ddb6SLionel Sambuc    return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
25484684ddb6SLionel Sambuc}
25494684ddb6SLionel Sambuc
25504684ddb6SLionel Sambuc// min_element
25514684ddb6SLionel Sambuc
25524684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Compare>
2553*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
25544684ddb6SLionel Sambuc_ForwardIterator
25554684ddb6SLionel Sambucmin_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
25564684ddb6SLionel Sambuc{
25574684ddb6SLionel Sambuc    if (__first != __last)
25584684ddb6SLionel Sambuc    {
25594684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
25604684ddb6SLionel Sambuc        while (++__i != __last)
25614684ddb6SLionel Sambuc            if (__comp(*__i, *__first))
25624684ddb6SLionel Sambuc                __first = __i;
25634684ddb6SLionel Sambuc    }
25644684ddb6SLionel Sambuc    return __first;
25654684ddb6SLionel Sambuc}
25664684ddb6SLionel Sambuc
25674684ddb6SLionel Sambuctemplate <class _ForwardIterator>
2568*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
25694684ddb6SLionel Sambuc_ForwardIterator
25704684ddb6SLionel Sambucmin_element(_ForwardIterator __first, _ForwardIterator __last)
25714684ddb6SLionel Sambuc{
25724684ddb6SLionel Sambuc    return _VSTD::min_element(__first, __last,
25734684ddb6SLionel Sambuc              __less<typename iterator_traits<_ForwardIterator>::value_type>());
25744684ddb6SLionel Sambuc}
25754684ddb6SLionel Sambuc
25764684ddb6SLionel Sambuc// min
25774684ddb6SLionel Sambuc
25784684ddb6SLionel Sambuctemplate <class _Tp, class _Compare>
2579*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
25804684ddb6SLionel Sambucconst _Tp&
25814684ddb6SLionel Sambucmin(const _Tp& __a, const _Tp& __b, _Compare __comp)
25824684ddb6SLionel Sambuc{
25834684ddb6SLionel Sambuc    return __comp(__b, __a) ? __b : __a;
25844684ddb6SLionel Sambuc}
25854684ddb6SLionel Sambuc
25864684ddb6SLionel Sambuctemplate <class _Tp>
2587*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
25884684ddb6SLionel Sambucconst _Tp&
25894684ddb6SLionel Sambucmin(const _Tp& __a, const _Tp& __b)
25904684ddb6SLionel Sambuc{
25914684ddb6SLionel Sambuc    return _VSTD::min(__a, __b, __less<_Tp>());
25924684ddb6SLionel Sambuc}
25934684ddb6SLionel Sambuc
25944684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
25954684ddb6SLionel Sambuc
25964684ddb6SLionel Sambuctemplate<class _Tp, class _Compare>
2597*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
25984684ddb6SLionel Sambuc_Tp
25994684ddb6SLionel Sambucmin(initializer_list<_Tp> __t, _Compare __comp)
26004684ddb6SLionel Sambuc{
26014684ddb6SLionel Sambuc    return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
26024684ddb6SLionel Sambuc}
26034684ddb6SLionel Sambuc
26044684ddb6SLionel Sambuctemplate<class _Tp>
2605*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26064684ddb6SLionel Sambuc_Tp
26074684ddb6SLionel Sambucmin(initializer_list<_Tp> __t)
26084684ddb6SLionel Sambuc{
2609*0a6a1f1dSLionel Sambuc    return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
26104684ddb6SLionel Sambuc}
26114684ddb6SLionel Sambuc
26124684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
26134684ddb6SLionel Sambuc
26144684ddb6SLionel Sambuc// max_element
26154684ddb6SLionel Sambuc
26164684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Compare>
2617*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26184684ddb6SLionel Sambuc_ForwardIterator
26194684ddb6SLionel Sambucmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
26204684ddb6SLionel Sambuc{
26214684ddb6SLionel Sambuc    if (__first != __last)
26224684ddb6SLionel Sambuc    {
26234684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
26244684ddb6SLionel Sambuc        while (++__i != __last)
26254684ddb6SLionel Sambuc            if (__comp(*__first, *__i))
26264684ddb6SLionel Sambuc                __first = __i;
26274684ddb6SLionel Sambuc    }
26284684ddb6SLionel Sambuc    return __first;
26294684ddb6SLionel Sambuc}
26304684ddb6SLionel Sambuc
2631*0a6a1f1dSLionel Sambuc
26324684ddb6SLionel Sambuctemplate <class _ForwardIterator>
2633*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26344684ddb6SLionel Sambuc_ForwardIterator
26354684ddb6SLionel Sambucmax_element(_ForwardIterator __first, _ForwardIterator __last)
26364684ddb6SLionel Sambuc{
26374684ddb6SLionel Sambuc    return _VSTD::max_element(__first, __last,
26384684ddb6SLionel Sambuc              __less<typename iterator_traits<_ForwardIterator>::value_type>());
26394684ddb6SLionel Sambuc}
26404684ddb6SLionel Sambuc
26414684ddb6SLionel Sambuc// max
26424684ddb6SLionel Sambuc
26434684ddb6SLionel Sambuctemplate <class _Tp, class _Compare>
2644*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26454684ddb6SLionel Sambucconst _Tp&
26464684ddb6SLionel Sambucmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
26474684ddb6SLionel Sambuc{
26484684ddb6SLionel Sambuc    return __comp(__a, __b) ? __b : __a;
26494684ddb6SLionel Sambuc}
26504684ddb6SLionel Sambuc
26514684ddb6SLionel Sambuctemplate <class _Tp>
2652*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26534684ddb6SLionel Sambucconst _Tp&
26544684ddb6SLionel Sambucmax(const _Tp& __a, const _Tp& __b)
26554684ddb6SLionel Sambuc{
26564684ddb6SLionel Sambuc    return _VSTD::max(__a, __b, __less<_Tp>());
26574684ddb6SLionel Sambuc}
26584684ddb6SLionel Sambuc
26594684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
26604684ddb6SLionel Sambuc
26614684ddb6SLionel Sambuctemplate<class _Tp, class _Compare>
2662*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26634684ddb6SLionel Sambuc_Tp
26644684ddb6SLionel Sambucmax(initializer_list<_Tp> __t, _Compare __comp)
26654684ddb6SLionel Sambuc{
26664684ddb6SLionel Sambuc    return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
26674684ddb6SLionel Sambuc}
26684684ddb6SLionel Sambuc
26694684ddb6SLionel Sambuctemplate<class _Tp>
2670*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
26714684ddb6SLionel Sambuc_Tp
26724684ddb6SLionel Sambucmax(initializer_list<_Tp> __t)
26734684ddb6SLionel Sambuc{
2674*0a6a1f1dSLionel Sambuc    return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
26754684ddb6SLionel Sambuc}
26764684ddb6SLionel Sambuc
26774684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
26784684ddb6SLionel Sambuc
26794684ddb6SLionel Sambuc// minmax_element
26804684ddb6SLionel Sambuc
26814684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Compare>
2682*0a6a1f1dSLionel Sambuc_LIBCPP_CONSTEXPR_AFTER_CXX11
26834684ddb6SLionel Sambucstd::pair<_ForwardIterator, _ForwardIterator>
26844684ddb6SLionel Sambucminmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
26854684ddb6SLionel Sambuc{
26864684ddb6SLionel Sambuc  std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
26874684ddb6SLionel Sambuc  if (__first != __last)
26884684ddb6SLionel Sambuc  {
26894684ddb6SLionel Sambuc      if (++__first != __last)
26904684ddb6SLionel Sambuc      {
26914684ddb6SLionel Sambuc          if (__comp(*__first, *__result.first))
26924684ddb6SLionel Sambuc              __result.first = __first;
26934684ddb6SLionel Sambuc          else
26944684ddb6SLionel Sambuc              __result.second = __first;
26954684ddb6SLionel Sambuc          while (++__first != __last)
26964684ddb6SLionel Sambuc          {
26974684ddb6SLionel Sambuc              _ForwardIterator __i = __first;
26984684ddb6SLionel Sambuc              if (++__first == __last)
26994684ddb6SLionel Sambuc              {
27004684ddb6SLionel Sambuc                  if (__comp(*__i, *__result.first))
27014684ddb6SLionel Sambuc                      __result.first = __i;
27024684ddb6SLionel Sambuc                  else if (!__comp(*__i, *__result.second))
27034684ddb6SLionel Sambuc                      __result.second = __i;
27044684ddb6SLionel Sambuc                  break;
27054684ddb6SLionel Sambuc              }
27064684ddb6SLionel Sambuc              else
27074684ddb6SLionel Sambuc              {
27084684ddb6SLionel Sambuc                  if (__comp(*__first, *__i))
27094684ddb6SLionel Sambuc                  {
27104684ddb6SLionel Sambuc                      if (__comp(*__first, *__result.first))
27114684ddb6SLionel Sambuc                          __result.first = __first;
27124684ddb6SLionel Sambuc                      if (!__comp(*__i, *__result.second))
27134684ddb6SLionel Sambuc                          __result.second = __i;
27144684ddb6SLionel Sambuc                  }
27154684ddb6SLionel Sambuc                  else
27164684ddb6SLionel Sambuc                  {
27174684ddb6SLionel Sambuc                      if (__comp(*__i, *__result.first))
27184684ddb6SLionel Sambuc                          __result.first = __i;
27194684ddb6SLionel Sambuc                      if (!__comp(*__first, *__result.second))
27204684ddb6SLionel Sambuc                          __result.second = __first;
27214684ddb6SLionel Sambuc                  }
27224684ddb6SLionel Sambuc              }
27234684ddb6SLionel Sambuc          }
27244684ddb6SLionel Sambuc      }
27254684ddb6SLionel Sambuc  }
27264684ddb6SLionel Sambuc  return __result;
27274684ddb6SLionel Sambuc}
27284684ddb6SLionel Sambuc
27294684ddb6SLionel Sambuctemplate <class _ForwardIterator>
2730*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
27314684ddb6SLionel Sambucstd::pair<_ForwardIterator, _ForwardIterator>
27324684ddb6SLionel Sambucminmax_element(_ForwardIterator __first, _ForwardIterator __last)
27334684ddb6SLionel Sambuc{
2734*0a6a1f1dSLionel Sambuc    return _VSTD::minmax_element(__first, __last,
2735*0a6a1f1dSLionel Sambuc              __less<typename iterator_traits<_ForwardIterator>::value_type>());
27364684ddb6SLionel Sambuc}
27374684ddb6SLionel Sambuc
27384684ddb6SLionel Sambuc// minmax
27394684ddb6SLionel Sambuc
27404684ddb6SLionel Sambuctemplate<class _Tp, class _Compare>
2741*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
27424684ddb6SLionel Sambucpair<const _Tp&, const _Tp&>
27434684ddb6SLionel Sambucminmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
27444684ddb6SLionel Sambuc{
27454684ddb6SLionel Sambuc    return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
27464684ddb6SLionel Sambuc                              pair<const _Tp&, const _Tp&>(__a, __b);
27474684ddb6SLionel Sambuc}
27484684ddb6SLionel Sambuc
27494684ddb6SLionel Sambuctemplate<class _Tp>
2750*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
27514684ddb6SLionel Sambucpair<const _Tp&, const _Tp&>
27524684ddb6SLionel Sambucminmax(const _Tp& __a, const _Tp& __b)
27534684ddb6SLionel Sambuc{
27544684ddb6SLionel Sambuc    return _VSTD::minmax(__a, __b, __less<_Tp>());
27554684ddb6SLionel Sambuc}
27564684ddb6SLionel Sambuc
27574684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
27584684ddb6SLionel Sambuc
27594684ddb6SLionel Sambuctemplate<class _Tp, class _Compare>
2760*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
27614684ddb6SLionel Sambucpair<_Tp, _Tp>
27624684ddb6SLionel Sambucminmax(initializer_list<_Tp> __t, _Compare __comp)
27634684ddb6SLionel Sambuc{
2764*0a6a1f1dSLionel Sambuc    typedef typename initializer_list<_Tp>::const_iterator _Iter;
2765*0a6a1f1dSLionel Sambuc    _Iter __first = __t.begin();
2766*0a6a1f1dSLionel Sambuc    _Iter __last  = __t.end();
2767*0a6a1f1dSLionel Sambuc    std::pair<_Tp, _Tp> __result(*__first, *__first);
2768*0a6a1f1dSLionel Sambuc
2769*0a6a1f1dSLionel Sambuc    ++__first;
2770*0a6a1f1dSLionel Sambuc    if (__t.size() % 2 == 0)
2771*0a6a1f1dSLionel Sambuc    {
2772*0a6a1f1dSLionel Sambuc        if (__comp(*__first,  __result.first))
2773*0a6a1f1dSLionel Sambuc            __result.first  = *__first;
2774*0a6a1f1dSLionel Sambuc        else
2775*0a6a1f1dSLionel Sambuc            __result.second = *__first;
2776*0a6a1f1dSLionel Sambuc        ++__first;
2777*0a6a1f1dSLionel Sambuc    }
2778*0a6a1f1dSLionel Sambuc
2779*0a6a1f1dSLionel Sambuc    while (__first != __last)
2780*0a6a1f1dSLionel Sambuc    {
2781*0a6a1f1dSLionel Sambuc        _Tp __prev = *__first++;
2782*0a6a1f1dSLionel Sambuc        if (__comp(*__first, __prev)) {
2783*0a6a1f1dSLionel Sambuc            if ( __comp(*__first, __result.first)) __result.first  = *__first;
2784*0a6a1f1dSLionel Sambuc            if (!__comp(__prev, __result.second))  __result.second = __prev;
2785*0a6a1f1dSLionel Sambuc            }
2786*0a6a1f1dSLionel Sambuc        else {
2787*0a6a1f1dSLionel Sambuc            if ( __comp(__prev, __result.first))    __result.first  = __prev;
2788*0a6a1f1dSLionel Sambuc            if (!__comp(*__first, __result.second)) __result.second = *__first;
2789*0a6a1f1dSLionel Sambuc            }
2790*0a6a1f1dSLionel Sambuc
2791*0a6a1f1dSLionel Sambuc        __first++;
2792*0a6a1f1dSLionel Sambuc    }
2793*0a6a1f1dSLionel Sambuc    return __result;
2794*0a6a1f1dSLionel Sambuc}
2795*0a6a1f1dSLionel Sambuc
2796*0a6a1f1dSLionel Sambuctemplate<class _Tp>
2797*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2798*0a6a1f1dSLionel Sambucpair<_Tp, _Tp>
2799*0a6a1f1dSLionel Sambucminmax(initializer_list<_Tp> __t)
2800*0a6a1f1dSLionel Sambuc{
2801*0a6a1f1dSLionel Sambuc    return _VSTD::minmax(__t, __less<_Tp>());
28024684ddb6SLionel Sambuc}
28034684ddb6SLionel Sambuc
28044684ddb6SLionel Sambuc#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
28054684ddb6SLionel Sambuc
28064684ddb6SLionel Sambuc// random_shuffle
28074684ddb6SLionel Sambuc
28084684ddb6SLionel Sambuc// __independent_bits_engine
28094684ddb6SLionel Sambuc
28104684ddb6SLionel Sambuctemplate <unsigned long long _Xp, size_t _Rp>
28114684ddb6SLionel Sambucstruct __log2_imp
28124684ddb6SLionel Sambuc{
28134684ddb6SLionel Sambuc    static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
28144684ddb6SLionel Sambuc                                           : __log2_imp<_Xp, _Rp - 1>::value;
28154684ddb6SLionel Sambuc};
28164684ddb6SLionel Sambuc
28174684ddb6SLionel Sambuctemplate <unsigned long long _Xp>
28184684ddb6SLionel Sambucstruct __log2_imp<_Xp, 0>
28194684ddb6SLionel Sambuc{
28204684ddb6SLionel Sambuc    static const size_t value = 0;
28214684ddb6SLionel Sambuc};
28224684ddb6SLionel Sambuc
28234684ddb6SLionel Sambuctemplate <size_t _Rp>
28244684ddb6SLionel Sambucstruct __log2_imp<0, _Rp>
28254684ddb6SLionel Sambuc{
28264684ddb6SLionel Sambuc    static const size_t value = _Rp + 1;
28274684ddb6SLionel Sambuc};
28284684ddb6SLionel Sambuc
28294684ddb6SLionel Sambuctemplate <class _UI, _UI _Xp>
28304684ddb6SLionel Sambucstruct __log2
28314684ddb6SLionel Sambuc{
28324684ddb6SLionel Sambuc    static const size_t value = __log2_imp<_Xp,
28334684ddb6SLionel Sambuc                                         sizeof(_UI) * __CHAR_BIT__ - 1>::value;
28344684ddb6SLionel Sambuc};
28354684ddb6SLionel Sambuc
28364684ddb6SLionel Sambuctemplate<class _Engine, class _UIntType>
28374684ddb6SLionel Sambucclass __independent_bits_engine
28384684ddb6SLionel Sambuc{
28394684ddb6SLionel Sambucpublic:
28404684ddb6SLionel Sambuc    // types
28414684ddb6SLionel Sambuc    typedef _UIntType result_type;
28424684ddb6SLionel Sambuc
28434684ddb6SLionel Sambucprivate:
28444684ddb6SLionel Sambuc    typedef typename _Engine::result_type _Engine_result_type;
28454684ddb6SLionel Sambuc    typedef typename conditional
28464684ddb6SLionel Sambuc        <
28474684ddb6SLionel Sambuc            sizeof(_Engine_result_type) <= sizeof(result_type),
28484684ddb6SLionel Sambuc                result_type,
28494684ddb6SLionel Sambuc                _Engine_result_type
28504684ddb6SLionel Sambuc        >::type _Working_result_type;
28514684ddb6SLionel Sambuc
28524684ddb6SLionel Sambuc    _Engine& __e_;
28534684ddb6SLionel Sambuc    size_t __w_;
28544684ddb6SLionel Sambuc    size_t __w0_;
28554684ddb6SLionel Sambuc    size_t __n_;
28564684ddb6SLionel Sambuc    size_t __n0_;
28574684ddb6SLionel Sambuc    _Working_result_type __y0_;
28584684ddb6SLionel Sambuc    _Working_result_type __y1_;
28594684ddb6SLionel Sambuc    _Engine_result_type __mask0_;
28604684ddb6SLionel Sambuc    _Engine_result_type __mask1_;
28614684ddb6SLionel Sambuc
28624684ddb6SLionel Sambuc#ifdef _LIBCPP_HAS_NO_CONSTEXPR
28634684ddb6SLionel Sambuc    static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
28644684ddb6SLionel Sambuc                                          + _Working_result_type(1);
28654684ddb6SLionel Sambuc#else
28664684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
28674684ddb6SLionel Sambuc                                                      + _Working_result_type(1);
28684684ddb6SLionel Sambuc#endif
28694684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
28704684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
28714684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
28724684ddb6SLionel Sambuc
28734684ddb6SLionel Sambucpublic:
28744684ddb6SLionel Sambuc    // constructors and seeding functions
28754684ddb6SLionel Sambuc    __independent_bits_engine(_Engine& __e, size_t __w);
28764684ddb6SLionel Sambuc
28774684ddb6SLionel Sambuc    // generating functions
28784684ddb6SLionel Sambuc    result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
28794684ddb6SLionel Sambuc
28804684ddb6SLionel Sambucprivate:
28814684ddb6SLionel Sambuc    result_type __eval(false_type);
28824684ddb6SLionel Sambuc    result_type __eval(true_type);
28834684ddb6SLionel Sambuc};
28844684ddb6SLionel Sambuc
28854684ddb6SLionel Sambuctemplate<class _Engine, class _UIntType>
28864684ddb6SLionel Sambuc__independent_bits_engine<_Engine, _UIntType>
28874684ddb6SLionel Sambuc    ::__independent_bits_engine(_Engine& __e, size_t __w)
28884684ddb6SLionel Sambuc        : __e_(__e),
28894684ddb6SLionel Sambuc          __w_(__w)
28904684ddb6SLionel Sambuc{
28914684ddb6SLionel Sambuc    __n_ = __w_ / __m + (__w_ % __m != 0);
28924684ddb6SLionel Sambuc    __w0_ = __w_ / __n_;
28934684ddb6SLionel Sambuc    if (_Rp == 0)
28944684ddb6SLionel Sambuc        __y0_ = _Rp;
28954684ddb6SLionel Sambuc    else if (__w0_ < _WDt)
28964684ddb6SLionel Sambuc        __y0_ = (_Rp >> __w0_) << __w0_;
28974684ddb6SLionel Sambuc    else
28984684ddb6SLionel Sambuc        __y0_ = 0;
28994684ddb6SLionel Sambuc    if (_Rp - __y0_ > __y0_ / __n_)
29004684ddb6SLionel Sambuc    {
29014684ddb6SLionel Sambuc        ++__n_;
29024684ddb6SLionel Sambuc        __w0_ = __w_ / __n_;
29034684ddb6SLionel Sambuc        if (__w0_ < _WDt)
29044684ddb6SLionel Sambuc            __y0_ = (_Rp >> __w0_) << __w0_;
29054684ddb6SLionel Sambuc        else
29064684ddb6SLionel Sambuc            __y0_ = 0;
29074684ddb6SLionel Sambuc    }
29084684ddb6SLionel Sambuc    __n0_ = __n_ - __w_ % __n_;
29094684ddb6SLionel Sambuc    if (__w0_ < _WDt - 1)
29104684ddb6SLionel Sambuc        __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
29114684ddb6SLionel Sambuc    else
29124684ddb6SLionel Sambuc        __y1_ = 0;
29134684ddb6SLionel Sambuc    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
29144684ddb6SLionel Sambuc                          _Engine_result_type(0);
29154684ddb6SLionel Sambuc    __mask1_ = __w0_ < _EDt - 1 ?
29164684ddb6SLionel Sambuc                               _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
29174684ddb6SLionel Sambuc                               _Engine_result_type(~0);
29184684ddb6SLionel Sambuc}
29194684ddb6SLionel Sambuc
29204684ddb6SLionel Sambuctemplate<class _Engine, class _UIntType>
29214684ddb6SLionel Sambucinline
29224684ddb6SLionel Sambuc_UIntType
29234684ddb6SLionel Sambuc__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
29244684ddb6SLionel Sambuc{
29254684ddb6SLionel Sambuc    return static_cast<result_type>(__e_() & __mask0_);
29264684ddb6SLionel Sambuc}
29274684ddb6SLionel Sambuc
29284684ddb6SLionel Sambuctemplate<class _Engine, class _UIntType>
29294684ddb6SLionel Sambuc_UIntType
29304684ddb6SLionel Sambuc__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
29314684ddb6SLionel Sambuc{
29324684ddb6SLionel Sambuc    result_type _Sp = 0;
29334684ddb6SLionel Sambuc    for (size_t __k = 0; __k < __n0_; ++__k)
29344684ddb6SLionel Sambuc    {
29354684ddb6SLionel Sambuc        _Engine_result_type __u;
29364684ddb6SLionel Sambuc        do
29374684ddb6SLionel Sambuc        {
29384684ddb6SLionel Sambuc            __u = __e_() - _Engine::min();
29394684ddb6SLionel Sambuc        } while (__u >= __y0_);
29404684ddb6SLionel Sambuc        if (__w0_ < _WDt)
29414684ddb6SLionel Sambuc            _Sp <<= __w0_;
29424684ddb6SLionel Sambuc        else
29434684ddb6SLionel Sambuc            _Sp = 0;
29444684ddb6SLionel Sambuc        _Sp += __u & __mask0_;
29454684ddb6SLionel Sambuc    }
29464684ddb6SLionel Sambuc    for (size_t __k = __n0_; __k < __n_; ++__k)
29474684ddb6SLionel Sambuc    {
29484684ddb6SLionel Sambuc        _Engine_result_type __u;
29494684ddb6SLionel Sambuc        do
29504684ddb6SLionel Sambuc        {
29514684ddb6SLionel Sambuc            __u = __e_() - _Engine::min();
29524684ddb6SLionel Sambuc        } while (__u >= __y1_);
29534684ddb6SLionel Sambuc        if (__w0_ < _WDt - 1)
29544684ddb6SLionel Sambuc            _Sp <<= __w0_ + 1;
29554684ddb6SLionel Sambuc        else
29564684ddb6SLionel Sambuc            _Sp = 0;
29574684ddb6SLionel Sambuc        _Sp += __u & __mask1_;
29584684ddb6SLionel Sambuc    }
29594684ddb6SLionel Sambuc    return _Sp;
29604684ddb6SLionel Sambuc}
29614684ddb6SLionel Sambuc
29624684ddb6SLionel Sambuc// uniform_int_distribution
29634684ddb6SLionel Sambuc
29644684ddb6SLionel Sambuctemplate<class _IntType = int>
29654684ddb6SLionel Sambucclass uniform_int_distribution
29664684ddb6SLionel Sambuc{
29674684ddb6SLionel Sambucpublic:
29684684ddb6SLionel Sambuc    // types
29694684ddb6SLionel Sambuc    typedef _IntType result_type;
29704684ddb6SLionel Sambuc
29714684ddb6SLionel Sambuc    class param_type
29724684ddb6SLionel Sambuc    {
29734684ddb6SLionel Sambuc        result_type __a_;
29744684ddb6SLionel Sambuc        result_type __b_;
29754684ddb6SLionel Sambuc    public:
29764684ddb6SLionel Sambuc        typedef uniform_int_distribution distribution_type;
29774684ddb6SLionel Sambuc
29784684ddb6SLionel Sambuc        explicit param_type(result_type __a = 0,
29794684ddb6SLionel Sambuc                            result_type __b = numeric_limits<result_type>::max())
29804684ddb6SLionel Sambuc            : __a_(__a), __b_(__b) {}
29814684ddb6SLionel Sambuc
29824684ddb6SLionel Sambuc        result_type a() const {return __a_;}
29834684ddb6SLionel Sambuc        result_type b() const {return __b_;}
29844684ddb6SLionel Sambuc
29854684ddb6SLionel Sambuc        friend bool operator==(const param_type& __x, const param_type& __y)
29864684ddb6SLionel Sambuc            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
29874684ddb6SLionel Sambuc        friend bool operator!=(const param_type& __x, const param_type& __y)
29884684ddb6SLionel Sambuc            {return !(__x == __y);}
29894684ddb6SLionel Sambuc    };
29904684ddb6SLionel Sambuc
29914684ddb6SLionel Sambucprivate:
29924684ddb6SLionel Sambuc    param_type __p_;
29934684ddb6SLionel Sambuc
29944684ddb6SLionel Sambucpublic:
29954684ddb6SLionel Sambuc    // constructors and reset functions
29964684ddb6SLionel Sambuc    explicit uniform_int_distribution(result_type __a = 0,
29974684ddb6SLionel Sambuc                                      result_type __b = numeric_limits<result_type>::max())
29984684ddb6SLionel Sambuc        : __p_(param_type(__a, __b)) {}
29994684ddb6SLionel Sambuc    explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
30004684ddb6SLionel Sambuc    void reset() {}
30014684ddb6SLionel Sambuc
30024684ddb6SLionel Sambuc    // generating functions
30034684ddb6SLionel Sambuc    template<class _URNG> result_type operator()(_URNG& __g)
30044684ddb6SLionel Sambuc        {return (*this)(__g, __p_);}
30054684ddb6SLionel Sambuc    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
30064684ddb6SLionel Sambuc
30074684ddb6SLionel Sambuc    // property functions
30084684ddb6SLionel Sambuc    result_type a() const {return __p_.a();}
30094684ddb6SLionel Sambuc    result_type b() const {return __p_.b();}
30104684ddb6SLionel Sambuc
30114684ddb6SLionel Sambuc    param_type param() const {return __p_;}
30124684ddb6SLionel Sambuc    void param(const param_type& __p) {__p_ = __p;}
30134684ddb6SLionel Sambuc
30144684ddb6SLionel Sambuc    result_type min() const {return a();}
30154684ddb6SLionel Sambuc    result_type max() const {return b();}
30164684ddb6SLionel Sambuc
30174684ddb6SLionel Sambuc    friend bool operator==(const uniform_int_distribution& __x,
30184684ddb6SLionel Sambuc                           const uniform_int_distribution& __y)
30194684ddb6SLionel Sambuc        {return __x.__p_ == __y.__p_;}
30204684ddb6SLionel Sambuc    friend bool operator!=(const uniform_int_distribution& __x,
30214684ddb6SLionel Sambuc                           const uniform_int_distribution& __y)
30224684ddb6SLionel Sambuc            {return !(__x == __y);}
30234684ddb6SLionel Sambuc};
30244684ddb6SLionel Sambuc
30254684ddb6SLionel Sambuctemplate<class _IntType>
30264684ddb6SLionel Sambuctemplate<class _URNG>
30274684ddb6SLionel Sambuctypename uniform_int_distribution<_IntType>::result_type
30284684ddb6SLionel Sambucuniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
30294684ddb6SLionel Sambuc{
30304684ddb6SLionel Sambuc    typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
30314684ddb6SLionel Sambuc                                            uint32_t, uint64_t>::type _UIntType;
30324684ddb6SLionel Sambuc    const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
30334684ddb6SLionel Sambuc    if (_Rp == 1)
30344684ddb6SLionel Sambuc        return __p.a();
30354684ddb6SLionel Sambuc    const size_t _Dt = numeric_limits<_UIntType>::digits;
30364684ddb6SLionel Sambuc    typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
30374684ddb6SLionel Sambuc    if (_Rp == 0)
30384684ddb6SLionel Sambuc        return static_cast<result_type>(_Eng(__g, _Dt)());
30394684ddb6SLionel Sambuc    size_t __w = _Dt - __clz(_Rp) - 1;
3040*0a6a1f1dSLionel Sambuc    if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
30414684ddb6SLionel Sambuc        ++__w;
30424684ddb6SLionel Sambuc    _Eng __e(__g, __w);
30434684ddb6SLionel Sambuc    _UIntType __u;
30444684ddb6SLionel Sambuc    do
30454684ddb6SLionel Sambuc    {
30464684ddb6SLionel Sambuc        __u = __e();
30474684ddb6SLionel Sambuc    } while (__u >= _Rp);
30484684ddb6SLionel Sambuc    return static_cast<result_type>(__u + __p.a());
30494684ddb6SLionel Sambuc}
30504684ddb6SLionel Sambuc
30514684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS __rs_default;
30524684ddb6SLionel Sambuc
30534684ddb6SLionel Sambuc_LIBCPP_FUNC_VIS __rs_default __rs_get();
30544684ddb6SLionel Sambuc
30554684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS __rs_default
30564684ddb6SLionel Sambuc{
30574684ddb6SLionel Sambuc    static unsigned __c_;
30584684ddb6SLionel Sambuc
30594684ddb6SLionel Sambuc    __rs_default();
30604684ddb6SLionel Sambucpublic:
30614684ddb6SLionel Sambuc    typedef uint_fast32_t result_type;
30624684ddb6SLionel Sambuc
30634684ddb6SLionel Sambuc    static const result_type _Min = 0;
30644684ddb6SLionel Sambuc    static const result_type _Max = 0xFFFFFFFF;
30654684ddb6SLionel Sambuc
30664684ddb6SLionel Sambuc    __rs_default(const __rs_default&);
30674684ddb6SLionel Sambuc    ~__rs_default();
30684684ddb6SLionel Sambuc
30694684ddb6SLionel Sambuc    result_type operator()();
30704684ddb6SLionel Sambuc
30714684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
30724684ddb6SLionel Sambuc    static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
30734684ddb6SLionel Sambuc
30744684ddb6SLionel Sambuc    friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
30754684ddb6SLionel Sambuc};
30764684ddb6SLionel Sambuc
30774684ddb6SLionel Sambuc_LIBCPP_FUNC_VIS __rs_default __rs_get();
30784684ddb6SLionel Sambuc
30794684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
30804684ddb6SLionel Sambucvoid
30814684ddb6SLionel Sambucrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
30824684ddb6SLionel Sambuc{
30834684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
30844684ddb6SLionel Sambuc    typedef uniform_int_distribution<ptrdiff_t> _Dp;
30854684ddb6SLionel Sambuc    typedef typename _Dp::param_type _Pp;
30864684ddb6SLionel Sambuc    difference_type __d = __last - __first;
30874684ddb6SLionel Sambuc    if (__d > 1)
30884684ddb6SLionel Sambuc    {
30894684ddb6SLionel Sambuc        _Dp __uid;
30904684ddb6SLionel Sambuc        __rs_default __g = __rs_get();
30914684ddb6SLionel Sambuc        for (--__last, --__d; __first < __last; ++__first, --__d)
30924684ddb6SLionel Sambuc        {
30934684ddb6SLionel Sambuc            difference_type __i = __uid(__g, _Pp(0, __d));
30944684ddb6SLionel Sambuc            if (__i != difference_type(0))
30954684ddb6SLionel Sambuc                swap(*__first, *(__first + __i));
30964684ddb6SLionel Sambuc        }
30974684ddb6SLionel Sambuc    }
30984684ddb6SLionel Sambuc}
30994684ddb6SLionel Sambuc
31004684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _RandomNumberGenerator>
31014684ddb6SLionel Sambucvoid
31024684ddb6SLionel Sambucrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
31034684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
31044684ddb6SLionel Sambuc               _RandomNumberGenerator&& __rand)
31054684ddb6SLionel Sambuc#else
31064684ddb6SLionel Sambuc               _RandomNumberGenerator& __rand)
31074684ddb6SLionel Sambuc#endif
31084684ddb6SLionel Sambuc{
31094684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
31104684ddb6SLionel Sambuc    difference_type __d = __last - __first;
31114684ddb6SLionel Sambuc    if (__d > 1)
31124684ddb6SLionel Sambuc    {
31134684ddb6SLionel Sambuc        for (--__last; __first < __last; ++__first, --__d)
31144684ddb6SLionel Sambuc        {
31154684ddb6SLionel Sambuc            difference_type __i = __rand(__d);
31164684ddb6SLionel Sambuc            swap(*__first, *(__first + __i));
31174684ddb6SLionel Sambuc        }
31184684ddb6SLionel Sambuc    }
31194684ddb6SLionel Sambuc}
31204684ddb6SLionel Sambuc
31214684ddb6SLionel Sambuctemplate<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
31224684ddb6SLionel Sambuc    void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
31234684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
31244684ddb6SLionel Sambuc                 _UniformRandomNumberGenerator&& __g)
31254684ddb6SLionel Sambuc#else
31264684ddb6SLionel Sambuc                 _UniformRandomNumberGenerator& __g)
31274684ddb6SLionel Sambuc#endif
31284684ddb6SLionel Sambuc{
31294684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
31304684ddb6SLionel Sambuc    typedef uniform_int_distribution<ptrdiff_t> _Dp;
31314684ddb6SLionel Sambuc    typedef typename _Dp::param_type _Pp;
31324684ddb6SLionel Sambuc    difference_type __d = __last - __first;
31334684ddb6SLionel Sambuc    if (__d > 1)
31344684ddb6SLionel Sambuc    {
31354684ddb6SLionel Sambuc        _Dp __uid;
31364684ddb6SLionel Sambuc        for (--__last, --__d; __first < __last; ++__first, --__d)
31374684ddb6SLionel Sambuc        {
31384684ddb6SLionel Sambuc            difference_type __i = __uid(__g, _Pp(0, __d));
31394684ddb6SLionel Sambuc            if (__i != difference_type(0))
31404684ddb6SLionel Sambuc                swap(*__first, *(__first + __i));
31414684ddb6SLionel Sambuc        }
31424684ddb6SLionel Sambuc    }
31434684ddb6SLionel Sambuc}
31444684ddb6SLionel Sambuc
31454684ddb6SLionel Sambuctemplate <class _InputIterator, class _Predicate>
31464684ddb6SLionel Sambucbool
31474684ddb6SLionel Sambucis_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
31484684ddb6SLionel Sambuc{
31494684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
31504684ddb6SLionel Sambuc        if (!__pred(*__first))
31514684ddb6SLionel Sambuc            break;
3152*0a6a1f1dSLionel Sambuc    if ( __first == __last )
3153*0a6a1f1dSLionel Sambuc        return true;
3154*0a6a1f1dSLionel Sambuc    ++__first;
31554684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
31564684ddb6SLionel Sambuc        if (__pred(*__first))
31574684ddb6SLionel Sambuc            return false;
31584684ddb6SLionel Sambuc    return true;
31594684ddb6SLionel Sambuc}
31604684ddb6SLionel Sambuc
31614684ddb6SLionel Sambuc// partition
31624684ddb6SLionel Sambuc
31634684ddb6SLionel Sambuctemplate <class _Predicate, class _ForwardIterator>
31644684ddb6SLionel Sambuc_ForwardIterator
31654684ddb6SLionel Sambuc__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
31664684ddb6SLionel Sambuc{
31674684ddb6SLionel Sambuc    while (true)
31684684ddb6SLionel Sambuc    {
31694684ddb6SLionel Sambuc        if (__first == __last)
31704684ddb6SLionel Sambuc            return __first;
31714684ddb6SLionel Sambuc        if (!__pred(*__first))
31724684ddb6SLionel Sambuc            break;
31734684ddb6SLionel Sambuc        ++__first;
31744684ddb6SLionel Sambuc    }
31754684ddb6SLionel Sambuc    for (_ForwardIterator __p = __first; ++__p != __last;)
31764684ddb6SLionel Sambuc    {
31774684ddb6SLionel Sambuc        if (__pred(*__p))
31784684ddb6SLionel Sambuc        {
31794684ddb6SLionel Sambuc            swap(*__first, *__p);
31804684ddb6SLionel Sambuc            ++__first;
31814684ddb6SLionel Sambuc        }
31824684ddb6SLionel Sambuc    }
31834684ddb6SLionel Sambuc    return __first;
31844684ddb6SLionel Sambuc}
31854684ddb6SLionel Sambuc
31864684ddb6SLionel Sambuctemplate <class _Predicate, class _BidirectionalIterator>
31874684ddb6SLionel Sambuc_BidirectionalIterator
31884684ddb6SLionel Sambuc__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
31894684ddb6SLionel Sambuc            bidirectional_iterator_tag)
31904684ddb6SLionel Sambuc{
31914684ddb6SLionel Sambuc    while (true)
31924684ddb6SLionel Sambuc    {
31934684ddb6SLionel Sambuc        while (true)
31944684ddb6SLionel Sambuc        {
31954684ddb6SLionel Sambuc            if (__first == __last)
31964684ddb6SLionel Sambuc                return __first;
31974684ddb6SLionel Sambuc            if (!__pred(*__first))
31984684ddb6SLionel Sambuc                break;
31994684ddb6SLionel Sambuc            ++__first;
32004684ddb6SLionel Sambuc        }
32014684ddb6SLionel Sambuc        do
32024684ddb6SLionel Sambuc        {
32034684ddb6SLionel Sambuc            if (__first == --__last)
32044684ddb6SLionel Sambuc                return __first;
32054684ddb6SLionel Sambuc        } while (!__pred(*__last));
32064684ddb6SLionel Sambuc        swap(*__first, *__last);
32074684ddb6SLionel Sambuc        ++__first;
32084684ddb6SLionel Sambuc    }
32094684ddb6SLionel Sambuc}
32104684ddb6SLionel Sambuc
32114684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Predicate>
32124684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
32134684ddb6SLionel Sambuc_ForwardIterator
32144684ddb6SLionel Sambucpartition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
32154684ddb6SLionel Sambuc{
32164684ddb6SLionel Sambuc    return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
32174684ddb6SLionel Sambuc                            (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
32184684ddb6SLionel Sambuc}
32194684ddb6SLionel Sambuc
32204684ddb6SLionel Sambuc// partition_copy
32214684ddb6SLionel Sambuc
32224684ddb6SLionel Sambuctemplate <class _InputIterator, class _OutputIterator1,
32234684ddb6SLionel Sambuc          class _OutputIterator2, class _Predicate>
32244684ddb6SLionel Sambucpair<_OutputIterator1, _OutputIterator2>
32254684ddb6SLionel Sambucpartition_copy(_InputIterator __first, _InputIterator __last,
32264684ddb6SLionel Sambuc               _OutputIterator1 __out_true, _OutputIterator2 __out_false,
32274684ddb6SLionel Sambuc               _Predicate __pred)
32284684ddb6SLionel Sambuc{
32294684ddb6SLionel Sambuc    for (; __first != __last; ++__first)
32304684ddb6SLionel Sambuc    {
32314684ddb6SLionel Sambuc        if (__pred(*__first))
32324684ddb6SLionel Sambuc        {
32334684ddb6SLionel Sambuc            *__out_true = *__first;
32344684ddb6SLionel Sambuc            ++__out_true;
32354684ddb6SLionel Sambuc        }
32364684ddb6SLionel Sambuc        else
32374684ddb6SLionel Sambuc        {
32384684ddb6SLionel Sambuc            *__out_false = *__first;
32394684ddb6SLionel Sambuc            ++__out_false;
32404684ddb6SLionel Sambuc        }
32414684ddb6SLionel Sambuc    }
32424684ddb6SLionel Sambuc    return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
32434684ddb6SLionel Sambuc}
32444684ddb6SLionel Sambuc
32454684ddb6SLionel Sambuc// partition_point
32464684ddb6SLionel Sambuc
32474684ddb6SLionel Sambuctemplate<class _ForwardIterator, class _Predicate>
32484684ddb6SLionel Sambuc_ForwardIterator
32494684ddb6SLionel Sambucpartition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
32504684ddb6SLionel Sambuc{
32514684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
32524684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last);
32534684ddb6SLionel Sambuc    while (__len != 0)
32544684ddb6SLionel Sambuc    {
32554684ddb6SLionel Sambuc        difference_type __l2 = __len / 2;
32564684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
32574684ddb6SLionel Sambuc        _VSTD::advance(__m, __l2);
32584684ddb6SLionel Sambuc        if (__pred(*__m))
32594684ddb6SLionel Sambuc        {
32604684ddb6SLionel Sambuc            __first = ++__m;
32614684ddb6SLionel Sambuc            __len -= __l2 + 1;
32624684ddb6SLionel Sambuc        }
32634684ddb6SLionel Sambuc        else
32644684ddb6SLionel Sambuc            __len = __l2;
32654684ddb6SLionel Sambuc    }
32664684ddb6SLionel Sambuc    return __first;
32674684ddb6SLionel Sambuc}
32684684ddb6SLionel Sambuc
32694684ddb6SLionel Sambuc// stable_partition
32704684ddb6SLionel Sambuc
32714684ddb6SLionel Sambuctemplate <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
32724684ddb6SLionel Sambuc_ForwardIterator
32734684ddb6SLionel Sambuc__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
32744684ddb6SLionel Sambuc                   _Distance __len, _Pair __p, forward_iterator_tag __fit)
32754684ddb6SLionel Sambuc{
32764684ddb6SLionel Sambuc    // *__first is known to be false
32774684ddb6SLionel Sambuc    // __len >= 1
32784684ddb6SLionel Sambuc    if (__len == 1)
32794684ddb6SLionel Sambuc        return __first;
32804684ddb6SLionel Sambuc    if (__len == 2)
32814684ddb6SLionel Sambuc    {
32824684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
32834684ddb6SLionel Sambuc        if (__pred(*++__m))
32844684ddb6SLionel Sambuc        {
32854684ddb6SLionel Sambuc            swap(*__first, *__m);
32864684ddb6SLionel Sambuc            return __m;
32874684ddb6SLionel Sambuc        }
32884684ddb6SLionel Sambuc        return __first;
32894684ddb6SLionel Sambuc    }
32904684ddb6SLionel Sambuc    if (__len <= __p.second)
32914684ddb6SLionel Sambuc    {   // The buffer is big enough to use
32924684ddb6SLionel Sambuc        typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
32934684ddb6SLionel Sambuc        __destruct_n __d(0);
32944684ddb6SLionel Sambuc        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
32954684ddb6SLionel Sambuc        // Move the falses into the temporary buffer, and the trues to the front of the line
32964684ddb6SLionel Sambuc        // Update __first to always point to the end of the trues
32974684ddb6SLionel Sambuc        value_type* __t = __p.first;
32984684ddb6SLionel Sambuc        ::new(__t) value_type(_VSTD::move(*__first));
32994684ddb6SLionel Sambuc        __d.__incr((value_type*)0);
33004684ddb6SLionel Sambuc        ++__t;
33014684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
33024684ddb6SLionel Sambuc        while (++__i != __last)
33034684ddb6SLionel Sambuc        {
33044684ddb6SLionel Sambuc            if (__pred(*__i))
33054684ddb6SLionel Sambuc            {
33064684ddb6SLionel Sambuc                *__first = _VSTD::move(*__i);
33074684ddb6SLionel Sambuc                ++__first;
33084684ddb6SLionel Sambuc            }
33094684ddb6SLionel Sambuc            else
33104684ddb6SLionel Sambuc            {
33114684ddb6SLionel Sambuc                ::new(__t) value_type(_VSTD::move(*__i));
33124684ddb6SLionel Sambuc                __d.__incr((value_type*)0);
33134684ddb6SLionel Sambuc                ++__t;
33144684ddb6SLionel Sambuc            }
33154684ddb6SLionel Sambuc        }
33164684ddb6SLionel Sambuc        // All trues now at start of range, all falses in buffer
33174684ddb6SLionel Sambuc        // Move falses back into range, but don't mess up __first which points to first false
33184684ddb6SLionel Sambuc        __i = __first;
33194684ddb6SLionel Sambuc        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
33204684ddb6SLionel Sambuc            *__i = _VSTD::move(*__t2);
33214684ddb6SLionel Sambuc        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
33224684ddb6SLionel Sambuc        return __first;
33234684ddb6SLionel Sambuc    }
33244684ddb6SLionel Sambuc    // Else not enough buffer, do in place
33254684ddb6SLionel Sambuc    // __len >= 3
33264684ddb6SLionel Sambuc    _ForwardIterator __m = __first;
33274684ddb6SLionel Sambuc    _Distance __len2 = __len / 2;  // __len2 >= 2
33284684ddb6SLionel Sambuc    _VSTD::advance(__m, __len2);
33294684ddb6SLionel Sambuc    // recurse on [__first, __m), *__first know to be false
33304684ddb6SLionel Sambuc    // F?????????????????
33314684ddb6SLionel Sambuc    // f       m         l
33324684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
33334684ddb6SLionel Sambuc    _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
33344684ddb6SLionel Sambuc    // TTTFFFFF??????????
33354684ddb6SLionel Sambuc    // f  ff   m         l
33364684ddb6SLionel Sambuc    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
33374684ddb6SLionel Sambuc    _ForwardIterator __m1 = __m;
33384684ddb6SLionel Sambuc    _ForwardIterator __second_false = __last;
33394684ddb6SLionel Sambuc    _Distance __len_half = __len - __len2;
33404684ddb6SLionel Sambuc    while (__pred(*__m1))
33414684ddb6SLionel Sambuc    {
33424684ddb6SLionel Sambuc        if (++__m1 == __last)
33434684ddb6SLionel Sambuc            goto __second_half_done;
33444684ddb6SLionel Sambuc        --__len_half;
33454684ddb6SLionel Sambuc    }
33464684ddb6SLionel Sambuc    // TTTFFFFFTTTF??????
33474684ddb6SLionel Sambuc    // f  ff   m  m1     l
33484684ddb6SLionel Sambuc    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
33494684ddb6SLionel Sambuc__second_half_done:
33504684ddb6SLionel Sambuc    // TTTFFFFFTTTTTFFFFF
33514684ddb6SLionel Sambuc    // f  ff   m    sf   l
33524684ddb6SLionel Sambuc    return _VSTD::rotate(__first_false, __m, __second_false);
33534684ddb6SLionel Sambuc    // TTTTTTTTFFFFFFFFFF
33544684ddb6SLionel Sambuc    //         |
33554684ddb6SLionel Sambuc}
33564684ddb6SLionel Sambuc
33574684ddb6SLionel Sambucstruct __return_temporary_buffer
33584684ddb6SLionel Sambuc{
33594684ddb6SLionel Sambuc    template <class _Tp>
33604684ddb6SLionel Sambuc    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
33614684ddb6SLionel Sambuc};
33624684ddb6SLionel Sambuc
33634684ddb6SLionel Sambuctemplate <class _Predicate, class _ForwardIterator>
33644684ddb6SLionel Sambuc_ForwardIterator
33654684ddb6SLionel Sambuc__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
33664684ddb6SLionel Sambuc                   forward_iterator_tag)
33674684ddb6SLionel Sambuc{
33684684ddb6SLionel Sambuc    const unsigned __alloc_limit = 3;  // might want to make this a function of trivial assignment
33694684ddb6SLionel Sambuc    // Either prove all true and return __first or point to first false
33704684ddb6SLionel Sambuc    while (true)
33714684ddb6SLionel Sambuc    {
33724684ddb6SLionel Sambuc        if (__first == __last)
33734684ddb6SLionel Sambuc            return __first;
33744684ddb6SLionel Sambuc        if (!__pred(*__first))
33754684ddb6SLionel Sambuc            break;
33764684ddb6SLionel Sambuc        ++__first;
33774684ddb6SLionel Sambuc    }
33784684ddb6SLionel Sambuc    // We now have a reduced range [__first, __last)
33794684ddb6SLionel Sambuc    // *__first is known to be false
33804684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
33814684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
33824684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last);
33834684ddb6SLionel Sambuc    pair<value_type*, ptrdiff_t> __p(0, 0);
33844684ddb6SLionel Sambuc    unique_ptr<value_type, __return_temporary_buffer> __h;
33854684ddb6SLionel Sambuc    if (__len >= __alloc_limit)
33864684ddb6SLionel Sambuc    {
33874684ddb6SLionel Sambuc        __p = _VSTD::get_temporary_buffer<value_type>(__len);
33884684ddb6SLionel Sambuc        __h.reset(__p.first);
33894684ddb6SLionel Sambuc    }
33904684ddb6SLionel Sambuc    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
33914684ddb6SLionel Sambuc                             (__first, __last, __pred, __len, __p, forward_iterator_tag());
33924684ddb6SLionel Sambuc}
33934684ddb6SLionel Sambuc
33944684ddb6SLionel Sambuctemplate <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
33954684ddb6SLionel Sambuc_BidirectionalIterator
33964684ddb6SLionel Sambuc__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
33974684ddb6SLionel Sambuc                   _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
33984684ddb6SLionel Sambuc{
33994684ddb6SLionel Sambuc    // *__first is known to be false
34004684ddb6SLionel Sambuc    // *__last is known to be true
34014684ddb6SLionel Sambuc    // __len >= 2
34024684ddb6SLionel Sambuc    if (__len == 2)
34034684ddb6SLionel Sambuc    {
34044684ddb6SLionel Sambuc        swap(*__first, *__last);
34054684ddb6SLionel Sambuc        return __last;
34064684ddb6SLionel Sambuc    }
34074684ddb6SLionel Sambuc    if (__len == 3)
34084684ddb6SLionel Sambuc    {
34094684ddb6SLionel Sambuc        _BidirectionalIterator __m = __first;
34104684ddb6SLionel Sambuc        if (__pred(*++__m))
34114684ddb6SLionel Sambuc        {
34124684ddb6SLionel Sambuc            swap(*__first, *__m);
34134684ddb6SLionel Sambuc            swap(*__m, *__last);
34144684ddb6SLionel Sambuc            return __last;
34154684ddb6SLionel Sambuc        }
34164684ddb6SLionel Sambuc        swap(*__m, *__last);
34174684ddb6SLionel Sambuc        swap(*__first, *__m);
34184684ddb6SLionel Sambuc        return __m;
34194684ddb6SLionel Sambuc    }
34204684ddb6SLionel Sambuc    if (__len <= __p.second)
34214684ddb6SLionel Sambuc    {   // The buffer is big enough to use
34224684ddb6SLionel Sambuc        typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
34234684ddb6SLionel Sambuc        __destruct_n __d(0);
34244684ddb6SLionel Sambuc        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
34254684ddb6SLionel Sambuc        // Move the falses into the temporary buffer, and the trues to the front of the line
34264684ddb6SLionel Sambuc        // Update __first to always point to the end of the trues
34274684ddb6SLionel Sambuc        value_type* __t = __p.first;
34284684ddb6SLionel Sambuc        ::new(__t) value_type(_VSTD::move(*__first));
34294684ddb6SLionel Sambuc        __d.__incr((value_type*)0);
34304684ddb6SLionel Sambuc        ++__t;
34314684ddb6SLionel Sambuc        _BidirectionalIterator __i = __first;
34324684ddb6SLionel Sambuc        while (++__i != __last)
34334684ddb6SLionel Sambuc        {
34344684ddb6SLionel Sambuc            if (__pred(*__i))
34354684ddb6SLionel Sambuc            {
34364684ddb6SLionel Sambuc                *__first = _VSTD::move(*__i);
34374684ddb6SLionel Sambuc                ++__first;
34384684ddb6SLionel Sambuc            }
34394684ddb6SLionel Sambuc            else
34404684ddb6SLionel Sambuc            {
34414684ddb6SLionel Sambuc                ::new(__t) value_type(_VSTD::move(*__i));
34424684ddb6SLionel Sambuc                __d.__incr((value_type*)0);
34434684ddb6SLionel Sambuc                ++__t;
34444684ddb6SLionel Sambuc            }
34454684ddb6SLionel Sambuc        }
34464684ddb6SLionel Sambuc        // move *__last, known to be true
34474684ddb6SLionel Sambuc        *__first = _VSTD::move(*__i);
34484684ddb6SLionel Sambuc        __i = ++__first;
34494684ddb6SLionel Sambuc        // All trues now at start of range, all falses in buffer
34504684ddb6SLionel Sambuc        // Move falses back into range, but don't mess up __first which points to first false
34514684ddb6SLionel Sambuc        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
34524684ddb6SLionel Sambuc            *__i = _VSTD::move(*__t2);
34534684ddb6SLionel Sambuc        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
34544684ddb6SLionel Sambuc        return __first;
34554684ddb6SLionel Sambuc    }
34564684ddb6SLionel Sambuc    // Else not enough buffer, do in place
34574684ddb6SLionel Sambuc    // __len >= 4
34584684ddb6SLionel Sambuc    _BidirectionalIterator __m = __first;
34594684ddb6SLionel Sambuc    _Distance __len2 = __len / 2;  // __len2 >= 2
34604684ddb6SLionel Sambuc    _VSTD::advance(__m, __len2);
34614684ddb6SLionel Sambuc    // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
34624684ddb6SLionel Sambuc    // F????????????????T
34634684ddb6SLionel Sambuc    // f       m        l
34644684ddb6SLionel Sambuc    _BidirectionalIterator __m1 = __m;
34654684ddb6SLionel Sambuc    _BidirectionalIterator __first_false = __first;
34664684ddb6SLionel Sambuc    _Distance __len_half = __len2;
34674684ddb6SLionel Sambuc    while (!__pred(*--__m1))
34684684ddb6SLionel Sambuc    {
34694684ddb6SLionel Sambuc        if (__m1 == __first)
34704684ddb6SLionel Sambuc            goto __first_half_done;
34714684ddb6SLionel Sambuc        --__len_half;
34724684ddb6SLionel Sambuc    }
34734684ddb6SLionel Sambuc    // F???TFFF?????????T
34744684ddb6SLionel Sambuc    // f   m1  m        l
34754684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
34764684ddb6SLionel Sambuc    __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
34774684ddb6SLionel Sambuc__first_half_done:
34784684ddb6SLionel Sambuc    // TTTFFFFF?????????T
34794684ddb6SLionel Sambuc    // f  ff   m        l
34804684ddb6SLionel Sambuc    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
34814684ddb6SLionel Sambuc    __m1 = __m;
34824684ddb6SLionel Sambuc    _BidirectionalIterator __second_false = __last;
34834684ddb6SLionel Sambuc    ++__second_false;
34844684ddb6SLionel Sambuc    __len_half = __len - __len2;
34854684ddb6SLionel Sambuc    while (__pred(*__m1))
34864684ddb6SLionel Sambuc    {
34874684ddb6SLionel Sambuc        if (++__m1 == __last)
34884684ddb6SLionel Sambuc            goto __second_half_done;
34894684ddb6SLionel Sambuc        --__len_half;
34904684ddb6SLionel Sambuc    }
34914684ddb6SLionel Sambuc    // TTTFFFFFTTTF?????T
34924684ddb6SLionel Sambuc    // f  ff   m  m1    l
34934684ddb6SLionel Sambuc    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
34944684ddb6SLionel Sambuc__second_half_done:
34954684ddb6SLionel Sambuc    // TTTFFFFFTTTTTFFFFF
34964684ddb6SLionel Sambuc    // f  ff   m    sf  l
34974684ddb6SLionel Sambuc    return _VSTD::rotate(__first_false, __m, __second_false);
34984684ddb6SLionel Sambuc    // TTTTTTTTFFFFFFFFFF
34994684ddb6SLionel Sambuc    //         |
35004684ddb6SLionel Sambuc}
35014684ddb6SLionel Sambuc
35024684ddb6SLionel Sambuctemplate <class _Predicate, class _BidirectionalIterator>
35034684ddb6SLionel Sambuc_BidirectionalIterator
35044684ddb6SLionel Sambuc__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
35054684ddb6SLionel Sambuc                   bidirectional_iterator_tag)
35064684ddb6SLionel Sambuc{
35074684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
35084684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
35094684ddb6SLionel Sambuc    const difference_type __alloc_limit = 4;  // might want to make this a function of trivial assignment
35104684ddb6SLionel Sambuc    // Either prove all true and return __first or point to first false
35114684ddb6SLionel Sambuc    while (true)
35124684ddb6SLionel Sambuc    {
35134684ddb6SLionel Sambuc        if (__first == __last)
35144684ddb6SLionel Sambuc            return __first;
35154684ddb6SLionel Sambuc        if (!__pred(*__first))
35164684ddb6SLionel Sambuc            break;
35174684ddb6SLionel Sambuc        ++__first;
35184684ddb6SLionel Sambuc    }
35194684ddb6SLionel Sambuc    // __first points to first false, everything prior to __first is already set.
35204684ddb6SLionel Sambuc    // Either prove [__first, __last) is all false and return __first, or point __last to last true
35214684ddb6SLionel Sambuc    do
35224684ddb6SLionel Sambuc    {
35234684ddb6SLionel Sambuc        if (__first == --__last)
35244684ddb6SLionel Sambuc            return __first;
35254684ddb6SLionel Sambuc    } while (!__pred(*__last));
35264684ddb6SLionel Sambuc    // We now have a reduced range [__first, __last]
35274684ddb6SLionel Sambuc    // *__first is known to be false
35284684ddb6SLionel Sambuc    // *__last is known to be true
35294684ddb6SLionel Sambuc    // __len >= 2
35304684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last) + 1;
35314684ddb6SLionel Sambuc    pair<value_type*, ptrdiff_t> __p(0, 0);
35324684ddb6SLionel Sambuc    unique_ptr<value_type, __return_temporary_buffer> __h;
35334684ddb6SLionel Sambuc    if (__len >= __alloc_limit)
35344684ddb6SLionel Sambuc    {
35354684ddb6SLionel Sambuc        __p = _VSTD::get_temporary_buffer<value_type>(__len);
35364684ddb6SLionel Sambuc        __h.reset(__p.first);
35374684ddb6SLionel Sambuc    }
35384684ddb6SLionel Sambuc    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
35394684ddb6SLionel Sambuc                             (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
35404684ddb6SLionel Sambuc}
35414684ddb6SLionel Sambuc
35424684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Predicate>
35434684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
35444684ddb6SLionel Sambuc_ForwardIterator
35454684ddb6SLionel Sambucstable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
35464684ddb6SLionel Sambuc{
35474684ddb6SLionel Sambuc    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
35484684ddb6SLionel Sambuc                             (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
35494684ddb6SLionel Sambuc}
35504684ddb6SLionel Sambuc
35514684ddb6SLionel Sambuc// is_sorted_until
35524684ddb6SLionel Sambuc
35534684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Compare>
35544684ddb6SLionel Sambuc_ForwardIterator
35554684ddb6SLionel Sambucis_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
35564684ddb6SLionel Sambuc{
35574684ddb6SLionel Sambuc    if (__first != __last)
35584684ddb6SLionel Sambuc    {
35594684ddb6SLionel Sambuc        _ForwardIterator __i = __first;
35604684ddb6SLionel Sambuc        while (++__i != __last)
35614684ddb6SLionel Sambuc        {
35624684ddb6SLionel Sambuc            if (__comp(*__i, *__first))
35634684ddb6SLionel Sambuc                return __i;
35644684ddb6SLionel Sambuc            __first = __i;
35654684ddb6SLionel Sambuc        }
35664684ddb6SLionel Sambuc    }
35674684ddb6SLionel Sambuc    return __last;
35684684ddb6SLionel Sambuc}
35694684ddb6SLionel Sambuc
35704684ddb6SLionel Sambuctemplate<class _ForwardIterator>
35714684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
35724684ddb6SLionel Sambuc_ForwardIterator
35734684ddb6SLionel Sambucis_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
35744684ddb6SLionel Sambuc{
35754684ddb6SLionel Sambuc    return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
35764684ddb6SLionel Sambuc}
35774684ddb6SLionel Sambuc
35784684ddb6SLionel Sambuc// is_sorted
35794684ddb6SLionel Sambuc
35804684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Compare>
35814684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
35824684ddb6SLionel Sambucbool
35834684ddb6SLionel Sambucis_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
35844684ddb6SLionel Sambuc{
35854684ddb6SLionel Sambuc    return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
35864684ddb6SLionel Sambuc}
35874684ddb6SLionel Sambuc
35884684ddb6SLionel Sambuctemplate<class _ForwardIterator>
35894684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
35904684ddb6SLionel Sambucbool
35914684ddb6SLionel Sambucis_sorted(_ForwardIterator __first, _ForwardIterator __last)
35924684ddb6SLionel Sambuc{
35934684ddb6SLionel Sambuc    return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
35944684ddb6SLionel Sambuc}
35954684ddb6SLionel Sambuc
35964684ddb6SLionel Sambuc// sort
35974684ddb6SLionel Sambuc
35984684ddb6SLionel Sambuc// stable, 2-3 compares, 0-2 swaps
35994684ddb6SLionel Sambuc
36004684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator>
36014684ddb6SLionel Sambucunsigned
36024684ddb6SLionel Sambuc__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
36034684ddb6SLionel Sambuc{
36044684ddb6SLionel Sambuc    unsigned __r = 0;
36054684ddb6SLionel Sambuc    if (!__c(*__y, *__x))          // if x <= y
36064684ddb6SLionel Sambuc    {
36074684ddb6SLionel Sambuc        if (!__c(*__z, *__y))      // if y <= z
36084684ddb6SLionel Sambuc            return __r;            // x <= y && y <= z
36094684ddb6SLionel Sambuc                                   // x <= y && y > z
36104684ddb6SLionel Sambuc        swap(*__y, *__z);          // x <= z && y < z
36114684ddb6SLionel Sambuc        __r = 1;
36124684ddb6SLionel Sambuc        if (__c(*__y, *__x))       // if x > y
36134684ddb6SLionel Sambuc        {
36144684ddb6SLionel Sambuc            swap(*__x, *__y);      // x < y && y <= z
36154684ddb6SLionel Sambuc            __r = 2;
36164684ddb6SLionel Sambuc        }
36174684ddb6SLionel Sambuc        return __r;                // x <= y && y < z
36184684ddb6SLionel Sambuc    }
36194684ddb6SLionel Sambuc    if (__c(*__z, *__y))           // x > y, if y > z
36204684ddb6SLionel Sambuc    {
36214684ddb6SLionel Sambuc        swap(*__x, *__z);          // x < y && y < z
36224684ddb6SLionel Sambuc        __r = 1;
36234684ddb6SLionel Sambuc        return __r;
36244684ddb6SLionel Sambuc    }
36254684ddb6SLionel Sambuc    swap(*__x, *__y);              // x > y && y <= z
36264684ddb6SLionel Sambuc    __r = 1;                       // x < y && x <= z
36274684ddb6SLionel Sambuc    if (__c(*__z, *__y))           // if y > z
36284684ddb6SLionel Sambuc    {
36294684ddb6SLionel Sambuc        swap(*__y, *__z);          // x <= y && y < z
36304684ddb6SLionel Sambuc        __r = 2;
36314684ddb6SLionel Sambuc    }
36324684ddb6SLionel Sambuc    return __r;
36334684ddb6SLionel Sambuc}                                  // x <= y && y <= z
36344684ddb6SLionel Sambuc
36354684ddb6SLionel Sambuc// stable, 3-6 compares, 0-5 swaps
36364684ddb6SLionel Sambuc
36374684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator>
36384684ddb6SLionel Sambucunsigned
36394684ddb6SLionel Sambuc__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
36404684ddb6SLionel Sambuc            _ForwardIterator __x4, _Compare __c)
36414684ddb6SLionel Sambuc{
36424684ddb6SLionel Sambuc    unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
36434684ddb6SLionel Sambuc    if (__c(*__x4, *__x3))
36444684ddb6SLionel Sambuc    {
36454684ddb6SLionel Sambuc        swap(*__x3, *__x4);
36464684ddb6SLionel Sambuc        ++__r;
36474684ddb6SLionel Sambuc        if (__c(*__x3, *__x2))
36484684ddb6SLionel Sambuc        {
36494684ddb6SLionel Sambuc            swap(*__x2, *__x3);
36504684ddb6SLionel Sambuc            ++__r;
36514684ddb6SLionel Sambuc            if (__c(*__x2, *__x1))
36524684ddb6SLionel Sambuc            {
36534684ddb6SLionel Sambuc                swap(*__x1, *__x2);
36544684ddb6SLionel Sambuc                ++__r;
36554684ddb6SLionel Sambuc            }
36564684ddb6SLionel Sambuc        }
36574684ddb6SLionel Sambuc    }
36584684ddb6SLionel Sambuc    return __r;
36594684ddb6SLionel Sambuc}
36604684ddb6SLionel Sambuc
36614684ddb6SLionel Sambuc// stable, 4-10 compares, 0-9 swaps
36624684ddb6SLionel Sambuc
36634684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator>
36644684ddb6SLionel Sambucunsigned
36654684ddb6SLionel Sambuc__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
36664684ddb6SLionel Sambuc            _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
36674684ddb6SLionel Sambuc{
36684684ddb6SLionel Sambuc    unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
36694684ddb6SLionel Sambuc    if (__c(*__x5, *__x4))
36704684ddb6SLionel Sambuc    {
36714684ddb6SLionel Sambuc        swap(*__x4, *__x5);
36724684ddb6SLionel Sambuc        ++__r;
36734684ddb6SLionel Sambuc        if (__c(*__x4, *__x3))
36744684ddb6SLionel Sambuc        {
36754684ddb6SLionel Sambuc            swap(*__x3, *__x4);
36764684ddb6SLionel Sambuc            ++__r;
36774684ddb6SLionel Sambuc            if (__c(*__x3, *__x2))
36784684ddb6SLionel Sambuc            {
36794684ddb6SLionel Sambuc                swap(*__x2, *__x3);
36804684ddb6SLionel Sambuc                ++__r;
36814684ddb6SLionel Sambuc                if (__c(*__x2, *__x1))
36824684ddb6SLionel Sambuc                {
36834684ddb6SLionel Sambuc                    swap(*__x1, *__x2);
36844684ddb6SLionel Sambuc                    ++__r;
36854684ddb6SLionel Sambuc                }
36864684ddb6SLionel Sambuc            }
36874684ddb6SLionel Sambuc        }
36884684ddb6SLionel Sambuc    }
36894684ddb6SLionel Sambuc    return __r;
36904684ddb6SLionel Sambuc}
36914684ddb6SLionel Sambuc
36924684ddb6SLionel Sambuc// Assumes size > 0
36934684ddb6SLionel Sambuctemplate <class _Compare, class _BirdirectionalIterator>
36944684ddb6SLionel Sambucvoid
36954684ddb6SLionel Sambuc__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
36964684ddb6SLionel Sambuc{
36974684ddb6SLionel Sambuc    _BirdirectionalIterator __lm1 = __last;
36984684ddb6SLionel Sambuc    for (--__lm1; __first != __lm1; ++__first)
36994684ddb6SLionel Sambuc    {
37004684ddb6SLionel Sambuc        _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
37014684ddb6SLionel Sambuc                                                        typename add_lvalue_reference<_Compare>::type>
37024684ddb6SLionel Sambuc                                                       (__first, __last, __comp);
37034684ddb6SLionel Sambuc        if (__i != __first)
37044684ddb6SLionel Sambuc            swap(*__first, *__i);
37054684ddb6SLionel Sambuc    }
37064684ddb6SLionel Sambuc}
37074684ddb6SLionel Sambuc
37084684ddb6SLionel Sambuctemplate <class _Compare, class _BirdirectionalIterator>
37094684ddb6SLionel Sambucvoid
37104684ddb6SLionel Sambuc__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
37114684ddb6SLionel Sambuc{
37124684ddb6SLionel Sambuc    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
37134684ddb6SLionel Sambuc    if (__first != __last)
37144684ddb6SLionel Sambuc    {
37154684ddb6SLionel Sambuc        _BirdirectionalIterator __i = __first;
37164684ddb6SLionel Sambuc        for (++__i; __i != __last; ++__i)
37174684ddb6SLionel Sambuc        {
37184684ddb6SLionel Sambuc            _BirdirectionalIterator __j = __i;
37194684ddb6SLionel Sambuc            value_type __t(_VSTD::move(*__j));
37204684ddb6SLionel Sambuc            for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t,  *--__k); --__j)
37214684ddb6SLionel Sambuc                *__j = _VSTD::move(*__k);
37224684ddb6SLionel Sambuc            *__j = _VSTD::move(__t);
37234684ddb6SLionel Sambuc        }
37244684ddb6SLionel Sambuc    }
37254684ddb6SLionel Sambuc}
37264684ddb6SLionel Sambuc
37274684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
37284684ddb6SLionel Sambucvoid
37294684ddb6SLionel Sambuc__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
37304684ddb6SLionel Sambuc{
37314684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
37324684ddb6SLionel Sambuc    _RandomAccessIterator __j = __first+2;
37334684ddb6SLionel Sambuc    __sort3<_Compare>(__first, __first+1, __j, __comp);
37344684ddb6SLionel Sambuc    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
37354684ddb6SLionel Sambuc    {
37364684ddb6SLionel Sambuc        if (__comp(*__i, *__j))
37374684ddb6SLionel Sambuc        {
37384684ddb6SLionel Sambuc            value_type __t(_VSTD::move(*__i));
37394684ddb6SLionel Sambuc            _RandomAccessIterator __k = __j;
37404684ddb6SLionel Sambuc            __j = __i;
37414684ddb6SLionel Sambuc            do
37424684ddb6SLionel Sambuc            {
37434684ddb6SLionel Sambuc                *__j = _VSTD::move(*__k);
37444684ddb6SLionel Sambuc                __j = __k;
37454684ddb6SLionel Sambuc            } while (__j != __first && __comp(__t, *--__k));
37464684ddb6SLionel Sambuc            *__j = _VSTD::move(__t);
37474684ddb6SLionel Sambuc        }
37484684ddb6SLionel Sambuc        __j = __i;
37494684ddb6SLionel Sambuc    }
37504684ddb6SLionel Sambuc}
37514684ddb6SLionel Sambuc
37524684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
37534684ddb6SLionel Sambucbool
37544684ddb6SLionel Sambuc__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
37554684ddb6SLionel Sambuc{
37564684ddb6SLionel Sambuc    switch (__last - __first)
37574684ddb6SLionel Sambuc    {
37584684ddb6SLionel Sambuc    case 0:
37594684ddb6SLionel Sambuc    case 1:
37604684ddb6SLionel Sambuc        return true;
37614684ddb6SLionel Sambuc    case 2:
37624684ddb6SLionel Sambuc        if (__comp(*--__last, *__first))
37634684ddb6SLionel Sambuc            swap(*__first, *__last);
37644684ddb6SLionel Sambuc        return true;
37654684ddb6SLionel Sambuc    case 3:
37664684ddb6SLionel Sambuc        _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
37674684ddb6SLionel Sambuc        return true;
37684684ddb6SLionel Sambuc    case 4:
37694684ddb6SLionel Sambuc        _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
37704684ddb6SLionel Sambuc        return true;
37714684ddb6SLionel Sambuc    case 5:
37724684ddb6SLionel Sambuc        _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
37734684ddb6SLionel Sambuc        return true;
37744684ddb6SLionel Sambuc    }
37754684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
37764684ddb6SLionel Sambuc    _RandomAccessIterator __j = __first+2;
37774684ddb6SLionel Sambuc    __sort3<_Compare>(__first, __first+1, __j, __comp);
37784684ddb6SLionel Sambuc    const unsigned __limit = 8;
37794684ddb6SLionel Sambuc    unsigned __count = 0;
37804684ddb6SLionel Sambuc    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
37814684ddb6SLionel Sambuc    {
37824684ddb6SLionel Sambuc        if (__comp(*__i, *__j))
37834684ddb6SLionel Sambuc        {
37844684ddb6SLionel Sambuc            value_type __t(_VSTD::move(*__i));
37854684ddb6SLionel Sambuc            _RandomAccessIterator __k = __j;
37864684ddb6SLionel Sambuc            __j = __i;
37874684ddb6SLionel Sambuc            do
37884684ddb6SLionel Sambuc            {
37894684ddb6SLionel Sambuc                *__j = _VSTD::move(*__k);
37904684ddb6SLionel Sambuc                __j = __k;
37914684ddb6SLionel Sambuc            } while (__j != __first && __comp(__t, *--__k));
37924684ddb6SLionel Sambuc            *__j = _VSTD::move(__t);
37934684ddb6SLionel Sambuc            if (++__count == __limit)
37944684ddb6SLionel Sambuc                return ++__i == __last;
37954684ddb6SLionel Sambuc        }
37964684ddb6SLionel Sambuc        __j = __i;
37974684ddb6SLionel Sambuc    }
37984684ddb6SLionel Sambuc    return true;
37994684ddb6SLionel Sambuc}
38004684ddb6SLionel Sambuc
38014684ddb6SLionel Sambuctemplate <class _Compare, class _BirdirectionalIterator>
38024684ddb6SLionel Sambucvoid
38034684ddb6SLionel Sambuc__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
38044684ddb6SLionel Sambuc                      typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
38054684ddb6SLionel Sambuc{
38064684ddb6SLionel Sambuc    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
38074684ddb6SLionel Sambuc    if (__first1 != __last1)
38084684ddb6SLionel Sambuc    {
38094684ddb6SLionel Sambuc        __destruct_n __d(0);
38104684ddb6SLionel Sambuc        unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
38114684ddb6SLionel Sambuc        value_type* __last2 = __first2;
38124684ddb6SLionel Sambuc        ::new(__last2) value_type(_VSTD::move(*__first1));
38134684ddb6SLionel Sambuc        __d.__incr((value_type*)0);
38144684ddb6SLionel Sambuc        for (++__last2; ++__first1 != __last1; ++__last2)
38154684ddb6SLionel Sambuc        {
38164684ddb6SLionel Sambuc            value_type* __j2 = __last2;
38174684ddb6SLionel Sambuc            value_type* __i2 = __j2;
38184684ddb6SLionel Sambuc            if (__comp(*__first1, *--__i2))
38194684ddb6SLionel Sambuc            {
38204684ddb6SLionel Sambuc                ::new(__j2) value_type(_VSTD::move(*__i2));
38214684ddb6SLionel Sambuc                __d.__incr((value_type*)0);
38224684ddb6SLionel Sambuc                for (--__j2; __i2 != __first2 && __comp(*__first1,  *--__i2); --__j2)
38234684ddb6SLionel Sambuc                    *__j2 = _VSTD::move(*__i2);
38244684ddb6SLionel Sambuc                *__j2 = _VSTD::move(*__first1);
38254684ddb6SLionel Sambuc            }
38264684ddb6SLionel Sambuc            else
38274684ddb6SLionel Sambuc            {
38284684ddb6SLionel Sambuc                ::new(__j2) value_type(_VSTD::move(*__first1));
38294684ddb6SLionel Sambuc                __d.__incr((value_type*)0);
38304684ddb6SLionel Sambuc            }
38314684ddb6SLionel Sambuc        }
38324684ddb6SLionel Sambuc        __h.release();
38334684ddb6SLionel Sambuc    }
38344684ddb6SLionel Sambuc}
38354684ddb6SLionel Sambuc
38364684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
38374684ddb6SLionel Sambucvoid
38384684ddb6SLionel Sambuc__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
38394684ddb6SLionel Sambuc{
38404684ddb6SLionel Sambuc    // _Compare is known to be a reference type
38414684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
38424684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
38434684ddb6SLionel Sambuc    const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
38444684ddb6SLionel Sambuc                                    is_trivially_copy_assignable<value_type>::value ? 30 : 6;
38454684ddb6SLionel Sambuc    while (true)
38464684ddb6SLionel Sambuc    {
38474684ddb6SLionel Sambuc    __restart:
38484684ddb6SLionel Sambuc        difference_type __len = __last - __first;
38494684ddb6SLionel Sambuc        switch (__len)
38504684ddb6SLionel Sambuc        {
38514684ddb6SLionel Sambuc        case 0:
38524684ddb6SLionel Sambuc        case 1:
38534684ddb6SLionel Sambuc            return;
38544684ddb6SLionel Sambuc        case 2:
38554684ddb6SLionel Sambuc            if (__comp(*--__last, *__first))
38564684ddb6SLionel Sambuc                swap(*__first, *__last);
38574684ddb6SLionel Sambuc            return;
38584684ddb6SLionel Sambuc        case 3:
38594684ddb6SLionel Sambuc            _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
38604684ddb6SLionel Sambuc            return;
38614684ddb6SLionel Sambuc        case 4:
38624684ddb6SLionel Sambuc            _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
38634684ddb6SLionel Sambuc            return;
38644684ddb6SLionel Sambuc        case 5:
38654684ddb6SLionel Sambuc            _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
38664684ddb6SLionel Sambuc            return;
38674684ddb6SLionel Sambuc        }
38684684ddb6SLionel Sambuc        if (__len <= __limit)
38694684ddb6SLionel Sambuc        {
38704684ddb6SLionel Sambuc            _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
38714684ddb6SLionel Sambuc            return;
38724684ddb6SLionel Sambuc        }
38734684ddb6SLionel Sambuc        // __len > 5
38744684ddb6SLionel Sambuc        _RandomAccessIterator __m = __first;
38754684ddb6SLionel Sambuc        _RandomAccessIterator __lm1 = __last;
38764684ddb6SLionel Sambuc        --__lm1;
38774684ddb6SLionel Sambuc        unsigned __n_swaps;
38784684ddb6SLionel Sambuc        {
38794684ddb6SLionel Sambuc        difference_type __delta;
38804684ddb6SLionel Sambuc        if (__len >= 1000)
38814684ddb6SLionel Sambuc        {
38824684ddb6SLionel Sambuc            __delta = __len/2;
38834684ddb6SLionel Sambuc            __m += __delta;
38844684ddb6SLionel Sambuc            __delta /= 2;
38854684ddb6SLionel Sambuc            __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
38864684ddb6SLionel Sambuc        }
38874684ddb6SLionel Sambuc        else
38884684ddb6SLionel Sambuc        {
38894684ddb6SLionel Sambuc            __delta = __len/2;
38904684ddb6SLionel Sambuc            __m += __delta;
38914684ddb6SLionel Sambuc            __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
38924684ddb6SLionel Sambuc        }
38934684ddb6SLionel Sambuc        }
38944684ddb6SLionel Sambuc        // *__m is median
38954684ddb6SLionel Sambuc        // partition [__first, __m) < *__m and *__m <= [__m, __last)
38964684ddb6SLionel Sambuc        // (this inhibits tossing elements equivalent to __m around unnecessarily)
38974684ddb6SLionel Sambuc        _RandomAccessIterator __i = __first;
38984684ddb6SLionel Sambuc        _RandomAccessIterator __j = __lm1;
38994684ddb6SLionel Sambuc        // j points beyond range to be tested, *__m is known to be <= *__lm1
39004684ddb6SLionel Sambuc        // The search going up is known to be guarded but the search coming down isn't.
39014684ddb6SLionel Sambuc        // Prime the downward search with a guard.
39024684ddb6SLionel Sambuc        if (!__comp(*__i, *__m))  // if *__first == *__m
39034684ddb6SLionel Sambuc        {
39044684ddb6SLionel Sambuc            // *__first == *__m, *__first doesn't go in first part
39054684ddb6SLionel Sambuc            // manually guard downward moving __j against __i
39064684ddb6SLionel Sambuc            while (true)
39074684ddb6SLionel Sambuc            {
39084684ddb6SLionel Sambuc                if (__i == --__j)
39094684ddb6SLionel Sambuc                {
39104684ddb6SLionel Sambuc                    // *__first == *__m, *__m <= all other elements
39114684ddb6SLionel Sambuc                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
39124684ddb6SLionel Sambuc                    ++__i;  // __first + 1
39134684ddb6SLionel Sambuc                    __j = __last;
39144684ddb6SLionel Sambuc                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
39154684ddb6SLionel Sambuc                    {
39164684ddb6SLionel Sambuc                        while (true)
39174684ddb6SLionel Sambuc                        {
39184684ddb6SLionel Sambuc                            if (__i == __j)
39194684ddb6SLionel Sambuc                                return;  // [__first, __last) all equivalent elements
39204684ddb6SLionel Sambuc                            if (__comp(*__first, *__i))
39214684ddb6SLionel Sambuc                            {
39224684ddb6SLionel Sambuc                                swap(*__i, *__j);
39234684ddb6SLionel Sambuc                                ++__n_swaps;
39244684ddb6SLionel Sambuc                                ++__i;
39254684ddb6SLionel Sambuc                                break;
39264684ddb6SLionel Sambuc                            }
39274684ddb6SLionel Sambuc                            ++__i;
39284684ddb6SLionel Sambuc                        }
39294684ddb6SLionel Sambuc                    }
39304684ddb6SLionel Sambuc                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
39314684ddb6SLionel Sambuc                    if (__i == __j)
39324684ddb6SLionel Sambuc                        return;
39334684ddb6SLionel Sambuc                    while (true)
39344684ddb6SLionel Sambuc                    {
39354684ddb6SLionel Sambuc                        while (!__comp(*__first, *__i))
39364684ddb6SLionel Sambuc                            ++__i;
39374684ddb6SLionel Sambuc                        while (__comp(*__first, *--__j))
39384684ddb6SLionel Sambuc                            ;
39394684ddb6SLionel Sambuc                        if (__i >= __j)
39404684ddb6SLionel Sambuc                            break;
39414684ddb6SLionel Sambuc                        swap(*__i, *__j);
39424684ddb6SLionel Sambuc                        ++__n_swaps;
39434684ddb6SLionel Sambuc                        ++__i;
39444684ddb6SLionel Sambuc                    }
39454684ddb6SLionel Sambuc                    // [__first, __i) == *__first and *__first < [__i, __last)
39464684ddb6SLionel Sambuc                    // The first part is sorted, sort the secod part
39474684ddb6SLionel Sambuc                    // _VSTD::__sort<_Compare>(__i, __last, __comp);
39484684ddb6SLionel Sambuc                    __first = __i;
39494684ddb6SLionel Sambuc                    goto __restart;
39504684ddb6SLionel Sambuc                }
39514684ddb6SLionel Sambuc                if (__comp(*__j, *__m))
39524684ddb6SLionel Sambuc                {
39534684ddb6SLionel Sambuc                    swap(*__i, *__j);
39544684ddb6SLionel Sambuc                    ++__n_swaps;
39554684ddb6SLionel Sambuc                    break;  // found guard for downward moving __j, now use unguarded partition
39564684ddb6SLionel Sambuc                }
39574684ddb6SLionel Sambuc            }
39584684ddb6SLionel Sambuc        }
39594684ddb6SLionel Sambuc        // It is known that *__i < *__m
39604684ddb6SLionel Sambuc        ++__i;
39614684ddb6SLionel Sambuc        // j points beyond range to be tested, *__m is known to be <= *__lm1
39624684ddb6SLionel Sambuc        // if not yet partitioned...
39634684ddb6SLionel Sambuc        if (__i < __j)
39644684ddb6SLionel Sambuc        {
39654684ddb6SLionel Sambuc            // known that *(__i - 1) < *__m
39664684ddb6SLionel Sambuc            // known that __i <= __m
39674684ddb6SLionel Sambuc            while (true)
39684684ddb6SLionel Sambuc            {
39694684ddb6SLionel Sambuc                // __m still guards upward moving __i
39704684ddb6SLionel Sambuc                while (__comp(*__i, *__m))
39714684ddb6SLionel Sambuc                    ++__i;
39724684ddb6SLionel Sambuc                // It is now known that a guard exists for downward moving __j
39734684ddb6SLionel Sambuc                while (!__comp(*--__j, *__m))
39744684ddb6SLionel Sambuc                    ;
39754684ddb6SLionel Sambuc                if (__i > __j)
39764684ddb6SLionel Sambuc                    break;
39774684ddb6SLionel Sambuc                swap(*__i, *__j);
39784684ddb6SLionel Sambuc                ++__n_swaps;
39794684ddb6SLionel Sambuc                // It is known that __m != __j
39804684ddb6SLionel Sambuc                // If __m just moved, follow it
39814684ddb6SLionel Sambuc                if (__m == __i)
39824684ddb6SLionel Sambuc                    __m = __j;
39834684ddb6SLionel Sambuc                ++__i;
39844684ddb6SLionel Sambuc            }
39854684ddb6SLionel Sambuc        }
39864684ddb6SLionel Sambuc        // [__first, __i) < *__m and *__m <= [__i, __last)
39874684ddb6SLionel Sambuc        if (__i != __m && __comp(*__m, *__i))
39884684ddb6SLionel Sambuc        {
39894684ddb6SLionel Sambuc            swap(*__i, *__m);
39904684ddb6SLionel Sambuc            ++__n_swaps;
39914684ddb6SLionel Sambuc        }
39924684ddb6SLionel Sambuc        // [__first, __i) < *__i and *__i <= [__i+1, __last)
39934684ddb6SLionel Sambuc        // If we were given a perfect partition, see if insertion sort is quick...
39944684ddb6SLionel Sambuc        if (__n_swaps == 0)
39954684ddb6SLionel Sambuc        {
39964684ddb6SLionel Sambuc            bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
39974684ddb6SLionel Sambuc            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
39984684ddb6SLionel Sambuc            {
39994684ddb6SLionel Sambuc                if (__fs)
40004684ddb6SLionel Sambuc                    return;
40014684ddb6SLionel Sambuc                __last = __i;
40024684ddb6SLionel Sambuc                continue;
40034684ddb6SLionel Sambuc            }
40044684ddb6SLionel Sambuc            else
40054684ddb6SLionel Sambuc            {
40064684ddb6SLionel Sambuc                if (__fs)
40074684ddb6SLionel Sambuc                {
40084684ddb6SLionel Sambuc                    __first = ++__i;
40094684ddb6SLionel Sambuc                    continue;
40104684ddb6SLionel Sambuc                }
40114684ddb6SLionel Sambuc            }
40124684ddb6SLionel Sambuc        }
40134684ddb6SLionel Sambuc        // sort smaller range with recursive call and larger with tail recursion elimination
40144684ddb6SLionel Sambuc        if (__i - __first < __last - __i)
40154684ddb6SLionel Sambuc        {
40164684ddb6SLionel Sambuc            _VSTD::__sort<_Compare>(__first, __i, __comp);
40174684ddb6SLionel Sambuc            // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
40184684ddb6SLionel Sambuc            __first = ++__i;
40194684ddb6SLionel Sambuc        }
40204684ddb6SLionel Sambuc        else
40214684ddb6SLionel Sambuc        {
40224684ddb6SLionel Sambuc            _VSTD::__sort<_Compare>(__i+1, __last, __comp);
40234684ddb6SLionel Sambuc            // _VSTD::__sort<_Compare>(__first, __i, __comp);
40244684ddb6SLionel Sambuc            __last = __i;
40254684ddb6SLionel Sambuc        }
40264684ddb6SLionel Sambuc    }
40274684ddb6SLionel Sambuc}
40284684ddb6SLionel Sambuc
40294684ddb6SLionel Sambuc// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
40304684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
40314684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
40324684ddb6SLionel Sambucvoid
40334684ddb6SLionel Sambucsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
40344684ddb6SLionel Sambuc{
40354684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
40364684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
40374684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
40384684ddb6SLionel Sambuc    __sort<_Comp_ref>(__first, __last, __c);
40394684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
40404684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
40414684ddb6SLionel Sambuc    __sort<_Comp_ref>(__first, __last, __comp);
40424684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
40434684ddb6SLionel Sambuc}
40444684ddb6SLionel Sambuc
40454684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
40464684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
40474684ddb6SLionel Sambucvoid
40484684ddb6SLionel Sambucsort(_RandomAccessIterator __first, _RandomAccessIterator __last)
40494684ddb6SLionel Sambuc{
40504684ddb6SLionel Sambuc    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
40514684ddb6SLionel Sambuc}
40524684ddb6SLionel Sambuc
40534684ddb6SLionel Sambuctemplate <class _Tp>
40544684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
40554684ddb6SLionel Sambucvoid
40564684ddb6SLionel Sambucsort(_Tp** __first, _Tp** __last)
40574684ddb6SLionel Sambuc{
40584684ddb6SLionel Sambuc    _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
40594684ddb6SLionel Sambuc}
40604684ddb6SLionel Sambuc
40614684ddb6SLionel Sambuctemplate <class _Tp>
40624684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
40634684ddb6SLionel Sambucvoid
40644684ddb6SLionel Sambucsort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
40654684ddb6SLionel Sambuc{
40664684ddb6SLionel Sambuc    _VSTD::sort(__first.base(), __last.base());
40674684ddb6SLionel Sambuc}
40684684ddb6SLionel Sambuc
40694684ddb6SLionel Sambuctemplate <class _Tp, class _Compare>
40704684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
40714684ddb6SLionel Sambucvoid
40724684ddb6SLionel Sambucsort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
40734684ddb6SLionel Sambuc{
40744684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
40754684ddb6SLionel Sambuc    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
40764684ddb6SLionel Sambuc}
40774684ddb6SLionel Sambuc
40784684ddb6SLionel Sambuc#ifdef _LIBCPP_MSVC
40794684ddb6SLionel Sambuc#pragma warning( push )
40804684ddb6SLionel Sambuc#pragma warning( disable: 4231)
40814684ddb6SLionel Sambuc#endif // _LIBCPP_MSVC
40824684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
40834684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
40844684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
40854684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
40864684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
40874684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
40884684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
40894684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
40904684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
40914684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
40924684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
40934684ddb6SLionel Sambuc_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>&))
40944684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
40954684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
40964684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
40974684ddb6SLionel Sambuc
40984684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
40994684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
41004684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
41014684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
41024684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
41034684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
41044684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
41054684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
41064684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
41074684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
41084684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
41094684ddb6SLionel Sambuc_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>&))
41104684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
41114684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
41124684ddb6SLionel Sambuc_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
41134684ddb6SLionel Sambuc
41144684ddb6SLionel Sambuc_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>&))
41154684ddb6SLionel Sambuc#ifdef _LIBCPP_MSVC
41164684ddb6SLionel Sambuc#pragma warning( pop )
41174684ddb6SLionel Sambuc#endif  // _LIBCPP_MSVC
41184684ddb6SLionel Sambuc
41194684ddb6SLionel Sambuc// lower_bound
41204684ddb6SLionel Sambuc
41214684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator, class _Tp>
41224684ddb6SLionel Sambuc_ForwardIterator
41234684ddb6SLionel Sambuc__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
41244684ddb6SLionel Sambuc{
41254684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
41264684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last);
41274684ddb6SLionel Sambuc    while (__len != 0)
41284684ddb6SLionel Sambuc    {
41294684ddb6SLionel Sambuc        difference_type __l2 = __len / 2;
41304684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
41314684ddb6SLionel Sambuc        _VSTD::advance(__m, __l2);
41324684ddb6SLionel Sambuc        if (__comp(*__m, __value_))
41334684ddb6SLionel Sambuc        {
41344684ddb6SLionel Sambuc            __first = ++__m;
41354684ddb6SLionel Sambuc            __len -= __l2 + 1;
41364684ddb6SLionel Sambuc        }
41374684ddb6SLionel Sambuc        else
41384684ddb6SLionel Sambuc            __len = __l2;
41394684ddb6SLionel Sambuc    }
41404684ddb6SLionel Sambuc    return __first;
41414684ddb6SLionel Sambuc}
41424684ddb6SLionel Sambuc
41434684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp, class _Compare>
41444684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
41454684ddb6SLionel Sambuc_ForwardIterator
41464684ddb6SLionel Sambuclower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
41474684ddb6SLionel Sambuc{
41484684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
41494684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
41504684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
41514684ddb6SLionel Sambuc    return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
41524684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
41534684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
41544684ddb6SLionel Sambuc    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
41554684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
41564684ddb6SLionel Sambuc}
41574684ddb6SLionel Sambuc
41584684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
41594684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
41604684ddb6SLionel Sambuc_ForwardIterator
41614684ddb6SLionel Sambuclower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
41624684ddb6SLionel Sambuc{
41634684ddb6SLionel Sambuc    return _VSTD::lower_bound(__first, __last, __value_,
41644684ddb6SLionel Sambuc                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
41654684ddb6SLionel Sambuc}
41664684ddb6SLionel Sambuc
41674684ddb6SLionel Sambuc// upper_bound
41684684ddb6SLionel Sambuc
41694684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator, class _Tp>
41704684ddb6SLionel Sambuc_ForwardIterator
41714684ddb6SLionel Sambuc__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
41724684ddb6SLionel Sambuc{
41734684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
41744684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last);
41754684ddb6SLionel Sambuc    while (__len != 0)
41764684ddb6SLionel Sambuc    {
41774684ddb6SLionel Sambuc        difference_type __l2 = __len / 2;
41784684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
41794684ddb6SLionel Sambuc        _VSTD::advance(__m, __l2);
41804684ddb6SLionel Sambuc        if (__comp(__value_, *__m))
41814684ddb6SLionel Sambuc            __len = __l2;
41824684ddb6SLionel Sambuc        else
41834684ddb6SLionel Sambuc        {
41844684ddb6SLionel Sambuc            __first = ++__m;
41854684ddb6SLionel Sambuc            __len -= __l2 + 1;
41864684ddb6SLionel Sambuc        }
41874684ddb6SLionel Sambuc    }
41884684ddb6SLionel Sambuc    return __first;
41894684ddb6SLionel Sambuc}
41904684ddb6SLionel Sambuc
41914684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp, class _Compare>
41924684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
41934684ddb6SLionel Sambuc_ForwardIterator
41944684ddb6SLionel Sambucupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
41954684ddb6SLionel Sambuc{
41964684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
41974684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
41984684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
41994684ddb6SLionel Sambuc    return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
42004684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
42014684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
42024684ddb6SLionel Sambuc    return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
42034684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
42044684ddb6SLionel Sambuc}
42054684ddb6SLionel Sambuc
42064684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
42074684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
42084684ddb6SLionel Sambuc_ForwardIterator
42094684ddb6SLionel Sambucupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
42104684ddb6SLionel Sambuc{
42114684ddb6SLionel Sambuc    return _VSTD::upper_bound(__first, __last, __value_,
42124684ddb6SLionel Sambuc                             __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
42134684ddb6SLionel Sambuc}
42144684ddb6SLionel Sambuc
42154684ddb6SLionel Sambuc// equal_range
42164684ddb6SLionel Sambuc
42174684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator, class _Tp>
42184684ddb6SLionel Sambucpair<_ForwardIterator, _ForwardIterator>
42194684ddb6SLionel Sambuc__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
42204684ddb6SLionel Sambuc{
42214684ddb6SLionel Sambuc    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
42224684ddb6SLionel Sambuc    difference_type __len = _VSTD::distance(__first, __last);
42234684ddb6SLionel Sambuc    while (__len != 0)
42244684ddb6SLionel Sambuc    {
42254684ddb6SLionel Sambuc        difference_type __l2 = __len / 2;
42264684ddb6SLionel Sambuc        _ForwardIterator __m = __first;
42274684ddb6SLionel Sambuc        _VSTD::advance(__m, __l2);
42284684ddb6SLionel Sambuc        if (__comp(*__m, __value_))
42294684ddb6SLionel Sambuc        {
42304684ddb6SLionel Sambuc            __first = ++__m;
42314684ddb6SLionel Sambuc            __len -= __l2 + 1;
42324684ddb6SLionel Sambuc        }
42334684ddb6SLionel Sambuc        else if (__comp(__value_, *__m))
42344684ddb6SLionel Sambuc        {
42354684ddb6SLionel Sambuc            __last = __m;
42364684ddb6SLionel Sambuc            __len = __l2;
42374684ddb6SLionel Sambuc        }
42384684ddb6SLionel Sambuc        else
42394684ddb6SLionel Sambuc        {
42404684ddb6SLionel Sambuc            _ForwardIterator __mp1 = __m;
42414684ddb6SLionel Sambuc            return pair<_ForwardIterator, _ForwardIterator>
42424684ddb6SLionel Sambuc                   (
42434684ddb6SLionel Sambuc                      __lower_bound<_Compare>(__first, __m, __value_, __comp),
42444684ddb6SLionel Sambuc                      __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
42454684ddb6SLionel Sambuc                   );
42464684ddb6SLionel Sambuc        }
42474684ddb6SLionel Sambuc    }
42484684ddb6SLionel Sambuc    return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
42494684ddb6SLionel Sambuc}
42504684ddb6SLionel Sambuc
42514684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp, class _Compare>
42524684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
42534684ddb6SLionel Sambucpair<_ForwardIterator, _ForwardIterator>
42544684ddb6SLionel Sambucequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
42554684ddb6SLionel Sambuc{
42564684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
42574684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
42584684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
42594684ddb6SLionel Sambuc    return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
42604684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
42614684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
42624684ddb6SLionel Sambuc    return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
42634684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
42644684ddb6SLionel Sambuc}
42654684ddb6SLionel Sambuc
42664684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
42674684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
42684684ddb6SLionel Sambucpair<_ForwardIterator, _ForwardIterator>
42694684ddb6SLionel Sambucequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
42704684ddb6SLionel Sambuc{
42714684ddb6SLionel Sambuc    return _VSTD::equal_range(__first, __last, __value_,
42724684ddb6SLionel Sambuc                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
42734684ddb6SLionel Sambuc}
42744684ddb6SLionel Sambuc
42754684ddb6SLionel Sambuc// binary_search
42764684ddb6SLionel Sambuc
42774684ddb6SLionel Sambuctemplate <class _Compare, class _ForwardIterator, class _Tp>
42784684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
42794684ddb6SLionel Sambucbool
42804684ddb6SLionel Sambuc__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
42814684ddb6SLionel Sambuc{
42824684ddb6SLionel Sambuc    __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
42834684ddb6SLionel Sambuc    return __first != __last && !__comp(__value_, *__first);
42844684ddb6SLionel Sambuc}
42854684ddb6SLionel Sambuc
42864684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp, class _Compare>
42874684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
42884684ddb6SLionel Sambucbool
42894684ddb6SLionel Sambucbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
42904684ddb6SLionel Sambuc{
42914684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
42924684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
42934684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
42944684ddb6SLionel Sambuc    return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
42954684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
42964684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
42974684ddb6SLionel Sambuc    return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
42984684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
42994684ddb6SLionel Sambuc}
43004684ddb6SLionel Sambuc
43014684ddb6SLionel Sambuctemplate <class _ForwardIterator, class _Tp>
43024684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
43034684ddb6SLionel Sambucbool
43044684ddb6SLionel Sambucbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
43054684ddb6SLionel Sambuc{
43064684ddb6SLionel Sambuc    return _VSTD::binary_search(__first, __last, __value_,
43074684ddb6SLionel Sambuc                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
43084684ddb6SLionel Sambuc}
43094684ddb6SLionel Sambuc
43104684ddb6SLionel Sambuc// merge
43114684ddb6SLionel Sambuc
43124684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
43134684ddb6SLionel Sambuc_OutputIterator
43144684ddb6SLionel Sambuc__merge(_InputIterator1 __first1, _InputIterator1 __last1,
43154684ddb6SLionel Sambuc        _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
43164684ddb6SLionel Sambuc{
43174684ddb6SLionel Sambuc    for (; __first1 != __last1; ++__result)
43184684ddb6SLionel Sambuc    {
43194684ddb6SLionel Sambuc        if (__first2 == __last2)
43204684ddb6SLionel Sambuc            return _VSTD::copy(__first1, __last1, __result);
43214684ddb6SLionel Sambuc        if (__comp(*__first2, *__first1))
43224684ddb6SLionel Sambuc        {
43234684ddb6SLionel Sambuc            *__result = *__first2;
43244684ddb6SLionel Sambuc            ++__first2;
43254684ddb6SLionel Sambuc        }
43264684ddb6SLionel Sambuc        else
43274684ddb6SLionel Sambuc        {
43284684ddb6SLionel Sambuc            *__result = *__first1;
43294684ddb6SLionel Sambuc            ++__first1;
43304684ddb6SLionel Sambuc        }
43314684ddb6SLionel Sambuc    }
43324684ddb6SLionel Sambuc    return _VSTD::copy(__first2, __last2, __result);
43334684ddb6SLionel Sambuc}
43344684ddb6SLionel Sambuc
43354684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
43364684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
43374684ddb6SLionel Sambuc_OutputIterator
43384684ddb6SLionel Sambucmerge(_InputIterator1 __first1, _InputIterator1 __last1,
43394684ddb6SLionel Sambuc      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
43404684ddb6SLionel Sambuc{
43414684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
43424684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
43434684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
43444684ddb6SLionel Sambuc    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
43454684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
43464684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
43474684ddb6SLionel Sambuc    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
43484684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
43494684ddb6SLionel Sambuc}
43504684ddb6SLionel Sambuc
43514684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
43524684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
43534684ddb6SLionel Sambuc_OutputIterator
43544684ddb6SLionel Sambucmerge(_InputIterator1 __first1, _InputIterator1 __last1,
43554684ddb6SLionel Sambuc      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
43564684ddb6SLionel Sambuc{
43574684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
43584684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
43594684ddb6SLionel Sambuc    return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
43604684ddb6SLionel Sambuc}
43614684ddb6SLionel Sambuc
43624684ddb6SLionel Sambuc// inplace_merge
43634684ddb6SLionel Sambuc
4364*0a6a1f1dSLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2,
4365*0a6a1f1dSLionel Sambuc          class _OutputIterator>
4366*0a6a1f1dSLionel Sambucvoid __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4367*0a6a1f1dSLionel Sambuc                          _InputIterator2 __first2, _InputIterator2 __last2,
4368*0a6a1f1dSLionel Sambuc                          _OutputIterator __result, _Compare __comp)
4369*0a6a1f1dSLionel Sambuc{
4370*0a6a1f1dSLionel Sambuc    for (; __first1 != __last1; ++__result)
4371*0a6a1f1dSLionel Sambuc    {
4372*0a6a1f1dSLionel Sambuc        if (__first2 == __last2)
4373*0a6a1f1dSLionel Sambuc        {
4374*0a6a1f1dSLionel Sambuc            _VSTD::move(__first1, __last1, __result);
4375*0a6a1f1dSLionel Sambuc            return;
4376*0a6a1f1dSLionel Sambuc        }
4377*0a6a1f1dSLionel Sambuc
4378*0a6a1f1dSLionel Sambuc        if (__comp(*__first2, *__first1))
4379*0a6a1f1dSLionel Sambuc        {
4380*0a6a1f1dSLionel Sambuc            *__result = _VSTD::move(*__first2);
4381*0a6a1f1dSLionel Sambuc            ++__first2;
4382*0a6a1f1dSLionel Sambuc        }
4383*0a6a1f1dSLionel Sambuc        else
4384*0a6a1f1dSLionel Sambuc        {
4385*0a6a1f1dSLionel Sambuc            *__result = _VSTD::move(*__first1);
4386*0a6a1f1dSLionel Sambuc            ++__first1;
4387*0a6a1f1dSLionel Sambuc        }
4388*0a6a1f1dSLionel Sambuc    }
4389*0a6a1f1dSLionel Sambuc    // __first2 through __last2 are already in the right spot.
4390*0a6a1f1dSLionel Sambuc}
4391*0a6a1f1dSLionel Sambuc
43924684ddb6SLionel Sambuctemplate <class _Compare, class _BidirectionalIterator>
43934684ddb6SLionel Sambucvoid
43944684ddb6SLionel Sambuc__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
43954684ddb6SLionel Sambuc                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
43964684ddb6SLionel Sambuc                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
43974684ddb6SLionel Sambuc                typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
43984684ddb6SLionel Sambuc{
43994684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
44004684ddb6SLionel Sambuc    __destruct_n __d(0);
44014684ddb6SLionel Sambuc    unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
44024684ddb6SLionel Sambuc    if (__len1 <= __len2)
44034684ddb6SLionel Sambuc    {
44044684ddb6SLionel Sambuc        value_type* __p = __buff;
4405*0a6a1f1dSLionel Sambuc        for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
44064684ddb6SLionel Sambuc            ::new(__p) value_type(_VSTD::move(*__i));
4407*0a6a1f1dSLionel Sambuc        __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
44084684ddb6SLionel Sambuc    }
44094684ddb6SLionel Sambuc    else
44104684ddb6SLionel Sambuc    {
44114684ddb6SLionel Sambuc        value_type* __p = __buff;
4412*0a6a1f1dSLionel Sambuc        for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
44134684ddb6SLionel Sambuc            ::new(__p) value_type(_VSTD::move(*__i));
44144684ddb6SLionel Sambuc        typedef reverse_iterator<_BidirectionalIterator> _RBi;
44154684ddb6SLionel Sambuc        typedef reverse_iterator<value_type*> _Rv;
4416*0a6a1f1dSLionel Sambuc        __half_inplace_merge(_Rv(__p), _Rv(__buff),
4417*0a6a1f1dSLionel Sambuc                             _RBi(__middle), _RBi(__first),
44184684ddb6SLionel Sambuc                             _RBi(__last), __negate<_Compare>(__comp));
44194684ddb6SLionel Sambuc    }
44204684ddb6SLionel Sambuc}
44214684ddb6SLionel Sambuc
44224684ddb6SLionel Sambuctemplate <class _Compare, class _BidirectionalIterator>
44234684ddb6SLionel Sambucvoid
44244684ddb6SLionel Sambuc__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
44254684ddb6SLionel Sambuc                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
44264684ddb6SLionel Sambuc                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
44274684ddb6SLionel Sambuc                typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
44284684ddb6SLionel Sambuc{
44294684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
44304684ddb6SLionel Sambuc    while (true)
44314684ddb6SLionel Sambuc    {
44324684ddb6SLionel Sambuc        // if __middle == __last, we're done
44334684ddb6SLionel Sambuc        if (__len2 == 0)
44344684ddb6SLionel Sambuc            return;
4435*0a6a1f1dSLionel Sambuc        if (__len1 <= __buff_size || __len2 <= __buff_size)
4436*0a6a1f1dSLionel Sambuc            return __buffered_inplace_merge<_Compare>
4437*0a6a1f1dSLionel Sambuc                   (__first, __middle, __last, __comp, __len1, __len2, __buff);
44384684ddb6SLionel Sambuc        // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4439*0a6a1f1dSLionel Sambuc        for (; true; ++__first, (void) --__len1)
44404684ddb6SLionel Sambuc        {
44414684ddb6SLionel Sambuc            if (__len1 == 0)
44424684ddb6SLionel Sambuc                return;
44434684ddb6SLionel Sambuc            if (__comp(*__middle, *__first))
44444684ddb6SLionel Sambuc                break;
44454684ddb6SLionel Sambuc        }
44464684ddb6SLionel Sambuc        // __first < __middle < __last
44474684ddb6SLionel Sambuc        // *__first > *__middle
44484684ddb6SLionel Sambuc        // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
44494684ddb6SLionel Sambuc        //     all elements in:
44504684ddb6SLionel Sambuc        //         [__first, __m1)  <= [__middle, __m2)
44514684ddb6SLionel Sambuc        //         [__middle, __m2) <  [__m1, __middle)
44524684ddb6SLionel Sambuc        //         [__m1, __middle) <= [__m2, __last)
44534684ddb6SLionel Sambuc        //     and __m1 or __m2 is in the middle of its range
44544684ddb6SLionel Sambuc        _BidirectionalIterator __m1;  // "median" of [__first, __middle)
44554684ddb6SLionel Sambuc        _BidirectionalIterator __m2;  // "median" of [__middle, __last)
44564684ddb6SLionel Sambuc        difference_type __len11;      // distance(__first, __m1)
44574684ddb6SLionel Sambuc        difference_type __len21;      // distance(__middle, __m2)
44584684ddb6SLionel Sambuc        // binary search smaller range
44594684ddb6SLionel Sambuc        if (__len1 < __len2)
44604684ddb6SLionel Sambuc        {   // __len >= 1, __len2 >= 2
44614684ddb6SLionel Sambuc            __len21 = __len2 / 2;
44624684ddb6SLionel Sambuc            __m2 = __middle;
44634684ddb6SLionel Sambuc            _VSTD::advance(__m2, __len21);
44644684ddb6SLionel Sambuc            __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
44654684ddb6SLionel Sambuc            __len11 = _VSTD::distance(__first, __m1);
44664684ddb6SLionel Sambuc        }
44674684ddb6SLionel Sambuc        else
44684684ddb6SLionel Sambuc        {
44694684ddb6SLionel Sambuc            if (__len1 == 1)
44704684ddb6SLionel Sambuc            {   // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
44714684ddb6SLionel Sambuc                // It is known *__first > *__middle
44724684ddb6SLionel Sambuc                swap(*__first, *__middle);
44734684ddb6SLionel Sambuc                return;
44744684ddb6SLionel Sambuc            }
44754684ddb6SLionel Sambuc            // __len1 >= 2, __len2 >= 1
44764684ddb6SLionel Sambuc            __len11 = __len1 / 2;
44774684ddb6SLionel Sambuc            __m1 = __first;
44784684ddb6SLionel Sambuc            _VSTD::advance(__m1, __len11);
44794684ddb6SLionel Sambuc            __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
44804684ddb6SLionel Sambuc            __len21 = _VSTD::distance(__middle, __m2);
44814684ddb6SLionel Sambuc        }
44824684ddb6SLionel Sambuc        difference_type __len12 = __len1 - __len11;  // distance(__m1, __middle)
44834684ddb6SLionel Sambuc        difference_type __len22 = __len2 - __len21;  // distance(__m2, __last)
44844684ddb6SLionel Sambuc        // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
44854684ddb6SLionel Sambuc        // swap middle two partitions
44864684ddb6SLionel Sambuc        __middle = _VSTD::rotate(__m1, __middle, __m2);
44874684ddb6SLionel Sambuc        // __len12 and __len21 now have swapped meanings
44884684ddb6SLionel Sambuc        // merge smaller range with recurisve call and larger with tail recursion elimination
44894684ddb6SLionel Sambuc        if (__len11 + __len21 < __len12 + __len22)
44904684ddb6SLionel Sambuc        {
44914684ddb6SLionel Sambuc            __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
44924684ddb6SLionel Sambuc//          __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
44934684ddb6SLionel Sambuc            __first = __middle;
44944684ddb6SLionel Sambuc            __middle = __m2;
44954684ddb6SLionel Sambuc            __len1 = __len12;
44964684ddb6SLionel Sambuc            __len2 = __len22;
44974684ddb6SLionel Sambuc        }
44984684ddb6SLionel Sambuc        else
44994684ddb6SLionel Sambuc        {
45004684ddb6SLionel Sambuc            __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
45014684ddb6SLionel Sambuc//          __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
45024684ddb6SLionel Sambuc            __last = __middle;
45034684ddb6SLionel Sambuc            __middle = __m1;
45044684ddb6SLionel Sambuc            __len1 = __len11;
45054684ddb6SLionel Sambuc            __len2 = __len21;
45064684ddb6SLionel Sambuc        }
45074684ddb6SLionel Sambuc    }
45084684ddb6SLionel Sambuc}
45094684ddb6SLionel Sambuc
45104684ddb6SLionel Sambuctemplate <class _BidirectionalIterator, class _Compare>
45114684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
45124684ddb6SLionel Sambucvoid
45134684ddb6SLionel Sambucinplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
45144684ddb6SLionel Sambuc              _Compare __comp)
45154684ddb6SLionel Sambuc{
45164684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
45174684ddb6SLionel Sambuc    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
45184684ddb6SLionel Sambuc    difference_type __len1 = _VSTD::distance(__first, __middle);
45194684ddb6SLionel Sambuc    difference_type __len2 = _VSTD::distance(__middle, __last);
45204684ddb6SLionel Sambuc    difference_type __buf_size = _VSTD::min(__len1, __len2);
4521*0a6a1f1dSLionel Sambuc    pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4522*0a6a1f1dSLionel Sambuc    unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4523*0a6a1f1dSLionel Sambuc
45244684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
45254684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
45264684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
45274684ddb6SLionel Sambuc    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
45284684ddb6SLionel Sambuc                                            __buf.first, __buf.second);
45294684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
45304684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
45314684ddb6SLionel Sambuc    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
45324684ddb6SLionel Sambuc                                            __buf.first, __buf.second);
45334684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
45344684ddb6SLionel Sambuc}
45354684ddb6SLionel Sambuc
45364684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
45374684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
45384684ddb6SLionel Sambucvoid
45394684ddb6SLionel Sambucinplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
45404684ddb6SLionel Sambuc{
45414684ddb6SLionel Sambuc    _VSTD::inplace_merge(__first, __middle, __last,
45424684ddb6SLionel Sambuc                        __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
45434684ddb6SLionel Sambuc}
45444684ddb6SLionel Sambuc
45454684ddb6SLionel Sambuc// stable_sort
45464684ddb6SLionel Sambuc
45474684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2>
45484684ddb6SLionel Sambucvoid
45494684ddb6SLionel Sambuc__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
45504684ddb6SLionel Sambuc        _InputIterator2 __first2, _InputIterator2 __last2,
45514684ddb6SLionel Sambuc        typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
45524684ddb6SLionel Sambuc{
45534684ddb6SLionel Sambuc    typedef typename iterator_traits<_InputIterator1>::value_type value_type;
45544684ddb6SLionel Sambuc    __destruct_n __d(0);
45554684ddb6SLionel Sambuc    unique_ptr<value_type, __destruct_n&> __h(__result, __d);
45564684ddb6SLionel Sambuc    for (; true; ++__result)
45574684ddb6SLionel Sambuc    {
45584684ddb6SLionel Sambuc        if (__first1 == __last1)
45594684ddb6SLionel Sambuc        {
45604684ddb6SLionel Sambuc            for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
45614684ddb6SLionel Sambuc                ::new (__result) value_type(_VSTD::move(*__first2));
45624684ddb6SLionel Sambuc            __h.release();
45634684ddb6SLionel Sambuc            return;
45644684ddb6SLionel Sambuc        }
45654684ddb6SLionel Sambuc        if (__first2 == __last2)
45664684ddb6SLionel Sambuc        {
45674684ddb6SLionel Sambuc            for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
45684684ddb6SLionel Sambuc                ::new (__result) value_type(_VSTD::move(*__first1));
45694684ddb6SLionel Sambuc            __h.release();
45704684ddb6SLionel Sambuc            return;
45714684ddb6SLionel Sambuc        }
45724684ddb6SLionel Sambuc        if (__comp(*__first2, *__first1))
45734684ddb6SLionel Sambuc        {
45744684ddb6SLionel Sambuc            ::new (__result) value_type(_VSTD::move(*__first2));
45754684ddb6SLionel Sambuc            __d.__incr((value_type*)0);
45764684ddb6SLionel Sambuc            ++__first2;
45774684ddb6SLionel Sambuc        }
45784684ddb6SLionel Sambuc        else
45794684ddb6SLionel Sambuc        {
45804684ddb6SLionel Sambuc            ::new (__result) value_type(_VSTD::move(*__first1));
45814684ddb6SLionel Sambuc            __d.__incr((value_type*)0);
45824684ddb6SLionel Sambuc            ++__first1;
45834684ddb6SLionel Sambuc        }
45844684ddb6SLionel Sambuc    }
45854684ddb6SLionel Sambuc}
45864684ddb6SLionel Sambuc
45874684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
45884684ddb6SLionel Sambucvoid
45894684ddb6SLionel Sambuc__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
45904684ddb6SLionel Sambuc        _InputIterator2 __first2, _InputIterator2 __last2,
45914684ddb6SLionel Sambuc        _OutputIterator __result, _Compare __comp)
45924684ddb6SLionel Sambuc{
45934684ddb6SLionel Sambuc    for (; __first1 != __last1; ++__result)
45944684ddb6SLionel Sambuc    {
45954684ddb6SLionel Sambuc        if (__first2 == __last2)
45964684ddb6SLionel Sambuc        {
45974684ddb6SLionel Sambuc            for (; __first1 != __last1; ++__first1, ++__result)
45984684ddb6SLionel Sambuc                *__result = _VSTD::move(*__first1);
45994684ddb6SLionel Sambuc            return;
46004684ddb6SLionel Sambuc        }
46014684ddb6SLionel Sambuc        if (__comp(*__first2, *__first1))
46024684ddb6SLionel Sambuc        {
46034684ddb6SLionel Sambuc            *__result = _VSTD::move(*__first2);
46044684ddb6SLionel Sambuc            ++__first2;
46054684ddb6SLionel Sambuc        }
46064684ddb6SLionel Sambuc        else
46074684ddb6SLionel Sambuc        {
46084684ddb6SLionel Sambuc            *__result = _VSTD::move(*__first1);
46094684ddb6SLionel Sambuc            ++__first1;
46104684ddb6SLionel Sambuc        }
46114684ddb6SLionel Sambuc    }
46124684ddb6SLionel Sambuc    for (; __first2 != __last2; ++__first2, ++__result)
46134684ddb6SLionel Sambuc        *__result = _VSTD::move(*__first2);
46144684ddb6SLionel Sambuc}
46154684ddb6SLionel Sambuc
46164684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
46174684ddb6SLionel Sambucvoid
46184684ddb6SLionel Sambuc__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
46194684ddb6SLionel Sambuc              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
46204684ddb6SLionel Sambuc              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
46214684ddb6SLionel Sambuc
46224684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
46234684ddb6SLionel Sambucvoid
46244684ddb6SLionel Sambuc__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
46254684ddb6SLionel Sambuc                   typename iterator_traits<_RandomAccessIterator>::difference_type __len,
46264684ddb6SLionel Sambuc                   typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
46274684ddb6SLionel Sambuc{
46284684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
46294684ddb6SLionel Sambuc    switch (__len)
46304684ddb6SLionel Sambuc    {
46314684ddb6SLionel Sambuc    case 0:
46324684ddb6SLionel Sambuc        return;
46334684ddb6SLionel Sambuc    case 1:
46344684ddb6SLionel Sambuc        ::new(__first2) value_type(_VSTD::move(*__first1));
46354684ddb6SLionel Sambuc        return;
46364684ddb6SLionel Sambuc    case 2:
46374684ddb6SLionel Sambuc       __destruct_n __d(0);
46384684ddb6SLionel Sambuc        unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
46394684ddb6SLionel Sambuc         if (__comp(*--__last1, *__first1))
46404684ddb6SLionel Sambuc        {
46414684ddb6SLionel Sambuc            ::new(__first2) value_type(_VSTD::move(*__last1));
46424684ddb6SLionel Sambuc            __d.__incr((value_type*)0);
46434684ddb6SLionel Sambuc            ++__first2;
46444684ddb6SLionel Sambuc            ::new(__first2) value_type(_VSTD::move(*__first1));
46454684ddb6SLionel Sambuc        }
46464684ddb6SLionel Sambuc        else
46474684ddb6SLionel Sambuc        {
46484684ddb6SLionel Sambuc            ::new(__first2) value_type(_VSTD::move(*__first1));
46494684ddb6SLionel Sambuc            __d.__incr((value_type*)0);
46504684ddb6SLionel Sambuc            ++__first2;
46514684ddb6SLionel Sambuc            ::new(__first2) value_type(_VSTD::move(*__last1));
46524684ddb6SLionel Sambuc        }
46534684ddb6SLionel Sambuc        __h2.release();
46544684ddb6SLionel Sambuc        return;
46554684ddb6SLionel Sambuc    }
46564684ddb6SLionel Sambuc    if (__len <= 8)
46574684ddb6SLionel Sambuc    {
46584684ddb6SLionel Sambuc        __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
46594684ddb6SLionel Sambuc        return;
46604684ddb6SLionel Sambuc    }
46614684ddb6SLionel Sambuc    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
46624684ddb6SLionel Sambuc    _RandomAccessIterator __m = __first1 + __l2;
46634684ddb6SLionel Sambuc    __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
46644684ddb6SLionel Sambuc    __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
46654684ddb6SLionel Sambuc    __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
46664684ddb6SLionel Sambuc}
46674684ddb6SLionel Sambuc
46684684ddb6SLionel Sambuctemplate <class _Tp>
46694684ddb6SLionel Sambucstruct __stable_sort_switch
46704684ddb6SLionel Sambuc{
46714684ddb6SLionel Sambuc    static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
46724684ddb6SLionel Sambuc};
46734684ddb6SLionel Sambuc
46744684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
46754684ddb6SLionel Sambucvoid
46764684ddb6SLionel Sambuc__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
46774684ddb6SLionel Sambuc              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
46784684ddb6SLionel Sambuc              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
46794684ddb6SLionel Sambuc{
46804684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
46814684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
46824684ddb6SLionel Sambuc    switch (__len)
46834684ddb6SLionel Sambuc    {
46844684ddb6SLionel Sambuc    case 0:
46854684ddb6SLionel Sambuc    case 1:
46864684ddb6SLionel Sambuc        return;
46874684ddb6SLionel Sambuc    case 2:
46884684ddb6SLionel Sambuc        if (__comp(*--__last, *__first))
46894684ddb6SLionel Sambuc            swap(*__first, *__last);
46904684ddb6SLionel Sambuc        return;
46914684ddb6SLionel Sambuc    }
46924684ddb6SLionel Sambuc    if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
46934684ddb6SLionel Sambuc    {
46944684ddb6SLionel Sambuc        __insertion_sort<_Compare>(__first, __last, __comp);
46954684ddb6SLionel Sambuc        return;
46964684ddb6SLionel Sambuc    }
46974684ddb6SLionel Sambuc    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
46984684ddb6SLionel Sambuc    _RandomAccessIterator __m = __first + __l2;
46994684ddb6SLionel Sambuc    if (__len <= __buff_size)
47004684ddb6SLionel Sambuc    {
47014684ddb6SLionel Sambuc        __destruct_n __d(0);
47024684ddb6SLionel Sambuc        unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
47034684ddb6SLionel Sambuc        __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
47044684ddb6SLionel Sambuc        __d.__set(__l2, (value_type*)0);
47054684ddb6SLionel Sambuc        __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
47064684ddb6SLionel Sambuc        __d.__set(__len, (value_type*)0);
47074684ddb6SLionel Sambuc        __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
47084684ddb6SLionel Sambuc//         __merge<_Compare>(move_iterator<value_type*>(__buff),
47094684ddb6SLionel Sambuc//                           move_iterator<value_type*>(__buff + __l2),
47104684ddb6SLionel Sambuc//                           move_iterator<_RandomAccessIterator>(__buff + __l2),
47114684ddb6SLionel Sambuc//                           move_iterator<_RandomAccessIterator>(__buff + __len),
47124684ddb6SLionel Sambuc//                           __first, __comp);
47134684ddb6SLionel Sambuc        return;
47144684ddb6SLionel Sambuc    }
47154684ddb6SLionel Sambuc    __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
47164684ddb6SLionel Sambuc    __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
47174684ddb6SLionel Sambuc    __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
47184684ddb6SLionel Sambuc}
47194684ddb6SLionel Sambuc
47204684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
47214684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
47224684ddb6SLionel Sambucvoid
47234684ddb6SLionel Sambucstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
47244684ddb6SLionel Sambuc{
47254684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
47264684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
47274684ddb6SLionel Sambuc    difference_type __len = __last - __first;
47284684ddb6SLionel Sambuc    pair<value_type*, ptrdiff_t> __buf(0, 0);
47294684ddb6SLionel Sambuc    unique_ptr<value_type, __return_temporary_buffer> __h;
47304684ddb6SLionel Sambuc    if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
47314684ddb6SLionel Sambuc    {
47324684ddb6SLionel Sambuc        __buf = _VSTD::get_temporary_buffer<value_type>(__len);
47334684ddb6SLionel Sambuc        __h.reset(__buf.first);
47344684ddb6SLionel Sambuc    }
47354684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
47364684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
47374684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
47384684ddb6SLionel Sambuc    __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
47394684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
47404684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
47414684ddb6SLionel Sambuc    __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
47424684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
47434684ddb6SLionel Sambuc}
47444684ddb6SLionel Sambuc
47454684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
47464684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
47474684ddb6SLionel Sambucvoid
47484684ddb6SLionel Sambucstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
47494684ddb6SLionel Sambuc{
47504684ddb6SLionel Sambuc    _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
47514684ddb6SLionel Sambuc}
47524684ddb6SLionel Sambuc
47534684ddb6SLionel Sambuc// is_heap_until
47544684ddb6SLionel Sambuc
47554684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
47564684ddb6SLionel Sambuc_RandomAccessIterator
47574684ddb6SLionel Sambucis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
47584684ddb6SLionel Sambuc{
47594684ddb6SLionel Sambuc    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
47604684ddb6SLionel Sambuc    difference_type __len = __last - __first;
47614684ddb6SLionel Sambuc    difference_type __p = 0;
47624684ddb6SLionel Sambuc    difference_type __c = 1;
47634684ddb6SLionel Sambuc    _RandomAccessIterator __pp = __first;
47644684ddb6SLionel Sambuc    while (__c < __len)
47654684ddb6SLionel Sambuc    {
47664684ddb6SLionel Sambuc        _RandomAccessIterator __cp = __first + __c;
47674684ddb6SLionel Sambuc        if (__comp(*__pp, *__cp))
47684684ddb6SLionel Sambuc            return __cp;
47694684ddb6SLionel Sambuc        ++__c;
47704684ddb6SLionel Sambuc        ++__cp;
47714684ddb6SLionel Sambuc        if (__c == __len)
47724684ddb6SLionel Sambuc            return __last;
47734684ddb6SLionel Sambuc        if (__comp(*__pp, *__cp))
47744684ddb6SLionel Sambuc            return __cp;
47754684ddb6SLionel Sambuc        ++__p;
47764684ddb6SLionel Sambuc        ++__pp;
47774684ddb6SLionel Sambuc        __c = 2 * __p + 1;
47784684ddb6SLionel Sambuc    }
47794684ddb6SLionel Sambuc    return __last;
47804684ddb6SLionel Sambuc}
47814684ddb6SLionel Sambuc
47824684ddb6SLionel Sambuctemplate<class _RandomAccessIterator>
47834684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
47844684ddb6SLionel Sambuc_RandomAccessIterator
47854684ddb6SLionel Sambucis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
47864684ddb6SLionel Sambuc{
47874684ddb6SLionel Sambuc    return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
47884684ddb6SLionel Sambuc}
47894684ddb6SLionel Sambuc
47904684ddb6SLionel Sambuc// is_heap
47914684ddb6SLionel Sambuc
47924684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
47934684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
47944684ddb6SLionel Sambucbool
47954684ddb6SLionel Sambucis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
47964684ddb6SLionel Sambuc{
47974684ddb6SLionel Sambuc    return _VSTD::is_heap_until(__first, __last, __comp) == __last;
47984684ddb6SLionel Sambuc}
47994684ddb6SLionel Sambuc
48004684ddb6SLionel Sambuctemplate<class _RandomAccessIterator>
48014684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
48024684ddb6SLionel Sambucbool
48034684ddb6SLionel Sambucis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
48044684ddb6SLionel Sambuc{
48054684ddb6SLionel Sambuc    return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
48064684ddb6SLionel Sambuc}
48074684ddb6SLionel Sambuc
48084684ddb6SLionel Sambuc// push_heap
48094684ddb6SLionel Sambuc
48104684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
48114684ddb6SLionel Sambucvoid
4812*0a6a1f1dSLionel Sambuc__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
48134684ddb6SLionel Sambuc          typename iterator_traits<_RandomAccessIterator>::difference_type __len)
48144684ddb6SLionel Sambuc{
48154684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
48164684ddb6SLionel Sambuc    if (__len > 1)
48174684ddb6SLionel Sambuc    {
48184684ddb6SLionel Sambuc        __len = (__len - 2) / 2;
48194684ddb6SLionel Sambuc        _RandomAccessIterator __ptr = __first + __len;
48204684ddb6SLionel Sambuc        if (__comp(*__ptr, *--__last))
48214684ddb6SLionel Sambuc        {
48224684ddb6SLionel Sambuc            value_type __t(_VSTD::move(*__last));
48234684ddb6SLionel Sambuc            do
48244684ddb6SLionel Sambuc            {
48254684ddb6SLionel Sambuc                *__last = _VSTD::move(*__ptr);
48264684ddb6SLionel Sambuc                __last = __ptr;
48274684ddb6SLionel Sambuc                if (__len == 0)
48284684ddb6SLionel Sambuc                    break;
48294684ddb6SLionel Sambuc                __len = (__len - 1) / 2;
48304684ddb6SLionel Sambuc                __ptr = __first + __len;
48314684ddb6SLionel Sambuc            } while (__comp(*__ptr, __t));
48324684ddb6SLionel Sambuc            *__last = _VSTD::move(__t);
48334684ddb6SLionel Sambuc        }
48344684ddb6SLionel Sambuc    }
48354684ddb6SLionel Sambuc}
48364684ddb6SLionel Sambuc
48374684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
48384684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
48394684ddb6SLionel Sambucvoid
48404684ddb6SLionel Sambucpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
48414684ddb6SLionel Sambuc{
48424684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
48434684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
48444684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
4845*0a6a1f1dSLionel Sambuc    __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
48464684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
48474684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4848*0a6a1f1dSLionel Sambuc    __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
48494684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
48504684ddb6SLionel Sambuc}
48514684ddb6SLionel Sambuc
48524684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
48534684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
48544684ddb6SLionel Sambucvoid
48554684ddb6SLionel Sambucpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
48564684ddb6SLionel Sambuc{
48574684ddb6SLionel Sambuc    _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
48584684ddb6SLionel Sambuc}
48594684ddb6SLionel Sambuc
48604684ddb6SLionel Sambuc// pop_heap
48614684ddb6SLionel Sambuc
48624684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
4863*0a6a1f1dSLionel Sambucvoid
4864*0a6a1f1dSLionel Sambuc__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4865*0a6a1f1dSLionel Sambuc            typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4866*0a6a1f1dSLionel Sambuc            _RandomAccessIterator __start)
4867*0a6a1f1dSLionel Sambuc{
4868*0a6a1f1dSLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4869*0a6a1f1dSLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4870*0a6a1f1dSLionel Sambuc    // left-child of __start is at 2 * __start + 1
4871*0a6a1f1dSLionel Sambuc    // right-child of __start is at 2 * __start + 2
4872*0a6a1f1dSLionel Sambuc    difference_type __child = __start - __first;
4873*0a6a1f1dSLionel Sambuc
4874*0a6a1f1dSLionel Sambuc    if (__len < 2 || (__len - 2) / 2 < __child)
4875*0a6a1f1dSLionel Sambuc        return;
4876*0a6a1f1dSLionel Sambuc
4877*0a6a1f1dSLionel Sambuc    __child = 2 * __child + 1;
4878*0a6a1f1dSLionel Sambuc    _RandomAccessIterator __child_i = __first + __child;
4879*0a6a1f1dSLionel Sambuc
4880*0a6a1f1dSLionel Sambuc    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4881*0a6a1f1dSLionel Sambuc        // right-child exists and is greater than left-child
4882*0a6a1f1dSLionel Sambuc        ++__child_i;
4883*0a6a1f1dSLionel Sambuc        ++__child;
4884*0a6a1f1dSLionel Sambuc    }
4885*0a6a1f1dSLionel Sambuc
4886*0a6a1f1dSLionel Sambuc    // check if we are in heap-order
4887*0a6a1f1dSLionel Sambuc    if (__comp(*__child_i, *__start))
4888*0a6a1f1dSLionel Sambuc        // we are, __start is larger than it's largest child
4889*0a6a1f1dSLionel Sambuc        return;
4890*0a6a1f1dSLionel Sambuc
4891*0a6a1f1dSLionel Sambuc    value_type __top(_VSTD::move(*__start));
4892*0a6a1f1dSLionel Sambuc    do
4893*0a6a1f1dSLionel Sambuc    {
4894*0a6a1f1dSLionel Sambuc        // we are not in heap-order, swap the parent with it's largest child
4895*0a6a1f1dSLionel Sambuc        *__start = _VSTD::move(*__child_i);
4896*0a6a1f1dSLionel Sambuc        __start = __child_i;
4897*0a6a1f1dSLionel Sambuc
4898*0a6a1f1dSLionel Sambuc        if ((__len - 2) / 2 < __child)
4899*0a6a1f1dSLionel Sambuc            break;
4900*0a6a1f1dSLionel Sambuc
4901*0a6a1f1dSLionel Sambuc        // recompute the child based off of the updated parent
4902*0a6a1f1dSLionel Sambuc        __child = 2 * __child + 1;
4903*0a6a1f1dSLionel Sambuc        __child_i = __first + __child;
4904*0a6a1f1dSLionel Sambuc
4905*0a6a1f1dSLionel Sambuc        if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4906*0a6a1f1dSLionel Sambuc            // right-child exists and is greater than left-child
4907*0a6a1f1dSLionel Sambuc            ++__child_i;
4908*0a6a1f1dSLionel Sambuc            ++__child;
4909*0a6a1f1dSLionel Sambuc        }
4910*0a6a1f1dSLionel Sambuc
4911*0a6a1f1dSLionel Sambuc        // check if we are in heap-order
4912*0a6a1f1dSLionel Sambuc    } while (!__comp(*__child_i, __top));
4913*0a6a1f1dSLionel Sambuc    *__start = _VSTD::move(__top);
4914*0a6a1f1dSLionel Sambuc}
4915*0a6a1f1dSLionel Sambuc
4916*0a6a1f1dSLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
49174684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
49184684ddb6SLionel Sambucvoid
49194684ddb6SLionel Sambuc__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
49204684ddb6SLionel Sambuc           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
49214684ddb6SLionel Sambuc{
49224684ddb6SLionel Sambuc    if (__len > 1)
49234684ddb6SLionel Sambuc    {
49244684ddb6SLionel Sambuc        swap(*__first, *--__last);
4925*0a6a1f1dSLionel Sambuc        __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
49264684ddb6SLionel Sambuc    }
49274684ddb6SLionel Sambuc}
49284684ddb6SLionel Sambuc
49294684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
49304684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
49314684ddb6SLionel Sambucvoid
49324684ddb6SLionel Sambucpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
49334684ddb6SLionel Sambuc{
49344684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
49354684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
49364684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
49374684ddb6SLionel Sambuc    __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
49384684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
49394684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
49404684ddb6SLionel Sambuc    __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
49414684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
49424684ddb6SLionel Sambuc}
49434684ddb6SLionel Sambuc
49444684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
49454684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
49464684ddb6SLionel Sambucvoid
49474684ddb6SLionel Sambucpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
49484684ddb6SLionel Sambuc{
49494684ddb6SLionel Sambuc    _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
49504684ddb6SLionel Sambuc}
49514684ddb6SLionel Sambuc
49524684ddb6SLionel Sambuc// make_heap
49534684ddb6SLionel Sambuc
49544684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
49554684ddb6SLionel Sambucvoid
49564684ddb6SLionel Sambuc__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
49574684ddb6SLionel Sambuc{
49584684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
49594684ddb6SLionel Sambuc    difference_type __n = __last - __first;
49604684ddb6SLionel Sambuc    if (__n > 1)
49614684ddb6SLionel Sambuc    {
4962*0a6a1f1dSLionel Sambuc        // start from the first parent, there is no need to consider children
4963*0a6a1f1dSLionel Sambuc        for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4964*0a6a1f1dSLionel Sambuc        {
4965*0a6a1f1dSLionel Sambuc            __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4966*0a6a1f1dSLionel Sambuc        }
49674684ddb6SLionel Sambuc    }
49684684ddb6SLionel Sambuc}
49694684ddb6SLionel Sambuc
49704684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
49714684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
49724684ddb6SLionel Sambucvoid
49734684ddb6SLionel Sambucmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
49744684ddb6SLionel Sambuc{
49754684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
49764684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
49774684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
49784684ddb6SLionel Sambuc    __make_heap<_Comp_ref>(__first, __last, __c);
49794684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
49804684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
49814684ddb6SLionel Sambuc    __make_heap<_Comp_ref>(__first, __last, __comp);
49824684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
49834684ddb6SLionel Sambuc}
49844684ddb6SLionel Sambuc
49854684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
49864684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
49874684ddb6SLionel Sambucvoid
49884684ddb6SLionel Sambucmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
49894684ddb6SLionel Sambuc{
49904684ddb6SLionel Sambuc    _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
49914684ddb6SLionel Sambuc}
49924684ddb6SLionel Sambuc
49934684ddb6SLionel Sambuc// sort_heap
49944684ddb6SLionel Sambuc
49954684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
49964684ddb6SLionel Sambucvoid
49974684ddb6SLionel Sambuc__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
49984684ddb6SLionel Sambuc{
49994684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
50004684ddb6SLionel Sambuc    for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
50014684ddb6SLionel Sambuc        __pop_heap<_Compare>(__first, __last, __comp, __n);
50024684ddb6SLionel Sambuc}
50034684ddb6SLionel Sambuc
50044684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
50054684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
50064684ddb6SLionel Sambucvoid
50074684ddb6SLionel Sambucsort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
50084684ddb6SLionel Sambuc{
50094684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
50104684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
50114684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
50124684ddb6SLionel Sambuc    __sort_heap<_Comp_ref>(__first, __last, __c);
50134684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
50144684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
50154684ddb6SLionel Sambuc    __sort_heap<_Comp_ref>(__first, __last, __comp);
50164684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
50174684ddb6SLionel Sambuc}
50184684ddb6SLionel Sambuc
50194684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
50204684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
50214684ddb6SLionel Sambucvoid
50224684ddb6SLionel Sambucsort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
50234684ddb6SLionel Sambuc{
50244684ddb6SLionel Sambuc    _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
50254684ddb6SLionel Sambuc}
50264684ddb6SLionel Sambuc
50274684ddb6SLionel Sambuc// partial_sort
50284684ddb6SLionel Sambuc
50294684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
50304684ddb6SLionel Sambucvoid
50314684ddb6SLionel Sambuc__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
50324684ddb6SLionel Sambuc             _Compare __comp)
50334684ddb6SLionel Sambuc{
50344684ddb6SLionel Sambuc    __make_heap<_Compare>(__first, __middle, __comp);
50354684ddb6SLionel Sambuc    typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
50364684ddb6SLionel Sambuc    for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
50374684ddb6SLionel Sambuc    {
50384684ddb6SLionel Sambuc        if (__comp(*__i, *__first))
50394684ddb6SLionel Sambuc        {
50404684ddb6SLionel Sambuc            swap(*__i, *__first);
5041*0a6a1f1dSLionel Sambuc            __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
50424684ddb6SLionel Sambuc        }
50434684ddb6SLionel Sambuc    }
50444684ddb6SLionel Sambuc    __sort_heap<_Compare>(__first, __middle, __comp);
50454684ddb6SLionel Sambuc}
50464684ddb6SLionel Sambuc
50474684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
50484684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
50494684ddb6SLionel Sambucvoid
50504684ddb6SLionel Sambucpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
50514684ddb6SLionel Sambuc             _Compare __comp)
50524684ddb6SLionel Sambuc{
50534684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
50544684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
50554684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
50564684ddb6SLionel Sambuc    __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
50574684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
50584684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
50594684ddb6SLionel Sambuc    __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
50604684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
50614684ddb6SLionel Sambuc}
50624684ddb6SLionel Sambuc
50634684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
50644684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
50654684ddb6SLionel Sambucvoid
50664684ddb6SLionel Sambucpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
50674684ddb6SLionel Sambuc{
50684684ddb6SLionel Sambuc    _VSTD::partial_sort(__first, __middle, __last,
50694684ddb6SLionel Sambuc                       __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
50704684ddb6SLionel Sambuc}
50714684ddb6SLionel Sambuc
50724684ddb6SLionel Sambuc// partial_sort_copy
50734684ddb6SLionel Sambuc
50744684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator, class _RandomAccessIterator>
50754684ddb6SLionel Sambuc_RandomAccessIterator
50764684ddb6SLionel Sambuc__partial_sort_copy(_InputIterator __first, _InputIterator __last,
50774684ddb6SLionel Sambuc                    _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
50784684ddb6SLionel Sambuc{
50794684ddb6SLionel Sambuc    _RandomAccessIterator __r = __result_first;
50804684ddb6SLionel Sambuc    if (__r != __result_last)
50814684ddb6SLionel Sambuc    {
5082*0a6a1f1dSLionel Sambuc        for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
50834684ddb6SLionel Sambuc            *__r = *__first;
50844684ddb6SLionel Sambuc        __make_heap<_Compare>(__result_first, __r, __comp);
5085*0a6a1f1dSLionel Sambuc        typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
50864684ddb6SLionel Sambuc        for (; __first != __last; ++__first)
50874684ddb6SLionel Sambuc            if (__comp(*__first, *__result_first))
50884684ddb6SLionel Sambuc            {
50894684ddb6SLionel Sambuc                *__result_first = *__first;
5090*0a6a1f1dSLionel Sambuc                __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
50914684ddb6SLionel Sambuc            }
50924684ddb6SLionel Sambuc        __sort_heap<_Compare>(__result_first, __r, __comp);
50934684ddb6SLionel Sambuc    }
50944684ddb6SLionel Sambuc    return __r;
50954684ddb6SLionel Sambuc}
50964684ddb6SLionel Sambuc
50974684ddb6SLionel Sambuctemplate <class _InputIterator, class _RandomAccessIterator, class _Compare>
50984684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
50994684ddb6SLionel Sambuc_RandomAccessIterator
51004684ddb6SLionel Sambucpartial_sort_copy(_InputIterator __first, _InputIterator __last,
51014684ddb6SLionel Sambuc                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
51024684ddb6SLionel Sambuc{
51034684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
51044684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
51054684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
51064684ddb6SLionel Sambuc    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
51074684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
51084684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
51094684ddb6SLionel Sambuc    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
51104684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
51114684ddb6SLionel Sambuc}
51124684ddb6SLionel Sambuc
51134684ddb6SLionel Sambuctemplate <class _InputIterator, class _RandomAccessIterator>
51144684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
51154684ddb6SLionel Sambuc_RandomAccessIterator
51164684ddb6SLionel Sambucpartial_sort_copy(_InputIterator __first, _InputIterator __last,
51174684ddb6SLionel Sambuc                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
51184684ddb6SLionel Sambuc{
51194684ddb6SLionel Sambuc    return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
51204684ddb6SLionel Sambuc                                   __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
51214684ddb6SLionel Sambuc}
51224684ddb6SLionel Sambuc
51234684ddb6SLionel Sambuc// nth_element
51244684ddb6SLionel Sambuc
51254684ddb6SLionel Sambuctemplate <class _Compare, class _RandomAccessIterator>
51264684ddb6SLionel Sambucvoid
51274684ddb6SLionel Sambuc__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
51284684ddb6SLionel Sambuc{
51294684ddb6SLionel Sambuc    // _Compare is known to be a reference type
51304684ddb6SLionel Sambuc    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
51314684ddb6SLionel Sambuc    const difference_type __limit = 7;
51324684ddb6SLionel Sambuc    while (true)
51334684ddb6SLionel Sambuc    {
51344684ddb6SLionel Sambuc    __restart:
51354684ddb6SLionel Sambuc        if (__nth == __last)
51364684ddb6SLionel Sambuc            return;
51374684ddb6SLionel Sambuc        difference_type __len = __last - __first;
51384684ddb6SLionel Sambuc        switch (__len)
51394684ddb6SLionel Sambuc        {
51404684ddb6SLionel Sambuc        case 0:
51414684ddb6SLionel Sambuc        case 1:
51424684ddb6SLionel Sambuc            return;
51434684ddb6SLionel Sambuc        case 2:
51444684ddb6SLionel Sambuc            if (__comp(*--__last, *__first))
51454684ddb6SLionel Sambuc                swap(*__first, *__last);
51464684ddb6SLionel Sambuc            return;
51474684ddb6SLionel Sambuc        case 3:
51484684ddb6SLionel Sambuc            {
51494684ddb6SLionel Sambuc            _RandomAccessIterator __m = __first;
51504684ddb6SLionel Sambuc            _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
51514684ddb6SLionel Sambuc            return;
51524684ddb6SLionel Sambuc            }
51534684ddb6SLionel Sambuc        }
51544684ddb6SLionel Sambuc        if (__len <= __limit)
51554684ddb6SLionel Sambuc        {
51564684ddb6SLionel Sambuc            __selection_sort<_Compare>(__first, __last, __comp);
51574684ddb6SLionel Sambuc            return;
51584684ddb6SLionel Sambuc        }
51594684ddb6SLionel Sambuc        // __len > __limit >= 3
51604684ddb6SLionel Sambuc        _RandomAccessIterator __m = __first + __len/2;
51614684ddb6SLionel Sambuc        _RandomAccessIterator __lm1 = __last;
51624684ddb6SLionel Sambuc        unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
51634684ddb6SLionel Sambuc        // *__m is median
51644684ddb6SLionel Sambuc        // partition [__first, __m) < *__m and *__m <= [__m, __last)
51654684ddb6SLionel Sambuc        // (this inhibits tossing elements equivalent to __m around unnecessarily)
51664684ddb6SLionel Sambuc        _RandomAccessIterator __i = __first;
51674684ddb6SLionel Sambuc        _RandomAccessIterator __j = __lm1;
51684684ddb6SLionel Sambuc        // j points beyond range to be tested, *__lm1 is known to be <= *__m
51694684ddb6SLionel Sambuc        // The search going up is known to be guarded but the search coming down isn't.
51704684ddb6SLionel Sambuc        // Prime the downward search with a guard.
51714684ddb6SLionel Sambuc        if (!__comp(*__i, *__m))  // if *__first == *__m
51724684ddb6SLionel Sambuc        {
51734684ddb6SLionel Sambuc            // *__first == *__m, *__first doesn't go in first part
51744684ddb6SLionel Sambuc            // manually guard downward moving __j against __i
51754684ddb6SLionel Sambuc            while (true)
51764684ddb6SLionel Sambuc            {
51774684ddb6SLionel Sambuc                if (__i == --__j)
51784684ddb6SLionel Sambuc                {
51794684ddb6SLionel Sambuc                    // *__first == *__m, *__m <= all other elements
51804684ddb6SLionel Sambuc                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
51814684ddb6SLionel Sambuc                    ++__i;  // __first + 1
51824684ddb6SLionel Sambuc                    __j = __last;
51834684ddb6SLionel Sambuc                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
51844684ddb6SLionel Sambuc                    {
51854684ddb6SLionel Sambuc                        while (true)
51864684ddb6SLionel Sambuc                        {
51874684ddb6SLionel Sambuc                            if (__i == __j)
51884684ddb6SLionel Sambuc                                return;  // [__first, __last) all equivalent elements
51894684ddb6SLionel Sambuc                            if (__comp(*__first, *__i))
51904684ddb6SLionel Sambuc                            {
51914684ddb6SLionel Sambuc                                swap(*__i, *__j);
51924684ddb6SLionel Sambuc                                ++__n_swaps;
51934684ddb6SLionel Sambuc                                ++__i;
51944684ddb6SLionel Sambuc                                break;
51954684ddb6SLionel Sambuc                            }
51964684ddb6SLionel Sambuc                            ++__i;
51974684ddb6SLionel Sambuc                        }
51984684ddb6SLionel Sambuc                    }
51994684ddb6SLionel Sambuc                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
52004684ddb6SLionel Sambuc                    if (__i == __j)
52014684ddb6SLionel Sambuc                        return;
52024684ddb6SLionel Sambuc                    while (true)
52034684ddb6SLionel Sambuc                    {
52044684ddb6SLionel Sambuc                        while (!__comp(*__first, *__i))
52054684ddb6SLionel Sambuc                            ++__i;
52064684ddb6SLionel Sambuc                        while (__comp(*__first, *--__j))
52074684ddb6SLionel Sambuc                            ;
52084684ddb6SLionel Sambuc                        if (__i >= __j)
52094684ddb6SLionel Sambuc                            break;
52104684ddb6SLionel Sambuc                        swap(*__i, *__j);
52114684ddb6SLionel Sambuc                        ++__n_swaps;
52124684ddb6SLionel Sambuc                        ++__i;
52134684ddb6SLionel Sambuc                    }
52144684ddb6SLionel Sambuc                    // [__first, __i) == *__first and *__first < [__i, __last)
52154684ddb6SLionel Sambuc                    // The first part is sorted,
52164684ddb6SLionel Sambuc                    if (__nth < __i)
52174684ddb6SLionel Sambuc                        return;
52184684ddb6SLionel Sambuc                    // __nth_element the secod part
52194684ddb6SLionel Sambuc                    // __nth_element<_Compare>(__i, __nth, __last, __comp);
52204684ddb6SLionel Sambuc                    __first = __i;
52214684ddb6SLionel Sambuc                    goto __restart;
52224684ddb6SLionel Sambuc                }
52234684ddb6SLionel Sambuc                if (__comp(*__j, *__m))
52244684ddb6SLionel Sambuc                {
52254684ddb6SLionel Sambuc                    swap(*__i, *__j);
52264684ddb6SLionel Sambuc                    ++__n_swaps;
52274684ddb6SLionel Sambuc                    break;  // found guard for downward moving __j, now use unguarded partition
52284684ddb6SLionel Sambuc                }
52294684ddb6SLionel Sambuc            }
52304684ddb6SLionel Sambuc        }
52314684ddb6SLionel Sambuc        ++__i;
52324684ddb6SLionel Sambuc        // j points beyond range to be tested, *__lm1 is known to be <= *__m
52334684ddb6SLionel Sambuc        // if not yet partitioned...
52344684ddb6SLionel Sambuc        if (__i < __j)
52354684ddb6SLionel Sambuc        {
52364684ddb6SLionel Sambuc            // known that *(__i - 1) < *__m
52374684ddb6SLionel Sambuc            while (true)
52384684ddb6SLionel Sambuc            {
52394684ddb6SLionel Sambuc                // __m still guards upward moving __i
52404684ddb6SLionel Sambuc                while (__comp(*__i, *__m))
52414684ddb6SLionel Sambuc                    ++__i;
52424684ddb6SLionel Sambuc                // It is now known that a guard exists for downward moving __j
52434684ddb6SLionel Sambuc                while (!__comp(*--__j, *__m))
52444684ddb6SLionel Sambuc                    ;
52454684ddb6SLionel Sambuc                if (__i >= __j)
52464684ddb6SLionel Sambuc                    break;
52474684ddb6SLionel Sambuc                swap(*__i, *__j);
52484684ddb6SLionel Sambuc                ++__n_swaps;
52494684ddb6SLionel Sambuc                // It is known that __m != __j
52504684ddb6SLionel Sambuc                // If __m just moved, follow it
52514684ddb6SLionel Sambuc                if (__m == __i)
52524684ddb6SLionel Sambuc                    __m = __j;
52534684ddb6SLionel Sambuc                ++__i;
52544684ddb6SLionel Sambuc            }
52554684ddb6SLionel Sambuc        }
52564684ddb6SLionel Sambuc        // [__first, __i) < *__m and *__m <= [__i, __last)
52574684ddb6SLionel Sambuc        if (__i != __m && __comp(*__m, *__i))
52584684ddb6SLionel Sambuc        {
52594684ddb6SLionel Sambuc            swap(*__i, *__m);
52604684ddb6SLionel Sambuc            ++__n_swaps;
52614684ddb6SLionel Sambuc        }
52624684ddb6SLionel Sambuc        // [__first, __i) < *__i and *__i <= [__i+1, __last)
52634684ddb6SLionel Sambuc        if (__nth == __i)
52644684ddb6SLionel Sambuc            return;
52654684ddb6SLionel Sambuc        if (__n_swaps == 0)
52664684ddb6SLionel Sambuc        {
52674684ddb6SLionel Sambuc            // We were given a perfectly partitioned sequence.  Coincidence?
52684684ddb6SLionel Sambuc            if (__nth < __i)
52694684ddb6SLionel Sambuc            {
52704684ddb6SLionel Sambuc                // Check for [__first, __i) already sorted
52714684ddb6SLionel Sambuc                __j = __m = __first;
52724684ddb6SLionel Sambuc                while (++__j != __i)
52734684ddb6SLionel Sambuc                {
52744684ddb6SLionel Sambuc                    if (__comp(*__j, *__m))
52754684ddb6SLionel Sambuc                        // not yet sorted, so sort
52764684ddb6SLionel Sambuc                        goto not_sorted;
52774684ddb6SLionel Sambuc                    __m = __j;
52784684ddb6SLionel Sambuc                }
52794684ddb6SLionel Sambuc                // [__first, __i) sorted
52804684ddb6SLionel Sambuc                return;
52814684ddb6SLionel Sambuc            }
52824684ddb6SLionel Sambuc            else
52834684ddb6SLionel Sambuc            {
52844684ddb6SLionel Sambuc                // Check for [__i, __last) already sorted
52854684ddb6SLionel Sambuc                __j = __m = __i;
52864684ddb6SLionel Sambuc                while (++__j != __last)
52874684ddb6SLionel Sambuc                {
52884684ddb6SLionel Sambuc                    if (__comp(*__j, *__m))
52894684ddb6SLionel Sambuc                        // not yet sorted, so sort
52904684ddb6SLionel Sambuc                        goto not_sorted;
52914684ddb6SLionel Sambuc                    __m = __j;
52924684ddb6SLionel Sambuc                }
52934684ddb6SLionel Sambuc                // [__i, __last) sorted
52944684ddb6SLionel Sambuc                return;
52954684ddb6SLionel Sambuc            }
52964684ddb6SLionel Sambuc        }
52974684ddb6SLionel Sambucnot_sorted:
52984684ddb6SLionel Sambuc        // __nth_element on range containing __nth
52994684ddb6SLionel Sambuc        if (__nth < __i)
53004684ddb6SLionel Sambuc        {
53014684ddb6SLionel Sambuc            // __nth_element<_Compare>(__first, __nth, __i, __comp);
53024684ddb6SLionel Sambuc            __last = __i;
53034684ddb6SLionel Sambuc        }
53044684ddb6SLionel Sambuc        else
53054684ddb6SLionel Sambuc        {
53064684ddb6SLionel Sambuc            // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
53074684ddb6SLionel Sambuc            __first = ++__i;
53084684ddb6SLionel Sambuc        }
53094684ddb6SLionel Sambuc    }
53104684ddb6SLionel Sambuc}
53114684ddb6SLionel Sambuc
53124684ddb6SLionel Sambuctemplate <class _RandomAccessIterator, class _Compare>
53134684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
53144684ddb6SLionel Sambucvoid
53154684ddb6SLionel Sambucnth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
53164684ddb6SLionel Sambuc{
53174684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
53184684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
53194684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
53204684ddb6SLionel Sambuc    __nth_element<_Comp_ref>(__first, __nth, __last, __c);
53214684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
53224684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
53234684ddb6SLionel Sambuc    __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
53244684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
53254684ddb6SLionel Sambuc}
53264684ddb6SLionel Sambuc
53274684ddb6SLionel Sambuctemplate <class _RandomAccessIterator>
53284684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
53294684ddb6SLionel Sambucvoid
53304684ddb6SLionel Sambucnth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
53314684ddb6SLionel Sambuc{
53324684ddb6SLionel Sambuc    _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
53334684ddb6SLionel Sambuc}
53344684ddb6SLionel Sambuc
53354684ddb6SLionel Sambuc// includes
53364684ddb6SLionel Sambuc
53374684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2>
53384684ddb6SLionel Sambucbool
53394684ddb6SLionel Sambuc__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
53404684ddb6SLionel Sambuc           _Compare __comp)
53414684ddb6SLionel Sambuc{
53424684ddb6SLionel Sambuc    for (; __first2 != __last2; ++__first1)
53434684ddb6SLionel Sambuc    {
53444684ddb6SLionel Sambuc        if (__first1 == __last1 || __comp(*__first2, *__first1))
53454684ddb6SLionel Sambuc            return false;
53464684ddb6SLionel Sambuc        if (!__comp(*__first1, *__first2))
53474684ddb6SLionel Sambuc            ++__first2;
53484684ddb6SLionel Sambuc    }
53494684ddb6SLionel Sambuc    return true;
53504684ddb6SLionel Sambuc}
53514684ddb6SLionel Sambuc
53524684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _Compare>
53534684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
53544684ddb6SLionel Sambucbool
53554684ddb6SLionel Sambucincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
53564684ddb6SLionel Sambuc         _Compare __comp)
53574684ddb6SLionel Sambuc{
53584684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
53594684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
53604684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
53614684ddb6SLionel Sambuc    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
53624684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
53634684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
53644684ddb6SLionel Sambuc    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
53654684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
53664684ddb6SLionel Sambuc}
53674684ddb6SLionel Sambuc
53684684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
53694684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
53704684ddb6SLionel Sambucbool
53714684ddb6SLionel Sambucincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
53724684ddb6SLionel Sambuc{
53734684ddb6SLionel Sambuc    return _VSTD::includes(__first1, __last1, __first2, __last2,
53744684ddb6SLionel Sambuc                          __less<typename iterator_traits<_InputIterator1>::value_type,
53754684ddb6SLionel Sambuc                                 typename iterator_traits<_InputIterator2>::value_type>());
53764684ddb6SLionel Sambuc}
53774684ddb6SLionel Sambuc
53784684ddb6SLionel Sambuc// set_union
53794684ddb6SLionel Sambuc
53804684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
53814684ddb6SLionel Sambuc_OutputIterator
53824684ddb6SLionel Sambuc__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
53834684ddb6SLionel Sambuc            _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
53844684ddb6SLionel Sambuc{
53854684ddb6SLionel Sambuc    for (; __first1 != __last1; ++__result)
53864684ddb6SLionel Sambuc    {
53874684ddb6SLionel Sambuc        if (__first2 == __last2)
53884684ddb6SLionel Sambuc            return _VSTD::copy(__first1, __last1, __result);
53894684ddb6SLionel Sambuc        if (__comp(*__first2, *__first1))
53904684ddb6SLionel Sambuc        {
53914684ddb6SLionel Sambuc            *__result = *__first2;
53924684ddb6SLionel Sambuc            ++__first2;
53934684ddb6SLionel Sambuc        }
53944684ddb6SLionel Sambuc        else
53954684ddb6SLionel Sambuc        {
53964684ddb6SLionel Sambuc            *__result = *__first1;
53974684ddb6SLionel Sambuc            if (!__comp(*__first1, *__first2))
53984684ddb6SLionel Sambuc                ++__first2;
53994684ddb6SLionel Sambuc            ++__first1;
54004684ddb6SLionel Sambuc        }
54014684ddb6SLionel Sambuc    }
54024684ddb6SLionel Sambuc    return _VSTD::copy(__first2, __last2, __result);
54034684ddb6SLionel Sambuc}
54044684ddb6SLionel Sambuc
54054684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
54064684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
54074684ddb6SLionel Sambuc_OutputIterator
54084684ddb6SLionel Sambucset_union(_InputIterator1 __first1, _InputIterator1 __last1,
54094684ddb6SLionel Sambuc          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
54104684ddb6SLionel Sambuc{
54114684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
54124684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
54134684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
54144684ddb6SLionel Sambuc    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
54154684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
54164684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
54174684ddb6SLionel Sambuc    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
54184684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
54194684ddb6SLionel Sambuc}
54204684ddb6SLionel Sambuc
54214684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
54224684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
54234684ddb6SLionel Sambuc_OutputIterator
54244684ddb6SLionel Sambucset_union(_InputIterator1 __first1, _InputIterator1 __last1,
54254684ddb6SLionel Sambuc          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
54264684ddb6SLionel Sambuc{
54274684ddb6SLionel Sambuc    return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
54284684ddb6SLionel Sambuc                          __less<typename iterator_traits<_InputIterator1>::value_type,
54294684ddb6SLionel Sambuc                                 typename iterator_traits<_InputIterator2>::value_type>());
54304684ddb6SLionel Sambuc}
54314684ddb6SLionel Sambuc
54324684ddb6SLionel Sambuc// set_intersection
54334684ddb6SLionel Sambuc
54344684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
54354684ddb6SLionel Sambuc_OutputIterator
54364684ddb6SLionel Sambuc__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
54374684ddb6SLionel Sambuc                   _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
54384684ddb6SLionel Sambuc{
54394684ddb6SLionel Sambuc    while (__first1 != __last1 && __first2 != __last2)
54404684ddb6SLionel Sambuc    {
54414684ddb6SLionel Sambuc        if (__comp(*__first1, *__first2))
54424684ddb6SLionel Sambuc            ++__first1;
54434684ddb6SLionel Sambuc        else
54444684ddb6SLionel Sambuc        {
54454684ddb6SLionel Sambuc            if (!__comp(*__first2, *__first1))
54464684ddb6SLionel Sambuc            {
54474684ddb6SLionel Sambuc                *__result = *__first1;
54484684ddb6SLionel Sambuc                ++__result;
54494684ddb6SLionel Sambuc                ++__first1;
54504684ddb6SLionel Sambuc            }
54514684ddb6SLionel Sambuc            ++__first2;
54524684ddb6SLionel Sambuc        }
54534684ddb6SLionel Sambuc    }
54544684ddb6SLionel Sambuc    return __result;
54554684ddb6SLionel Sambuc}
54564684ddb6SLionel Sambuc
54574684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
54584684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
54594684ddb6SLionel Sambuc_OutputIterator
54604684ddb6SLionel Sambucset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
54614684ddb6SLionel Sambuc                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
54624684ddb6SLionel Sambuc{
54634684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
54644684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
54654684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
54664684ddb6SLionel Sambuc    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
54674684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
54684684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
54694684ddb6SLionel Sambuc    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
54704684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
54714684ddb6SLionel Sambuc}
54724684ddb6SLionel Sambuc
54734684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
54744684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
54754684ddb6SLionel Sambuc_OutputIterator
54764684ddb6SLionel Sambucset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
54774684ddb6SLionel Sambuc                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
54784684ddb6SLionel Sambuc{
54794684ddb6SLionel Sambuc    return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
54804684ddb6SLionel Sambuc                                  __less<typename iterator_traits<_InputIterator1>::value_type,
54814684ddb6SLionel Sambuc                                         typename iterator_traits<_InputIterator2>::value_type>());
54824684ddb6SLionel Sambuc}
54834684ddb6SLionel Sambuc
54844684ddb6SLionel Sambuc// set_difference
54854684ddb6SLionel Sambuc
54864684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
54874684ddb6SLionel Sambuc_OutputIterator
54884684ddb6SLionel Sambuc__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
54894684ddb6SLionel Sambuc                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
54904684ddb6SLionel Sambuc{
54914684ddb6SLionel Sambuc    while (__first1 != __last1)
54924684ddb6SLionel Sambuc    {
54934684ddb6SLionel Sambuc        if (__first2 == __last2)
54944684ddb6SLionel Sambuc            return _VSTD::copy(__first1, __last1, __result);
54954684ddb6SLionel Sambuc        if (__comp(*__first1, *__first2))
54964684ddb6SLionel Sambuc        {
54974684ddb6SLionel Sambuc            *__result = *__first1;
54984684ddb6SLionel Sambuc            ++__result;
54994684ddb6SLionel Sambuc            ++__first1;
55004684ddb6SLionel Sambuc        }
55014684ddb6SLionel Sambuc        else
55024684ddb6SLionel Sambuc        {
55034684ddb6SLionel Sambuc            if (!__comp(*__first2, *__first1))
55044684ddb6SLionel Sambuc                ++__first1;
55054684ddb6SLionel Sambuc            ++__first2;
55064684ddb6SLionel Sambuc        }
55074684ddb6SLionel Sambuc    }
55084684ddb6SLionel Sambuc    return __result;
55094684ddb6SLionel Sambuc}
55104684ddb6SLionel Sambuc
55114684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
55124684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
55134684ddb6SLionel Sambuc_OutputIterator
55144684ddb6SLionel Sambucset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
55154684ddb6SLionel Sambuc               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
55164684ddb6SLionel Sambuc{
55174684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
55184684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
55194684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
55204684ddb6SLionel Sambuc    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
55214684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
55224684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
55234684ddb6SLionel Sambuc    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
55244684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
55254684ddb6SLionel Sambuc}
55264684ddb6SLionel Sambuc
55274684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
55284684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
55294684ddb6SLionel Sambuc_OutputIterator
55304684ddb6SLionel Sambucset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
55314684ddb6SLionel Sambuc               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
55324684ddb6SLionel Sambuc{
55334684ddb6SLionel Sambuc    return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
55344684ddb6SLionel Sambuc                                __less<typename iterator_traits<_InputIterator1>::value_type,
55354684ddb6SLionel Sambuc                                       typename iterator_traits<_InputIterator2>::value_type>());
55364684ddb6SLionel Sambuc}
55374684ddb6SLionel Sambuc
55384684ddb6SLionel Sambuc// set_symmetric_difference
55394684ddb6SLionel Sambuc
55404684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
55414684ddb6SLionel Sambuc_OutputIterator
55424684ddb6SLionel Sambuc__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
55434684ddb6SLionel Sambuc                           _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
55444684ddb6SLionel Sambuc{
55454684ddb6SLionel Sambuc    while (__first1 != __last1)
55464684ddb6SLionel Sambuc    {
55474684ddb6SLionel Sambuc        if (__first2 == __last2)
55484684ddb6SLionel Sambuc            return _VSTD::copy(__first1, __last1, __result);
55494684ddb6SLionel Sambuc        if (__comp(*__first1, *__first2))
55504684ddb6SLionel Sambuc        {
55514684ddb6SLionel Sambuc            *__result = *__first1;
55524684ddb6SLionel Sambuc            ++__result;
55534684ddb6SLionel Sambuc            ++__first1;
55544684ddb6SLionel Sambuc        }
55554684ddb6SLionel Sambuc        else
55564684ddb6SLionel Sambuc        {
55574684ddb6SLionel Sambuc            if (__comp(*__first2, *__first1))
55584684ddb6SLionel Sambuc            {
55594684ddb6SLionel Sambuc                *__result = *__first2;
55604684ddb6SLionel Sambuc                ++__result;
55614684ddb6SLionel Sambuc            }
55624684ddb6SLionel Sambuc            else
55634684ddb6SLionel Sambuc                ++__first1;
55644684ddb6SLionel Sambuc            ++__first2;
55654684ddb6SLionel Sambuc        }
55664684ddb6SLionel Sambuc    }
55674684ddb6SLionel Sambuc    return _VSTD::copy(__first2, __last2, __result);
55684684ddb6SLionel Sambuc}
55694684ddb6SLionel Sambuc
55704684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
55714684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
55724684ddb6SLionel Sambuc_OutputIterator
55734684ddb6SLionel Sambucset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
55744684ddb6SLionel Sambuc                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
55754684ddb6SLionel Sambuc{
55764684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
55774684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
55784684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
55794684ddb6SLionel Sambuc    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
55804684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
55814684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
55824684ddb6SLionel Sambuc    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
55834684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
55844684ddb6SLionel Sambuc}
55854684ddb6SLionel Sambuc
55864684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
55874684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
55884684ddb6SLionel Sambuc_OutputIterator
55894684ddb6SLionel Sambucset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
55904684ddb6SLionel Sambuc                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
55914684ddb6SLionel Sambuc{
55924684ddb6SLionel Sambuc    return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
55934684ddb6SLionel Sambuc                                          __less<typename iterator_traits<_InputIterator1>::value_type,
55944684ddb6SLionel Sambuc                                                 typename iterator_traits<_InputIterator2>::value_type>());
55954684ddb6SLionel Sambuc}
55964684ddb6SLionel Sambuc
55974684ddb6SLionel Sambuc// lexicographical_compare
55984684ddb6SLionel Sambuc
55994684ddb6SLionel Sambuctemplate <class _Compare, class _InputIterator1, class _InputIterator2>
56004684ddb6SLionel Sambucbool
56014684ddb6SLionel Sambuc__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
56024684ddb6SLionel Sambuc                          _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
56034684ddb6SLionel Sambuc{
5604*0a6a1f1dSLionel Sambuc    for (; __first2 != __last2; ++__first1, (void) ++__first2)
56054684ddb6SLionel Sambuc    {
56064684ddb6SLionel Sambuc        if (__first1 == __last1 || __comp(*__first1, *__first2))
56074684ddb6SLionel Sambuc            return true;
56084684ddb6SLionel Sambuc        if (__comp(*__first2, *__first1))
56094684ddb6SLionel Sambuc            return false;
56104684ddb6SLionel Sambuc    }
56114684ddb6SLionel Sambuc    return false;
56124684ddb6SLionel Sambuc}
56134684ddb6SLionel Sambuc
56144684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2, class _Compare>
56154684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
56164684ddb6SLionel Sambucbool
56174684ddb6SLionel Sambuclexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
56184684ddb6SLionel Sambuc                        _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
56194684ddb6SLionel Sambuc{
56204684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
56214684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
56224684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
56234684ddb6SLionel Sambuc    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
56244684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
56254684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
56264684ddb6SLionel Sambuc    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
56274684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
56284684ddb6SLionel Sambuc}
56294684ddb6SLionel Sambuc
56304684ddb6SLionel Sambuctemplate <class _InputIterator1, class _InputIterator2>
56314684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
56324684ddb6SLionel Sambucbool
56334684ddb6SLionel Sambuclexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
56344684ddb6SLionel Sambuc                        _InputIterator2 __first2, _InputIterator2 __last2)
56354684ddb6SLionel Sambuc{
56364684ddb6SLionel Sambuc    return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
56374684ddb6SLionel Sambuc                                         __less<typename iterator_traits<_InputIterator1>::value_type,
56384684ddb6SLionel Sambuc                                                typename iterator_traits<_InputIterator2>::value_type>());
56394684ddb6SLionel Sambuc}
56404684ddb6SLionel Sambuc
56414684ddb6SLionel Sambuc// next_permutation
56424684ddb6SLionel Sambuc
56434684ddb6SLionel Sambuctemplate <class _Compare, class _BidirectionalIterator>
56444684ddb6SLionel Sambucbool
56454684ddb6SLionel Sambuc__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
56464684ddb6SLionel Sambuc{
56474684ddb6SLionel Sambuc    _BidirectionalIterator __i = __last;
56484684ddb6SLionel Sambuc    if (__first == __last || __first == --__i)
56494684ddb6SLionel Sambuc        return false;
56504684ddb6SLionel Sambuc    while (true)
56514684ddb6SLionel Sambuc    {
56524684ddb6SLionel Sambuc        _BidirectionalIterator __ip1 = __i;
56534684ddb6SLionel Sambuc        if (__comp(*--__i, *__ip1))
56544684ddb6SLionel Sambuc        {
56554684ddb6SLionel Sambuc            _BidirectionalIterator __j = __last;
56564684ddb6SLionel Sambuc            while (!__comp(*__i, *--__j))
56574684ddb6SLionel Sambuc                ;
56584684ddb6SLionel Sambuc            swap(*__i, *__j);
56594684ddb6SLionel Sambuc            _VSTD::reverse(__ip1, __last);
56604684ddb6SLionel Sambuc            return true;
56614684ddb6SLionel Sambuc        }
56624684ddb6SLionel Sambuc        if (__i == __first)
56634684ddb6SLionel Sambuc        {
56644684ddb6SLionel Sambuc            _VSTD::reverse(__first, __last);
56654684ddb6SLionel Sambuc            return false;
56664684ddb6SLionel Sambuc        }
56674684ddb6SLionel Sambuc    }
56684684ddb6SLionel Sambuc}
56694684ddb6SLionel Sambuc
56704684ddb6SLionel Sambuctemplate <class _BidirectionalIterator, class _Compare>
56714684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
56724684ddb6SLionel Sambucbool
56734684ddb6SLionel Sambucnext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
56744684ddb6SLionel Sambuc{
56754684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
56764684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
56774684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
56784684ddb6SLionel Sambuc    return __next_permutation<_Comp_ref>(__first, __last, __c);
56794684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
56804684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
56814684ddb6SLionel Sambuc    return __next_permutation<_Comp_ref>(__first, __last, __comp);
56824684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
56834684ddb6SLionel Sambuc}
56844684ddb6SLionel Sambuc
56854684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
56864684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
56874684ddb6SLionel Sambucbool
56884684ddb6SLionel Sambucnext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
56894684ddb6SLionel Sambuc{
56904684ddb6SLionel Sambuc    return _VSTD::next_permutation(__first, __last,
56914684ddb6SLionel Sambuc                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
56924684ddb6SLionel Sambuc}
56934684ddb6SLionel Sambuc
56944684ddb6SLionel Sambuc// prev_permutation
56954684ddb6SLionel Sambuc
56964684ddb6SLionel Sambuctemplate <class _Compare, class _BidirectionalIterator>
56974684ddb6SLionel Sambucbool
56984684ddb6SLionel Sambuc__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
56994684ddb6SLionel Sambuc{
57004684ddb6SLionel Sambuc    _BidirectionalIterator __i = __last;
57014684ddb6SLionel Sambuc    if (__first == __last || __first == --__i)
57024684ddb6SLionel Sambuc        return false;
57034684ddb6SLionel Sambuc    while (true)
57044684ddb6SLionel Sambuc    {
57054684ddb6SLionel Sambuc        _BidirectionalIterator __ip1 = __i;
57064684ddb6SLionel Sambuc        if (__comp(*__ip1, *--__i))
57074684ddb6SLionel Sambuc        {
57084684ddb6SLionel Sambuc            _BidirectionalIterator __j = __last;
57094684ddb6SLionel Sambuc            while (!__comp(*--__j, *__i))
57104684ddb6SLionel Sambuc                ;
57114684ddb6SLionel Sambuc            swap(*__i, *__j);
57124684ddb6SLionel Sambuc            _VSTD::reverse(__ip1, __last);
57134684ddb6SLionel Sambuc            return true;
57144684ddb6SLionel Sambuc        }
57154684ddb6SLionel Sambuc        if (__i == __first)
57164684ddb6SLionel Sambuc        {
57174684ddb6SLionel Sambuc            _VSTD::reverse(__first, __last);
57184684ddb6SLionel Sambuc            return false;
57194684ddb6SLionel Sambuc        }
57204684ddb6SLionel Sambuc    }
57214684ddb6SLionel Sambuc}
57224684ddb6SLionel Sambuc
57234684ddb6SLionel Sambuctemplate <class _BidirectionalIterator, class _Compare>
57244684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
57254684ddb6SLionel Sambucbool
57264684ddb6SLionel Sambucprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
57274684ddb6SLionel Sambuc{
57284684ddb6SLionel Sambuc#ifdef _LIBCPP_DEBUG
57294684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
57304684ddb6SLionel Sambuc    __debug_less<_Compare> __c(__comp);
57314684ddb6SLionel Sambuc    return __prev_permutation<_Comp_ref>(__first, __last, __c);
57324684ddb6SLionel Sambuc#else  // _LIBCPP_DEBUG
57334684ddb6SLionel Sambuc    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
57344684ddb6SLionel Sambuc    return __prev_permutation<_Comp_ref>(__first, __last, __comp);
57354684ddb6SLionel Sambuc#endif  // _LIBCPP_DEBUG
57364684ddb6SLionel Sambuc}
57374684ddb6SLionel Sambuc
57384684ddb6SLionel Sambuctemplate <class _BidirectionalIterator>
57394684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
57404684ddb6SLionel Sambucbool
57414684ddb6SLionel Sambucprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
57424684ddb6SLionel Sambuc{
57434684ddb6SLionel Sambuc    return _VSTD::prev_permutation(__first, __last,
57444684ddb6SLionel Sambuc                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
57454684ddb6SLionel Sambuc}
57464684ddb6SLionel Sambuc
57474684ddb6SLionel Sambuctemplate <class _Tp>
57484684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
57494684ddb6SLionel Sambuctypename enable_if
57504684ddb6SLionel Sambuc<
57514684ddb6SLionel Sambuc    is_integral<_Tp>::value,
57524684ddb6SLionel Sambuc    _Tp
57534684ddb6SLionel Sambuc>::type
57544684ddb6SLionel Sambuc__rotate_left(_Tp __t, _Tp __n = 1)
57554684ddb6SLionel Sambuc{
57564684ddb6SLionel Sambuc    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
57574684ddb6SLionel Sambuc    __n &= __bits;
57584684ddb6SLionel Sambuc    return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
57594684ddb6SLionel Sambuc}
57604684ddb6SLionel Sambuc
57614684ddb6SLionel Sambuctemplate <class _Tp>
57624684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY
57634684ddb6SLionel Sambuctypename enable_if
57644684ddb6SLionel Sambuc<
57654684ddb6SLionel Sambuc    is_integral<_Tp>::value,
57664684ddb6SLionel Sambuc    _Tp
57674684ddb6SLionel Sambuc>::type
57684684ddb6SLionel Sambuc__rotate_right(_Tp __t, _Tp __n = 1)
57694684ddb6SLionel Sambuc{
57704684ddb6SLionel Sambuc    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
57714684ddb6SLionel Sambuc    __n &= __bits;
57724684ddb6SLionel Sambuc    return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
57734684ddb6SLionel Sambuc}
57744684ddb6SLionel Sambuc
57754684ddb6SLionel Sambuc_LIBCPP_END_NAMESPACE_STD
57764684ddb6SLionel Sambuc
57774684ddb6SLionel Sambuc#endif  // _LIBCPP_ALGORITHM
5778