1227825Stheraven// -*- C++ -*-
2227825Stheraven//===-------------------------- algorithm ---------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_ALGORITHM
12227825Stheraven#define _LIBCPP_ALGORITHM
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    algorithm synopsis
16227825Stheraven
17227825Stheraven#include <initializer_list>
18227825Stheraven
19227825Stheravennamespace std
20227825Stheraven{
21227825Stheraven
22227825Stheraventemplate <class InputIterator, class Predicate>
23227825Stheraven    bool
24227825Stheraven    all_of(InputIterator first, InputIterator last, Predicate pred);
25227825Stheraven
26227825Stheraventemplate <class InputIterator, class Predicate>
27227825Stheraven    bool
28227825Stheraven    any_of(InputIterator first, InputIterator last, Predicate pred);
29227825Stheraven
30227825Stheraventemplate <class InputIterator, class Predicate>
31227825Stheraven    bool
32227825Stheraven    none_of(InputIterator first, InputIterator last, Predicate pred);
33227825Stheraven
34227825Stheraventemplate <class InputIterator, class Function>
35227825Stheraven    Function
36227825Stheraven    for_each(InputIterator first, InputIterator last, Function f);
37227825Stheraven
38227825Stheraventemplate <class InputIterator, class T>
39227825Stheraven    InputIterator
40227825Stheraven    find(InputIterator first, InputIterator last, const T& value);
41227825Stheraven
42227825Stheraventemplate <class InputIterator, class Predicate>
43227825Stheraven    InputIterator
44227825Stheraven    find_if(InputIterator first, InputIterator last, Predicate pred);
45227825Stheraven
46227825Stheraventemplate<class InputIterator, class Predicate>
47227825Stheraven    InputIterator
48227825Stheraven    find_if_not(InputIterator first, InputIterator last, Predicate pred);
49227825Stheraven
50227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
51227825Stheraven    ForwardIterator1
52227825Stheraven    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53227825Stheraven             ForwardIterator2 first2, ForwardIterator2 last2);
54227825Stheraven
55227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
56227825Stheraven    ForwardIterator1
57227825Stheraven    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58227825Stheraven             ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
59227825Stheraven
60227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
61227825Stheraven    ForwardIterator1
62227825Stheraven    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63227825Stheraven                  ForwardIterator2 first2, ForwardIterator2 last2);
64227825Stheraven
65227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
66227825Stheraven    ForwardIterator1
67227825Stheraven    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68227825Stheraven                  ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
69227825Stheraven
70227825Stheraventemplate <class ForwardIterator>
71227825Stheraven    ForwardIterator
72227825Stheraven    adjacent_find(ForwardIterator first, ForwardIterator last);
73227825Stheraven
74227825Stheraventemplate <class ForwardIterator, class BinaryPredicate>
75227825Stheraven    ForwardIterator
76227825Stheraven    adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
77227825Stheraven
78227825Stheraventemplate <class InputIterator, class T>
79227825Stheraven    typename iterator_traits<InputIterator>::difference_type
80227825Stheraven    count(InputIterator first, InputIterator last, const T& value);
81227825Stheraven
82227825Stheraventemplate <class InputIterator, class Predicate>
83227825Stheraven    typename iterator_traits<InputIterator>::difference_type
84227825Stheraven    count_if(InputIterator first, InputIterator last, Predicate pred);
85227825Stheraven
86227825Stheraventemplate <class InputIterator1, class InputIterator2>
87227825Stheraven    pair<InputIterator1, InputIterator2>
88227825Stheraven    mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
89227825Stheraven
90253159Stheraventemplate <class InputIterator1, class InputIterator2>
91253159Stheraven    pair<InputIterator1, InputIterator2>
92253159Stheraven    mismatch(InputIterator1 first1, InputIterator1 last1, 
93253159Stheraven             InputIterator2 first2, InputIterator2 last2); // **C++14**
94253159Stheraven
95227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
96227825Stheraven    pair<InputIterator1, InputIterator2>
97227825Stheraven    mismatch(InputIterator1 first1, InputIterator1 last1,
98227825Stheraven             InputIterator2 first2, BinaryPredicate pred);
99227825Stheraven
100253159Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
101253159Stheraven    pair<InputIterator1, InputIterator2>
102253159Stheraven    mismatch(InputIterator1 first1, InputIterator1 last1,
103253159Stheraven             InputIterator2 first2, InputIterator2 last2,
104253159Stheraven             BinaryPredicate pred); // **C++14**
105253159Stheraven
106227825Stheraventemplate <class InputIterator1, class InputIterator2>
107227825Stheraven    bool
108227825Stheraven    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
109227825Stheraven
110253159Stheraventemplate <class InputIterator1, class InputIterator2>
111253159Stheraven    bool
112253159Stheraven    equal(InputIterator1 first1, InputIterator1 last1, 
113253159Stheraven          InputIterator2 first2, InputIterator2 last2); // **C++14**
114253159Stheraven
115227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
116227825Stheraven    bool
117227825Stheraven    equal(InputIterator1 first1, InputIterator1 last1,
118227825Stheraven          InputIterator2 first2, BinaryPredicate pred);
119227825Stheraven
120253159Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
121253159Stheraven    bool
122253159Stheraven    equal(InputIterator1 first1, InputIterator1 last1,
123253159Stheraven          InputIterator2 first2, InputIterator2 last2,
124253159Stheraven          BinaryPredicate pred); // **C++14**
125253159Stheraven
126227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2>
127227825Stheraven    bool
128227825Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129227825Stheraven                   ForwardIterator2 first2);
130227825Stheraven
131253159Stheraventemplate<class ForwardIterator1, class ForwardIterator2>
132253159Stheraven    bool
133253159Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134253159Stheraven                   ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
135253159Stheraven
136227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
137227825Stheraven    bool
138227825Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139227825Stheraven                   ForwardIterator2 first2, BinaryPredicate pred);
140227825Stheraven
141253159Stheraventemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
142253159Stheraven    bool
143253159Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144253159Stheraven                   ForwardIterator2 first2, ForwardIterator2 last2,
145253159Stheraven                   BinaryPredicate pred);  // **C++14**
146253159Stheraven
147227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
148227825Stheraven    ForwardIterator1
149227825Stheraven    search(ForwardIterator1 first1, ForwardIterator1 last1,
150227825Stheraven           ForwardIterator2 first2, ForwardIterator2 last2);
151227825Stheraven
152227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
153227825Stheraven    ForwardIterator1
154227825Stheraven    search(ForwardIterator1 first1, ForwardIterator1 last1,
155227825Stheraven           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
156227825Stheraven
157227825Stheraventemplate <class ForwardIterator, class Size, class T>
158227825Stheraven    ForwardIterator
159227825Stheraven    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
160227825Stheraven
161227825Stheraventemplate <class ForwardIterator, class Size, class T, class BinaryPredicate>
162227825Stheraven    ForwardIterator
163227825Stheraven    search_n(ForwardIterator first, ForwardIterator last,
164227825Stheraven             Size count, const T& value, BinaryPredicate pred);
165227825Stheraven
166227825Stheraventemplate <class InputIterator, class OutputIterator>
167227825Stheraven    OutputIterator
168227825Stheraven    copy(InputIterator first, InputIterator last, OutputIterator result);
169227825Stheraven
170227825Stheraventemplate<class InputIterator, class OutputIterator, class Predicate>
171227825Stheraven    OutputIterator
172227825Stheraven    copy_if(InputIterator first, InputIterator last,
173227825Stheraven            OutputIterator result, Predicate pred);
174227825Stheraven
175227825Stheraventemplate<class InputIterator, class Size, class OutputIterator>
176227825Stheraven    OutputIterator
177227825Stheraven    copy_n(InputIterator first, Size n, OutputIterator result);
178227825Stheraven
179227825Stheraventemplate <class BidirectionalIterator1, class BidirectionalIterator2>
180227825Stheraven    BidirectionalIterator2
181227825Stheraven    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182227825Stheraven                  BidirectionalIterator2 result);
183227825Stheraven
184227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
185227825Stheraven    ForwardIterator2
186227825Stheraven    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
187227825Stheraven
188227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
189227825Stheraven    void
190227825Stheraven    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
191227825Stheraven
192227825Stheraventemplate <class InputIterator, class OutputIterator, class UnaryOperation>
193227825Stheraven    OutputIterator
194227825Stheraven    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
195227825Stheraven
196227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
197227825Stheraven    OutputIterator
198227825Stheraven    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199227825Stheraven              OutputIterator result, BinaryOperation binary_op);
200227825Stheraven
201227825Stheraventemplate <class ForwardIterator, class T>
202227825Stheraven    void
203227825Stheraven    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
204227825Stheraven
205227825Stheraventemplate <class ForwardIterator, class Predicate, class T>
206227825Stheraven    void
207227825Stheraven    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
208227825Stheraven
209227825Stheraventemplate <class InputIterator, class OutputIterator, class T>
210227825Stheraven    OutputIterator
211227825Stheraven    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212227825Stheraven                 const T& old_value, const T& new_value);
213227825Stheraven
214227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate, class T>
215227825Stheraven    OutputIterator
216227825Stheraven    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
217227825Stheraven
218227825Stheraventemplate <class ForwardIterator, class T>
219227825Stheraven    void
220227825Stheraven    fill(ForwardIterator first, ForwardIterator last, const T& value);
221227825Stheraven
222227825Stheraventemplate <class OutputIterator, class Size, class T>
223227825Stheraven    OutputIterator
224227825Stheraven    fill_n(OutputIterator first, Size n, const T& value);
225227825Stheraven
226227825Stheraventemplate <class ForwardIterator, class Generator>
227227825Stheraven    void
228227825Stheraven    generate(ForwardIterator first, ForwardIterator last, Generator gen);
229227825Stheraven
230227825Stheraventemplate <class OutputIterator, class Size, class Generator>
231227825Stheraven    OutputIterator
232227825Stheraven    generate_n(OutputIterator first, Size n, Generator gen);
233227825Stheraven
234227825Stheraventemplate <class ForwardIterator, class T>
235227825Stheraven    ForwardIterator
236227825Stheraven    remove(ForwardIterator first, ForwardIterator last, const T& value);
237227825Stheraven
238227825Stheraventemplate <class ForwardIterator, class Predicate>
239227825Stheraven    ForwardIterator
240227825Stheraven    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
241227825Stheraven
242227825Stheraventemplate <class InputIterator, class OutputIterator, class T>
243227825Stheraven    OutputIterator
244227825Stheraven    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
245227825Stheraven
246227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate>
247227825Stheraven    OutputIterator
248227825Stheraven    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
249227825Stheraven
250227825Stheraventemplate <class ForwardIterator>
251227825Stheraven    ForwardIterator
252227825Stheraven    unique(ForwardIterator first, ForwardIterator last);
253227825Stheraven
254227825Stheraventemplate <class ForwardIterator, class BinaryPredicate>
255227825Stheraven    ForwardIterator
256227825Stheraven    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
257227825Stheraven
258227825Stheraventemplate <class InputIterator, class OutputIterator>
259227825Stheraven    OutputIterator
260227825Stheraven    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
261227825Stheraven
262227825Stheraventemplate <class InputIterator, class OutputIterator, class BinaryPredicate>
263227825Stheraven    OutputIterator
264227825Stheraven    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
265227825Stheraven
266227825Stheraventemplate <class BidirectionalIterator>
267227825Stheraven    void
268227825Stheraven    reverse(BidirectionalIterator first, BidirectionalIterator last);
269227825Stheraven
270227825Stheraventemplate <class BidirectionalIterator, class OutputIterator>
271227825Stheraven    OutputIterator
272227825Stheraven    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
273227825Stheraven
274227825Stheraventemplate <class ForwardIterator>
275227825Stheraven    ForwardIterator
276227825Stheraven    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
277227825Stheraven
278227825Stheraventemplate <class ForwardIterator, class OutputIterator>
279227825Stheraven    OutputIterator
280227825Stheraven    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
281227825Stheraven
282227825Stheraventemplate <class RandomAccessIterator>
283227825Stheraven    void
284278724Sdim    random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
285227825Stheraven
286227825Stheraventemplate <class RandomAccessIterator, class RandomNumberGenerator>
287227825Stheraven    void
288278724Sdim    random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289278724Sdim                   RandomNumberGenerator& rand);  // deprecated in C++14
290227825Stheraven
291227825Stheraventemplate<class RandomAccessIterator, class UniformRandomNumberGenerator>
292227825Stheraven    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
293227825Stheraven                 UniformRandomNumberGenerator&& g);
294227825Stheraven
295227825Stheraventemplate <class InputIterator, class Predicate>
296227825Stheraven    bool
297227825Stheraven    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
298227825Stheraven
299227825Stheraventemplate <class ForwardIterator, class Predicate>
300227825Stheraven    ForwardIterator
301227825Stheraven    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
302227825Stheraven
303227825Stheraventemplate <class InputIterator, class OutputIterator1,
304227825Stheraven          class OutputIterator2, class Predicate>
305227825Stheraven    pair<OutputIterator1, OutputIterator2>
306227825Stheraven    partition_copy(InputIterator first, InputIterator last,
307227825Stheraven                   OutputIterator1 out_true, OutputIterator2 out_false,
308227825Stheraven                   Predicate pred);
309227825Stheraven
310227825Stheraventemplate <class ForwardIterator, class Predicate>
311227825Stheraven    ForwardIterator
312227825Stheraven    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313227825Stheraven
314227825Stheraventemplate<class ForwardIterator, class Predicate>
315227825Stheraven    ForwardIterator
316227825Stheraven    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
317227825Stheraven
318227825Stheraventemplate <class ForwardIterator>
319227825Stheraven    bool
320227825Stheraven    is_sorted(ForwardIterator first, ForwardIterator last);
321227825Stheraven
322227825Stheraventemplate <class ForwardIterator, class Compare>
323227825Stheraven    bool
324227825Stheraven    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
325227825Stheraven
326227825Stheraventemplate<class ForwardIterator>
327227825Stheraven    ForwardIterator
328227825Stheraven    is_sorted_until(ForwardIterator first, ForwardIterator last);
329227825Stheraven
330227825Stheraventemplate <class ForwardIterator, class Compare>
331227825Stheraven    ForwardIterator
332227825Stheraven    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
333227825Stheraven
334227825Stheraventemplate <class RandomAccessIterator>
335227825Stheraven    void
336227825Stheraven    sort(RandomAccessIterator first, RandomAccessIterator last);
337227825Stheraven
338227825Stheraventemplate <class RandomAccessIterator, class Compare>
339227825Stheraven    void
340227825Stheraven    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
341227825Stheraven
342227825Stheraventemplate <class RandomAccessIterator>
343227825Stheraven    void
344227825Stheraven    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
345227825Stheraven
346227825Stheraventemplate <class RandomAccessIterator, class Compare>
347227825Stheraven    void
348227825Stheraven    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
349227825Stheraven
350227825Stheraventemplate <class RandomAccessIterator>
351227825Stheraven    void
352227825Stheraven    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
353227825Stheraven
354227825Stheraventemplate <class RandomAccessIterator, class Compare>
355227825Stheraven    void
356227825Stheraven    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
357227825Stheraven
358227825Stheraventemplate <class InputIterator, class RandomAccessIterator>
359227825Stheraven    RandomAccessIterator
360227825Stheraven    partial_sort_copy(InputIterator first, InputIterator last,
361227825Stheraven                      RandomAccessIterator result_first, RandomAccessIterator result_last);
362227825Stheraven
363227825Stheraventemplate <class InputIterator, class RandomAccessIterator, class Compare>
364227825Stheraven    RandomAccessIterator
365227825Stheraven    partial_sort_copy(InputIterator first, InputIterator last,
366227825Stheraven                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
367227825Stheraven
368227825Stheraventemplate <class RandomAccessIterator>
369227825Stheraven    void
370227825Stheraven    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
371227825Stheraven
372227825Stheraventemplate <class RandomAccessIterator, class Compare>
373227825Stheraven    void
374227825Stheraven    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
375227825Stheraven
376227825Stheraventemplate <class ForwardIterator, class T>
377227825Stheraven    ForwardIterator
378227825Stheraven    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
379227825Stheraven
380227825Stheraventemplate <class ForwardIterator, class T, class Compare>
381227825Stheraven    ForwardIterator
382227825Stheraven    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
383227825Stheraven
384227825Stheraventemplate <class ForwardIterator, class T>
385227825Stheraven    ForwardIterator
386227825Stheraven    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
387227825Stheraven
388227825Stheraventemplate <class ForwardIterator, class T, class Compare>
389227825Stheraven    ForwardIterator
390227825Stheraven    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
391227825Stheraven
392227825Stheraventemplate <class ForwardIterator, class T>
393227825Stheraven    pair<ForwardIterator, ForwardIterator>
394227825Stheraven    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
395227825Stheraven
396227825Stheraventemplate <class ForwardIterator, class T, class Compare>
397227825Stheraven    pair<ForwardIterator, ForwardIterator>
398227825Stheraven    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
399227825Stheraven
400227825Stheraventemplate <class ForwardIterator, class T>
401227825Stheraven    bool
402227825Stheraven    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
403227825Stheraven
404227825Stheraventemplate <class ForwardIterator, class T, class Compare>
405227825Stheraven    bool
406227825Stheraven    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
407227825Stheraven
408227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
409227825Stheraven    OutputIterator
410227825Stheraven    merge(InputIterator1 first1, InputIterator1 last1,
411227825Stheraven          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
412227825Stheraven
413227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
414227825Stheraven    OutputIterator
415227825Stheraven    merge(InputIterator1 first1, InputIterator1 last1,
416227825Stheraven          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
417227825Stheraven
418227825Stheraventemplate <class BidirectionalIterator>
419227825Stheraven    void
420227825Stheraven    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
421227825Stheraven
422227825Stheraventemplate <class BidirectionalIterator, class Compare>
423227825Stheraven    void
424227825Stheraven    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
425227825Stheraven
426227825Stheraventemplate <class InputIterator1, class InputIterator2>
427227825Stheraven    bool
428227825Stheraven    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
429227825Stheraven
430227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare>
431227825Stheraven    bool
432227825Stheraven    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
433227825Stheraven
434227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
435227825Stheraven    OutputIterator
436227825Stheraven    set_union(InputIterator1 first1, InputIterator1 last1,
437227825Stheraven              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
438227825Stheraven
439227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
440227825Stheraven    OutputIterator
441227825Stheraven    set_union(InputIterator1 first1, InputIterator1 last1,
442227825Stheraven              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
443227825Stheraven
444227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
445227825Stheraven    OutputIterator
446227825Stheraven    set_intersection(InputIterator1 first1, InputIterator1 last1,
447227825Stheraven                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448227825Stheraven
449227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450227825Stheraven    OutputIterator
451227825Stheraven    set_intersection(InputIterator1 first1, InputIterator1 last1,
452227825Stheraven                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453227825Stheraven
454227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
455227825Stheraven    OutputIterator
456227825Stheraven    set_difference(InputIterator1 first1, InputIterator1 last1,
457227825Stheraven                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458227825Stheraven
459227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460227825Stheraven    OutputIterator
461227825Stheraven    set_difference(InputIterator1 first1, InputIterator1 last1,
462227825Stheraven                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463227825Stheraven
464227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
465227825Stheraven    OutputIterator
466227825Stheraven    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
467227825Stheraven                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468227825Stheraven
469227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470227825Stheraven    OutputIterator
471227825Stheraven    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
472227825Stheraven                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473227825Stheraven
474227825Stheraventemplate <class RandomAccessIterator>
475227825Stheraven    void
476227825Stheraven    push_heap(RandomAccessIterator first, RandomAccessIterator last);
477227825Stheraven
478227825Stheraventemplate <class RandomAccessIterator, class Compare>
479227825Stheraven    void
480227825Stheraven    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
481227825Stheraven
482227825Stheraventemplate <class RandomAccessIterator>
483227825Stheraven    void
484227825Stheraven    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
485227825Stheraven
486227825Stheraventemplate <class RandomAccessIterator, class Compare>
487227825Stheraven    void
488227825Stheraven    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
489227825Stheraven
490227825Stheraventemplate <class RandomAccessIterator>
491227825Stheraven    void
492227825Stheraven    make_heap(RandomAccessIterator first, RandomAccessIterator last);
493227825Stheraven
494227825Stheraventemplate <class RandomAccessIterator, class Compare>
495227825Stheraven    void
496227825Stheraven    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
497227825Stheraven
498227825Stheraventemplate <class RandomAccessIterator>
499227825Stheraven    void
500227825Stheraven    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
501227825Stheraven
502227825Stheraventemplate <class RandomAccessIterator, class Compare>
503227825Stheraven    void
504227825Stheraven    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
505227825Stheraven
506227825Stheraventemplate <class RandomAccessIterator>
507227825Stheraven    bool
508227825Stheraven    is_heap(RandomAccessIterator first, RandomAccessiterator last);
509227825Stheraven
510227825Stheraventemplate <class RandomAccessIterator, class Compare>
511227825Stheraven    bool
512227825Stheraven    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
513227825Stheraven
514227825Stheraventemplate <class RandomAccessIterator>
515227825Stheraven    RandomAccessIterator
516227825Stheraven    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
517227825Stheraven
518227825Stheraventemplate <class RandomAccessIterator, class Compare>
519227825Stheraven    RandomAccessIterator
520227825Stheraven    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
521227825Stheraven
522227825Stheraventemplate <class ForwardIterator>
523227825Stheraven    ForwardIterator
524227825Stheraven    min_element(ForwardIterator first, ForwardIterator last);
525227825Stheraven
526227825Stheraventemplate <class ForwardIterator, class Compare>
527227825Stheraven    ForwardIterator
528227825Stheraven    min_element(ForwardIterator first, ForwardIterator last, Compare comp);
529227825Stheraven
530227825Stheraventemplate <class T>
531227825Stheraven    const T&
532278724Sdim    min(const T& a, const T& b);  // constexpr in C++14
533227825Stheraven
534227825Stheraventemplate <class T, class Compare>
535227825Stheraven    const T&
536278724Sdim    min(const T& a, const T& b, Compare comp);  // constexpr in C++14
537227825Stheraven
538227825Stheraventemplate<class T>
539227825Stheraven    T
540278724Sdim    min(initializer_list<T> t);  // constexpr in C++14
541227825Stheraven
542227825Stheraventemplate<class T, class Compare>
543227825Stheraven    T
544278724Sdim    min(initializer_list<T> t, Compare comp);  // constexpr in C++14
545227825Stheraven
546227825Stheraventemplate <class ForwardIterator>
547227825Stheraven    ForwardIterator
548227825Stheraven    max_element(ForwardIterator first, ForwardIterator last);
549227825Stheraven
550227825Stheraventemplate <class ForwardIterator, class Compare>
551227825Stheraven    ForwardIterator
552227825Stheraven    max_element(ForwardIterator first, ForwardIterator last, Compare comp);
553227825Stheraven
554227825Stheraventemplate <class T>
555227825Stheraven    const T&
556278724Sdim    max(const T& a, const T& b); // constexpr in C++14
557227825Stheraven
558227825Stheraventemplate <class T, class Compare>
559227825Stheraven    const T&
560278724Sdim    max(const T& a, const T& b, Compare comp);  // constexpr in C++14
561227825Stheraven
562227825Stheraventemplate<class T>
563227825Stheraven    T
564278724Sdim    max(initializer_list<T> t);  // constexpr in C++14
565227825Stheraven
566227825Stheraventemplate<class T, class Compare>
567227825Stheraven    T
568278724Sdim    max(initializer_list<T> t, Compare comp);  // constexpr in C++14
569227825Stheraven
570227825Stheraventemplate<class ForwardIterator>
571227825Stheraven    pair<ForwardIterator, ForwardIterator>
572227825Stheraven    minmax_element(ForwardIterator first, ForwardIterator last);
573227825Stheraven
574227825Stheraventemplate<class ForwardIterator, class Compare>
575227825Stheraven    pair<ForwardIterator, ForwardIterator>
576227825Stheraven    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
577227825Stheraven
578227825Stheraventemplate<class T>
579227825Stheraven    pair<const T&, const T&>
580278724Sdim    minmax(const T& a, const T& b);  // constexpr in C++14
581227825Stheraven
582227825Stheraventemplate<class T, class Compare>
583227825Stheraven    pair<const T&, const T&>
584278724Sdim    minmax(const T& a, const T& b, Compare comp);  // constexpr in C++14
585227825Stheraven
586227825Stheraventemplate<class T>
587227825Stheraven    pair<T, T>
588278724Sdim    minmax(initializer_list<T> t);  // constexpr in C++14
589227825Stheraven
590227825Stheraventemplate<class T, class Compare>
591227825Stheraven    pair<T, T>
592278724Sdim    minmax(initializer_list<T> t, Compare comp);  // constexpr in C++14
593227825Stheraven
594227825Stheraventemplate <class InputIterator1, class InputIterator2>
595227825Stheraven    bool
596227825Stheraven    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
597227825Stheraven
598227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare>
599227825Stheraven    bool
600227825Stheraven    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
601227825Stheraven                            InputIterator2 first2, InputIterator2 last2, Compare comp);
602227825Stheraven
603227825Stheraventemplate <class BidirectionalIterator>
604227825Stheraven    bool
605227825Stheraven    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
606227825Stheraven
607227825Stheraventemplate <class BidirectionalIterator, class Compare>
608227825Stheraven    bool
609227825Stheraven    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
610227825Stheraven
611227825Stheraventemplate <class BidirectionalIterator>
612227825Stheraven    bool
613227825Stheraven    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
614227825Stheraven
615227825Stheraventemplate <class BidirectionalIterator, class Compare>
616227825Stheraven    bool
617227825Stheraven    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
618227825Stheraven
619227825Stheraven}  // std
620227825Stheraven
621227825Stheraven*/
622227825Stheraven
623227825Stheraven#include <__config>
624227825Stheraven#include <initializer_list>
625227825Stheraven#include <type_traits>
626227825Stheraven#include <cstring>
627227825Stheraven#include <utility>
628227825Stheraven#include <memory>
629227825Stheraven#include <iterator>
630241903Sdim#include <cstddef>
631227825Stheraven
632262801Sdim#if defined(__IBMCPP__)
633262801Sdim#include "support/ibm/support.h"
634262801Sdim#endif
635262801Sdim#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
636262801Sdim#include "support/win32/support.h"
637262801Sdim#endif
638262801Sdim
639232950Stheraven#include <__undef_min_max>
640232950Stheraven
641278724Sdim#include <__debug>
642278724Sdim
643227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
644227825Stheraven#pragma GCC system_header
645227825Stheraven#endif
646227825Stheraven
647227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
648227825Stheraven
649278724Sdim// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
650278724Sdim//   * That only works with C++14 and later, and
651278724Sdim//   * We haven't included <functional> here.
652227825Stheraventemplate <class _T1, class _T2 = _T1>
653227825Stheravenstruct __equal_to
654227825Stheraven{
655227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
656227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
657227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
658227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
659227825Stheraven};
660227825Stheraven
661227825Stheraventemplate <class _T1>
662227825Stheravenstruct __equal_to<_T1, _T1>
663227825Stheraven{
664278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
665278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
666227825Stheraven};
667227825Stheraven
668227825Stheraventemplate <class _T1>
669227825Stheravenstruct __equal_to<const _T1, _T1>
670227825Stheraven{
671278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
672278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
673227825Stheraven};
674227825Stheraven
675227825Stheraventemplate <class _T1>
676227825Stheravenstruct __equal_to<_T1, const _T1>
677227825Stheraven{
678278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
680227825Stheraven};
681227825Stheraven
682227825Stheraventemplate <class _T1, class _T2 = _T1>
683227825Stheravenstruct __less
684227825Stheraven{
685278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 
686278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
687278724Sdim
688278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689278724Sdim    bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
690278724Sdim
691278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692278724Sdim    bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
693278724Sdim
694278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695278724Sdim    bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
696227825Stheraven};
697227825Stheraven
698227825Stheraventemplate <class _T1>
699227825Stheravenstruct __less<_T1, _T1>
700227825Stheraven{
701278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
703227825Stheraven};
704227825Stheraven
705227825Stheraventemplate <class _T1>
706227825Stheravenstruct __less<const _T1, _T1>
707227825Stheraven{
708278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
710227825Stheraven};
711227825Stheraven
712227825Stheraventemplate <class _T1>
713227825Stheravenstruct __less<_T1, const _T1>
714227825Stheraven{
715278724Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716278724Sdim    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
717227825Stheraven};
718227825Stheraven
719227825Stheraventemplate <class _Predicate>
720227825Stheravenclass __negate
721227825Stheraven{
722227825Stheravenprivate:
723227825Stheraven    _Predicate __p_;
724227825Stheravenpublic:
725227825Stheraven    _LIBCPP_INLINE_VISIBILITY __negate() {}
726227825Stheraven
727227825Stheraven    _LIBCPP_INLINE_VISIBILITY
728227825Stheraven    explicit __negate(_Predicate __p) : __p_(__p) {}
729227825Stheraven
730227825Stheraven    template <class _T1>
731227825Stheraven    _LIBCPP_INLINE_VISIBILITY
732227825Stheraven    bool operator()(const _T1& __x) {return !__p_(__x);}
733227825Stheraven
734227825Stheraven    template <class _T1, class _T2>
735227825Stheraven    _LIBCPP_INLINE_VISIBILITY
736227825Stheraven    bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
737227825Stheraven};
738227825Stheraven
739262801Sdim#ifdef _LIBCPP_DEBUG
740227825Stheraven
741227825Stheraventemplate <class _Compare>
742227825Stheravenstruct __debug_less
743227825Stheraven{
744227825Stheraven    _Compare __comp_;
745227825Stheraven    __debug_less(_Compare& __c) : __comp_(__c) {}
746227825Stheraven    template <class _Tp, class _Up>
747227825Stheraven    bool operator()(const _Tp& __x, const _Up& __y)
748227825Stheraven    {
749227825Stheraven        bool __r = __comp_(__x, __y);
750227825Stheraven        if (__r)
751227825Stheraven            _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
752227825Stheraven        return __r;
753227825Stheraven    }
754227825Stheraven};
755227825Stheraven
756262801Sdim#endif  // _LIBCPP_DEBUG
757227825Stheraven
758227825Stheraven// Precondition:  __x != 0
759232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
760232950Stheravenunsigned
761232950Stheraven__ctz(unsigned __x)
762232950Stheraven{
763232950Stheraven    return static_cast<unsigned>(__builtin_ctz(__x));
764232950Stheraven}
765227825Stheraven
766232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
767232950Stheravenunsigned long
768232950Stheraven__ctz(unsigned long __x)
769232950Stheraven{
770232950Stheraven    return static_cast<unsigned long>(__builtin_ctzl(__x));
771232950Stheraven}
772232950Stheraven
773232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
774232950Stheravenunsigned long long
775232950Stheraven__ctz(unsigned long long __x)
776232950Stheraven{
777232950Stheraven    return static_cast<unsigned long long>(__builtin_ctzll(__x));
778232950Stheraven}
779232950Stheraven
780227825Stheraven// Precondition:  __x != 0
781232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
782232950Stheravenunsigned
783232950Stheraven__clz(unsigned __x)
784232950Stheraven{
785232950Stheraven    return static_cast<unsigned>(__builtin_clz(__x));
786232950Stheraven}
787227825Stheraven
788232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
789232950Stheravenunsigned long
790232950Stheraven__clz(unsigned long __x)
791232950Stheraven{
792232950Stheraven    return static_cast<unsigned long>(__builtin_clzl (__x));
793232950Stheraven}
794232950Stheraven
795232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
796232950Stheravenunsigned long long
797232950Stheraven__clz(unsigned long long __x)
798232950Stheraven{
799232950Stheraven    return static_cast<unsigned long long>(__builtin_clzll(__x));
800232950Stheraven}
801232950Stheraven
802227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned           __x) {return __builtin_popcount  (__x);}
803227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned      long __x) {return __builtin_popcountl (__x);}
804227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
805227825Stheraven
806227825Stheraven// all_of
807227825Stheraven
808227825Stheraventemplate <class _InputIterator, class _Predicate>
809227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
810227825Stheravenbool
811227825Stheravenall_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
812227825Stheraven{
813227825Stheraven    for (; __first != __last; ++__first)
814227825Stheraven        if (!__pred(*__first))
815227825Stheraven            return false;
816227825Stheraven    return true;
817227825Stheraven}
818227825Stheraven
819227825Stheraven// any_of
820227825Stheraven
821227825Stheraventemplate <class _InputIterator, class _Predicate>
822227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
823227825Stheravenbool
824227825Stheravenany_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
825227825Stheraven{
826227825Stheraven    for (; __first != __last; ++__first)
827227825Stheraven        if (__pred(*__first))
828227825Stheraven            return true;
829227825Stheraven    return false;
830227825Stheraven}
831227825Stheraven
832227825Stheraven// none_of
833227825Stheraven
834227825Stheraventemplate <class _InputIterator, class _Predicate>
835227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
836227825Stheravenbool
837227825Stheravennone_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
838227825Stheraven{
839227825Stheraven    for (; __first != __last; ++__first)
840227825Stheraven        if (__pred(*__first))
841227825Stheraven            return false;
842227825Stheraven    return true;
843227825Stheraven}
844227825Stheraven
845227825Stheraven// for_each
846227825Stheraven
847227825Stheraventemplate <class _InputIterator, class _Function>
848227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
849227825Stheraven_Function
850227825Stheravenfor_each(_InputIterator __first, _InputIterator __last, _Function __f)
851227825Stheraven{
852227825Stheraven    for (; __first != __last; ++__first)
853227825Stheraven        __f(*__first);
854262801Sdim    return _VSTD::move(__f);  // explicitly moved for (emulated) C++03
855227825Stheraven}
856227825Stheraven
857227825Stheraven// find
858227825Stheraven
859227825Stheraventemplate <class _InputIterator, class _Tp>
860227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
861227825Stheraven_InputIterator
862227825Stheravenfind(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
863227825Stheraven{
864227825Stheraven    for (; __first != __last; ++__first)
865227825Stheraven        if (*__first == __value_)
866227825Stheraven            break;
867227825Stheraven    return __first;
868227825Stheraven}
869227825Stheraven
870227825Stheraven// find_if
871227825Stheraven
872227825Stheraventemplate <class _InputIterator, class _Predicate>
873227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
874227825Stheraven_InputIterator
875227825Stheravenfind_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
876227825Stheraven{
877227825Stheraven    for (; __first != __last; ++__first)
878227825Stheraven        if (__pred(*__first))
879227825Stheraven            break;
880227825Stheraven    return __first;
881227825Stheraven}
882227825Stheraven
883227825Stheraven// find_if_not
884227825Stheraven
885227825Stheraventemplate<class _InputIterator, class _Predicate>
886227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
887227825Stheraven_InputIterator
888227825Stheravenfind_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
889227825Stheraven{
890227825Stheraven    for (; __first != __last; ++__first)
891227825Stheraven        if (!__pred(*__first))
892227825Stheraven            break;
893227825Stheraven    return __first;
894227825Stheraven}
895227825Stheraven
896227825Stheraven// find_end
897227825Stheraven
898227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
899227825Stheraven_ForwardIterator1
900227825Stheraven__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
901227825Stheraven           _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
902227825Stheraven           forward_iterator_tag, forward_iterator_tag)
903227825Stheraven{
904227825Stheraven    // modeled after search algorithm
905227825Stheraven    _ForwardIterator1 __r = __last1;  // __last1 is the "default" answer
906227825Stheraven    if (__first2 == __last2)
907227825Stheraven        return __r;
908227825Stheraven    while (true)
909227825Stheraven    {
910227825Stheraven        while (true)
911227825Stheraven        {
912227825Stheraven            if (__first1 == __last1)         // if source exhausted return last correct answer
913227825Stheraven                return __r;                  //    (or __last1 if never found)
914227825Stheraven            if (__pred(*__first1, *__first2))
915227825Stheraven                break;
916227825Stheraven            ++__first1;
917227825Stheraven        }
918227825Stheraven        // *__first1 matches *__first2, now match elements after here
919227825Stheraven        _ForwardIterator1 __m1 = __first1;
920227825Stheraven        _ForwardIterator2 __m2 = __first2;
921227825Stheraven        while (true)
922227825Stheraven        {
923227825Stheraven            if (++__m2 == __last2)
924227825Stheraven            {                         // Pattern exhaused, record answer and search for another one
925227825Stheraven                __r = __first1;
926227825Stheraven                ++__first1;
927227825Stheraven                break;
928227825Stheraven            }
929227825Stheraven            if (++__m1 == __last1)     // Source exhausted, return last answer
930227825Stheraven                return __r;
931227825Stheraven            if (!__pred(*__m1, *__m2))  // mismatch, restart with a new __first
932227825Stheraven            {
933227825Stheraven                ++__first1;
934227825Stheraven                break;
935227825Stheraven            }  // else there is a match, check next elements
936227825Stheraven        }
937227825Stheraven    }
938227825Stheraven}
939227825Stheraven
940227825Stheraventemplate <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
941227825Stheraven_BidirectionalIterator1
942227825Stheraven__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
943227825Stheraven           _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
944227825Stheraven           bidirectional_iterator_tag, bidirectional_iterator_tag)
945227825Stheraven{
946227825Stheraven    // modeled after search algorithm (in reverse)
947227825Stheraven    if (__first2 == __last2)
948227825Stheraven        return __last1;  // Everything matches an empty sequence
949227825Stheraven    _BidirectionalIterator1 __l1 = __last1;
950227825Stheraven    _BidirectionalIterator2 __l2 = __last2;
951227825Stheraven    --__l2;
952227825Stheraven    while (true)
953227825Stheraven    {
954227825Stheraven        // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
955227825Stheraven        while (true)
956227825Stheraven        {
957227825Stheraven            if (__first1 == __l1)  // return __last1 if no element matches *__first2
958227825Stheraven                return __last1;
959227825Stheraven            if (__pred(*--__l1, *__l2))
960227825Stheraven                break;
961227825Stheraven        }
962227825Stheraven        // *__l1 matches *__l2, now match elements before here
963227825Stheraven        _BidirectionalIterator1 __m1 = __l1;
964227825Stheraven        _BidirectionalIterator2 __m2 = __l2;
965227825Stheraven        while (true)
966227825Stheraven        {
967227825Stheraven            if (__m2 == __first2)  // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
968227825Stheraven                return __m1;
969227825Stheraven            if (__m1 == __first1)  // Otherwise if source exhaused, pattern not found
970227825Stheraven                return __last1;
971227825Stheraven            if (!__pred(*--__m1, *--__m2))  // if there is a mismatch, restart with a new __l1
972227825Stheraven            {
973227825Stheraven                break;
974227825Stheraven            }  // else there is a match, check next elements
975227825Stheraven        }
976227825Stheraven    }
977227825Stheraven}
978227825Stheraven
979227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
980278724Sdim_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
981227825Stheraven__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
982227825Stheraven           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
983227825Stheraven           random_access_iterator_tag, random_access_iterator_tag)
984227825Stheraven{
985227825Stheraven    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
986227825Stheraven    typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
987227825Stheraven    if (__len2 == 0)
988227825Stheraven        return __last1;
989227825Stheraven    typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
990227825Stheraven    if (__len1 < __len2)
991227825Stheraven        return __last1;
992227825Stheraven    const _RandomAccessIterator1 __s = __first1 + (__len2 - 1);  // End of pattern match can't go before here
993227825Stheraven    _RandomAccessIterator1 __l1 = __last1;
994227825Stheraven    _RandomAccessIterator2 __l2 = __last2;
995227825Stheraven    --__l2;
996227825Stheraven    while (true)
997227825Stheraven    {
998227825Stheraven        while (true)
999227825Stheraven        {
1000227825Stheraven            if (__s == __l1)
1001227825Stheraven                return __last1;
1002227825Stheraven            if (__pred(*--__l1, *__l2))
1003227825Stheraven                break;
1004227825Stheraven        }
1005227825Stheraven        _RandomAccessIterator1 __m1 = __l1;
1006227825Stheraven        _RandomAccessIterator2 __m2 = __l2;
1007227825Stheraven        while (true)
1008227825Stheraven        {
1009227825Stheraven            if (__m2 == __first2)
1010227825Stheraven                return __m1;
1011227825Stheraven                                 // no need to check range on __m1 because __s guarantees we have enough source
1012227825Stheraven            if (!__pred(*--__m1, *--__m2))
1013227825Stheraven            {
1014227825Stheraven                break;
1015227825Stheraven            }
1016227825Stheraven        }
1017227825Stheraven    }
1018227825Stheraven}
1019227825Stheraven
1020227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1021227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1022227825Stheraven_ForwardIterator1
1023227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1024227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1025227825Stheraven{
1026227825Stheraven    return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
1027227825Stheraven                         (__first1, __last1, __first2, __last2, __pred,
1028227825Stheraven                          typename iterator_traits<_ForwardIterator1>::iterator_category(),
1029227825Stheraven                          typename iterator_traits<_ForwardIterator2>::iterator_category());
1030227825Stheraven}
1031227825Stheraven
1032227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
1033227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1034227825Stheraven_ForwardIterator1
1035227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1036227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1037227825Stheraven{
1038227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1039227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1040227825Stheraven    return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1041227825Stheraven}
1042227825Stheraven
1043227825Stheraven// find_first_of
1044227825Stheraven
1045227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1046278724Sdim_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047278724Sdim__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1048227825Stheraven              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1049227825Stheraven{
1050227825Stheraven    for (; __first1 != __last1; ++__first1)
1051227825Stheraven        for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1052227825Stheraven            if (__pred(*__first1, *__j))
1053227825Stheraven                return __first1;
1054227825Stheraven    return __last1;
1055227825Stheraven}
1056227825Stheraven
1057278724Sdim
1058278724Sdimtemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1059278724Sdiminline _LIBCPP_INLINE_VISIBILITY
1060278724Sdim_ForwardIterator1
1061278724Sdimfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062278724Sdim              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1063278724Sdim{
1064278724Sdim    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1065278724Sdim}
1066278724Sdim
1067227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
1068227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1069227825Stheraven_ForwardIterator1
1070227825Stheravenfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071227825Stheraven              _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1072227825Stheraven{
1073227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1074227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1075278724Sdim    return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1076227825Stheraven}
1077227825Stheraven
1078227825Stheraven// adjacent_find
1079227825Stheraven
1080227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate>
1081227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1082227825Stheraven_ForwardIterator
1083227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1084227825Stheraven{
1085227825Stheraven    if (__first != __last)
1086227825Stheraven    {
1087227825Stheraven        _ForwardIterator __i = __first;
1088227825Stheraven        while (++__i != __last)
1089227825Stheraven        {
1090227825Stheraven            if (__pred(*__first, *__i))
1091227825Stheraven                return __first;
1092227825Stheraven            __first = __i;
1093227825Stheraven        }
1094227825Stheraven    }
1095227825Stheraven    return __last;
1096227825Stheraven}
1097227825Stheraven
1098227825Stheraventemplate <class _ForwardIterator>
1099227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1100227825Stheraven_ForwardIterator
1101227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1102227825Stheraven{
1103227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1104227825Stheraven    return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1105227825Stheraven}
1106227825Stheraven
1107227825Stheraven// count
1108227825Stheraven
1109227825Stheraventemplate <class _InputIterator, class _Tp>
1110227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1111227825Stheraventypename iterator_traits<_InputIterator>::difference_type
1112227825Stheravencount(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1113227825Stheraven{
1114227825Stheraven    typename iterator_traits<_InputIterator>::difference_type __r(0);
1115227825Stheraven    for (; __first != __last; ++__first)
1116227825Stheraven        if (*__first == __value_)
1117227825Stheraven            ++__r;
1118227825Stheraven    return __r;
1119227825Stheraven}
1120227825Stheraven
1121227825Stheraven// count_if
1122227825Stheraven
1123227825Stheraventemplate <class _InputIterator, class _Predicate>
1124227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1125227825Stheraventypename iterator_traits<_InputIterator>::difference_type
1126227825Stheravencount_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1127227825Stheraven{
1128227825Stheraven    typename iterator_traits<_InputIterator>::difference_type __r(0);
1129227825Stheraven    for (; __first != __last; ++__first)
1130227825Stheraven        if (__pred(*__first))
1131227825Stheraven            ++__r;
1132227825Stheraven    return __r;
1133227825Stheraven}
1134227825Stheraven
1135227825Stheraven// mismatch
1136227825Stheraven
1137227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1138227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1139227825Stheravenpair<_InputIterator1, _InputIterator2>
1140227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141227825Stheraven         _InputIterator2 __first2, _BinaryPredicate __pred)
1142227825Stheraven{
1143278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1144227825Stheraven        if (!__pred(*__first1, *__first2))
1145227825Stheraven            break;
1146227825Stheraven    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1147227825Stheraven}
1148227825Stheraven
1149227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
1150227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1151227825Stheravenpair<_InputIterator1, _InputIterator2>
1152227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1153227825Stheraven{
1154227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1155227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1156227825Stheraven    return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1157227825Stheraven}
1158227825Stheraven
1159253159Stheraven#if _LIBCPP_STD_VER > 11
1160253159Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1161253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1162253159Stheravenpair<_InputIterator1, _InputIterator2>
1163253159Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1164253159Stheraven         _InputIterator2 __first2, _InputIterator2 __last2,
1165253159Stheraven         _BinaryPredicate __pred)
1166253159Stheraven{
1167278724Sdim    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1168253159Stheraven        if (!__pred(*__first1, *__first2))
1169253159Stheraven            break;
1170253159Stheraven    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1171253159Stheraven}
1172253159Stheraven
1173253159Stheraventemplate <class _InputIterator1, class _InputIterator2>
1174253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1175253159Stheravenpair<_InputIterator1, _InputIterator2>
1176253159Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1177253159Stheraven         _InputIterator2 __first2, _InputIterator2 __last2)
1178253159Stheraven{
1179253159Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180253159Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1181253159Stheraven    return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1182253159Stheraven}
1183253159Stheraven#endif
1184253159Stheraven
1185227825Stheraven// equal
1186227825Stheraven
1187227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1188227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1189227825Stheravenbool
1190227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1191227825Stheraven{
1192278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1193227825Stheraven        if (!__pred(*__first1, *__first2))
1194227825Stheraven            return false;
1195227825Stheraven    return true;
1196227825Stheraven}
1197227825Stheraven
1198227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
1199227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1200227825Stheravenbool
1201227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1202227825Stheraven{
1203227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1204227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1205227825Stheraven    return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1206227825Stheraven}
1207227825Stheraven
1208253159Stheraven#if _LIBCPP_STD_VER > 11
1209253159Stheraventemplate <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1210253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1211253159Stheravenbool
1212253159Stheraven__equal(_InputIterator1 __first1, _InputIterator1 __last1, 
1213253159Stheraven        _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1214253159Stheraven        input_iterator_tag, input_iterator_tag )
1215253159Stheraven{
1216278724Sdim    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1217253159Stheraven        if (!__pred(*__first1, *__first2))
1218253159Stheraven            return false;
1219253159Stheraven    return __first1 == __last1 && __first2 == __last2;
1220253159Stheraven}
1221253159Stheraven
1222253159Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1223253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1224253159Stheravenbool
1225253159Stheraven__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 
1226253159Stheraven        _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 
1227253159Stheraven      random_access_iterator_tag, random_access_iterator_tag )
1228253159Stheraven{
1229253159Stheraven    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1230253159Stheraven        return false;
1231253159Stheraven    return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1232253159Stheraven                        typename add_lvalue_reference<_BinaryPredicate>::type>
1233253159Stheraven                       (__first1, __last1, __first2, __pred );
1234253159Stheraven}
1235253159Stheraven
1236253159Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1238253159Stheravenbool
1239253159Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, 
1240253159Stheraven      _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1241253159Stheraven{
1242253159Stheraven    return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1243253159Stheraven       (__first1, __last1, __first2, __last2, __pred, 
1244253159Stheraven        typename iterator_traits<_InputIterator1>::iterator_category(),
1245253159Stheraven        typename iterator_traits<_InputIterator2>::iterator_category());
1246253159Stheraven}
1247253159Stheraven
1248253159Stheraventemplate <class _InputIterator1, class _InputIterator2>
1249253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1250253159Stheravenbool
1251253159Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, 
1252253159Stheraven      _InputIterator2 __first2, _InputIterator2 __last2)
1253253159Stheraven{
1254253159Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1255253159Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1256253159Stheraven    return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1257253159Stheraven        typename iterator_traits<_InputIterator1>::iterator_category(),
1258253159Stheraven        typename iterator_traits<_InputIterator2>::iterator_category());
1259253159Stheraven}
1260253159Stheraven#endif
1261253159Stheraven
1262227825Stheraven// is_permutation
1263227825Stheraven
1264227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1265227825Stheravenbool
1266227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1267227825Stheraven               _ForwardIterator2 __first2, _BinaryPredicate __pred)
1268227825Stheraven{
1269227825Stheraven    // shorten sequences as much as possible by lopping of any equal parts
1270278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2)
1271227825Stheraven        if (!__pred(*__first1, *__first2))
1272227825Stheraven            goto __not_done;
1273227825Stheraven    return true;
1274227825Stheraven__not_done:
1275227825Stheraven    // __first1 != __last1 && *__first1 != *__first2
1276227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1277227825Stheraven    _D1 __l1 = _VSTD::distance(__first1, __last1);
1278227825Stheraven    if (__l1 == _D1(1))
1279227825Stheraven        return false;
1280227825Stheraven    _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1281227825Stheraven    // For each element in [f1, l1) see if there are the same number of
1282227825Stheraven    //    equal elements in [f2, l2)
1283227825Stheraven    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1284227825Stheraven    {
1285227825Stheraven        // Have we already counted the number of *__i in [f1, l1)?
1286227825Stheraven        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1287227825Stheraven            if (__pred(*__j, *__i))
1288227825Stheraven                goto __next_iter;
1289227825Stheraven        {
1290227825Stheraven            // Count number of *__i in [f2, l2)
1291227825Stheraven            _D1 __c2 = 0;
1292227825Stheraven            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1293227825Stheraven                if (__pred(*__i, *__j))
1294227825Stheraven                    ++__c2;
1295227825Stheraven            if (__c2 == 0)
1296227825Stheraven                return false;
1297227825Stheraven            // Count number of *__i in [__i, l1) (we can start with 1)
1298227825Stheraven            _D1 __c1 = 1;
1299227825Stheraven            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1300227825Stheraven                if (__pred(*__i, *__j))
1301227825Stheraven                    ++__c1;
1302227825Stheraven            if (__c1 != __c2)
1303227825Stheraven                return false;
1304227825Stheraven        }
1305227825Stheraven__next_iter:;
1306227825Stheraven    }
1307227825Stheraven    return true;
1308227825Stheraven}
1309227825Stheraven
1310227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2>
1311227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1312227825Stheravenbool
1313227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1314227825Stheraven               _ForwardIterator2 __first2)
1315227825Stheraven{
1316227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1317227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1318227825Stheraven    return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1319227825Stheraven}
1320227825Stheraven
1321253159Stheraven#if _LIBCPP_STD_VER > 11
1322253159Stheraventemplate<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1323253159Stheravenbool
1324253159Stheraven__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1325253159Stheraven                 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 
1326253159Stheraven                 _BinaryPredicate __pred,
1327253159Stheraven                 forward_iterator_tag, forward_iterator_tag )
1328253159Stheraven{
1329253159Stheraven    // shorten sequences as much as possible by lopping of any equal parts
1330278724Sdim    for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
1331253159Stheraven        if (!__pred(*__first1, *__first2))
1332253159Stheraven            goto __not_done;
1333253159Stheraven    return __first1 == __last1 && __first2 == __last2;
1334253159Stheraven__not_done:
1335253159Stheraven    // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1336253159Stheraven    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1337253159Stheraven    _D1 __l1 = _VSTD::distance(__first1, __last1);
1338253159Stheraven
1339253159Stheraven    typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
1340253159Stheraven    _D2 __l2 = _VSTD::distance(__first2, __last2);
1341253159Stheraven    if (__l1 != __l2)
1342253159Stheraven        return false;
1343253159Stheraven
1344253159Stheraven    // For each element in [f1, l1) see if there are the same number of
1345253159Stheraven    //    equal elements in [f2, l2)
1346253159Stheraven    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1347253159Stheraven    {
1348253159Stheraven        // Have we already counted the number of *__i in [f1, l1)?
1349253159Stheraven        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1350253159Stheraven            if (__pred(*__j, *__i))
1351253159Stheraven                goto __next_iter;
1352253159Stheraven        {
1353253159Stheraven            // Count number of *__i in [f2, l2)
1354253159Stheraven            _D1 __c2 = 0;
1355253159Stheraven            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1356253159Stheraven                if (__pred(*__i, *__j))
1357253159Stheraven                    ++__c2;
1358253159Stheraven            if (__c2 == 0)
1359253159Stheraven                return false;
1360253159Stheraven            // Count number of *__i in [__i, l1) (we can start with 1)
1361253159Stheraven            _D1 __c1 = 1;
1362253159Stheraven            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1363253159Stheraven                if (__pred(*__i, *__j))
1364253159Stheraven                    ++__c1;
1365253159Stheraven            if (__c1 != __c2)
1366253159Stheraven                return false;
1367253159Stheraven        }
1368253159Stheraven__next_iter:;
1369253159Stheraven    }
1370253159Stheraven    return true;
1371253159Stheraven}
1372253159Stheraven
1373253159Stheraventemplate<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1374253159Stheravenbool
1375253159Stheraven__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1376253159Stheraven               _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2, 
1377253159Stheraven               _BinaryPredicate __pred,
1378253159Stheraven               random_access_iterator_tag, random_access_iterator_tag )
1379253159Stheraven{
1380253159Stheraven    if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1381253159Stheraven        return false;
1382253159Stheraven    return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1383253159Stheraven                                 typename add_lvalue_reference<_BinaryPredicate>::type>
1384253159Stheraven                                (__first1, __last1, __first2, __pred );
1385253159Stheraven}
1386253159Stheraven
1387253159Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1388253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1389253159Stheravenbool
1390253159Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1391253159Stheraven               _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1392253159Stheraven               _BinaryPredicate __pred )
1393253159Stheraven{
1394253159Stheraven    return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1395253159Stheraven       (__first1, __last1, __first2, __last2, __pred,
1396253159Stheraven        typename iterator_traits<_ForwardIterator1>::iterator_category(),
1397253159Stheraven        typename iterator_traits<_ForwardIterator2>::iterator_category());
1398253159Stheraven}
1399253159Stheraven
1400253159Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2>
1401253159Stheraveninline _LIBCPP_INLINE_VISIBILITY
1402253159Stheravenbool
1403253159Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1404253159Stheraven               _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1405253159Stheraven{
1406253159Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1407253159Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1408253159Stheraven    return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1409253159Stheraven        __equal_to<__v1, __v2>(),
1410253159Stheraven        typename iterator_traits<_ForwardIterator1>::iterator_category(),
1411253159Stheraven        typename iterator_traits<_ForwardIterator2>::iterator_category());
1412253159Stheraven}
1413253159Stheraven#endif
1414253159Stheraven
1415227825Stheraven// search
1416227825Stheraven
1417227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1418227825Stheraven_ForwardIterator1
1419227825Stheraven__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1421227825Stheraven         forward_iterator_tag, forward_iterator_tag)
1422227825Stheraven{
1423227825Stheraven    if (__first2 == __last2)
1424227825Stheraven        return __first1;  // Everything matches an empty sequence
1425227825Stheraven    while (true)
1426227825Stheraven    {
1427227825Stheraven        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1428227825Stheraven        while (true)
1429227825Stheraven        {
1430227825Stheraven            if (__first1 == __last1)  // return __last1 if no element matches *__first2
1431227825Stheraven                return __last1;
1432227825Stheraven            if (__pred(*__first1, *__first2))
1433227825Stheraven                break;
1434227825Stheraven            ++__first1;
1435227825Stheraven        }
1436227825Stheraven        // *__first1 matches *__first2, now match elements after here
1437227825Stheraven        _ForwardIterator1 __m1 = __first1;
1438227825Stheraven        _ForwardIterator2 __m2 = __first2;
1439227825Stheraven        while (true)
1440227825Stheraven        {
1441227825Stheraven            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1442227825Stheraven                return __first1;
1443227825Stheraven            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
1444227825Stheraven                return __last1;
1445227825Stheraven            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
1446227825Stheraven            {
1447227825Stheraven                ++__first1;
1448227825Stheraven                break;
1449227825Stheraven            }  // else there is a match, check next elements
1450227825Stheraven        }
1451227825Stheraven    }
1452227825Stheraven}
1453227825Stheraven
1454227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1455278724Sdim_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
1456227825Stheraven__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1457227825Stheraven           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1458227825Stheraven           random_access_iterator_tag, random_access_iterator_tag)
1459227825Stheraven{
1460227825Stheraven    typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1461227825Stheraven    typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1462227825Stheraven    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
1463227825Stheraven    _D2 __len2 = __last2 - __first2;
1464227825Stheraven    if (__len2 == 0)
1465227825Stheraven        return __first1;
1466227825Stheraven    _D1 __len1 = __last1 - __first1;
1467227825Stheraven    if (__len1 < __len2)
1468227825Stheraven        return __last1;
1469227825Stheraven    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
1470227825Stheraven    while (true)
1471227825Stheraven    {
1472227825Stheraven#if !_LIBCPP_UNROLL_LOOPS
1473227825Stheraven        while (true)
1474227825Stheraven        {
1475227825Stheraven            if (__first1 == __s)
1476227825Stheraven                return __last1;
1477227825Stheraven            if (__pred(*__first1, *__first2))
1478227825Stheraven                break;
1479227825Stheraven            ++__first1;
1480227825Stheraven        }
1481227825Stheraven#else  // !_LIBCPP_UNROLL_LOOPS
1482227825Stheraven        for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1483227825Stheraven        {
1484227825Stheraven            if (__pred(*__first1, *__first2))
1485227825Stheraven                goto __phase2;
1486227825Stheraven            if (__pred(*++__first1, *__first2))
1487227825Stheraven                goto __phase2;
1488227825Stheraven            if (__pred(*++__first1, *__first2))
1489227825Stheraven                goto __phase2;
1490227825Stheraven            if (__pred(*++__first1, *__first2))
1491227825Stheraven                goto __phase2;
1492227825Stheraven            ++__first1;
1493227825Stheraven        }
1494227825Stheraven        switch (__s - __first1)
1495227825Stheraven        {
1496227825Stheraven        case 3:
1497227825Stheraven            if (__pred(*__first1, *__first2))
1498227825Stheraven                break;
1499227825Stheraven            ++__first1;
1500227825Stheraven        case 2:
1501227825Stheraven            if (__pred(*__first1, *__first2))
1502227825Stheraven                break;
1503227825Stheraven            ++__first1;
1504227825Stheraven        case 1:
1505227825Stheraven            if (__pred(*__first1, *__first2))
1506227825Stheraven                break;
1507227825Stheraven        case 0:
1508227825Stheraven            return __last1;
1509227825Stheraven        }
1510227825Stheraven    __phase2:
1511227825Stheraven#endif  // !_LIBCPP_UNROLL_LOOPS
1512227825Stheraven        _RandomAccessIterator1 __m1 = __first1;
1513227825Stheraven        _RandomAccessIterator2 __m2 = __first2;
1514227825Stheraven#if !_LIBCPP_UNROLL_LOOPS
1515227825Stheraven         while (true)
1516227825Stheraven         {
1517227825Stheraven             if (++__m2 == __last2)
1518227825Stheraven                 return __first1;
1519227825Stheraven             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
1520227825Stheraven             if (!__pred(*__m1, *__m2))
1521227825Stheraven             {
1522227825Stheraven                 ++__first1;
1523227825Stheraven                 break;
1524227825Stheraven             }
1525227825Stheraven         }
1526227825Stheraven#else  // !_LIBCPP_UNROLL_LOOPS
1527227825Stheraven        ++__m2;
1528227825Stheraven        ++__m1;
1529227825Stheraven        for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1530227825Stheraven        {
1531227825Stheraven            if (!__pred(*__m1, *__m2))
1532227825Stheraven                goto __continue;
1533227825Stheraven            if (!__pred(*++__m1, *++__m2))
1534227825Stheraven                goto __continue;
1535227825Stheraven            if (!__pred(*++__m1, *++__m2))
1536227825Stheraven                goto __continue;
1537227825Stheraven            if (!__pred(*++__m1, *++__m2))
1538227825Stheraven                goto __continue;
1539227825Stheraven            ++__m1;
1540227825Stheraven            ++__m2;
1541227825Stheraven        }
1542227825Stheraven        switch (__last2 - __m2)
1543227825Stheraven        {
1544227825Stheraven        case 3:
1545227825Stheraven            if (!__pred(*__m1, *__m2))
1546227825Stheraven                break;
1547227825Stheraven            ++__m1;
1548227825Stheraven            ++__m2;
1549227825Stheraven        case 2:
1550227825Stheraven            if (!__pred(*__m1, *__m2))
1551227825Stheraven                break;
1552227825Stheraven            ++__m1;
1553227825Stheraven            ++__m2;
1554227825Stheraven        case 1:
1555227825Stheraven            if (!__pred(*__m1, *__m2))
1556227825Stheraven                break;
1557227825Stheraven        case 0:
1558227825Stheraven            return __first1;
1559227825Stheraven        }
1560227825Stheraven    __continue:
1561227825Stheraven        ++__first1;
1562227825Stheraven#endif  // !_LIBCPP_UNROLL_LOOPS
1563227825Stheraven    }
1564227825Stheraven}
1565227825Stheraven
1566227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1567227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1568227825Stheraven_ForwardIterator1
1569227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1570227825Stheraven       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1571227825Stheraven{
1572227825Stheraven    return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1573227825Stheraven                         (__first1, __last1, __first2, __last2, __pred,
1574227825Stheraven                          typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1575227825Stheraven                          typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1576227825Stheraven}
1577227825Stheraven
1578227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
1579227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1580227825Stheraven_ForwardIterator1
1581227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1582227825Stheraven       _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1583227825Stheraven{
1584227825Stheraven    typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1585227825Stheraven    typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1586227825Stheraven    return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1587227825Stheraven}
1588227825Stheraven
1589227825Stheraven// search_n
1590227825Stheraven
1591227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1592227825Stheraven_ForwardIterator
1593227825Stheraven__search_n(_ForwardIterator __first, _ForwardIterator __last,
1594227825Stheraven           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1595227825Stheraven{
1596227825Stheraven    if (__count <= 0)
1597227825Stheraven        return __first;
1598227825Stheraven    while (true)
1599227825Stheraven    {
1600227825Stheraven        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1601227825Stheraven        while (true)
1602227825Stheraven        {
1603227825Stheraven            if (__first == __last)  // return __last if no element matches __value_
1604227825Stheraven                return __last;
1605227825Stheraven            if (__pred(*__first, __value_))
1606227825Stheraven                break;
1607227825Stheraven            ++__first;
1608227825Stheraven        }
1609227825Stheraven        // *__first matches __value_, now match elements after here
1610227825Stheraven        _ForwardIterator __m = __first;
1611227825Stheraven        _Size __c(0);
1612227825Stheraven        while (true)
1613227825Stheraven        {
1614227825Stheraven            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1615227825Stheraven                return __first;
1616227825Stheraven            if (++__m == __last)  // Otherwise if source exhaused, pattern not found
1617227825Stheraven                return __last;
1618227825Stheraven            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1619227825Stheraven            {
1620227825Stheraven                __first = __m;
1621227825Stheraven                ++__first;
1622227825Stheraven                break;
1623227825Stheraven            }  // else there is a match, check next elements
1624227825Stheraven        }
1625227825Stheraven    }
1626227825Stheraven}
1627227825Stheraven
1628227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1629227825Stheraven_RandomAccessIterator
1630227825Stheraven__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1631227825Stheraven           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1632227825Stheraven{
1633227825Stheraven    if (__count <= 0)
1634227825Stheraven        return __first;
1635227825Stheraven    _Size __len = static_cast<_Size>(__last - __first);
1636227825Stheraven    if (__len < __count)
1637227825Stheraven        return __last;
1638227825Stheraven    const _RandomAccessIterator __s = __last - (__count - 1);  // Start of pattern match can't go beyond here
1639227825Stheraven    while (true)
1640227825Stheraven    {
1641227825Stheraven        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1642227825Stheraven        while (true)
1643227825Stheraven        {
1644249998Sdim            if (__first >= __s)  // return __last if no element matches __value_
1645227825Stheraven                return __last;
1646227825Stheraven            if (__pred(*__first, __value_))
1647227825Stheraven                break;
1648227825Stheraven            ++__first;
1649227825Stheraven        }
1650227825Stheraven        // *__first matches __value_, now match elements after here
1651227825Stheraven        _RandomAccessIterator __m = __first;
1652227825Stheraven        _Size __c(0);
1653227825Stheraven        while (true)
1654227825Stheraven        {
1655227825Stheraven            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1656227825Stheraven                return __first;
1657227825Stheraven             ++__m;          // no need to check range on __m because __s guarantees we have enough source
1658227825Stheraven            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1659227825Stheraven            {
1660227825Stheraven                __first = __m;
1661227825Stheraven                ++__first;
1662227825Stheraven                break;
1663227825Stheraven            }  // else there is a match, check next elements
1664227825Stheraven        }
1665227825Stheraven    }
1666227825Stheraven}
1667227825Stheraven
1668227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1669227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1670227825Stheraven_ForwardIterator
1671227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last,
1672227825Stheraven         _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1673227825Stheraven{
1674227825Stheraven    return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1675227825Stheraven           (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1676227825Stheraven}
1677227825Stheraven
1678227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp>
1679227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1680227825Stheraven_ForwardIterator
1681227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1682227825Stheraven{
1683227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1684227825Stheraven    return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
1685227825Stheraven}
1686227825Stheraven
1687227825Stheraven// copy
1688227825Stheraven
1689227825Stheraventemplate <class _Iter>
1690227825Stheravenstruct __libcpp_is_trivial_iterator
1691227825Stheraven{
1692227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1693227825Stheraven};
1694227825Stheraven
1695227825Stheraventemplate <class _Iter>
1696227825Stheravenstruct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1697227825Stheraven{
1698227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1699227825Stheraven};
1700227825Stheraven
1701227825Stheraventemplate <class _Iter>
1702227825Stheravenstruct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1703227825Stheraven{
1704227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1705227825Stheraven};
1706227825Stheraven
1707227825Stheraventemplate <class _Iter>
1708227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1709227825Stheraven_Iter
1710227825Stheraven__unwrap_iter(_Iter __i)
1711227825Stheraven{
1712227825Stheraven    return __i;
1713227825Stheraven}
1714227825Stheraven
1715227825Stheraventemplate <class _Tp>
1716227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1717227825Stheraventypename enable_if
1718227825Stheraven<
1719227825Stheraven    is_trivially_copy_assignable<_Tp>::value,
1720227825Stheraven    _Tp*
1721227825Stheraven>::type
1722227825Stheraven__unwrap_iter(move_iterator<_Tp*> __i)
1723227825Stheraven{
1724227825Stheraven    return __i.base();
1725227825Stheraven}
1726227825Stheraven
1727262801Sdim#if _LIBCPP_DEBUG_LEVEL < 2
1728262801Sdim
1729227825Stheraventemplate <class _Tp>
1730227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1731227825Stheraventypename enable_if
1732227825Stheraven<
1733227825Stheraven    is_trivially_copy_assignable<_Tp>::value,
1734227825Stheraven    _Tp*
1735227825Stheraven>::type
1736227825Stheraven__unwrap_iter(__wrap_iter<_Tp*> __i)
1737227825Stheraven{
1738227825Stheraven    return __i.base();
1739227825Stheraven}
1740227825Stheraven
1741262801Sdim#endif  // _LIBCPP_DEBUG_LEVEL < 2
1742262801Sdim
1743227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1744227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1745227825Stheraven_OutputIterator
1746227825Stheraven__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1747227825Stheraven{
1748278724Sdim    for (; __first != __last; ++__first, (void) ++__result)
1749227825Stheraven        *__result = *__first;
1750227825Stheraven    return __result;
1751227825Stheraven}
1752227825Stheraven
1753227825Stheraventemplate <class _Tp, class _Up>
1754227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1755227825Stheraventypename enable_if
1756227825Stheraven<
1757227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1758227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1759227825Stheraven    _Up*
1760227825Stheraven>::type
1761227825Stheraven__copy(_Tp* __first, _Tp* __last, _Up* __result)
1762227825Stheraven{
1763227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1764227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1765227825Stheraven    return __result + __n;
1766227825Stheraven}
1767227825Stheraven
1768227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1769227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1770227825Stheraven_OutputIterator
1771227825Stheravencopy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1772227825Stheraven{
1773227825Stheraven    return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1774227825Stheraven}
1775227825Stheraven
1776227825Stheraven// copy_backward
1777227825Stheraven
1778246487Stheraventemplate <class _BidirectionalIterator, class _OutputIterator>
1779227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1780227825Stheraven_OutputIterator
1781246487Stheraven__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1782227825Stheraven{
1783227825Stheraven    while (__first != __last)
1784227825Stheraven        *--__result = *--__last;
1785227825Stheraven    return __result;
1786227825Stheraven}
1787227825Stheraven
1788227825Stheraventemplate <class _Tp, class _Up>
1789227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1790227825Stheraventypename enable_if
1791227825Stheraven<
1792227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1793227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1794227825Stheraven    _Up*
1795227825Stheraven>::type
1796227825Stheraven__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1797227825Stheraven{
1798227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1799227825Stheraven    __result -= __n;
1800227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1801227825Stheraven    return __result;
1802227825Stheraven}
1803227825Stheraven
1804227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
1805227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1806227825Stheraven_BidirectionalIterator2
1807227825Stheravencopy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1808227825Stheraven              _BidirectionalIterator2 __result)
1809227825Stheraven{
1810227825Stheraven    return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1811227825Stheraven}
1812227825Stheraven
1813227825Stheraven// copy_if
1814227825Stheraven
1815227825Stheraventemplate<class _InputIterator, class _OutputIterator, class _Predicate>
1816227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1817227825Stheraven_OutputIterator
1818227825Stheravencopy_if(_InputIterator __first, _InputIterator __last,
1819227825Stheraven        _OutputIterator __result, _Predicate __pred)
1820227825Stheraven{
1821227825Stheraven    for (; __first != __last; ++__first)
1822227825Stheraven    {
1823227825Stheraven        if (__pred(*__first))
1824227825Stheraven        {
1825227825Stheraven            *__result = *__first;
1826227825Stheraven            ++__result;
1827227825Stheraven        }
1828227825Stheraven    }
1829227825Stheraven    return __result;
1830227825Stheraven}
1831227825Stheraven
1832227825Stheraven// copy_n
1833227825Stheraven
1834227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator>
1835227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1836227825Stheraventypename enable_if
1837227825Stheraven<
1838227825Stheraven    __is_input_iterator<_InputIterator>::value &&
1839227825Stheraven   !__is_random_access_iterator<_InputIterator>::value,
1840227825Stheraven    _OutputIterator
1841227825Stheraven>::type
1842227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1843227825Stheraven{
1844227825Stheraven    if (__n > 0)
1845227825Stheraven    {
1846227825Stheraven        *__result = *__first;
1847227825Stheraven        ++__result;
1848227825Stheraven        for (--__n; __n > 0; --__n)
1849227825Stheraven        {
1850227825Stheraven            ++__first;
1851227825Stheraven            *__result = *__first;
1852227825Stheraven            ++__result;
1853227825Stheraven        }
1854227825Stheraven    }
1855227825Stheraven    return __result;
1856227825Stheraven}
1857227825Stheraven
1858227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator>
1859227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1860227825Stheraventypename enable_if
1861227825Stheraven<
1862227825Stheraven    __is_random_access_iterator<_InputIterator>::value,
1863227825Stheraven    _OutputIterator
1864227825Stheraven>::type
1865227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1866227825Stheraven{
1867227825Stheraven    return _VSTD::copy(__first, __first + __n, __result);
1868227825Stheraven}
1869227825Stheraven
1870227825Stheraven// move
1871227825Stheraven
1872227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1873227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1874227825Stheraven_OutputIterator
1875227825Stheraven__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1876227825Stheraven{
1877278724Sdim    for (; __first != __last; ++__first, (void) ++__result)
1878227825Stheraven        *__result = _VSTD::move(*__first);
1879227825Stheraven    return __result;
1880227825Stheraven}
1881227825Stheraven
1882227825Stheraventemplate <class _Tp, class _Up>
1883227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1884227825Stheraventypename enable_if
1885227825Stheraven<
1886227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1887227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1888227825Stheraven    _Up*
1889227825Stheraven>::type
1890227825Stheraven__move(_Tp* __first, _Tp* __last, _Up* __result)
1891227825Stheraven{
1892227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1893227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1894227825Stheraven    return __result + __n;
1895227825Stheraven}
1896227825Stheraven
1897227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1898227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1899227825Stheraven_OutputIterator
1900227825Stheravenmove(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1901227825Stheraven{
1902227825Stheraven    return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1903227825Stheraven}
1904227825Stheraven
1905227825Stheraven// move_backward
1906227825Stheraven
1907227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1908227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1909227825Stheraven_OutputIterator
1910227825Stheraven__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1911227825Stheraven{
1912227825Stheraven    while (__first != __last)
1913227825Stheraven        *--__result = _VSTD::move(*--__last);
1914227825Stheraven    return __result;
1915227825Stheraven}
1916227825Stheraven
1917227825Stheraventemplate <class _Tp, class _Up>
1918227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1919227825Stheraventypename enable_if
1920227825Stheraven<
1921227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1922227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1923227825Stheraven    _Up*
1924227825Stheraven>::type
1925227825Stheraven__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1926227825Stheraven{
1927227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1928227825Stheraven    __result -= __n;
1929227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1930227825Stheraven    return __result;
1931227825Stheraven}
1932227825Stheraven
1933227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
1934227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1935227825Stheraven_BidirectionalIterator2
1936227825Stheravenmove_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1937227825Stheraven              _BidirectionalIterator2 __result)
1938227825Stheraven{
1939227825Stheraven    return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1940227825Stheraven}
1941227825Stheraven
1942227825Stheraven// iter_swap
1943227825Stheraven
1944227825Stheraven// moved to <type_traits> for better swap / noexcept support
1945227825Stheraven
1946227825Stheraven// transform
1947227825Stheraven
1948227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1949227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1950227825Stheraven_OutputIterator
1951227825Stheraventransform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1952227825Stheraven{
1953278724Sdim    for (; __first != __last; ++__first, (void) ++__result)
1954227825Stheraven        *__result = __op(*__first);
1955227825Stheraven    return __result;
1956227825Stheraven}
1957227825Stheraven
1958227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1959227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1960227825Stheraven_OutputIterator
1961227825Stheraventransform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1962227825Stheraven          _OutputIterator __result, _BinaryOperation __binary_op)
1963227825Stheraven{
1964278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
1965227825Stheraven        *__result = __binary_op(*__first1, *__first2);
1966227825Stheraven    return __result;
1967227825Stheraven}
1968227825Stheraven
1969227825Stheraven// replace
1970227825Stheraven
1971227825Stheraventemplate <class _ForwardIterator, class _Tp>
1972227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1973227825Stheravenvoid
1974227825Stheravenreplace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1975227825Stheraven{
1976227825Stheraven    for (; __first != __last; ++__first)
1977227825Stheraven        if (*__first == __old_value)
1978227825Stheraven            *__first = __new_value;
1979227825Stheraven}
1980227825Stheraven
1981227825Stheraven// replace_if
1982227825Stheraven
1983227825Stheraventemplate <class _ForwardIterator, class _Predicate, class _Tp>
1984227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1985227825Stheravenvoid
1986227825Stheravenreplace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1987227825Stheraven{
1988227825Stheraven    for (; __first != __last; ++__first)
1989227825Stheraven        if (__pred(*__first))
1990227825Stheraven            *__first = __new_value;
1991227825Stheraven}
1992227825Stheraven
1993227825Stheraven// replace_copy
1994227825Stheraven
1995227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp>
1996227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1997227825Stheraven_OutputIterator
1998227825Stheravenreplace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1999227825Stheraven             const _Tp& __old_value, const _Tp& __new_value)
2000227825Stheraven{
2001278724Sdim    for (; __first != __last; ++__first, (void) ++__result)
2002227825Stheraven        if (*__first == __old_value)
2003227825Stheraven            *__result = __new_value;
2004227825Stheraven        else
2005227825Stheraven            *__result = *__first;
2006227825Stheraven    return __result;
2007227825Stheraven}
2008227825Stheraven
2009227825Stheraven// replace_copy_if
2010227825Stheraven
2011227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2012227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2013227825Stheraven_OutputIterator
2014227825Stheravenreplace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2015227825Stheraven                _Predicate __pred, const _Tp& __new_value)
2016227825Stheraven{
2017278724Sdim    for (; __first != __last; ++__first, (void) ++__result)
2018227825Stheraven        if (__pred(*__first))
2019227825Stheraven            *__result = __new_value;
2020227825Stheraven        else
2021227825Stheraven            *__result = *__first;
2022227825Stheraven    return __result;
2023227825Stheraven}
2024227825Stheraven
2025227825Stheraven// fill_n
2026227825Stheraven
2027227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp>
2028227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2029227825Stheraven_OutputIterator
2030262801Sdim__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2031227825Stheraven{
2032278724Sdim    for (; __n > 0; ++__first, (void) --__n)
2033227825Stheraven        *__first = __value_;
2034227825Stheraven    return __first;
2035227825Stheraven}
2036227825Stheraven
2037262801Sdimtemplate <class _Tp, class _Size, class _Up>
2038227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2039262801Sdimtypename enable_if
2040262801Sdim<
2041262801Sdim    is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2042262801Sdim    !is_same<_Tp, bool>::value &&
2043262801Sdim    is_integral<_Up>::value && sizeof(_Up) == 1,
2044262801Sdim    _Tp*
2045262801Sdim>::type
2046262801Sdim__fill_n(_Tp* __first, _Size __n,_Up __value_)
2047227825Stheraven{
2048227825Stheraven    if (__n > 0)
2049227825Stheraven        _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2050227825Stheraven    return __first + __n;
2051227825Stheraven}
2052227825Stheraven
2053227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp>
2054227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2055227825Stheraven_OutputIterator
2056227825Stheravenfill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2057227825Stheraven{
2058262801Sdim   return _VSTD::__fill_n(__first, __n, __value_);
2059227825Stheraven}
2060227825Stheraven
2061227825Stheraven// fill
2062227825Stheraven
2063227825Stheraventemplate <class _ForwardIterator, class _Tp>
2064227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2065227825Stheravenvoid
2066227825Stheraven__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
2067227825Stheraven{
2068227825Stheraven    for (; __first != __last; ++__first)
2069227825Stheraven        *__first = __value_;
2070227825Stheraven}
2071227825Stheraven
2072227825Stheraventemplate <class _RandomAccessIterator, class _Tp>
2073227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2074227825Stheravenvoid
2075227825Stheraven__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
2076227825Stheraven{
2077227825Stheraven    _VSTD::fill_n(__first, __last - __first, __value_);
2078227825Stheraven}
2079227825Stheraven
2080227825Stheraventemplate <class _ForwardIterator, class _Tp>
2081227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2082227825Stheravenvoid
2083227825Stheravenfill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2084227825Stheraven{
2085227825Stheraven    _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
2086227825Stheraven}
2087227825Stheraven
2088227825Stheraven// generate
2089227825Stheraven
2090227825Stheraventemplate <class _ForwardIterator, class _Generator>
2091227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2092227825Stheravenvoid
2093227825Stheravengenerate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2094227825Stheraven{
2095227825Stheraven    for (; __first != __last; ++__first)
2096227825Stheraven        *__first = __gen();
2097227825Stheraven}
2098227825Stheraven
2099227825Stheraven// generate_n
2100227825Stheraven
2101227825Stheraventemplate <class _OutputIterator, class _Size, class _Generator>
2102227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2103227825Stheraven_OutputIterator
2104227825Stheravengenerate_n(_OutputIterator __first, _Size __n, _Generator __gen)
2105227825Stheraven{
2106278724Sdim    for (; __n > 0; ++__first, (void) --__n)
2107227825Stheraven        *__first = __gen();
2108227825Stheraven    return __first;
2109227825Stheraven}
2110227825Stheraven
2111227825Stheraven// remove
2112227825Stheraven
2113227825Stheraventemplate <class _ForwardIterator, class _Tp>
2114227825Stheraven_ForwardIterator
2115227825Stheravenremove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
2116227825Stheraven{
2117227825Stheraven    __first = _VSTD::find(__first, __last, __value_);
2118227825Stheraven    if (__first != __last)
2119227825Stheraven    {
2120227825Stheraven        _ForwardIterator __i = __first;
2121227825Stheraven        while (++__i != __last)
2122227825Stheraven        {
2123227825Stheraven            if (!(*__i == __value_))
2124227825Stheraven            {
2125227825Stheraven                *__first = _VSTD::move(*__i);
2126227825Stheraven                ++__first;
2127227825Stheraven            }
2128227825Stheraven        }
2129227825Stheraven    }
2130227825Stheraven    return __first;
2131227825Stheraven}
2132227825Stheraven
2133227825Stheraven// remove_if
2134227825Stheraven
2135227825Stheraventemplate <class _ForwardIterator, class _Predicate>
2136227825Stheraven_ForwardIterator
2137227825Stheravenremove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2138227825Stheraven{
2139227825Stheraven    __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
2140227825Stheraven                           (__first, __last, __pred);
2141227825Stheraven    if (__first != __last)
2142227825Stheraven    {
2143227825Stheraven        _ForwardIterator __i = __first;
2144227825Stheraven        while (++__i != __last)
2145227825Stheraven        {
2146227825Stheraven            if (!__pred(*__i))
2147227825Stheraven            {
2148227825Stheraven                *__first = _VSTD::move(*__i);
2149227825Stheraven                ++__first;
2150227825Stheraven            }
2151227825Stheraven        }
2152227825Stheraven    }
2153227825Stheraven    return __first;
2154227825Stheraven}
2155227825Stheraven
2156227825Stheraven// remove_copy
2157227825Stheraven
2158227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp>
2159227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2160227825Stheraven_OutputIterator
2161227825Stheravenremove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
2162227825Stheraven{
2163227825Stheraven    for (; __first != __last; ++__first)
2164227825Stheraven    {
2165227825Stheraven        if (!(*__first == __value_))
2166227825Stheraven        {
2167227825Stheraven            *__result = *__first;
2168227825Stheraven            ++__result;
2169227825Stheraven        }
2170227825Stheraven    }
2171227825Stheraven    return __result;
2172227825Stheraven}
2173227825Stheraven
2174227825Stheraven// remove_copy_if
2175227825Stheraven
2176227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate>
2177227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2178227825Stheraven_OutputIterator
2179227825Stheravenremove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2180227825Stheraven{
2181227825Stheraven    for (; __first != __last; ++__first)
2182227825Stheraven    {
2183227825Stheraven        if (!__pred(*__first))
2184227825Stheraven        {
2185227825Stheraven            *__result = *__first;
2186227825Stheraven            ++__result;
2187227825Stheraven        }
2188227825Stheraven    }
2189227825Stheraven    return __result;
2190227825Stheraven}
2191227825Stheraven
2192227825Stheraven// unique
2193227825Stheraven
2194227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate>
2195227825Stheraven_ForwardIterator
2196227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2197227825Stheraven{
2198227825Stheraven    __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
2199227825Stheraven                                 (__first, __last, __pred);
2200227825Stheraven    if (__first != __last)
2201227825Stheraven    {
2202227825Stheraven        // ...  a  a  ?  ...
2203227825Stheraven        //      f     i
2204227825Stheraven        _ForwardIterator __i = __first;
2205227825Stheraven        for (++__i; ++__i != __last;)
2206227825Stheraven            if (!__pred(*__first, *__i))
2207227825Stheraven                *++__first = _VSTD::move(*__i);
2208227825Stheraven        ++__first;
2209227825Stheraven    }
2210227825Stheraven    return __first;
2211227825Stheraven}
2212227825Stheraven
2213227825Stheraventemplate <class _ForwardIterator>
2214227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2215227825Stheraven_ForwardIterator
2216227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last)
2217227825Stheraven{
2218227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
2219227825Stheraven    return _VSTD::unique(__first, __last, __equal_to<__v>());
2220227825Stheraven}
2221227825Stheraven
2222227825Stheraven// unique_copy
2223227825Stheraven
2224227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2225227825Stheraven_OutputIterator
2226227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2227227825Stheraven              input_iterator_tag, output_iterator_tag)
2228227825Stheraven{
2229227825Stheraven    if (__first != __last)
2230227825Stheraven    {
2231227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t(*__first);
2232227825Stheraven        *__result = __t;
2233227825Stheraven        ++__result;
2234227825Stheraven        while (++__first != __last)
2235227825Stheraven        {
2236227825Stheraven            if (!__pred(__t, *__first))
2237227825Stheraven            {
2238227825Stheraven                __t = *__first;
2239227825Stheraven                *__result = __t;
2240227825Stheraven                ++__result;
2241227825Stheraven            }
2242227825Stheraven        }
2243227825Stheraven    }
2244227825Stheraven    return __result;
2245227825Stheraven}
2246227825Stheraven
2247227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2248227825Stheraven_OutputIterator
2249227825Stheraven__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2250227825Stheraven              forward_iterator_tag, output_iterator_tag)
2251227825Stheraven{
2252227825Stheraven    if (__first != __last)
2253227825Stheraven    {
2254227825Stheraven        _ForwardIterator __i = __first;
2255227825Stheraven        *__result = *__i;
2256227825Stheraven        ++__result;
2257227825Stheraven        while (++__first != __last)
2258227825Stheraven        {
2259227825Stheraven            if (!__pred(*__i, *__first))
2260227825Stheraven            {
2261227825Stheraven                *__result = *__first;
2262227825Stheraven                ++__result;
2263227825Stheraven                __i = __first;
2264227825Stheraven            }
2265227825Stheraven        }
2266227825Stheraven    }
2267227825Stheraven    return __result;
2268227825Stheraven}
2269227825Stheraven
2270227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2271227825Stheraven_ForwardIterator
2272227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2273227825Stheraven              input_iterator_tag, forward_iterator_tag)
2274227825Stheraven{
2275227825Stheraven    if (__first != __last)
2276227825Stheraven    {
2277227825Stheraven        *__result = *__first;
2278227825Stheraven        while (++__first != __last)
2279227825Stheraven            if (!__pred(*__result, *__first))
2280227825Stheraven                *++__result = *__first;
2281227825Stheraven        ++__result;
2282227825Stheraven    }
2283227825Stheraven    return __result;
2284227825Stheraven}
2285227825Stheraven
2286227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2287227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2288227825Stheraven_OutputIterator
2289227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2290227825Stheraven{
2291227825Stheraven    return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2292227825Stheraven                              (__first, __last, __result, __pred,
2293227825Stheraven                               typename iterator_traits<_InputIterator>::iterator_category(),
2294227825Stheraven                               typename iterator_traits<_OutputIterator>::iterator_category());
2295227825Stheraven}
2296227825Stheraven
2297227825Stheraventemplate <class _InputIterator, class _OutputIterator>
2298227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2299227825Stheraven_OutputIterator
2300227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2301227825Stheraven{
2302227825Stheraven    typedef typename iterator_traits<_InputIterator>::value_type __v;
2303227825Stheraven    return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2304227825Stheraven}
2305227825Stheraven
2306227825Stheraven// reverse
2307227825Stheraven
2308227825Stheraventemplate <class _BidirectionalIterator>
2309227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2310227825Stheravenvoid
2311227825Stheraven__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2312227825Stheraven{
2313227825Stheraven    while (__first != __last)
2314227825Stheraven    {
2315227825Stheraven        if (__first == --__last)
2316227825Stheraven            break;
2317227825Stheraven        swap(*__first, *__last);
2318227825Stheraven        ++__first;
2319227825Stheraven    }
2320227825Stheraven}
2321227825Stheraven
2322227825Stheraventemplate <class _RandomAccessIterator>
2323227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2324227825Stheravenvoid
2325227825Stheraven__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2326227825Stheraven{
2327227825Stheraven    if (__first != __last)
2328227825Stheraven        for (; __first < --__last; ++__first)
2329227825Stheraven            swap(*__first, *__last);
2330227825Stheraven}
2331227825Stheraven
2332227825Stheraventemplate <class _BidirectionalIterator>
2333227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2334227825Stheravenvoid
2335227825Stheravenreverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2336227825Stheraven{
2337227825Stheraven    _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2338227825Stheraven}
2339227825Stheraven
2340227825Stheraven// reverse_copy
2341227825Stheraven
2342227825Stheraventemplate <class _BidirectionalIterator, class _OutputIterator>
2343227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2344227825Stheraven_OutputIterator
2345227825Stheravenreverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2346227825Stheraven{
2347227825Stheraven    for (; __first != __last; ++__result)
2348227825Stheraven        *__result = *--__last;
2349227825Stheraven    return __result;
2350227825Stheraven}
2351227825Stheraven
2352227825Stheraven// rotate
2353227825Stheraven
2354227825Stheraventemplate <class _ForwardIterator>
2355227825Stheraven_ForwardIterator
2356241903Sdim__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2357227825Stheraven{
2358241903Sdim    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2359241903Sdim    value_type __tmp = _VSTD::move(*__first);
2360241903Sdim    _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2361241903Sdim    *__lm1 = _VSTD::move(__tmp);
2362241903Sdim    return __lm1;
2363241903Sdim}
2364241903Sdim
2365241903Sdimtemplate <class _BidirectionalIterator>
2366241903Sdim_BidirectionalIterator
2367241903Sdim__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2368241903Sdim{
2369241903Sdim    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2370241903Sdim    _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2371241903Sdim    value_type __tmp = _VSTD::move(*__lm1);
2372241903Sdim    _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2373241903Sdim    *__first = _VSTD::move(__tmp);
2374241903Sdim    return __fp1;
2375241903Sdim}
2376241903Sdim
2377241903Sdimtemplate <class _ForwardIterator>
2378241903Sdim_ForwardIterator
2379241903Sdim__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2380241903Sdim{
2381227825Stheraven    _ForwardIterator __i = __middle;
2382227825Stheraven    while (true)
2383227825Stheraven    {
2384227825Stheraven        swap(*__first, *__i);
2385227825Stheraven        ++__first;
2386227825Stheraven        if (++__i == __last)
2387227825Stheraven            break;
2388227825Stheraven        if (__first == __middle)
2389227825Stheraven            __middle = __i;
2390227825Stheraven    }
2391227825Stheraven    _ForwardIterator __r = __first;
2392227825Stheraven    if (__first != __middle)
2393227825Stheraven    {
2394227825Stheraven        __i = __middle;
2395227825Stheraven        while (true)
2396227825Stheraven        {
2397227825Stheraven            swap(*__first, *__i);
2398227825Stheraven            ++__first;
2399227825Stheraven            if (++__i == __last)
2400227825Stheraven            {
2401227825Stheraven                if (__first == __middle)
2402227825Stheraven                    break;
2403227825Stheraven                __i = __middle;
2404227825Stheraven            }
2405227825Stheraven            else if (__first == __middle)
2406227825Stheraven                __middle = __i;
2407227825Stheraven        }
2408227825Stheraven    }
2409227825Stheraven    return __r;
2410227825Stheraven}
2411227825Stheraven
2412227825Stheraventemplate<typename _Integral>
2413227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2414227825Stheraven_Integral
2415227825Stheraven__gcd(_Integral __x, _Integral __y)
2416227825Stheraven{
2417227825Stheraven    do
2418227825Stheraven    {
2419227825Stheraven        _Integral __t = __x % __y;
2420227825Stheraven        __x = __y;
2421227825Stheraven        __y = __t;
2422227825Stheraven    } while (__y);
2423227825Stheraven    return __x;
2424227825Stheraven}
2425227825Stheraven
2426227825Stheraventemplate<typename _RandomAccessIterator>
2427227825Stheraven_RandomAccessIterator
2428241903Sdim__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2429227825Stheraven{
2430227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2431227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2432227825Stheraven
2433227825Stheraven    const difference_type __m1 = __middle - __first;
2434227825Stheraven    const difference_type __m2 = __last - __middle;
2435227825Stheraven    if (__m1 == __m2)
2436227825Stheraven    {
2437227825Stheraven        _VSTD::swap_ranges(__first, __middle, __middle);
2438227825Stheraven        return __middle;
2439227825Stheraven    }
2440241903Sdim    const difference_type __g = _VSTD::__gcd(__m1, __m2);
2441227825Stheraven    for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2442227825Stheraven    {
2443241903Sdim        value_type __t(_VSTD::move(*--__p));
2444227825Stheraven        _RandomAccessIterator __p1 = __p;
2445227825Stheraven        _RandomAccessIterator __p2 = __p1 + __m1;
2446227825Stheraven        do
2447227825Stheraven        {
2448241903Sdim            *__p1 = _VSTD::move(*__p2);
2449227825Stheraven            __p1 = __p2;
2450227825Stheraven            const difference_type __d = __last - __p2;
2451227825Stheraven            if (__m1 < __d)
2452227825Stheraven                __p2 += __m1;
2453227825Stheraven            else
2454227825Stheraven                __p2 = __first + (__m1 - __d);
2455227825Stheraven        } while (__p2 != __p);
2456241903Sdim        *__p1 = _VSTD::move(__t);
2457227825Stheraven    }
2458227825Stheraven    return __first + __m2;
2459227825Stheraven}
2460227825Stheraven
2461227825Stheraventemplate <class _ForwardIterator>
2462227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2463227825Stheraven_ForwardIterator
2464241903Sdim__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2465241903Sdim         _VSTD::forward_iterator_tag)
2466241903Sdim{
2467241903Sdim    typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2468241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2469241903Sdim    {
2470241903Sdim        if (_VSTD::next(__first) == __middle)
2471241903Sdim            return _VSTD::__rotate_left(__first, __last);
2472241903Sdim    }
2473241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2474241903Sdim}
2475241903Sdim
2476241903Sdimtemplate <class _BidirectionalIterator>
2477241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2478241903Sdim_BidirectionalIterator
2479241903Sdim__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2480241903Sdim         _VSTD::bidirectional_iterator_tag)
2481241903Sdim{
2482241903Sdim    typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2483241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2484241903Sdim    {
2485241903Sdim        if (_VSTD::next(__first) == __middle)
2486241903Sdim            return _VSTD::__rotate_left(__first, __last);
2487241903Sdim        if (_VSTD::next(__middle) == __last)
2488241903Sdim            return _VSTD::__rotate_right(__first, __last);
2489241903Sdim    }
2490241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2491241903Sdim}
2492241903Sdim
2493241903Sdimtemplate <class _RandomAccessIterator>
2494241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2495241903Sdim_RandomAccessIterator
2496241903Sdim__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2497241903Sdim         _VSTD::random_access_iterator_tag)
2498241903Sdim{
2499241903Sdim    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2500241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2501241903Sdim    {
2502241903Sdim        if (_VSTD::next(__first) == __middle)
2503241903Sdim            return _VSTD::__rotate_left(__first, __last);
2504241903Sdim        if (_VSTD::next(__middle) == __last)
2505241903Sdim            return _VSTD::__rotate_right(__first, __last);
2506241903Sdim        return _VSTD::__rotate_gcd(__first, __middle, __last);
2507241903Sdim    }
2508241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2509241903Sdim}
2510241903Sdim
2511241903Sdimtemplate <class _ForwardIterator>
2512241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2513241903Sdim_ForwardIterator
2514227825Stheravenrotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2515227825Stheraven{
2516241903Sdim    if (__first == __middle)
2517241903Sdim        return __last;
2518241903Sdim    if (__middle == __last)
2519241903Sdim        return __first;
2520227825Stheraven    return _VSTD::__rotate(__first, __middle, __last,
2521241903Sdim                           typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2522227825Stheraven}
2523227825Stheraven
2524227825Stheraven// rotate_copy
2525227825Stheraven
2526227825Stheraventemplate <class _ForwardIterator, class _OutputIterator>
2527227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2528227825Stheraven_OutputIterator
2529227825Stheravenrotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2530227825Stheraven{
2531227825Stheraven    return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2532227825Stheraven}
2533227825Stheraven
2534227825Stheraven// min_element
2535227825Stheraven
2536227825Stheraventemplate <class _ForwardIterator, class _Compare>
2537278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2538227825Stheraven_ForwardIterator
2539278724Sdim__min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2540227825Stheraven{
2541227825Stheraven    if (__first != __last)
2542227825Stheraven    {
2543227825Stheraven        _ForwardIterator __i = __first;
2544227825Stheraven        while (++__i != __last)
2545227825Stheraven            if (__comp(*__i, *__first))
2546227825Stheraven                __first = __i;
2547227825Stheraven    }
2548227825Stheraven    return __first;
2549227825Stheraven}
2550227825Stheraven
2551278724Sdimtemplate <class _ForwardIterator, class _Compare>
2552278724Sdiminline _LIBCPP_INLINE_VISIBILITY
2553278724Sdim_ForwardIterator
2554278724Sdimmin_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2555278724Sdim{
2556278724Sdim    return __min_element(__first, __last, __comp);
2557278724Sdim}
2558278724Sdim
2559227825Stheraventemplate <class _ForwardIterator>
2560227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2561227825Stheraven_ForwardIterator
2562227825Stheravenmin_element(_ForwardIterator __first, _ForwardIterator __last)
2563227825Stheraven{
2564278724Sdim    return __min_element(__first, __last,
2565227825Stheraven              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2566227825Stheraven}
2567227825Stheraven
2568227825Stheraven// min
2569227825Stheraven
2570227825Stheraventemplate <class _Tp, class _Compare>
2571278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2572227825Stheravenconst _Tp&
2573227825Stheravenmin(const _Tp& __a, const _Tp& __b, _Compare __comp)
2574227825Stheraven{
2575227825Stheraven    return __comp(__b, __a) ? __b : __a;
2576227825Stheraven}
2577227825Stheraven
2578227825Stheraventemplate <class _Tp>
2579278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2580227825Stheravenconst _Tp&
2581227825Stheravenmin(const _Tp& __a, const _Tp& __b)
2582227825Stheraven{
2583227825Stheraven    return _VSTD::min(__a, __b, __less<_Tp>());
2584227825Stheraven}
2585227825Stheraven
2586227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2587227825Stheraven
2588227825Stheraventemplate<class _Tp, class _Compare>
2589278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2590227825Stheraven_Tp
2591227825Stheravenmin(initializer_list<_Tp> __t, _Compare __comp)
2592227825Stheraven{
2593278724Sdim    return *__min_element(__t.begin(), __t.end(), __comp);
2594227825Stheraven}
2595227825Stheraven
2596227825Stheraventemplate<class _Tp>
2597278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2598227825Stheraven_Tp
2599227825Stheravenmin(initializer_list<_Tp> __t)
2600227825Stheraven{
2601278724Sdim    return *__min_element(__t.begin(), __t.end(), __less<_Tp>());
2602227825Stheraven}
2603227825Stheraven
2604227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2605227825Stheraven
2606227825Stheraven// max_element
2607227825Stheraven
2608227825Stheraventemplate <class _ForwardIterator, class _Compare>
2609278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2610227825Stheraven_ForwardIterator
2611278724Sdim__max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2612227825Stheraven{
2613227825Stheraven    if (__first != __last)
2614227825Stheraven    {
2615227825Stheraven        _ForwardIterator __i = __first;
2616227825Stheraven        while (++__i != __last)
2617227825Stheraven            if (__comp(*__first, *__i))
2618227825Stheraven                __first = __i;
2619227825Stheraven    }
2620227825Stheraven    return __first;
2621227825Stheraven}
2622227825Stheraven
2623278724Sdim
2624278724Sdimtemplate <class _ForwardIterator, class _Compare>
2625278724Sdiminline _LIBCPP_INLINE_VISIBILITY
2626278724Sdim_ForwardIterator
2627278724Sdimmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2628278724Sdim{
2629278724Sdim    return __max_element(__first, __last, __comp);
2630278724Sdim}
2631278724Sdim
2632227825Stheraventemplate <class _ForwardIterator>
2633227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2634227825Stheraven_ForwardIterator
2635227825Stheravenmax_element(_ForwardIterator __first, _ForwardIterator __last)
2636227825Stheraven{
2637278724Sdim    return __max_element(__first, __last,
2638227825Stheraven              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2639227825Stheraven}
2640227825Stheraven
2641227825Stheraven// max
2642227825Stheraven
2643227825Stheraventemplate <class _Tp, class _Compare>
2644278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2645227825Stheravenconst _Tp&
2646227825Stheravenmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2647227825Stheraven{
2648227825Stheraven    return __comp(__a, __b) ? __b : __a;
2649227825Stheraven}
2650227825Stheraven
2651227825Stheraventemplate <class _Tp>
2652278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2653227825Stheravenconst _Tp&
2654227825Stheravenmax(const _Tp& __a, const _Tp& __b)
2655227825Stheraven{
2656227825Stheraven    return _VSTD::max(__a, __b, __less<_Tp>());
2657227825Stheraven}
2658227825Stheraven
2659227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2660227825Stheraven
2661227825Stheraventemplate<class _Tp, class _Compare>
2662278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2663227825Stheraven_Tp
2664227825Stheravenmax(initializer_list<_Tp> __t, _Compare __comp)
2665227825Stheraven{
2666278724Sdim    return *__max_element(__t.begin(), __t.end(), __comp);
2667227825Stheraven}
2668227825Stheraven
2669227825Stheraventemplate<class _Tp>
2670278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2671227825Stheraven_Tp
2672227825Stheravenmax(initializer_list<_Tp> __t)
2673227825Stheraven{
2674278724Sdim    return *__max_element(__t.begin(), __t.end(), __less<_Tp>());
2675227825Stheraven}
2676227825Stheraven
2677227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2678227825Stheraven
2679227825Stheraven// minmax_element
2680227825Stheraven
2681227825Stheraventemplate <class _ForwardIterator, class _Compare>
2682227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator>
2683227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2684227825Stheraven{
2685227825Stheraven  std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2686227825Stheraven  if (__first != __last)
2687227825Stheraven  {
2688227825Stheraven      if (++__first != __last)
2689227825Stheraven      {
2690227825Stheraven          if (__comp(*__first, *__result.first))
2691227825Stheraven              __result.first = __first;
2692227825Stheraven          else
2693227825Stheraven              __result.second = __first;
2694227825Stheraven          while (++__first != __last)
2695227825Stheraven          {
2696227825Stheraven              _ForwardIterator __i = __first;
2697227825Stheraven              if (++__first == __last)
2698227825Stheraven              {
2699227825Stheraven                  if (__comp(*__i, *__result.first))
2700227825Stheraven                      __result.first = __i;
2701227825Stheraven                  else if (!__comp(*__i, *__result.second))
2702227825Stheraven                      __result.second = __i;
2703227825Stheraven                  break;
2704227825Stheraven              }
2705227825Stheraven              else
2706227825Stheraven              {
2707227825Stheraven                  if (__comp(*__first, *__i))
2708227825Stheraven                  {
2709227825Stheraven                      if (__comp(*__first, *__result.first))
2710227825Stheraven                          __result.first = __first;
2711227825Stheraven                      if (!__comp(*__i, *__result.second))
2712227825Stheraven                          __result.second = __i;
2713227825Stheraven                  }
2714227825Stheraven                  else
2715227825Stheraven                  {
2716227825Stheraven                      if (__comp(*__i, *__result.first))
2717227825Stheraven                          __result.first = __i;
2718227825Stheraven                      if (!__comp(*__first, *__result.second))
2719227825Stheraven                          __result.second = __first;
2720227825Stheraven                  }
2721227825Stheraven              }
2722227825Stheraven          }
2723227825Stheraven      }
2724227825Stheraven  }
2725227825Stheraven  return __result;
2726227825Stheraven}
2727227825Stheraven
2728227825Stheraventemplate <class _ForwardIterator>
2729227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2730227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator>
2731227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last)
2732227825Stheraven{
2733278724Sdim    return _VSTD::minmax_element(__first, __last,
2734278724Sdim              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2735227825Stheraven}
2736227825Stheraven
2737227825Stheraven// minmax
2738227825Stheraven
2739227825Stheraventemplate<class _Tp, class _Compare>
2740278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2741227825Stheravenpair<const _Tp&, const _Tp&>
2742227825Stheravenminmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2743227825Stheraven{
2744227825Stheraven    return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2745227825Stheraven                              pair<const _Tp&, const _Tp&>(__a, __b);
2746227825Stheraven}
2747227825Stheraven
2748227825Stheraventemplate<class _Tp>
2749278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2750227825Stheravenpair<const _Tp&, const _Tp&>
2751227825Stheravenminmax(const _Tp& __a, const _Tp& __b)
2752227825Stheraven{
2753227825Stheraven    return _VSTD::minmax(__a, __b, __less<_Tp>());
2754227825Stheraven}
2755227825Stheraven
2756227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2757227825Stheraven
2758278724Sdimtemplate<class _Tp, class _Compare>
2759278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2760227825Stheravenpair<_Tp, _Tp>
2761278724Sdimminmax(initializer_list<_Tp> __t, _Compare __comp)
2762227825Stheraven{
2763278724Sdim    typedef typename initializer_list<_Tp>::const_iterator _Iter;
2764278724Sdim    _Iter __first = __t.begin();
2765278724Sdim    _Iter __last  = __t.end();
2766278724Sdim    std::pair<_Tp, _Tp> __result ( *__first, *__first );
2767278724Sdim
2768278724Sdim    ++__first;
2769278724Sdim    if (__t.size() % 2 == 0)
2770278724Sdim    {
2771278724Sdim        if (__comp(*__first,  __result.first))
2772278724Sdim            __result.first  = *__first;
2773278724Sdim        else
2774278724Sdim            __result.second = *__first;
2775278724Sdim        ++__first;
2776278724Sdim    }
2777278724Sdim    
2778278724Sdim    while (__first != __last)
2779278724Sdim    {
2780278724Sdim        _Tp __prev = *__first++;
2781278724Sdim        if (__comp(__prev, *__first)) {
2782278724Sdim            if (__comp(__prev, __result.first))    __result.first  = __prev;
2783278724Sdim            if (__comp(__result.second, *__first)) __result.second = *__first;
2784278724Sdim            }
2785278724Sdim        else {
2786278724Sdim            if (__comp(*__first, __result.first)) __result.first  = *__first;
2787278724Sdim            if (__comp(__result.second, __prev))  __result.second = __prev;
2788278724Sdim            }
2789278724Sdim                
2790278724Sdim        __first++;
2791278724Sdim    }
2792278724Sdim    return __result;
2793227825Stheraven}
2794227825Stheraven
2795278724Sdimtemplate<class _Tp>
2796278724Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2797227825Stheravenpair<_Tp, _Tp>
2798278724Sdimminmax(initializer_list<_Tp> __t)
2799227825Stheraven{
2800278724Sdim    return _VSTD::minmax(__t, __less<_Tp>());
2801227825Stheraven}
2802227825Stheraven
2803227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2804227825Stheraven
2805227825Stheraven// random_shuffle
2806227825Stheraven
2807227825Stheraven// __independent_bits_engine
2808227825Stheraven
2809232950Stheraventemplate <unsigned long long _Xp, size_t _Rp>
2810227825Stheravenstruct __log2_imp
2811227825Stheraven{
2812232950Stheraven    static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2813232950Stheraven                                           : __log2_imp<_Xp, _Rp - 1>::value;
2814227825Stheraven};
2815227825Stheraven
2816232950Stheraventemplate <unsigned long long _Xp>
2817232950Stheravenstruct __log2_imp<_Xp, 0>
2818227825Stheraven{
2819227825Stheraven    static const size_t value = 0;
2820227825Stheraven};
2821227825Stheraven
2822232950Stheraventemplate <size_t _Rp>
2823232950Stheravenstruct __log2_imp<0, _Rp>
2824227825Stheraven{
2825232950Stheraven    static const size_t value = _Rp + 1;
2826227825Stheraven};
2827227825Stheraven
2828232950Stheraventemplate <class _UI, _UI _Xp>
2829227825Stheravenstruct __log2
2830227825Stheraven{
2831232950Stheraven    static const size_t value = __log2_imp<_Xp,
2832227825Stheraven                                         sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2833227825Stheraven};
2834227825Stheraven
2835227825Stheraventemplate<class _Engine, class _UIntType>
2836227825Stheravenclass __independent_bits_engine
2837227825Stheraven{
2838227825Stheravenpublic:
2839227825Stheraven    // types
2840227825Stheraven    typedef _UIntType result_type;
2841227825Stheraven
2842227825Stheravenprivate:
2843227825Stheraven    typedef typename _Engine::result_type _Engine_result_type;
2844227825Stheraven    typedef typename conditional
2845227825Stheraven        <
2846227825Stheraven            sizeof(_Engine_result_type) <= sizeof(result_type),
2847227825Stheraven                result_type,
2848227825Stheraven                _Engine_result_type
2849227825Stheraven        >::type _Working_result_type;
2850227825Stheraven
2851227825Stheraven    _Engine& __e_;
2852227825Stheraven    size_t __w_;
2853227825Stheraven    size_t __w0_;
2854227825Stheraven    size_t __n_;
2855227825Stheraven    size_t __n0_;
2856227825Stheraven    _Working_result_type __y0_;
2857227825Stheraven    _Working_result_type __y1_;
2858227825Stheraven    _Engine_result_type __mask0_;
2859227825Stheraven    _Engine_result_type __mask1_;
2860227825Stheraven
2861234976Stheraven#ifdef _LIBCPP_HAS_NO_CONSTEXPR
2862232950Stheraven    static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2863234976Stheraven                                          + _Working_result_type(1);
2864234976Stheraven#else
2865234976Stheraven    static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2866234976Stheraven                                                      + _Working_result_type(1);
2867234976Stheraven#endif
2868234976Stheraven    static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2869234976Stheraven    static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2870234976Stheraven    static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2871227825Stheraven
2872227825Stheravenpublic:
2873227825Stheraven    // constructors and seeding functions
2874227825Stheraven    __independent_bits_engine(_Engine& __e, size_t __w);
2875227825Stheraven
2876227825Stheraven    // generating functions
2877232950Stheraven    result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2878227825Stheraven
2879227825Stheravenprivate:
2880227825Stheraven    result_type __eval(false_type);
2881227825Stheraven    result_type __eval(true_type);
2882227825Stheraven};
2883227825Stheraven
2884227825Stheraventemplate<class _Engine, class _UIntType>
2885227825Stheraven__independent_bits_engine<_Engine, _UIntType>
2886227825Stheraven    ::__independent_bits_engine(_Engine& __e, size_t __w)
2887227825Stheraven        : __e_(__e),
2888227825Stheraven          __w_(__w)
2889227825Stheraven{
2890227825Stheraven    __n_ = __w_ / __m + (__w_ % __m != 0);
2891227825Stheraven    __w0_ = __w_ / __n_;
2892232950Stheraven    if (_Rp == 0)
2893232950Stheraven        __y0_ = _Rp;
2894227825Stheraven    else if (__w0_ < _WDt)
2895232950Stheraven        __y0_ = (_Rp >> __w0_) << __w0_;
2896227825Stheraven    else
2897227825Stheraven        __y0_ = 0;
2898232950Stheraven    if (_Rp - __y0_ > __y0_ / __n_)
2899227825Stheraven    {
2900227825Stheraven        ++__n_;
2901227825Stheraven        __w0_ = __w_ / __n_;
2902227825Stheraven        if (__w0_ < _WDt)
2903232950Stheraven            __y0_ = (_Rp >> __w0_) << __w0_;
2904227825Stheraven        else
2905227825Stheraven            __y0_ = 0;
2906227825Stheraven    }
2907227825Stheraven    __n0_ = __n_ - __w_ % __n_;
2908227825Stheraven    if (__w0_ < _WDt - 1)
2909232950Stheraven        __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2910227825Stheraven    else
2911227825Stheraven        __y1_ = 0;
2912227825Stheraven    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2913227825Stheraven                          _Engine_result_type(0);
2914227825Stheraven    __mask1_ = __w0_ < _EDt - 1 ?
2915227825Stheraven                               _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2916227825Stheraven                               _Engine_result_type(~0);
2917227825Stheraven}
2918227825Stheraven
2919227825Stheraventemplate<class _Engine, class _UIntType>
2920227825Stheraveninline
2921227825Stheraven_UIntType
2922227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2923227825Stheraven{
2924227825Stheraven    return static_cast<result_type>(__e_() & __mask0_);
2925227825Stheraven}
2926227825Stheraven
2927227825Stheraventemplate<class _Engine, class _UIntType>
2928227825Stheraven_UIntType
2929227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2930227825Stheraven{
2931232950Stheraven    result_type _Sp = 0;
2932227825Stheraven    for (size_t __k = 0; __k < __n0_; ++__k)
2933227825Stheraven    {
2934227825Stheraven        _Engine_result_type __u;
2935227825Stheraven        do
2936227825Stheraven        {
2937227825Stheraven            __u = __e_() - _Engine::min();
2938227825Stheraven        } while (__u >= __y0_);
2939227825Stheraven        if (__w0_ < _WDt)
2940232950Stheraven            _Sp <<= __w0_;
2941227825Stheraven        else
2942232950Stheraven            _Sp = 0;
2943232950Stheraven        _Sp += __u & __mask0_;
2944227825Stheraven    }
2945227825Stheraven    for (size_t __k = __n0_; __k < __n_; ++__k)
2946227825Stheraven    {
2947227825Stheraven        _Engine_result_type __u;
2948227825Stheraven        do
2949227825Stheraven        {
2950227825Stheraven            __u = __e_() - _Engine::min();
2951227825Stheraven        } while (__u >= __y1_);
2952227825Stheraven        if (__w0_ < _WDt - 1)
2953232950Stheraven            _Sp <<= __w0_ + 1;
2954227825Stheraven        else
2955232950Stheraven            _Sp = 0;
2956232950Stheraven        _Sp += __u & __mask1_;
2957227825Stheraven    }
2958232950Stheraven    return _Sp;
2959227825Stheraven}
2960227825Stheraven
2961227825Stheraven// uniform_int_distribution
2962227825Stheraven
2963227825Stheraventemplate<class _IntType = int>
2964227825Stheravenclass uniform_int_distribution
2965227825Stheraven{
2966227825Stheravenpublic:
2967227825Stheraven    // types
2968227825Stheraven    typedef _IntType result_type;
2969227825Stheraven
2970227825Stheraven    class param_type
2971227825Stheraven    {
2972227825Stheraven        result_type __a_;
2973227825Stheraven        result_type __b_;
2974227825Stheraven    public:
2975227825Stheraven        typedef uniform_int_distribution distribution_type;
2976227825Stheraven
2977227825Stheraven        explicit param_type(result_type __a = 0,
2978227825Stheraven                            result_type __b = numeric_limits<result_type>::max())
2979227825Stheraven            : __a_(__a), __b_(__b) {}
2980227825Stheraven
2981227825Stheraven        result_type a() const {return __a_;}
2982227825Stheraven        result_type b() const {return __b_;}
2983227825Stheraven
2984227825Stheraven        friend bool operator==(const param_type& __x, const param_type& __y)
2985227825Stheraven            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2986227825Stheraven        friend bool operator!=(const param_type& __x, const param_type& __y)
2987227825Stheraven            {return !(__x == __y);}
2988227825Stheraven    };
2989227825Stheraven
2990227825Stheravenprivate:
2991227825Stheraven    param_type __p_;
2992227825Stheraven
2993227825Stheravenpublic:
2994227825Stheraven    // constructors and reset functions
2995227825Stheraven    explicit uniform_int_distribution(result_type __a = 0,
2996227825Stheraven                                      result_type __b = numeric_limits<result_type>::max())
2997227825Stheraven        : __p_(param_type(__a, __b)) {}
2998227825Stheraven    explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2999227825Stheraven    void reset() {}
3000227825Stheraven
3001227825Stheraven    // generating functions
3002227825Stheraven    template<class _URNG> result_type operator()(_URNG& __g)
3003227825Stheraven        {return (*this)(__g, __p_);}
3004227825Stheraven    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3005227825Stheraven
3006227825Stheraven    // property functions
3007227825Stheraven    result_type a() const {return __p_.a();}
3008227825Stheraven    result_type b() const {return __p_.b();}
3009227825Stheraven
3010227825Stheraven    param_type param() const {return __p_;}
3011227825Stheraven    void param(const param_type& __p) {__p_ = __p;}
3012227825Stheraven
3013227825Stheraven    result_type min() const {return a();}
3014227825Stheraven    result_type max() const {return b();}
3015227825Stheraven
3016227825Stheraven    friend bool operator==(const uniform_int_distribution& __x,
3017227825Stheraven                           const uniform_int_distribution& __y)
3018227825Stheraven        {return __x.__p_ == __y.__p_;}
3019227825Stheraven    friend bool operator!=(const uniform_int_distribution& __x,
3020227825Stheraven                           const uniform_int_distribution& __y)
3021227825Stheraven            {return !(__x == __y);}
3022227825Stheraven};
3023227825Stheraven
3024227825Stheraventemplate<class _IntType>
3025227825Stheraventemplate<class _URNG>
3026227825Stheraventypename uniform_int_distribution<_IntType>::result_type
3027227825Stheravenuniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3028227825Stheraven{
3029227825Stheraven    typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3030227825Stheraven                                            uint32_t, uint64_t>::type _UIntType;
3031232950Stheraven    const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3032232950Stheraven    if (_Rp == 1)
3033227825Stheraven        return __p.a();
3034227825Stheraven    const size_t _Dt = numeric_limits<_UIntType>::digits;
3035227825Stheraven    typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3036232950Stheraven    if (_Rp == 0)
3037227825Stheraven        return static_cast<result_type>(_Eng(__g, _Dt)());
3038232950Stheraven    size_t __w = _Dt - __clz(_Rp) - 1;
3039232950Stheraven    if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
3040227825Stheraven        ++__w;
3041227825Stheraven    _Eng __e(__g, __w);
3042227825Stheraven    _UIntType __u;
3043227825Stheraven    do
3044227825Stheraven    {
3045227825Stheraven        __u = __e();
3046232950Stheraven    } while (__u >= _Rp);
3047227825Stheraven    return static_cast<result_type>(__u + __p.a());
3048227825Stheraven}
3049227825Stheraven
3050262801Sdimclass _LIBCPP_TYPE_VIS __rs_default;
3051227825Stheraven
3052262801Sdim_LIBCPP_FUNC_VIS __rs_default __rs_get();
3053227825Stheraven
3054262801Sdimclass _LIBCPP_TYPE_VIS __rs_default
3055227825Stheraven{
3056227825Stheraven    static unsigned __c_;
3057227825Stheraven
3058227825Stheraven    __rs_default();
3059227825Stheravenpublic:
3060249998Sdim    typedef uint_fast32_t result_type;
3061227825Stheraven
3062227825Stheraven    static const result_type _Min = 0;
3063227825Stheraven    static const result_type _Max = 0xFFFFFFFF;
3064227825Stheraven
3065227825Stheraven    __rs_default(const __rs_default&);
3066227825Stheraven    ~__rs_default();
3067227825Stheraven
3068227825Stheraven    result_type operator()();
3069227825Stheraven
3070234976Stheraven    static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3071234976Stheraven    static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
3072227825Stheraven
3073262801Sdim    friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
3074227825Stheraven};
3075227825Stheraven
3076262801Sdim_LIBCPP_FUNC_VIS __rs_default __rs_get();
3077227825Stheraven
3078227825Stheraventemplate <class _RandomAccessIterator>
3079227825Stheravenvoid
3080227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3081227825Stheraven{
3082227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3083232950Stheraven    typedef uniform_int_distribution<ptrdiff_t> _Dp;
3084232950Stheraven    typedef typename _Dp::param_type _Pp;
3085227825Stheraven    difference_type __d = __last - __first;
3086227825Stheraven    if (__d > 1)
3087227825Stheraven    {
3088232950Stheraven        _Dp __uid;
3089227825Stheraven        __rs_default __g = __rs_get();
3090227825Stheraven        for (--__last, --__d; __first < __last; ++__first, --__d)
3091227825Stheraven        {
3092232950Stheraven            difference_type __i = __uid(__g, _Pp(0, __d));
3093227825Stheraven            if (__i != difference_type(0))
3094227825Stheraven                swap(*__first, *(__first + __i));
3095227825Stheraven        }
3096227825Stheraven    }
3097227825Stheraven}
3098227825Stheraven
3099227825Stheraventemplate <class _RandomAccessIterator, class _RandomNumberGenerator>
3100227825Stheravenvoid
3101227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3102227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3103227825Stheraven               _RandomNumberGenerator&& __rand)
3104227825Stheraven#else
3105227825Stheraven               _RandomNumberGenerator& __rand)
3106227825Stheraven#endif
3107227825Stheraven{
3108227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3109227825Stheraven    difference_type __d = __last - __first;
3110227825Stheraven    if (__d > 1)
3111227825Stheraven    {
3112227825Stheraven        for (--__last; __first < __last; ++__first, --__d)
3113227825Stheraven        {
3114227825Stheraven            difference_type __i = __rand(__d);
3115227825Stheraven            swap(*__first, *(__first + __i));
3116227825Stheraven        }
3117227825Stheraven    }
3118227825Stheraven}
3119227825Stheraven
3120227825Stheraventemplate<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3121227825Stheraven    void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3122227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3123227825Stheraven                 _UniformRandomNumberGenerator&& __g)
3124227825Stheraven#else
3125227825Stheraven                 _UniformRandomNumberGenerator& __g)
3126227825Stheraven#endif
3127227825Stheraven{
3128227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3129232950Stheraven    typedef uniform_int_distribution<ptrdiff_t> _Dp;
3130232950Stheraven    typedef typename _Dp::param_type _Pp;
3131227825Stheraven    difference_type __d = __last - __first;
3132227825Stheraven    if (__d > 1)
3133227825Stheraven    {
3134232950Stheraven        _Dp __uid;
3135227825Stheraven        for (--__last, --__d; __first < __last; ++__first, --__d)
3136227825Stheraven        {
3137232950Stheraven            difference_type __i = __uid(__g, _Pp(0, __d));
3138227825Stheraven            if (__i != difference_type(0))
3139227825Stheraven                swap(*__first, *(__first + __i));
3140227825Stheraven        }
3141227825Stheraven    }
3142227825Stheraven}
3143227825Stheraven
3144227825Stheraventemplate <class _InputIterator, class _Predicate>
3145227825Stheravenbool
3146227825Stheravenis_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3147227825Stheraven{
3148227825Stheraven    for (; __first != __last; ++__first)
3149227825Stheraven        if (!__pred(*__first))
3150227825Stheraven            break;
3151227825Stheraven    for (; __first != __last; ++__first)
3152227825Stheraven        if (__pred(*__first))
3153227825Stheraven            return false;
3154227825Stheraven    return true;
3155227825Stheraven}
3156227825Stheraven
3157227825Stheraven// partition
3158227825Stheraven
3159227825Stheraventemplate <class _Predicate, class _ForwardIterator>
3160227825Stheraven_ForwardIterator
3161227825Stheraven__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3162227825Stheraven{
3163227825Stheraven    while (true)
3164227825Stheraven    {
3165227825Stheraven        if (__first == __last)
3166227825Stheraven            return __first;
3167227825Stheraven        if (!__pred(*__first))
3168227825Stheraven            break;
3169227825Stheraven        ++__first;
3170227825Stheraven    }
3171227825Stheraven    for (_ForwardIterator __p = __first; ++__p != __last;)
3172227825Stheraven    {
3173227825Stheraven        if (__pred(*__p))
3174227825Stheraven        {
3175227825Stheraven            swap(*__first, *__p);
3176227825Stheraven            ++__first;
3177227825Stheraven        }
3178227825Stheraven    }
3179227825Stheraven    return __first;
3180227825Stheraven}
3181227825Stheraven
3182227825Stheraventemplate <class _Predicate, class _BidirectionalIterator>
3183227825Stheraven_BidirectionalIterator
3184227825Stheraven__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3185227825Stheraven            bidirectional_iterator_tag)
3186227825Stheraven{
3187227825Stheraven    while (true)
3188227825Stheraven    {
3189227825Stheraven        while (true)
3190227825Stheraven        {
3191227825Stheraven            if (__first == __last)
3192227825Stheraven                return __first;
3193227825Stheraven            if (!__pred(*__first))
3194227825Stheraven                break;
3195227825Stheraven            ++__first;
3196227825Stheraven        }
3197227825Stheraven        do
3198227825Stheraven        {
3199227825Stheraven            if (__first == --__last)
3200227825Stheraven                return __first;
3201227825Stheraven        } while (!__pred(*__last));
3202227825Stheraven        swap(*__first, *__last);
3203227825Stheraven        ++__first;
3204227825Stheraven    }
3205227825Stheraven}
3206227825Stheraven
3207227825Stheraventemplate <class _ForwardIterator, class _Predicate>
3208227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3209227825Stheraven_ForwardIterator
3210227825Stheravenpartition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3211227825Stheraven{
3212227825Stheraven    return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
3213227825Stheraven                            (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3214227825Stheraven}
3215227825Stheraven
3216227825Stheraven// partition_copy
3217227825Stheraven
3218227825Stheraventemplate <class _InputIterator, class _OutputIterator1,
3219227825Stheraven          class _OutputIterator2, class _Predicate>
3220227825Stheravenpair<_OutputIterator1, _OutputIterator2>
3221227825Stheravenpartition_copy(_InputIterator __first, _InputIterator __last,
3222227825Stheraven               _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3223227825Stheraven               _Predicate __pred)
3224227825Stheraven{
3225227825Stheraven    for (; __first != __last; ++__first)
3226227825Stheraven    {
3227227825Stheraven        if (__pred(*__first))
3228227825Stheraven        {
3229227825Stheraven            *__out_true = *__first;
3230227825Stheraven            ++__out_true;
3231227825Stheraven        }
3232227825Stheraven        else
3233227825Stheraven        {
3234227825Stheraven            *__out_false = *__first;
3235227825Stheraven            ++__out_false;
3236227825Stheraven        }
3237227825Stheraven    }
3238227825Stheraven    return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3239227825Stheraven}
3240227825Stheraven
3241227825Stheraven// partition_point
3242227825Stheraven
3243227825Stheraventemplate<class _ForwardIterator, class _Predicate>
3244227825Stheraven_ForwardIterator
3245227825Stheravenpartition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3246227825Stheraven{
3247227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3248227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3249227825Stheraven    while (__len != 0)
3250227825Stheraven    {
3251227825Stheraven        difference_type __l2 = __len / 2;
3252227825Stheraven        _ForwardIterator __m = __first;
3253227825Stheraven        _VSTD::advance(__m, __l2);
3254227825Stheraven        if (__pred(*__m))
3255227825Stheraven        {
3256227825Stheraven            __first = ++__m;
3257227825Stheraven            __len -= __l2 + 1;
3258227825Stheraven        }
3259227825Stheraven        else
3260227825Stheraven            __len = __l2;
3261227825Stheraven    }
3262227825Stheraven    return __first;
3263227825Stheraven}
3264227825Stheraven
3265227825Stheraven// stable_partition
3266227825Stheraven
3267227825Stheraventemplate <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3268227825Stheraven_ForwardIterator
3269227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3270227825Stheraven                   _Distance __len, _Pair __p, forward_iterator_tag __fit)
3271227825Stheraven{
3272227825Stheraven    // *__first is known to be false
3273227825Stheraven    // __len >= 1
3274227825Stheraven    if (__len == 1)
3275227825Stheraven        return __first;
3276227825Stheraven    if (__len == 2)
3277227825Stheraven    {
3278227825Stheraven        _ForwardIterator __m = __first;
3279227825Stheraven        if (__pred(*++__m))
3280227825Stheraven        {
3281227825Stheraven            swap(*__first, *__m);
3282227825Stheraven            return __m;
3283227825Stheraven        }
3284227825Stheraven        return __first;
3285227825Stheraven    }
3286227825Stheraven    if (__len <= __p.second)
3287227825Stheraven    {   // The buffer is big enough to use
3288227825Stheraven        typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3289227825Stheraven        __destruct_n __d(0);
3290227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3291227825Stheraven        // Move the falses into the temporary buffer, and the trues to the front of the line
3292227825Stheraven        // Update __first to always point to the end of the trues
3293227825Stheraven        value_type* __t = __p.first;
3294227825Stheraven        ::new(__t) value_type(_VSTD::move(*__first));
3295227825Stheraven        __d.__incr((value_type*)0);
3296227825Stheraven        ++__t;
3297227825Stheraven        _ForwardIterator __i = __first;
3298227825Stheraven        while (++__i != __last)
3299227825Stheraven        {
3300227825Stheraven            if (__pred(*__i))
3301227825Stheraven            {
3302227825Stheraven                *__first = _VSTD::move(*__i);
3303227825Stheraven                ++__first;
3304227825Stheraven            }
3305227825Stheraven            else
3306227825Stheraven            {
3307227825Stheraven                ::new(__t) value_type(_VSTD::move(*__i));
3308227825Stheraven                __d.__incr((value_type*)0);
3309227825Stheraven                ++__t;
3310227825Stheraven            }
3311227825Stheraven        }
3312227825Stheraven        // All trues now at start of range, all falses in buffer
3313227825Stheraven        // Move falses back into range, but don't mess up __first which points to first false
3314227825Stheraven        __i = __first;
3315227825Stheraven        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3316227825Stheraven            *__i = _VSTD::move(*__t2);
3317227825Stheraven        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3318227825Stheraven        return __first;
3319227825Stheraven    }
3320227825Stheraven    // Else not enough buffer, do in place
3321227825Stheraven    // __len >= 3
3322227825Stheraven    _ForwardIterator __m = __first;
3323227825Stheraven    _Distance __len2 = __len / 2;  // __len2 >= 2
3324227825Stheraven    _VSTD::advance(__m, __len2);
3325227825Stheraven    // recurse on [__first, __m), *__first know to be false
3326227825Stheraven    // F?????????????????
3327227825Stheraven    // f       m         l
3328227825Stheraven    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3329227825Stheraven    _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3330227825Stheraven    // TTTFFFFF??????????
3331227825Stheraven    // f  ff   m         l
3332227825Stheraven    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3333227825Stheraven    _ForwardIterator __m1 = __m;
3334227825Stheraven    _ForwardIterator __second_false = __last;
3335227825Stheraven    _Distance __len_half = __len - __len2;
3336227825Stheraven    while (__pred(*__m1))
3337227825Stheraven    {
3338227825Stheraven        if (++__m1 == __last)
3339227825Stheraven            goto __second_half_done;
3340227825Stheraven        --__len_half;
3341227825Stheraven    }
3342227825Stheraven    // TTTFFFFFTTTF??????
3343227825Stheraven    // f  ff   m  m1     l
3344227825Stheraven    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3345227825Stheraven__second_half_done:
3346227825Stheraven    // TTTFFFFFTTTTTFFFFF
3347227825Stheraven    // f  ff   m    sf   l
3348227825Stheraven    return _VSTD::rotate(__first_false, __m, __second_false);
3349227825Stheraven    // TTTTTTTTFFFFFFFFFF
3350227825Stheraven    //         |
3351227825Stheraven}
3352227825Stheraven
3353227825Stheravenstruct __return_temporary_buffer
3354227825Stheraven{
3355227825Stheraven    template <class _Tp>
3356227825Stheraven    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3357227825Stheraven};
3358227825Stheraven
3359227825Stheraventemplate <class _Predicate, class _ForwardIterator>
3360227825Stheraven_ForwardIterator
3361227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3362227825Stheraven                   forward_iterator_tag)
3363227825Stheraven{
3364227825Stheraven    const unsigned __alloc_limit = 3;  // might want to make this a function of trivial assignment
3365227825Stheraven    // Either prove all true and return __first or point to first false
3366227825Stheraven    while (true)
3367227825Stheraven    {
3368227825Stheraven        if (__first == __last)
3369227825Stheraven            return __first;
3370227825Stheraven        if (!__pred(*__first))
3371227825Stheraven            break;
3372227825Stheraven        ++__first;
3373227825Stheraven    }
3374227825Stheraven    // We now have a reduced range [__first, __last)
3375227825Stheraven    // *__first is known to be false
3376227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3377227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3378227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3379227825Stheraven    pair<value_type*, ptrdiff_t> __p(0, 0);
3380227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
3381227825Stheraven    if (__len >= __alloc_limit)
3382227825Stheraven    {
3383227825Stheraven        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3384227825Stheraven        __h.reset(__p.first);
3385227825Stheraven    }
3386227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3387227825Stheraven                             (__first, __last, __pred, __len, __p, forward_iterator_tag());
3388227825Stheraven}
3389227825Stheraven
3390227825Stheraventemplate <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3391227825Stheraven_BidirectionalIterator
3392227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3393227825Stheraven                   _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3394227825Stheraven{
3395227825Stheraven    // *__first is known to be false
3396227825Stheraven    // *__last is known to be true
3397227825Stheraven    // __len >= 2
3398227825Stheraven    if (__len == 2)
3399227825Stheraven    {
3400227825Stheraven        swap(*__first, *__last);
3401227825Stheraven        return __last;
3402227825Stheraven    }
3403227825Stheraven    if (__len == 3)
3404227825Stheraven    {
3405227825Stheraven        _BidirectionalIterator __m = __first;
3406227825Stheraven        if (__pred(*++__m))
3407227825Stheraven        {
3408227825Stheraven            swap(*__first, *__m);
3409227825Stheraven            swap(*__m, *__last);
3410227825Stheraven            return __last;
3411227825Stheraven        }
3412227825Stheraven        swap(*__m, *__last);
3413227825Stheraven        swap(*__first, *__m);
3414227825Stheraven        return __m;
3415227825Stheraven    }
3416227825Stheraven    if (__len <= __p.second)
3417227825Stheraven    {   // The buffer is big enough to use
3418227825Stheraven        typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3419227825Stheraven        __destruct_n __d(0);
3420227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3421227825Stheraven        // Move the falses into the temporary buffer, and the trues to the front of the line
3422227825Stheraven        // Update __first to always point to the end of the trues
3423227825Stheraven        value_type* __t = __p.first;
3424227825Stheraven        ::new(__t) value_type(_VSTD::move(*__first));
3425227825Stheraven        __d.__incr((value_type*)0);
3426227825Stheraven        ++__t;
3427227825Stheraven        _BidirectionalIterator __i = __first;
3428227825Stheraven        while (++__i != __last)
3429227825Stheraven        {
3430227825Stheraven            if (__pred(*__i))
3431227825Stheraven            {
3432227825Stheraven                *__first = _VSTD::move(*__i);
3433227825Stheraven                ++__first;
3434227825Stheraven            }
3435227825Stheraven            else
3436227825Stheraven            {
3437227825Stheraven                ::new(__t) value_type(_VSTD::move(*__i));
3438227825Stheraven                __d.__incr((value_type*)0);
3439227825Stheraven                ++__t;
3440227825Stheraven            }
3441227825Stheraven        }
3442227825Stheraven        // move *__last, known to be true
3443227825Stheraven        *__first = _VSTD::move(*__i);
3444227825Stheraven        __i = ++__first;
3445227825Stheraven        // All trues now at start of range, all falses in buffer
3446227825Stheraven        // Move falses back into range, but don't mess up __first which points to first false
3447227825Stheraven        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3448227825Stheraven            *__i = _VSTD::move(*__t2);
3449227825Stheraven        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3450227825Stheraven        return __first;
3451227825Stheraven    }
3452227825Stheraven    // Else not enough buffer, do in place
3453227825Stheraven    // __len >= 4
3454227825Stheraven    _BidirectionalIterator __m = __first;
3455227825Stheraven    _Distance __len2 = __len / 2;  // __len2 >= 2
3456227825Stheraven    _VSTD::advance(__m, __len2);
3457227825Stheraven    // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3458227825Stheraven    // F????????????????T
3459227825Stheraven    // f       m        l
3460227825Stheraven    _BidirectionalIterator __m1 = __m;
3461227825Stheraven    _BidirectionalIterator __first_false = __first;
3462227825Stheraven    _Distance __len_half = __len2;
3463227825Stheraven    while (!__pred(*--__m1))
3464227825Stheraven    {
3465227825Stheraven        if (__m1 == __first)
3466227825Stheraven            goto __first_half_done;
3467227825Stheraven        --__len_half;
3468227825Stheraven    }
3469227825Stheraven    // F???TFFF?????????T
3470227825Stheraven    // f   m1  m        l
3471227825Stheraven    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3472227825Stheraven    __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3473227825Stheraven__first_half_done:
3474227825Stheraven    // TTTFFFFF?????????T
3475227825Stheraven    // f  ff   m        l
3476227825Stheraven    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3477227825Stheraven    __m1 = __m;
3478227825Stheraven    _BidirectionalIterator __second_false = __last;
3479227825Stheraven    ++__second_false;
3480227825Stheraven    __len_half = __len - __len2;
3481227825Stheraven    while (__pred(*__m1))
3482227825Stheraven    {
3483227825Stheraven        if (++__m1 == __last)
3484227825Stheraven            goto __second_half_done;
3485227825Stheraven        --__len_half;
3486227825Stheraven    }
3487227825Stheraven    // TTTFFFFFTTTF?????T
3488227825Stheraven    // f  ff   m  m1    l
3489227825Stheraven    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3490227825Stheraven__second_half_done:
3491227825Stheraven    // TTTFFFFFTTTTTFFFFF
3492227825Stheraven    // f  ff   m    sf  l
3493227825Stheraven    return _VSTD::rotate(__first_false, __m, __second_false);
3494227825Stheraven    // TTTTTTTTFFFFFFFFFF
3495227825Stheraven    //         |
3496227825Stheraven}
3497227825Stheraven
3498227825Stheraventemplate <class _Predicate, class _BidirectionalIterator>
3499227825Stheraven_BidirectionalIterator
3500227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3501227825Stheraven                   bidirectional_iterator_tag)
3502227825Stheraven{
3503227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3504227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3505227825Stheraven    const difference_type __alloc_limit = 4;  // might want to make this a function of trivial assignment
3506227825Stheraven    // Either prove all true and return __first or point to first false
3507227825Stheraven    while (true)
3508227825Stheraven    {
3509227825Stheraven        if (__first == __last)
3510227825Stheraven            return __first;
3511227825Stheraven        if (!__pred(*__first))
3512227825Stheraven            break;
3513227825Stheraven        ++__first;
3514227825Stheraven    }
3515227825Stheraven    // __first points to first false, everything prior to __first is already set.
3516227825Stheraven    // Either prove [__first, __last) is all false and return __first, or point __last to last true
3517227825Stheraven    do
3518227825Stheraven    {
3519227825Stheraven        if (__first == --__last)
3520227825Stheraven            return __first;
3521227825Stheraven    } while (!__pred(*__last));
3522227825Stheraven    // We now have a reduced range [__first, __last]
3523227825Stheraven    // *__first is known to be false
3524227825Stheraven    // *__last is known to be true
3525227825Stheraven    // __len >= 2
3526227825Stheraven    difference_type __len = _VSTD::distance(__first, __last) + 1;
3527227825Stheraven    pair<value_type*, ptrdiff_t> __p(0, 0);
3528227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
3529227825Stheraven    if (__len >= __alloc_limit)
3530227825Stheraven    {
3531227825Stheraven        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3532227825Stheraven        __h.reset(__p.first);
3533227825Stheraven    }
3534227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3535227825Stheraven                             (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3536227825Stheraven}
3537227825Stheraven
3538227825Stheraventemplate <class _ForwardIterator, class _Predicate>
3539227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3540227825Stheraven_ForwardIterator
3541227825Stheravenstable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3542227825Stheraven{
3543227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3544227825Stheraven                             (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3545227825Stheraven}
3546227825Stheraven
3547227825Stheraven// is_sorted_until
3548227825Stheraven
3549227825Stheraventemplate <class _ForwardIterator, class _Compare>
3550227825Stheraven_ForwardIterator
3551227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3552227825Stheraven{
3553227825Stheraven    if (__first != __last)
3554227825Stheraven    {
3555227825Stheraven        _ForwardIterator __i = __first;
3556227825Stheraven        while (++__i != __last)
3557227825Stheraven        {
3558227825Stheraven            if (__comp(*__i, *__first))
3559227825Stheraven                return __i;
3560227825Stheraven            __first = __i;
3561227825Stheraven        }
3562227825Stheraven    }
3563227825Stheraven    return __last;
3564227825Stheraven}
3565227825Stheraven
3566227825Stheraventemplate<class _ForwardIterator>
3567227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3568227825Stheraven_ForwardIterator
3569227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3570227825Stheraven{
3571227825Stheraven    return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3572227825Stheraven}
3573227825Stheraven
3574227825Stheraven// is_sorted
3575227825Stheraven
3576227825Stheraventemplate <class _ForwardIterator, class _Compare>
3577227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3578227825Stheravenbool
3579227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3580227825Stheraven{
3581227825Stheraven    return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3582227825Stheraven}
3583227825Stheraven
3584227825Stheraventemplate<class _ForwardIterator>
3585227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3586227825Stheravenbool
3587227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last)
3588227825Stheraven{
3589227825Stheraven    return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3590227825Stheraven}
3591227825Stheraven
3592227825Stheraven// sort
3593227825Stheraven
3594227825Stheraven// stable, 2-3 compares, 0-2 swaps
3595227825Stheraven
3596227825Stheraventemplate <class _Compare, class _ForwardIterator>
3597227825Stheravenunsigned
3598227825Stheraven__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3599227825Stheraven{
3600227825Stheraven    unsigned __r = 0;
3601227825Stheraven    if (!__c(*__y, *__x))          // if x <= y
3602227825Stheraven    {
3603227825Stheraven        if (!__c(*__z, *__y))      // if y <= z
3604227825Stheraven            return __r;            // x <= y && y <= z
3605227825Stheraven                                   // x <= y && y > z
3606227825Stheraven        swap(*__y, *__z);          // x <= z && y < z
3607227825Stheraven        __r = 1;
3608227825Stheraven        if (__c(*__y, *__x))       // if x > y
3609227825Stheraven        {
3610227825Stheraven            swap(*__x, *__y);      // x < y && y <= z
3611227825Stheraven            __r = 2;
3612227825Stheraven        }
3613227825Stheraven        return __r;                // x <= y && y < z
3614227825Stheraven    }
3615227825Stheraven    if (__c(*__z, *__y))           // x > y, if y > z
3616227825Stheraven    {
3617227825Stheraven        swap(*__x, *__z);          // x < y && y < z
3618227825Stheraven        __r = 1;
3619227825Stheraven        return __r;
3620227825Stheraven    }
3621227825Stheraven    swap(*__x, *__y);              // x > y && y <= z
3622227825Stheraven    __r = 1;                       // x < y && x <= z
3623227825Stheraven    if (__c(*__z, *__y))           // if y > z
3624227825Stheraven    {
3625227825Stheraven        swap(*__y, *__z);          // x <= y && y < z
3626227825Stheraven        __r = 2;
3627227825Stheraven    }
3628227825Stheraven    return __r;
3629227825Stheraven}                                  // x <= y && y <= z
3630227825Stheraven
3631227825Stheraven// stable, 3-6 compares, 0-5 swaps
3632227825Stheraven
3633227825Stheraventemplate <class _Compare, class _ForwardIterator>
3634227825Stheravenunsigned
3635227825Stheraven__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3636227825Stheraven            _ForwardIterator __x4, _Compare __c)
3637227825Stheraven{
3638227825Stheraven    unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3639227825Stheraven    if (__c(*__x4, *__x3))
3640227825Stheraven    {
3641227825Stheraven        swap(*__x3, *__x4);
3642227825Stheraven        ++__r;
3643227825Stheraven        if (__c(*__x3, *__x2))
3644227825Stheraven        {
3645227825Stheraven            swap(*__x2, *__x3);
3646227825Stheraven            ++__r;
3647227825Stheraven            if (__c(*__x2, *__x1))
3648227825Stheraven            {
3649227825Stheraven                swap(*__x1, *__x2);
3650227825Stheraven                ++__r;
3651227825Stheraven            }
3652227825Stheraven        }
3653227825Stheraven    }
3654227825Stheraven    return __r;
3655227825Stheraven}
3656227825Stheraven
3657227825Stheraven// stable, 4-10 compares, 0-9 swaps
3658227825Stheraven
3659227825Stheraventemplate <class _Compare, class _ForwardIterator>
3660227825Stheravenunsigned
3661227825Stheraven__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3662227825Stheraven            _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3663227825Stheraven{
3664227825Stheraven    unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3665227825Stheraven    if (__c(*__x5, *__x4))
3666227825Stheraven    {
3667227825Stheraven        swap(*__x4, *__x5);
3668227825Stheraven        ++__r;
3669227825Stheraven        if (__c(*__x4, *__x3))
3670227825Stheraven        {
3671227825Stheraven            swap(*__x3, *__x4);
3672227825Stheraven            ++__r;
3673227825Stheraven            if (__c(*__x3, *__x2))
3674227825Stheraven            {
3675227825Stheraven                swap(*__x2, *__x3);
3676227825Stheraven                ++__r;
3677227825Stheraven                if (__c(*__x2, *__x1))
3678227825Stheraven                {
3679227825Stheraven                    swap(*__x1, *__x2);
3680227825Stheraven                    ++__r;
3681227825Stheraven                }
3682227825Stheraven            }
3683227825Stheraven        }
3684227825Stheraven    }
3685227825Stheraven    return __r;
3686227825Stheraven}
3687227825Stheraven
3688227825Stheraven// Assumes size > 0
3689227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3690227825Stheravenvoid
3691227825Stheraven__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3692227825Stheraven{
3693227825Stheraven    _BirdirectionalIterator __lm1 = __last;
3694227825Stheraven    for (--__lm1; __first != __lm1; ++__first)
3695227825Stheraven    {
3696227825Stheraven        _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3697227825Stheraven                                                        typename add_lvalue_reference<_Compare>::type>
3698227825Stheraven                                                       (__first, __last, __comp);
3699227825Stheraven        if (__i != __first)
3700227825Stheraven            swap(*__first, *__i);
3701227825Stheraven    }
3702227825Stheraven}
3703227825Stheraven
3704227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3705227825Stheravenvoid
3706227825Stheraven__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3707227825Stheraven{
3708227825Stheraven    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3709227825Stheraven    if (__first != __last)
3710227825Stheraven    {
3711227825Stheraven        _BirdirectionalIterator __i = __first;
3712227825Stheraven        for (++__i; __i != __last; ++__i)
3713227825Stheraven        {
3714227825Stheraven            _BirdirectionalIterator __j = __i;
3715227825Stheraven            value_type __t(_VSTD::move(*__j));
3716227825Stheraven            for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t,  *--__k); --__j)
3717227825Stheraven                *__j = _VSTD::move(*__k);
3718227825Stheraven            *__j = _VSTD::move(__t);
3719227825Stheraven        }
3720227825Stheraven    }
3721227825Stheraven}
3722227825Stheraven
3723227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3724227825Stheravenvoid
3725227825Stheraven__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3726227825Stheraven{
3727227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3728227825Stheraven    _RandomAccessIterator __j = __first+2;
3729227825Stheraven    __sort3<_Compare>(__first, __first+1, __j, __comp);
3730227825Stheraven    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3731227825Stheraven    {
3732227825Stheraven        if (__comp(*__i, *__j))
3733227825Stheraven        {
3734227825Stheraven            value_type __t(_VSTD::move(*__i));
3735227825Stheraven            _RandomAccessIterator __k = __j;
3736227825Stheraven            __j = __i;
3737227825Stheraven            do
3738227825Stheraven            {
3739227825Stheraven                *__j = _VSTD::move(*__k);
3740227825Stheraven                __j = __k;
3741227825Stheraven            } while (__j != __first && __comp(__t, *--__k));
3742227825Stheraven            *__j = _VSTD::move(__t);
3743227825Stheraven        }
3744227825Stheraven        __j = __i;
3745227825Stheraven    }
3746227825Stheraven}
3747227825Stheraven
3748227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3749227825Stheravenbool
3750227825Stheraven__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3751227825Stheraven{
3752227825Stheraven    switch (__last - __first)
3753227825Stheraven    {
3754227825Stheraven    case 0:
3755227825Stheraven    case 1:
3756227825Stheraven        return true;
3757227825Stheraven    case 2:
3758227825Stheraven        if (__comp(*--__last, *__first))
3759227825Stheraven            swap(*__first, *__last);
3760227825Stheraven        return true;
3761227825Stheraven    case 3:
3762227825Stheraven        _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3763227825Stheraven        return true;
3764227825Stheraven    case 4:
3765227825Stheraven        _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3766227825Stheraven        return true;
3767227825Stheraven    case 5:
3768227825Stheraven        _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3769227825Stheraven        return true;
3770227825Stheraven    }
3771227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3772227825Stheraven    _RandomAccessIterator __j = __first+2;
3773227825Stheraven    __sort3<_Compare>(__first, __first+1, __j, __comp);
3774227825Stheraven    const unsigned __limit = 8;
3775227825Stheraven    unsigned __count = 0;
3776227825Stheraven    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3777227825Stheraven    {
3778227825Stheraven        if (__comp(*__i, *__j))
3779227825Stheraven        {
3780227825Stheraven            value_type __t(_VSTD::move(*__i));
3781227825Stheraven            _RandomAccessIterator __k = __j;
3782227825Stheraven            __j = __i;
3783227825Stheraven            do
3784227825Stheraven            {
3785227825Stheraven                *__j = _VSTD::move(*__k);
3786227825Stheraven                __j = __k;
3787227825Stheraven            } while (__j != __first && __comp(__t, *--__k));
3788227825Stheraven            *__j = _VSTD::move(__t);
3789227825Stheraven            if (++__count == __limit)
3790227825Stheraven                return ++__i == __last;
3791227825Stheraven        }
3792227825Stheraven        __j = __i;
3793227825Stheraven    }
3794227825Stheraven    return true;
3795227825Stheraven}
3796227825Stheraven
3797227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3798227825Stheravenvoid
3799227825Stheraven__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3800227825Stheraven                      typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3801227825Stheraven{
3802227825Stheraven    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3803227825Stheraven    if (__first1 != __last1)
3804227825Stheraven    {
3805227825Stheraven        __destruct_n __d(0);
3806227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3807227825Stheraven        value_type* __last2 = __first2;
3808227825Stheraven        ::new(__last2) value_type(_VSTD::move(*__first1));
3809227825Stheraven        __d.__incr((value_type*)0);
3810227825Stheraven        for (++__last2; ++__first1 != __last1; ++__last2)
3811227825Stheraven        {
3812227825Stheraven            value_type* __j2 = __last2;
3813227825Stheraven            value_type* __i2 = __j2;
3814227825Stheraven            if (__comp(*__first1, *--__i2))
3815227825Stheraven            {
3816227825Stheraven                ::new(__j2) value_type(_VSTD::move(*__i2));
3817227825Stheraven                __d.__incr((value_type*)0);
3818227825Stheraven                for (--__j2; __i2 != __first2 && __comp(*__first1,  *--__i2); --__j2)
3819227825Stheraven                    *__j2 = _VSTD::move(*__i2);
3820227825Stheraven                *__j2 = _VSTD::move(*__first1);
3821227825Stheraven            }
3822227825Stheraven            else
3823227825Stheraven            {
3824227825Stheraven                ::new(__j2) value_type(_VSTD::move(*__first1));
3825227825Stheraven                __d.__incr((value_type*)0);
3826227825Stheraven            }
3827227825Stheraven        }
3828227825Stheraven        __h.release();
3829227825Stheraven    }
3830227825Stheraven}
3831227825Stheraven
3832227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3833227825Stheravenvoid
3834227825Stheraven__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3835227825Stheraven{
3836227825Stheraven    // _Compare is known to be a reference type
3837227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3838227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3839227825Stheraven    const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3840227825Stheraven                                    is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3841227825Stheraven    while (true)
3842227825Stheraven    {
3843227825Stheraven    __restart:
3844227825Stheraven        difference_type __len = __last - __first;
3845227825Stheraven        switch (__len)
3846227825Stheraven        {
3847227825Stheraven        case 0:
3848227825Stheraven        case 1:
3849227825Stheraven            return;
3850227825Stheraven        case 2:
3851227825Stheraven            if (__comp(*--__last, *__first))
3852227825Stheraven                swap(*__first, *__last);
3853227825Stheraven            return;
3854227825Stheraven        case 3:
3855227825Stheraven            _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3856227825Stheraven            return;
3857227825Stheraven        case 4:
3858227825Stheraven            _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3859227825Stheraven            return;
3860227825Stheraven        case 5:
3861227825Stheraven            _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3862227825Stheraven            return;
3863227825Stheraven        }
3864227825Stheraven        if (__len <= __limit)
3865227825Stheraven        {
3866227825Stheraven            _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3867227825Stheraven            return;
3868227825Stheraven        }
3869227825Stheraven        // __len > 5
3870227825Stheraven        _RandomAccessIterator __m = __first;
3871227825Stheraven        _RandomAccessIterator __lm1 = __last;
3872227825Stheraven        --__lm1;
3873227825Stheraven        unsigned __n_swaps;
3874227825Stheraven        {
3875227825Stheraven        difference_type __delta;
3876227825Stheraven        if (__len >= 1000)
3877227825Stheraven        {
3878227825Stheraven            __delta = __len/2;
3879227825Stheraven            __m += __delta;
3880227825Stheraven            __delta /= 2;
3881227825Stheraven            __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3882227825Stheraven        }
3883227825Stheraven        else
3884227825Stheraven        {
3885227825Stheraven            __delta = __len/2;
3886227825Stheraven            __m += __delta;
3887227825Stheraven            __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3888227825Stheraven        }
3889227825Stheraven        }
3890227825Stheraven        // *__m is median
3891227825Stheraven        // partition [__first, __m) < *__m and *__m <= [__m, __last)
3892227825Stheraven        // (this inhibits tossing elements equivalent to __m around unnecessarily)
3893227825Stheraven        _RandomAccessIterator __i = __first;
3894227825Stheraven        _RandomAccessIterator __j = __lm1;
3895227825Stheraven        // j points beyond range to be tested, *__m is known to be <= *__lm1
3896227825Stheraven        // The search going up is known to be guarded but the search coming down isn't.
3897227825Stheraven        // Prime the downward search with a guard.
3898227825Stheraven        if (!__comp(*__i, *__m))  // if *__first == *__m
3899227825Stheraven        {
3900227825Stheraven            // *__first == *__m, *__first doesn't go in first part
3901227825Stheraven            // manually guard downward moving __j against __i
3902227825Stheraven            while (true)
3903227825Stheraven            {
3904227825Stheraven                if (__i == --__j)
3905227825Stheraven                {
3906227825Stheraven                    // *__first == *__m, *__m <= all other elements
3907227825Stheraven                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3908227825Stheraven                    ++__i;  // __first + 1
3909227825Stheraven                    __j = __last;
3910227825Stheraven                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
3911227825Stheraven                    {
3912227825Stheraven                        while (true)
3913227825Stheraven                        {
3914227825Stheraven                            if (__i == __j)
3915227825Stheraven                                return;  // [__first, __last) all equivalent elements
3916227825Stheraven                            if (__comp(*__first, *__i))
3917227825Stheraven                            {
3918227825Stheraven                                swap(*__i, *__j);
3919227825Stheraven                                ++__n_swaps;
3920227825Stheraven                                ++__i;
3921227825Stheraven                                break;
3922227825Stheraven                            }
3923227825Stheraven                            ++__i;
3924227825Stheraven                        }
3925227825Stheraven                    }
3926227825Stheraven                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3927227825Stheraven                    if (__i == __j)
3928227825Stheraven                        return;
3929227825Stheraven                    while (true)
3930227825Stheraven                    {
3931227825Stheraven                        while (!__comp(*__first, *__i))
3932227825Stheraven                            ++__i;
3933227825Stheraven                        while (__comp(*__first, *--__j))
3934227825Stheraven                            ;
3935227825Stheraven                        if (__i >= __j)
3936227825Stheraven                            break;
3937227825Stheraven                        swap(*__i, *__j);
3938227825Stheraven                        ++__n_swaps;
3939227825Stheraven                        ++__i;
3940227825Stheraven                    }
3941227825Stheraven                    // [__first, __i) == *__first and *__first < [__i, __last)
3942227825Stheraven                    // The first part is sorted, sort the secod part
3943227825Stheraven                    // _VSTD::__sort<_Compare>(__i, __last, __comp);
3944227825Stheraven                    __first = __i;
3945227825Stheraven                    goto __restart;
3946227825Stheraven                }
3947227825Stheraven                if (__comp(*__j, *__m))
3948227825Stheraven                {
3949227825Stheraven                    swap(*__i, *__j);
3950227825Stheraven                    ++__n_swaps;
3951227825Stheraven                    break;  // found guard for downward moving __j, now use unguarded partition
3952227825Stheraven                }
3953227825Stheraven            }
3954227825Stheraven        }
3955227825Stheraven        // It is known that *__i < *__m
3956227825Stheraven        ++__i;
3957227825Stheraven        // j points beyond range to be tested, *__m is known to be <= *__lm1
3958227825Stheraven        // if not yet partitioned...
3959227825Stheraven        if (__i < __j)
3960227825Stheraven        {
3961227825Stheraven            // known that *(__i - 1) < *__m
3962227825Stheraven            // known that __i <= __m
3963227825Stheraven            while (true)
3964227825Stheraven            {
3965227825Stheraven                // __m still guards upward moving __i
3966227825Stheraven                while (__comp(*__i, *__m))
3967227825Stheraven                    ++__i;
3968227825Stheraven                // It is now known that a guard exists for downward moving __j
3969227825Stheraven                while (!__comp(*--__j, *__m))
3970227825Stheraven                    ;
3971227825Stheraven                if (__i > __j)
3972227825Stheraven                    break;
3973227825Stheraven                swap(*__i, *__j);
3974227825Stheraven                ++__n_swaps;
3975227825Stheraven                // It is known that __m != __j
3976227825Stheraven                // If __m just moved, follow it
3977227825Stheraven                if (__m == __i)
3978227825Stheraven                    __m = __j;
3979227825Stheraven                ++__i;
3980227825Stheraven            }
3981227825Stheraven        }
3982227825Stheraven        // [__first, __i) < *__m and *__m <= [__i, __last)
3983227825Stheraven        if (__i != __m && __comp(*__m, *__i))
3984227825Stheraven        {
3985227825Stheraven            swap(*__i, *__m);
3986227825Stheraven            ++__n_swaps;
3987227825Stheraven        }
3988227825Stheraven        // [__first, __i) < *__i and *__i <= [__i+1, __last)
3989227825Stheraven        // If we were given a perfect partition, see if insertion sort is quick...
3990227825Stheraven        if (__n_swaps == 0)
3991227825Stheraven        {
3992227825Stheraven            bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3993227825Stheraven            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
3994227825Stheraven            {
3995227825Stheraven                if (__fs)
3996227825Stheraven                    return;
3997227825Stheraven                __last = __i;
3998227825Stheraven                continue;
3999227825Stheraven            }
4000227825Stheraven            else
4001227825Stheraven            {
4002227825Stheraven                if (__fs)
4003227825Stheraven                {
4004227825Stheraven                    __first = ++__i;
4005227825Stheraven                    continue;
4006227825Stheraven                }
4007227825Stheraven            }
4008227825Stheraven        }
4009227825Stheraven        // sort smaller range with recursive call and larger with tail recursion elimination
4010227825Stheraven        if (__i - __first < __last - __i)
4011227825Stheraven        {
4012227825Stheraven            _VSTD::__sort<_Compare>(__first, __i, __comp);
4013227825Stheraven            // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4014227825Stheraven            __first = ++__i;
4015227825Stheraven        }
4016227825Stheraven        else
4017227825Stheraven        {
4018227825Stheraven            _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4019227825Stheraven            // _VSTD::__sort<_Compare>(__first, __i, __comp);
4020227825Stheraven            __last = __i;
4021227825Stheraven        }
4022227825Stheraven    }
4023227825Stheraven}
4024227825Stheraven
4025227825Stheraven// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4026227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4027227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4028227825Stheravenvoid
4029227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4030227825Stheraven{
4031262801Sdim#ifdef _LIBCPP_DEBUG
4032227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4033227825Stheraven    __debug_less<_Compare> __c(__comp);
4034227825Stheraven    __sort<_Comp_ref>(__first, __last, __c);
4035262801Sdim#else  // _LIBCPP_DEBUG
4036227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4037227825Stheraven    __sort<_Comp_ref>(__first, __last, __comp);
4038262801Sdim#endif  // _LIBCPP_DEBUG
4039227825Stheraven}
4040227825Stheraven
4041227825Stheraventemplate <class _RandomAccessIterator>
4042227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4043227825Stheravenvoid
4044227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4045227825Stheraven{
4046227825Stheraven    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4047227825Stheraven}
4048227825Stheraven
4049227825Stheraventemplate <class _Tp>
4050227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4051227825Stheravenvoid
4052227825Stheravensort(_Tp** __first, _Tp** __last)
4053227825Stheraven{
4054227825Stheraven    _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4055227825Stheraven}
4056227825Stheraven
4057227825Stheraventemplate <class _Tp>
4058227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4059227825Stheravenvoid
4060227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4061227825Stheraven{
4062227825Stheraven    _VSTD::sort(__first.base(), __last.base());
4063227825Stheraven}
4064227825Stheraven
4065227825Stheraventemplate <class _Tp, class _Compare>
4066227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4067227825Stheravenvoid
4068227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4069227825Stheraven{
4070227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4071227825Stheraven    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4072227825Stheraven}
4073227825Stheraven
4074262801Sdim#ifdef _LIBCPP_MSVC
4075227825Stheraven#pragma warning( push )
4076227825Stheraven#pragma warning( disable: 4231)
4077262801Sdim#endif // _LIBCPP_MSVC
4078262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4079262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4080262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4081262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4082262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4083262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4084262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4085262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4086262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4087262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4088262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4089262801Sdim_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>&))
4090262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4091262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4092262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4093227825Stheraven
4094262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4095262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4096262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4097262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4098262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4099262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4100262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4101262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4102262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4103262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4104262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4105262801Sdim_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>&))
4106262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4107262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4108262801Sdim_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
4109227825Stheraven
4110262801Sdim_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>&))
4111262801Sdim#ifdef _LIBCPP_MSVC
4112227825Stheraven#pragma warning( pop )
4113262801Sdim#endif  // _LIBCPP_MSVC
4114227825Stheraven
4115227825Stheraven// lower_bound
4116227825Stheraven
4117227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
4118227825Stheraven_ForwardIterator
4119227825Stheraven__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4120227825Stheraven{
4121227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4122227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
4123227825Stheraven    while (__len != 0)
4124227825Stheraven    {
4125227825Stheraven        difference_type __l2 = __len / 2;
4126227825Stheraven        _ForwardIterator __m = __first;
4127227825Stheraven        _VSTD::advance(__m, __l2);
4128227825Stheraven        if (__comp(*__m, __value_))
4129227825Stheraven        {
4130227825Stheraven            __first = ++__m;
4131227825Stheraven            __len -= __l2 + 1;
4132227825Stheraven        }
4133227825Stheraven        else
4134227825Stheraven            __len = __l2;
4135227825Stheraven    }
4136227825Stheraven    return __first;
4137227825Stheraven}
4138227825Stheraven
4139227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
4140227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4141227825Stheraven_ForwardIterator
4142227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4143227825Stheraven{
4144262801Sdim#ifdef _LIBCPP_DEBUG
4145227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4146227825Stheraven    __debug_less<_Compare> __c(__comp);
4147227825Stheraven    return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
4148262801Sdim#else  // _LIBCPP_DEBUG
4149227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4150227825Stheraven    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
4151262801Sdim#endif  // _LIBCPP_DEBUG
4152227825Stheraven}
4153227825Stheraven
4154227825Stheraventemplate <class _ForwardIterator, class _Tp>
4155227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4156227825Stheraven_ForwardIterator
4157227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4158227825Stheraven{
4159227825Stheraven    return _VSTD::lower_bound(__first, __last, __value_,
4160227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4161227825Stheraven}
4162227825Stheraven
4163227825Stheraven// upper_bound
4164227825Stheraven
4165227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
4166227825Stheraven_ForwardIterator
4167227825Stheraven__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4168227825Stheraven{
4169227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4170227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
4171227825Stheraven    while (__len != 0)
4172227825Stheraven    {
4173227825Stheraven        difference_type __l2 = __len / 2;
4174227825Stheraven        _ForwardIterator __m = __first;
4175227825Stheraven        _VSTD::advance(__m, __l2);
4176227825Stheraven        if (__comp(__value_, *__m))
4177227825Stheraven            __len = __l2;
4178227825Stheraven        else
4179227825Stheraven        {
4180227825Stheraven            __first = ++__m;
4181227825Stheraven            __len -= __l2 + 1;
4182227825Stheraven        }
4183227825Stheraven    }
4184227825Stheraven    return __first;
4185227825Stheraven}
4186227825Stheraven
4187227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
4188227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4189227825Stheraven_ForwardIterator
4190227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4191227825Stheraven{
4192262801Sdim#ifdef _LIBCPP_DEBUG
4193227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4194227825Stheraven    __debug_less<_Compare> __c(__comp);
4195227825Stheraven    return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
4196262801Sdim#else  // _LIBCPP_DEBUG
4197227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4198227825Stheraven    return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
4199262801Sdim#endif  // _LIBCPP_DEBUG
4200227825Stheraven}
4201227825Stheraven
4202227825Stheraventemplate <class _ForwardIterator, class _Tp>
4203227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4204227825Stheraven_ForwardIterator
4205227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4206227825Stheraven{
4207227825Stheraven    return _VSTD::upper_bound(__first, __last, __value_,
4208227825Stheraven                             __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4209227825Stheraven}
4210227825Stheraven
4211227825Stheraven// equal_range
4212227825Stheraven
4213227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
4214227825Stheravenpair<_ForwardIterator, _ForwardIterator>
4215227825Stheraven__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4216227825Stheraven{
4217227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
4218227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
4219227825Stheraven    while (__len != 0)
4220227825Stheraven    {
4221227825Stheraven        difference_type __l2 = __len / 2;
4222227825Stheraven        _ForwardIterator __m = __first;
4223227825Stheraven        _VSTD::advance(__m, __l2);
4224227825Stheraven        if (__comp(*__m, __value_))
4225227825Stheraven        {
4226227825Stheraven            __first = ++__m;
4227227825Stheraven            __len -= __l2 + 1;
4228227825Stheraven        }
4229227825Stheraven        else if (__comp(__value_, *__m))
4230227825Stheraven        {
4231227825Stheraven            __last = __m;
4232227825Stheraven            __len = __l2;
4233227825Stheraven        }
4234227825Stheraven        else
4235227825Stheraven        {
4236227825Stheraven            _ForwardIterator __mp1 = __m;
4237227825Stheraven            return pair<_ForwardIterator, _ForwardIterator>
4238227825Stheraven                   (
4239227825Stheraven                      __lower_bound<_Compare>(__first, __m, __value_, __comp),
4240227825Stheraven                      __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
4241227825Stheraven                   );
4242227825Stheraven        }
4243227825Stheraven    }
4244227825Stheraven    return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4245227825Stheraven}
4246227825Stheraven
4247227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
4248227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4249227825Stheravenpair<_ForwardIterator, _ForwardIterator>
4250227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4251227825Stheraven{
4252262801Sdim#ifdef _LIBCPP_DEBUG
4253227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4254227825Stheraven    __debug_less<_Compare> __c(__comp);
4255227825Stheraven    return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
4256262801Sdim#else  // _LIBCPP_DEBUG
4257227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4258227825Stheraven    return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
4259262801Sdim#endif  // _LIBCPP_DEBUG
4260227825Stheraven}
4261227825Stheraven
4262227825Stheraventemplate <class _ForwardIterator, class _Tp>
4263227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4264227825Stheravenpair<_ForwardIterator, _ForwardIterator>
4265227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4266227825Stheraven{
4267227825Stheraven    return _VSTD::equal_range(__first, __last, __value_,
4268227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4269227825Stheraven}
4270227825Stheraven
4271227825Stheraven// binary_search
4272227825Stheraven
4273227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
4274227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4275227825Stheravenbool
4276227825Stheraven__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4277227825Stheraven{
4278227825Stheraven    __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4279227825Stheraven    return __first != __last && !__comp(__value_, *__first);
4280227825Stheraven}
4281227825Stheraven
4282227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
4283227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4284227825Stheravenbool
4285227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
4286227825Stheraven{
4287262801Sdim#ifdef _LIBCPP_DEBUG
4288227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4289227825Stheraven    __debug_less<_Compare> __c(__comp);
4290227825Stheraven    return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
4291262801Sdim#else  // _LIBCPP_DEBUG
4292227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4293227825Stheraven    return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4294262801Sdim#endif  // _LIBCPP_DEBUG
4295227825Stheraven}
4296227825Stheraven
4297227825Stheraventemplate <class _ForwardIterator, class _Tp>
4298227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4299227825Stheravenbool
4300227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4301227825Stheraven{
4302227825Stheraven    return _VSTD::binary_search(__first, __last, __value_,
4303227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4304227825Stheraven}
4305227825Stheraven
4306227825Stheraven// merge
4307227825Stheraven
4308227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4309227825Stheraven_OutputIterator
4310227825Stheraven__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4311227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4312227825Stheraven{
4313227825Stheraven    for (; __first1 != __last1; ++__result)
4314227825Stheraven    {
4315227825Stheraven        if (__first2 == __last2)
4316227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
4317227825Stheraven        if (__comp(*__first2, *__first1))
4318227825Stheraven        {
4319227825Stheraven            *__result = *__first2;
4320227825Stheraven            ++__first2;
4321227825Stheraven        }
4322227825Stheraven        else
4323227825Stheraven        {
4324227825Stheraven            *__result = *__first1;
4325227825Stheraven            ++__first1;
4326227825Stheraven        }
4327227825Stheraven    }
4328227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
4329227825Stheraven}
4330227825Stheraven
4331227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4332227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4333227825Stheraven_OutputIterator
4334227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1,
4335227825Stheraven      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4336227825Stheraven{
4337262801Sdim#ifdef _LIBCPP_DEBUG
4338227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4339227825Stheraven    __debug_less<_Compare> __c(__comp);
4340227825Stheraven    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4341262801Sdim#else  // _LIBCPP_DEBUG
4342227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4343227825Stheraven    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4344262801Sdim#endif  // _LIBCPP_DEBUG
4345227825Stheraven}
4346227825Stheraven
4347227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4348227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4349227825Stheraven_OutputIterator
4350227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1,
4351227825Stheraven      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4352227825Stheraven{
4353227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4354227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4355227825Stheraven    return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4356227825Stheraven}
4357227825Stheraven
4358227825Stheraven// inplace_merge
4359227825Stheraven
4360227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
4361227825Stheravenvoid
4362227825Stheraven__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4363227825Stheraven                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4364227825Stheraven                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4365227825Stheraven                typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4366227825Stheraven{
4367227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4368227825Stheraven    __destruct_n __d(0);
4369227825Stheraven    unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4370227825Stheraven    if (__len1 <= __len2)
4371227825Stheraven    {
4372227825Stheraven        value_type* __p = __buff;
4373278724Sdim        for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4374227825Stheraven            ::new(__p) value_type(_VSTD::move(*__i));
4375227825Stheraven        __merge<_Compare>(move_iterator<value_type*>(__buff),
4376227825Stheraven                          move_iterator<value_type*>(__p),
4377227825Stheraven                          move_iterator<_BidirectionalIterator>(__middle),
4378227825Stheraven                          move_iterator<_BidirectionalIterator>(__last),
4379227825Stheraven                          __first, __comp);
4380227825Stheraven    }
4381227825Stheraven    else
4382227825Stheraven    {
4383227825Stheraven        value_type* __p = __buff;
4384278724Sdim        for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4385227825Stheraven            ::new(__p) value_type(_VSTD::move(*__i));
4386227825Stheraven        typedef reverse_iterator<_BidirectionalIterator> _RBi;
4387227825Stheraven        typedef reverse_iterator<value_type*> _Rv;
4388227825Stheraven        __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
4389227825Stheraven                move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
4390227825Stheraven                _RBi(__last), __negate<_Compare>(__comp));
4391227825Stheraven    }
4392227825Stheraven}
4393227825Stheraven
4394227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
4395227825Stheravenvoid
4396227825Stheraven__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4397227825Stheraven                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4398227825Stheraven                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4399227825Stheraven                typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4400227825Stheraven{
4401227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4402227825Stheraven    while (true)
4403227825Stheraven    {
4404227825Stheraven        // if __middle == __last, we're done
4405227825Stheraven        if (__len2 == 0)
4406227825Stheraven            return;
4407227825Stheraven        // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4408278724Sdim        for (; true; ++__first, (void) --__len1)
4409227825Stheraven        {
4410227825Stheraven            if (__len1 == 0)
4411227825Stheraven                return;
4412227825Stheraven            if (__comp(*__middle, *__first))
4413227825Stheraven                break;
4414227825Stheraven        }
4415227825Stheraven        if (__len1 <= __buff_size || __len2 <= __buff_size)
4416227825Stheraven        {
4417227825Stheraven            __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
4418227825Stheraven            return;
4419227825Stheraven        }
4420227825Stheraven        // __first < __middle < __last
4421227825Stheraven        // *__first > *__middle
4422227825Stheraven        // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4423227825Stheraven        //     all elements in:
4424227825Stheraven        //         [__first, __m1)  <= [__middle, __m2)
4425227825Stheraven        //         [__middle, __m2) <  [__m1, __middle)
4426227825Stheraven        //         [__m1, __middle) <= [__m2, __last)
4427227825Stheraven        //     and __m1 or __m2 is in the middle of its range
4428227825Stheraven        _BidirectionalIterator __m1;  // "median" of [__first, __middle)
4429227825Stheraven        _BidirectionalIterator __m2;  // "median" of [__middle, __last)
4430227825Stheraven        difference_type __len11;      // distance(__first, __m1)
4431227825Stheraven        difference_type __len21;      // distance(__middle, __m2)
4432227825Stheraven        // binary search smaller range
4433227825Stheraven        if (__len1 < __len2)
4434227825Stheraven        {   // __len >= 1, __len2 >= 2
4435227825Stheraven            __len21 = __len2 / 2;
4436227825Stheraven            __m2 = __middle;
4437227825Stheraven            _VSTD::advance(__m2, __len21);
4438227825Stheraven            __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4439227825Stheraven            __len11 = _VSTD::distance(__first, __m1);
4440227825Stheraven        }
4441227825Stheraven        else
4442227825Stheraven        {
4443227825Stheraven            if (__len1 == 1)
4444227825Stheraven            {   // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4445227825Stheraven                // It is known *__first > *__middle
4446227825Stheraven                swap(*__first, *__middle);
4447227825Stheraven                return;
4448227825Stheraven            }
4449227825Stheraven            // __len1 >= 2, __len2 >= 1
4450227825Stheraven            __len11 = __len1 / 2;
4451227825Stheraven            __m1 = __first;
4452227825Stheraven            _VSTD::advance(__m1, __len11);
4453227825Stheraven            __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4454227825Stheraven            __len21 = _VSTD::distance(__middle, __m2);
4455227825Stheraven        }
4456227825Stheraven        difference_type __len12 = __len1 - __len11;  // distance(__m1, __middle)
4457227825Stheraven        difference_type __len22 = __len2 - __len21;  // distance(__m2, __last)
4458227825Stheraven        // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4459227825Stheraven        // swap middle two partitions
4460227825Stheraven        __middle = _VSTD::rotate(__m1, __middle, __m2);
4461227825Stheraven        // __len12 and __len21 now have swapped meanings
4462227825Stheraven        // merge smaller range with recurisve call and larger with tail recursion elimination
4463227825Stheraven        if (__len11 + __len21 < __len12 + __len22)
4464227825Stheraven        {
4465227825Stheraven            __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4466227825Stheraven//          __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4467227825Stheraven            __first = __middle;
4468227825Stheraven            __middle = __m2;
4469227825Stheraven            __len1 = __len12;
4470227825Stheraven            __len2 = __len22;
4471227825Stheraven        }
4472227825Stheraven        else
4473227825Stheraven        {
4474227825Stheraven            __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4475227825Stheraven//          __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4476227825Stheraven            __last = __middle;
4477227825Stheraven            __middle = __m1;
4478227825Stheraven            __len1 = __len11;
4479227825Stheraven            __len2 = __len21;
4480227825Stheraven        }
4481227825Stheraven    }
4482227825Stheraven}
4483227825Stheraven
4484227825Stheraventemplate <class _Tp>
4485227825Stheravenstruct __inplace_merge_switch
4486227825Stheraven{
4487227825Stheraven    static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
4488227825Stheraven};
4489227825Stheraven
4490227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
4491227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4492227825Stheravenvoid
4493227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4494227825Stheraven              _Compare __comp)
4495227825Stheraven{
4496227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4497227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4498227825Stheraven    difference_type __len1 = _VSTD::distance(__first, __middle);
4499227825Stheraven    difference_type __len2 = _VSTD::distance(__middle, __last);
4500227825Stheraven    difference_type __buf_size = _VSTD::min(__len1, __len2);
4501227825Stheraven    pair<value_type*, ptrdiff_t> __buf(0, 0);
4502227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
4503227825Stheraven    if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4504227825Stheraven    {
4505227825Stheraven        __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4506227825Stheraven        __h.reset(__buf.first);
4507227825Stheraven    }
4508262801Sdim#ifdef _LIBCPP_DEBUG
4509227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4510227825Stheraven    __debug_less<_Compare> __c(__comp);
4511227825Stheraven    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4512227825Stheraven                                            __buf.first, __buf.second);
4513262801Sdim#else  // _LIBCPP_DEBUG
4514227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4515227825Stheraven    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4516227825Stheraven                                            __buf.first, __buf.second);
4517262801Sdim#endif  // _LIBCPP_DEBUG
4518227825Stheraven}
4519227825Stheraven
4520227825Stheraventemplate <class _BidirectionalIterator>
4521227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4522227825Stheravenvoid
4523227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4524227825Stheraven{
4525227825Stheraven    _VSTD::inplace_merge(__first, __middle, __last,
4526227825Stheraven                        __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4527227825Stheraven}
4528227825Stheraven
4529227825Stheraven// stable_sort
4530227825Stheraven
4531227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
4532227825Stheravenvoid
4533227825Stheraven__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4534227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2,
4535227825Stheraven        typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4536227825Stheraven{
4537227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4538227825Stheraven    __destruct_n __d(0);
4539227825Stheraven    unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4540227825Stheraven    for (; true; ++__result)
4541227825Stheraven    {
4542227825Stheraven        if (__first1 == __last1)
4543227825Stheraven        {
4544227825Stheraven            for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4545227825Stheraven                ::new (__result) value_type(_VSTD::move(*__first2));
4546227825Stheraven            __h.release();
4547227825Stheraven            return;
4548227825Stheraven        }
4549227825Stheraven        if (__first2 == __last2)
4550227825Stheraven        {
4551227825Stheraven            for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4552227825Stheraven                ::new (__result) value_type(_VSTD::move(*__first1));
4553227825Stheraven            __h.release();
4554227825Stheraven            return;
4555227825Stheraven        }
4556227825Stheraven        if (__comp(*__first2, *__first1))
4557227825Stheraven        {
4558227825Stheraven            ::new (__result) value_type(_VSTD::move(*__first2));
4559227825Stheraven            __d.__incr((value_type*)0);
4560227825Stheraven            ++__first2;
4561227825Stheraven        }
4562227825Stheraven        else
4563227825Stheraven        {
4564227825Stheraven            ::new (__result) value_type(_VSTD::move(*__first1));
4565227825Stheraven            __d.__incr((value_type*)0);
4566227825Stheraven            ++__first1;
4567227825Stheraven        }
4568227825Stheraven    }
4569227825Stheraven}
4570227825Stheraven
4571227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4572227825Stheravenvoid
4573227825Stheraven__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4574227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2,
4575227825Stheraven        _OutputIterator __result, _Compare __comp)
4576227825Stheraven{
4577227825Stheraven    for (; __first1 != __last1; ++__result)
4578227825Stheraven    {
4579227825Stheraven        if (__first2 == __last2)
4580227825Stheraven        {
4581227825Stheraven            for (; __first1 != __last1; ++__first1, ++__result)
4582227825Stheraven                *__result = _VSTD::move(*__first1);
4583227825Stheraven            return;
4584227825Stheraven        }
4585227825Stheraven        if (__comp(*__first2, *__first1))
4586227825Stheraven        {
4587227825Stheraven            *__result = _VSTD::move(*__first2);
4588227825Stheraven            ++__first2;
4589227825Stheraven        }
4590227825Stheraven        else
4591227825Stheraven        {
4592227825Stheraven            *__result = _VSTD::move(*__first1);
4593227825Stheraven            ++__first1;
4594227825Stheraven        }
4595227825Stheraven    }
4596227825Stheraven    for (; __first2 != __last2; ++__first2, ++__result)
4597227825Stheraven        *__result = _VSTD::move(*__first2);
4598227825Stheraven}
4599227825Stheraven
4600227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4601227825Stheravenvoid
4602227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4603227825Stheraven              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4604227825Stheraven              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4605227825Stheraven
4606227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4607227825Stheravenvoid
4608227825Stheraven__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4609227825Stheraven                   typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4610227825Stheraven                   typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4611227825Stheraven{
4612227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4613227825Stheraven    switch (__len)
4614227825Stheraven    {
4615227825Stheraven    case 0:
4616227825Stheraven        return;
4617227825Stheraven    case 1:
4618227825Stheraven        ::new(__first2) value_type(_VSTD::move(*__first1));
4619227825Stheraven        return;
4620227825Stheraven    case 2:
4621227825Stheraven       __destruct_n __d(0);
4622227825Stheraven        unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4623227825Stheraven         if (__comp(*--__last1, *__first1))
4624227825Stheraven        {
4625227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__last1));
4626227825Stheraven            __d.__incr((value_type*)0);
4627227825Stheraven            ++__first2;
4628227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__first1));
4629227825Stheraven        }
4630227825Stheraven        else
4631227825Stheraven        {
4632227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__first1));
4633227825Stheraven            __d.__incr((value_type*)0);
4634227825Stheraven            ++__first2;
4635227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__last1));
4636227825Stheraven        }
4637227825Stheraven        __h2.release();
4638227825Stheraven        return;
4639227825Stheraven    }
4640227825Stheraven    if (__len <= 8)
4641227825Stheraven    {
4642227825Stheraven        __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4643227825Stheraven        return;
4644227825Stheraven    }
4645227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4646227825Stheraven    _RandomAccessIterator __m = __first1 + __l2;
4647227825Stheraven    __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4648227825Stheraven    __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4649227825Stheraven    __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4650227825Stheraven}
4651227825Stheraven
4652227825Stheraventemplate <class _Tp>
4653227825Stheravenstruct __stable_sort_switch
4654227825Stheraven{
4655227825Stheraven    static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4656227825Stheraven};
4657227825Stheraven
4658227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4659227825Stheravenvoid
4660227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4661227825Stheraven              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4662227825Stheraven              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4663227825Stheraven{
4664227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4665227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4666227825Stheraven    switch (__len)
4667227825Stheraven    {
4668227825Stheraven    case 0:
4669227825Stheraven    case 1:
4670227825Stheraven        return;
4671227825Stheraven    case 2:
4672227825Stheraven        if (__comp(*--__last, *__first))
4673227825Stheraven            swap(*__first, *__last);
4674227825Stheraven        return;
4675227825Stheraven    }
4676227825Stheraven    if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4677227825Stheraven    {
4678227825Stheraven        __insertion_sort<_Compare>(__first, __last, __comp);
4679227825Stheraven        return;
4680227825Stheraven    }
4681227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4682227825Stheraven    _RandomAccessIterator __m = __first + __l2;
4683227825Stheraven    if (__len <= __buff_size)
4684227825Stheraven    {
4685227825Stheraven        __destruct_n __d(0);
4686227825Stheraven        unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4687227825Stheraven        __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4688227825Stheraven        __d.__set(__l2, (value_type*)0);
4689227825Stheraven        __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4690227825Stheraven        __d.__set(__len, (value_type*)0);
4691227825Stheraven        __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4692227825Stheraven//         __merge<_Compare>(move_iterator<value_type*>(__buff),
4693227825Stheraven//                           move_iterator<value_type*>(__buff + __l2),
4694227825Stheraven//                           move_iterator<_RandomAccessIterator>(__buff + __l2),
4695227825Stheraven//                           move_iterator<_RandomAccessIterator>(__buff + __len),
4696227825Stheraven//                           __first, __comp);
4697227825Stheraven        return;
4698227825Stheraven    }
4699227825Stheraven    __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4700227825Stheraven    __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4701227825Stheraven    __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4702227825Stheraven}
4703227825Stheraven
4704227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4705227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4706227825Stheravenvoid
4707227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4708227825Stheraven{
4709227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4710227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4711227825Stheraven    difference_type __len = __last - __first;
4712227825Stheraven    pair<value_type*, ptrdiff_t> __buf(0, 0);
4713227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
4714227825Stheraven    if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4715227825Stheraven    {
4716227825Stheraven        __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4717227825Stheraven        __h.reset(__buf.first);
4718227825Stheraven    }
4719262801Sdim#ifdef _LIBCPP_DEBUG
4720227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4721227825Stheraven    __debug_less<_Compare> __c(__comp);
4722227825Stheraven    __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4723262801Sdim#else  // _LIBCPP_DEBUG
4724227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4725227825Stheraven    __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4726262801Sdim#endif  // _LIBCPP_DEBUG
4727227825Stheraven}
4728227825Stheraven
4729227825Stheraventemplate <class _RandomAccessIterator>
4730227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4731227825Stheravenvoid
4732227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4733227825Stheraven{
4734227825Stheraven    _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4735227825Stheraven}
4736227825Stheraven
4737227825Stheraven// is_heap_until
4738227825Stheraven
4739227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4740227825Stheraven_RandomAccessIterator
4741227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4742227825Stheraven{
4743227825Stheraven    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4744227825Stheraven    difference_type __len = __last - __first;
4745227825Stheraven    difference_type __p = 0;
4746227825Stheraven    difference_type __c = 1;
4747227825Stheraven    _RandomAccessIterator __pp = __first;
4748227825Stheraven    while (__c < __len)
4749227825Stheraven    {
4750227825Stheraven        _RandomAccessIterator __cp = __first + __c;
4751227825Stheraven        if (__comp(*__pp, *__cp))
4752227825Stheraven            return __cp;
4753227825Stheraven        ++__c;
4754227825Stheraven        ++__cp;
4755227825Stheraven        if (__c == __len)
4756227825Stheraven            return __last;
4757227825Stheraven        if (__comp(*__pp, *__cp))
4758227825Stheraven            return __cp;
4759227825Stheraven        ++__p;
4760227825Stheraven        ++__pp;
4761227825Stheraven        __c = 2 * __p + 1;
4762227825Stheraven    }
4763227825Stheraven    return __last;
4764227825Stheraven}
4765227825Stheraven
4766227825Stheraventemplate<class _RandomAccessIterator>
4767227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4768227825Stheraven_RandomAccessIterator
4769227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4770227825Stheraven{
4771227825Stheraven    return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4772227825Stheraven}
4773227825Stheraven
4774227825Stheraven// is_heap
4775227825Stheraven
4776227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4777227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4778227825Stheravenbool
4779227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4780227825Stheraven{
4781227825Stheraven    return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4782227825Stheraven}
4783227825Stheraven
4784227825Stheraventemplate<class _RandomAccessIterator>
4785227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4786227825Stheravenbool
4787227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4788227825Stheraven{
4789227825Stheraven    return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4790227825Stheraven}
4791227825Stheraven
4792227825Stheraven// push_heap
4793227825Stheraven
4794227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4795227825Stheravenvoid
4796278724Sdim__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4797278724Sdim          typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4798227825Stheraven{
4799227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4800227825Stheraven    if (__len > 1)
4801227825Stheraven    {
4802227825Stheraven        __len = (__len - 2) / 2;
4803227825Stheraven        _RandomAccessIterator __ptr = __first + __len;
4804227825Stheraven        if (__comp(*__ptr, *--__last))
4805227825Stheraven        {
4806227825Stheraven            value_type __t(_VSTD::move(*__last));
4807227825Stheraven            do
4808227825Stheraven            {
4809227825Stheraven                *__last = _VSTD::move(*__ptr);
4810227825Stheraven                __last = __ptr;
4811227825Stheraven                if (__len == 0)
4812227825Stheraven                    break;
4813227825Stheraven                __len = (__len - 1) / 2;
4814227825Stheraven                __ptr = __first + __len;
4815227825Stheraven            } while (__comp(*__ptr, __t));
4816227825Stheraven            *__last = _VSTD::move(__t);
4817227825Stheraven        }
4818227825Stheraven    }
4819227825Stheraven}
4820227825Stheraven
4821227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4822227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4823227825Stheravenvoid
4824227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4825227825Stheraven{
4826262801Sdim#ifdef _LIBCPP_DEBUG
4827227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4828227825Stheraven    __debug_less<_Compare> __c(__comp);
4829278724Sdim    __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
4830262801Sdim#else  // _LIBCPP_DEBUG
4831227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4832278724Sdim    __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
4833262801Sdim#endif  // _LIBCPP_DEBUG
4834227825Stheraven}
4835227825Stheraven
4836227825Stheraventemplate <class _RandomAccessIterator>
4837227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4838227825Stheravenvoid
4839227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4840227825Stheraven{
4841227825Stheraven    _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4842227825Stheraven}
4843227825Stheraven
4844227825Stheraven// pop_heap
4845227825Stheraven
4846227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4847278724Sdimvoid
4848278724Sdim__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4849278724Sdim            typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4850278724Sdim            _RandomAccessIterator __start)
4851278724Sdim{
4852278724Sdim    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4853278724Sdim    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4854278724Sdim    // left-child of __start is at 2 * __start + 1
4855278724Sdim    // right-child of __start is at 2 * __start + 2
4856278724Sdim    difference_type __child = __start - __first;
4857278724Sdim
4858278724Sdim    if (__len < 2 || (__len - 2) / 2 < __child)
4859278724Sdim        return;
4860278724Sdim
4861278724Sdim    __child = 2 * __child + 1;
4862278724Sdim    _RandomAccessIterator __child_i = __first + __child;
4863278724Sdim
4864278724Sdim    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4865278724Sdim        // right-child exists and is greater than left-child
4866278724Sdim        ++__child_i;
4867278724Sdim        ++__child;
4868278724Sdim    }
4869278724Sdim
4870278724Sdim    // check if we are in heap-order
4871278724Sdim    if (__comp(*__child_i, *__start))
4872278724Sdim        // we are, __start is larger than it's largest child
4873278724Sdim        return;
4874278724Sdim
4875278724Sdim    value_type __top(_VSTD::move(*__start));
4876278724Sdim    do
4877278724Sdim    {
4878278724Sdim        // we are not in heap-order, swap the parent with it's largest child
4879278724Sdim        *__start = _VSTD::move(*__child_i);
4880278724Sdim        __start = __child_i;
4881278724Sdim
4882278724Sdim        if ((__len - 2) / 2 < __child)
4883278724Sdim            break;
4884278724Sdim
4885278724Sdim        // recompute the child based off of the updated parent
4886278724Sdim        __child = 2 * __child + 1;
4887278724Sdim        __child_i = __first + __child;
4888278724Sdim
4889278724Sdim        if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4890278724Sdim            // right-child exists and is greater than left-child
4891278724Sdim            ++__child_i;
4892278724Sdim            ++__child;
4893278724Sdim        }
4894278724Sdim
4895278724Sdim        // check if we are in heap-order
4896278724Sdim    } while (!__comp(*__child_i, __top));
4897278724Sdim    *__start = _VSTD::move(__top);
4898278724Sdim}
4899278724Sdim
4900278724Sdimtemplate <class _Compare, class _RandomAccessIterator>
4901227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4902227825Stheravenvoid
4903227825Stheraven__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4904227825Stheraven           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4905227825Stheraven{
4906227825Stheraven    if (__len > 1)
4907227825Stheraven    {
4908227825Stheraven        swap(*__first, *--__last);
4909278724Sdim        __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
4910227825Stheraven    }
4911227825Stheraven}
4912227825Stheraven
4913227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4914227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4915227825Stheravenvoid
4916227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4917227825Stheraven{
4918262801Sdim#ifdef _LIBCPP_DEBUG
4919227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4920227825Stheraven    __debug_less<_Compare> __c(__comp);
4921227825Stheraven    __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4922262801Sdim#else  // _LIBCPP_DEBUG
4923227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4924227825Stheraven    __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4925262801Sdim#endif  // _LIBCPP_DEBUG
4926227825Stheraven}
4927227825Stheraven
4928227825Stheraventemplate <class _RandomAccessIterator>
4929227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4930227825Stheravenvoid
4931227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4932227825Stheraven{
4933227825Stheraven    _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4934227825Stheraven}
4935227825Stheraven
4936227825Stheraven// make_heap
4937227825Stheraven
4938227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4939227825Stheravenvoid
4940227825Stheraven__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4941227825Stheraven{
4942227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4943227825Stheraven    difference_type __n = __last - __first;
4944227825Stheraven    if (__n > 1)
4945227825Stheraven    {
4946278724Sdim        // start from the first parent, there is no need to consider children
4947278724Sdim        for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4948278724Sdim        {
4949278724Sdim            __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4950278724Sdim        }
4951227825Stheraven    }
4952227825Stheraven}
4953227825Stheraven
4954227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4955227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4956227825Stheravenvoid
4957227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4958227825Stheraven{
4959262801Sdim#ifdef _LIBCPP_DEBUG
4960227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4961227825Stheraven    __debug_less<_Compare> __c(__comp);
4962227825Stheraven    __make_heap<_Comp_ref>(__first, __last, __c);
4963262801Sdim#else  // _LIBCPP_DEBUG
4964227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4965227825Stheraven    __make_heap<_Comp_ref>(__first, __last, __comp);
4966262801Sdim#endif  // _LIBCPP_DEBUG
4967227825Stheraven}
4968227825Stheraven
4969227825Stheraventemplate <class _RandomAccessIterator>
4970227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4971227825Stheravenvoid
4972227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4973227825Stheraven{
4974227825Stheraven    _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4975227825Stheraven}
4976227825Stheraven
4977227825Stheraven// sort_heap
4978227825Stheraven
4979227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4980227825Stheravenvoid
4981227825Stheraven__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4982227825Stheraven{
4983227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4984227825Stheraven    for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4985227825Stheraven        __pop_heap<_Compare>(__first, __last, __comp, __n);
4986227825Stheraven}
4987227825Stheraven
4988227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4989227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4990227825Stheravenvoid
4991227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4992227825Stheraven{
4993262801Sdim#ifdef _LIBCPP_DEBUG
4994227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4995227825Stheraven    __debug_less<_Compare> __c(__comp);
4996227825Stheraven    __sort_heap<_Comp_ref>(__first, __last, __c);
4997262801Sdim#else  // _LIBCPP_DEBUG
4998227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4999227825Stheraven    __sort_heap<_Comp_ref>(__first, __last, __comp);
5000262801Sdim#endif  // _LIBCPP_DEBUG
5001227825Stheraven}
5002227825Stheraven
5003227825Stheraventemplate <class _RandomAccessIterator>
5004227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5005227825Stheravenvoid
5006227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5007227825Stheraven{
5008227825Stheraven    _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5009227825Stheraven}
5010227825Stheraven
5011227825Stheraven// partial_sort
5012227825Stheraven
5013227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
5014227825Stheravenvoid
5015227825Stheraven__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5016227825Stheraven             _Compare __comp)
5017227825Stheraven{
5018227825Stheraven    __make_heap<_Compare>(__first, __middle, __comp);
5019227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5020227825Stheraven    for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5021227825Stheraven    {
5022227825Stheraven        if (__comp(*__i, *__first))
5023227825Stheraven        {
5024227825Stheraven            swap(*__i, *__first);
5025278724Sdim            __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
5026227825Stheraven        }
5027227825Stheraven    }
5028227825Stheraven    __sort_heap<_Compare>(__first, __middle, __comp);
5029227825Stheraven}
5030227825Stheraven
5031227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
5032227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5033227825Stheravenvoid
5034227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5035227825Stheraven             _Compare __comp)
5036227825Stheraven{
5037262801Sdim#ifdef _LIBCPP_DEBUG
5038227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5039227825Stheraven    __debug_less<_Compare> __c(__comp);
5040227825Stheraven    __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
5041262801Sdim#else  // _LIBCPP_DEBUG
5042227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5043227825Stheraven    __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
5044262801Sdim#endif  // _LIBCPP_DEBUG
5045227825Stheraven}
5046227825Stheraven
5047227825Stheraventemplate <class _RandomAccessIterator>
5048227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5049227825Stheravenvoid
5050227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5051227825Stheraven{
5052227825Stheraven    _VSTD::partial_sort(__first, __middle, __last,
5053227825Stheraven                       __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5054227825Stheraven}
5055227825Stheraven
5056227825Stheraven// partial_sort_copy
5057227825Stheraven
5058227825Stheraventemplate <class _Compare, class _InputIterator, class _RandomAccessIterator>
5059227825Stheraven_RandomAccessIterator
5060227825Stheraven__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5061227825Stheraven                    _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5062227825Stheraven{
5063227825Stheraven    _RandomAccessIterator __r = __result_first;
5064227825Stheraven    if (__r != __result_last)
5065227825Stheraven    {
5066278724Sdim        for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5067227825Stheraven            *__r = *__first;
5068227825Stheraven        __make_heap<_Compare>(__result_first, __r, __comp);
5069278724Sdim        typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
5070227825Stheraven        for (; __first != __last; ++__first)
5071227825Stheraven            if (__comp(*__first, *__result_first))
5072227825Stheraven            {
5073227825Stheraven                *__result_first = *__first;
5074278724Sdim                __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
5075227825Stheraven            }
5076227825Stheraven        __sort_heap<_Compare>(__result_first, __r, __comp);
5077227825Stheraven    }
5078227825Stheraven    return __r;
5079227825Stheraven}
5080227825Stheraven
5081227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator, class _Compare>
5082227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5083227825Stheraven_RandomAccessIterator
5084227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last,
5085227825Stheraven                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5086227825Stheraven{
5087262801Sdim#ifdef _LIBCPP_DEBUG
5088227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5089227825Stheraven    __debug_less<_Compare> __c(__comp);
5090227825Stheraven    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
5091262801Sdim#else  // _LIBCPP_DEBUG
5092227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5093227825Stheraven    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
5094262801Sdim#endif  // _LIBCPP_DEBUG
5095227825Stheraven}
5096227825Stheraven
5097227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator>
5098227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5099227825Stheraven_RandomAccessIterator
5100227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last,
5101227825Stheraven                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5102227825Stheraven{
5103227825Stheraven    return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
5104227825Stheraven                                   __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5105227825Stheraven}
5106227825Stheraven
5107227825Stheraven// nth_element
5108227825Stheraven
5109227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
5110227825Stheravenvoid
5111227825Stheraven__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5112227825Stheraven{
5113227825Stheraven    // _Compare is known to be a reference type
5114227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5115227825Stheraven    const difference_type __limit = 7;
5116227825Stheraven    while (true)
5117227825Stheraven    {
5118227825Stheraven    __restart:
5119232950Stheraven        if (__nth == __last)
5120232950Stheraven            return;
5121227825Stheraven        difference_type __len = __last - __first;
5122227825Stheraven        switch (__len)
5123227825Stheraven        {
5124227825Stheraven        case 0:
5125227825Stheraven        case 1:
5126227825Stheraven            return;
5127227825Stheraven        case 2:
5128227825Stheraven            if (__comp(*--__last, *__first))
5129227825Stheraven                swap(*__first, *__last);
5130227825Stheraven            return;
5131227825Stheraven        case 3:
5132227825Stheraven            {
5133227825Stheraven            _RandomAccessIterator __m = __first;
5134227825Stheraven            _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
5135227825Stheraven            return;
5136227825Stheraven            }
5137227825Stheraven        }
5138227825Stheraven        if (__len <= __limit)
5139227825Stheraven        {
5140227825Stheraven            __selection_sort<_Compare>(__first, __last, __comp);
5141227825Stheraven            return;
5142227825Stheraven        }
5143227825Stheraven        // __len > __limit >= 3
5144227825Stheraven        _RandomAccessIterator __m = __first + __len/2;
5145227825Stheraven        _RandomAccessIterator __lm1 = __last;
5146227825Stheraven        unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
5147227825Stheraven        // *__m is median
5148227825Stheraven        // partition [__first, __m) < *__m and *__m <= [__m, __last)
5149227825Stheraven        // (this inhibits tossing elements equivalent to __m around unnecessarily)
5150227825Stheraven        _RandomAccessIterator __i = __first;
5151227825Stheraven        _RandomAccessIterator __j = __lm1;
5152227825Stheraven        // j points beyond range to be tested, *__lm1 is known to be <= *__m
5153227825Stheraven        // The search going up is known to be guarded but the search coming down isn't.
5154227825Stheraven        // Prime the downward search with a guard.
5155227825Stheraven        if (!__comp(*__i, *__m))  // if *__first == *__m
5156227825Stheraven        {
5157227825Stheraven            // *__first == *__m, *__first doesn't go in first part
5158227825Stheraven            // manually guard downward moving __j against __i
5159227825Stheraven            while (true)
5160227825Stheraven            {
5161227825Stheraven                if (__i == --__j)
5162227825Stheraven                {
5163227825Stheraven                    // *__first == *__m, *__m <= all other elements
5164227825Stheraven                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5165227825Stheraven                    ++__i;  // __first + 1
5166227825Stheraven                    __j = __last;
5167227825Stheraven                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
5168227825Stheraven                    {
5169227825Stheraven                        while (true)
5170227825Stheraven                        {
5171227825Stheraven                            if (__i == __j)
5172227825Stheraven                                return;  // [__first, __last) all equivalent elements
5173227825Stheraven                            if (__comp(*__first, *__i))
5174227825Stheraven                            {
5175227825Stheraven                                swap(*__i, *__j);
5176227825Stheraven                                ++__n_swaps;
5177227825Stheraven                                ++__i;
5178227825Stheraven                                break;
5179227825Stheraven                            }
5180227825Stheraven                            ++__i;
5181227825Stheraven                        }
5182227825Stheraven                    }
5183227825Stheraven                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5184227825Stheraven                    if (__i == __j)
5185227825Stheraven                        return;
5186227825Stheraven                    while (true)
5187227825Stheraven                    {
5188227825Stheraven                        while (!__comp(*__first, *__i))
5189227825Stheraven                            ++__i;
5190227825Stheraven                        while (__comp(*__first, *--__j))
5191227825Stheraven                            ;
5192227825Stheraven                        if (__i >= __j)
5193227825Stheraven                            break;
5194227825Stheraven                        swap(*__i, *__j);
5195227825Stheraven                        ++__n_swaps;
5196227825Stheraven                        ++__i;
5197227825Stheraven                    }
5198227825Stheraven                    // [__first, __i) == *__first and *__first < [__i, __last)
5199227825Stheraven                    // The first part is sorted,
5200227825Stheraven                    if (__nth < __i)
5201227825Stheraven                        return;
5202227825Stheraven                    // __nth_element the secod part
5203227825Stheraven                    // __nth_element<_Compare>(__i, __nth, __last, __comp);
5204227825Stheraven                    __first = __i;
5205227825Stheraven                    goto __restart;
5206227825Stheraven                }
5207227825Stheraven                if (__comp(*__j, *__m))
5208227825Stheraven                {
5209227825Stheraven                    swap(*__i, *__j);
5210227825Stheraven                    ++__n_swaps;
5211227825Stheraven                    break;  // found guard for downward moving __j, now use unguarded partition
5212227825Stheraven                }
5213227825Stheraven            }
5214227825Stheraven        }
5215227825Stheraven        ++__i;
5216227825Stheraven        // j points beyond range to be tested, *__lm1 is known to be <= *__m
5217227825Stheraven        // if not yet partitioned...
5218227825Stheraven        if (__i < __j)
5219227825Stheraven        {
5220227825Stheraven            // known that *(__i - 1) < *__m
5221227825Stheraven            while (true)
5222227825Stheraven            {
5223227825Stheraven                // __m still guards upward moving __i
5224227825Stheraven                while (__comp(*__i, *__m))
5225227825Stheraven                    ++__i;
5226227825Stheraven                // It is now known that a guard exists for downward moving __j
5227227825Stheraven                while (!__comp(*--__j, *__m))
5228227825Stheraven                    ;
5229227825Stheraven                if (__i >= __j)
5230227825Stheraven                    break;
5231227825Stheraven                swap(*__i, *__j);
5232227825Stheraven                ++__n_swaps;
5233227825Stheraven                // It is known that __m != __j
5234227825Stheraven                // If __m just moved, follow it
5235227825Stheraven                if (__m == __i)
5236227825Stheraven                    __m = __j;
5237227825Stheraven                ++__i;
5238227825Stheraven            }
5239227825Stheraven        }
5240227825Stheraven        // [__first, __i) < *__m and *__m <= [__i, __last)
5241227825Stheraven        if (__i != __m && __comp(*__m, *__i))
5242227825Stheraven        {
5243227825Stheraven            swap(*__i, *__m);
5244227825Stheraven            ++__n_swaps;
5245227825Stheraven        }
5246227825Stheraven        // [__first, __i) < *__i and *__i <= [__i+1, __last)
5247227825Stheraven        if (__nth == __i)
5248227825Stheraven            return;
5249227825Stheraven        if (__n_swaps == 0)
5250227825Stheraven        {
5251227825Stheraven            // We were given a perfectly partitioned sequence.  Coincidence?
5252227825Stheraven            if (__nth < __i)
5253227825Stheraven            {
5254227825Stheraven                // Check for [__first, __i) already sorted
5255227825Stheraven                __j = __m = __first;
5256227825Stheraven                while (++__j != __i)
5257227825Stheraven                {
5258227825Stheraven                    if (__comp(*__j, *__m))
5259227825Stheraven                        // not yet sorted, so sort
5260227825Stheraven                        goto not_sorted;
5261227825Stheraven                    __m = __j;
5262227825Stheraven                }
5263227825Stheraven                // [__first, __i) sorted
5264227825Stheraven                return;
5265227825Stheraven            }
5266227825Stheraven            else
5267227825Stheraven            {
5268227825Stheraven                // Check for [__i, __last) already sorted
5269227825Stheraven                __j = __m = __i;
5270227825Stheraven                while (++__j != __last)
5271227825Stheraven                {
5272227825Stheraven                    if (__comp(*__j, *__m))
5273227825Stheraven                        // not yet sorted, so sort
5274227825Stheraven                        goto not_sorted;
5275227825Stheraven                    __m = __j;
5276227825Stheraven                }
5277227825Stheraven                // [__i, __last) sorted
5278227825Stheraven                return;
5279227825Stheraven            }
5280227825Stheraven        }
5281227825Stheravennot_sorted:
5282227825Stheraven        // __nth_element on range containing __nth
5283227825Stheraven        if (__nth < __i)
5284227825Stheraven        {
5285227825Stheraven            // __nth_element<_Compare>(__first, __nth, __i, __comp);
5286227825Stheraven            __last = __i;
5287227825Stheraven        }
5288227825Stheraven        else
5289227825Stheraven        {
5290227825Stheraven            // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5291227825Stheraven            __first = ++__i;
5292227825Stheraven        }
5293227825Stheraven    }
5294227825Stheraven}
5295227825Stheraven
5296227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
5297227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5298227825Stheravenvoid
5299227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5300227825Stheraven{
5301262801Sdim#ifdef _LIBCPP_DEBUG
5302227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5303227825Stheraven    __debug_less<_Compare> __c(__comp);
5304227825Stheraven    __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5305262801Sdim#else  // _LIBCPP_DEBUG
5306227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5307227825Stheraven    __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5308262801Sdim#endif  // _LIBCPP_DEBUG
5309227825Stheraven}
5310227825Stheraven
5311227825Stheraventemplate <class _RandomAccessIterator>
5312227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5313227825Stheravenvoid
5314227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5315227825Stheraven{
5316227825Stheraven    _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5317227825Stheraven}
5318227825Stheraven
5319227825Stheraven// includes
5320227825Stheraven
5321227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
5322227825Stheravenbool
5323227825Stheraven__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5324227825Stheraven           _Compare __comp)
5325227825Stheraven{
5326227825Stheraven    for (; __first2 != __last2; ++__first1)
5327227825Stheraven    {
5328227825Stheraven        if (__first1 == __last1 || __comp(*__first2, *__first1))
5329227825Stheraven            return false;
5330227825Stheraven        if (!__comp(*__first1, *__first2))
5331227825Stheraven            ++__first2;
5332227825Stheraven    }
5333227825Stheraven    return true;
5334227825Stheraven}
5335227825Stheraven
5336227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare>
5337227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5338227825Stheravenbool
5339227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5340227825Stheraven         _Compare __comp)
5341227825Stheraven{
5342262801Sdim#ifdef _LIBCPP_DEBUG
5343227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5344227825Stheraven    __debug_less<_Compare> __c(__comp);
5345227825Stheraven    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5346262801Sdim#else  // _LIBCPP_DEBUG
5347227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5348227825Stheraven    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5349262801Sdim#endif  // _LIBCPP_DEBUG
5350227825Stheraven}
5351227825Stheraven
5352227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
5353227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5354227825Stheravenbool
5355227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5356227825Stheraven{
5357227825Stheraven    return _VSTD::includes(__first1, __last1, __first2, __last2,
5358227825Stheraven                          __less<typename iterator_traits<_InputIterator1>::value_type,
5359227825Stheraven                                 typename iterator_traits<_InputIterator2>::value_type>());
5360227825Stheraven}
5361227825Stheraven
5362227825Stheraven// set_union
5363227825Stheraven
5364227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5365227825Stheraven_OutputIterator
5366227825Stheraven__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5367227825Stheraven            _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5368227825Stheraven{
5369227825Stheraven    for (; __first1 != __last1; ++__result)
5370227825Stheraven    {
5371227825Stheraven        if (__first2 == __last2)
5372227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5373227825Stheraven        if (__comp(*__first2, *__first1))
5374227825Stheraven        {
5375227825Stheraven            *__result = *__first2;
5376227825Stheraven            ++__first2;
5377227825Stheraven        }
5378227825Stheraven        else
5379227825Stheraven        {
5380227825Stheraven            *__result = *__first1;
5381227825Stheraven            if (!__comp(*__first1, *__first2))
5382227825Stheraven                ++__first2;
5383227825Stheraven            ++__first1;
5384227825Stheraven        }
5385227825Stheraven    }
5386227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
5387227825Stheraven}
5388227825Stheraven
5389227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5390227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5391227825Stheraven_OutputIterator
5392227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1,
5393227825Stheraven          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5394227825Stheraven{
5395262801Sdim#ifdef _LIBCPP_DEBUG
5396227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5397227825Stheraven    __debug_less<_Compare> __c(__comp);
5398227825Stheraven    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5399262801Sdim#else  // _LIBCPP_DEBUG
5400227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5401227825Stheraven    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5402262801Sdim#endif  // _LIBCPP_DEBUG
5403227825Stheraven}
5404227825Stheraven
5405227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5406227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5407227825Stheraven_OutputIterator
5408227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1,
5409227825Stheraven          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5410227825Stheraven{
5411227825Stheraven    return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5412227825Stheraven                          __less<typename iterator_traits<_InputIterator1>::value_type,
5413227825Stheraven                                 typename iterator_traits<_InputIterator2>::value_type>());
5414227825Stheraven}
5415227825Stheraven
5416227825Stheraven// set_intersection
5417227825Stheraven
5418227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5419227825Stheraven_OutputIterator
5420227825Stheraven__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5421227825Stheraven                   _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5422227825Stheraven{
5423227825Stheraven    while (__first1 != __last1 && __first2 != __last2)
5424227825Stheraven    {
5425227825Stheraven        if (__comp(*__first1, *__first2))
5426227825Stheraven            ++__first1;
5427227825Stheraven        else
5428227825Stheraven        {
5429227825Stheraven            if (!__comp(*__first2, *__first1))
5430227825Stheraven            {
5431227825Stheraven                *__result = *__first1;
5432227825Stheraven                ++__result;
5433227825Stheraven                ++__first1;
5434227825Stheraven            }
5435227825Stheraven            ++__first2;
5436227825Stheraven        }
5437227825Stheraven    }
5438227825Stheraven    return __result;
5439227825Stheraven}
5440227825Stheraven
5441227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5442227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5443227825Stheraven_OutputIterator
5444227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5445227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5446227825Stheraven{
5447262801Sdim#ifdef _LIBCPP_DEBUG
5448227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5449227825Stheraven    __debug_less<_Compare> __c(__comp);
5450227825Stheraven    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5451262801Sdim#else  // _LIBCPP_DEBUG
5452227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5453227825Stheraven    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5454262801Sdim#endif  // _LIBCPP_DEBUG
5455227825Stheraven}
5456227825Stheraven
5457227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5458227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5459227825Stheraven_OutputIterator
5460227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5461227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5462227825Stheraven{
5463227825Stheraven    return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5464227825Stheraven                                  __less<typename iterator_traits<_InputIterator1>::value_type,
5465227825Stheraven                                         typename iterator_traits<_InputIterator2>::value_type>());
5466227825Stheraven}
5467227825Stheraven
5468227825Stheraven// set_difference
5469227825Stheraven
5470227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5471227825Stheraven_OutputIterator
5472227825Stheraven__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5473227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5474227825Stheraven{
5475227825Stheraven    while (__first1 != __last1)
5476227825Stheraven    {
5477227825Stheraven        if (__first2 == __last2)
5478227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5479227825Stheraven        if (__comp(*__first1, *__first2))
5480227825Stheraven        {
5481227825Stheraven            *__result = *__first1;
5482227825Stheraven            ++__result;
5483227825Stheraven            ++__first1;
5484227825Stheraven        }
5485227825Stheraven        else
5486227825Stheraven        {
5487227825Stheraven            if (!__comp(*__first2, *__first1))
5488227825Stheraven                ++__first1;
5489227825Stheraven            ++__first2;
5490227825Stheraven        }
5491227825Stheraven    }
5492227825Stheraven    return __result;
5493227825Stheraven}
5494227825Stheraven
5495227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5496227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5497227825Stheraven_OutputIterator
5498227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5499227825Stheraven               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5500227825Stheraven{
5501262801Sdim#ifdef _LIBCPP_DEBUG
5502227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5503227825Stheraven    __debug_less<_Compare> __c(__comp);
5504227825Stheraven    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5505262801Sdim#else  // _LIBCPP_DEBUG
5506227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5507227825Stheraven    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5508262801Sdim#endif  // _LIBCPP_DEBUG
5509227825Stheraven}
5510227825Stheraven
5511227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5512227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5513227825Stheraven_OutputIterator
5514227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5515227825Stheraven               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5516227825Stheraven{
5517227825Stheraven    return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5518227825Stheraven                                __less<typename iterator_traits<_InputIterator1>::value_type,
5519227825Stheraven                                       typename iterator_traits<_InputIterator2>::value_type>());
5520227825Stheraven}
5521227825Stheraven
5522227825Stheraven// set_symmetric_difference
5523227825Stheraven
5524227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5525227825Stheraven_OutputIterator
5526227825Stheraven__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5527227825Stheraven                           _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5528227825Stheraven{
5529227825Stheraven    while (__first1 != __last1)
5530227825Stheraven    {
5531227825Stheraven        if (__first2 == __last2)
5532227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5533227825Stheraven        if (__comp(*__first1, *__first2))
5534227825Stheraven        {
5535227825Stheraven            *__result = *__first1;
5536227825Stheraven            ++__result;
5537227825Stheraven            ++__first1;
5538227825Stheraven        }
5539227825Stheraven        else
5540227825Stheraven        {
5541227825Stheraven            if (__comp(*__first2, *__first1))
5542227825Stheraven            {
5543227825Stheraven                *__result = *__first2;
5544227825Stheraven                ++__result;
5545227825Stheraven            }
5546227825Stheraven            else
5547227825Stheraven                ++__first1;
5548227825Stheraven            ++__first2;
5549227825Stheraven        }
5550227825Stheraven    }
5551227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
5552227825Stheraven}
5553227825Stheraven
5554227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5555227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5556227825Stheraven_OutputIterator
5557227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5558227825Stheraven                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5559227825Stheraven{
5560262801Sdim#ifdef _LIBCPP_DEBUG
5561227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5562227825Stheraven    __debug_less<_Compare> __c(__comp);
5563227825Stheraven    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5564262801Sdim#else  // _LIBCPP_DEBUG
5565227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5566227825Stheraven    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5567262801Sdim#endif  // _LIBCPP_DEBUG
5568227825Stheraven}
5569227825Stheraven
5570227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5571227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5572227825Stheraven_OutputIterator
5573227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5574227825Stheraven                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5575227825Stheraven{
5576227825Stheraven    return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5577227825Stheraven                                          __less<typename iterator_traits<_InputIterator1>::value_type,
5578227825Stheraven                                                 typename iterator_traits<_InputIterator2>::value_type>());
5579227825Stheraven}
5580227825Stheraven
5581227825Stheraven// lexicographical_compare
5582227825Stheraven
5583227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
5584227825Stheravenbool
5585227825Stheraven__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5586227825Stheraven                          _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5587227825Stheraven{
5588278724Sdim    for (; __first2 != __last2; ++__first1, (void) ++__first2)
5589227825Stheraven    {
5590227825Stheraven        if (__first1 == __last1 || __comp(*__first1, *__first2))
5591227825Stheraven            return true;
5592227825Stheraven        if (__comp(*__first2, *__first1))
5593227825Stheraven            return false;
5594227825Stheraven    }
5595227825Stheraven    return false;
5596227825Stheraven}
5597227825Stheraven
5598227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare>
5599227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5600227825Stheravenbool
5601227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5602227825Stheraven                        _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5603227825Stheraven{
5604262801Sdim#ifdef _LIBCPP_DEBUG
5605227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5606227825Stheraven    __debug_less<_Compare> __c(__comp);
5607227825Stheraven    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5608262801Sdim#else  // _LIBCPP_DEBUG
5609227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5610227825Stheraven    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5611262801Sdim#endif  // _LIBCPP_DEBUG
5612227825Stheraven}
5613227825Stheraven
5614227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
5615227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5616227825Stheravenbool
5617227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5618227825Stheraven                        _InputIterator2 __first2, _InputIterator2 __last2)
5619227825Stheraven{
5620227825Stheraven    return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5621227825Stheraven                                         __less<typename iterator_traits<_InputIterator1>::value_type,
5622227825Stheraven                                                typename iterator_traits<_InputIterator2>::value_type>());
5623227825Stheraven}
5624227825Stheraven
5625227825Stheraven// next_permutation
5626227825Stheraven
5627227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
5628227825Stheravenbool
5629227825Stheraven__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5630227825Stheraven{
5631227825Stheraven    _BidirectionalIterator __i = __last;
5632227825Stheraven    if (__first == __last || __first == --__i)
5633227825Stheraven        return false;
5634227825Stheraven    while (true)
5635227825Stheraven    {
5636227825Stheraven        _BidirectionalIterator __ip1 = __i;
5637227825Stheraven        if (__comp(*--__i, *__ip1))
5638227825Stheraven        {
5639227825Stheraven            _BidirectionalIterator __j = __last;
5640227825Stheraven            while (!__comp(*__i, *--__j))
5641227825Stheraven                ;
5642227825Stheraven            swap(*__i, *__j);
5643227825Stheraven            _VSTD::reverse(__ip1, __last);
5644227825Stheraven            return true;
5645227825Stheraven        }
5646227825Stheraven        if (__i == __first)
5647227825Stheraven        {
5648227825Stheraven            _VSTD::reverse(__first, __last);
5649227825Stheraven            return false;
5650227825Stheraven        }
5651227825Stheraven    }
5652227825Stheraven}
5653227825Stheraven
5654227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
5655227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5656227825Stheravenbool
5657227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5658227825Stheraven{
5659262801Sdim#ifdef _LIBCPP_DEBUG
5660227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5661227825Stheraven    __debug_less<_Compare> __c(__comp);
5662227825Stheraven    return __next_permutation<_Comp_ref>(__first, __last, __c);
5663262801Sdim#else  // _LIBCPP_DEBUG
5664227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5665227825Stheraven    return __next_permutation<_Comp_ref>(__first, __last, __comp);
5666262801Sdim#endif  // _LIBCPP_DEBUG
5667227825Stheraven}
5668227825Stheraven
5669227825Stheraventemplate <class _BidirectionalIterator>
5670227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5671227825Stheravenbool
5672227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5673227825Stheraven{
5674227825Stheraven    return _VSTD::next_permutation(__first, __last,
5675227825Stheraven                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5676227825Stheraven}
5677227825Stheraven
5678227825Stheraven// prev_permutation
5679227825Stheraven
5680227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
5681227825Stheravenbool
5682227825Stheraven__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5683227825Stheraven{
5684227825Stheraven    _BidirectionalIterator __i = __last;
5685227825Stheraven    if (__first == __last || __first == --__i)
5686227825Stheraven        return false;
5687227825Stheraven    while (true)
5688227825Stheraven    {
5689227825Stheraven        _BidirectionalIterator __ip1 = __i;
5690227825Stheraven        if (__comp(*__ip1, *--__i))
5691227825Stheraven        {
5692227825Stheraven            _BidirectionalIterator __j = __last;
5693227825Stheraven            while (!__comp(*--__j, *__i))
5694227825Stheraven                ;
5695227825Stheraven            swap(*__i, *__j);
5696227825Stheraven            _VSTD::reverse(__ip1, __last);
5697227825Stheraven            return true;
5698227825Stheraven        }
5699227825Stheraven        if (__i == __first)
5700227825Stheraven        {
5701227825Stheraven            _VSTD::reverse(__first, __last);
5702227825Stheraven            return false;
5703227825Stheraven        }
5704227825Stheraven    }
5705227825Stheraven}
5706227825Stheraven
5707227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
5708227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5709227825Stheravenbool
5710227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5711227825Stheraven{
5712262801Sdim#ifdef _LIBCPP_DEBUG
5713227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5714227825Stheraven    __debug_less<_Compare> __c(__comp);
5715227825Stheraven    return __prev_permutation<_Comp_ref>(__first, __last, __c);
5716262801Sdim#else  // _LIBCPP_DEBUG
5717227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5718227825Stheraven    return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5719262801Sdim#endif  // _LIBCPP_DEBUG
5720227825Stheraven}
5721227825Stheraven
5722227825Stheraventemplate <class _BidirectionalIterator>
5723227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5724227825Stheravenbool
5725227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5726227825Stheraven{
5727227825Stheraven    return _VSTD::prev_permutation(__first, __last,
5728227825Stheraven                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5729227825Stheraven}
5730227825Stheraven
5731227825Stheraventemplate <class _Tp>
5732227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5733227825Stheraventypename enable_if
5734227825Stheraven<
5735227825Stheraven    is_integral<_Tp>::value,
5736227825Stheraven    _Tp
5737227825Stheraven>::type
5738227825Stheraven__rotate_left(_Tp __t, _Tp __n = 1)
5739227825Stheraven{
5740227825Stheraven    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5741227825Stheraven    __n &= __bits;
5742227825Stheraven    return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5743227825Stheraven}
5744227825Stheraven
5745227825Stheraventemplate <class _Tp>
5746227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5747227825Stheraventypename enable_if
5748227825Stheraven<
5749227825Stheraven    is_integral<_Tp>::value,
5750227825Stheraven    _Tp
5751227825Stheraven>::type
5752227825Stheraven__rotate_right(_Tp __t, _Tp __n = 1)
5753227825Stheraven{
5754227825Stheraven    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5755227825Stheraven    __n &= __bits;
5756227825Stheraven    return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5757227825Stheraven}
5758227825Stheraven
5759227825Stheraven_LIBCPP_END_NAMESPACE_STD
5760227825Stheraven
5761227825Stheraven#endif  // _LIBCPP_ALGORITHM
5762