1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ALGORITHM
11#define _LIBCPP_ALGORITHM
12
13/*
14    algorithm synopsis
15
16#include <initializer_list>
17
18namespace std
19{
20
21namespace ranges {
22  template <class I, class F>
23    struct in_fun_result;     // since C++20
24
25  template <class I1, class I2>
26    struct in_in_result;      // since C++20
27
28  template <class I1, class I2, class O>
29    struct in_in_out_result;  // since C++20
30
31  template <class I, class O1, class O2>
32    struct in_out_out_result; // since C++20
33
34  template <class I1, class I2>
35    struct min_max_result;    // since C++20
36
37  template <class I>
38    struct in_found_result;   // since C++20
39
40  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
41    indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>             // since C++20
42  constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
43
44  template<forward_range R, class Proj = identity,
45    indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less> // since C++20
46  constexpr borrowed_iterator_t<R> min_element(R&& r, Comp comp = {}, Proj proj = {});
47
48  template <input_iterator I1, sentinel_for<_I1> S1, input_iterator I2, sentinel_for<_I2> S2,
49          class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
50    requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
51  constexpr mismatch_result<_I1, _I2>
52  mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) // since C++20
53
54  template <input_range R1, input_range R2,
55          class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
56    requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
57  constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
58  mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})                           // since C++20
59
60    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
61    constexpr I find(I first, S last, const T& value, Proj proj = {});              // since C++20
62
63  template<input_range R, class T, class Proj = identity>
64    requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
65    constexpr borrowed_iterator_t<R>
66      find(R&& r, const T& value, Proj proj = {});                                  // since C++20
67
68  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
69           indirect_unary_predicate<projected<I, Proj>> Pred>
70    constexpr I find_if(I first, S last, Pred pred, Proj proj = {});                // since C++20
71
72  template<input_range R, class Proj = identity,
73           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
74    constexpr borrowed_iterator_t<R>
75      find_if(R&& r, Pred pred, Proj proj = {});                                    // since C++20
76
77  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
78           indirect_unary_predicate<projected<I, Proj>> Pred>
79    constexpr I find_if_not(I first, S last, Pred pred, Proj proj = {});            // since C++20
80
81  template<input_range R, class Proj = identity,
82           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
83    constexpr borrowed_iterator_t<R>
84      find_if_not(R&& r, Pred pred, Proj proj = {});                                // since C++20
85
86  template<class T, class Proj = identity,
87           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
88    constexpr const T& min(const T& a, const T& b, Comp comp = {}, Proj proj = {}); // since C++20
89
90  template<copyable T, class Proj = identity,
91           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
92    constexpr T min(initializer_list<T> r, Comp comp = {}, Proj proj = {});         // since C++20
93
94 template<input_range R, class Proj = identity,
95          indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
96   requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
97   constexpr range_value_t<R>
98     min(R&& r, Comp comp = {}, Proj proj = {});                                    // since C++20
99
100  template<class T, class Proj = identity,
101           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
102    constexpr const T& max(const T& a, const T& b, Comp comp = {}, Proj proj = {}); // since C++20
103
104  template<copyable T, class Proj = identity,
105           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
106    constexpr T max(initializer_list<T> r, Comp comp = {}, Proj proj = {});         // since C++20
107
108  template<input_range R, class Proj = identity,
109           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
110    requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
111    constexpr range_value_t<R>
112      max(R&& r, Comp comp = {}, Proj proj = {});                                   // since C++20
113
114  template<class I, class O>
115    using unary_transform_result = in_out_result<I, O>;                             // since C++20
116
117  template<class I1, class I2, class O>
118    using binary_transform_result = in_in_out_result<I1, I2, O>;                    // since C++20
119
120  template<input_iterator I, sentinel_for<I> S, weakly_incrementable O,
121           copy_constructible F, class Proj = identity>
122    requires indirectly_writable<O, indirect_result_t<F&, projected<I, Proj>>>
123    constexpr ranges::unary_transform_result<I, O>
124      transform(I first1, S last1, O result, F op, Proj proj = {});                 // since C++20
125
126  template<input_range R, weakly_incrementable O, copy_constructible F,
127           class Proj = identity>
128    requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R>, Proj>>>
129    constexpr ranges::unary_transform_result<borrowed_iterator_t<R>, O>
130      transform(R&& r, O result, F op, Proj proj = {});                             // since C++20
131
132  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
133           weakly_incrementable O, copy_constructible F, class Proj1 = identity,
134           class Proj2 = identity>
135    requires indirectly_writable<O, indirect_result_t<F&, projected<I1, Proj1>,
136                                           projected<I2, Proj2>>>
137    constexpr ranges::binary_transform_result<I1, I2, O>
138      transform(I1 first1, S1 last1, I2 first2, S2 last2, O result,
139                        F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});           // since C++20
140
141  template<input_range R1, input_range R2, weakly_incrementable O,
142           copy_constructible F, class Proj1 = identity, class Proj2 = identity>
143    requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>,
144                                           projected<iterator_t<R2>, Proj2>>>
145    constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
146      transform(R1&& r1, R2&& r2, O result,
147                        F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});           // since C++20
148}
149
150    constexpr bool     // constexpr in C++20
151    all_of(InputIterator first, InputIterator last, Predicate pred);
152
153template <class InputIterator, class Predicate>
154    constexpr bool     // constexpr in C++20
155    any_of(InputIterator first, InputIterator last, Predicate pred);
156
157template <class InputIterator, class Predicate>
158    constexpr bool     // constexpr in C++20
159    none_of(InputIterator first, InputIterator last, Predicate pred);
160
161template <class InputIterator, class Function>
162    constexpr Function          // constexpr in C++20
163    for_each(InputIterator first, InputIterator last, Function f);
164
165template<class InputIterator, class Size, class Function>
166    constexpr InputIterator     // constexpr in C++20
167    for_each_n(InputIterator first, Size n, Function f); // C++17
168
169template <class InputIterator, class T>
170    constexpr InputIterator     // constexpr in C++20
171    find(InputIterator first, InputIterator last, const T& value);
172
173template <class InputIterator, class Predicate>
174    constexpr InputIterator     // constexpr in C++20
175    find_if(InputIterator first, InputIterator last, Predicate pred);
176
177template<class InputIterator, class Predicate>
178    constexpr InputIterator     // constexpr in C++20
179    find_if_not(InputIterator first, InputIterator last, Predicate pred);
180
181template <class ForwardIterator1, class ForwardIterator2>
182    constexpr ForwardIterator1  // constexpr in C++20
183    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
184             ForwardIterator2 first2, ForwardIterator2 last2);
185
186template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
187    constexpr ForwardIterator1  // constexpr in C++20
188    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
189             ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
190
191template <class ForwardIterator1, class ForwardIterator2>
192    constexpr ForwardIterator1  // constexpr in C++20
193    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
194                  ForwardIterator2 first2, ForwardIterator2 last2);
195
196template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
197    constexpr ForwardIterator1  // constexpr in C++20
198    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
199                  ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
200
201template <class ForwardIterator>
202    constexpr ForwardIterator   // constexpr in C++20
203    adjacent_find(ForwardIterator first, ForwardIterator last);
204
205template <class ForwardIterator, class BinaryPredicate>
206    constexpr ForwardIterator   // constexpr in C++20
207    adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
208
209template <class InputIterator, class T>
210    constexpr typename iterator_traits<InputIterator>::difference_type  // constexpr in C++20
211    count(InputIterator first, InputIterator last, const T& value);
212
213template <class InputIterator, class Predicate>
214    constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
215    count_if(InputIterator first, InputIterator last, Predicate pred);
216
217template <class InputIterator1, class InputIterator2>
218    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
219    mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
220
221template <class InputIterator1, class InputIterator2>
222    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
223    mismatch(InputIterator1 first1, InputIterator1 last1,
224             InputIterator2 first2, InputIterator2 last2); // **C++14**
225
226template <class InputIterator1, class InputIterator2, class BinaryPredicate>
227    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
228    mismatch(InputIterator1 first1, InputIterator1 last1,
229             InputIterator2 first2, BinaryPredicate pred);
230
231template <class InputIterator1, class InputIterator2, class BinaryPredicate>
232    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
233    mismatch(InputIterator1 first1, InputIterator1 last1,
234             InputIterator2 first2, InputIterator2 last2,
235             BinaryPredicate pred); // **C++14**
236
237template <class InputIterator1, class InputIterator2>
238    constexpr bool      // constexpr in C++20
239    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
240
241template <class InputIterator1, class InputIterator2>
242    constexpr bool      // constexpr in C++20
243    equal(InputIterator1 first1, InputIterator1 last1,
244          InputIterator2 first2, InputIterator2 last2); // **C++14**
245
246template <class InputIterator1, class InputIterator2, class BinaryPredicate>
247    constexpr bool      // constexpr in C++20
248    equal(InputIterator1 first1, InputIterator1 last1,
249          InputIterator2 first2, BinaryPredicate pred);
250
251template <class InputIterator1, class InputIterator2, class BinaryPredicate>
252    constexpr bool      // constexpr in C++20
253    equal(InputIterator1 first1, InputIterator1 last1,
254          InputIterator2 first2, InputIterator2 last2,
255          BinaryPredicate pred); // **C++14**
256
257template<class ForwardIterator1, class ForwardIterator2>
258    constexpr bool      // constexpr in C++20
259    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
260                   ForwardIterator2 first2);
261
262template<class ForwardIterator1, class ForwardIterator2>
263    constexpr bool      // constexpr in C++20
264    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
265                   ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
266
267template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
268    constexpr bool      // constexpr in C++20
269    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
270                   ForwardIterator2 first2, BinaryPredicate pred);
271
272template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
273    constexpr bool      // constexpr in C++20
274    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
275                   ForwardIterator2 first2, ForwardIterator2 last2,
276                   BinaryPredicate pred);  // **C++14**
277
278template <class ForwardIterator1, class ForwardIterator2>
279    constexpr ForwardIterator1      // constexpr in C++20
280    search(ForwardIterator1 first1, ForwardIterator1 last1,
281           ForwardIterator2 first2, ForwardIterator2 last2);
282
283template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
284    constexpr ForwardIterator1      // constexpr in C++20
285    search(ForwardIterator1 first1, ForwardIterator1 last1,
286           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
287
288template <class ForwardIterator, class Size, class T>
289    constexpr ForwardIterator       // constexpr in C++20
290    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
291
292template <class ForwardIterator, class Size, class T, class BinaryPredicate>
293    constexpr ForwardIterator       // constexpr in C++20
294    search_n(ForwardIterator first, ForwardIterator last,
295             Size count, const T& value, BinaryPredicate pred);
296
297template <class InputIterator, class OutputIterator>
298    constexpr OutputIterator      // constexpr in C++20
299    copy(InputIterator first, InputIterator last, OutputIterator result);
300
301template<class InputIterator, class OutputIterator, class Predicate>
302    constexpr OutputIterator      // constexpr in C++20
303    copy_if(InputIterator first, InputIterator last,
304            OutputIterator result, Predicate pred);
305
306template<class InputIterator, class Size, class OutputIterator>
307    constexpr OutputIterator      // constexpr in C++20
308    copy_n(InputIterator first, Size n, OutputIterator result);
309
310template <class BidirectionalIterator1, class BidirectionalIterator2>
311    constexpr BidirectionalIterator2      // constexpr in C++20
312    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
313                  BidirectionalIterator2 result);
314
315template <class ForwardIterator1, class ForwardIterator2>
316    constexpr ForwardIterator2    // constexpr in C++20
317    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
318
319template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
320        requires indirectly_swappable<I1, I2>
321    constexpr ranges::swap_ranges_result<I1, I2>
322        ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
323
324template<input_range R1, input_range R2>
325        requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
326    constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
327        ranges::swap_ranges(R1&& r1, R2&& r2);
328
329template <class ForwardIterator1, class ForwardIterator2>
330    constexpr void                // constexpr in C++20
331    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
332
333template <class InputIterator, class OutputIterator, class UnaryOperation>
334    constexpr OutputIterator      // constexpr in C++20
335    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
336
337template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
338    constexpr OutputIterator      // constexpr in C++20
339    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
340              OutputIterator result, BinaryOperation binary_op);
341
342template <class ForwardIterator, class T>
343    constexpr void      // constexpr in C++20
344    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
345
346template <class ForwardIterator, class Predicate, class T>
347    constexpr void      // constexpr in C++20
348    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
349
350template <class InputIterator, class OutputIterator, class T>
351    constexpr OutputIterator      // constexpr in C++20
352    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
353                 const T& old_value, const T& new_value);
354
355template <class InputIterator, class OutputIterator, class Predicate, class T>
356    constexpr OutputIterator      // constexpr in C++20
357    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
358
359template <class ForwardIterator, class T>
360    constexpr void      // constexpr in C++20
361    fill(ForwardIterator first, ForwardIterator last, const T& value);
362
363template <class OutputIterator, class Size, class T>
364    constexpr OutputIterator      // constexpr in C++20
365    fill_n(OutputIterator first, Size n, const T& value);
366
367template <class ForwardIterator, class Generator>
368    constexpr void      // constexpr in C++20
369    generate(ForwardIterator first, ForwardIterator last, Generator gen);
370
371template <class OutputIterator, class Size, class Generator>
372    constexpr OutputIterator      // constexpr in C++20
373    generate_n(OutputIterator first, Size n, Generator gen);
374
375template <class ForwardIterator, class T>
376    constexpr ForwardIterator     // constexpr in C++20
377    remove(ForwardIterator first, ForwardIterator last, const T& value);
378
379template <class ForwardIterator, class Predicate>
380    constexpr ForwardIterator     // constexpr in C++20
381    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
382
383template <class InputIterator, class OutputIterator, class T>
384    constexpr OutputIterator     // constexpr in C++20
385    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
386
387template <class InputIterator, class OutputIterator, class Predicate>
388    constexpr OutputIterator     // constexpr in C++20
389    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
390
391template <class ForwardIterator>
392    constexpr ForwardIterator    // constexpr in C++20
393    unique(ForwardIterator first, ForwardIterator last);
394
395template <class ForwardIterator, class BinaryPredicate>
396    constexpr ForwardIterator    // constexpr in C++20
397    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
398
399template <class InputIterator, class OutputIterator>
400    constexpr OutputIterator     // constexpr in C++20
401    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
402
403template <class InputIterator, class OutputIterator, class BinaryPredicate>
404    constexpr OutputIterator     // constexpr in C++20
405    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
406
407template <class BidirectionalIterator>
408    constexpr void               // constexpr in C++20
409    reverse(BidirectionalIterator first, BidirectionalIterator last);
410
411template <class BidirectionalIterator, class OutputIterator>
412    constexpr OutputIterator       // constexpr in C++20
413    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
414
415template <class ForwardIterator>
416    constexpr ForwardIterator      // constexpr in C++20
417    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
418
419template <class ForwardIterator, class OutputIterator>
420    constexpr OutputIterator       // constexpr in C++20
421    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
422
423template <class RandomAccessIterator>
424    void
425    random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
426
427template <class RandomAccessIterator, class RandomNumberGenerator>
428    void
429    random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
430                   RandomNumberGenerator& rand);  // deprecated in C++14, removed in C++17
431
432template<class PopulationIterator, class SampleIterator,
433         class Distance, class UniformRandomBitGenerator>
434    SampleIterator sample(PopulationIterator first, PopulationIterator last,
435                          SampleIterator out, Distance n,
436                          UniformRandomBitGenerator&& g); // C++17
437
438template<class RandomAccessIterator, class UniformRandomNumberGenerator>
439    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
440                 UniformRandomNumberGenerator&& g);
441
442template<class ForwardIterator>
443  constexpr ForwardIterator
444    shift_left(ForwardIterator first, ForwardIterator last,
445               typename iterator_traits<ForwardIterator>::difference_type n); // C++20
446
447template<class ForwardIterator>
448  constexpr ForwardIterator
449    shift_right(ForwardIterator first, ForwardIterator last,
450                typename iterator_traits<ForwardIterator>::difference_type n); // C++20
451
452template <class InputIterator, class Predicate>
453    constexpr bool  // constexpr in C++20
454    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
455
456template <class ForwardIterator, class Predicate>
457    constexpr ForwardIterator  // constexpr in C++20
458    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
459
460template <class InputIterator, class OutputIterator1,
461          class OutputIterator2, class Predicate>
462    constexpr pair<OutputIterator1, OutputIterator2>   // constexpr in C++20
463    partition_copy(InputIterator first, InputIterator last,
464                   OutputIterator1 out_true, OutputIterator2 out_false,
465                   Predicate pred);
466
467template <class ForwardIterator, class Predicate>
468    ForwardIterator
469    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
470
471template<class ForwardIterator, class Predicate>
472    constexpr ForwardIterator  // constexpr in C++20
473    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
474
475template <class ForwardIterator>
476    constexpr bool  // constexpr in C++20
477    is_sorted(ForwardIterator first, ForwardIterator last);
478
479template <class ForwardIterator, class Compare>
480    constexpr bool  // constexpr in C++20
481    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
482
483template<class ForwardIterator>
484    constexpr ForwardIterator    // constexpr in C++20
485    is_sorted_until(ForwardIterator first, ForwardIterator last);
486
487template <class ForwardIterator, class Compare>
488    constexpr ForwardIterator    // constexpr in C++20
489    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
490
491template <class RandomAccessIterator>
492    constexpr void               // constexpr in C++20
493    sort(RandomAccessIterator first, RandomAccessIterator last);
494
495template <class RandomAccessIterator, class Compare>
496    constexpr void               // constexpr in C++20
497    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
498
499template <class RandomAccessIterator>
500    void
501    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
502
503template <class RandomAccessIterator, class Compare>
504    void
505    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
506
507template <class RandomAccessIterator>
508    constexpr void                    // constexpr in C++20
509    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
510
511template <class RandomAccessIterator, class Compare>
512    constexpr void                    // constexpr in C++20
513    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
514
515template <class InputIterator, class RandomAccessIterator>
516    constexpr RandomAccessIterator    // constexpr in C++20
517    partial_sort_copy(InputIterator first, InputIterator last,
518                      RandomAccessIterator result_first, RandomAccessIterator result_last);
519
520template <class InputIterator, class RandomAccessIterator, class Compare>
521    constexpr RandomAccessIterator    // constexpr in C++20
522    partial_sort_copy(InputIterator first, InputIterator last,
523                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
524
525template <class RandomAccessIterator>
526    constexpr void                    // constexpr in C++20
527    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
528
529template <class RandomAccessIterator, class Compare>
530    constexpr void                    // constexpr in C++20
531    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
532
533template <class ForwardIterator, class T>
534    constexpr ForwardIterator                         // constexpr in C++20
535    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
536
537template <class ForwardIterator, class T, class Compare>
538    constexpr ForwardIterator                         // constexpr in C++20
539    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
540
541template <class ForwardIterator, class T>
542    constexpr ForwardIterator                         // constexpr in C++20
543    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
544
545template <class ForwardIterator, class T, class Compare>
546    constexpr ForwardIterator                         // constexpr in C++20
547    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
548
549template <class ForwardIterator, class T>
550    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++20
551    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
552
553template <class ForwardIterator, class T, class Compare>
554    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++20
555    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
556
557template <class ForwardIterator, class T>
558    constexpr bool                                    // constexpr in C++20
559    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
560
561template <class ForwardIterator, class T, class Compare>
562    constexpr bool                                    // constexpr in C++20
563    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
564
565template <class InputIterator1, class InputIterator2, class OutputIterator>
566    constexpr OutputIterator                          // constexpr in C++20
567    merge(InputIterator1 first1, InputIterator1 last1,
568          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
569
570template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
571    constexpr OutputIterator                          // constexpr in C++20
572    merge(InputIterator1 first1, InputIterator1 last1,
573          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
574
575template <class BidirectionalIterator>
576    void
577    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
578
579template <class BidirectionalIterator, class Compare>
580    void
581    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
582
583template <class InputIterator1, class InputIterator2>
584    constexpr bool                                    // constexpr in C++20
585    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
586
587template <class InputIterator1, class InputIterator2, class Compare>
588    constexpr bool                                    // constexpr in C++20
589    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
590
591template <class InputIterator1, class InputIterator2, class OutputIterator>
592    constexpr OutputIterator                          // constexpr in C++20
593    set_union(InputIterator1 first1, InputIterator1 last1,
594              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
595
596template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
597    constexpr OutputIterator                          // constexpr in C++20
598    set_union(InputIterator1 first1, InputIterator1 last1,
599              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
600
601template <class InputIterator1, class InputIterator2, class OutputIterator>
602    constexpr OutputIterator                         // constexpr in C++20
603    set_intersection(InputIterator1 first1, InputIterator1 last1,
604                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
605
606template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
607    constexpr OutputIterator                         // constexpr in C++20
608    set_intersection(InputIterator1 first1, InputIterator1 last1,
609                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
610
611template <class InputIterator1, class InputIterator2, class OutputIterator>
612    constexpr OutputIterator                         // constexpr in C++20
613    set_difference(InputIterator1 first1, InputIterator1 last1,
614                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
615
616template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
617    constexpr OutputIterator                         // constexpr in C++20
618    set_difference(InputIterator1 first1, InputIterator1 last1,
619                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
620
621template <class InputIterator1, class InputIterator2, class OutputIterator>
622    constexpr OutputIterator                         // constexpr in C++20
623    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
624                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
625
626template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
627    constexpr OutputIterator                         // constexpr in C++20
628    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
629                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
630
631template <class RandomAccessIterator>
632    constexpr void                                   // constexpr in C++20
633    push_heap(RandomAccessIterator first, RandomAccessIterator last);
634
635template <class RandomAccessIterator, class Compare>
636    constexpr void                                   // constexpr in C++20
637    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
638
639template <class RandomAccessIterator>
640    constexpr void                                   // constexpr in C++20
641    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
642
643template <class RandomAccessIterator, class Compare>
644    constexpr void                                   // constexpr in C++20
645    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
646
647template <class RandomAccessIterator>
648    constexpr void                                   // constexpr in C++20
649    make_heap(RandomAccessIterator first, RandomAccessIterator last);
650
651template <class RandomAccessIterator, class Compare>
652    constexpr void                                   // constexpr in C++20
653    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
654
655template <class RandomAccessIterator>
656    constexpr void                                   // constexpr in C++20
657    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
658
659template <class RandomAccessIterator, class Compare>
660    constexpr void                                   // constexpr in C++20
661    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
662
663template <class RandomAccessIterator>
664    constexpr bool   // constexpr in C++20
665    is_heap(RandomAccessIterator first, RandomAccessiterator last);
666
667template <class RandomAccessIterator, class Compare>
668    constexpr bool   // constexpr in C++20
669    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
670
671template <class RandomAccessIterator>
672    constexpr RandomAccessIterator   // constexpr in C++20
673    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
674
675template <class RandomAccessIterator, class Compare>
676    constexpr RandomAccessIterator   // constexpr in C++20
677    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
678
679template <class ForwardIterator>
680    constexpr ForwardIterator        // constexpr in C++14
681    min_element(ForwardIterator first, ForwardIterator last);
682
683template <class ForwardIterator, class Compare>
684    constexpr ForwardIterator        // constexpr in C++14
685    min_element(ForwardIterator first, ForwardIterator last, Compare comp);
686
687template <class T>
688    constexpr const T&               // constexpr in C++14
689    min(const T& a, const T& b);
690
691template <class T, class Compare>
692    constexpr const T&               // constexpr in C++14
693    min(const T& a, const T& b, Compare comp);
694
695template<class T>
696    constexpr T                      // constexpr in C++14
697    min(initializer_list<T> t);
698
699template<class T, class Compare>
700    constexpr T                      // constexpr in C++14
701    min(initializer_list<T> t, Compare comp);
702
703template<class T>
704    constexpr const T& clamp(const T& v, const T& lo, const T& hi);               // C++17
705
706template<class T, class Compare>
707    constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
708
709template <class ForwardIterator>
710    constexpr ForwardIterator        // constexpr in C++14
711    max_element(ForwardIterator first, ForwardIterator last);
712
713template <class ForwardIterator, class Compare>
714    constexpr ForwardIterator        // constexpr in C++14
715    max_element(ForwardIterator first, ForwardIterator last, Compare comp);
716
717template <class T>
718    constexpr const T&               // constexpr in C++14
719    max(const T& a, const T& b);
720
721template <class T, class Compare>
722    constexpr const T&               // constexpr in C++14
723    max(const T& a, const T& b, Compare comp);
724
725template<class T>
726    constexpr T                      // constexpr in C++14
727    max(initializer_list<T> t);
728
729template<class T, class Compare>
730    constexpr T                      // constexpr in C++14
731    max(initializer_list<T> t, Compare comp);
732
733template<class ForwardIterator>
734    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++14
735    minmax_element(ForwardIterator first, ForwardIterator last);
736
737template<class ForwardIterator, class Compare>
738    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++14
739    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
740
741template<class T>
742    constexpr pair<const T&, const T&>  // constexpr in C++14
743    minmax(const T& a, const T& b);
744
745template<class T, class Compare>
746    constexpr pair<const T&, const T&>  // constexpr in C++14
747    minmax(const T& a, const T& b, Compare comp);
748
749template<class T>
750    constexpr pair<T, T>                // constexpr in C++14
751    minmax(initializer_list<T> t);
752
753template<class T, class Compare>
754    constexpr pair<T, T>                // constexpr in C++14
755    minmax(initializer_list<T> t, Compare comp);
756
757template <class InputIterator1, class InputIterator2>
758    constexpr bool     // constexpr in C++20
759    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
760
761template <class InputIterator1, class InputIterator2, class Compare>
762    constexpr bool     // constexpr in C++20
763    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
764                            InputIterator2 first2, InputIterator2 last2, Compare comp);
765
766template <class BidirectionalIterator>
767    constexpr bool     // constexpr in C++20
768    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
769
770template <class BidirectionalIterator, class Compare>
771    constexpr bool     // constexpr in C++20
772    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
773
774template <class BidirectionalIterator>
775    constexpr bool     // constexpr in C++20
776    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
777
778template <class BidirectionalIterator, class Compare>
779    constexpr bool     // constexpr in C++20
780    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
781}  // std
782
783*/
784
785#include <__assert> // all public C++ headers provide the assertion handler
786#include <__bits>
787#include <__config>
788#include <__debug>
789#include <cstddef>
790#include <cstring>
791#include <functional>
792#include <initializer_list>
793#include <iterator>
794#include <memory>
795#include <type_traits>
796#include <version>
797
798#include <__algorithm/adjacent_find.h>
799#include <__algorithm/all_of.h>
800#include <__algorithm/any_of.h>
801#include <__algorithm/binary_search.h>
802#include <__algorithm/clamp.h>
803#include <__algorithm/comp.h>
804#include <__algorithm/comp_ref_type.h>
805#include <__algorithm/copy.h>
806#include <__algorithm/copy_backward.h>
807#include <__algorithm/copy_if.h>
808#include <__algorithm/copy_n.h>
809#include <__algorithm/count.h>
810#include <__algorithm/count_if.h>
811#include <__algorithm/equal.h>
812#include <__algorithm/equal_range.h>
813#include <__algorithm/fill.h>
814#include <__algorithm/fill_n.h>
815#include <__algorithm/find.h>
816#include <__algorithm/find_end.h>
817#include <__algorithm/find_first_of.h>
818#include <__algorithm/find_if.h>
819#include <__algorithm/find_if_not.h>
820#include <__algorithm/for_each.h>
821#include <__algorithm/for_each_n.h>
822#include <__algorithm/generate.h>
823#include <__algorithm/generate_n.h>
824#include <__algorithm/half_positive.h>
825#include <__algorithm/in_found_result.h>
826#include <__algorithm/in_fun_result.h>
827#include <__algorithm/in_in_out_result.h>
828#include <__algorithm/in_in_result.h>
829#include <__algorithm/in_out_out_result.h>
830#include <__algorithm/in_out_result.h>
831#include <__algorithm/includes.h>
832#include <__algorithm/inplace_merge.h>
833#include <__algorithm/is_heap.h>
834#include <__algorithm/is_heap_until.h>
835#include <__algorithm/is_partitioned.h>
836#include <__algorithm/is_permutation.h>
837#include <__algorithm/is_sorted.h>
838#include <__algorithm/is_sorted_until.h>
839#include <__algorithm/iter_swap.h>
840#include <__algorithm/lexicographical_compare.h>
841#include <__algorithm/lower_bound.h>
842#include <__algorithm/make_heap.h>
843#include <__algorithm/max.h>
844#include <__algorithm/max_element.h>
845#include <__algorithm/merge.h>
846#include <__algorithm/min.h>
847#include <__algorithm/min_element.h>
848#include <__algorithm/min_max_result.h>
849#include <__algorithm/minmax.h>
850#include <__algorithm/minmax_element.h>
851#include <__algorithm/mismatch.h>
852#include <__algorithm/move.h>
853#include <__algorithm/move_backward.h>
854#include <__algorithm/next_permutation.h>
855#include <__algorithm/none_of.h>
856#include <__algorithm/nth_element.h>
857#include <__algorithm/partial_sort.h>
858#include <__algorithm/partial_sort_copy.h>
859#include <__algorithm/partition.h>
860#include <__algorithm/partition_copy.h>
861#include <__algorithm/partition_point.h>
862#include <__algorithm/pop_heap.h>
863#include <__algorithm/prev_permutation.h>
864#include <__algorithm/push_heap.h>
865#include <__algorithm/ranges_find.h>
866#include <__algorithm/ranges_find_if.h>
867#include <__algorithm/ranges_find_if_not.h>
868#include <__algorithm/ranges_max.h>
869#include <__algorithm/ranges_max_element.h>
870#include <__algorithm/ranges_min.h>
871#include <__algorithm/ranges_min_element.h>
872#include <__algorithm/ranges_mismatch.h>
873#include <__algorithm/ranges_swap_ranges.h>
874#include <__algorithm/ranges_transform.h>
875#include <__algorithm/remove.h>
876#include <__algorithm/remove_copy.h>
877#include <__algorithm/remove_copy_if.h>
878#include <__algorithm/remove_if.h>
879#include <__algorithm/replace.h>
880#include <__algorithm/replace_copy.h>
881#include <__algorithm/replace_copy_if.h>
882#include <__algorithm/replace_if.h>
883#include <__algorithm/reverse.h>
884#include <__algorithm/reverse_copy.h>
885#include <__algorithm/rotate.h>
886#include <__algorithm/rotate_copy.h>
887#include <__algorithm/sample.h>
888#include <__algorithm/search.h>
889#include <__algorithm/search_n.h>
890#include <__algorithm/set_difference.h>
891#include <__algorithm/set_intersection.h>
892#include <__algorithm/set_symmetric_difference.h>
893#include <__algorithm/set_union.h>
894#include <__algorithm/shift_left.h>
895#include <__algorithm/shift_right.h>
896#include <__algorithm/shuffle.h>
897#include <__algorithm/sift_down.h>
898#include <__algorithm/sort.h>
899#include <__algorithm/sort_heap.h>
900#include <__algorithm/stable_partition.h>
901#include <__algorithm/stable_sort.h>
902#include <__algorithm/swap_ranges.h>
903#include <__algorithm/transform.h>
904#include <__algorithm/unique.h>
905#include <__algorithm/unique_copy.h>
906#include <__algorithm/unwrap_iter.h>
907#include <__algorithm/upper_bound.h>
908
909#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
910#  pragma GCC system_header
911#endif
912
913#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
914#   include <__pstl_algorithm>
915#endif
916
917#endif // _LIBCPP_ALGORITHM
918