algorithm revision 246487
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
90227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
91227825Stheraven    pair<InputIterator1, InputIterator2>
92227825Stheraven    mismatch(InputIterator1 first1, InputIterator1 last1,
93227825Stheraven             InputIterator2 first2, BinaryPredicate pred);
94227825Stheraven
95227825Stheraventemplate <class InputIterator1, class InputIterator2>
96227825Stheraven    bool
97227825Stheraven    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
98227825Stheraven
99227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate>
100227825Stheraven    bool
101227825Stheraven    equal(InputIterator1 first1, InputIterator1 last1,
102227825Stheraven          InputIterator2 first2, BinaryPredicate pred);
103227825Stheraven
104227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2>
105227825Stheraven    bool
106227825Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
107227825Stheraven                   ForwardIterator2 first2);
108227825Stheraven
109227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
110227825Stheraven    bool
111227825Stheraven    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
112227825Stheraven                   ForwardIterator2 first2, BinaryPredicate pred);
113227825Stheraven
114227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
115227825Stheraven    ForwardIterator1
116227825Stheraven    search(ForwardIterator1 first1, ForwardIterator1 last1,
117227825Stheraven           ForwardIterator2 first2, ForwardIterator2 last2);
118227825Stheraven
119227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
120227825Stheraven    ForwardIterator1
121227825Stheraven    search(ForwardIterator1 first1, ForwardIterator1 last1,
122227825Stheraven           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
123227825Stheraven
124227825Stheraventemplate <class ForwardIterator, class Size, class T>
125227825Stheraven    ForwardIterator
126227825Stheraven    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
127227825Stheraven
128227825Stheraventemplate <class ForwardIterator, class Size, class T, class BinaryPredicate>
129227825Stheraven    ForwardIterator
130227825Stheraven    search_n(ForwardIterator first, ForwardIterator last,
131227825Stheraven             Size count, const T& value, BinaryPredicate pred);
132227825Stheraven
133227825Stheraventemplate <class InputIterator, class OutputIterator>
134227825Stheraven    OutputIterator
135227825Stheraven    copy(InputIterator first, InputIterator last, OutputIterator result);
136227825Stheraven
137227825Stheraventemplate<class InputIterator, class OutputIterator, class Predicate>
138227825Stheraven    OutputIterator
139227825Stheraven    copy_if(InputIterator first, InputIterator last,
140227825Stheraven            OutputIterator result, Predicate pred);
141227825Stheraven
142227825Stheraventemplate<class InputIterator, class Size, class OutputIterator>
143227825Stheraven    OutputIterator
144227825Stheraven    copy_n(InputIterator first, Size n, OutputIterator result);
145227825Stheraven
146227825Stheraventemplate <class BidirectionalIterator1, class BidirectionalIterator2>
147227825Stheraven    BidirectionalIterator2
148227825Stheraven    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
149227825Stheraven                  BidirectionalIterator2 result);
150227825Stheraven
151227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
152227825Stheraven    ForwardIterator2
153227825Stheraven    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
154227825Stheraven
155227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2>
156227825Stheraven    void
157227825Stheraven    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
158227825Stheraven
159227825Stheraventemplate <class InputIterator, class OutputIterator, class UnaryOperation>
160227825Stheraven    OutputIterator
161227825Stheraven    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
162227825Stheraven
163227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
164227825Stheraven    OutputIterator
165227825Stheraven    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
166227825Stheraven              OutputIterator result, BinaryOperation binary_op);
167227825Stheraven
168227825Stheraventemplate <class ForwardIterator, class T>
169227825Stheraven    void
170227825Stheraven    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
171227825Stheraven
172227825Stheraventemplate <class ForwardIterator, class Predicate, class T>
173227825Stheraven    void
174227825Stheraven    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
175227825Stheraven
176227825Stheraventemplate <class InputIterator, class OutputIterator, class T>
177227825Stheraven    OutputIterator
178227825Stheraven    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
179227825Stheraven                 const T& old_value, const T& new_value);
180227825Stheraven
181227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate, class T>
182227825Stheraven    OutputIterator
183227825Stheraven    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
184227825Stheraven
185227825Stheraventemplate <class ForwardIterator, class T>
186227825Stheraven    void
187227825Stheraven    fill(ForwardIterator first, ForwardIterator last, const T& value);
188227825Stheraven
189227825Stheraventemplate <class OutputIterator, class Size, class T>
190227825Stheraven    OutputIterator
191227825Stheraven    fill_n(OutputIterator first, Size n, const T& value);
192227825Stheraven
193227825Stheraventemplate <class ForwardIterator, class Generator>
194227825Stheraven    void
195227825Stheraven    generate(ForwardIterator first, ForwardIterator last, Generator gen);
196227825Stheraven
197227825Stheraventemplate <class OutputIterator, class Size, class Generator>
198227825Stheraven    OutputIterator
199227825Stheraven    generate_n(OutputIterator first, Size n, Generator gen);
200227825Stheraven
201227825Stheraventemplate <class ForwardIterator, class T>
202227825Stheraven    ForwardIterator
203227825Stheraven    remove(ForwardIterator first, ForwardIterator last, const T& value);
204227825Stheraven
205227825Stheraventemplate <class ForwardIterator, class Predicate>
206227825Stheraven    ForwardIterator
207227825Stheraven    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
208227825Stheraven
209227825Stheraventemplate <class InputIterator, class OutputIterator, class T>
210227825Stheraven    OutputIterator
211227825Stheraven    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
212227825Stheraven
213227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate>
214227825Stheraven    OutputIterator
215227825Stheraven    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
216227825Stheraven
217227825Stheraventemplate <class ForwardIterator>
218227825Stheraven    ForwardIterator
219227825Stheraven    unique(ForwardIterator first, ForwardIterator last);
220227825Stheraven
221227825Stheraventemplate <class ForwardIterator, class BinaryPredicate>
222227825Stheraven    ForwardIterator
223227825Stheraven    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
224227825Stheraven
225227825Stheraventemplate <class InputIterator, class OutputIterator>
226227825Stheraven    OutputIterator
227227825Stheraven    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
228227825Stheraven
229227825Stheraventemplate <class InputIterator, class OutputIterator, class BinaryPredicate>
230227825Stheraven    OutputIterator
231227825Stheraven    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
232227825Stheraven
233227825Stheraventemplate <class BidirectionalIterator>
234227825Stheraven    void
235227825Stheraven    reverse(BidirectionalIterator first, BidirectionalIterator last);
236227825Stheraven
237227825Stheraventemplate <class BidirectionalIterator, class OutputIterator>
238227825Stheraven    OutputIterator
239227825Stheraven    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
240227825Stheraven
241227825Stheraventemplate <class ForwardIterator>
242227825Stheraven    ForwardIterator
243227825Stheraven    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
244227825Stheraven
245227825Stheraventemplate <class ForwardIterator, class OutputIterator>
246227825Stheraven    OutputIterator
247227825Stheraven    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
248227825Stheraven
249227825Stheraventemplate <class RandomAccessIterator>
250227825Stheraven    void
251227825Stheraven    random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
252227825Stheraven
253227825Stheraventemplate <class RandomAccessIterator, class RandomNumberGenerator>
254227825Stheraven    void
255227825Stheraven    random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand);
256227825Stheraven
257227825Stheraventemplate<class RandomAccessIterator, class UniformRandomNumberGenerator>
258227825Stheraven    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
259227825Stheraven                 UniformRandomNumberGenerator&& g);
260227825Stheraven
261227825Stheraventemplate <class InputIterator, class Predicate>
262227825Stheraven    bool
263227825Stheraven    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
264227825Stheraven
265227825Stheraventemplate <class ForwardIterator, class Predicate>
266227825Stheraven    ForwardIterator
267227825Stheraven    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
268227825Stheraven
269227825Stheraventemplate <class InputIterator, class OutputIterator1,
270227825Stheraven          class OutputIterator2, class Predicate>
271227825Stheraven    pair<OutputIterator1, OutputIterator2>
272227825Stheraven    partition_copy(InputIterator first, InputIterator last,
273227825Stheraven                   OutputIterator1 out_true, OutputIterator2 out_false,
274227825Stheraven                   Predicate pred);
275227825Stheraven
276227825Stheraventemplate <class ForwardIterator, class Predicate>
277227825Stheraven    ForwardIterator
278227825Stheraven    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
279227825Stheraven
280227825Stheraventemplate<class ForwardIterator, class Predicate>
281227825Stheraven    ForwardIterator
282227825Stheraven    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
283227825Stheraven
284227825Stheraventemplate <class ForwardIterator>
285227825Stheraven    bool
286227825Stheraven    is_sorted(ForwardIterator first, ForwardIterator last);
287227825Stheraven
288227825Stheraventemplate <class ForwardIterator, class Compare>
289227825Stheraven    bool
290227825Stheraven    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
291227825Stheraven
292227825Stheraventemplate<class ForwardIterator>
293227825Stheraven    ForwardIterator
294227825Stheraven    is_sorted_until(ForwardIterator first, ForwardIterator last);
295227825Stheraven
296227825Stheraventemplate <class ForwardIterator, class Compare>
297227825Stheraven    ForwardIterator
298227825Stheraven    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
299227825Stheraven
300227825Stheraventemplate <class RandomAccessIterator>
301227825Stheraven    void
302227825Stheraven    sort(RandomAccessIterator first, RandomAccessIterator last);
303227825Stheraven
304227825Stheraventemplate <class RandomAccessIterator, class Compare>
305227825Stheraven    void
306227825Stheraven    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
307227825Stheraven
308227825Stheraventemplate <class RandomAccessIterator>
309227825Stheraven    void
310227825Stheraven    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
311227825Stheraven
312227825Stheraventemplate <class RandomAccessIterator, class Compare>
313227825Stheraven    void
314227825Stheraven    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
315227825Stheraven
316227825Stheraventemplate <class RandomAccessIterator>
317227825Stheraven    void
318227825Stheraven    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
319227825Stheraven
320227825Stheraventemplate <class RandomAccessIterator, class Compare>
321227825Stheraven    void
322227825Stheraven    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
323227825Stheraven
324227825Stheraventemplate <class InputIterator, class RandomAccessIterator>
325227825Stheraven    RandomAccessIterator
326227825Stheraven    partial_sort_copy(InputIterator first, InputIterator last,
327227825Stheraven                      RandomAccessIterator result_first, RandomAccessIterator result_last);
328227825Stheraven
329227825Stheraventemplate <class InputIterator, class RandomAccessIterator, class Compare>
330227825Stheraven    RandomAccessIterator
331227825Stheraven    partial_sort_copy(InputIterator first, InputIterator last,
332227825Stheraven                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
333227825Stheraven
334227825Stheraventemplate <class RandomAccessIterator>
335227825Stheraven    void
336227825Stheraven    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
337227825Stheraven
338227825Stheraventemplate <class RandomAccessIterator, class Compare>
339227825Stheraven    void
340227825Stheraven    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
341227825Stheraven
342227825Stheraventemplate <class ForwardIterator, class T>
343227825Stheraven    ForwardIterator
344227825Stheraven    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
345227825Stheraven
346227825Stheraventemplate <class ForwardIterator, class T, class Compare>
347227825Stheraven    ForwardIterator
348227825Stheraven    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
349227825Stheraven
350227825Stheraventemplate <class ForwardIterator, class T>
351227825Stheraven    ForwardIterator
352227825Stheraven    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
353227825Stheraven
354227825Stheraventemplate <class ForwardIterator, class T, class Compare>
355227825Stheraven    ForwardIterator
356227825Stheraven    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
357227825Stheraven
358227825Stheraventemplate <class ForwardIterator, class T>
359227825Stheraven    pair<ForwardIterator, ForwardIterator>
360227825Stheraven    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
361227825Stheraven
362227825Stheraventemplate <class ForwardIterator, class T, class Compare>
363227825Stheraven    pair<ForwardIterator, ForwardIterator>
364227825Stheraven    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
365227825Stheraven
366227825Stheraventemplate <class ForwardIterator, class T>
367227825Stheraven    bool
368227825Stheraven    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
369227825Stheraven
370227825Stheraventemplate <class ForwardIterator, class T, class Compare>
371227825Stheraven    bool
372227825Stheraven    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
373227825Stheraven
374227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
375227825Stheraven    OutputIterator
376227825Stheraven    merge(InputIterator1 first1, InputIterator1 last1,
377227825Stheraven          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
378227825Stheraven
379227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
380227825Stheraven    OutputIterator
381227825Stheraven    merge(InputIterator1 first1, InputIterator1 last1,
382227825Stheraven          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
383227825Stheraven
384227825Stheraventemplate <class BidirectionalIterator>
385227825Stheraven    void
386227825Stheraven    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
387227825Stheraven
388227825Stheraventemplate <class BidirectionalIterator, class Compare>
389227825Stheraven    void
390227825Stheraven    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
391227825Stheraven
392227825Stheraventemplate <class InputIterator1, class InputIterator2>
393227825Stheraven    bool
394227825Stheraven    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
395227825Stheraven
396227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare>
397227825Stheraven    bool
398227825Stheraven    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
399227825Stheraven
400227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
401227825Stheraven    OutputIterator
402227825Stheraven    set_union(InputIterator1 first1, InputIterator1 last1,
403227825Stheraven              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
404227825Stheraven
405227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
406227825Stheraven    OutputIterator
407227825Stheraven    set_union(InputIterator1 first1, InputIterator1 last1,
408227825Stheraven              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
409227825Stheraven
410227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
411227825Stheraven    OutputIterator
412227825Stheraven    set_intersection(InputIterator1 first1, InputIterator1 last1,
413227825Stheraven                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
414227825Stheraven
415227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
416227825Stheraven    OutputIterator
417227825Stheraven    set_intersection(InputIterator1 first1, InputIterator1 last1,
418227825Stheraven                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
419227825Stheraven
420227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
421227825Stheraven    OutputIterator
422227825Stheraven    set_difference(InputIterator1 first1, InputIterator1 last1,
423227825Stheraven                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
424227825Stheraven
425227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
426227825Stheraven    OutputIterator
427227825Stheraven    set_difference(InputIterator1 first1, InputIterator1 last1,
428227825Stheraven                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
429227825Stheraven
430227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator>
431227825Stheraven    OutputIterator
432227825Stheraven    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
433227825Stheraven                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
434227825Stheraven
435227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
436227825Stheraven    OutputIterator
437227825Stheraven    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
438227825Stheraven                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
439227825Stheraven
440227825Stheraventemplate <class RandomAccessIterator>
441227825Stheraven    void
442227825Stheraven    push_heap(RandomAccessIterator first, RandomAccessIterator last);
443227825Stheraven
444227825Stheraventemplate <class RandomAccessIterator, class Compare>
445227825Stheraven    void
446227825Stheraven    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
447227825Stheraven
448227825Stheraventemplate <class RandomAccessIterator>
449227825Stheraven    void
450227825Stheraven    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
451227825Stheraven
452227825Stheraventemplate <class RandomAccessIterator, class Compare>
453227825Stheraven    void
454227825Stheraven    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
455227825Stheraven
456227825Stheraventemplate <class RandomAccessIterator>
457227825Stheraven    void
458227825Stheraven    make_heap(RandomAccessIterator first, RandomAccessIterator last);
459227825Stheraven
460227825Stheraventemplate <class RandomAccessIterator, class Compare>
461227825Stheraven    void
462227825Stheraven    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
463227825Stheraven
464227825Stheraventemplate <class RandomAccessIterator>
465227825Stheraven    void
466227825Stheraven    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
467227825Stheraven
468227825Stheraventemplate <class RandomAccessIterator, class Compare>
469227825Stheraven    void
470227825Stheraven    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
471227825Stheraven
472227825Stheraventemplate <class RandomAccessIterator>
473227825Stheraven    bool
474227825Stheraven    is_heap(RandomAccessIterator first, RandomAccessiterator last);
475227825Stheraven
476227825Stheraventemplate <class RandomAccessIterator, class Compare>
477227825Stheraven    bool
478227825Stheraven    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
479227825Stheraven
480227825Stheraventemplate <class RandomAccessIterator>
481227825Stheraven    RandomAccessIterator
482227825Stheraven    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
483227825Stheraven
484227825Stheraventemplate <class RandomAccessIterator, class Compare>
485227825Stheraven    RandomAccessIterator
486227825Stheraven    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
487227825Stheraven
488227825Stheraventemplate <class ForwardIterator>
489227825Stheraven    ForwardIterator
490227825Stheraven    min_element(ForwardIterator first, ForwardIterator last);
491227825Stheraven
492227825Stheraventemplate <class ForwardIterator, class Compare>
493227825Stheraven    ForwardIterator
494227825Stheraven    min_element(ForwardIterator first, ForwardIterator last, Compare comp);
495227825Stheraven
496227825Stheraventemplate <class T>
497227825Stheraven    const T&
498227825Stheraven    min(const T& a, const T& b);
499227825Stheraven
500227825Stheraventemplate <class T, class Compare>
501227825Stheraven    const T&
502227825Stheraven    min(const T& a, const T& b, Compare comp);
503227825Stheraven
504227825Stheraventemplate<class T>
505227825Stheraven    T
506227825Stheraven    min(initializer_list<T> t);
507227825Stheraven
508227825Stheraventemplate<class T, class Compare>
509227825Stheraven    T
510227825Stheraven    min(initializer_list<T> t, Compare comp);
511227825Stheraven
512227825Stheraventemplate <class ForwardIterator>
513227825Stheraven    ForwardIterator
514227825Stheraven    max_element(ForwardIterator first, ForwardIterator last);
515227825Stheraven
516227825Stheraventemplate <class ForwardIterator, class Compare>
517227825Stheraven    ForwardIterator
518227825Stheraven    max_element(ForwardIterator first, ForwardIterator last, Compare comp);
519227825Stheraven
520227825Stheraventemplate <class T>
521227825Stheraven    const T&
522227825Stheraven    max(const T& a, const T& b);
523227825Stheraven
524227825Stheraventemplate <class T, class Compare>
525227825Stheraven    const T&
526227825Stheraven    max(const T& a, const T& b, Compare comp);
527227825Stheraven
528227825Stheraventemplate<class T>
529227825Stheraven    T
530227825Stheraven    max(initializer_list<T> t);
531227825Stheraven
532227825Stheraventemplate<class T, class Compare>
533227825Stheraven    T
534227825Stheraven    max(initializer_list<T> t, Compare comp);
535227825Stheraven
536227825Stheraventemplate<class ForwardIterator>
537227825Stheraven    pair<ForwardIterator, ForwardIterator>
538227825Stheraven    minmax_element(ForwardIterator first, ForwardIterator last);
539227825Stheraven
540227825Stheraventemplate<class ForwardIterator, class Compare>
541227825Stheraven    pair<ForwardIterator, ForwardIterator>
542227825Stheraven    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
543227825Stheraven
544227825Stheraventemplate<class T>
545227825Stheraven    pair<const T&, const T&>
546227825Stheraven    minmax(const T& a, const T& b);
547227825Stheraven
548227825Stheraventemplate<class T, class Compare>
549227825Stheraven    pair<const T&, const T&>
550227825Stheraven    minmax(const T& a, const T& b, Compare comp);
551227825Stheraven
552227825Stheraventemplate<class T>
553227825Stheraven    pair<T, T>
554227825Stheraven    minmax(initializer_list<T> t);
555227825Stheraven
556227825Stheraventemplate<class T, class Compare>
557227825Stheraven    pair<T, T>
558227825Stheraven    minmax(initializer_list<T> t, Compare comp);
559227825Stheraven
560227825Stheraventemplate <class InputIterator1, class InputIterator2>
561227825Stheraven    bool
562227825Stheraven    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
563227825Stheraven
564227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare>
565227825Stheraven    bool
566227825Stheraven    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
567227825Stheraven                            InputIterator2 first2, InputIterator2 last2, Compare comp);
568227825Stheraven
569227825Stheraventemplate <class BidirectionalIterator>
570227825Stheraven    bool
571227825Stheraven    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
572227825Stheraven
573227825Stheraventemplate <class BidirectionalIterator, class Compare>
574227825Stheraven    bool
575227825Stheraven    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
576227825Stheraven
577227825Stheraventemplate <class BidirectionalIterator>
578227825Stheraven    bool
579227825Stheraven    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
580227825Stheraven
581227825Stheraventemplate <class BidirectionalIterator, class Compare>
582227825Stheraven    bool
583227825Stheraven    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
584227825Stheraven
585227825Stheraven}  // std
586227825Stheraven
587227825Stheraven*/
588227825Stheraven
589227825Stheraven#include <__config>
590227825Stheraven#include <initializer_list>
591227825Stheraven#include <type_traits>
592227825Stheraven#include <cstring>
593227825Stheraven#include <utility>
594227825Stheraven#include <memory>
595227825Stheraven#include <iterator>
596241903Sdim#include <cstddef>
597227825Stheraven
598232950Stheraven#include <__undef_min_max>
599232950Stheraven
600227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
601227825Stheraven#pragma GCC system_header
602227825Stheraven#endif
603227825Stheraven
604227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
605227825Stheraven
606227825Stheraventemplate <class _T1, class _T2 = _T1>
607227825Stheravenstruct __equal_to
608227825Stheraven{
609227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
610227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
611227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
612227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
613227825Stheraven};
614227825Stheraven
615227825Stheraventemplate <class _T1>
616227825Stheravenstruct __equal_to<_T1, _T1>
617227825Stheraven{
618227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
619227825Stheraven};
620227825Stheraven
621227825Stheraventemplate <class _T1>
622227825Stheravenstruct __equal_to<const _T1, _T1>
623227825Stheraven{
624227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
625227825Stheraven};
626227825Stheraven
627227825Stheraventemplate <class _T1>
628227825Stheravenstruct __equal_to<_T1, const _T1>
629227825Stheraven{
630227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
631227825Stheraven};
632227825Stheraven
633227825Stheraventemplate <class _T1, class _T2 = _T1>
634227825Stheravenstruct __less
635227825Stheraven{
636227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
637227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
638227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
639227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
640227825Stheraven};
641227825Stheraven
642227825Stheraventemplate <class _T1>
643227825Stheravenstruct __less<_T1, _T1>
644227825Stheraven{
645227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
646227825Stheraven};
647227825Stheraven
648227825Stheraventemplate <class _T1>
649227825Stheravenstruct __less<const _T1, _T1>
650227825Stheraven{
651227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
652227825Stheraven};
653227825Stheraven
654227825Stheraventemplate <class _T1>
655227825Stheravenstruct __less<_T1, const _T1>
656227825Stheraven{
657227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
658227825Stheraven};
659227825Stheraven
660227825Stheraventemplate <class _Predicate>
661227825Stheravenclass __negate
662227825Stheraven{
663227825Stheravenprivate:
664227825Stheraven    _Predicate __p_;
665227825Stheravenpublic:
666227825Stheraven    _LIBCPP_INLINE_VISIBILITY __negate() {}
667227825Stheraven
668227825Stheraven    _LIBCPP_INLINE_VISIBILITY
669227825Stheraven    explicit __negate(_Predicate __p) : __p_(__p) {}
670227825Stheraven
671227825Stheraven    template <class _T1>
672227825Stheraven    _LIBCPP_INLINE_VISIBILITY
673227825Stheraven    bool operator()(const _T1& __x) {return !__p_(__x);}
674227825Stheraven
675227825Stheraven    template <class _T1, class _T2>
676227825Stheraven    _LIBCPP_INLINE_VISIBILITY
677227825Stheraven    bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
678227825Stheraven};
679227825Stheraven
680227825Stheraven#ifdef _LIBCPP_DEBUG2
681227825Stheraven
682227825Stheraventemplate <class _Compare>
683227825Stheravenstruct __debug_less
684227825Stheraven{
685227825Stheraven    _Compare __comp_;
686227825Stheraven    __debug_less(_Compare& __c) : __comp_(__c) {}
687227825Stheraven    template <class _Tp, class _Up>
688227825Stheraven    bool operator()(const _Tp& __x, const _Up& __y)
689227825Stheraven    {
690227825Stheraven        bool __r = __comp_(__x, __y);
691227825Stheraven        if (__r)
692227825Stheraven            _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
693227825Stheraven        return __r;
694227825Stheraven    }
695227825Stheraven};
696227825Stheraven
697227825Stheraven#endif  // _LIBCPP_DEBUG2
698227825Stheraven
699227825Stheraven// Precondition:  __x != 0
700232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
701232950Stheravenunsigned
702232950Stheraven__ctz(unsigned __x)
703232950Stheraven{
704232950Stheraven    return static_cast<unsigned>(__builtin_ctz(__x));
705232950Stheraven}
706227825Stheraven
707232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
708232950Stheravenunsigned long
709232950Stheraven__ctz(unsigned long __x)
710232950Stheraven{
711232950Stheraven    return static_cast<unsigned long>(__builtin_ctzl(__x));
712232950Stheraven}
713232950Stheraven
714232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
715232950Stheravenunsigned long long
716232950Stheraven__ctz(unsigned long long __x)
717232950Stheraven{
718232950Stheraven    return static_cast<unsigned long long>(__builtin_ctzll(__x));
719232950Stheraven}
720232950Stheraven
721227825Stheraven// Precondition:  __x != 0
722232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
723232950Stheravenunsigned
724232950Stheraven__clz(unsigned __x)
725232950Stheraven{
726232950Stheraven    return static_cast<unsigned>(__builtin_clz(__x));
727232950Stheraven}
728227825Stheraven
729232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
730232950Stheravenunsigned long
731232950Stheraven__clz(unsigned long __x)
732232950Stheraven{
733232950Stheraven    return static_cast<unsigned long>(__builtin_clzl (__x));
734232950Stheraven}
735232950Stheraven
736232950Stheraveninline _LIBCPP_INLINE_VISIBILITY
737232950Stheravenunsigned long long
738232950Stheraven__clz(unsigned long long __x)
739232950Stheraven{
740232950Stheraven    return static_cast<unsigned long long>(__builtin_clzll(__x));
741232950Stheraven}
742232950Stheraven
743227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned           __x) {return __builtin_popcount  (__x);}
744227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned      long __x) {return __builtin_popcountl (__x);}
745227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
746227825Stheraven
747227825Stheraven// all_of
748227825Stheraven
749227825Stheraventemplate <class _InputIterator, class _Predicate>
750227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
751227825Stheravenbool
752227825Stheravenall_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
753227825Stheraven{
754227825Stheraven    for (; __first != __last; ++__first)
755227825Stheraven        if (!__pred(*__first))
756227825Stheraven            return false;
757227825Stheraven    return true;
758227825Stheraven}
759227825Stheraven
760227825Stheraven// any_of
761227825Stheraven
762227825Stheraventemplate <class _InputIterator, class _Predicate>
763227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
764227825Stheravenbool
765227825Stheravenany_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
766227825Stheraven{
767227825Stheraven    for (; __first != __last; ++__first)
768227825Stheraven        if (__pred(*__first))
769227825Stheraven            return true;
770227825Stheraven    return false;
771227825Stheraven}
772227825Stheraven
773227825Stheraven// none_of
774227825Stheraven
775227825Stheraventemplate <class _InputIterator, class _Predicate>
776227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
777227825Stheravenbool
778227825Stheravennone_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
779227825Stheraven{
780227825Stheraven    for (; __first != __last; ++__first)
781227825Stheraven        if (__pred(*__first))
782227825Stheraven            return false;
783227825Stheraven    return true;
784227825Stheraven}
785227825Stheraven
786227825Stheraven// for_each
787227825Stheraven
788227825Stheraventemplate <class _InputIterator, class _Function>
789227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
790227825Stheraven_Function
791227825Stheravenfor_each(_InputIterator __first, _InputIterator __last, _Function __f)
792227825Stheraven{
793227825Stheraven    for (; __first != __last; ++__first)
794227825Stheraven        __f(*__first);
795227825Stheraven    return _VSTD::move(__f);
796227825Stheraven}
797227825Stheraven
798227825Stheraven// find
799227825Stheraven
800227825Stheraventemplate <class _InputIterator, class _Tp>
801227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
802227825Stheraven_InputIterator
803227825Stheravenfind(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
804227825Stheraven{
805227825Stheraven    for (; __first != __last; ++__first)
806227825Stheraven        if (*__first == __value_)
807227825Stheraven            break;
808227825Stheraven    return __first;
809227825Stheraven}
810227825Stheraven
811227825Stheraven// find_if
812227825Stheraven
813227825Stheraventemplate <class _InputIterator, class _Predicate>
814227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
815227825Stheraven_InputIterator
816227825Stheravenfind_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
817227825Stheraven{
818227825Stheraven    for (; __first != __last; ++__first)
819227825Stheraven        if (__pred(*__first))
820227825Stheraven            break;
821227825Stheraven    return __first;
822227825Stheraven}
823227825Stheraven
824227825Stheraven// find_if_not
825227825Stheraven
826227825Stheraventemplate<class _InputIterator, class _Predicate>
827227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
828227825Stheraven_InputIterator
829227825Stheravenfind_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
830227825Stheraven{
831227825Stheraven    for (; __first != __last; ++__first)
832227825Stheraven        if (!__pred(*__first))
833227825Stheraven            break;
834227825Stheraven    return __first;
835227825Stheraven}
836227825Stheraven
837227825Stheraven// find_end
838227825Stheraven
839227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
840227825Stheraven_ForwardIterator1
841227825Stheraven__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
842227825Stheraven           _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
843227825Stheraven           forward_iterator_tag, forward_iterator_tag)
844227825Stheraven{
845227825Stheraven    // modeled after search algorithm
846227825Stheraven    _ForwardIterator1 __r = __last1;  // __last1 is the "default" answer
847227825Stheraven    if (__first2 == __last2)
848227825Stheraven        return __r;
849227825Stheraven    while (true)
850227825Stheraven    {
851227825Stheraven        while (true)
852227825Stheraven        {
853227825Stheraven            if (__first1 == __last1)         // if source exhausted return last correct answer
854227825Stheraven                return __r;                  //    (or __last1 if never found)
855227825Stheraven            if (__pred(*__first1, *__first2))
856227825Stheraven                break;
857227825Stheraven            ++__first1;
858227825Stheraven        }
859227825Stheraven        // *__first1 matches *__first2, now match elements after here
860227825Stheraven        _ForwardIterator1 __m1 = __first1;
861227825Stheraven        _ForwardIterator2 __m2 = __first2;
862227825Stheraven        while (true)
863227825Stheraven        {
864227825Stheraven            if (++__m2 == __last2)
865227825Stheraven            {                         // Pattern exhaused, record answer and search for another one
866227825Stheraven                __r = __first1;
867227825Stheraven                ++__first1;
868227825Stheraven                break;
869227825Stheraven            }
870227825Stheraven            if (++__m1 == __last1)     // Source exhausted, return last answer
871227825Stheraven                return __r;
872227825Stheraven            if (!__pred(*__m1, *__m2))  // mismatch, restart with a new __first
873227825Stheraven            {
874227825Stheraven                ++__first1;
875227825Stheraven                break;
876227825Stheraven            }  // else there is a match, check next elements
877227825Stheraven        }
878227825Stheraven    }
879227825Stheraven}
880227825Stheraven
881227825Stheraventemplate <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
882227825Stheraven_BidirectionalIterator1
883227825Stheraven__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
884227825Stheraven           _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
885227825Stheraven           bidirectional_iterator_tag, bidirectional_iterator_tag)
886227825Stheraven{
887227825Stheraven    // modeled after search algorithm (in reverse)
888227825Stheraven    if (__first2 == __last2)
889227825Stheraven        return __last1;  // Everything matches an empty sequence
890227825Stheraven    _BidirectionalIterator1 __l1 = __last1;
891227825Stheraven    _BidirectionalIterator2 __l2 = __last2;
892227825Stheraven    --__l2;
893227825Stheraven    while (true)
894227825Stheraven    {
895227825Stheraven        // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
896227825Stheraven        while (true)
897227825Stheraven        {
898227825Stheraven            if (__first1 == __l1)  // return __last1 if no element matches *__first2
899227825Stheraven                return __last1;
900227825Stheraven            if (__pred(*--__l1, *__l2))
901227825Stheraven                break;
902227825Stheraven        }
903227825Stheraven        // *__l1 matches *__l2, now match elements before here
904227825Stheraven        _BidirectionalIterator1 __m1 = __l1;
905227825Stheraven        _BidirectionalIterator2 __m2 = __l2;
906227825Stheraven        while (true)
907227825Stheraven        {
908227825Stheraven            if (__m2 == __first2)  // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
909227825Stheraven                return __m1;
910227825Stheraven            if (__m1 == __first1)  // Otherwise if source exhaused, pattern not found
911227825Stheraven                return __last1;
912227825Stheraven            if (!__pred(*--__m1, *--__m2))  // if there is a mismatch, restart with a new __l1
913227825Stheraven            {
914227825Stheraven                break;
915227825Stheraven            }  // else there is a match, check next elements
916227825Stheraven        }
917227825Stheraven    }
918227825Stheraven}
919227825Stheraven
920227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
921227825Stheraven_RandomAccessIterator1
922227825Stheraven__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
923227825Stheraven           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
924227825Stheraven           random_access_iterator_tag, random_access_iterator_tag)
925227825Stheraven{
926227825Stheraven    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
927227825Stheraven    typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
928227825Stheraven    if (__len2 == 0)
929227825Stheraven        return __last1;
930227825Stheraven    typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
931227825Stheraven    if (__len1 < __len2)
932227825Stheraven        return __last1;
933227825Stheraven    const _RandomAccessIterator1 __s = __first1 + (__len2 - 1);  // End of pattern match can't go before here
934227825Stheraven    _RandomAccessIterator1 __l1 = __last1;
935227825Stheraven    _RandomAccessIterator2 __l2 = __last2;
936227825Stheraven    --__l2;
937227825Stheraven    while (true)
938227825Stheraven    {
939227825Stheraven        while (true)
940227825Stheraven        {
941227825Stheraven            if (__s == __l1)
942227825Stheraven                return __last1;
943227825Stheraven            if (__pred(*--__l1, *__l2))
944227825Stheraven                break;
945227825Stheraven        }
946227825Stheraven        _RandomAccessIterator1 __m1 = __l1;
947227825Stheraven        _RandomAccessIterator2 __m2 = __l2;
948227825Stheraven        while (true)
949227825Stheraven        {
950227825Stheraven            if (__m2 == __first2)
951227825Stheraven                return __m1;
952227825Stheraven                                 // no need to check range on __m1 because __s guarantees we have enough source
953227825Stheraven            if (!__pred(*--__m1, *--__m2))
954227825Stheraven            {
955227825Stheraven                break;
956227825Stheraven            }
957227825Stheraven        }
958227825Stheraven    }
959227825Stheraven}
960227825Stheraven
961227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
962227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
963227825Stheraven_ForwardIterator1
964227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
965227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
966227825Stheraven{
967227825Stheraven    return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
968227825Stheraven                         (__first1, __last1, __first2, __last2, __pred,
969227825Stheraven                          typename iterator_traits<_ForwardIterator1>::iterator_category(),
970227825Stheraven                          typename iterator_traits<_ForwardIterator2>::iterator_category());
971227825Stheraven}
972227825Stheraven
973227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
974227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
975227825Stheraven_ForwardIterator1
976227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
977227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2)
978227825Stheraven{
979227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
980227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
981227825Stheraven    return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
982227825Stheraven}
983227825Stheraven
984227825Stheraven// find_first_of
985227825Stheraven
986227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
987227825Stheraven_ForwardIterator1
988227825Stheravenfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
989227825Stheraven              _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
990227825Stheraven{
991227825Stheraven    for (; __first1 != __last1; ++__first1)
992227825Stheraven        for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
993227825Stheraven            if (__pred(*__first1, *__j))
994227825Stheraven                return __first1;
995227825Stheraven    return __last1;
996227825Stheraven}
997227825Stheraven
998227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
999227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1000227825Stheraven_ForwardIterator1
1001227825Stheravenfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1002227825Stheraven              _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1003227825Stheraven{
1004227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1005227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1006227825Stheraven    return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1007227825Stheraven}
1008227825Stheraven
1009227825Stheraven// adjacent_find
1010227825Stheraven
1011227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate>
1012227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1013227825Stheraven_ForwardIterator
1014227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1015227825Stheraven{
1016227825Stheraven    if (__first != __last)
1017227825Stheraven    {
1018227825Stheraven        _ForwardIterator __i = __first;
1019227825Stheraven        while (++__i != __last)
1020227825Stheraven        {
1021227825Stheraven            if (__pred(*__first, *__i))
1022227825Stheraven                return __first;
1023227825Stheraven            __first = __i;
1024227825Stheraven        }
1025227825Stheraven    }
1026227825Stheraven    return __last;
1027227825Stheraven}
1028227825Stheraven
1029227825Stheraventemplate <class _ForwardIterator>
1030227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1031227825Stheraven_ForwardIterator
1032227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1033227825Stheraven{
1034227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1035227825Stheraven    return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
1036227825Stheraven}
1037227825Stheraven
1038227825Stheraven// count
1039227825Stheraven
1040227825Stheraventemplate <class _InputIterator, class _Tp>
1041227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1042227825Stheraventypename iterator_traits<_InputIterator>::difference_type
1043227825Stheravencount(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
1044227825Stheraven{
1045227825Stheraven    typename iterator_traits<_InputIterator>::difference_type __r(0);
1046227825Stheraven    for (; __first != __last; ++__first)
1047227825Stheraven        if (*__first == __value_)
1048227825Stheraven            ++__r;
1049227825Stheraven    return __r;
1050227825Stheraven}
1051227825Stheraven
1052227825Stheraven// count_if
1053227825Stheraven
1054227825Stheraventemplate <class _InputIterator, class _Predicate>
1055227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1056227825Stheraventypename iterator_traits<_InputIterator>::difference_type
1057227825Stheravencount_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1058227825Stheraven{
1059227825Stheraven    typename iterator_traits<_InputIterator>::difference_type __r(0);
1060227825Stheraven    for (; __first != __last; ++__first)
1061227825Stheraven        if (__pred(*__first))
1062227825Stheraven            ++__r;
1063227825Stheraven    return __r;
1064227825Stheraven}
1065227825Stheraven
1066227825Stheraven// mismatch
1067227825Stheraven
1068227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1069227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1070227825Stheravenpair<_InputIterator1, _InputIterator2>
1071227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1072227825Stheraven         _InputIterator2 __first2, _BinaryPredicate __pred)
1073227825Stheraven{
1074227825Stheraven    for (; __first1 != __last1; ++__first1, ++__first2)
1075227825Stheraven        if (!__pred(*__first1, *__first2))
1076227825Stheraven            break;
1077227825Stheraven    return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1078227825Stheraven}
1079227825Stheraven
1080227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
1081227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1082227825Stheravenpair<_InputIterator1, _InputIterator2>
1083227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1084227825Stheraven{
1085227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1086227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1087227825Stheraven    return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1088227825Stheraven}
1089227825Stheraven
1090227825Stheraven// equal
1091227825Stheraven
1092227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1093227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1094227825Stheravenbool
1095227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1096227825Stheraven{
1097227825Stheraven    for (; __first1 != __last1; ++__first1, ++__first2)
1098227825Stheraven        if (!__pred(*__first1, *__first2))
1099227825Stheraven            return false;
1100227825Stheraven    return true;
1101227825Stheraven}
1102227825Stheraven
1103227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
1104227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1105227825Stheravenbool
1106227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1107227825Stheraven{
1108227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1109227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1110227825Stheraven    return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1111227825Stheraven}
1112227825Stheraven
1113227825Stheraven// is_permutation
1114227825Stheraven
1115227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1116227825Stheravenbool
1117227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1118227825Stheraven               _ForwardIterator2 __first2, _BinaryPredicate __pred)
1119227825Stheraven{
1120227825Stheraven    // shorten sequences as much as possible by lopping of any equal parts
1121227825Stheraven    for (; __first1 != __last1; ++__first1, ++__first2)
1122227825Stheraven        if (!__pred(*__first1, *__first2))
1123227825Stheraven            goto __not_done;
1124227825Stheraven    return true;
1125227825Stheraven__not_done:
1126227825Stheraven    // __first1 != __last1 && *__first1 != *__first2
1127227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1128227825Stheraven    _D1 __l1 = _VSTD::distance(__first1, __last1);
1129227825Stheraven    if (__l1 == _D1(1))
1130227825Stheraven        return false;
1131227825Stheraven    _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
1132227825Stheraven    // For each element in [f1, l1) see if there are the same number of
1133227825Stheraven    //    equal elements in [f2, l2)
1134227825Stheraven    for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1135227825Stheraven    {
1136227825Stheraven        // Have we already counted the number of *__i in [f1, l1)?
1137227825Stheraven        for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1138227825Stheraven            if (__pred(*__j, *__i))
1139227825Stheraven                goto __next_iter;
1140227825Stheraven        {
1141227825Stheraven            // Count number of *__i in [f2, l2)
1142227825Stheraven            _D1 __c2 = 0;
1143227825Stheraven            for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1144227825Stheraven                if (__pred(*__i, *__j))
1145227825Stheraven                    ++__c2;
1146227825Stheraven            if (__c2 == 0)
1147227825Stheraven                return false;
1148227825Stheraven            // Count number of *__i in [__i, l1) (we can start with 1)
1149227825Stheraven            _D1 __c1 = 1;
1150227825Stheraven            for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1151227825Stheraven                if (__pred(*__i, *__j))
1152227825Stheraven                    ++__c1;
1153227825Stheraven            if (__c1 != __c2)
1154227825Stheraven                return false;
1155227825Stheraven        }
1156227825Stheraven__next_iter:;
1157227825Stheraven    }
1158227825Stheraven    return true;
1159227825Stheraven}
1160227825Stheraven
1161227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2>
1162227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1163227825Stheravenbool
1164227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1165227825Stheraven               _ForwardIterator2 __first2)
1166227825Stheraven{
1167227825Stheraven    typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1168227825Stheraven    typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1169227825Stheraven    return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1170227825Stheraven}
1171227825Stheraven
1172227825Stheraven// search
1173227825Stheraven
1174227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1175227825Stheraven_ForwardIterator1
1176227825Stheraven__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1177227825Stheraven         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1178227825Stheraven         forward_iterator_tag, forward_iterator_tag)
1179227825Stheraven{
1180227825Stheraven    if (__first2 == __last2)
1181227825Stheraven        return __first1;  // Everything matches an empty sequence
1182227825Stheraven    while (true)
1183227825Stheraven    {
1184227825Stheraven        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1185227825Stheraven        while (true)
1186227825Stheraven        {
1187227825Stheraven            if (__first1 == __last1)  // return __last1 if no element matches *__first2
1188227825Stheraven                return __last1;
1189227825Stheraven            if (__pred(*__first1, *__first2))
1190227825Stheraven                break;
1191227825Stheraven            ++__first1;
1192227825Stheraven        }
1193227825Stheraven        // *__first1 matches *__first2, now match elements after here
1194227825Stheraven        _ForwardIterator1 __m1 = __first1;
1195227825Stheraven        _ForwardIterator2 __m2 = __first2;
1196227825Stheraven        while (true)
1197227825Stheraven        {
1198227825Stheraven            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1199227825Stheraven                return __first1;
1200227825Stheraven            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
1201227825Stheraven                return __last1;
1202227825Stheraven            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
1203227825Stheraven            {
1204227825Stheraven                ++__first1;
1205227825Stheraven                break;
1206227825Stheraven            }  // else there is a match, check next elements
1207227825Stheraven        }
1208227825Stheraven    }
1209227825Stheraven}
1210227825Stheraven
1211227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1212227825Stheraven_RandomAccessIterator1
1213227825Stheraven__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1214227825Stheraven           _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1215227825Stheraven           random_access_iterator_tag, random_access_iterator_tag)
1216227825Stheraven{
1217227825Stheraven    typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1218227825Stheraven    typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1219227825Stheraven    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
1220227825Stheraven    _D2 __len2 = __last2 - __first2;
1221227825Stheraven    if (__len2 == 0)
1222227825Stheraven        return __first1;
1223227825Stheraven    _D1 __len1 = __last1 - __first1;
1224227825Stheraven    if (__len1 < __len2)
1225227825Stheraven        return __last1;
1226227825Stheraven    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
1227227825Stheraven    while (true)
1228227825Stheraven    {
1229227825Stheraven#if !_LIBCPP_UNROLL_LOOPS
1230227825Stheraven        while (true)
1231227825Stheraven        {
1232227825Stheraven            if (__first1 == __s)
1233227825Stheraven                return __last1;
1234227825Stheraven            if (__pred(*__first1, *__first2))
1235227825Stheraven                break;
1236227825Stheraven            ++__first1;
1237227825Stheraven        }
1238227825Stheraven#else  // !_LIBCPP_UNROLL_LOOPS
1239227825Stheraven        for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1240227825Stheraven        {
1241227825Stheraven            if (__pred(*__first1, *__first2))
1242227825Stheraven                goto __phase2;
1243227825Stheraven            if (__pred(*++__first1, *__first2))
1244227825Stheraven                goto __phase2;
1245227825Stheraven            if (__pred(*++__first1, *__first2))
1246227825Stheraven                goto __phase2;
1247227825Stheraven            if (__pred(*++__first1, *__first2))
1248227825Stheraven                goto __phase2;
1249227825Stheraven            ++__first1;
1250227825Stheraven        }
1251227825Stheraven        switch (__s - __first1)
1252227825Stheraven        {
1253227825Stheraven        case 3:
1254227825Stheraven            if (__pred(*__first1, *__first2))
1255227825Stheraven                break;
1256227825Stheraven            ++__first1;
1257227825Stheraven        case 2:
1258227825Stheraven            if (__pred(*__first1, *__first2))
1259227825Stheraven                break;
1260227825Stheraven            ++__first1;
1261227825Stheraven        case 1:
1262227825Stheraven            if (__pred(*__first1, *__first2))
1263227825Stheraven                break;
1264227825Stheraven        case 0:
1265227825Stheraven            return __last1;
1266227825Stheraven        }
1267227825Stheraven    __phase2:
1268227825Stheraven#endif  // !_LIBCPP_UNROLL_LOOPS
1269227825Stheraven        _RandomAccessIterator1 __m1 = __first1;
1270227825Stheraven        _RandomAccessIterator2 __m2 = __first2;
1271227825Stheraven#if !_LIBCPP_UNROLL_LOOPS
1272227825Stheraven         while (true)
1273227825Stheraven         {
1274227825Stheraven             if (++__m2 == __last2)
1275227825Stheraven                 return __first1;
1276227825Stheraven             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
1277227825Stheraven             if (!__pred(*__m1, *__m2))
1278227825Stheraven             {
1279227825Stheraven                 ++__first1;
1280227825Stheraven                 break;
1281227825Stheraven             }
1282227825Stheraven         }
1283227825Stheraven#else  // !_LIBCPP_UNROLL_LOOPS
1284227825Stheraven        ++__m2;
1285227825Stheraven        ++__m1;
1286227825Stheraven        for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1287227825Stheraven        {
1288227825Stheraven            if (!__pred(*__m1, *__m2))
1289227825Stheraven                goto __continue;
1290227825Stheraven            if (!__pred(*++__m1, *++__m2))
1291227825Stheraven                goto __continue;
1292227825Stheraven            if (!__pred(*++__m1, *++__m2))
1293227825Stheraven                goto __continue;
1294227825Stheraven            if (!__pred(*++__m1, *++__m2))
1295227825Stheraven                goto __continue;
1296227825Stheraven            ++__m1;
1297227825Stheraven            ++__m2;
1298227825Stheraven        }
1299227825Stheraven        switch (__last2 - __m2)
1300227825Stheraven        {
1301227825Stheraven        case 3:
1302227825Stheraven            if (!__pred(*__m1, *__m2))
1303227825Stheraven                break;
1304227825Stheraven            ++__m1;
1305227825Stheraven            ++__m2;
1306227825Stheraven        case 2:
1307227825Stheraven            if (!__pred(*__m1, *__m2))
1308227825Stheraven                break;
1309227825Stheraven            ++__m1;
1310227825Stheraven            ++__m2;
1311227825Stheraven        case 1:
1312227825Stheraven            if (!__pred(*__m1, *__m2))
1313227825Stheraven                break;
1314227825Stheraven        case 0:
1315227825Stheraven            return __first1;
1316227825Stheraven        }
1317227825Stheraven    __continue:
1318227825Stheraven        ++__first1;
1319227825Stheraven#endif  // !_LIBCPP_UNROLL_LOOPS
1320227825Stheraven    }
1321227825Stheraven}
1322227825Stheraven
1323227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1324227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1325227825Stheraven_ForwardIterator1
1326227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1327227825Stheraven       _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1328227825Stheraven{
1329227825Stheraven    return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1330227825Stheraven                         (__first1, __last1, __first2, __last2, __pred,
1331227825Stheraven                          typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1332227825Stheraven                          typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1333227825Stheraven}
1334227825Stheraven
1335227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2>
1336227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1337227825Stheraven_ForwardIterator1
1338227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1339227825Stheraven       _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1340227825Stheraven{
1341227825Stheraven    typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1342227825Stheraven    typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1343227825Stheraven    return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1344227825Stheraven}
1345227825Stheraven
1346227825Stheraven// search_n
1347227825Stheraven
1348227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1349227825Stheraven_ForwardIterator
1350227825Stheraven__search_n(_ForwardIterator __first, _ForwardIterator __last,
1351227825Stheraven           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
1352227825Stheraven{
1353227825Stheraven    if (__count <= 0)
1354227825Stheraven        return __first;
1355227825Stheraven    while (true)
1356227825Stheraven    {
1357227825Stheraven        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1358227825Stheraven        while (true)
1359227825Stheraven        {
1360227825Stheraven            if (__first == __last)  // return __last if no element matches __value_
1361227825Stheraven                return __last;
1362227825Stheraven            if (__pred(*__first, __value_))
1363227825Stheraven                break;
1364227825Stheraven            ++__first;
1365227825Stheraven        }
1366227825Stheraven        // *__first matches __value_, now match elements after here
1367227825Stheraven        _ForwardIterator __m = __first;
1368227825Stheraven        _Size __c(0);
1369227825Stheraven        while (true)
1370227825Stheraven        {
1371227825Stheraven            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1372227825Stheraven                return __first;
1373227825Stheraven            if (++__m == __last)  // Otherwise if source exhaused, pattern not found
1374227825Stheraven                return __last;
1375227825Stheraven            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1376227825Stheraven            {
1377227825Stheraven                __first = __m;
1378227825Stheraven                ++__first;
1379227825Stheraven                break;
1380227825Stheraven            }  // else there is a match, check next elements
1381227825Stheraven        }
1382227825Stheraven    }
1383227825Stheraven}
1384227825Stheraven
1385227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1386227825Stheraven_RandomAccessIterator
1387227825Stheraven__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1388227825Stheraven           _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
1389227825Stheraven{
1390227825Stheraven    if (__count <= 0)
1391227825Stheraven        return __first;
1392227825Stheraven    _Size __len = static_cast<_Size>(__last - __first);
1393227825Stheraven    if (__len < __count)
1394227825Stheraven        return __last;
1395227825Stheraven    const _RandomAccessIterator __s = __last - (__count - 1);  // Start of pattern match can't go beyond here
1396227825Stheraven    while (true)
1397227825Stheraven    {
1398227825Stheraven        // Find first element in sequence that matchs __value_, with a mininum of loop checks
1399227825Stheraven        while (true)
1400227825Stheraven        {
1401227825Stheraven            if (__first == __s)  // return __last if no element matches __value_
1402227825Stheraven                return __last;
1403227825Stheraven            if (__pred(*__first, __value_))
1404227825Stheraven                break;
1405227825Stheraven            ++__first;
1406227825Stheraven        }
1407227825Stheraven        // *__first matches __value_, now match elements after here
1408227825Stheraven        _RandomAccessIterator __m = __first;
1409227825Stheraven        _Size __c(0);
1410227825Stheraven        while (true)
1411227825Stheraven        {
1412227825Stheraven            if (++__c == __count)  // If pattern exhausted, __first is the answer (works for 1 element pattern)
1413227825Stheraven                return __first;
1414227825Stheraven             ++__m;          // no need to check range on __m because __s guarantees we have enough source
1415227825Stheraven            if (!__pred(*__m, __value_))  // if there is a mismatch, restart with a new __first
1416227825Stheraven            {
1417227825Stheraven                __first = __m;
1418227825Stheraven                ++__first;
1419227825Stheraven                break;
1420227825Stheraven            }  // else there is a match, check next elements
1421227825Stheraven        }
1422227825Stheraven    }
1423227825Stheraven}
1424227825Stheraven
1425227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1426227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1427227825Stheraven_ForwardIterator
1428227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last,
1429227825Stheraven         _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
1430227825Stheraven{
1431227825Stheraven    return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1432227825Stheraven           (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1433227825Stheraven}
1434227825Stheraven
1435227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp>
1436227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1437227825Stheraven_ForwardIterator
1438227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
1439227825Stheraven{
1440227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1441227825Stheraven    return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
1442227825Stheraven}
1443227825Stheraven
1444227825Stheraven// copy
1445227825Stheraven
1446227825Stheraventemplate <class _Iter>
1447227825Stheravenstruct __libcpp_is_trivial_iterator
1448227825Stheraven{
1449227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1450227825Stheraven};
1451227825Stheraven
1452227825Stheraventemplate <class _Iter>
1453227825Stheravenstruct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1454227825Stheraven{
1455227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1456227825Stheraven};
1457227825Stheraven
1458227825Stheraventemplate <class _Iter>
1459227825Stheravenstruct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1460227825Stheraven{
1461227825Stheraven    static const bool value = is_pointer<_Iter>::value;
1462227825Stheraven};
1463227825Stheraven
1464227825Stheraventemplate <class _Iter>
1465227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1466227825Stheraven_Iter
1467227825Stheraven__unwrap_iter(_Iter __i)
1468227825Stheraven{
1469227825Stheraven    return __i;
1470227825Stheraven}
1471227825Stheraven
1472227825Stheraventemplate <class _Tp>
1473227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1474227825Stheraventypename enable_if
1475227825Stheraven<
1476227825Stheraven    is_trivially_copy_assignable<_Tp>::value,
1477227825Stheraven    _Tp*
1478227825Stheraven>::type
1479227825Stheraven__unwrap_iter(move_iterator<_Tp*> __i)
1480227825Stheraven{
1481227825Stheraven    return __i.base();
1482227825Stheraven}
1483227825Stheraven
1484227825Stheraventemplate <class _Tp>
1485227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1486227825Stheraventypename enable_if
1487227825Stheraven<
1488227825Stheraven    is_trivially_copy_assignable<_Tp>::value,
1489227825Stheraven    _Tp*
1490227825Stheraven>::type
1491227825Stheraven__unwrap_iter(__wrap_iter<_Tp*> __i)
1492227825Stheraven{
1493227825Stheraven    return __i.base();
1494227825Stheraven}
1495227825Stheraven
1496227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1497227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1498227825Stheraven_OutputIterator
1499227825Stheraven__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1500227825Stheraven{
1501227825Stheraven    for (; __first != __last; ++__first, ++__result)
1502227825Stheraven        *__result = *__first;
1503227825Stheraven    return __result;
1504227825Stheraven}
1505227825Stheraven
1506227825Stheraventemplate <class _Tp, class _Up>
1507227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1508227825Stheraventypename enable_if
1509227825Stheraven<
1510227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1511227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1512227825Stheraven    _Up*
1513227825Stheraven>::type
1514227825Stheraven__copy(_Tp* __first, _Tp* __last, _Up* __result)
1515227825Stheraven{
1516227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1517227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1518227825Stheraven    return __result + __n;
1519227825Stheraven}
1520227825Stheraven
1521227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1522227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1523227825Stheraven_OutputIterator
1524227825Stheravencopy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1525227825Stheraven{
1526227825Stheraven    return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1527227825Stheraven}
1528227825Stheraven
1529227825Stheraven// copy_backward
1530227825Stheraven
1531246487Stheraventemplate <class _BidirectionalIterator, class _OutputIterator>
1532227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1533227825Stheraven_OutputIterator
1534246487Stheraven__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1535227825Stheraven{
1536227825Stheraven    while (__first != __last)
1537227825Stheraven        *--__result = *--__last;
1538227825Stheraven    return __result;
1539227825Stheraven}
1540227825Stheraven
1541227825Stheraventemplate <class _Tp, class _Up>
1542227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1543227825Stheraventypename enable_if
1544227825Stheraven<
1545227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1546227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1547227825Stheraven    _Up*
1548227825Stheraven>::type
1549227825Stheraven__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1550227825Stheraven{
1551227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1552227825Stheraven    __result -= __n;
1553227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1554227825Stheraven    return __result;
1555227825Stheraven}
1556227825Stheraven
1557227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
1558227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1559227825Stheraven_BidirectionalIterator2
1560227825Stheravencopy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1561227825Stheraven              _BidirectionalIterator2 __result)
1562227825Stheraven{
1563227825Stheraven    return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1564227825Stheraven}
1565227825Stheraven
1566227825Stheraven// copy_if
1567227825Stheraven
1568227825Stheraventemplate<class _InputIterator, class _OutputIterator, class _Predicate>
1569227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1570227825Stheraven_OutputIterator
1571227825Stheravencopy_if(_InputIterator __first, _InputIterator __last,
1572227825Stheraven        _OutputIterator __result, _Predicate __pred)
1573227825Stheraven{
1574227825Stheraven    for (; __first != __last; ++__first)
1575227825Stheraven    {
1576227825Stheraven        if (__pred(*__first))
1577227825Stheraven        {
1578227825Stheraven            *__result = *__first;
1579227825Stheraven            ++__result;
1580227825Stheraven        }
1581227825Stheraven    }
1582227825Stheraven    return __result;
1583227825Stheraven}
1584227825Stheraven
1585227825Stheraven// copy_n
1586227825Stheraven
1587227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator>
1588227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1589227825Stheraventypename enable_if
1590227825Stheraven<
1591227825Stheraven    __is_input_iterator<_InputIterator>::value &&
1592227825Stheraven   !__is_random_access_iterator<_InputIterator>::value,
1593227825Stheraven    _OutputIterator
1594227825Stheraven>::type
1595227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1596227825Stheraven{
1597227825Stheraven    if (__n > 0)
1598227825Stheraven    {
1599227825Stheraven        *__result = *__first;
1600227825Stheraven        ++__result;
1601227825Stheraven        for (--__n; __n > 0; --__n)
1602227825Stheraven        {
1603227825Stheraven            ++__first;
1604227825Stheraven            *__result = *__first;
1605227825Stheraven            ++__result;
1606227825Stheraven        }
1607227825Stheraven    }
1608227825Stheraven    return __result;
1609227825Stheraven}
1610227825Stheraven
1611227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator>
1612227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1613227825Stheraventypename enable_if
1614227825Stheraven<
1615227825Stheraven    __is_random_access_iterator<_InputIterator>::value,
1616227825Stheraven    _OutputIterator
1617227825Stheraven>::type
1618227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1619227825Stheraven{
1620227825Stheraven    return _VSTD::copy(__first, __first + __n, __result);
1621227825Stheraven}
1622227825Stheraven
1623227825Stheraven// move
1624227825Stheraven
1625227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1626227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1627227825Stheraven_OutputIterator
1628227825Stheraven__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1629227825Stheraven{
1630227825Stheraven    for (; __first != __last; ++__first, ++__result)
1631227825Stheraven        *__result = _VSTD::move(*__first);
1632227825Stheraven    return __result;
1633227825Stheraven}
1634227825Stheraven
1635227825Stheraventemplate <class _Tp, class _Up>
1636227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1637227825Stheraventypename enable_if
1638227825Stheraven<
1639227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1640227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1641227825Stheraven    _Up*
1642227825Stheraven>::type
1643227825Stheraven__move(_Tp* __first, _Tp* __last, _Up* __result)
1644227825Stheraven{
1645227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1646227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1647227825Stheraven    return __result + __n;
1648227825Stheraven}
1649227825Stheraven
1650227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1651227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1652227825Stheraven_OutputIterator
1653227825Stheravenmove(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1654227825Stheraven{
1655227825Stheraven    return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1656227825Stheraven}
1657227825Stheraven
1658227825Stheraven// move_backward
1659227825Stheraven
1660227825Stheraventemplate <class _InputIterator, class _OutputIterator>
1661227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1662227825Stheraven_OutputIterator
1663227825Stheraven__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1664227825Stheraven{
1665227825Stheraven    while (__first != __last)
1666227825Stheraven        *--__result = _VSTD::move(*--__last);
1667227825Stheraven    return __result;
1668227825Stheraven}
1669227825Stheraven
1670227825Stheraventemplate <class _Tp, class _Up>
1671227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1672227825Stheraventypename enable_if
1673227825Stheraven<
1674227825Stheraven    is_same<typename remove_const<_Tp>::type, _Up>::value &&
1675227825Stheraven    is_trivially_copy_assignable<_Up>::value,
1676227825Stheraven    _Up*
1677227825Stheraven>::type
1678227825Stheraven__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1679227825Stheraven{
1680227825Stheraven    const size_t __n = static_cast<size_t>(__last - __first);
1681227825Stheraven    __result -= __n;
1682227825Stheraven    _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1683227825Stheraven    return __result;
1684227825Stheraven}
1685227825Stheraven
1686227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2>
1687227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1688227825Stheraven_BidirectionalIterator2
1689227825Stheravenmove_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1690227825Stheraven              _BidirectionalIterator2 __result)
1691227825Stheraven{
1692227825Stheraven    return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1693227825Stheraven}
1694227825Stheraven
1695227825Stheraven// iter_swap
1696227825Stheraven
1697227825Stheraven// moved to <type_traits> for better swap / noexcept support
1698227825Stheraven
1699227825Stheraven// transform
1700227825Stheraven
1701227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1702227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1703227825Stheraven_OutputIterator
1704227825Stheraventransform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1705227825Stheraven{
1706227825Stheraven    for (; __first != __last; ++__first, ++__result)
1707227825Stheraven        *__result = __op(*__first);
1708227825Stheraven    return __result;
1709227825Stheraven}
1710227825Stheraven
1711227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1712227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1713227825Stheraven_OutputIterator
1714227825Stheraventransform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1715227825Stheraven          _OutputIterator __result, _BinaryOperation __binary_op)
1716227825Stheraven{
1717227825Stheraven    for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
1718227825Stheraven        *__result = __binary_op(*__first1, *__first2);
1719227825Stheraven    return __result;
1720227825Stheraven}
1721227825Stheraven
1722227825Stheraven// replace
1723227825Stheraven
1724227825Stheraventemplate <class _ForwardIterator, class _Tp>
1725227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1726227825Stheravenvoid
1727227825Stheravenreplace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1728227825Stheraven{
1729227825Stheraven    for (; __first != __last; ++__first)
1730227825Stheraven        if (*__first == __old_value)
1731227825Stheraven            *__first = __new_value;
1732227825Stheraven}
1733227825Stheraven
1734227825Stheraven// replace_if
1735227825Stheraven
1736227825Stheraventemplate <class _ForwardIterator, class _Predicate, class _Tp>
1737227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1738227825Stheravenvoid
1739227825Stheravenreplace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1740227825Stheraven{
1741227825Stheraven    for (; __first != __last; ++__first)
1742227825Stheraven        if (__pred(*__first))
1743227825Stheraven            *__first = __new_value;
1744227825Stheraven}
1745227825Stheraven
1746227825Stheraven// replace_copy
1747227825Stheraven
1748227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp>
1749227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1750227825Stheraven_OutputIterator
1751227825Stheravenreplace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1752227825Stheraven             const _Tp& __old_value, const _Tp& __new_value)
1753227825Stheraven{
1754227825Stheraven    for (; __first != __last; ++__first, ++__result)
1755227825Stheraven        if (*__first == __old_value)
1756227825Stheraven            *__result = __new_value;
1757227825Stheraven        else
1758227825Stheraven            *__result = *__first;
1759227825Stheraven    return __result;
1760227825Stheraven}
1761227825Stheraven
1762227825Stheraven// replace_copy_if
1763227825Stheraven
1764227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
1765227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1766227825Stheraven_OutputIterator
1767227825Stheravenreplace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1768227825Stheraven                _Predicate __pred, const _Tp& __new_value)
1769227825Stheraven{
1770227825Stheraven    for (; __first != __last; ++__first, ++__result)
1771227825Stheraven        if (__pred(*__first))
1772227825Stheraven            *__result = __new_value;
1773227825Stheraven        else
1774227825Stheraven            *__result = *__first;
1775227825Stheraven    return __result;
1776227825Stheraven}
1777227825Stheraven
1778227825Stheraven// fill_n
1779227825Stheraven
1780227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp>
1781227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1782227825Stheraven_OutputIterator
1783227825Stheraven__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, false_type)
1784227825Stheraven{
1785227825Stheraven    for (; __n > 0; ++__first, --__n)
1786227825Stheraven        *__first = __value_;
1787227825Stheraven    return __first;
1788227825Stheraven}
1789227825Stheraven
1790227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp>
1791227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1792227825Stheraven_OutputIterator
1793227825Stheraven__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, true_type)
1794227825Stheraven{
1795227825Stheraven    if (__n > 0)
1796227825Stheraven        _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
1797227825Stheraven    return __first + __n;
1798227825Stheraven}
1799227825Stheraven
1800227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp>
1801227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1802227825Stheraven_OutputIterator
1803227825Stheravenfill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
1804227825Stheraven{
1805227825Stheraven   return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool,
1806227825Stheraven                                              is_pointer<_OutputIterator>::value &&
1807227825Stheraven                                              is_trivially_copy_assignable<_Tp>::value     &&
1808227825Stheraven                                              sizeof(_Tp) == 1>());
1809227825Stheraven}
1810227825Stheraven
1811227825Stheraven// fill
1812227825Stheraven
1813227825Stheraventemplate <class _ForwardIterator, class _Tp>
1814227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1815227825Stheravenvoid
1816227825Stheraven__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
1817227825Stheraven{
1818227825Stheraven    for (; __first != __last; ++__first)
1819227825Stheraven        *__first = __value_;
1820227825Stheraven}
1821227825Stheraven
1822227825Stheraventemplate <class _RandomAccessIterator, class _Tp>
1823227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1824227825Stheravenvoid
1825227825Stheraven__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
1826227825Stheraven{
1827227825Stheraven    _VSTD::fill_n(__first, __last - __first, __value_);
1828227825Stheraven}
1829227825Stheraven
1830227825Stheraventemplate <class _ForwardIterator, class _Tp>
1831227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1832227825Stheravenvoid
1833227825Stheravenfill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
1834227825Stheraven{
1835227825Stheraven    _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
1836227825Stheraven}
1837227825Stheraven
1838227825Stheraven// generate
1839227825Stheraven
1840227825Stheraventemplate <class _ForwardIterator, class _Generator>
1841227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1842227825Stheravenvoid
1843227825Stheravengenerate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1844227825Stheraven{
1845227825Stheraven    for (; __first != __last; ++__first)
1846227825Stheraven        *__first = __gen();
1847227825Stheraven}
1848227825Stheraven
1849227825Stheraven// generate_n
1850227825Stheraven
1851227825Stheraventemplate <class _OutputIterator, class _Size, class _Generator>
1852227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1853227825Stheraven_OutputIterator
1854227825Stheravengenerate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1855227825Stheraven{
1856227825Stheraven    for (; __n > 0; ++__first, --__n)
1857227825Stheraven        *__first = __gen();
1858227825Stheraven    return __first;
1859227825Stheraven}
1860227825Stheraven
1861227825Stheraven// remove
1862227825Stheraven
1863227825Stheraventemplate <class _ForwardIterator, class _Tp>
1864227825Stheraven_ForwardIterator
1865227825Stheravenremove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
1866227825Stheraven{
1867227825Stheraven    __first = _VSTD::find(__first, __last, __value_);
1868227825Stheraven    if (__first != __last)
1869227825Stheraven    {
1870227825Stheraven        _ForwardIterator __i = __first;
1871227825Stheraven        while (++__i != __last)
1872227825Stheraven        {
1873227825Stheraven            if (!(*__i == __value_))
1874227825Stheraven            {
1875227825Stheraven                *__first = _VSTD::move(*__i);
1876227825Stheraven                ++__first;
1877227825Stheraven            }
1878227825Stheraven        }
1879227825Stheraven    }
1880227825Stheraven    return __first;
1881227825Stheraven}
1882227825Stheraven
1883227825Stheraven// remove_if
1884227825Stheraven
1885227825Stheraventemplate <class _ForwardIterator, class _Predicate>
1886227825Stheraven_ForwardIterator
1887227825Stheravenremove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1888227825Stheraven{
1889227825Stheraven    __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
1890227825Stheraven                           (__first, __last, __pred);
1891227825Stheraven    if (__first != __last)
1892227825Stheraven    {
1893227825Stheraven        _ForwardIterator __i = __first;
1894227825Stheraven        while (++__i != __last)
1895227825Stheraven        {
1896227825Stheraven            if (!__pred(*__i))
1897227825Stheraven            {
1898227825Stheraven                *__first = _VSTD::move(*__i);
1899227825Stheraven                ++__first;
1900227825Stheraven            }
1901227825Stheraven        }
1902227825Stheraven    }
1903227825Stheraven    return __first;
1904227825Stheraven}
1905227825Stheraven
1906227825Stheraven// remove_copy
1907227825Stheraven
1908227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp>
1909227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1910227825Stheraven_OutputIterator
1911227825Stheravenremove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
1912227825Stheraven{
1913227825Stheraven    for (; __first != __last; ++__first)
1914227825Stheraven    {
1915227825Stheraven        if (!(*__first == __value_))
1916227825Stheraven        {
1917227825Stheraven            *__result = *__first;
1918227825Stheraven            ++__result;
1919227825Stheraven        }
1920227825Stheraven    }
1921227825Stheraven    return __result;
1922227825Stheraven}
1923227825Stheraven
1924227825Stheraven// remove_copy_if
1925227825Stheraven
1926227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate>
1927227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1928227825Stheraven_OutputIterator
1929227825Stheravenremove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
1930227825Stheraven{
1931227825Stheraven    for (; __first != __last; ++__first)
1932227825Stheraven    {
1933227825Stheraven        if (!__pred(*__first))
1934227825Stheraven        {
1935227825Stheraven            *__result = *__first;
1936227825Stheraven            ++__result;
1937227825Stheraven        }
1938227825Stheraven    }
1939227825Stheraven    return __result;
1940227825Stheraven}
1941227825Stheraven
1942227825Stheraven// unique
1943227825Stheraven
1944227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate>
1945227825Stheraven_ForwardIterator
1946227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1947227825Stheraven{
1948227825Stheraven    __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
1949227825Stheraven                                 (__first, __last, __pred);
1950227825Stheraven    if (__first != __last)
1951227825Stheraven    {
1952227825Stheraven        // ...  a  a  ?  ...
1953227825Stheraven        //      f     i
1954227825Stheraven        _ForwardIterator __i = __first;
1955227825Stheraven        for (++__i; ++__i != __last;)
1956227825Stheraven            if (!__pred(*__first, *__i))
1957227825Stheraven                *++__first = _VSTD::move(*__i);
1958227825Stheraven        ++__first;
1959227825Stheraven    }
1960227825Stheraven    return __first;
1961227825Stheraven}
1962227825Stheraven
1963227825Stheraventemplate <class _ForwardIterator>
1964227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1965227825Stheraven_ForwardIterator
1966227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last)
1967227825Stheraven{
1968227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1969227825Stheraven    return _VSTD::unique(__first, __last, __equal_to<__v>());
1970227825Stheraven}
1971227825Stheraven
1972227825Stheraven// unique_copy
1973227825Stheraven
1974227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
1975227825Stheraven_OutputIterator
1976227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1977227825Stheraven              input_iterator_tag, output_iterator_tag)
1978227825Stheraven{
1979227825Stheraven    if (__first != __last)
1980227825Stheraven    {
1981227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t(*__first);
1982227825Stheraven        *__result = __t;
1983227825Stheraven        ++__result;
1984227825Stheraven        while (++__first != __last)
1985227825Stheraven        {
1986227825Stheraven            if (!__pred(__t, *__first))
1987227825Stheraven            {
1988227825Stheraven                __t = *__first;
1989227825Stheraven                *__result = __t;
1990227825Stheraven                ++__result;
1991227825Stheraven            }
1992227825Stheraven        }
1993227825Stheraven    }
1994227825Stheraven    return __result;
1995227825Stheraven}
1996227825Stheraven
1997227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
1998227825Stheraven_OutputIterator
1999227825Stheraven__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2000227825Stheraven              forward_iterator_tag, output_iterator_tag)
2001227825Stheraven{
2002227825Stheraven    if (__first != __last)
2003227825Stheraven    {
2004227825Stheraven        _ForwardIterator __i = __first;
2005227825Stheraven        *__result = *__i;
2006227825Stheraven        ++__result;
2007227825Stheraven        while (++__first != __last)
2008227825Stheraven        {
2009227825Stheraven            if (!__pred(*__i, *__first))
2010227825Stheraven            {
2011227825Stheraven                *__result = *__first;
2012227825Stheraven                ++__result;
2013227825Stheraven                __i = __first;
2014227825Stheraven            }
2015227825Stheraven        }
2016227825Stheraven    }
2017227825Stheraven    return __result;
2018227825Stheraven}
2019227825Stheraven
2020227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2021227825Stheraven_ForwardIterator
2022227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2023227825Stheraven              input_iterator_tag, forward_iterator_tag)
2024227825Stheraven{
2025227825Stheraven    if (__first != __last)
2026227825Stheraven    {
2027227825Stheraven        *__result = *__first;
2028227825Stheraven        while (++__first != __last)
2029227825Stheraven            if (!__pred(*__result, *__first))
2030227825Stheraven                *++__result = *__first;
2031227825Stheraven        ++__result;
2032227825Stheraven    }
2033227825Stheraven    return __result;
2034227825Stheraven}
2035227825Stheraven
2036227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2037227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2038227825Stheraven_OutputIterator
2039227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2040227825Stheraven{
2041227825Stheraven    return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
2042227825Stheraven                              (__first, __last, __result, __pred,
2043227825Stheraven                               typename iterator_traits<_InputIterator>::iterator_category(),
2044227825Stheraven                               typename iterator_traits<_OutputIterator>::iterator_category());
2045227825Stheraven}
2046227825Stheraven
2047227825Stheraventemplate <class _InputIterator, class _OutputIterator>
2048227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2049227825Stheraven_OutputIterator
2050227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2051227825Stheraven{
2052227825Stheraven    typedef typename iterator_traits<_InputIterator>::value_type __v;
2053227825Stheraven    return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
2054227825Stheraven}
2055227825Stheraven
2056227825Stheraven// reverse
2057227825Stheraven
2058227825Stheraventemplate <class _BidirectionalIterator>
2059227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2060227825Stheravenvoid
2061227825Stheraven__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2062227825Stheraven{
2063227825Stheraven    while (__first != __last)
2064227825Stheraven    {
2065227825Stheraven        if (__first == --__last)
2066227825Stheraven            break;
2067227825Stheraven        swap(*__first, *__last);
2068227825Stheraven        ++__first;
2069227825Stheraven    }
2070227825Stheraven}
2071227825Stheraven
2072227825Stheraventemplate <class _RandomAccessIterator>
2073227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2074227825Stheravenvoid
2075227825Stheraven__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2076227825Stheraven{
2077227825Stheraven    if (__first != __last)
2078227825Stheraven        for (; __first < --__last; ++__first)
2079227825Stheraven            swap(*__first, *__last);
2080227825Stheraven}
2081227825Stheraven
2082227825Stheraventemplate <class _BidirectionalIterator>
2083227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2084227825Stheravenvoid
2085227825Stheravenreverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2086227825Stheraven{
2087227825Stheraven    _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
2088227825Stheraven}
2089227825Stheraven
2090227825Stheraven// reverse_copy
2091227825Stheraven
2092227825Stheraventemplate <class _BidirectionalIterator, class _OutputIterator>
2093227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2094227825Stheraven_OutputIterator
2095227825Stheravenreverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2096227825Stheraven{
2097227825Stheraven    for (; __first != __last; ++__result)
2098227825Stheraven        *__result = *--__last;
2099227825Stheraven    return __result;
2100227825Stheraven}
2101227825Stheraven
2102227825Stheraven// rotate
2103227825Stheraven
2104227825Stheraventemplate <class _ForwardIterator>
2105227825Stheraven_ForwardIterator
2106241903Sdim__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
2107227825Stheraven{
2108241903Sdim    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2109241903Sdim    value_type __tmp = _VSTD::move(*__first);
2110241903Sdim    _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2111241903Sdim    *__lm1 = _VSTD::move(__tmp);
2112241903Sdim    return __lm1;
2113241903Sdim}
2114241903Sdim
2115241903Sdimtemplate <class _BidirectionalIterator>
2116241903Sdim_BidirectionalIterator
2117241903Sdim__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2118241903Sdim{
2119241903Sdim    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2120241903Sdim    _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2121241903Sdim    value_type __tmp = _VSTD::move(*__lm1);
2122241903Sdim    _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2123241903Sdim    *__first = _VSTD::move(__tmp);
2124241903Sdim    return __fp1;
2125241903Sdim}
2126241903Sdim
2127241903Sdimtemplate <class _ForwardIterator>
2128241903Sdim_ForwardIterator
2129241903Sdim__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2130241903Sdim{
2131227825Stheraven    _ForwardIterator __i = __middle;
2132227825Stheraven    while (true)
2133227825Stheraven    {
2134227825Stheraven        swap(*__first, *__i);
2135227825Stheraven        ++__first;
2136227825Stheraven        if (++__i == __last)
2137227825Stheraven            break;
2138227825Stheraven        if (__first == __middle)
2139227825Stheraven            __middle = __i;
2140227825Stheraven    }
2141227825Stheraven    _ForwardIterator __r = __first;
2142227825Stheraven    if (__first != __middle)
2143227825Stheraven    {
2144227825Stheraven        __i = __middle;
2145227825Stheraven        while (true)
2146227825Stheraven        {
2147227825Stheraven            swap(*__first, *__i);
2148227825Stheraven            ++__first;
2149227825Stheraven            if (++__i == __last)
2150227825Stheraven            {
2151227825Stheraven                if (__first == __middle)
2152227825Stheraven                    break;
2153227825Stheraven                __i = __middle;
2154227825Stheraven            }
2155227825Stheraven            else if (__first == __middle)
2156227825Stheraven                __middle = __i;
2157227825Stheraven        }
2158227825Stheraven    }
2159227825Stheraven    return __r;
2160227825Stheraven}
2161227825Stheraven
2162227825Stheraventemplate<typename _Integral>
2163227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2164227825Stheraven_Integral
2165227825Stheraven__gcd(_Integral __x, _Integral __y)
2166227825Stheraven{
2167227825Stheraven    do
2168227825Stheraven    {
2169227825Stheraven        _Integral __t = __x % __y;
2170227825Stheraven        __x = __y;
2171227825Stheraven        __y = __t;
2172227825Stheraven    } while (__y);
2173227825Stheraven    return __x;
2174227825Stheraven}
2175227825Stheraven
2176227825Stheraventemplate<typename _RandomAccessIterator>
2177227825Stheraven_RandomAccessIterator
2178241903Sdim__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
2179227825Stheraven{
2180227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2181227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2182227825Stheraven
2183227825Stheraven    const difference_type __m1 = __middle - __first;
2184227825Stheraven    const difference_type __m2 = __last - __middle;
2185227825Stheraven    if (__m1 == __m2)
2186227825Stheraven    {
2187227825Stheraven        _VSTD::swap_ranges(__first, __middle, __middle);
2188227825Stheraven        return __middle;
2189227825Stheraven    }
2190241903Sdim    const difference_type __g = _VSTD::__gcd(__m1, __m2);
2191227825Stheraven    for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2192227825Stheraven    {
2193241903Sdim        value_type __t(_VSTD::move(*--__p));
2194227825Stheraven        _RandomAccessIterator __p1 = __p;
2195227825Stheraven        _RandomAccessIterator __p2 = __p1 + __m1;
2196227825Stheraven        do
2197227825Stheraven        {
2198241903Sdim            *__p1 = _VSTD::move(*__p2);
2199227825Stheraven            __p1 = __p2;
2200227825Stheraven            const difference_type __d = __last - __p2;
2201227825Stheraven            if (__m1 < __d)
2202227825Stheraven                __p2 += __m1;
2203227825Stheraven            else
2204227825Stheraven                __p2 = __first + (__m1 - __d);
2205227825Stheraven        } while (__p2 != __p);
2206241903Sdim        *__p1 = _VSTD::move(__t);
2207227825Stheraven    }
2208227825Stheraven    return __first + __m2;
2209227825Stheraven}
2210227825Stheraven
2211227825Stheraventemplate <class _ForwardIterator>
2212227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2213227825Stheraven_ForwardIterator
2214241903Sdim__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2215241903Sdim         _VSTD::forward_iterator_tag)
2216241903Sdim{
2217241903Sdim    typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2218241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2219241903Sdim    {
2220241903Sdim        if (_VSTD::next(__first) == __middle)
2221241903Sdim            return _VSTD::__rotate_left(__first, __last);
2222241903Sdim    }
2223241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2224241903Sdim}
2225241903Sdim
2226241903Sdimtemplate <class _BidirectionalIterator>
2227241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2228241903Sdim_BidirectionalIterator
2229241903Sdim__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2230241903Sdim         _VSTD::bidirectional_iterator_tag)
2231241903Sdim{
2232241903Sdim    typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2233241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2234241903Sdim    {
2235241903Sdim        if (_VSTD::next(__first) == __middle)
2236241903Sdim            return _VSTD::__rotate_left(__first, __last);
2237241903Sdim        if (_VSTD::next(__middle) == __last)
2238241903Sdim            return _VSTD::__rotate_right(__first, __last);
2239241903Sdim    }
2240241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2241241903Sdim}
2242241903Sdim
2243241903Sdimtemplate <class _RandomAccessIterator>
2244241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2245241903Sdim_RandomAccessIterator
2246241903Sdim__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2247241903Sdim         _VSTD::random_access_iterator_tag)
2248241903Sdim{
2249241903Sdim    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2250241903Sdim    if (_VSTD::is_trivially_move_assignable<value_type>::value)
2251241903Sdim    {
2252241903Sdim        if (_VSTD::next(__first) == __middle)
2253241903Sdim            return _VSTD::__rotate_left(__first, __last);
2254241903Sdim        if (_VSTD::next(__middle) == __last)
2255241903Sdim            return _VSTD::__rotate_right(__first, __last);
2256241903Sdim        return _VSTD::__rotate_gcd(__first, __middle, __last);
2257241903Sdim    }
2258241903Sdim    return _VSTD::__rotate_forward(__first, __middle, __last);
2259241903Sdim}
2260241903Sdim
2261241903Sdimtemplate <class _ForwardIterator>
2262241903Sdiminline _LIBCPP_INLINE_VISIBILITY
2263241903Sdim_ForwardIterator
2264227825Stheravenrotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2265227825Stheraven{
2266241903Sdim    if (__first == __middle)
2267241903Sdim        return __last;
2268241903Sdim    if (__middle == __last)
2269241903Sdim        return __first;
2270227825Stheraven    return _VSTD::__rotate(__first, __middle, __last,
2271241903Sdim                           typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
2272227825Stheraven}
2273227825Stheraven
2274227825Stheraven// rotate_copy
2275227825Stheraven
2276227825Stheraventemplate <class _ForwardIterator, class _OutputIterator>
2277227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2278227825Stheraven_OutputIterator
2279227825Stheravenrotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2280227825Stheraven{
2281227825Stheraven    return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
2282227825Stheraven}
2283227825Stheraven
2284227825Stheraven// min_element
2285227825Stheraven
2286227825Stheraventemplate <class _ForwardIterator, class _Compare>
2287227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2288227825Stheraven_ForwardIterator
2289227825Stheravenmin_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2290227825Stheraven{
2291227825Stheraven    if (__first != __last)
2292227825Stheraven    {
2293227825Stheraven        _ForwardIterator __i = __first;
2294227825Stheraven        while (++__i != __last)
2295227825Stheraven            if (__comp(*__i, *__first))
2296227825Stheraven                __first = __i;
2297227825Stheraven    }
2298227825Stheraven    return __first;
2299227825Stheraven}
2300227825Stheraven
2301227825Stheraventemplate <class _ForwardIterator>
2302227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2303227825Stheraven_ForwardIterator
2304227825Stheravenmin_element(_ForwardIterator __first, _ForwardIterator __last)
2305227825Stheraven{
2306227825Stheraven    return _VSTD::min_element(__first, __last,
2307227825Stheraven              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2308227825Stheraven}
2309227825Stheraven
2310227825Stheraven// min
2311227825Stheraven
2312227825Stheraventemplate <class _Tp, class _Compare>
2313227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2314227825Stheravenconst _Tp&
2315227825Stheravenmin(const _Tp& __a, const _Tp& __b, _Compare __comp)
2316227825Stheraven{
2317227825Stheraven    return __comp(__b, __a) ? __b : __a;
2318227825Stheraven}
2319227825Stheraven
2320227825Stheraventemplate <class _Tp>
2321227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2322227825Stheravenconst _Tp&
2323227825Stheravenmin(const _Tp& __a, const _Tp& __b)
2324227825Stheraven{
2325227825Stheraven    return _VSTD::min(__a, __b, __less<_Tp>());
2326227825Stheraven}
2327227825Stheraven
2328227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2329227825Stheraven
2330227825Stheraventemplate<class _Tp, class _Compare>
2331227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2332227825Stheraven_Tp
2333227825Stheravenmin(initializer_list<_Tp> __t, _Compare __comp)
2334227825Stheraven{
2335227825Stheraven    return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
2336227825Stheraven}
2337227825Stheraven
2338227825Stheraventemplate<class _Tp>
2339227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2340227825Stheraven_Tp
2341227825Stheravenmin(initializer_list<_Tp> __t)
2342227825Stheraven{
2343227825Stheraven    return *_VSTD::min_element(__t.begin(), __t.end());
2344227825Stheraven}
2345227825Stheraven
2346227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2347227825Stheraven
2348227825Stheraven// max_element
2349227825Stheraven
2350227825Stheraventemplate <class _ForwardIterator, class _Compare>
2351227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2352227825Stheraven_ForwardIterator
2353227825Stheravenmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2354227825Stheraven{
2355227825Stheraven    if (__first != __last)
2356227825Stheraven    {
2357227825Stheraven        _ForwardIterator __i = __first;
2358227825Stheraven        while (++__i != __last)
2359227825Stheraven            if (__comp(*__first, *__i))
2360227825Stheraven                __first = __i;
2361227825Stheraven    }
2362227825Stheraven    return __first;
2363227825Stheraven}
2364227825Stheraven
2365227825Stheraventemplate <class _ForwardIterator>
2366227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2367227825Stheraven_ForwardIterator
2368227825Stheravenmax_element(_ForwardIterator __first, _ForwardIterator __last)
2369227825Stheraven{
2370227825Stheraven    return _VSTD::max_element(__first, __last,
2371227825Stheraven              __less<typename iterator_traits<_ForwardIterator>::value_type>());
2372227825Stheraven}
2373227825Stheraven
2374227825Stheraven// max
2375227825Stheraven
2376227825Stheraventemplate <class _Tp, class _Compare>
2377227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2378227825Stheravenconst _Tp&
2379227825Stheravenmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2380227825Stheraven{
2381227825Stheraven    return __comp(__a, __b) ? __b : __a;
2382227825Stheraven}
2383227825Stheraven
2384227825Stheraventemplate <class _Tp>
2385227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2386227825Stheravenconst _Tp&
2387227825Stheravenmax(const _Tp& __a, const _Tp& __b)
2388227825Stheraven{
2389227825Stheraven    return _VSTD::max(__a, __b, __less<_Tp>());
2390227825Stheraven}
2391227825Stheraven
2392227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2393227825Stheraven
2394227825Stheraventemplate<class _Tp, class _Compare>
2395227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2396227825Stheraven_Tp
2397227825Stheravenmax(initializer_list<_Tp> __t, _Compare __comp)
2398227825Stheraven{
2399227825Stheraven    return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
2400227825Stheraven}
2401227825Stheraven
2402227825Stheraventemplate<class _Tp>
2403227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2404227825Stheraven_Tp
2405227825Stheravenmax(initializer_list<_Tp> __t)
2406227825Stheraven{
2407227825Stheraven    return *_VSTD::max_element(__t.begin(), __t.end());
2408227825Stheraven}
2409227825Stheraven
2410227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2411227825Stheraven
2412227825Stheraven// minmax_element
2413227825Stheraven
2414227825Stheraventemplate <class _ForwardIterator, class _Compare>
2415227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator>
2416227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2417227825Stheraven{
2418227825Stheraven  std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2419227825Stheraven  if (__first != __last)
2420227825Stheraven  {
2421227825Stheraven      if (++__first != __last)
2422227825Stheraven      {
2423227825Stheraven          if (__comp(*__first, *__result.first))
2424227825Stheraven              __result.first = __first;
2425227825Stheraven          else
2426227825Stheraven              __result.second = __first;
2427227825Stheraven          while (++__first != __last)
2428227825Stheraven          {
2429227825Stheraven              _ForwardIterator __i = __first;
2430227825Stheraven              if (++__first == __last)
2431227825Stheraven              {
2432227825Stheraven                  if (__comp(*__i, *__result.first))
2433227825Stheraven                      __result.first = __i;
2434227825Stheraven                  else if (!__comp(*__i, *__result.second))
2435227825Stheraven                      __result.second = __i;
2436227825Stheraven                  break;
2437227825Stheraven              }
2438227825Stheraven              else
2439227825Stheraven              {
2440227825Stheraven                  if (__comp(*__first, *__i))
2441227825Stheraven                  {
2442227825Stheraven                      if (__comp(*__first, *__result.first))
2443227825Stheraven                          __result.first = __first;
2444227825Stheraven                      if (!__comp(*__i, *__result.second))
2445227825Stheraven                          __result.second = __i;
2446227825Stheraven                  }
2447227825Stheraven                  else
2448227825Stheraven                  {
2449227825Stheraven                      if (__comp(*__i, *__result.first))
2450227825Stheraven                          __result.first = __i;
2451227825Stheraven                      if (!__comp(*__first, *__result.second))
2452227825Stheraven                          __result.second = __first;
2453227825Stheraven                  }
2454227825Stheraven              }
2455227825Stheraven          }
2456227825Stheraven      }
2457227825Stheraven  }
2458227825Stheraven  return __result;
2459227825Stheraven}
2460227825Stheraven
2461227825Stheraventemplate <class _ForwardIterator>
2462227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2463227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator>
2464227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last)
2465227825Stheraven{
2466227825Stheraven    return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
2467227825Stheraven}
2468227825Stheraven
2469227825Stheraven// minmax
2470227825Stheraven
2471227825Stheraventemplate<class _Tp, class _Compare>
2472227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2473227825Stheravenpair<const _Tp&, const _Tp&>
2474227825Stheravenminmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2475227825Stheraven{
2476227825Stheraven    return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2477227825Stheraven                              pair<const _Tp&, const _Tp&>(__a, __b);
2478227825Stheraven}
2479227825Stheraven
2480227825Stheraventemplate<class _Tp>
2481227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2482227825Stheravenpair<const _Tp&, const _Tp&>
2483227825Stheravenminmax(const _Tp& __a, const _Tp& __b)
2484227825Stheraven{
2485227825Stheraven    return _VSTD::minmax(__a, __b, __less<_Tp>());
2486227825Stheraven}
2487227825Stheraven
2488227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2489227825Stheraven
2490227825Stheraventemplate<class _Tp>
2491227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2492227825Stheravenpair<_Tp, _Tp>
2493227825Stheravenminmax(initializer_list<_Tp> __t)
2494227825Stheraven{
2495227825Stheraven    pair<const _Tp*, const _Tp*> __p =
2496227825Stheraven                                   _VSTD::minmax_element(__t.begin(), __t.end());
2497227825Stheraven    return pair<_Tp, _Tp>(*__p.first, *__p.second);
2498227825Stheraven}
2499227825Stheraven
2500227825Stheraventemplate<class _Tp, class _Compare>
2501227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2502227825Stheravenpair<_Tp, _Tp>
2503227825Stheravenminmax(initializer_list<_Tp> __t, _Compare __comp)
2504227825Stheraven{
2505227825Stheraven    pair<const _Tp*, const _Tp*> __p =
2506227825Stheraven                           _VSTD::minmax_element(__t.begin(), __t.end(), __comp);
2507227825Stheraven    return pair<_Tp, _Tp>(*__p.first, *__p.second);
2508227825Stheraven}
2509227825Stheraven
2510227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2511227825Stheraven
2512227825Stheraven// random_shuffle
2513227825Stheraven
2514227825Stheraven// __independent_bits_engine
2515227825Stheraven
2516232950Stheraventemplate <unsigned long long _Xp, size_t _Rp>
2517227825Stheravenstruct __log2_imp
2518227825Stheraven{
2519232950Stheraven    static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2520232950Stheraven                                           : __log2_imp<_Xp, _Rp - 1>::value;
2521227825Stheraven};
2522227825Stheraven
2523232950Stheraventemplate <unsigned long long _Xp>
2524232950Stheravenstruct __log2_imp<_Xp, 0>
2525227825Stheraven{
2526227825Stheraven    static const size_t value = 0;
2527227825Stheraven};
2528227825Stheraven
2529232950Stheraventemplate <size_t _Rp>
2530232950Stheravenstruct __log2_imp<0, _Rp>
2531227825Stheraven{
2532232950Stheraven    static const size_t value = _Rp + 1;
2533227825Stheraven};
2534227825Stheraven
2535232950Stheraventemplate <class _UI, _UI _Xp>
2536227825Stheravenstruct __log2
2537227825Stheraven{
2538232950Stheraven    static const size_t value = __log2_imp<_Xp,
2539227825Stheraven                                         sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2540227825Stheraven};
2541227825Stheraven
2542227825Stheraventemplate<class _Engine, class _UIntType>
2543227825Stheravenclass __independent_bits_engine
2544227825Stheraven{
2545227825Stheravenpublic:
2546227825Stheraven    // types
2547227825Stheraven    typedef _UIntType result_type;
2548227825Stheraven
2549227825Stheravenprivate:
2550227825Stheraven    typedef typename _Engine::result_type _Engine_result_type;
2551227825Stheraven    typedef typename conditional
2552227825Stheraven        <
2553227825Stheraven            sizeof(_Engine_result_type) <= sizeof(result_type),
2554227825Stheraven                result_type,
2555227825Stheraven                _Engine_result_type
2556227825Stheraven        >::type _Working_result_type;
2557227825Stheraven
2558227825Stheraven    _Engine& __e_;
2559227825Stheraven    size_t __w_;
2560227825Stheraven    size_t __w0_;
2561227825Stheraven    size_t __n_;
2562227825Stheraven    size_t __n0_;
2563227825Stheraven    _Working_result_type __y0_;
2564227825Stheraven    _Working_result_type __y1_;
2565227825Stheraven    _Engine_result_type __mask0_;
2566227825Stheraven    _Engine_result_type __mask1_;
2567227825Stheraven
2568234976Stheraven#ifdef _LIBCPP_HAS_NO_CONSTEXPR
2569232950Stheraven    static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
2570234976Stheraven                                          + _Working_result_type(1);
2571234976Stheraven#else
2572234976Stheraven    static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2573234976Stheraven                                                      + _Working_result_type(1);
2574234976Stheraven#endif
2575234976Stheraven    static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2576234976Stheraven    static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2577234976Stheraven    static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2578227825Stheraven
2579227825Stheravenpublic:
2580227825Stheraven    // constructors and seeding functions
2581227825Stheraven    __independent_bits_engine(_Engine& __e, size_t __w);
2582227825Stheraven
2583227825Stheraven    // generating functions
2584232950Stheraven    result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
2585227825Stheraven
2586227825Stheravenprivate:
2587227825Stheraven    result_type __eval(false_type);
2588227825Stheraven    result_type __eval(true_type);
2589227825Stheraven};
2590227825Stheraven
2591227825Stheraventemplate<class _Engine, class _UIntType>
2592227825Stheraven__independent_bits_engine<_Engine, _UIntType>
2593227825Stheraven    ::__independent_bits_engine(_Engine& __e, size_t __w)
2594227825Stheraven        : __e_(__e),
2595227825Stheraven          __w_(__w)
2596227825Stheraven{
2597227825Stheraven    __n_ = __w_ / __m + (__w_ % __m != 0);
2598227825Stheraven    __w0_ = __w_ / __n_;
2599232950Stheraven    if (_Rp == 0)
2600232950Stheraven        __y0_ = _Rp;
2601227825Stheraven    else if (__w0_ < _WDt)
2602232950Stheraven        __y0_ = (_Rp >> __w0_) << __w0_;
2603227825Stheraven    else
2604227825Stheraven        __y0_ = 0;
2605232950Stheraven    if (_Rp - __y0_ > __y0_ / __n_)
2606227825Stheraven    {
2607227825Stheraven        ++__n_;
2608227825Stheraven        __w0_ = __w_ / __n_;
2609227825Stheraven        if (__w0_ < _WDt)
2610232950Stheraven            __y0_ = (_Rp >> __w0_) << __w0_;
2611227825Stheraven        else
2612227825Stheraven            __y0_ = 0;
2613227825Stheraven    }
2614227825Stheraven    __n0_ = __n_ - __w_ % __n_;
2615227825Stheraven    if (__w0_ < _WDt - 1)
2616232950Stheraven        __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
2617227825Stheraven    else
2618227825Stheraven        __y1_ = 0;
2619227825Stheraven    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2620227825Stheraven                          _Engine_result_type(0);
2621227825Stheraven    __mask1_ = __w0_ < _EDt - 1 ?
2622227825Stheraven                               _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2623227825Stheraven                               _Engine_result_type(~0);
2624227825Stheraven}
2625227825Stheraven
2626227825Stheraventemplate<class _Engine, class _UIntType>
2627227825Stheraveninline
2628227825Stheraven_UIntType
2629227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2630227825Stheraven{
2631227825Stheraven    return static_cast<result_type>(__e_() & __mask0_);
2632227825Stheraven}
2633227825Stheraven
2634227825Stheraventemplate<class _Engine, class _UIntType>
2635227825Stheraven_UIntType
2636227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2637227825Stheraven{
2638232950Stheraven    result_type _Sp = 0;
2639227825Stheraven    for (size_t __k = 0; __k < __n0_; ++__k)
2640227825Stheraven    {
2641227825Stheraven        _Engine_result_type __u;
2642227825Stheraven        do
2643227825Stheraven        {
2644227825Stheraven            __u = __e_() - _Engine::min();
2645227825Stheraven        } while (__u >= __y0_);
2646227825Stheraven        if (__w0_ < _WDt)
2647232950Stheraven            _Sp <<= __w0_;
2648227825Stheraven        else
2649232950Stheraven            _Sp = 0;
2650232950Stheraven        _Sp += __u & __mask0_;
2651227825Stheraven    }
2652227825Stheraven    for (size_t __k = __n0_; __k < __n_; ++__k)
2653227825Stheraven    {
2654227825Stheraven        _Engine_result_type __u;
2655227825Stheraven        do
2656227825Stheraven        {
2657227825Stheraven            __u = __e_() - _Engine::min();
2658227825Stheraven        } while (__u >= __y1_);
2659227825Stheraven        if (__w0_ < _WDt - 1)
2660232950Stheraven            _Sp <<= __w0_ + 1;
2661227825Stheraven        else
2662232950Stheraven            _Sp = 0;
2663232950Stheraven        _Sp += __u & __mask1_;
2664227825Stheraven    }
2665232950Stheraven    return _Sp;
2666227825Stheraven}
2667227825Stheraven
2668227825Stheraven// uniform_int_distribution
2669227825Stheraven
2670227825Stheraventemplate<class _IntType = int>
2671227825Stheravenclass uniform_int_distribution
2672227825Stheraven{
2673227825Stheravenpublic:
2674227825Stheraven    // types
2675227825Stheraven    typedef _IntType result_type;
2676227825Stheraven
2677227825Stheraven    class param_type
2678227825Stheraven    {
2679227825Stheraven        result_type __a_;
2680227825Stheraven        result_type __b_;
2681227825Stheraven    public:
2682227825Stheraven        typedef uniform_int_distribution distribution_type;
2683227825Stheraven
2684227825Stheraven        explicit param_type(result_type __a = 0,
2685227825Stheraven                            result_type __b = numeric_limits<result_type>::max())
2686227825Stheraven            : __a_(__a), __b_(__b) {}
2687227825Stheraven
2688227825Stheraven        result_type a() const {return __a_;}
2689227825Stheraven        result_type b() const {return __b_;}
2690227825Stheraven
2691227825Stheraven        friend bool operator==(const param_type& __x, const param_type& __y)
2692227825Stheraven            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2693227825Stheraven        friend bool operator!=(const param_type& __x, const param_type& __y)
2694227825Stheraven            {return !(__x == __y);}
2695227825Stheraven    };
2696227825Stheraven
2697227825Stheravenprivate:
2698227825Stheraven    param_type __p_;
2699227825Stheraven
2700227825Stheravenpublic:
2701227825Stheraven    // constructors and reset functions
2702227825Stheraven    explicit uniform_int_distribution(result_type __a = 0,
2703227825Stheraven                                      result_type __b = numeric_limits<result_type>::max())
2704227825Stheraven        : __p_(param_type(__a, __b)) {}
2705227825Stheraven    explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2706227825Stheraven    void reset() {}
2707227825Stheraven
2708227825Stheraven    // generating functions
2709227825Stheraven    template<class _URNG> result_type operator()(_URNG& __g)
2710227825Stheraven        {return (*this)(__g, __p_);}
2711227825Stheraven    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2712227825Stheraven
2713227825Stheraven    // property functions
2714227825Stheraven    result_type a() const {return __p_.a();}
2715227825Stheraven    result_type b() const {return __p_.b();}
2716227825Stheraven
2717227825Stheraven    param_type param() const {return __p_;}
2718227825Stheraven    void param(const param_type& __p) {__p_ = __p;}
2719227825Stheraven
2720227825Stheraven    result_type min() const {return a();}
2721227825Stheraven    result_type max() const {return b();}
2722227825Stheraven
2723227825Stheraven    friend bool operator==(const uniform_int_distribution& __x,
2724227825Stheraven                           const uniform_int_distribution& __y)
2725227825Stheraven        {return __x.__p_ == __y.__p_;}
2726227825Stheraven    friend bool operator!=(const uniform_int_distribution& __x,
2727227825Stheraven                           const uniform_int_distribution& __y)
2728227825Stheraven            {return !(__x == __y);}
2729227825Stheraven};
2730227825Stheraven
2731227825Stheraventemplate<class _IntType>
2732227825Stheraventemplate<class _URNG>
2733227825Stheraventypename uniform_int_distribution<_IntType>::result_type
2734227825Stheravenuniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2735227825Stheraven{
2736227825Stheraven    typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2737227825Stheraven                                            uint32_t, uint64_t>::type _UIntType;
2738232950Stheraven    const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
2739232950Stheraven    if (_Rp == 1)
2740227825Stheraven        return __p.a();
2741227825Stheraven    const size_t _Dt = numeric_limits<_UIntType>::digits;
2742227825Stheraven    typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2743232950Stheraven    if (_Rp == 0)
2744227825Stheraven        return static_cast<result_type>(_Eng(__g, _Dt)());
2745232950Stheraven    size_t __w = _Dt - __clz(_Rp) - 1;
2746232950Stheraven    if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
2747227825Stheraven        ++__w;
2748227825Stheraven    _Eng __e(__g, __w);
2749227825Stheraven    _UIntType __u;
2750227825Stheraven    do
2751227825Stheraven    {
2752227825Stheraven        __u = __e();
2753232950Stheraven    } while (__u >= _Rp);
2754227825Stheraven    return static_cast<result_type>(__u + __p.a());
2755227825Stheraven}
2756227825Stheraven
2757227825Stheravenclass __rs_default;
2758227825Stheraven
2759227825Stheraven__rs_default __rs_get();
2760227825Stheraven
2761227825Stheravenclass __rs_default
2762227825Stheraven{
2763227825Stheraven    static unsigned __c_;
2764227825Stheraven
2765227825Stheraven    __rs_default();
2766227825Stheravenpublic:
2767227825Stheraven    typedef unsigned result_type;
2768227825Stheraven
2769227825Stheraven    static const result_type _Min = 0;
2770227825Stheraven    static const result_type _Max = 0xFFFFFFFF;
2771227825Stheraven
2772227825Stheraven    __rs_default(const __rs_default&);
2773227825Stheraven    ~__rs_default();
2774227825Stheraven
2775227825Stheraven    result_type operator()();
2776227825Stheraven
2777234976Stheraven    static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2778234976Stheraven    static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
2779227825Stheraven
2780227825Stheraven    friend __rs_default __rs_get();
2781227825Stheraven};
2782227825Stheraven
2783227825Stheraven__rs_default __rs_get();
2784227825Stheraven
2785227825Stheraventemplate <class _RandomAccessIterator>
2786227825Stheravenvoid
2787227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2788227825Stheraven{
2789227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2790232950Stheraven    typedef uniform_int_distribution<ptrdiff_t> _Dp;
2791232950Stheraven    typedef typename _Dp::param_type _Pp;
2792227825Stheraven    difference_type __d = __last - __first;
2793227825Stheraven    if (__d > 1)
2794227825Stheraven    {
2795232950Stheraven        _Dp __uid;
2796227825Stheraven        __rs_default __g = __rs_get();
2797227825Stheraven        for (--__last, --__d; __first < __last; ++__first, --__d)
2798227825Stheraven        {
2799232950Stheraven            difference_type __i = __uid(__g, _Pp(0, __d));
2800227825Stheraven            if (__i != difference_type(0))
2801227825Stheraven                swap(*__first, *(__first + __i));
2802227825Stheraven        }
2803227825Stheraven    }
2804227825Stheraven}
2805227825Stheraven
2806227825Stheraventemplate <class _RandomAccessIterator, class _RandomNumberGenerator>
2807227825Stheravenvoid
2808227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
2809227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2810227825Stheraven               _RandomNumberGenerator&& __rand)
2811227825Stheraven#else
2812227825Stheraven               _RandomNumberGenerator& __rand)
2813227825Stheraven#endif
2814227825Stheraven{
2815227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2816227825Stheraven    difference_type __d = __last - __first;
2817227825Stheraven    if (__d > 1)
2818227825Stheraven    {
2819227825Stheraven        for (--__last; __first < __last; ++__first, --__d)
2820227825Stheraven        {
2821227825Stheraven            difference_type __i = __rand(__d);
2822227825Stheraven            swap(*__first, *(__first + __i));
2823227825Stheraven        }
2824227825Stheraven    }
2825227825Stheraven}
2826227825Stheraven
2827227825Stheraventemplate<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
2828227825Stheraven    void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
2829227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2830227825Stheraven                 _UniformRandomNumberGenerator&& __g)
2831227825Stheraven#else
2832227825Stheraven                 _UniformRandomNumberGenerator& __g)
2833227825Stheraven#endif
2834227825Stheraven{
2835227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2836232950Stheraven    typedef uniform_int_distribution<ptrdiff_t> _Dp;
2837232950Stheraven    typedef typename _Dp::param_type _Pp;
2838227825Stheraven    difference_type __d = __last - __first;
2839227825Stheraven    if (__d > 1)
2840227825Stheraven    {
2841232950Stheraven        _Dp __uid;
2842227825Stheraven        for (--__last, --__d; __first < __last; ++__first, --__d)
2843227825Stheraven        {
2844232950Stheraven            difference_type __i = __uid(__g, _Pp(0, __d));
2845227825Stheraven            if (__i != difference_type(0))
2846227825Stheraven                swap(*__first, *(__first + __i));
2847227825Stheraven        }
2848227825Stheraven    }
2849227825Stheraven}
2850227825Stheraven
2851227825Stheraventemplate <class _InputIterator, class _Predicate>
2852227825Stheravenbool
2853227825Stheravenis_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2854227825Stheraven{
2855227825Stheraven    for (; __first != __last; ++__first)
2856227825Stheraven        if (!__pred(*__first))
2857227825Stheraven            break;
2858227825Stheraven    for (; __first != __last; ++__first)
2859227825Stheraven        if (__pred(*__first))
2860227825Stheraven            return false;
2861227825Stheraven    return true;
2862227825Stheraven}
2863227825Stheraven
2864227825Stheraven// partition
2865227825Stheraven
2866227825Stheraventemplate <class _Predicate, class _ForwardIterator>
2867227825Stheraven_ForwardIterator
2868227825Stheraven__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
2869227825Stheraven{
2870227825Stheraven    while (true)
2871227825Stheraven    {
2872227825Stheraven        if (__first == __last)
2873227825Stheraven            return __first;
2874227825Stheraven        if (!__pred(*__first))
2875227825Stheraven            break;
2876227825Stheraven        ++__first;
2877227825Stheraven    }
2878227825Stheraven    for (_ForwardIterator __p = __first; ++__p != __last;)
2879227825Stheraven    {
2880227825Stheraven        if (__pred(*__p))
2881227825Stheraven        {
2882227825Stheraven            swap(*__first, *__p);
2883227825Stheraven            ++__first;
2884227825Stheraven        }
2885227825Stheraven    }
2886227825Stheraven    return __first;
2887227825Stheraven}
2888227825Stheraven
2889227825Stheraventemplate <class _Predicate, class _BidirectionalIterator>
2890227825Stheraven_BidirectionalIterator
2891227825Stheraven__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2892227825Stheraven            bidirectional_iterator_tag)
2893227825Stheraven{
2894227825Stheraven    while (true)
2895227825Stheraven    {
2896227825Stheraven        while (true)
2897227825Stheraven        {
2898227825Stheraven            if (__first == __last)
2899227825Stheraven                return __first;
2900227825Stheraven            if (!__pred(*__first))
2901227825Stheraven                break;
2902227825Stheraven            ++__first;
2903227825Stheraven        }
2904227825Stheraven        do
2905227825Stheraven        {
2906227825Stheraven            if (__first == --__last)
2907227825Stheraven                return __first;
2908227825Stheraven        } while (!__pred(*__last));
2909227825Stheraven        swap(*__first, *__last);
2910227825Stheraven        ++__first;
2911227825Stheraven    }
2912227825Stheraven}
2913227825Stheraven
2914227825Stheraventemplate <class _ForwardIterator, class _Predicate>
2915227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2916227825Stheraven_ForwardIterator
2917227825Stheravenpartition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2918227825Stheraven{
2919227825Stheraven    return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
2920227825Stheraven                            (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
2921227825Stheraven}
2922227825Stheraven
2923227825Stheraven// partition_copy
2924227825Stheraven
2925227825Stheraventemplate <class _InputIterator, class _OutputIterator1,
2926227825Stheraven          class _OutputIterator2, class _Predicate>
2927227825Stheravenpair<_OutputIterator1, _OutputIterator2>
2928227825Stheravenpartition_copy(_InputIterator __first, _InputIterator __last,
2929227825Stheraven               _OutputIterator1 __out_true, _OutputIterator2 __out_false,
2930227825Stheraven               _Predicate __pred)
2931227825Stheraven{
2932227825Stheraven    for (; __first != __last; ++__first)
2933227825Stheraven    {
2934227825Stheraven        if (__pred(*__first))
2935227825Stheraven        {
2936227825Stheraven            *__out_true = *__first;
2937227825Stheraven            ++__out_true;
2938227825Stheraven        }
2939227825Stheraven        else
2940227825Stheraven        {
2941227825Stheraven            *__out_false = *__first;
2942227825Stheraven            ++__out_false;
2943227825Stheraven        }
2944227825Stheraven    }
2945227825Stheraven    return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
2946227825Stheraven}
2947227825Stheraven
2948227825Stheraven// partition_point
2949227825Stheraven
2950227825Stheraventemplate<class _ForwardIterator, class _Predicate>
2951227825Stheraven_ForwardIterator
2952227825Stheravenpartition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2953227825Stheraven{
2954227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
2955227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
2956227825Stheraven    while (__len != 0)
2957227825Stheraven    {
2958227825Stheraven        difference_type __l2 = __len / 2;
2959227825Stheraven        _ForwardIterator __m = __first;
2960227825Stheraven        _VSTD::advance(__m, __l2);
2961227825Stheraven        if (__pred(*__m))
2962227825Stheraven        {
2963227825Stheraven            __first = ++__m;
2964227825Stheraven            __len -= __l2 + 1;
2965227825Stheraven        }
2966227825Stheraven        else
2967227825Stheraven            __len = __l2;
2968227825Stheraven    }
2969227825Stheraven    return __first;
2970227825Stheraven}
2971227825Stheraven
2972227825Stheraven// stable_partition
2973227825Stheraven
2974227825Stheraventemplate <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
2975227825Stheraven_ForwardIterator
2976227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2977227825Stheraven                   _Distance __len, _Pair __p, forward_iterator_tag __fit)
2978227825Stheraven{
2979227825Stheraven    // *__first is known to be false
2980227825Stheraven    // __len >= 1
2981227825Stheraven    if (__len == 1)
2982227825Stheraven        return __first;
2983227825Stheraven    if (__len == 2)
2984227825Stheraven    {
2985227825Stheraven        _ForwardIterator __m = __first;
2986227825Stheraven        if (__pred(*++__m))
2987227825Stheraven        {
2988227825Stheraven            swap(*__first, *__m);
2989227825Stheraven            return __m;
2990227825Stheraven        }
2991227825Stheraven        return __first;
2992227825Stheraven    }
2993227825Stheraven    if (__len <= __p.second)
2994227825Stheraven    {   // The buffer is big enough to use
2995227825Stheraven        typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2996227825Stheraven        __destruct_n __d(0);
2997227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
2998227825Stheraven        // Move the falses into the temporary buffer, and the trues to the front of the line
2999227825Stheraven        // Update __first to always point to the end of the trues
3000227825Stheraven        value_type* __t = __p.first;
3001227825Stheraven        ::new(__t) value_type(_VSTD::move(*__first));
3002227825Stheraven        __d.__incr((value_type*)0);
3003227825Stheraven        ++__t;
3004227825Stheraven        _ForwardIterator __i = __first;
3005227825Stheraven        while (++__i != __last)
3006227825Stheraven        {
3007227825Stheraven            if (__pred(*__i))
3008227825Stheraven            {
3009227825Stheraven                *__first = _VSTD::move(*__i);
3010227825Stheraven                ++__first;
3011227825Stheraven            }
3012227825Stheraven            else
3013227825Stheraven            {
3014227825Stheraven                ::new(__t) value_type(_VSTD::move(*__i));
3015227825Stheraven                __d.__incr((value_type*)0);
3016227825Stheraven                ++__t;
3017227825Stheraven            }
3018227825Stheraven        }
3019227825Stheraven        // All trues now at start of range, all falses in buffer
3020227825Stheraven        // Move falses back into range, but don't mess up __first which points to first false
3021227825Stheraven        __i = __first;
3022227825Stheraven        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3023227825Stheraven            *__i = _VSTD::move(*__t2);
3024227825Stheraven        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3025227825Stheraven        return __first;
3026227825Stheraven    }
3027227825Stheraven    // Else not enough buffer, do in place
3028227825Stheraven    // __len >= 3
3029227825Stheraven    _ForwardIterator __m = __first;
3030227825Stheraven    _Distance __len2 = __len / 2;  // __len2 >= 2
3031227825Stheraven    _VSTD::advance(__m, __len2);
3032227825Stheraven    // recurse on [__first, __m), *__first know to be false
3033227825Stheraven    // F?????????????????
3034227825Stheraven    // f       m         l
3035227825Stheraven    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3036227825Stheraven    _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3037227825Stheraven    // TTTFFFFF??????????
3038227825Stheraven    // f  ff   m         l
3039227825Stheraven    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3040227825Stheraven    _ForwardIterator __m1 = __m;
3041227825Stheraven    _ForwardIterator __second_false = __last;
3042227825Stheraven    _Distance __len_half = __len - __len2;
3043227825Stheraven    while (__pred(*__m1))
3044227825Stheraven    {
3045227825Stheraven        if (++__m1 == __last)
3046227825Stheraven            goto __second_half_done;
3047227825Stheraven        --__len_half;
3048227825Stheraven    }
3049227825Stheraven    // TTTFFFFFTTTF??????
3050227825Stheraven    // f  ff   m  m1     l
3051227825Stheraven    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3052227825Stheraven__second_half_done:
3053227825Stheraven    // TTTFFFFFTTTTTFFFFF
3054227825Stheraven    // f  ff   m    sf   l
3055227825Stheraven    return _VSTD::rotate(__first_false, __m, __second_false);
3056227825Stheraven    // TTTTTTTTFFFFFFFFFF
3057227825Stheraven    //         |
3058227825Stheraven}
3059227825Stheraven
3060227825Stheravenstruct __return_temporary_buffer
3061227825Stheraven{
3062227825Stheraven    template <class _Tp>
3063227825Stheraven    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
3064227825Stheraven};
3065227825Stheraven
3066227825Stheraventemplate <class _Predicate, class _ForwardIterator>
3067227825Stheraven_ForwardIterator
3068227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3069227825Stheraven                   forward_iterator_tag)
3070227825Stheraven{
3071227825Stheraven    const unsigned __alloc_limit = 3;  // might want to make this a function of trivial assignment
3072227825Stheraven    // Either prove all true and return __first or point to first false
3073227825Stheraven    while (true)
3074227825Stheraven    {
3075227825Stheraven        if (__first == __last)
3076227825Stheraven            return __first;
3077227825Stheraven        if (!__pred(*__first))
3078227825Stheraven            break;
3079227825Stheraven        ++__first;
3080227825Stheraven    }
3081227825Stheraven    // We now have a reduced range [__first, __last)
3082227825Stheraven    // *__first is known to be false
3083227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3084227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3085227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3086227825Stheraven    pair<value_type*, ptrdiff_t> __p(0, 0);
3087227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
3088227825Stheraven    if (__len >= __alloc_limit)
3089227825Stheraven    {
3090227825Stheraven        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3091227825Stheraven        __h.reset(__p.first);
3092227825Stheraven    }
3093227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3094227825Stheraven                             (__first, __last, __pred, __len, __p, forward_iterator_tag());
3095227825Stheraven}
3096227825Stheraven
3097227825Stheraventemplate <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3098227825Stheraven_BidirectionalIterator
3099227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3100227825Stheraven                   _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3101227825Stheraven{
3102227825Stheraven    // *__first is known to be false
3103227825Stheraven    // *__last is known to be true
3104227825Stheraven    // __len >= 2
3105227825Stheraven    if (__len == 2)
3106227825Stheraven    {
3107227825Stheraven        swap(*__first, *__last);
3108227825Stheraven        return __last;
3109227825Stheraven    }
3110227825Stheraven    if (__len == 3)
3111227825Stheraven    {
3112227825Stheraven        _BidirectionalIterator __m = __first;
3113227825Stheraven        if (__pred(*++__m))
3114227825Stheraven        {
3115227825Stheraven            swap(*__first, *__m);
3116227825Stheraven            swap(*__m, *__last);
3117227825Stheraven            return __last;
3118227825Stheraven        }
3119227825Stheraven        swap(*__m, *__last);
3120227825Stheraven        swap(*__first, *__m);
3121227825Stheraven        return __m;
3122227825Stheraven    }
3123227825Stheraven    if (__len <= __p.second)
3124227825Stheraven    {   // The buffer is big enough to use
3125227825Stheraven        typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3126227825Stheraven        __destruct_n __d(0);
3127227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3128227825Stheraven        // Move the falses into the temporary buffer, and the trues to the front of the line
3129227825Stheraven        // Update __first to always point to the end of the trues
3130227825Stheraven        value_type* __t = __p.first;
3131227825Stheraven        ::new(__t) value_type(_VSTD::move(*__first));
3132227825Stheraven        __d.__incr((value_type*)0);
3133227825Stheraven        ++__t;
3134227825Stheraven        _BidirectionalIterator __i = __first;
3135227825Stheraven        while (++__i != __last)
3136227825Stheraven        {
3137227825Stheraven            if (__pred(*__i))
3138227825Stheraven            {
3139227825Stheraven                *__first = _VSTD::move(*__i);
3140227825Stheraven                ++__first;
3141227825Stheraven            }
3142227825Stheraven            else
3143227825Stheraven            {
3144227825Stheraven                ::new(__t) value_type(_VSTD::move(*__i));
3145227825Stheraven                __d.__incr((value_type*)0);
3146227825Stheraven                ++__t;
3147227825Stheraven            }
3148227825Stheraven        }
3149227825Stheraven        // move *__last, known to be true
3150227825Stheraven        *__first = _VSTD::move(*__i);
3151227825Stheraven        __i = ++__first;
3152227825Stheraven        // All trues now at start of range, all falses in buffer
3153227825Stheraven        // Move falses back into range, but don't mess up __first which points to first false
3154227825Stheraven        for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3155227825Stheraven            *__i = _VSTD::move(*__t2);
3156227825Stheraven        // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3157227825Stheraven        return __first;
3158227825Stheraven    }
3159227825Stheraven    // Else not enough buffer, do in place
3160227825Stheraven    // __len >= 4
3161227825Stheraven    _BidirectionalIterator __m = __first;
3162227825Stheraven    _Distance __len2 = __len / 2;  // __len2 >= 2
3163227825Stheraven    _VSTD::advance(__m, __len2);
3164227825Stheraven    // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3165227825Stheraven    // F????????????????T
3166227825Stheraven    // f       m        l
3167227825Stheraven    _BidirectionalIterator __m1 = __m;
3168227825Stheraven    _BidirectionalIterator __first_false = __first;
3169227825Stheraven    _Distance __len_half = __len2;
3170227825Stheraven    while (!__pred(*--__m1))
3171227825Stheraven    {
3172227825Stheraven        if (__m1 == __first)
3173227825Stheraven            goto __first_half_done;
3174227825Stheraven        --__len_half;
3175227825Stheraven    }
3176227825Stheraven    // F???TFFF?????????T
3177227825Stheraven    // f   m1  m        l
3178227825Stheraven    typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3179227825Stheraven    __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3180227825Stheraven__first_half_done:
3181227825Stheraven    // TTTFFFFF?????????T
3182227825Stheraven    // f  ff   m        l
3183227825Stheraven    // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3184227825Stheraven    __m1 = __m;
3185227825Stheraven    _BidirectionalIterator __second_false = __last;
3186227825Stheraven    ++__second_false;
3187227825Stheraven    __len_half = __len - __len2;
3188227825Stheraven    while (__pred(*__m1))
3189227825Stheraven    {
3190227825Stheraven        if (++__m1 == __last)
3191227825Stheraven            goto __second_half_done;
3192227825Stheraven        --__len_half;
3193227825Stheraven    }
3194227825Stheraven    // TTTFFFFFTTTF?????T
3195227825Stheraven    // f  ff   m  m1    l
3196227825Stheraven    __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3197227825Stheraven__second_half_done:
3198227825Stheraven    // TTTFFFFFTTTTTFFFFF
3199227825Stheraven    // f  ff   m    sf  l
3200227825Stheraven    return _VSTD::rotate(__first_false, __m, __second_false);
3201227825Stheraven    // TTTTTTTTFFFFFFFFFF
3202227825Stheraven    //         |
3203227825Stheraven}
3204227825Stheraven
3205227825Stheraventemplate <class _Predicate, class _BidirectionalIterator>
3206227825Stheraven_BidirectionalIterator
3207227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3208227825Stheraven                   bidirectional_iterator_tag)
3209227825Stheraven{
3210227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3211227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3212227825Stheraven    const difference_type __alloc_limit = 4;  // might want to make this a function of trivial assignment
3213227825Stheraven    // Either prove all true and return __first or point to first false
3214227825Stheraven    while (true)
3215227825Stheraven    {
3216227825Stheraven        if (__first == __last)
3217227825Stheraven            return __first;
3218227825Stheraven        if (!__pred(*__first))
3219227825Stheraven            break;
3220227825Stheraven        ++__first;
3221227825Stheraven    }
3222227825Stheraven    // __first points to first false, everything prior to __first is already set.
3223227825Stheraven    // Either prove [__first, __last) is all false and return __first, or point __last to last true
3224227825Stheraven    do
3225227825Stheraven    {
3226227825Stheraven        if (__first == --__last)
3227227825Stheraven            return __first;
3228227825Stheraven    } while (!__pred(*__last));
3229227825Stheraven    // We now have a reduced range [__first, __last]
3230227825Stheraven    // *__first is known to be false
3231227825Stheraven    // *__last is known to be true
3232227825Stheraven    // __len >= 2
3233227825Stheraven    difference_type __len = _VSTD::distance(__first, __last) + 1;
3234227825Stheraven    pair<value_type*, ptrdiff_t> __p(0, 0);
3235227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
3236227825Stheraven    if (__len >= __alloc_limit)
3237227825Stheraven    {
3238227825Stheraven        __p = _VSTD::get_temporary_buffer<value_type>(__len);
3239227825Stheraven        __h.reset(__p.first);
3240227825Stheraven    }
3241227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3242227825Stheraven                             (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3243227825Stheraven}
3244227825Stheraven
3245227825Stheraventemplate <class _ForwardIterator, class _Predicate>
3246227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3247227825Stheraven_ForwardIterator
3248227825Stheravenstable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3249227825Stheraven{
3250227825Stheraven    return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3251227825Stheraven                             (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3252227825Stheraven}
3253227825Stheraven
3254227825Stheraven// is_sorted_until
3255227825Stheraven
3256227825Stheraventemplate <class _ForwardIterator, class _Compare>
3257227825Stheraven_ForwardIterator
3258227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3259227825Stheraven{
3260227825Stheraven    if (__first != __last)
3261227825Stheraven    {
3262227825Stheraven        _ForwardIterator __i = __first;
3263227825Stheraven        while (++__i != __last)
3264227825Stheraven        {
3265227825Stheraven            if (__comp(*__i, *__first))
3266227825Stheraven                return __i;
3267227825Stheraven            __first = __i;
3268227825Stheraven        }
3269227825Stheraven    }
3270227825Stheraven    return __last;
3271227825Stheraven}
3272227825Stheraven
3273227825Stheraventemplate<class _ForwardIterator>
3274227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3275227825Stheraven_ForwardIterator
3276227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3277227825Stheraven{
3278227825Stheraven    return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3279227825Stheraven}
3280227825Stheraven
3281227825Stheraven// is_sorted
3282227825Stheraven
3283227825Stheraventemplate <class _ForwardIterator, class _Compare>
3284227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3285227825Stheravenbool
3286227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3287227825Stheraven{
3288227825Stheraven    return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
3289227825Stheraven}
3290227825Stheraven
3291227825Stheraventemplate<class _ForwardIterator>
3292227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3293227825Stheravenbool
3294227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last)
3295227825Stheraven{
3296227825Stheraven    return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3297227825Stheraven}
3298227825Stheraven
3299227825Stheraven// sort
3300227825Stheraven
3301227825Stheraven// stable, 2-3 compares, 0-2 swaps
3302227825Stheraven
3303227825Stheraventemplate <class _Compare, class _ForwardIterator>
3304227825Stheravenunsigned
3305227825Stheraven__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3306227825Stheraven{
3307227825Stheraven    unsigned __r = 0;
3308227825Stheraven    if (!__c(*__y, *__x))          // if x <= y
3309227825Stheraven    {
3310227825Stheraven        if (!__c(*__z, *__y))      // if y <= z
3311227825Stheraven            return __r;            // x <= y && y <= z
3312227825Stheraven                                   // x <= y && y > z
3313227825Stheraven        swap(*__y, *__z);          // x <= z && y < z
3314227825Stheraven        __r = 1;
3315227825Stheraven        if (__c(*__y, *__x))       // if x > y
3316227825Stheraven        {
3317227825Stheraven            swap(*__x, *__y);      // x < y && y <= z
3318227825Stheraven            __r = 2;
3319227825Stheraven        }
3320227825Stheraven        return __r;                // x <= y && y < z
3321227825Stheraven    }
3322227825Stheraven    if (__c(*__z, *__y))           // x > y, if y > z
3323227825Stheraven    {
3324227825Stheraven        swap(*__x, *__z);          // x < y && y < z
3325227825Stheraven        __r = 1;
3326227825Stheraven        return __r;
3327227825Stheraven    }
3328227825Stheraven    swap(*__x, *__y);              // x > y && y <= z
3329227825Stheraven    __r = 1;                       // x < y && x <= z
3330227825Stheraven    if (__c(*__z, *__y))           // if y > z
3331227825Stheraven    {
3332227825Stheraven        swap(*__y, *__z);          // x <= y && y < z
3333227825Stheraven        __r = 2;
3334227825Stheraven    }
3335227825Stheraven    return __r;
3336227825Stheraven}                                  // x <= y && y <= z
3337227825Stheraven
3338227825Stheraven// stable, 3-6 compares, 0-5 swaps
3339227825Stheraven
3340227825Stheraventemplate <class _Compare, class _ForwardIterator>
3341227825Stheravenunsigned
3342227825Stheraven__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3343227825Stheraven            _ForwardIterator __x4, _Compare __c)
3344227825Stheraven{
3345227825Stheraven    unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3346227825Stheraven    if (__c(*__x4, *__x3))
3347227825Stheraven    {
3348227825Stheraven        swap(*__x3, *__x4);
3349227825Stheraven        ++__r;
3350227825Stheraven        if (__c(*__x3, *__x2))
3351227825Stheraven        {
3352227825Stheraven            swap(*__x2, *__x3);
3353227825Stheraven            ++__r;
3354227825Stheraven            if (__c(*__x2, *__x1))
3355227825Stheraven            {
3356227825Stheraven                swap(*__x1, *__x2);
3357227825Stheraven                ++__r;
3358227825Stheraven            }
3359227825Stheraven        }
3360227825Stheraven    }
3361227825Stheraven    return __r;
3362227825Stheraven}
3363227825Stheraven
3364227825Stheraven// stable, 4-10 compares, 0-9 swaps
3365227825Stheraven
3366227825Stheraventemplate <class _Compare, class _ForwardIterator>
3367227825Stheravenunsigned
3368227825Stheraven__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3369227825Stheraven            _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3370227825Stheraven{
3371227825Stheraven    unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3372227825Stheraven    if (__c(*__x5, *__x4))
3373227825Stheraven    {
3374227825Stheraven        swap(*__x4, *__x5);
3375227825Stheraven        ++__r;
3376227825Stheraven        if (__c(*__x4, *__x3))
3377227825Stheraven        {
3378227825Stheraven            swap(*__x3, *__x4);
3379227825Stheraven            ++__r;
3380227825Stheraven            if (__c(*__x3, *__x2))
3381227825Stheraven            {
3382227825Stheraven                swap(*__x2, *__x3);
3383227825Stheraven                ++__r;
3384227825Stheraven                if (__c(*__x2, *__x1))
3385227825Stheraven                {
3386227825Stheraven                    swap(*__x1, *__x2);
3387227825Stheraven                    ++__r;
3388227825Stheraven                }
3389227825Stheraven            }
3390227825Stheraven        }
3391227825Stheraven    }
3392227825Stheraven    return __r;
3393227825Stheraven}
3394227825Stheraven
3395227825Stheraven// Assumes size > 0
3396227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3397227825Stheravenvoid
3398227825Stheraven__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3399227825Stheraven{
3400227825Stheraven    _BirdirectionalIterator __lm1 = __last;
3401227825Stheraven    for (--__lm1; __first != __lm1; ++__first)
3402227825Stheraven    {
3403227825Stheraven        _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
3404227825Stheraven                                                        typename add_lvalue_reference<_Compare>::type>
3405227825Stheraven                                                       (__first, __last, __comp);
3406227825Stheraven        if (__i != __first)
3407227825Stheraven            swap(*__first, *__i);
3408227825Stheraven    }
3409227825Stheraven}
3410227825Stheraven
3411227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3412227825Stheravenvoid
3413227825Stheraven__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3414227825Stheraven{
3415227825Stheraven    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3416227825Stheraven    if (__first != __last)
3417227825Stheraven    {
3418227825Stheraven        _BirdirectionalIterator __i = __first;
3419227825Stheraven        for (++__i; __i != __last; ++__i)
3420227825Stheraven        {
3421227825Stheraven            _BirdirectionalIterator __j = __i;
3422227825Stheraven            value_type __t(_VSTD::move(*__j));
3423227825Stheraven            for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t,  *--__k); --__j)
3424227825Stheraven                *__j = _VSTD::move(*__k);
3425227825Stheraven            *__j = _VSTD::move(__t);
3426227825Stheraven        }
3427227825Stheraven    }
3428227825Stheraven}
3429227825Stheraven
3430227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3431227825Stheravenvoid
3432227825Stheraven__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3433227825Stheraven{
3434227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3435227825Stheraven    _RandomAccessIterator __j = __first+2;
3436227825Stheraven    __sort3<_Compare>(__first, __first+1, __j, __comp);
3437227825Stheraven    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3438227825Stheraven    {
3439227825Stheraven        if (__comp(*__i, *__j))
3440227825Stheraven        {
3441227825Stheraven            value_type __t(_VSTD::move(*__i));
3442227825Stheraven            _RandomAccessIterator __k = __j;
3443227825Stheraven            __j = __i;
3444227825Stheraven            do
3445227825Stheraven            {
3446227825Stheraven                *__j = _VSTD::move(*__k);
3447227825Stheraven                __j = __k;
3448227825Stheraven            } while (__j != __first && __comp(__t, *--__k));
3449227825Stheraven            *__j = _VSTD::move(__t);
3450227825Stheraven        }
3451227825Stheraven        __j = __i;
3452227825Stheraven    }
3453227825Stheraven}
3454227825Stheraven
3455227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3456227825Stheravenbool
3457227825Stheraven__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3458227825Stheraven{
3459227825Stheraven    switch (__last - __first)
3460227825Stheraven    {
3461227825Stheraven    case 0:
3462227825Stheraven    case 1:
3463227825Stheraven        return true;
3464227825Stheraven    case 2:
3465227825Stheraven        if (__comp(*--__last, *__first))
3466227825Stheraven            swap(*__first, *__last);
3467227825Stheraven        return true;
3468227825Stheraven    case 3:
3469227825Stheraven        _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3470227825Stheraven        return true;
3471227825Stheraven    case 4:
3472227825Stheraven        _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3473227825Stheraven        return true;
3474227825Stheraven    case 5:
3475227825Stheraven        _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3476227825Stheraven        return true;
3477227825Stheraven    }
3478227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3479227825Stheraven    _RandomAccessIterator __j = __first+2;
3480227825Stheraven    __sort3<_Compare>(__first, __first+1, __j, __comp);
3481227825Stheraven    const unsigned __limit = 8;
3482227825Stheraven    unsigned __count = 0;
3483227825Stheraven    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3484227825Stheraven    {
3485227825Stheraven        if (__comp(*__i, *__j))
3486227825Stheraven        {
3487227825Stheraven            value_type __t(_VSTD::move(*__i));
3488227825Stheraven            _RandomAccessIterator __k = __j;
3489227825Stheraven            __j = __i;
3490227825Stheraven            do
3491227825Stheraven            {
3492227825Stheraven                *__j = _VSTD::move(*__k);
3493227825Stheraven                __j = __k;
3494227825Stheraven            } while (__j != __first && __comp(__t, *--__k));
3495227825Stheraven            *__j = _VSTD::move(__t);
3496227825Stheraven            if (++__count == __limit)
3497227825Stheraven                return ++__i == __last;
3498227825Stheraven        }
3499227825Stheraven        __j = __i;
3500227825Stheraven    }
3501227825Stheraven    return true;
3502227825Stheraven}
3503227825Stheraven
3504227825Stheraventemplate <class _Compare, class _BirdirectionalIterator>
3505227825Stheravenvoid
3506227825Stheraven__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3507227825Stheraven                      typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3508227825Stheraven{
3509227825Stheraven    typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3510227825Stheraven    if (__first1 != __last1)
3511227825Stheraven    {
3512227825Stheraven        __destruct_n __d(0);
3513227825Stheraven        unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3514227825Stheraven        value_type* __last2 = __first2;
3515227825Stheraven        ::new(__last2) value_type(_VSTD::move(*__first1));
3516227825Stheraven        __d.__incr((value_type*)0);
3517227825Stheraven        for (++__last2; ++__first1 != __last1; ++__last2)
3518227825Stheraven        {
3519227825Stheraven            value_type* __j2 = __last2;
3520227825Stheraven            value_type* __i2 = __j2;
3521227825Stheraven            if (__comp(*__first1, *--__i2))
3522227825Stheraven            {
3523227825Stheraven                ::new(__j2) value_type(_VSTD::move(*__i2));
3524227825Stheraven                __d.__incr((value_type*)0);
3525227825Stheraven                for (--__j2; __i2 != __first2 && __comp(*__first1,  *--__i2); --__j2)
3526227825Stheraven                    *__j2 = _VSTD::move(*__i2);
3527227825Stheraven                *__j2 = _VSTD::move(*__first1);
3528227825Stheraven            }
3529227825Stheraven            else
3530227825Stheraven            {
3531227825Stheraven                ::new(__j2) value_type(_VSTD::move(*__first1));
3532227825Stheraven                __d.__incr((value_type*)0);
3533227825Stheraven            }
3534227825Stheraven        }
3535227825Stheraven        __h.release();
3536227825Stheraven    }
3537227825Stheraven}
3538227825Stheraven
3539227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
3540227825Stheravenvoid
3541227825Stheraven__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3542227825Stheraven{
3543227825Stheraven    // _Compare is known to be a reference type
3544227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3545227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3546227825Stheraven    const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3547227825Stheraven                                    is_trivially_copy_assignable<value_type>::value ? 30 : 6;
3548227825Stheraven    while (true)
3549227825Stheraven    {
3550227825Stheraven    __restart:
3551227825Stheraven        difference_type __len = __last - __first;
3552227825Stheraven        switch (__len)
3553227825Stheraven        {
3554227825Stheraven        case 0:
3555227825Stheraven        case 1:
3556227825Stheraven            return;
3557227825Stheraven        case 2:
3558227825Stheraven            if (__comp(*--__last, *__first))
3559227825Stheraven                swap(*__first, *__last);
3560227825Stheraven            return;
3561227825Stheraven        case 3:
3562227825Stheraven            _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3563227825Stheraven            return;
3564227825Stheraven        case 4:
3565227825Stheraven            _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3566227825Stheraven            return;
3567227825Stheraven        case 5:
3568227825Stheraven            _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3569227825Stheraven            return;
3570227825Stheraven        }
3571227825Stheraven        if (__len <= __limit)
3572227825Stheraven        {
3573227825Stheraven            _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3574227825Stheraven            return;
3575227825Stheraven        }
3576227825Stheraven        // __len > 5
3577227825Stheraven        _RandomAccessIterator __m = __first;
3578227825Stheraven        _RandomAccessIterator __lm1 = __last;
3579227825Stheraven        --__lm1;
3580227825Stheraven        unsigned __n_swaps;
3581227825Stheraven        {
3582227825Stheraven        difference_type __delta;
3583227825Stheraven        if (__len >= 1000)
3584227825Stheraven        {
3585227825Stheraven            __delta = __len/2;
3586227825Stheraven            __m += __delta;
3587227825Stheraven            __delta /= 2;
3588227825Stheraven            __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3589227825Stheraven        }
3590227825Stheraven        else
3591227825Stheraven        {
3592227825Stheraven            __delta = __len/2;
3593227825Stheraven            __m += __delta;
3594227825Stheraven            __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3595227825Stheraven        }
3596227825Stheraven        }
3597227825Stheraven        // *__m is median
3598227825Stheraven        // partition [__first, __m) < *__m and *__m <= [__m, __last)
3599227825Stheraven        // (this inhibits tossing elements equivalent to __m around unnecessarily)
3600227825Stheraven        _RandomAccessIterator __i = __first;
3601227825Stheraven        _RandomAccessIterator __j = __lm1;
3602227825Stheraven        // j points beyond range to be tested, *__m is known to be <= *__lm1
3603227825Stheraven        // The search going up is known to be guarded but the search coming down isn't.
3604227825Stheraven        // Prime the downward search with a guard.
3605227825Stheraven        if (!__comp(*__i, *__m))  // if *__first == *__m
3606227825Stheraven        {
3607227825Stheraven            // *__first == *__m, *__first doesn't go in first part
3608227825Stheraven            // manually guard downward moving __j against __i
3609227825Stheraven            while (true)
3610227825Stheraven            {
3611227825Stheraven                if (__i == --__j)
3612227825Stheraven                {
3613227825Stheraven                    // *__first == *__m, *__m <= all other elements
3614227825Stheraven                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3615227825Stheraven                    ++__i;  // __first + 1
3616227825Stheraven                    __j = __last;
3617227825Stheraven                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
3618227825Stheraven                    {
3619227825Stheraven                        while (true)
3620227825Stheraven                        {
3621227825Stheraven                            if (__i == __j)
3622227825Stheraven                                return;  // [__first, __last) all equivalent elements
3623227825Stheraven                            if (__comp(*__first, *__i))
3624227825Stheraven                            {
3625227825Stheraven                                swap(*__i, *__j);
3626227825Stheraven                                ++__n_swaps;
3627227825Stheraven                                ++__i;
3628227825Stheraven                                break;
3629227825Stheraven                            }
3630227825Stheraven                            ++__i;
3631227825Stheraven                        }
3632227825Stheraven                    }
3633227825Stheraven                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3634227825Stheraven                    if (__i == __j)
3635227825Stheraven                        return;
3636227825Stheraven                    while (true)
3637227825Stheraven                    {
3638227825Stheraven                        while (!__comp(*__first, *__i))
3639227825Stheraven                            ++__i;
3640227825Stheraven                        while (__comp(*__first, *--__j))
3641227825Stheraven                            ;
3642227825Stheraven                        if (__i >= __j)
3643227825Stheraven                            break;
3644227825Stheraven                        swap(*__i, *__j);
3645227825Stheraven                        ++__n_swaps;
3646227825Stheraven                        ++__i;
3647227825Stheraven                    }
3648227825Stheraven                    // [__first, __i) == *__first and *__first < [__i, __last)
3649227825Stheraven                    // The first part is sorted, sort the secod part
3650227825Stheraven                    // _VSTD::__sort<_Compare>(__i, __last, __comp);
3651227825Stheraven                    __first = __i;
3652227825Stheraven                    goto __restart;
3653227825Stheraven                }
3654227825Stheraven                if (__comp(*__j, *__m))
3655227825Stheraven                {
3656227825Stheraven                    swap(*__i, *__j);
3657227825Stheraven                    ++__n_swaps;
3658227825Stheraven                    break;  // found guard for downward moving __j, now use unguarded partition
3659227825Stheraven                }
3660227825Stheraven            }
3661227825Stheraven        }
3662227825Stheraven        // It is known that *__i < *__m
3663227825Stheraven        ++__i;
3664227825Stheraven        // j points beyond range to be tested, *__m is known to be <= *__lm1
3665227825Stheraven        // if not yet partitioned...
3666227825Stheraven        if (__i < __j)
3667227825Stheraven        {
3668227825Stheraven            // known that *(__i - 1) < *__m
3669227825Stheraven            // known that __i <= __m
3670227825Stheraven            while (true)
3671227825Stheraven            {
3672227825Stheraven                // __m still guards upward moving __i
3673227825Stheraven                while (__comp(*__i, *__m))
3674227825Stheraven                    ++__i;
3675227825Stheraven                // It is now known that a guard exists for downward moving __j
3676227825Stheraven                while (!__comp(*--__j, *__m))
3677227825Stheraven                    ;
3678227825Stheraven                if (__i > __j)
3679227825Stheraven                    break;
3680227825Stheraven                swap(*__i, *__j);
3681227825Stheraven                ++__n_swaps;
3682227825Stheraven                // It is known that __m != __j
3683227825Stheraven                // If __m just moved, follow it
3684227825Stheraven                if (__m == __i)
3685227825Stheraven                    __m = __j;
3686227825Stheraven                ++__i;
3687227825Stheraven            }
3688227825Stheraven        }
3689227825Stheraven        // [__first, __i) < *__m and *__m <= [__i, __last)
3690227825Stheraven        if (__i != __m && __comp(*__m, *__i))
3691227825Stheraven        {
3692227825Stheraven            swap(*__i, *__m);
3693227825Stheraven            ++__n_swaps;
3694227825Stheraven        }
3695227825Stheraven        // [__first, __i) < *__i and *__i <= [__i+1, __last)
3696227825Stheraven        // If we were given a perfect partition, see if insertion sort is quick...
3697227825Stheraven        if (__n_swaps == 0)
3698227825Stheraven        {
3699227825Stheraven            bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3700227825Stheraven            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
3701227825Stheraven            {
3702227825Stheraven                if (__fs)
3703227825Stheraven                    return;
3704227825Stheraven                __last = __i;
3705227825Stheraven                continue;
3706227825Stheraven            }
3707227825Stheraven            else
3708227825Stheraven            {
3709227825Stheraven                if (__fs)
3710227825Stheraven                {
3711227825Stheraven                    __first = ++__i;
3712227825Stheraven                    continue;
3713227825Stheraven                }
3714227825Stheraven            }
3715227825Stheraven        }
3716227825Stheraven        // sort smaller range with recursive call and larger with tail recursion elimination
3717227825Stheraven        if (__i - __first < __last - __i)
3718227825Stheraven        {
3719227825Stheraven            _VSTD::__sort<_Compare>(__first, __i, __comp);
3720227825Stheraven            // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3721227825Stheraven            __first = ++__i;
3722227825Stheraven        }
3723227825Stheraven        else
3724227825Stheraven        {
3725227825Stheraven            _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3726227825Stheraven            // _VSTD::__sort<_Compare>(__first, __i, __comp);
3727227825Stheraven            __last = __i;
3728227825Stheraven        }
3729227825Stheraven    }
3730227825Stheraven}
3731227825Stheraven
3732227825Stheraven// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3733227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
3734227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3735227825Stheravenvoid
3736227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3737227825Stheraven{
3738227825Stheraven#ifdef _LIBCPP_DEBUG2
3739227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3740227825Stheraven    __debug_less<_Compare> __c(__comp);
3741227825Stheraven    __sort<_Comp_ref>(__first, __last, __c);
3742227825Stheraven#else  // _LIBCPP_DEBUG2
3743227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3744227825Stheraven    __sort<_Comp_ref>(__first, __last, __comp);
3745227825Stheraven#endif  // _LIBCPP_DEBUG2
3746227825Stheraven}
3747227825Stheraven
3748227825Stheraventemplate <class _RandomAccessIterator>
3749227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3750227825Stheravenvoid
3751227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3752227825Stheraven{
3753227825Stheraven    _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
3754227825Stheraven}
3755227825Stheraven
3756227825Stheraventemplate <class _Tp>
3757227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3758227825Stheravenvoid
3759227825Stheravensort(_Tp** __first, _Tp** __last)
3760227825Stheraven{
3761227825Stheraven    _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
3762227825Stheraven}
3763227825Stheraven
3764227825Stheraventemplate <class _Tp>
3765227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3766227825Stheravenvoid
3767227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
3768227825Stheraven{
3769227825Stheraven    _VSTD::sort(__first.base(), __last.base());
3770227825Stheraven}
3771227825Stheraven
3772227825Stheraventemplate <class _Tp, class _Compare>
3773227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3774227825Stheravenvoid
3775227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
3776227825Stheraven{
3777227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3778227825Stheraven    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
3779227825Stheraven}
3780227825Stheraven
3781227825Stheraven#ifdef _MSC_VER
3782227825Stheraven#pragma warning( push )
3783227825Stheraven#pragma warning( disable: 4231)
3784227825Stheraven#endif // _MSC_VER
3785242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
3786242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
3787242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
3788242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
3789242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
3790242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
3791242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
3792242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
3793242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
3794242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
3795242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
3796242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
3797242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
3798242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
3799242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
3800227825Stheraven
3801242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
3802242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
3803242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
3804242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
3805242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
3806242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
3807242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
3808242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
3809242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
3810242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
3811242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
3812242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
3813242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
3814242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
3815242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
3816227825Stheraven
3817242945Stheraven_LIBCPP_EXTERN_TEMPLATE(unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
3818227825Stheraven#ifdef _MSC_VER
3819227825Stheraven#pragma warning( pop )
3820232950Stheraven#endif  // _MSC_VER
3821227825Stheraven
3822227825Stheraven// lower_bound
3823227825Stheraven
3824227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
3825227825Stheraven_ForwardIterator
3826227825Stheraven__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3827227825Stheraven{
3828227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3829227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3830227825Stheraven    while (__len != 0)
3831227825Stheraven    {
3832227825Stheraven        difference_type __l2 = __len / 2;
3833227825Stheraven        _ForwardIterator __m = __first;
3834227825Stheraven        _VSTD::advance(__m, __l2);
3835227825Stheraven        if (__comp(*__m, __value_))
3836227825Stheraven        {
3837227825Stheraven            __first = ++__m;
3838227825Stheraven            __len -= __l2 + 1;
3839227825Stheraven        }
3840227825Stheraven        else
3841227825Stheraven            __len = __l2;
3842227825Stheraven    }
3843227825Stheraven    return __first;
3844227825Stheraven}
3845227825Stheraven
3846227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
3847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3848227825Stheraven_ForwardIterator
3849227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3850227825Stheraven{
3851227825Stheraven#ifdef _LIBCPP_DEBUG2
3852227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3853227825Stheraven    __debug_less<_Compare> __c(__comp);
3854227825Stheraven    return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
3855227825Stheraven#else  // _LIBCPP_DEBUG2
3856227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3857227825Stheraven    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
3858227825Stheraven#endif  // _LIBCPP_DEBUG2
3859227825Stheraven}
3860227825Stheraven
3861227825Stheraventemplate <class _ForwardIterator, class _Tp>
3862227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3863227825Stheraven_ForwardIterator
3864227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
3865227825Stheraven{
3866227825Stheraven    return _VSTD::lower_bound(__first, __last, __value_,
3867227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3868227825Stheraven}
3869227825Stheraven
3870227825Stheraven// upper_bound
3871227825Stheraven
3872227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
3873227825Stheraven_ForwardIterator
3874227825Stheraven__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3875227825Stheraven{
3876227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3877227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3878227825Stheraven    while (__len != 0)
3879227825Stheraven    {
3880227825Stheraven        difference_type __l2 = __len / 2;
3881227825Stheraven        _ForwardIterator __m = __first;
3882227825Stheraven        _VSTD::advance(__m, __l2);
3883227825Stheraven        if (__comp(__value_, *__m))
3884227825Stheraven            __len = __l2;
3885227825Stheraven        else
3886227825Stheraven        {
3887227825Stheraven            __first = ++__m;
3888227825Stheraven            __len -= __l2 + 1;
3889227825Stheraven        }
3890227825Stheraven    }
3891227825Stheraven    return __first;
3892227825Stheraven}
3893227825Stheraven
3894227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
3895227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3896227825Stheraven_ForwardIterator
3897227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3898227825Stheraven{
3899227825Stheraven#ifdef _LIBCPP_DEBUG2
3900227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3901227825Stheraven    __debug_less<_Compare> __c(__comp);
3902227825Stheraven    return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
3903227825Stheraven#else  // _LIBCPP_DEBUG2
3904227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3905227825Stheraven    return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
3906227825Stheraven#endif  // _LIBCPP_DEBUG2
3907227825Stheraven}
3908227825Stheraven
3909227825Stheraventemplate <class _ForwardIterator, class _Tp>
3910227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3911227825Stheraven_ForwardIterator
3912227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
3913227825Stheraven{
3914227825Stheraven    return _VSTD::upper_bound(__first, __last, __value_,
3915227825Stheraven                             __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
3916227825Stheraven}
3917227825Stheraven
3918227825Stheraven// equal_range
3919227825Stheraven
3920227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
3921227825Stheravenpair<_ForwardIterator, _ForwardIterator>
3922227825Stheraven__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3923227825Stheraven{
3924227825Stheraven    typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3925227825Stheraven    difference_type __len = _VSTD::distance(__first, __last);
3926227825Stheraven    while (__len != 0)
3927227825Stheraven    {
3928227825Stheraven        difference_type __l2 = __len / 2;
3929227825Stheraven        _ForwardIterator __m = __first;
3930227825Stheraven        _VSTD::advance(__m, __l2);
3931227825Stheraven        if (__comp(*__m, __value_))
3932227825Stheraven        {
3933227825Stheraven            __first = ++__m;
3934227825Stheraven            __len -= __l2 + 1;
3935227825Stheraven        }
3936227825Stheraven        else if (__comp(__value_, *__m))
3937227825Stheraven        {
3938227825Stheraven            __last = __m;
3939227825Stheraven            __len = __l2;
3940227825Stheraven        }
3941227825Stheraven        else
3942227825Stheraven        {
3943227825Stheraven            _ForwardIterator __mp1 = __m;
3944227825Stheraven            return pair<_ForwardIterator, _ForwardIterator>
3945227825Stheraven                   (
3946227825Stheraven                      __lower_bound<_Compare>(__first, __m, __value_, __comp),
3947227825Stheraven                      __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
3948227825Stheraven                   );
3949227825Stheraven        }
3950227825Stheraven    }
3951227825Stheraven    return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3952227825Stheraven}
3953227825Stheraven
3954227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
3955227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3956227825Stheravenpair<_ForwardIterator, _ForwardIterator>
3957227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3958227825Stheraven{
3959227825Stheraven#ifdef _LIBCPP_DEBUG2
3960227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3961227825Stheraven    __debug_less<_Compare> __c(__comp);
3962227825Stheraven    return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
3963227825Stheraven#else  // _LIBCPP_DEBUG2
3964227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3965227825Stheraven    return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
3966227825Stheraven#endif  // _LIBCPP_DEBUG2
3967227825Stheraven}
3968227825Stheraven
3969227825Stheraventemplate <class _ForwardIterator, class _Tp>
3970227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3971227825Stheravenpair<_ForwardIterator, _ForwardIterator>
3972227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
3973227825Stheraven{
3974227825Stheraven    return _VSTD::equal_range(__first, __last, __value_,
3975227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3976227825Stheraven}
3977227825Stheraven
3978227825Stheraven// binary_search
3979227825Stheraven
3980227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp>
3981227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3982227825Stheravenbool
3983227825Stheraven__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3984227825Stheraven{
3985227825Stheraven    __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
3986227825Stheraven    return __first != __last && !__comp(__value_, *__first);
3987227825Stheraven}
3988227825Stheraven
3989227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare>
3990227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
3991227825Stheravenbool
3992227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
3993227825Stheraven{
3994227825Stheraven#ifdef _LIBCPP_DEBUG2
3995227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3996227825Stheraven    __debug_less<_Compare> __c(__comp);
3997227825Stheraven    return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
3998227825Stheraven#else  // _LIBCPP_DEBUG2
3999227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4000227825Stheraven    return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
4001227825Stheraven#endif  // _LIBCPP_DEBUG2
4002227825Stheraven}
4003227825Stheraven
4004227825Stheraventemplate <class _ForwardIterator, class _Tp>
4005227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4006227825Stheravenbool
4007227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
4008227825Stheraven{
4009227825Stheraven    return _VSTD::binary_search(__first, __last, __value_,
4010227825Stheraven                             __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4011227825Stheraven}
4012227825Stheraven
4013227825Stheraven// merge
4014227825Stheraven
4015227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4016227825Stheraven_OutputIterator
4017227825Stheraven__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4018227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4019227825Stheraven{
4020227825Stheraven    for (; __first1 != __last1; ++__result)
4021227825Stheraven    {
4022227825Stheraven        if (__first2 == __last2)
4023227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
4024227825Stheraven        if (__comp(*__first2, *__first1))
4025227825Stheraven        {
4026227825Stheraven            *__result = *__first2;
4027227825Stheraven            ++__first2;
4028227825Stheraven        }
4029227825Stheraven        else
4030227825Stheraven        {
4031227825Stheraven            *__result = *__first1;
4032227825Stheraven            ++__first1;
4033227825Stheraven        }
4034227825Stheraven    }
4035227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
4036227825Stheraven}
4037227825Stheraven
4038227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4039227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4040227825Stheraven_OutputIterator
4041227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1,
4042227825Stheraven      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4043227825Stheraven{
4044227825Stheraven#ifdef _LIBCPP_DEBUG2
4045227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4046227825Stheraven    __debug_less<_Compare> __c(__comp);
4047227825Stheraven    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4048227825Stheraven#else  // _LIBCPP_DEBUG2
4049227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4050227825Stheraven    return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4051227825Stheraven#endif  // _LIBCPP_DEBUG2
4052227825Stheraven}
4053227825Stheraven
4054227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4055227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4056227825Stheraven_OutputIterator
4057227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1,
4058227825Stheraven      _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4059227825Stheraven{
4060227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4061227825Stheraven    typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4062227825Stheraven    return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4063227825Stheraven}
4064227825Stheraven
4065227825Stheraven// inplace_merge
4066227825Stheraven
4067227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
4068227825Stheravenvoid
4069227825Stheraven__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4070227825Stheraven                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4071227825Stheraven                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4072227825Stheraven                typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4073227825Stheraven{
4074227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4075227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4076227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
4077227825Stheraven    __destruct_n __d(0);
4078227825Stheraven    unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4079227825Stheraven    if (__len1 <= __len2)
4080227825Stheraven    {
4081227825Stheraven        value_type* __p = __buff;
4082227825Stheraven        for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
4083227825Stheraven            ::new(__p) value_type(_VSTD::move(*__i));
4084227825Stheraven        __merge<_Compare>(move_iterator<value_type*>(__buff),
4085227825Stheraven                          move_iterator<value_type*>(__p),
4086227825Stheraven                          move_iterator<_BidirectionalIterator>(__middle),
4087227825Stheraven                          move_iterator<_BidirectionalIterator>(__last),
4088227825Stheraven                          __first, __comp);
4089227825Stheraven    }
4090227825Stheraven    else
4091227825Stheraven    {
4092227825Stheraven        value_type* __p = __buff;
4093227825Stheraven        for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
4094227825Stheraven            ::new(__p) value_type(_VSTD::move(*__i));
4095227825Stheraven        typedef reverse_iterator<_BidirectionalIterator> _RBi;
4096227825Stheraven        typedef reverse_iterator<value_type*> _Rv;
4097227825Stheraven        __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
4098227825Stheraven                move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
4099227825Stheraven                _RBi(__last), __negate<_Compare>(__comp));
4100227825Stheraven    }
4101227825Stheraven}
4102227825Stheraven
4103227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
4104227825Stheravenvoid
4105227825Stheraven__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4106227825Stheraven                _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4107227825Stheraven                                 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4108227825Stheraven                typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4109227825Stheraven{
4110227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4111227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4112227825Stheraven    while (true)
4113227825Stheraven    {
4114227825Stheraven        // if __middle == __last, we're done
4115227825Stheraven        if (__len2 == 0)
4116227825Stheraven            return;
4117227825Stheraven        // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4118227825Stheraven        for (; true; ++__first, --__len1)
4119227825Stheraven        {
4120227825Stheraven            if (__len1 == 0)
4121227825Stheraven                return;
4122227825Stheraven            if (__comp(*__middle, *__first))
4123227825Stheraven                break;
4124227825Stheraven        }
4125227825Stheraven        if (__len1 <= __buff_size || __len2 <= __buff_size)
4126227825Stheraven        {
4127227825Stheraven            __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
4128227825Stheraven            return;
4129227825Stheraven        }
4130227825Stheraven        // __first < __middle < __last
4131227825Stheraven        // *__first > *__middle
4132227825Stheraven        // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4133227825Stheraven        //     all elements in:
4134227825Stheraven        //         [__first, __m1)  <= [__middle, __m2)
4135227825Stheraven        //         [__middle, __m2) <  [__m1, __middle)
4136227825Stheraven        //         [__m1, __middle) <= [__m2, __last)
4137227825Stheraven        //     and __m1 or __m2 is in the middle of its range
4138227825Stheraven        _BidirectionalIterator __m1;  // "median" of [__first, __middle)
4139227825Stheraven        _BidirectionalIterator __m2;  // "median" of [__middle, __last)
4140227825Stheraven        difference_type __len11;      // distance(__first, __m1)
4141227825Stheraven        difference_type __len21;      // distance(__middle, __m2)
4142227825Stheraven        // binary search smaller range
4143227825Stheraven        if (__len1 < __len2)
4144227825Stheraven        {   // __len >= 1, __len2 >= 2
4145227825Stheraven            __len21 = __len2 / 2;
4146227825Stheraven            __m2 = __middle;
4147227825Stheraven            _VSTD::advance(__m2, __len21);
4148227825Stheraven            __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
4149227825Stheraven            __len11 = _VSTD::distance(__first, __m1);
4150227825Stheraven        }
4151227825Stheraven        else
4152227825Stheraven        {
4153227825Stheraven            if (__len1 == 1)
4154227825Stheraven            {   // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4155227825Stheraven                // It is known *__first > *__middle
4156227825Stheraven                swap(*__first, *__middle);
4157227825Stheraven                return;
4158227825Stheraven            }
4159227825Stheraven            // __len1 >= 2, __len2 >= 1
4160227825Stheraven            __len11 = __len1 / 2;
4161227825Stheraven            __m1 = __first;
4162227825Stheraven            _VSTD::advance(__m1, __len11);
4163227825Stheraven            __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
4164227825Stheraven            __len21 = _VSTD::distance(__middle, __m2);
4165227825Stheraven        }
4166227825Stheraven        difference_type __len12 = __len1 - __len11;  // distance(__m1, __middle)
4167227825Stheraven        difference_type __len22 = __len2 - __len21;  // distance(__m2, __last)
4168227825Stheraven        // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4169227825Stheraven        // swap middle two partitions
4170227825Stheraven        __middle = _VSTD::rotate(__m1, __middle, __m2);
4171227825Stheraven        // __len12 and __len21 now have swapped meanings
4172227825Stheraven        // merge smaller range with recurisve call and larger with tail recursion elimination
4173227825Stheraven        if (__len11 + __len21 < __len12 + __len22)
4174227825Stheraven        {
4175227825Stheraven            __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4176227825Stheraven//          __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4177227825Stheraven            __first = __middle;
4178227825Stheraven            __middle = __m2;
4179227825Stheraven            __len1 = __len12;
4180227825Stheraven            __len2 = __len22;
4181227825Stheraven        }
4182227825Stheraven        else
4183227825Stheraven        {
4184227825Stheraven            __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4185227825Stheraven//          __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4186227825Stheraven            __last = __middle;
4187227825Stheraven            __middle = __m1;
4188227825Stheraven            __len1 = __len11;
4189227825Stheraven            __len2 = __len21;
4190227825Stheraven        }
4191227825Stheraven    }
4192227825Stheraven}
4193227825Stheraven
4194227825Stheraventemplate <class _Tp>
4195227825Stheravenstruct __inplace_merge_switch
4196227825Stheraven{
4197227825Stheraven    static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
4198227825Stheraven};
4199227825Stheraven
4200227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
4201227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4202227825Stheravenvoid
4203227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4204227825Stheraven              _Compare __comp)
4205227825Stheraven{
4206227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4207227825Stheraven    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4208227825Stheraven    difference_type __len1 = _VSTD::distance(__first, __middle);
4209227825Stheraven    difference_type __len2 = _VSTD::distance(__middle, __last);
4210227825Stheraven    difference_type __buf_size = _VSTD::min(__len1, __len2);
4211227825Stheraven    pair<value_type*, ptrdiff_t> __buf(0, 0);
4212227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
4213227825Stheraven    if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4214227825Stheraven    {
4215227825Stheraven        __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4216227825Stheraven        __h.reset(__buf.first);
4217227825Stheraven    }
4218227825Stheraven#ifdef _LIBCPP_DEBUG2
4219227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4220227825Stheraven    __debug_less<_Compare> __c(__comp);
4221227825Stheraven    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
4222227825Stheraven                                            __buf.first, __buf.second);
4223227825Stheraven#else  // _LIBCPP_DEBUG2
4224227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4225227825Stheraven    return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
4226227825Stheraven                                            __buf.first, __buf.second);
4227227825Stheraven#endif  // _LIBCPP_DEBUG2
4228227825Stheraven}
4229227825Stheraven
4230227825Stheraventemplate <class _BidirectionalIterator>
4231227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4232227825Stheravenvoid
4233227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4234227825Stheraven{
4235227825Stheraven    _VSTD::inplace_merge(__first, __middle, __last,
4236227825Stheraven                        __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4237227825Stheraven}
4238227825Stheraven
4239227825Stheraven// stable_sort
4240227825Stheraven
4241227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
4242227825Stheravenvoid
4243227825Stheraven__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4244227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2,
4245227825Stheraven        typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4246227825Stheraven{
4247227825Stheraven    typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4248227825Stheraven    __destruct_n __d(0);
4249227825Stheraven    unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4250227825Stheraven    for (; true; ++__result)
4251227825Stheraven    {
4252227825Stheraven        if (__first1 == __last1)
4253227825Stheraven        {
4254227825Stheraven            for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4255227825Stheraven                ::new (__result) value_type(_VSTD::move(*__first2));
4256227825Stheraven            __h.release();
4257227825Stheraven            return;
4258227825Stheraven        }
4259227825Stheraven        if (__first2 == __last2)
4260227825Stheraven        {
4261227825Stheraven            for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4262227825Stheraven                ::new (__result) value_type(_VSTD::move(*__first1));
4263227825Stheraven            __h.release();
4264227825Stheraven            return;
4265227825Stheraven        }
4266227825Stheraven        if (__comp(*__first2, *__first1))
4267227825Stheraven        {
4268227825Stheraven            ::new (__result) value_type(_VSTD::move(*__first2));
4269227825Stheraven            __d.__incr((value_type*)0);
4270227825Stheraven            ++__first2;
4271227825Stheraven        }
4272227825Stheraven        else
4273227825Stheraven        {
4274227825Stheraven            ::new (__result) value_type(_VSTD::move(*__first1));
4275227825Stheraven            __d.__incr((value_type*)0);
4276227825Stheraven            ++__first1;
4277227825Stheraven        }
4278227825Stheraven    }
4279227825Stheraven}
4280227825Stheraven
4281227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4282227825Stheravenvoid
4283227825Stheraven__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4284227825Stheraven        _InputIterator2 __first2, _InputIterator2 __last2,
4285227825Stheraven        _OutputIterator __result, _Compare __comp)
4286227825Stheraven{
4287227825Stheraven    for (; __first1 != __last1; ++__result)
4288227825Stheraven    {
4289227825Stheraven        if (__first2 == __last2)
4290227825Stheraven        {
4291227825Stheraven            for (; __first1 != __last1; ++__first1, ++__result)
4292227825Stheraven                *__result = _VSTD::move(*__first1);
4293227825Stheraven            return;
4294227825Stheraven        }
4295227825Stheraven        if (__comp(*__first2, *__first1))
4296227825Stheraven        {
4297227825Stheraven            *__result = _VSTD::move(*__first2);
4298227825Stheraven            ++__first2;
4299227825Stheraven        }
4300227825Stheraven        else
4301227825Stheraven        {
4302227825Stheraven            *__result = _VSTD::move(*__first1);
4303227825Stheraven            ++__first1;
4304227825Stheraven        }
4305227825Stheraven    }
4306227825Stheraven    for (; __first2 != __last2; ++__first2, ++__result)
4307227825Stheraven        *__result = _VSTD::move(*__first2);
4308227825Stheraven}
4309227825Stheraven
4310227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4311227825Stheravenvoid
4312227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4313227825Stheraven              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4314227825Stheraven              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4315227825Stheraven
4316227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4317227825Stheravenvoid
4318227825Stheraven__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4319227825Stheraven                   typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4320227825Stheraven                   typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4321227825Stheraven{
4322227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4323227825Stheraven    switch (__len)
4324227825Stheraven    {
4325227825Stheraven    case 0:
4326227825Stheraven        return;
4327227825Stheraven    case 1:
4328227825Stheraven        ::new(__first2) value_type(_VSTD::move(*__first1));
4329227825Stheraven        return;
4330227825Stheraven    case 2:
4331227825Stheraven       __destruct_n __d(0);
4332227825Stheraven        unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4333227825Stheraven         if (__comp(*--__last1, *__first1))
4334227825Stheraven        {
4335227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__last1));
4336227825Stheraven            __d.__incr((value_type*)0);
4337227825Stheraven            ++__first2;
4338227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__first1));
4339227825Stheraven        }
4340227825Stheraven        else
4341227825Stheraven        {
4342227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__first1));
4343227825Stheraven            __d.__incr((value_type*)0);
4344227825Stheraven            ++__first2;
4345227825Stheraven            ::new(__first2) value_type(_VSTD::move(*__last1));
4346227825Stheraven        }
4347227825Stheraven        __h2.release();
4348227825Stheraven        return;
4349227825Stheraven    }
4350227825Stheraven    if (__len <= 8)
4351227825Stheraven    {
4352227825Stheraven        __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4353227825Stheraven        return;
4354227825Stheraven    }
4355227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4356227825Stheraven    _RandomAccessIterator __m = __first1 + __l2;
4357227825Stheraven    __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4358227825Stheraven    __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4359227825Stheraven    __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4360227825Stheraven}
4361227825Stheraven
4362227825Stheraventemplate <class _Tp>
4363227825Stheravenstruct __stable_sort_switch
4364227825Stheraven{
4365227825Stheraven    static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
4366227825Stheraven};
4367227825Stheraven
4368227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4369227825Stheravenvoid
4370227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4371227825Stheraven              typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4372227825Stheraven              typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4373227825Stheraven{
4374227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4375227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4376227825Stheraven    switch (__len)
4377227825Stheraven    {
4378227825Stheraven    case 0:
4379227825Stheraven    case 1:
4380227825Stheraven        return;
4381227825Stheraven    case 2:
4382227825Stheraven        if (__comp(*--__last, *__first))
4383227825Stheraven            swap(*__first, *__last);
4384227825Stheraven        return;
4385227825Stheraven    }
4386227825Stheraven    if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4387227825Stheraven    {
4388227825Stheraven        __insertion_sort<_Compare>(__first, __last, __comp);
4389227825Stheraven        return;
4390227825Stheraven    }
4391227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4392227825Stheraven    _RandomAccessIterator __m = __first + __l2;
4393227825Stheraven    if (__len <= __buff_size)
4394227825Stheraven    {
4395227825Stheraven        __destruct_n __d(0);
4396227825Stheraven        unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4397227825Stheraven        __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4398227825Stheraven        __d.__set(__l2, (value_type*)0);
4399227825Stheraven        __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4400227825Stheraven        __d.__set(__len, (value_type*)0);
4401227825Stheraven        __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4402227825Stheraven//         __merge<_Compare>(move_iterator<value_type*>(__buff),
4403227825Stheraven//                           move_iterator<value_type*>(__buff + __l2),
4404227825Stheraven//                           move_iterator<_RandomAccessIterator>(__buff + __l2),
4405227825Stheraven//                           move_iterator<_RandomAccessIterator>(__buff + __len),
4406227825Stheraven//                           __first, __comp);
4407227825Stheraven        return;
4408227825Stheraven    }
4409227825Stheraven    __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4410227825Stheraven    __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4411227825Stheraven    __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4412227825Stheraven}
4413227825Stheraven
4414227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4415227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4416227825Stheravenvoid
4417227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4418227825Stheraven{
4419227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4420227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4421227825Stheraven    difference_type __len = __last - __first;
4422227825Stheraven    pair<value_type*, ptrdiff_t> __buf(0, 0);
4423227825Stheraven    unique_ptr<value_type, __return_temporary_buffer> __h;
4424227825Stheraven    if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4425227825Stheraven    {
4426227825Stheraven        __buf = _VSTD::get_temporary_buffer<value_type>(__len);
4427227825Stheraven        __h.reset(__buf.first);
4428227825Stheraven    }
4429227825Stheraven#ifdef _LIBCPP_DEBUG2
4430227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4431227825Stheraven    __debug_less<_Compare> __c(__comp);
4432227825Stheraven    __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4433227825Stheraven#else  // _LIBCPP_DEBUG2
4434227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4435227825Stheraven    __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4436227825Stheraven#endif  // _LIBCPP_DEBUG2
4437227825Stheraven}
4438227825Stheraven
4439227825Stheraventemplate <class _RandomAccessIterator>
4440227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4441227825Stheravenvoid
4442227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4443227825Stheraven{
4444227825Stheraven    _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4445227825Stheraven}
4446227825Stheraven
4447227825Stheraven// is_heap_until
4448227825Stheraven
4449227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4450227825Stheraven_RandomAccessIterator
4451227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4452227825Stheraven{
4453227825Stheraven    typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4454227825Stheraven    difference_type __len = __last - __first;
4455227825Stheraven    difference_type __p = 0;
4456227825Stheraven    difference_type __c = 1;
4457227825Stheraven    _RandomAccessIterator __pp = __first;
4458227825Stheraven    while (__c < __len)
4459227825Stheraven    {
4460227825Stheraven        _RandomAccessIterator __cp = __first + __c;
4461227825Stheraven        if (__comp(*__pp, *__cp))
4462227825Stheraven            return __cp;
4463227825Stheraven        ++__c;
4464227825Stheraven        ++__cp;
4465227825Stheraven        if (__c == __len)
4466227825Stheraven            return __last;
4467227825Stheraven        if (__comp(*__pp, *__cp))
4468227825Stheraven            return __cp;
4469227825Stheraven        ++__p;
4470227825Stheraven        ++__pp;
4471227825Stheraven        __c = 2 * __p + 1;
4472227825Stheraven    }
4473227825Stheraven    return __last;
4474227825Stheraven}
4475227825Stheraven
4476227825Stheraventemplate<class _RandomAccessIterator>
4477227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4478227825Stheraven_RandomAccessIterator
4479227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4480227825Stheraven{
4481227825Stheraven    return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4482227825Stheraven}
4483227825Stheraven
4484227825Stheraven// is_heap
4485227825Stheraven
4486227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4487227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4488227825Stheravenbool
4489227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4490227825Stheraven{
4491227825Stheraven    return _VSTD::is_heap_until(__first, __last, __comp) == __last;
4492227825Stheraven}
4493227825Stheraven
4494227825Stheraventemplate<class _RandomAccessIterator>
4495227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4496227825Stheravenbool
4497227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4498227825Stheraven{
4499227825Stheraven    return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4500227825Stheraven}
4501227825Stheraven
4502227825Stheraven// push_heap
4503227825Stheraven
4504227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4505227825Stheravenvoid
4506227825Stheraven__push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
4507227825Stheraven                  typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4508227825Stheraven{
4509227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4510227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4511227825Stheraven    if (__len > 1)
4512227825Stheraven    {
4513227825Stheraven        difference_type __p = 0;
4514227825Stheraven        _RandomAccessIterator __pp = __first;
4515227825Stheraven        difference_type __c = 2;
4516227825Stheraven        _RandomAccessIterator __cp = __first + __c;
4517227825Stheraven        if (__c == __len || __comp(*__cp, *(__cp - 1)))
4518227825Stheraven        {
4519227825Stheraven            --__c;
4520227825Stheraven            --__cp;
4521227825Stheraven        }
4522227825Stheraven        if (__comp(*__pp, *__cp))
4523227825Stheraven        {
4524227825Stheraven            value_type __t(_VSTD::move(*__pp));
4525227825Stheraven            do
4526227825Stheraven            {
4527227825Stheraven                *__pp = _VSTD::move(*__cp);
4528227825Stheraven                __pp = __cp;
4529227825Stheraven                __p = __c;
4530227825Stheraven                __c = (__p + 1) * 2;
4531227825Stheraven                if (__c > __len)
4532227825Stheraven                    break;
4533227825Stheraven                __cp = __first + __c;
4534227825Stheraven                if (__c == __len || __comp(*__cp, *(__cp - 1)))
4535227825Stheraven                {
4536227825Stheraven                    --__c;
4537227825Stheraven                    --__cp;
4538227825Stheraven                }
4539227825Stheraven            } while (__comp(__t, *__cp));
4540227825Stheraven            *__pp = _VSTD::move(__t);
4541227825Stheraven        }
4542227825Stheraven    }
4543227825Stheraven}
4544227825Stheraven
4545227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4546227825Stheravenvoid
4547227825Stheraven__push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4548227825Stheraven                 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4549227825Stheraven{
4550227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4551227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4552227825Stheraven    if (__len > 1)
4553227825Stheraven    {
4554227825Stheraven        __len = (__len - 2) / 2;
4555227825Stheraven        _RandomAccessIterator __ptr = __first + __len;
4556227825Stheraven        if (__comp(*__ptr, *--__last))
4557227825Stheraven        {
4558227825Stheraven            value_type __t(_VSTD::move(*__last));
4559227825Stheraven            do
4560227825Stheraven            {
4561227825Stheraven                *__last = _VSTD::move(*__ptr);
4562227825Stheraven                __last = __ptr;
4563227825Stheraven                if (__len == 0)
4564227825Stheraven                    break;
4565227825Stheraven                __len = (__len - 1) / 2;
4566227825Stheraven                __ptr = __first + __len;
4567227825Stheraven            } while (__comp(*__ptr, __t));
4568227825Stheraven            *__last = _VSTD::move(__t);
4569227825Stheraven        }
4570227825Stheraven    }
4571227825Stheraven}
4572227825Stheraven
4573227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4574227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4575227825Stheravenvoid
4576227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4577227825Stheraven{
4578227825Stheraven#ifdef _LIBCPP_DEBUG2
4579227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4580227825Stheraven    __debug_less<_Compare> __c(__comp);
4581227825Stheraven    __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
4582227825Stheraven#else  // _LIBCPP_DEBUG2
4583227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4584227825Stheraven    __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
4585227825Stheraven#endif  // _LIBCPP_DEBUG2
4586227825Stheraven}
4587227825Stheraven
4588227825Stheraventemplate <class _RandomAccessIterator>
4589227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4590227825Stheravenvoid
4591227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4592227825Stheraven{
4593227825Stheraven    _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4594227825Stheraven}
4595227825Stheraven
4596227825Stheraven// pop_heap
4597227825Stheraven
4598227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4599227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4600227825Stheravenvoid
4601227825Stheraven__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4602227825Stheraven           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4603227825Stheraven{
4604227825Stheraven    if (__len > 1)
4605227825Stheraven    {
4606227825Stheraven        swap(*__first, *--__last);
4607227825Stheraven        __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
4608227825Stheraven    }
4609227825Stheraven}
4610227825Stheraven
4611227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4612227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4613227825Stheravenvoid
4614227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4615227825Stheraven{
4616227825Stheraven#ifdef _LIBCPP_DEBUG2
4617227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4618227825Stheraven    __debug_less<_Compare> __c(__comp);
4619227825Stheraven    __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4620227825Stheraven#else  // _LIBCPP_DEBUG2
4621227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4622227825Stheraven    __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4623227825Stheraven#endif  // _LIBCPP_DEBUG2
4624227825Stheraven}
4625227825Stheraven
4626227825Stheraventemplate <class _RandomAccessIterator>
4627227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4628227825Stheravenvoid
4629227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4630227825Stheraven{
4631227825Stheraven    _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4632227825Stheraven}
4633227825Stheraven
4634227825Stheraven// make_heap
4635227825Stheraven
4636227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4637227825Stheravenvoid
4638227825Stheraven__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4639227825Stheraven{
4640227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4641227825Stheraven    difference_type __n = __last - __first;
4642227825Stheraven    if (__n > 1)
4643227825Stheraven    {
4644227825Stheraven        __last = __first;
4645227825Stheraven        ++__last;
4646227825Stheraven        for (difference_type __i = 1; __i < __n;)
4647227825Stheraven            __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
4648227825Stheraven    }
4649227825Stheraven}
4650227825Stheraven
4651227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4652227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4653227825Stheravenvoid
4654227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4655227825Stheraven{
4656227825Stheraven#ifdef _LIBCPP_DEBUG2
4657227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4658227825Stheraven    __debug_less<_Compare> __c(__comp);
4659227825Stheraven    __make_heap<_Comp_ref>(__first, __last, __c);
4660227825Stheraven#else  // _LIBCPP_DEBUG2
4661227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4662227825Stheraven    __make_heap<_Comp_ref>(__first, __last, __comp);
4663227825Stheraven#endif  // _LIBCPP_DEBUG2
4664227825Stheraven}
4665227825Stheraven
4666227825Stheraventemplate <class _RandomAccessIterator>
4667227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4668227825Stheravenvoid
4669227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4670227825Stheraven{
4671227825Stheraven    _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4672227825Stheraven}
4673227825Stheraven
4674227825Stheraven// sort_heap
4675227825Stheraven
4676227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4677227825Stheravenvoid
4678227825Stheraven__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4679227825Stheraven{
4680227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4681227825Stheraven    for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4682227825Stheraven        __pop_heap<_Compare>(__first, __last, __comp, __n);
4683227825Stheraven}
4684227825Stheraven
4685227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4686227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4687227825Stheravenvoid
4688227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4689227825Stheraven{
4690227825Stheraven#ifdef _LIBCPP_DEBUG2
4691227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4692227825Stheraven    __debug_less<_Compare> __c(__comp);
4693227825Stheraven    __sort_heap<_Comp_ref>(__first, __last, __c);
4694227825Stheraven#else  // _LIBCPP_DEBUG2
4695227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4696227825Stheraven    __sort_heap<_Comp_ref>(__first, __last, __comp);
4697227825Stheraven#endif  // _LIBCPP_DEBUG2
4698227825Stheraven}
4699227825Stheraven
4700227825Stheraventemplate <class _RandomAccessIterator>
4701227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4702227825Stheravenvoid
4703227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4704227825Stheraven{
4705227825Stheraven    _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4706227825Stheraven}
4707227825Stheraven
4708227825Stheraven// partial_sort
4709227825Stheraven
4710227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4711227825Stheravenvoid
4712227825Stheraven__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4713227825Stheraven             _Compare __comp)
4714227825Stheraven{
4715227825Stheraven    __make_heap<_Compare>(__first, __middle, __comp);
4716227825Stheraven    typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4717227825Stheraven    for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4718227825Stheraven    {
4719227825Stheraven        if (__comp(*__i, *__first))
4720227825Stheraven        {
4721227825Stheraven            swap(*__i, *__first);
4722227825Stheraven            __push_heap_front<_Compare>(__first, __middle, __comp, __len);
4723227825Stheraven        }
4724227825Stheraven    }
4725227825Stheraven    __sort_heap<_Compare>(__first, __middle, __comp);
4726227825Stheraven}
4727227825Stheraven
4728227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4729227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4730227825Stheravenvoid
4731227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4732227825Stheraven             _Compare __comp)
4733227825Stheraven{
4734227825Stheraven#ifdef _LIBCPP_DEBUG2
4735227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4736227825Stheraven    __debug_less<_Compare> __c(__comp);
4737227825Stheraven    __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
4738227825Stheraven#else  // _LIBCPP_DEBUG2
4739227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4740227825Stheraven    __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
4741227825Stheraven#endif  // _LIBCPP_DEBUG2
4742227825Stheraven}
4743227825Stheraven
4744227825Stheraventemplate <class _RandomAccessIterator>
4745227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4746227825Stheravenvoid
4747227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
4748227825Stheraven{
4749227825Stheraven    _VSTD::partial_sort(__first, __middle, __last,
4750227825Stheraven                       __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4751227825Stheraven}
4752227825Stheraven
4753227825Stheraven// partial_sort_copy
4754227825Stheraven
4755227825Stheraventemplate <class _Compare, class _InputIterator, class _RandomAccessIterator>
4756227825Stheraven_RandomAccessIterator
4757227825Stheraven__partial_sort_copy(_InputIterator __first, _InputIterator __last,
4758227825Stheraven                    _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4759227825Stheraven{
4760227825Stheraven    _RandomAccessIterator __r = __result_first;
4761227825Stheraven    if (__r != __result_last)
4762227825Stheraven    {
4763227825Stheraven        typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
4764227825Stheraven        for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
4765227825Stheraven            *__r = *__first;
4766227825Stheraven        __make_heap<_Compare>(__result_first, __r, __comp);
4767227825Stheraven        for (; __first != __last; ++__first)
4768227825Stheraven            if (__comp(*__first, *__result_first))
4769227825Stheraven            {
4770227825Stheraven                *__result_first = *__first;
4771227825Stheraven                __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
4772227825Stheraven            }
4773227825Stheraven        __sort_heap<_Compare>(__result_first, __r, __comp);
4774227825Stheraven    }
4775227825Stheraven    return __r;
4776227825Stheraven}
4777227825Stheraven
4778227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator, class _Compare>
4779227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4780227825Stheraven_RandomAccessIterator
4781227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last,
4782227825Stheraven                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4783227825Stheraven{
4784227825Stheraven#ifdef _LIBCPP_DEBUG2
4785227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4786227825Stheraven    __debug_less<_Compare> __c(__comp);
4787227825Stheraven    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
4788227825Stheraven#else  // _LIBCPP_DEBUG2
4789227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4790227825Stheraven    return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
4791227825Stheraven#endif  // _LIBCPP_DEBUG2
4792227825Stheraven}
4793227825Stheraven
4794227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator>
4795227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4796227825Stheraven_RandomAccessIterator
4797227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last,
4798227825Stheraven                  _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
4799227825Stheraven{
4800227825Stheraven    return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
4801227825Stheraven                                   __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4802227825Stheraven}
4803227825Stheraven
4804227825Stheraven// nth_element
4805227825Stheraven
4806227825Stheraventemplate <class _Compare, class _RandomAccessIterator>
4807227825Stheravenvoid
4808227825Stheraven__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4809227825Stheraven{
4810227825Stheraven    // _Compare is known to be a reference type
4811227825Stheraven    typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4812227825Stheraven    const difference_type __limit = 7;
4813227825Stheraven    while (true)
4814227825Stheraven    {
4815227825Stheraven    __restart:
4816232950Stheraven        if (__nth == __last)
4817232950Stheraven            return;
4818227825Stheraven        difference_type __len = __last - __first;
4819227825Stheraven        switch (__len)
4820227825Stheraven        {
4821227825Stheraven        case 0:
4822227825Stheraven        case 1:
4823227825Stheraven            return;
4824227825Stheraven        case 2:
4825227825Stheraven            if (__comp(*--__last, *__first))
4826227825Stheraven                swap(*__first, *__last);
4827227825Stheraven            return;
4828227825Stheraven        case 3:
4829227825Stheraven            {
4830227825Stheraven            _RandomAccessIterator __m = __first;
4831227825Stheraven            _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
4832227825Stheraven            return;
4833227825Stheraven            }
4834227825Stheraven        }
4835227825Stheraven        if (__len <= __limit)
4836227825Stheraven        {
4837227825Stheraven            __selection_sort<_Compare>(__first, __last, __comp);
4838227825Stheraven            return;
4839227825Stheraven        }
4840227825Stheraven        // __len > __limit >= 3
4841227825Stheraven        _RandomAccessIterator __m = __first + __len/2;
4842227825Stheraven        _RandomAccessIterator __lm1 = __last;
4843227825Stheraven        unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
4844227825Stheraven        // *__m is median
4845227825Stheraven        // partition [__first, __m) < *__m and *__m <= [__m, __last)
4846227825Stheraven        // (this inhibits tossing elements equivalent to __m around unnecessarily)
4847227825Stheraven        _RandomAccessIterator __i = __first;
4848227825Stheraven        _RandomAccessIterator __j = __lm1;
4849227825Stheraven        // j points beyond range to be tested, *__lm1 is known to be <= *__m
4850227825Stheraven        // The search going up is known to be guarded but the search coming down isn't.
4851227825Stheraven        // Prime the downward search with a guard.
4852227825Stheraven        if (!__comp(*__i, *__m))  // if *__first == *__m
4853227825Stheraven        {
4854227825Stheraven            // *__first == *__m, *__first doesn't go in first part
4855227825Stheraven            // manually guard downward moving __j against __i
4856227825Stheraven            while (true)
4857227825Stheraven            {
4858227825Stheraven                if (__i == --__j)
4859227825Stheraven                {
4860227825Stheraven                    // *__first == *__m, *__m <= all other elements
4861227825Stheraven                    // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4862227825Stheraven                    ++__i;  // __first + 1
4863227825Stheraven                    __j = __last;
4864227825Stheraven                    if (!__comp(*__first, *--__j))  // we need a guard if *__first == *(__last-1)
4865227825Stheraven                    {
4866227825Stheraven                        while (true)
4867227825Stheraven                        {
4868227825Stheraven                            if (__i == __j)
4869227825Stheraven                                return;  // [__first, __last) all equivalent elements
4870227825Stheraven                            if (__comp(*__first, *__i))
4871227825Stheraven                            {
4872227825Stheraven                                swap(*__i, *__j);
4873227825Stheraven                                ++__n_swaps;
4874227825Stheraven                                ++__i;
4875227825Stheraven                                break;
4876227825Stheraven                            }
4877227825Stheraven                            ++__i;
4878227825Stheraven                        }
4879227825Stheraven                    }
4880227825Stheraven                    // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4881227825Stheraven                    if (__i == __j)
4882227825Stheraven                        return;
4883227825Stheraven                    while (true)
4884227825Stheraven                    {
4885227825Stheraven                        while (!__comp(*__first, *__i))
4886227825Stheraven                            ++__i;
4887227825Stheraven                        while (__comp(*__first, *--__j))
4888227825Stheraven                            ;
4889227825Stheraven                        if (__i >= __j)
4890227825Stheraven                            break;
4891227825Stheraven                        swap(*__i, *__j);
4892227825Stheraven                        ++__n_swaps;
4893227825Stheraven                        ++__i;
4894227825Stheraven                    }
4895227825Stheraven                    // [__first, __i) == *__first and *__first < [__i, __last)
4896227825Stheraven                    // The first part is sorted,
4897227825Stheraven                    if (__nth < __i)
4898227825Stheraven                        return;
4899227825Stheraven                    // __nth_element the secod part
4900227825Stheraven                    // __nth_element<_Compare>(__i, __nth, __last, __comp);
4901227825Stheraven                    __first = __i;
4902227825Stheraven                    goto __restart;
4903227825Stheraven                }
4904227825Stheraven                if (__comp(*__j, *__m))
4905227825Stheraven                {
4906227825Stheraven                    swap(*__i, *__j);
4907227825Stheraven                    ++__n_swaps;
4908227825Stheraven                    break;  // found guard for downward moving __j, now use unguarded partition
4909227825Stheraven                }
4910227825Stheraven            }
4911227825Stheraven        }
4912227825Stheraven        ++__i;
4913227825Stheraven        // j points beyond range to be tested, *__lm1 is known to be <= *__m
4914227825Stheraven        // if not yet partitioned...
4915227825Stheraven        if (__i < __j)
4916227825Stheraven        {
4917227825Stheraven            // known that *(__i - 1) < *__m
4918227825Stheraven            while (true)
4919227825Stheraven            {
4920227825Stheraven                // __m still guards upward moving __i
4921227825Stheraven                while (__comp(*__i, *__m))
4922227825Stheraven                    ++__i;
4923227825Stheraven                // It is now known that a guard exists for downward moving __j
4924227825Stheraven                while (!__comp(*--__j, *__m))
4925227825Stheraven                    ;
4926227825Stheraven                if (__i >= __j)
4927227825Stheraven                    break;
4928227825Stheraven                swap(*__i, *__j);
4929227825Stheraven                ++__n_swaps;
4930227825Stheraven                // It is known that __m != __j
4931227825Stheraven                // If __m just moved, follow it
4932227825Stheraven                if (__m == __i)
4933227825Stheraven                    __m = __j;
4934227825Stheraven                ++__i;
4935227825Stheraven            }
4936227825Stheraven        }
4937227825Stheraven        // [__first, __i) < *__m and *__m <= [__i, __last)
4938227825Stheraven        if (__i != __m && __comp(*__m, *__i))
4939227825Stheraven        {
4940227825Stheraven            swap(*__i, *__m);
4941227825Stheraven            ++__n_swaps;
4942227825Stheraven        }
4943227825Stheraven        // [__first, __i) < *__i and *__i <= [__i+1, __last)
4944227825Stheraven        if (__nth == __i)
4945227825Stheraven            return;
4946227825Stheraven        if (__n_swaps == 0)
4947227825Stheraven        {
4948227825Stheraven            // We were given a perfectly partitioned sequence.  Coincidence?
4949227825Stheraven            if (__nth < __i)
4950227825Stheraven            {
4951227825Stheraven                // Check for [__first, __i) already sorted
4952227825Stheraven                __j = __m = __first;
4953227825Stheraven                while (++__j != __i)
4954227825Stheraven                {
4955227825Stheraven                    if (__comp(*__j, *__m))
4956227825Stheraven                        // not yet sorted, so sort
4957227825Stheraven                        goto not_sorted;
4958227825Stheraven                    __m = __j;
4959227825Stheraven                }
4960227825Stheraven                // [__first, __i) sorted
4961227825Stheraven                return;
4962227825Stheraven            }
4963227825Stheraven            else
4964227825Stheraven            {
4965227825Stheraven                // Check for [__i, __last) already sorted
4966227825Stheraven                __j = __m = __i;
4967227825Stheraven                while (++__j != __last)
4968227825Stheraven                {
4969227825Stheraven                    if (__comp(*__j, *__m))
4970227825Stheraven                        // not yet sorted, so sort
4971227825Stheraven                        goto not_sorted;
4972227825Stheraven                    __m = __j;
4973227825Stheraven                }
4974227825Stheraven                // [__i, __last) sorted
4975227825Stheraven                return;
4976227825Stheraven            }
4977227825Stheraven        }
4978227825Stheravennot_sorted:
4979227825Stheraven        // __nth_element on range containing __nth
4980227825Stheraven        if (__nth < __i)
4981227825Stheraven        {
4982227825Stheraven            // __nth_element<_Compare>(__first, __nth, __i, __comp);
4983227825Stheraven            __last = __i;
4984227825Stheraven        }
4985227825Stheraven        else
4986227825Stheraven        {
4987227825Stheraven            // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
4988227825Stheraven            __first = ++__i;
4989227825Stheraven        }
4990227825Stheraven    }
4991227825Stheraven}
4992227825Stheraven
4993227825Stheraventemplate <class _RandomAccessIterator, class _Compare>
4994227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
4995227825Stheravenvoid
4996227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4997227825Stheraven{
4998227825Stheraven#ifdef _LIBCPP_DEBUG2
4999227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5000227825Stheraven    __debug_less<_Compare> __c(__comp);
5001227825Stheraven    __nth_element<_Comp_ref>(__first, __nth, __last, __c);
5002227825Stheraven#else  // _LIBCPP_DEBUG2
5003227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5004227825Stheraven    __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
5005227825Stheraven#endif  // _LIBCPP_DEBUG2
5006227825Stheraven}
5007227825Stheraven
5008227825Stheraventemplate <class _RandomAccessIterator>
5009227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5010227825Stheravenvoid
5011227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5012227825Stheraven{
5013227825Stheraven    _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5014227825Stheraven}
5015227825Stheraven
5016227825Stheraven// includes
5017227825Stheraven
5018227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
5019227825Stheravenbool
5020227825Stheraven__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5021227825Stheraven           _Compare __comp)
5022227825Stheraven{
5023227825Stheraven    for (; __first2 != __last2; ++__first1)
5024227825Stheraven    {
5025227825Stheraven        if (__first1 == __last1 || __comp(*__first2, *__first1))
5026227825Stheraven            return false;
5027227825Stheraven        if (!__comp(*__first1, *__first2))
5028227825Stheraven            ++__first2;
5029227825Stheraven    }
5030227825Stheraven    return true;
5031227825Stheraven}
5032227825Stheraven
5033227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare>
5034227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5035227825Stheravenbool
5036227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5037227825Stheraven         _Compare __comp)
5038227825Stheraven{
5039227825Stheraven#ifdef _LIBCPP_DEBUG2
5040227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5041227825Stheraven    __debug_less<_Compare> __c(__comp);
5042227825Stheraven    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5043227825Stheraven#else  // _LIBCPP_DEBUG2
5044227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5045227825Stheraven    return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5046227825Stheraven#endif  // _LIBCPP_DEBUG2
5047227825Stheraven}
5048227825Stheraven
5049227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
5050227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5051227825Stheravenbool
5052227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5053227825Stheraven{
5054227825Stheraven    return _VSTD::includes(__first1, __last1, __first2, __last2,
5055227825Stheraven                          __less<typename iterator_traits<_InputIterator1>::value_type,
5056227825Stheraven                                 typename iterator_traits<_InputIterator2>::value_type>());
5057227825Stheraven}
5058227825Stheraven
5059227825Stheraven// set_union
5060227825Stheraven
5061227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5062227825Stheraven_OutputIterator
5063227825Stheraven__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5064227825Stheraven            _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5065227825Stheraven{
5066227825Stheraven    for (; __first1 != __last1; ++__result)
5067227825Stheraven    {
5068227825Stheraven        if (__first2 == __last2)
5069227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5070227825Stheraven        if (__comp(*__first2, *__first1))
5071227825Stheraven        {
5072227825Stheraven            *__result = *__first2;
5073227825Stheraven            ++__first2;
5074227825Stheraven        }
5075227825Stheraven        else
5076227825Stheraven        {
5077227825Stheraven            *__result = *__first1;
5078227825Stheraven            if (!__comp(*__first1, *__first2))
5079227825Stheraven                ++__first2;
5080227825Stheraven            ++__first1;
5081227825Stheraven        }
5082227825Stheraven    }
5083227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
5084227825Stheraven}
5085227825Stheraven
5086227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5087227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5088227825Stheraven_OutputIterator
5089227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1,
5090227825Stheraven          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5091227825Stheraven{
5092227825Stheraven#ifdef _LIBCPP_DEBUG2
5093227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5094227825Stheraven    __debug_less<_Compare> __c(__comp);
5095227825Stheraven    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5096227825Stheraven#else  // _LIBCPP_DEBUG2
5097227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5098227825Stheraven    return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5099227825Stheraven#endif  // _LIBCPP_DEBUG2
5100227825Stheraven}
5101227825Stheraven
5102227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5103227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5104227825Stheraven_OutputIterator
5105227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1,
5106227825Stheraven          _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5107227825Stheraven{
5108227825Stheraven    return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
5109227825Stheraven                          __less<typename iterator_traits<_InputIterator1>::value_type,
5110227825Stheraven                                 typename iterator_traits<_InputIterator2>::value_type>());
5111227825Stheraven}
5112227825Stheraven
5113227825Stheraven// set_intersection
5114227825Stheraven
5115227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5116227825Stheraven_OutputIterator
5117227825Stheraven__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5118227825Stheraven                   _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5119227825Stheraven{
5120227825Stheraven    while (__first1 != __last1 && __first2 != __last2)
5121227825Stheraven    {
5122227825Stheraven        if (__comp(*__first1, *__first2))
5123227825Stheraven            ++__first1;
5124227825Stheraven        else
5125227825Stheraven        {
5126227825Stheraven            if (!__comp(*__first2, *__first1))
5127227825Stheraven            {
5128227825Stheraven                *__result = *__first1;
5129227825Stheraven                ++__result;
5130227825Stheraven                ++__first1;
5131227825Stheraven            }
5132227825Stheraven            ++__first2;
5133227825Stheraven        }
5134227825Stheraven    }
5135227825Stheraven    return __result;
5136227825Stheraven}
5137227825Stheraven
5138227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5139227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5140227825Stheraven_OutputIterator
5141227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5142227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5143227825Stheraven{
5144227825Stheraven#ifdef _LIBCPP_DEBUG2
5145227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5146227825Stheraven    __debug_less<_Compare> __c(__comp);
5147227825Stheraven    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5148227825Stheraven#else  // _LIBCPP_DEBUG2
5149227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5150227825Stheraven    return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5151227825Stheraven#endif  // _LIBCPP_DEBUG2
5152227825Stheraven}
5153227825Stheraven
5154227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5155227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5156227825Stheraven_OutputIterator
5157227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5158227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5159227825Stheraven{
5160227825Stheraven    return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
5161227825Stheraven                                  __less<typename iterator_traits<_InputIterator1>::value_type,
5162227825Stheraven                                         typename iterator_traits<_InputIterator2>::value_type>());
5163227825Stheraven}
5164227825Stheraven
5165227825Stheraven// set_difference
5166227825Stheraven
5167227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5168227825Stheraven_OutputIterator
5169227825Stheraven__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5170227825Stheraven                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5171227825Stheraven{
5172227825Stheraven    while (__first1 != __last1)
5173227825Stheraven    {
5174227825Stheraven        if (__first2 == __last2)
5175227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5176227825Stheraven        if (__comp(*__first1, *__first2))
5177227825Stheraven        {
5178227825Stheraven            *__result = *__first1;
5179227825Stheraven            ++__result;
5180227825Stheraven            ++__first1;
5181227825Stheraven        }
5182227825Stheraven        else
5183227825Stheraven        {
5184227825Stheraven            if (!__comp(*__first2, *__first1))
5185227825Stheraven                ++__first1;
5186227825Stheraven            ++__first2;
5187227825Stheraven        }
5188227825Stheraven    }
5189227825Stheraven    return __result;
5190227825Stheraven}
5191227825Stheraven
5192227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5193227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5194227825Stheraven_OutputIterator
5195227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5196227825Stheraven               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5197227825Stheraven{
5198227825Stheraven#ifdef _LIBCPP_DEBUG2
5199227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5200227825Stheraven    __debug_less<_Compare> __c(__comp);
5201227825Stheraven    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5202227825Stheraven#else  // _LIBCPP_DEBUG2
5203227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5204227825Stheraven    return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5205227825Stheraven#endif  // _LIBCPP_DEBUG2
5206227825Stheraven}
5207227825Stheraven
5208227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5209227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5210227825Stheraven_OutputIterator
5211227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5212227825Stheraven               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5213227825Stheraven{
5214227825Stheraven    return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
5215227825Stheraven                                __less<typename iterator_traits<_InputIterator1>::value_type,
5216227825Stheraven                                       typename iterator_traits<_InputIterator2>::value_type>());
5217227825Stheraven}
5218227825Stheraven
5219227825Stheraven// set_symmetric_difference
5220227825Stheraven
5221227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5222227825Stheraven_OutputIterator
5223227825Stheraven__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5224227825Stheraven                           _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5225227825Stheraven{
5226227825Stheraven    while (__first1 != __last1)
5227227825Stheraven    {
5228227825Stheraven        if (__first2 == __last2)
5229227825Stheraven            return _VSTD::copy(__first1, __last1, __result);
5230227825Stheraven        if (__comp(*__first1, *__first2))
5231227825Stheraven        {
5232227825Stheraven            *__result = *__first1;
5233227825Stheraven            ++__result;
5234227825Stheraven            ++__first1;
5235227825Stheraven        }
5236227825Stheraven        else
5237227825Stheraven        {
5238227825Stheraven            if (__comp(*__first2, *__first1))
5239227825Stheraven            {
5240227825Stheraven                *__result = *__first2;
5241227825Stheraven                ++__result;
5242227825Stheraven            }
5243227825Stheraven            else
5244227825Stheraven                ++__first1;
5245227825Stheraven            ++__first2;
5246227825Stheraven        }
5247227825Stheraven    }
5248227825Stheraven    return _VSTD::copy(__first2, __last2, __result);
5249227825Stheraven}
5250227825Stheraven
5251227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5252227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5253227825Stheraven_OutputIterator
5254227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5255227825Stheraven                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5256227825Stheraven{
5257227825Stheraven#ifdef _LIBCPP_DEBUG2
5258227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5259227825Stheraven    __debug_less<_Compare> __c(__comp);
5260227825Stheraven    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
5261227825Stheraven#else  // _LIBCPP_DEBUG2
5262227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5263227825Stheraven    return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
5264227825Stheraven#endif  // _LIBCPP_DEBUG2
5265227825Stheraven}
5266227825Stheraven
5267227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5268227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5269227825Stheraven_OutputIterator
5270227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5271227825Stheraven                         _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5272227825Stheraven{
5273227825Stheraven    return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5274227825Stheraven                                          __less<typename iterator_traits<_InputIterator1>::value_type,
5275227825Stheraven                                                 typename iterator_traits<_InputIterator2>::value_type>());
5276227825Stheraven}
5277227825Stheraven
5278227825Stheraven// lexicographical_compare
5279227825Stheraven
5280227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2>
5281227825Stheravenbool
5282227825Stheraven__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5283227825Stheraven                          _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5284227825Stheraven{
5285227825Stheraven    for (; __first2 != __last2; ++__first1, ++__first2)
5286227825Stheraven    {
5287227825Stheraven        if (__first1 == __last1 || __comp(*__first1, *__first2))
5288227825Stheraven            return true;
5289227825Stheraven        if (__comp(*__first2, *__first1))
5290227825Stheraven            return false;
5291227825Stheraven    }
5292227825Stheraven    return false;
5293227825Stheraven}
5294227825Stheraven
5295227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare>
5296227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5297227825Stheravenbool
5298227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5299227825Stheraven                        _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5300227825Stheraven{
5301227825Stheraven#ifdef _LIBCPP_DEBUG2
5302227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5303227825Stheraven    __debug_less<_Compare> __c(__comp);
5304227825Stheraven    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5305227825Stheraven#else  // _LIBCPP_DEBUG2
5306227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5307227825Stheraven    return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5308227825Stheraven#endif  // _LIBCPP_DEBUG2
5309227825Stheraven}
5310227825Stheraven
5311227825Stheraventemplate <class _InputIterator1, class _InputIterator2>
5312227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5313227825Stheravenbool
5314227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5315227825Stheraven                        _InputIterator2 __first2, _InputIterator2 __last2)
5316227825Stheraven{
5317227825Stheraven    return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
5318227825Stheraven                                         __less<typename iterator_traits<_InputIterator1>::value_type,
5319227825Stheraven                                                typename iterator_traits<_InputIterator2>::value_type>());
5320227825Stheraven}
5321227825Stheraven
5322227825Stheraven// next_permutation
5323227825Stheraven
5324227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
5325227825Stheravenbool
5326227825Stheraven__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5327227825Stheraven{
5328227825Stheraven    _BidirectionalIterator __i = __last;
5329227825Stheraven    if (__first == __last || __first == --__i)
5330227825Stheraven        return false;
5331227825Stheraven    while (true)
5332227825Stheraven    {
5333227825Stheraven        _BidirectionalIterator __ip1 = __i;
5334227825Stheraven        if (__comp(*--__i, *__ip1))
5335227825Stheraven        {
5336227825Stheraven            _BidirectionalIterator __j = __last;
5337227825Stheraven            while (!__comp(*__i, *--__j))
5338227825Stheraven                ;
5339227825Stheraven            swap(*__i, *__j);
5340227825Stheraven            _VSTD::reverse(__ip1, __last);
5341227825Stheraven            return true;
5342227825Stheraven        }
5343227825Stheraven        if (__i == __first)
5344227825Stheraven        {
5345227825Stheraven            _VSTD::reverse(__first, __last);
5346227825Stheraven            return false;
5347227825Stheraven        }
5348227825Stheraven    }
5349227825Stheraven}
5350227825Stheraven
5351227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
5352227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5353227825Stheravenbool
5354227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5355227825Stheraven{
5356227825Stheraven#ifdef _LIBCPP_DEBUG2
5357227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5358227825Stheraven    __debug_less<_Compare> __c(__comp);
5359227825Stheraven    return __next_permutation<_Comp_ref>(__first, __last, __c);
5360227825Stheraven#else  // _LIBCPP_DEBUG2
5361227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5362227825Stheraven    return __next_permutation<_Comp_ref>(__first, __last, __comp);
5363227825Stheraven#endif  // _LIBCPP_DEBUG2
5364227825Stheraven}
5365227825Stheraven
5366227825Stheraventemplate <class _BidirectionalIterator>
5367227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5368227825Stheravenbool
5369227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5370227825Stheraven{
5371227825Stheraven    return _VSTD::next_permutation(__first, __last,
5372227825Stheraven                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5373227825Stheraven}
5374227825Stheraven
5375227825Stheraven// prev_permutation
5376227825Stheraven
5377227825Stheraventemplate <class _Compare, class _BidirectionalIterator>
5378227825Stheravenbool
5379227825Stheraven__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5380227825Stheraven{
5381227825Stheraven    _BidirectionalIterator __i = __last;
5382227825Stheraven    if (__first == __last || __first == --__i)
5383227825Stheraven        return false;
5384227825Stheraven    while (true)
5385227825Stheraven    {
5386227825Stheraven        _BidirectionalIterator __ip1 = __i;
5387227825Stheraven        if (__comp(*__ip1, *--__i))
5388227825Stheraven        {
5389227825Stheraven            _BidirectionalIterator __j = __last;
5390227825Stheraven            while (!__comp(*--__j, *__i))
5391227825Stheraven                ;
5392227825Stheraven            swap(*__i, *__j);
5393227825Stheraven            _VSTD::reverse(__ip1, __last);
5394227825Stheraven            return true;
5395227825Stheraven        }
5396227825Stheraven        if (__i == __first)
5397227825Stheraven        {
5398227825Stheraven            _VSTD::reverse(__first, __last);
5399227825Stheraven            return false;
5400227825Stheraven        }
5401227825Stheraven    }
5402227825Stheraven}
5403227825Stheraven
5404227825Stheraventemplate <class _BidirectionalIterator, class _Compare>
5405227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5406227825Stheravenbool
5407227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5408227825Stheraven{
5409227825Stheraven#ifdef _LIBCPP_DEBUG2
5410227825Stheraven    typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5411227825Stheraven    __debug_less<_Compare> __c(__comp);
5412227825Stheraven    return __prev_permutation<_Comp_ref>(__first, __last, __c);
5413227825Stheraven#else  // _LIBCPP_DEBUG2
5414227825Stheraven    typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5415227825Stheraven    return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5416227825Stheraven#endif  // _LIBCPP_DEBUG2
5417227825Stheraven}
5418227825Stheraven
5419227825Stheraventemplate <class _BidirectionalIterator>
5420227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5421227825Stheravenbool
5422227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5423227825Stheraven{
5424227825Stheraven    return _VSTD::prev_permutation(__first, __last,
5425227825Stheraven                                  __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5426227825Stheraven}
5427227825Stheraven
5428227825Stheraventemplate <class _Tp>
5429227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5430227825Stheraventypename enable_if
5431227825Stheraven<
5432227825Stheraven    is_integral<_Tp>::value,
5433227825Stheraven    _Tp
5434227825Stheraven>::type
5435227825Stheraven__rotate_left(_Tp __t, _Tp __n = 1)
5436227825Stheraven{
5437227825Stheraven    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5438227825Stheraven    __n &= __bits;
5439227825Stheraven    return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5440227825Stheraven}
5441227825Stheraven
5442227825Stheraventemplate <class _Tp>
5443227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
5444227825Stheraventypename enable_if
5445227825Stheraven<
5446227825Stheraven    is_integral<_Tp>::value,
5447227825Stheraven    _Tp
5448227825Stheraven>::type
5449227825Stheraven__rotate_right(_Tp __t, _Tp __n = 1)
5450227825Stheraven{
5451227825Stheraven    const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5452227825Stheraven    __n &= __bits;
5453227825Stheraven    return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5454227825Stheraven}
5455227825Stheraven
5456227825Stheraven_LIBCPP_END_NAMESPACE_STD
5457227825Stheraven
5458227825Stheraven#endif  // _LIBCPP_ALGORITHM
5459