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