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