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
23  // [algorithms.results], algorithm result types
24  template <class I, class F>
25    struct in_fun_result;     // since C++20
26
27  template <class I1, class I2>
28    struct in_in_result;      // since C++20
29
30  template <class I, class O>
31    struct in_out_result;  // since C++20
32
33  template <class I1, class I2, class O>
34    struct in_in_out_result;  // since C++20
35
36  template <class I, class O1, class O2>
37    struct in_out_out_result; // since C++20
38
39  template <class I1, class I2>
40    struct min_max_result;    // since C++20
41
42  template <class I>
43    struct in_found_result;   // since C++20
44
45  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
46    indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>             // since C++20
47  constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
48
49  template<forward_range R, class Proj = identity,
50    indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less> // since C++20
51  constexpr borrowed_iterator_t<R> min_element(R&& r, Comp comp = {}, Proj proj = {});
52
53  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
54    indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
55  constexpr I ranges::max_element(I first, S last, Comp comp = {}, Proj proj = {});                        // since C++20
56
57  template<forward_range R, class Proj = identity,
58    indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
59  constexpr borrowed_iterator_t<R> ranges::max_element(R&& r, Comp comp = {}, Proj proj = {});             // since C++20
60
61  template<class I1, class I2>
62    using mismatch_result = in_in_result<I1, I2>;
63
64  template <input_iterator I1, sentinel_for<_I1> S1, input_iterator I2, sentinel_for<_I2> S2,
65          class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
66    requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
67  constexpr mismatch_result<_I1, _I2>
68  mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) // since C++20
69
70  template <input_range R1, input_range R2,
71          class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
72    requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
73  constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
74  mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})                           // since C++20
75
76    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
77    constexpr I find(I first, S last, const T& value, Proj proj = {});              // since C++20
78
79  template<input_range R, class T, class Proj = identity>
80    requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
81    constexpr borrowed_iterator_t<R>
82      find(R&& r, const T& value, Proj proj = {});                                  // since C++20
83
84  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
85           indirect_unary_predicate<projected<I, Proj>> Pred>
86    constexpr I find_if(I first, S last, Pred pred, Proj proj = {});                // since C++20
87
88  template<input_range R, class Proj = identity,
89           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
90    constexpr borrowed_iterator_t<R>
91      find_if(R&& r, Pred pred, Proj proj = {});                                    // since C++20
92
93  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
94           indirect_unary_predicate<projected<I, Proj>> Pred>
95    constexpr I find_if_not(I first, S last, Pred pred, Proj proj = {});            // since C++20
96
97  template<input_range R, class Proj = identity,
98           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
99    constexpr borrowed_iterator_t<R>
100      find_if_not(R&& r, Pred pred, Proj proj = {});                                // since C++20
101
102  template<class T, class Proj = identity,
103           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
104    constexpr const T& min(const T& a, const T& b, Comp comp = {}, Proj proj = {}); // since C++20
105
106  template<copyable T, class Proj = identity,
107           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
108    constexpr T min(initializer_list<T> r, Comp comp = {}, Proj proj = {});         // since C++20
109
110 template<input_range R, class Proj = identity,
111          indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
112   requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
113   constexpr range_value_t<R>
114     min(R&& r, Comp comp = {}, Proj proj = {});                                    // since C++20
115
116  template<class T, class Proj = identity,
117           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
118    constexpr const T& max(const T& a, const T& b, Comp comp = {}, Proj proj = {}); // since C++20
119
120  template<copyable T, class Proj = identity,
121           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
122    constexpr T max(initializer_list<T> r, Comp comp = {}, Proj proj = {});         // since C++20
123
124  template<input_range R, class Proj = identity,
125           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
126    requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
127    constexpr range_value_t<R>
128      max(R&& r, Comp comp = {}, Proj proj = {});                                   // since C++20
129
130  template<class I, class O>
131    using unary_transform_result = in_out_result<I, O>;                             // since C++20
132
133  template<class I1, class I2, class O>
134    using binary_transform_result = in_in_out_result<I1, I2, O>;                    // since C++20
135
136  template<input_iterator I, sentinel_for<I> S, weakly_incrementable O,
137           copy_constructible F, class Proj = identity>
138    requires indirectly_writable<O, indirect_result_t<F&, projected<I, Proj>>>
139    constexpr ranges::unary_transform_result<I, O>
140      transform(I first1, S last1, O result, F op, Proj proj = {});                 // since C++20
141
142  template<input_range R, weakly_incrementable O, copy_constructible F,
143           class Proj = identity>
144    requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R>, Proj>>>
145    constexpr ranges::unary_transform_result<borrowed_iterator_t<R>, O>
146      transform(R&& r, O result, F op, Proj proj = {});                             // since C++20
147
148  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
149           weakly_incrementable O, copy_constructible F, class Proj1 = identity,
150           class Proj2 = identity>
151    requires indirectly_writable<O, indirect_result_t<F&, projected<I1, Proj1>,
152                                           projected<I2, Proj2>>>
153    constexpr ranges::binary_transform_result<I1, I2, O>
154      transform(I1 first1, S1 last1, I2 first2, S2 last2, O result,
155                        F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});           // since C++20
156
157  template<input_range R1, input_range R2, weakly_incrementable O,
158           copy_constructible F, class Proj1 = identity, class Proj2 = identity>
159    requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>,
160                                           projected<iterator_t<R2>, Proj2>>>
161    constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
162      transform(R1&& r1, R2&& r2, O result,
163                        F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});           // since C++20
164
165  template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
166    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
167    constexpr iter_difference_t<I>
168      count(I first, S last, const T& value, Proj proj = {});                       // since C++20
169
170  template<input_range R, class T, class Proj = identity>
171    requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
172    constexpr range_difference_t<R>
173      count(R&& r, const T& value, Proj proj = {});                                 // since C++20
174
175  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
176           indirect_unary_predicate<projected<I, Proj>> Pred>
177    constexpr iter_difference_t<I>
178      count_if(I first, S last, Pred pred, Proj proj = {});                         // since C++20
179
180  template<input_range R, class Proj = identity,
181           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
182    constexpr range_difference_t<R>
183      count_if(R&& r, Pred pred, Proj proj = {});                                   // since C++20
184
185  template<class T>
186  using minmax_result = min_max_result<T>;
187
188  template<class T, class Proj = identity,
189           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
190    constexpr ranges::minmax_result<const T&>
191      minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {});                     // since C++20
192
193  template<copyable T, class Proj = identity,
194           indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
195    constexpr ranges::minmax_result<T>
196      minmax(initializer_list<T> r, Comp comp = {}, Proj proj = {});                      // since C++20
197
198  template<input_range R, class Proj = identity,
199           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
200    requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
201    constexpr ranges::minmax_result<range_value_t<R>>
202      minmax(R&& r, Comp comp = {}, Proj proj = {});                                      // since C++20
203
204  template<class I>
205  using minmax_element_result = min_max_result<I>;
206
207  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
208           indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
209    constexpr ranges::minmax_element_result<I>
210      minmax_element(I first, S last, Comp comp = {}, Proj proj = {});                    // since C++20
211
212  template<forward_range R, class Proj = identity,
213           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
214    constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
215      minmax_element(R&& r, Comp comp = {}, Proj proj = {});                              // since C++20
216
217  template<class I, class O>
218    using copy_result = in_out_result<I, O>;                                              // since C++20
219
220  template<class I, class O>
221    using copy_n_result = in_out_result<I, O>;                                            // since C++20
222
223  template<class I, class O>
224    using copy_if_result = in_out_result<I, O>;                                             // since C++20
225
226  template<class I1, class I2>
227    using copy_backward_result = in_out_result<I1, I2>;                                     // since C++20
228
229  template<input_iterator I, sentinel_for<I> S, weakly_incrementable O>
230    requires indirectly_copyable<I, O>
231    constexpr ranges::copy_result<I, O> ranges::copy(I first, S last, O result);            // since C++20
232
233  template<input_range R, weakly_incrementable O>
234    requires indirectly_copyable<iterator_t<R>, O>
235    constexpr ranges::copy_result<borrowed_iterator_t<R>, O> ranges::copy(R&& r, O result); // since C++20
236
237  template<input_iterator I, weakly_incrementable O>
238    requires indirectly_copyable<I, O>
239    constexpr ranges::copy_n_result<I, O>
240      ranges::copy_n(I first, iter_difference_t<I> n, O result);                            // since C++20
241
242  template<input_iterator I, sentinel_for<I> S, weakly_incrementable O, class Proj = identity,
243           indirect_unary_predicate<projected<I, Proj>> Pred>
244    requires indirectly_copyable<I, O>
245    constexpr ranges::copy_if_result<I, O>
246      ranges::copy_if(I first, S last, O result, Pred pred, Proj proj = {});                // since C++20
247
248  template<input_range R, weakly_incrementable O, class Proj = identity,
249           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
250    requires indirectly_copyable<iterator_t<R>, O>
251    constexpr ranges::copy_if_result<borrowed_iterator_t<R>, O>
252      ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {});                          // since C++20
253
254  template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2>
255    requires indirectly_copyable<I1, I2>
256    constexpr ranges::copy_backward_result<I1, I2>
257      ranges::copy_backward(I1 first, S1 last, I2 result);                                  // since C++20
258
259  template<bidirectional_range R, bidirectional_iterator I>
260    requires indirectly_copyable<iterator_t<R>, I>
261    constexpr ranges::copy_backward_result<borrowed_iterator_t<R>, I>
262      ranges::copy_backward(R&& r, I result);                                               // since C++20
263
264  template<class I, class F>
265    using for_each_result = in_fun_result<I, F>;                                            // since C++20
266
267  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
268           indirectly_unary_invocable<projected<I, Proj>> Fun>
269    constexpr ranges::for_each_result<I, Fun>
270      ranges::for_each(I first, S last, Fun f, Proj proj = {});                             // since C++20
271
272  template<input_range R, class Proj = identity,
273           indirectly_unary_invocable<projected<iterator_t<R>, Proj>> Fun>
274    constexpr ranges::for_each_result<borrowed_iterator_t<R>, Fun>
275      ranges::for_each(R&& r, Fun f, Proj proj = {});                                       // since C++20
276
277  template<input_iterator I, class Proj = identity,
278           indirectly_unary_invocable<projected<I, Proj>> Fun>
279    constexpr ranges::for_each_n_result<I, Fun>
280      ranges::for_each_n(I first, iter_difference_t<I> n, Fun f, Proj proj = {});           // since C++20
281
282  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
283           indirect_unary_predicate<projected<I, Proj>> Pred>
284    constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {});      // since C++20
285
286  template<input_range R, class Proj = identity,
287           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
288    constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {});                // since C++20
289
290  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
291          class Proj = identity>
292    requires sortable<I, Comp, Proj>
293    constexpr I
294      ranges::push_heap(I first, S last, Comp comp = {}, Proj proj = {});                   // since C++20
295
296  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
297    requires sortable<iterator_t<R>, Comp, Proj>
298    constexpr borrowed_iterator_t<R>
299      ranges::push_heap(R&& r, Comp comp = {}, Proj proj = {});                             // since C++20
300
301  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
302          class Proj = identity>
303    requires sortable<I, Comp, Proj>
304    constexpr I
305      ranges::pop_heap(I first, S last, Comp comp = {}, Proj proj = {});                    // since C++20
306
307  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
308    requires sortable<iterator_t<R>, Comp, Proj>
309    constexpr borrowed_iterator_t<R>
310      ranges::pop_heap(R&& r, Comp comp = {}, Proj proj = {});                              // since C++20
311
312  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
313          class Proj = identity>
314    requires sortable<I, Comp, Proj>
315    constexpr I
316      ranges::make_heap(I first, S last, Comp comp = {}, Proj proj = {});                   // since C++20
317
318  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
319    requires sortable<iterator_t<R>, Comp, Proj>
320    constexpr borrowed_iterator_t<R>
321      ranges::make_heap(R&& r, Comp comp = {}, Proj proj = {});                             // since C++20
322
323  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
324          class Proj = identity>
325    requires sortable<I, Comp, Proj>
326    constexpr I
327      ranges::sort_heap(I first, S last, Comp comp = {}, Proj proj = {});                   // since C++20
328
329  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
330    requires sortable<iterator_t<R>, Comp, Proj>
331    constexpr borrowed_iterator_t<R>
332      ranges::sort_heap(R&& r, Comp comp = {}, Proj proj = {});                             // since C++20
333
334  template<bidirectional_iterator I, sentinel_for<I> S>
335    requires permutable<I>
336    constexpr I ranges::reverse(I first, S last);                                           // since C++20
337
338  template<bidirectional_range R>
339    requires permutable<iterator_t<R>>
340    constexpr borrowed_iterator_t<R> ranges::reverse(R&& r);                                // since C++20
341
342  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
343            class Proj = identity>
344    requires sortable<I, Comp, Proj>
345    constexpr I
346      ranges::sort(I first, S last, Comp comp = {}, Proj proj = {});                        // since C++20
347
348  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
349    requires sortable<iterator_t<R>, Comp, Proj>
350    constexpr borrowed_iterator_t<R>
351      ranges::sort(R&& r, Comp comp = {}, Proj proj = {});                                  // since C++20
352
353  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
354          class Proj = identity>
355    requires sortable<I, Comp, Proj>
356    I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {});                 // since C++20
357
358  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
359    requires sortable<iterator_t<R>, Comp, Proj>
360    borrowed_iterator_t<R>
361      ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {});                           // since C++20
362
363  template<class T, output_iterator<const T&> O, sentinel_for<O> S>
364    constexpr O ranges::fill(O first, S last, const T& value);                              // since C++20
365
366  template<class T, output_range<const T&> R>
367    constexpr borrowed_iterator_t<R> ranges::fill(R&& r, const T& value);                   // since C++20
368
369  template<class T, output_iterator<const T&> O>
370    constexpr O ranges::fill_n(O first, iter_difference_t<O> n, const T& value);            // since C++20
371
372 template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
373          class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
374   requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
375   constexpr bool ranges::equal(I1 first1, S1 last1, I2 first2, S2 last2,
376                                Pred pred = {},
377                                Proj1 proj1 = {}, Proj2 proj2 = {});                        // since C++20
378
379 template<input_range R1, input_range R2, class Pred = ranges::equal_to,
380          class Proj1 = identity, class Proj2 = identity>
381   requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
382   constexpr bool ranges::equal(R1&& r1, R2&& r2, Pred pred = {},
383                                Proj1 proj1 = {}, Proj2 proj2 = {});                        // since C++20
384
385  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
386           indirect_unary_predicate<projected<I, Proj>> Pred>
387    constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {});      // since C++20
388
389  template<input_range R, class Proj = identity,
390           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
391    constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {});                // since C++20
392
393  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
394           indirect_unary_predicate<projected<I, Proj>> Pred>
395    constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {});      // since C++20
396
397  template<input_range R, class Proj = identity,
398           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
399    constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {});                // since C++20
400
401  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
402           indirect_unary_predicate<projected<I, Proj>> Pred>
403    constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {});     // since C++20
404
405  template<input_range R, class Proj = identity,
406           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
407    constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {});               // since C++20
408
409  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
410           indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
411    constexpr bool ranges::is_sorted(I first, S last, Comp comp = {}, Proj proj = {});      // since C++20
412
413  template<forward_range R, class Proj = identity,
414           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
415    constexpr bool ranges::is_sorted(R&& r, Comp comp = {}, Proj proj = {});                // since C++20
416
417  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
418           indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
419    constexpr I ranges::is_sorted_until(I first, S last, Comp comp = {}, Proj proj = {});   // since C++20
420
421  template<forward_range R, class Proj = identity,
422           indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
423    constexpr borrowed_iterator_t<R>
424      ranges::is_sorted_until(R&& r, Comp comp = {}, Proj proj = {});                       // since C++20
425
426  template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
427          class Proj = identity>
428    requires sortable<I, Comp, Proj>
429    constexpr I
430      ranges::nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {});            // since C++20
431
432  template<random_access_range R, class Comp = ranges::less, class Proj = identity>
433    requires sortable<iterator_t<R>, Comp, Proj>
434    constexpr borrowed_iterator_t<R>
435      ranges::nth_element(R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {});          // since C++20
436
437  template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity,
438           indirect_strict_weak_order<const T*, projected<I, Proj>> Comp = ranges::less>
439    constexpr I upper_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); // since C++20
440
441  template<forward_range R, class T, class Proj = identity,
442           indirect_strict_weak_order<const T*, projected<iterator_t<R>, Proj>> Comp =
443             ranges::less>
444    constexpr borrowed_iterator_t<R>
445      upper_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {});                     // since C++20
446
447  template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity,
448           indirect_strict_weak_order<const T*, projected<I, Proj>> Comp = ranges::less>
449    constexpr I lower_bound(I first, S last, const T& value, Comp comp = {},
450                                    Proj proj = {});                                          // since C++20
451  template<forward_range R, class T, class Proj = identity,
452           indirect_strict_weak_order<const T*, projected<iterator_t<R>, Proj>> Comp =
453             ranges::less>
454    constexpr borrowed_iterator_t<R>
455      lower_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {});                     // since C++20
456
457  template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity,
458           indirect_strict_weak_order<const T*, projected<I, Proj>> Comp = ranges::less>
459    constexpr bool binary_search(I first, S last, const T& value, Comp comp = {},
460                                         Proj proj = {});                                     // since C++20
461
462  template<forward_range R, class T, class Proj = identity,
463           indirect_strict_weak_order<const T*, projected<iterator_t<R>, Proj>> Comp =
464             ranges::less>
465    constexpr bool binary_search(R&& r, const T& value, Comp comp = {},
466                                         Proj proj = {});                                     // since C++20
467  template<input_iterator I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2,
468           class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
469    requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
470    constexpr I1 ranges::find_first_of(I1 first1, S1 last1, I2 first2, S2 last2,
471                                       Pred pred = {},
472                                       Proj1 proj1 = {}, Proj2 proj2 = {});                 // since C++20
473
474  template<input_range R1, forward_range R2,
475           class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
476    requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
477    constexpr borrowed_iterator_t<R1>
478      ranges::find_first_of(R1&& r1, R2&& r2,
479                            Pred pred = {},
480                            Proj1 proj1 = {}, Proj2 proj2 = {});                            // since C++20
481
482  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
483           indirect_binary_predicate<projected<I, Proj>,
484                                     projected<I, Proj>> Pred = ranges::equal_to>
485    constexpr I ranges::adjacent_find(I first, S last, Pred pred = {}, Proj proj = {});     // since C+20
486
487  template<forward_range R, class Proj = identity,
488           indirect_binary_predicate<projected<iterator_t<R>, Proj>,
489                                     projected<iterator_t<R>, Proj>> Pred = ranges::equal_to>
490    constexpr borrowed_iterator_t<R> ranges::adjacent_find(R&& r, Pred pred = {}, Proj proj = {});  // since C++20
491
492  template<input_iterator I, sentinel_for<I> S, class T1, class T2, class Proj = identity>
493    requires indirectly_writable<I, const T2&> &&
494             indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
495    constexpr I
496      ranges::replace(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {});   // since C++20
497
498  template<input_range R, class T1, class T2, class Proj = identity>
499    requires indirectly_writable<iterator_t<R>, const T2&> &&
500             indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T1*>
501    constexpr borrowed_iterator_t<R>
502      ranges::replace(R&& r, const T1& old_value, const T2& new_value, Proj proj = {});             // since C++20
503
504  template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity,
505           indirect_unary_predicate<projected<I, Proj>> Pred>
506    requires indirectly_writable<I, const T&>
507    constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {}); // since C++20
508
509  template<input_range R, class T, class Proj = identity,
510           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
511    requires indirectly_writable<iterator_t<R>, const T&>
512    constexpr borrowed_iterator_t<R>
513      ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});                     // since C++20
514
515  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
516           class Proj1 = identity, class Proj2 = identity,
517           indirect_strict_weak_order<projected<I1, Proj1>,
518                                      projected<I2, Proj2>> Comp = ranges::less>
519    constexpr bool
520      ranges::lexicographical_compare(I1 first1, S1 last1, I2 first2, S2 last2,
521                                      Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});    // since C++20
522
523  template<input_range R1, input_range R2, class Proj1 = identity,
524           class Proj2 = identity,
525           indirect_strict_weak_order<projected<iterator_t<R1>, Proj1>,
526                                      projected<iterator_t<R2>, Proj2>> Comp = ranges::less>
527    constexpr bool
528      ranges::lexicographical_compare(R1&& r1, R2&& r2, Comp comp = {},
529                                      Proj1 proj1 = {}, Proj2 proj2 = {});                    // since C++20
530
531  template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2>
532    requires indirectly_movable<I1, I2>
533    constexpr ranges::move_backward_result<I1, I2>
534      ranges::move_backward(I1 first, S1 last, I2 result);                                          // since C++20
535
536  template<bidirectional_range R, bidirectional_iterator I>
537    requires indirectly_movable<iterator_t<R>, I>
538    constexpr ranges::move_backward_result<borrowed_iterator_t<R>, I>
539      ranges::move_backward(R&& r, I result);                                                       // since C++20
540
541  template<input_iterator I, sentinel_for<I> S, weakly_incrementable O>
542    requires indirectly_movable<I, O>
543    constexpr ranges::move_result<I, O>
544      ranges::move(I first, S last, O result);                                                      // since C++20
545
546  template<input_range R, weakly_incrementable O>
547    requires indirectly_movable<iterator_t<R>, O>
548    constexpr ranges::move_result<borrowed_iterator_t<R>, O>
549      ranges::move(R&& r, O result);                                                                // since C++20
550
551  template<class I1, class I2, class O>
552    using merge_result = in_in_out_result<I1, I2, O>;                                               // since C++20
553
554  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
555           weakly_incrementable O, class Comp = ranges::less, class Proj1 = identity,
556           class Proj2 = identity>
557    requires mergeable<I1, I2, O, Comp, Proj1, Proj2>
558    constexpr merge_result<I1, I2, O>
559      merge(I1 first1, S1 last1, I2 first2, S2 last2, O result,
560            Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                                    // since C++20
561
562  template<input_range R1, input_range R2, weakly_incrementable O, class Comp = ranges::less,
563           class Proj1 = identity, class Proj2 = identity>
564    requires mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
565    constexpr merge_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
566      merge(R1&& r1, R2&& r2, O result,
567            Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                                    // since C++20
568
569  template<permutable I, sentinel_for<I> S, class T, class Proj = identity>
570    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
571    constexpr subrange<I> ranges::remove(I first, S last, const T& value, Proj proj = {});          // since C++20
572
573  template<forward_range R, class T, class Proj = identity>
574    requires permutable<iterator_t<R>> &&
575             indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
576    constexpr borrowed_subrange_t<R>
577      ranges::remove(R&& r, const T& value, Proj proj = {});                                        // since C++20
578
579  template<permutable I, sentinel_for<I> S, class Proj = identity,
580           indirect_unary_predicate<projected<I, Proj>> Pred>
581    constexpr subrange<I> ranges::remove_if(I first, S last, Pred pred, Proj proj = {});            // since C++20
582
583  template<forward_range R, class Proj = identity,
584           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
585    requires permutable<iterator_t<R>>
586    constexpr borrowed_subrange_t<R>
587      ranges::remove_if(R&& r, Pred pred, Proj proj = {});                                          // since C++20
588
589  template<class I, class O>
590    using set_difference_result = in_out_result<I, O>;                                              // since C++20
591
592  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
593           weakly_incrementable O, class Comp = ranges::less,
594           class Proj1 = identity, class Proj2 = identity>
595    requires mergeable<I1, I2, O, Comp, Proj1, Proj2>
596    constexpr set_difference_result<I1, O>
597      set_difference(I1 first1, S1 last1, I2 first2, S2 last2, O result,
598                     Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
599
600  template<input_range R1, input_range R2, weakly_incrementable O,
601           class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity>
602    requires mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
603    constexpr set_difference_result<borrowed_iterator_t<R1>, O>
604      set_difference(R1&& r1, R2&& r2, O result,
605                     Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
606
607}
608
609    constexpr bool     // constexpr in C++20
610    all_of(InputIterator first, InputIterator last, Predicate pred);
611
612template <class InputIterator, class Predicate>
613    constexpr bool     // constexpr in C++20
614    any_of(InputIterator first, InputIterator last, Predicate pred);
615
616template <class InputIterator, class Predicate>
617    constexpr bool     // constexpr in C++20
618    none_of(InputIterator first, InputIterator last, Predicate pred);
619
620template <class InputIterator, class Function>
621    constexpr Function          // constexpr in C++20
622    for_each(InputIterator first, InputIterator last, Function f);
623
624template<class InputIterator, class Size, class Function>
625    constexpr InputIterator     // constexpr in C++20
626    for_each_n(InputIterator first, Size n, Function f); // C++17
627
628template <class InputIterator, class T>
629    constexpr InputIterator     // constexpr in C++20
630    find(InputIterator first, InputIterator last, const T& value);
631
632template <class InputIterator, class Predicate>
633    constexpr InputIterator     // constexpr in C++20
634    find_if(InputIterator first, InputIterator last, Predicate pred);
635
636template<class InputIterator, class Predicate>
637    constexpr InputIterator     // constexpr in C++20
638    find_if_not(InputIterator first, InputIterator last, Predicate pred);
639
640template <class ForwardIterator1, class ForwardIterator2>
641    constexpr ForwardIterator1  // constexpr in C++20
642    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
643             ForwardIterator2 first2, ForwardIterator2 last2);
644
645template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
646    constexpr ForwardIterator1  // constexpr in C++20
647    find_end(ForwardIterator1 first1, ForwardIterator1 last1,
648             ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
649
650template <class ForwardIterator1, class ForwardIterator2>
651    constexpr ForwardIterator1  // constexpr in C++20
652    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
653                  ForwardIterator2 first2, ForwardIterator2 last2);
654
655template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
656    constexpr ForwardIterator1  // constexpr in C++20
657    find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
658                  ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
659
660template <class ForwardIterator>
661    constexpr ForwardIterator   // constexpr in C++20
662    adjacent_find(ForwardIterator first, ForwardIterator last);
663
664template <class ForwardIterator, class BinaryPredicate>
665    constexpr ForwardIterator   // constexpr in C++20
666    adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
667
668template <class InputIterator, class T>
669    constexpr typename iterator_traits<InputIterator>::difference_type  // constexpr in C++20
670    count(InputIterator first, InputIterator last, const T& value);
671
672template <class InputIterator, class Predicate>
673    constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
674    count_if(InputIterator first, InputIterator last, Predicate pred);
675
676template <class InputIterator1, class InputIterator2>
677    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
678    mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
679
680template <class InputIterator1, class InputIterator2>
681    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
682    mismatch(InputIterator1 first1, InputIterator1 last1,
683             InputIterator2 first2, InputIterator2 last2); // **C++14**
684
685template <class InputIterator1, class InputIterator2, class BinaryPredicate>
686    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
687    mismatch(InputIterator1 first1, InputIterator1 last1,
688             InputIterator2 first2, BinaryPredicate pred);
689
690template <class InputIterator1, class InputIterator2, class BinaryPredicate>
691    constexpr pair<InputIterator1, InputIterator2>   // constexpr in C++20
692    mismatch(InputIterator1 first1, InputIterator1 last1,
693             InputIterator2 first2, InputIterator2 last2,
694             BinaryPredicate pred); // **C++14**
695
696template <class InputIterator1, class InputIterator2>
697    constexpr bool      // constexpr in C++20
698    equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
699
700template <class InputIterator1, class InputIterator2>
701    constexpr bool      // constexpr in C++20
702    equal(InputIterator1 first1, InputIterator1 last1,
703          InputIterator2 first2, InputIterator2 last2); // **C++14**
704
705template <class InputIterator1, class InputIterator2, class BinaryPredicate>
706    constexpr bool      // constexpr in C++20
707    equal(InputIterator1 first1, InputIterator1 last1,
708          InputIterator2 first2, BinaryPredicate pred);
709
710template <class InputIterator1, class InputIterator2, class BinaryPredicate>
711    constexpr bool      // constexpr in C++20
712    equal(InputIterator1 first1, InputIterator1 last1,
713          InputIterator2 first2, InputIterator2 last2,
714          BinaryPredicate pred); // **C++14**
715
716template<class ForwardIterator1, class ForwardIterator2>
717    constexpr bool      // constexpr in C++20
718    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
719                   ForwardIterator2 first2);
720
721template<class ForwardIterator1, class ForwardIterator2>
722    constexpr bool      // constexpr in C++20
723    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
724                   ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
725
726template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
727    constexpr bool      // constexpr in C++20
728    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
729                   ForwardIterator2 first2, BinaryPredicate pred);
730
731template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
732    constexpr bool      // constexpr in C++20
733    is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
734                   ForwardIterator2 first2, ForwardIterator2 last2,
735                   BinaryPredicate pred);  // **C++14**
736
737template <class ForwardIterator1, class ForwardIterator2>
738    constexpr ForwardIterator1      // constexpr in C++20
739    search(ForwardIterator1 first1, ForwardIterator1 last1,
740           ForwardIterator2 first2, ForwardIterator2 last2);
741
742template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
743    constexpr ForwardIterator1      // constexpr in C++20
744    search(ForwardIterator1 first1, ForwardIterator1 last1,
745           ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
746
747template <class ForwardIterator, class Size, class T>
748    constexpr ForwardIterator       // constexpr in C++20
749    search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
750
751template <class ForwardIterator, class Size, class T, class BinaryPredicate>
752    constexpr ForwardIterator       // constexpr in C++20
753    search_n(ForwardIterator first, ForwardIterator last,
754             Size count, const T& value, BinaryPredicate pred);
755
756template <class InputIterator, class OutputIterator>
757    constexpr OutputIterator      // constexpr in C++20
758    copy(InputIterator first, InputIterator last, OutputIterator result);
759
760template<class InputIterator, class OutputIterator, class Predicate>
761    constexpr OutputIterator      // constexpr in C++20
762    copy_if(InputIterator first, InputIterator last,
763            OutputIterator result, Predicate pred);
764
765template<class InputIterator, class Size, class OutputIterator>
766    constexpr OutputIterator      // constexpr in C++20
767    copy_n(InputIterator first, Size n, OutputIterator result);
768
769template <class BidirectionalIterator1, class BidirectionalIterator2>
770    constexpr BidirectionalIterator2      // constexpr in C++20
771    copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
772                  BidirectionalIterator2 result);
773
774// [alg.move], move
775template<class InputIterator, class OutputIterator>
776    constexpr OutputIterator move(InputIterator first, InputIterator last,
777                                OutputIterator result);
778
779template<class BidirectionalIterator1, class BidirectionalIterator2>
780    constexpr BidirectionalIterator2
781    move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
782                  BidirectionalIterator2 result);
783
784template <class ForwardIterator1, class ForwardIterator2>
785    constexpr ForwardIterator2    // constexpr in C++20
786    swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
787
788namespace ranges {
789    template<class I1, class I2>
790    using swap_ranges_result = in_in_result<I1, I2>;
791
792template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
793        requires indirectly_swappable<I1, I2>
794    constexpr ranges::swap_ranges_result<I1, I2>
795        swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
796
797template<input_range R1, input_range R2>
798        requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
799    constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
800        swap_ranges(R1&& r1, R2&& r2);
801}
802
803template <class ForwardIterator1, class ForwardIterator2>
804    constexpr void                // constexpr in C++20
805    iter_swap(ForwardIterator1 a, ForwardIterator2 b);
806
807template <class InputIterator, class OutputIterator, class UnaryOperation>
808    constexpr OutputIterator      // constexpr in C++20
809    transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
810
811template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
812    constexpr OutputIterator      // constexpr in C++20
813    transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
814              OutputIterator result, BinaryOperation binary_op);
815
816template <class ForwardIterator, class T>
817    constexpr void      // constexpr in C++20
818    replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
819
820template <class ForwardIterator, class Predicate, class T>
821    constexpr void      // constexpr in C++20
822    replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
823
824template <class InputIterator, class OutputIterator, class T>
825    constexpr OutputIterator      // constexpr in C++20
826    replace_copy(InputIterator first, InputIterator last, OutputIterator result,
827                 const T& old_value, const T& new_value);
828
829template <class InputIterator, class OutputIterator, class Predicate, class T>
830    constexpr OutputIterator      // constexpr in C++20
831    replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
832
833template <class ForwardIterator, class T>
834    constexpr void      // constexpr in C++20
835    fill(ForwardIterator first, ForwardIterator last, const T& value);
836
837template <class OutputIterator, class Size, class T>
838    constexpr OutputIterator      // constexpr in C++20
839    fill_n(OutputIterator first, Size n, const T& value);
840
841template <class ForwardIterator, class Generator>
842    constexpr void      // constexpr in C++20
843    generate(ForwardIterator first, ForwardIterator last, Generator gen);
844
845template <class OutputIterator, class Size, class Generator>
846    constexpr OutputIterator      // constexpr in C++20
847    generate_n(OutputIterator first, Size n, Generator gen);
848
849template <class ForwardIterator, class T>
850    constexpr ForwardIterator     // constexpr in C++20
851    remove(ForwardIterator first, ForwardIterator last, const T& value);
852
853template <class ForwardIterator, class Predicate>
854    constexpr ForwardIterator     // constexpr in C++20
855    remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
856
857template <class InputIterator, class OutputIterator, class T>
858    constexpr OutputIterator     // constexpr in C++20
859    remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
860
861template <class InputIterator, class OutputIterator, class Predicate>
862    constexpr OutputIterator     // constexpr in C++20
863    remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
864
865template <class ForwardIterator>
866    constexpr ForwardIterator    // constexpr in C++20
867    unique(ForwardIterator first, ForwardIterator last);
868
869template <class ForwardIterator, class BinaryPredicate>
870    constexpr ForwardIterator    // constexpr in C++20
871    unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
872
873template <class InputIterator, class OutputIterator>
874    constexpr OutputIterator     // constexpr in C++20
875    unique_copy(InputIterator first, InputIterator last, OutputIterator result);
876
877template <class InputIterator, class OutputIterator, class BinaryPredicate>
878    constexpr OutputIterator     // constexpr in C++20
879    unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
880
881template <class BidirectionalIterator>
882    constexpr void               // constexpr in C++20
883    reverse(BidirectionalIterator first, BidirectionalIterator last);
884
885template <class BidirectionalIterator, class OutputIterator>
886    constexpr OutputIterator       // constexpr in C++20
887    reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
888
889template <class ForwardIterator>
890    constexpr ForwardIterator      // constexpr in C++20
891    rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
892
893template <class ForwardIterator, class OutputIterator>
894    constexpr OutputIterator       // constexpr in C++20
895    rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
896
897template <class RandomAccessIterator>
898    void
899    random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
900
901template <class RandomAccessIterator, class RandomNumberGenerator>
902    void
903    random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
904                   RandomNumberGenerator& rand);  // deprecated in C++14, removed in C++17
905
906template<class PopulationIterator, class SampleIterator,
907         class Distance, class UniformRandomBitGenerator>
908    SampleIterator sample(PopulationIterator first, PopulationIterator last,
909                          SampleIterator out, Distance n,
910                          UniformRandomBitGenerator&& g); // C++17
911
912template<class RandomAccessIterator, class UniformRandomNumberGenerator>
913    void shuffle(RandomAccessIterator first, RandomAccessIterator last,
914                 UniformRandomNumberGenerator&& g);
915
916template<class ForwardIterator>
917  constexpr ForwardIterator
918    shift_left(ForwardIterator first, ForwardIterator last,
919               typename iterator_traits<ForwardIterator>::difference_type n); // C++20
920
921template<class ForwardIterator>
922  constexpr ForwardIterator
923    shift_right(ForwardIterator first, ForwardIterator last,
924                typename iterator_traits<ForwardIterator>::difference_type n); // C++20
925
926template <class InputIterator, class Predicate>
927    constexpr bool  // constexpr in C++20
928    is_partitioned(InputIterator first, InputIterator last, Predicate pred);
929
930template <class ForwardIterator, class Predicate>
931    constexpr ForwardIterator  // constexpr in C++20
932    partition(ForwardIterator first, ForwardIterator last, Predicate pred);
933
934template <class InputIterator, class OutputIterator1,
935          class OutputIterator2, class Predicate>
936    constexpr pair<OutputIterator1, OutputIterator2>   // constexpr in C++20
937    partition_copy(InputIterator first, InputIterator last,
938                   OutputIterator1 out_true, OutputIterator2 out_false,
939                   Predicate pred);
940
941template <class ForwardIterator, class Predicate>
942    ForwardIterator
943    stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
944
945template<class ForwardIterator, class Predicate>
946    constexpr ForwardIterator  // constexpr in C++20
947    partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
948
949template <class ForwardIterator>
950    constexpr bool  // constexpr in C++20
951    is_sorted(ForwardIterator first, ForwardIterator last);
952
953template <class ForwardIterator, class Compare>
954    constexpr bool  // constexpr in C++20
955    is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
956
957template<class ForwardIterator>
958    constexpr ForwardIterator    // constexpr in C++20
959    is_sorted_until(ForwardIterator first, ForwardIterator last);
960
961template <class ForwardIterator, class Compare>
962    constexpr ForwardIterator    // constexpr in C++20
963    is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
964
965template <class RandomAccessIterator>
966    constexpr void               // constexpr in C++20
967    sort(RandomAccessIterator first, RandomAccessIterator last);
968
969template <class RandomAccessIterator, class Compare>
970    constexpr void               // constexpr in C++20
971    sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
972
973template <class RandomAccessIterator>
974    void
975    stable_sort(RandomAccessIterator first, RandomAccessIterator last);
976
977template <class RandomAccessIterator, class Compare>
978    void
979    stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
980
981template <class RandomAccessIterator>
982    constexpr void                    // constexpr in C++20
983    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
984
985template <class RandomAccessIterator, class Compare>
986    constexpr void                    // constexpr in C++20
987    partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
988
989template <class InputIterator, class RandomAccessIterator>
990    constexpr RandomAccessIterator    // constexpr in C++20
991    partial_sort_copy(InputIterator first, InputIterator last,
992                      RandomAccessIterator result_first, RandomAccessIterator result_last);
993
994template <class InputIterator, class RandomAccessIterator, class Compare>
995    constexpr RandomAccessIterator    // constexpr in C++20
996    partial_sort_copy(InputIterator first, InputIterator last,
997                      RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
998
999template <class RandomAccessIterator>
1000    constexpr void                    // constexpr in C++20
1001    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
1002
1003template <class RandomAccessIterator, class Compare>
1004    constexpr void                    // constexpr in C++20
1005    nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
1006
1007template <class ForwardIterator, class T>
1008    constexpr ForwardIterator                         // constexpr in C++20
1009    lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
1010
1011template <class ForwardIterator, class T, class Compare>
1012    constexpr ForwardIterator                         // constexpr in C++20
1013    lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
1014
1015template <class ForwardIterator, class T>
1016    constexpr ForwardIterator                         // constexpr in C++20
1017    upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
1018
1019template <class ForwardIterator, class T, class Compare>
1020    constexpr ForwardIterator                         // constexpr in C++20
1021    upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
1022
1023template <class ForwardIterator, class T>
1024    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++20
1025    equal_range(ForwardIterator first, ForwardIterator last, const T& value);
1026
1027template <class ForwardIterator, class T, class Compare>
1028    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++20
1029    equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
1030
1031template <class ForwardIterator, class T>
1032    constexpr bool                                    // constexpr in C++20
1033    binary_search(ForwardIterator first, ForwardIterator last, const T& value);
1034
1035template <class ForwardIterator, class T, class Compare>
1036    constexpr bool                                    // constexpr in C++20
1037    binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
1038
1039template <class InputIterator1, class InputIterator2, class OutputIterator>
1040    constexpr OutputIterator                          // constexpr in C++20
1041    merge(InputIterator1 first1, InputIterator1 last1,
1042          InputIterator2 first2, InputIterator2 last2, OutputIterator result);
1043
1044template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
1045    constexpr OutputIterator                          // constexpr in C++20
1046    merge(InputIterator1 first1, InputIterator1 last1,
1047          InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
1048
1049template <class BidirectionalIterator>
1050    void
1051    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
1052
1053template <class BidirectionalIterator, class Compare>
1054    void
1055    inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
1056
1057template <class InputIterator1, class InputIterator2>
1058    constexpr bool                                    // constexpr in C++20
1059    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
1060
1061template <class InputIterator1, class InputIterator2, class Compare>
1062    constexpr bool                                    // constexpr in C++20
1063    includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
1064
1065template <class InputIterator1, class InputIterator2, class OutputIterator>
1066    constexpr OutputIterator                          // constexpr in C++20
1067    set_union(InputIterator1 first1, InputIterator1 last1,
1068              InputIterator2 first2, InputIterator2 last2, OutputIterator result);
1069
1070template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
1071    constexpr OutputIterator                          // constexpr in C++20
1072    set_union(InputIterator1 first1, InputIterator1 last1,
1073              InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
1074
1075template <class InputIterator1, class InputIterator2, class OutputIterator>
1076    constexpr OutputIterator                         // constexpr in C++20
1077    set_intersection(InputIterator1 first1, InputIterator1 last1,
1078                     InputIterator2 first2, InputIterator2 last2, OutputIterator result);
1079
1080template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
1081    constexpr OutputIterator                         // constexpr in C++20
1082    set_intersection(InputIterator1 first1, InputIterator1 last1,
1083                     InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
1084
1085template <class InputIterator1, class InputIterator2, class OutputIterator>
1086    constexpr OutputIterator                         // constexpr in C++20
1087    set_difference(InputIterator1 first1, InputIterator1 last1,
1088                   InputIterator2 first2, InputIterator2 last2, OutputIterator result);
1089
1090template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
1091    constexpr OutputIterator                         // constexpr in C++20
1092    set_difference(InputIterator1 first1, InputIterator1 last1,
1093                   InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
1094
1095template <class InputIterator1, class InputIterator2, class OutputIterator>
1096    constexpr OutputIterator                         // constexpr in C++20
1097    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
1098                             InputIterator2 first2, InputIterator2 last2, OutputIterator result);
1099
1100template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
1101    constexpr OutputIterator                         // constexpr in C++20
1102    set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
1103                             InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
1104
1105template <class RandomAccessIterator>
1106    constexpr void                                   // constexpr in C++20
1107    push_heap(RandomAccessIterator first, RandomAccessIterator last);
1108
1109template <class RandomAccessIterator, class Compare>
1110    constexpr void                                   // constexpr in C++20
1111    push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1112
1113template <class RandomAccessIterator>
1114    constexpr void                                   // constexpr in C++20
1115    pop_heap(RandomAccessIterator first, RandomAccessIterator last);
1116
1117template <class RandomAccessIterator, class Compare>
1118    constexpr void                                   // constexpr in C++20
1119    pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1120
1121template <class RandomAccessIterator>
1122    constexpr void                                   // constexpr in C++20
1123    make_heap(RandomAccessIterator first, RandomAccessIterator last);
1124
1125template <class RandomAccessIterator, class Compare>
1126    constexpr void                                   // constexpr in C++20
1127    make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1128
1129template <class RandomAccessIterator>
1130    constexpr void                                   // constexpr in C++20
1131    sort_heap(RandomAccessIterator first, RandomAccessIterator last);
1132
1133template <class RandomAccessIterator, class Compare>
1134    constexpr void                                   // constexpr in C++20
1135    sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1136
1137template <class RandomAccessIterator>
1138    constexpr bool   // constexpr in C++20
1139    is_heap(RandomAccessIterator first, RandomAccessiterator last);
1140
1141template <class RandomAccessIterator, class Compare>
1142    constexpr bool   // constexpr in C++20
1143    is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
1144
1145template <class RandomAccessIterator>
1146    constexpr RandomAccessIterator   // constexpr in C++20
1147    is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
1148
1149template <class RandomAccessIterator, class Compare>
1150    constexpr RandomAccessIterator   // constexpr in C++20
1151    is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
1152
1153template <class ForwardIterator>
1154    constexpr ForwardIterator        // constexpr in C++14
1155    min_element(ForwardIterator first, ForwardIterator last);
1156
1157template <class ForwardIterator, class Compare>
1158    constexpr ForwardIterator        // constexpr in C++14
1159    min_element(ForwardIterator first, ForwardIterator last, Compare comp);
1160
1161template <class T>
1162    constexpr const T&               // constexpr in C++14
1163    min(const T& a, const T& b);
1164
1165template <class T, class Compare>
1166    constexpr const T&               // constexpr in C++14
1167    min(const T& a, const T& b, Compare comp);
1168
1169template<class T>
1170    constexpr T                      // constexpr in C++14
1171    min(initializer_list<T> t);
1172
1173template<class T, class Compare>
1174    constexpr T                      // constexpr in C++14
1175    min(initializer_list<T> t, Compare comp);
1176
1177template<class T>
1178    constexpr const T& clamp(const T& v, const T& lo, const T& hi);               // C++17
1179
1180template<class T, class Compare>
1181    constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
1182
1183template <class ForwardIterator>
1184    constexpr ForwardIterator        // constexpr in C++14
1185    max_element(ForwardIterator first, ForwardIterator last);
1186
1187template <class ForwardIterator, class Compare>
1188    constexpr ForwardIterator        // constexpr in C++14
1189    max_element(ForwardIterator first, ForwardIterator last, Compare comp);
1190
1191template <class T>
1192    constexpr const T&               // constexpr in C++14
1193    max(const T& a, const T& b);
1194
1195template <class T, class Compare>
1196    constexpr const T&               // constexpr in C++14
1197    max(const T& a, const T& b, Compare comp);
1198
1199template<class T>
1200    constexpr T                      // constexpr in C++14
1201    max(initializer_list<T> t);
1202
1203template<class T, class Compare>
1204    constexpr T                      // constexpr in C++14
1205    max(initializer_list<T> t, Compare comp);
1206
1207template<class ForwardIterator>
1208    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++14
1209    minmax_element(ForwardIterator first, ForwardIterator last);
1210
1211template<class ForwardIterator, class Compare>
1212    constexpr pair<ForwardIterator, ForwardIterator>  // constexpr in C++14
1213    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
1214
1215template<class T>
1216    constexpr pair<const T&, const T&>  // constexpr in C++14
1217    minmax(const T& a, const T& b);
1218
1219template<class T, class Compare>
1220    constexpr pair<const T&, const T&>  // constexpr in C++14
1221    minmax(const T& a, const T& b, Compare comp);
1222
1223template<class T>
1224    constexpr pair<T, T>                // constexpr in C++14
1225    minmax(initializer_list<T> t);
1226
1227template<class T, class Compare>
1228    constexpr pair<T, T>                // constexpr in C++14
1229    minmax(initializer_list<T> t, Compare comp);
1230
1231template <class InputIterator1, class InputIterator2>
1232    constexpr bool     // constexpr in C++20
1233    lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
1234
1235template <class InputIterator1, class InputIterator2, class Compare>
1236    constexpr bool     // constexpr in C++20
1237    lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
1238                            InputIterator2 first2, InputIterator2 last2, Compare comp);
1239
1240template <class BidirectionalIterator>
1241    constexpr bool     // constexpr in C++20
1242    next_permutation(BidirectionalIterator first, BidirectionalIterator last);
1243
1244template <class BidirectionalIterator, class Compare>
1245    constexpr bool     // constexpr in C++20
1246    next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
1247
1248template <class BidirectionalIterator>
1249    constexpr bool     // constexpr in C++20
1250    prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
1251
1252template <class BidirectionalIterator, class Compare>
1253    constexpr bool     // constexpr in C++20
1254    prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
1255}  // std
1256
1257*/
1258
1259#include <__assert> // all public C++ headers provide the assertion handler
1260#include <__bits>
1261#include <__config>
1262#include <__debug>
1263#include <cstddef>
1264#include <cstring>
1265#include <memory>
1266#include <type_traits>
1267#include <version>
1268
1269#include <__algorithm/adjacent_find.h>
1270#include <__algorithm/all_of.h>
1271#include <__algorithm/any_of.h>
1272#include <__algorithm/binary_search.h>
1273#include <__algorithm/clamp.h>
1274#include <__algorithm/comp.h>
1275#include <__algorithm/comp_ref_type.h>
1276#include <__algorithm/copy.h>
1277#include <__algorithm/copy_backward.h>
1278#include <__algorithm/copy_if.h>
1279#include <__algorithm/copy_n.h>
1280#include <__algorithm/count.h>
1281#include <__algorithm/count_if.h>
1282#include <__algorithm/equal.h>
1283#include <__algorithm/equal_range.h>
1284#include <__algorithm/fill.h>
1285#include <__algorithm/fill_n.h>
1286#include <__algorithm/find.h>
1287#include <__algorithm/find_end.h>
1288#include <__algorithm/find_first_of.h>
1289#include <__algorithm/find_if.h>
1290#include <__algorithm/find_if_not.h>
1291#include <__algorithm/for_each.h>
1292#include <__algorithm/for_each_n.h>
1293#include <__algorithm/generate.h>
1294#include <__algorithm/generate_n.h>
1295#include <__algorithm/half_positive.h>
1296#include <__algorithm/in_found_result.h>
1297#include <__algorithm/in_fun_result.h>
1298#include <__algorithm/in_in_out_result.h>
1299#include <__algorithm/in_in_result.h>
1300#include <__algorithm/in_out_out_result.h>
1301#include <__algorithm/in_out_result.h>
1302#include <__algorithm/includes.h>
1303#include <__algorithm/inplace_merge.h>
1304#include <__algorithm/is_heap.h>
1305#include <__algorithm/is_heap_until.h>
1306#include <__algorithm/is_partitioned.h>
1307#include <__algorithm/is_permutation.h>
1308#include <__algorithm/is_sorted.h>
1309#include <__algorithm/is_sorted_until.h>
1310#include <__algorithm/iter_swap.h>
1311#include <__algorithm/lexicographical_compare.h>
1312#include <__algorithm/lower_bound.h>
1313#include <__algorithm/make_heap.h>
1314#include <__algorithm/max.h>
1315#include <__algorithm/max_element.h>
1316#include <__algorithm/merge.h>
1317#include <__algorithm/min.h>
1318#include <__algorithm/min_element.h>
1319#include <__algorithm/min_max_result.h>
1320#include <__algorithm/minmax.h>
1321#include <__algorithm/minmax_element.h>
1322#include <__algorithm/mismatch.h>
1323#include <__algorithm/move.h>
1324#include <__algorithm/move_backward.h>
1325#include <__algorithm/next_permutation.h>
1326#include <__algorithm/none_of.h>
1327#include <__algorithm/nth_element.h>
1328#include <__algorithm/partial_sort.h>
1329#include <__algorithm/partial_sort_copy.h>
1330#include <__algorithm/partition.h>
1331#include <__algorithm/partition_copy.h>
1332#include <__algorithm/partition_point.h>
1333#include <__algorithm/pop_heap.h>
1334#include <__algorithm/prev_permutation.h>
1335#include <__algorithm/push_heap.h>
1336#include <__algorithm/ranges_adjacent_find.h>
1337#include <__algorithm/ranges_all_of.h>
1338#include <__algorithm/ranges_any_of.h>
1339#include <__algorithm/ranges_binary_search.h>
1340#include <__algorithm/ranges_copy.h>
1341#include <__algorithm/ranges_copy_backward.h>
1342#include <__algorithm/ranges_copy_if.h>
1343#include <__algorithm/ranges_copy_n.h>
1344#include <__algorithm/ranges_count.h>
1345#include <__algorithm/ranges_count_if.h>
1346#include <__algorithm/ranges_equal.h>
1347#include <__algorithm/ranges_fill.h>
1348#include <__algorithm/ranges_fill_n.h>
1349#include <__algorithm/ranges_find.h>
1350#include <__algorithm/ranges_find_first_of.h>
1351#include <__algorithm/ranges_find_if.h>
1352#include <__algorithm/ranges_find_if_not.h>
1353#include <__algorithm/ranges_for_each.h>
1354#include <__algorithm/ranges_for_each_n.h>
1355#include <__algorithm/ranges_is_partitioned.h>
1356#include <__algorithm/ranges_is_sorted.h>
1357#include <__algorithm/ranges_is_sorted_until.h>
1358#include <__algorithm/ranges_lexicographical_compare.h>
1359#include <__algorithm/ranges_lower_bound.h>
1360#include <__algorithm/ranges_make_heap.h>
1361#include <__algorithm/ranges_max.h>
1362#include <__algorithm/ranges_max_element.h>
1363#include <__algorithm/ranges_merge.h>
1364#include <__algorithm/ranges_min.h>
1365#include <__algorithm/ranges_min_element.h>
1366#include <__algorithm/ranges_minmax.h>
1367#include <__algorithm/ranges_minmax_element.h>
1368#include <__algorithm/ranges_mismatch.h>
1369#include <__algorithm/ranges_move.h>
1370#include <__algorithm/ranges_move_backward.h>
1371#include <__algorithm/ranges_none_of.h>
1372#include <__algorithm/ranges_nth_element.h>
1373#include <__algorithm/ranges_pop_heap.h>
1374#include <__algorithm/ranges_push_heap.h>
1375#include <__algorithm/ranges_remove.h>
1376#include <__algorithm/ranges_remove_if.h>
1377#include <__algorithm/ranges_replace.h>
1378#include <__algorithm/ranges_replace_if.h>
1379#include <__algorithm/ranges_reverse.h>
1380#include <__algorithm/ranges_set_difference.h>
1381#include <__algorithm/ranges_sort.h>
1382#include <__algorithm/ranges_sort_heap.h>
1383#include <__algorithm/ranges_stable_sort.h>
1384#include <__algorithm/ranges_swap_ranges.h>
1385#include <__algorithm/ranges_transform.h>
1386#include <__algorithm/ranges_upper_bound.h>
1387#include <__algorithm/remove.h>
1388#include <__algorithm/remove_copy.h>
1389#include <__algorithm/remove_copy_if.h>
1390#include <__algorithm/remove_if.h>
1391#include <__algorithm/replace.h>
1392#include <__algorithm/replace_copy.h>
1393#include <__algorithm/replace_copy_if.h>
1394#include <__algorithm/replace_if.h>
1395#include <__algorithm/reverse.h>
1396#include <__algorithm/reverse_copy.h>
1397#include <__algorithm/rotate.h>
1398#include <__algorithm/rotate_copy.h>
1399#include <__algorithm/sample.h>
1400#include <__algorithm/search.h>
1401#include <__algorithm/search_n.h>
1402#include <__algorithm/set_difference.h>
1403#include <__algorithm/set_intersection.h>
1404#include <__algorithm/set_symmetric_difference.h>
1405#include <__algorithm/set_union.h>
1406#include <__algorithm/shift_left.h>
1407#include <__algorithm/shift_right.h>
1408#include <__algorithm/shuffle.h>
1409#include <__algorithm/sift_down.h>
1410#include <__algorithm/sort.h>
1411#include <__algorithm/sort_heap.h>
1412#include <__algorithm/stable_partition.h>
1413#include <__algorithm/stable_sort.h>
1414#include <__algorithm/swap_ranges.h>
1415#include <__algorithm/transform.h>
1416#include <__algorithm/unique.h>
1417#include <__algorithm/unique_copy.h>
1418#include <__algorithm/unwrap_iter.h>
1419#include <__algorithm/upper_bound.h>
1420
1421#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
1422#  include <chrono>
1423#  include <iterator>
1424#  include <utility>
1425#endif
1426
1427// standard-mandated includes
1428#include <initializer_list>
1429
1430#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1431#  pragma GCC system_header
1432#endif
1433
1434#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
1435#   include <__pstl_algorithm>
1436#endif
1437
1438#endif // _LIBCPP_ALGORITHM
1439