libstdc++
ranges_algo.h
Go to the documentation of this file.
1// Core algorithmic facilities -*- C++ -*-
2
3// Copyright (C) 2020-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/ranges_algo.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{algorithm}
28 */
29
30#ifndef _RANGES_ALGO_H
31#define _RANGES_ALGO_H 1
32
33#if __cplusplus > 201703L
34
35#if __cplusplus > 202002L
36#include <optional>
37#endif
39#include <bits/ranges_util.h>
40#include <bits/uniform_int_dist.h> // concept uniform_random_bit_generator
41
42#if __glibcxx_concepts
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46namespace ranges
47{
48 namespace __detail
49 {
50 template<typename _Comp, typename _Proj>
51 constexpr auto
52 __make_comp_proj(_Comp& __comp, _Proj& __proj)
53 {
54 return [&] (auto&& __lhs, auto&& __rhs) -> bool {
55 using _TL = decltype(__lhs);
56 using _TR = decltype(__rhs);
57 return std::__invoke(__comp,
58 std::__invoke(__proj, std::forward<_TL>(__lhs)),
59 std::__invoke(__proj, std::forward<_TR>(__rhs)));
60 };
61 }
62
63 template<typename _Pred, typename _Proj>
64 constexpr auto
65 __make_pred_proj(_Pred& __pred, _Proj& __proj)
66 {
67 return [&] <typename _Tp> (_Tp&& __arg) -> bool {
68 return std::__invoke(__pred,
69 std::__invoke(__proj, std::forward<_Tp>(__arg)));
70 };
71 }
72 } // namespace __detail
73
74 struct __all_of_fn
75 {
76 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
77 typename _Proj = identity,
78 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
79 constexpr bool
80 operator()(_Iter __first, _Sent __last,
81 _Pred __pred, _Proj __proj = {}) const
82 {
83 for (; __first != __last; ++__first)
84 if (!(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
85 return false;
86 return true;
87 }
88
89 template<input_range _Range, typename _Proj = identity,
90 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
91 _Pred>
92 constexpr bool
93 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
94 {
95 return (*this)(ranges::begin(__r), ranges::end(__r),
96 std::move(__pred), std::move(__proj));
97 }
98 };
99
100 inline constexpr __all_of_fn all_of{};
101
102 struct __any_of_fn
103 {
104 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
105 typename _Proj = identity,
106 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
107 constexpr bool
108 operator()(_Iter __first, _Sent __last,
109 _Pred __pred, _Proj __proj = {}) const
110 {
111 for (; __first != __last; ++__first)
112 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
113 return true;
114 return false;
115 }
116
117 template<input_range _Range, typename _Proj = identity,
118 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
119 _Pred>
120 constexpr bool
121 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
122 {
123 return (*this)(ranges::begin(__r), ranges::end(__r),
124 std::move(__pred), std::move(__proj));
125 }
126 };
127
128 inline constexpr __any_of_fn any_of{};
129
130 struct __none_of_fn
131 {
132 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
133 typename _Proj = identity,
134 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
135 constexpr bool
136 operator()(_Iter __first, _Sent __last,
137 _Pred __pred, _Proj __proj = {}) const
138 {
139 for (; __first != __last; ++__first)
140 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
141 return false;
142 return true;
143 }
144
145 template<input_range _Range, typename _Proj = identity,
146 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
147 _Pred>
148 constexpr bool
149 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
150 {
151 return (*this)(ranges::begin(__r), ranges::end(__r),
152 std::move(__pred), std::move(__proj));
153 }
154 };
155
156 inline constexpr __none_of_fn none_of{};
157
158 template<typename _Iter, typename _Fp>
159 struct in_fun_result
160 {
161 [[no_unique_address]] _Iter in;
162 [[no_unique_address]] _Fp fun;
163
164 template<typename _Iter2, typename _F2p>
165 requires convertible_to<const _Iter&, _Iter2>
166 && convertible_to<const _Fp&, _F2p>
167 constexpr
168 operator in_fun_result<_Iter2, _F2p>() const &
169 { return {in, fun}; }
170
171 template<typename _Iter2, typename _F2p>
172 requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
173 constexpr
174 operator in_fun_result<_Iter2, _F2p>() &&
175 { return {std::move(in), std::move(fun)}; }
176 };
177
178 template<typename _Iter, typename _Fp>
179 using for_each_result = in_fun_result<_Iter, _Fp>;
180
181 struct __for_each_fn
182 {
183 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
184 typename _Proj = identity,
185 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
186 constexpr for_each_result<_Iter, _Fun>
187 operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {}) const
188 {
189 for (; __first != __last; ++__first)
190 std::__invoke(__f, std::__invoke(__proj, *__first));
191 return { std::move(__first), std::move(__f) };
192 }
193
194 template<input_range _Range, typename _Proj = identity,
195 indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
196 _Fun>
197 constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
198 operator()(_Range&& __r, _Fun __f, _Proj __proj = {}) const
199 {
200 return (*this)(ranges::begin(__r), ranges::end(__r),
201 std::move(__f), std::move(__proj));
202 }
203 };
204
205 inline constexpr __for_each_fn for_each{};
206
207 template<typename _Iter, typename _Fp>
208 using for_each_n_result = in_fun_result<_Iter, _Fp>;
209
210 struct __for_each_n_fn
211 {
212 template<input_iterator _Iter, typename _Proj = identity,
213 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
214 constexpr for_each_n_result<_Iter, _Fun>
215 operator()(_Iter __first, iter_difference_t<_Iter> __n,
216 _Fun __f, _Proj __proj = {}) const
217 {
218 if constexpr (random_access_iterator<_Iter>)
219 {
220 if (__n <= 0)
221 return {std::move(__first), std::move(__f)};
222 auto __last = __first + __n;
223 return ranges::for_each(std::move(__first), std::move(__last),
224 std::move(__f), std::move(__proj));
225 }
226 else
227 {
228 while (__n-- > 0)
229 {
230 std::__invoke(__f, std::__invoke(__proj, *__first));
231 ++__first;
232 }
233 return {std::move(__first), std::move(__f)};
234 }
235 }
236 };
237
238 inline constexpr __for_each_n_fn for_each_n{};
239
240 // find, find_if and find_if_not are defined in <bits/ranges_util.h>.
241
242 struct __find_first_of_fn
243 {
244 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
245 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
246 typename _Pred = ranges::equal_to,
247 typename _Proj1 = identity, typename _Proj2 = identity>
248 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
249 constexpr _Iter1
250 operator()(_Iter1 __first1, _Sent1 __last1,
251 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
252 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
253 {
254 for (; __first1 != __last1; ++__first1)
255 for (auto __iter = __first2; __iter != __last2; ++__iter)
256 if (std::__invoke(__pred,
257 std::__invoke(__proj1, *__first1),
258 std::__invoke(__proj2, *__iter)))
259 return __first1;
260 return __first1;
261 }
262
263 template<input_range _Range1, forward_range _Range2,
264 typename _Pred = ranges::equal_to,
265 typename _Proj1 = identity, typename _Proj2 = identity>
266 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
267 _Pred, _Proj1, _Proj2>
268 constexpr borrowed_iterator_t<_Range1>
269 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
270 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
271 {
272 return (*this)(ranges::begin(__r1), ranges::end(__r1),
273 ranges::begin(__r2), ranges::end(__r2),
274 std::move(__pred),
275 std::move(__proj1), std::move(__proj2));
276 }
277 };
278
279 inline constexpr __find_first_of_fn find_first_of{};
280
281 struct __count_fn
282 {
283 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
284 typename _Proj = identity,
285 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
286 requires indirect_binary_predicate<ranges::equal_to,
287 projected<_Iter, _Proj>,
288 const _Tp*>
289 constexpr iter_difference_t<_Iter>
290 operator()(_Iter __first, _Sent __last,
291 const _Tp& __value, _Proj __proj = {}) const
292 {
293 iter_difference_t<_Iter> __n = 0;
294 for (; __first != __last; ++__first)
295 if (std::__invoke(__proj, *__first) == __value)
296 ++__n;
297 return __n;
298 }
299
300 template<input_range _Range, typename _Proj = identity,
301 typename _Tp
302 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
303 requires indirect_binary_predicate<ranges::equal_to,
304 projected<iterator_t<_Range>, _Proj>,
305 const _Tp*>
306 constexpr range_difference_t<_Range>
307 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
308 {
309 return (*this)(ranges::begin(__r), ranges::end(__r),
310 __value, std::move(__proj));
311 }
312 };
313
314 inline constexpr __count_fn count{};
315
316 struct __count_if_fn
317 {
318 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
319 typename _Proj = identity,
320 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
321 constexpr iter_difference_t<_Iter>
322 operator()(_Iter __first, _Sent __last,
323 _Pred __pred, _Proj __proj = {}) const
324 {
325 iter_difference_t<_Iter> __n = 0;
326 for (; __first != __last; ++__first)
327 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
328 ++__n;
329 return __n;
330 }
331
332 template<input_range _Range,
333 typename _Proj = identity,
334 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
335 _Pred>
336 constexpr range_difference_t<_Range>
337 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
338 {
339 return (*this)(ranges::begin(__r), ranges::end(__r),
340 std::move(__pred), std::move(__proj));
341 }
342 };
343
344 inline constexpr __count_if_fn count_if{};
345
346 // in_in_result, mismatch and search are defined in <bits/ranges_util.h>.
347
348 struct __search_n_fn
349 {
350 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
351 typename _Pred = ranges::equal_to, typename _Proj = identity,
352 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
353 requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
354 constexpr subrange<_Iter>
355 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
356 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
357 {
358 if (__count <= 0)
359 return {__first, __first};
360
361 auto __value_comp = [&] <typename _Rp> (_Rp&& __arg) -> bool {
362 return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
363 };
364 if (__count == 1)
365 {
366 __first = ranges::find_if(std::move(__first), __last,
367 std::move(__value_comp),
368 std::move(__proj));
369 if (__first == __last)
370 return {__first, __first};
371 else
372 {
373 auto __end = __first;
374 return {__first, ++__end};
375 }
376 }
377
378 if constexpr (sized_sentinel_for<_Sent, _Iter>
379 && random_access_iterator<_Iter>)
380 {
381 auto __tail_size = __last - __first;
382 auto __remainder = __count;
383
384 while (__remainder <= __tail_size)
385 {
386 __first += __remainder;
387 __tail_size -= __remainder;
388 auto __backtrack = __first;
389 while (__value_comp(std::__invoke(__proj, *--__backtrack)))
390 {
391 if (--__remainder == 0)
392 return {__first - __count, __first};
393 }
394 __remainder = __count + 1 - (__first - __backtrack);
395 }
396 auto __i = __first + __tail_size;
397 return {__i, __i};
398 }
399 else
400 {
401 __first = ranges::find_if(__first, __last, __value_comp, __proj);
402 while (__first != __last)
403 {
404 auto __n = __count;
405 auto __i = __first;
406 ++__i;
407 while (__i != __last && __n != 1
408 && __value_comp(std::__invoke(__proj, *__i)))
409 {
410 ++__i;
411 --__n;
412 }
413 if (__n == 1)
414 return {__first, __i};
415 if (__i == __last)
416 return {__i, __i};
417 __first = ranges::find_if(++__i, __last, __value_comp, __proj);
418 }
419 return {__first, __first};
420 }
421 }
422
423 template<forward_range _Range,
424 typename _Pred = ranges::equal_to, typename _Proj = identity,
425 typename _Tp
426 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
427 requires indirectly_comparable<iterator_t<_Range>, const _Tp*,
428 _Pred, _Proj>
429 constexpr borrowed_subrange_t<_Range>
430 operator()(_Range&& __r, range_difference_t<_Range> __count,
431 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
432 {
433 return (*this)(ranges::begin(__r), ranges::end(__r),
434 std::move(__count), __value,
435 std::move(__pred), std::move(__proj));
436 }
437 };
438
439 inline constexpr __search_n_fn search_n{};
440
441 struct __find_end_fn
442 {
443 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
444 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
445 typename _Pred = ranges::equal_to,
446 typename _Proj1 = identity, typename _Proj2 = identity>
447 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
448 constexpr subrange<_Iter1>
449 operator()(_Iter1 __first1, _Sent1 __last1,
450 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
451 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
452 {
453 if constexpr (bidirectional_iterator<_Iter1>
454 && bidirectional_iterator<_Iter2>)
455 {
456 auto __i1 = ranges::next(__first1, __last1);
457 auto __i2 = ranges::next(__first2, __last2);
458 auto __rresult
459 = ranges::search(reverse_iterator<_Iter1>{__i1},
460 reverse_iterator<_Iter1>{__first1},
461 reverse_iterator<_Iter2>{__i2},
462 reverse_iterator<_Iter2>{__first2},
463 std::move(__pred),
464 std::move(__proj1), std::move(__proj2));
465 auto __result_first = ranges::end(__rresult).base();
466 auto __result_last = ranges::begin(__rresult).base();
467 if (__result_last == __first1)
468 return {__i1, __i1};
469 else
470 return {__result_first, __result_last};
471 }
472 else
473 {
474 auto __i = ranges::next(__first1, __last1);
475 if (__first2 == __last2)
476 return {__i, __i};
477
478 auto __result_begin = __i;
479 auto __result_end = __i;
480 for (;;)
481 {
482 auto __new_range = ranges::search(__first1, __last1,
483 __first2, __last2,
484 __pred, __proj1, __proj2);
485 auto __new_result_begin = ranges::begin(__new_range);
486 auto __new_result_end = ranges::end(__new_range);
487 if (__new_result_begin == __last1)
488 return {__result_begin, __result_end};
489 else
490 {
491 __result_begin = __new_result_begin;
492 __result_end = __new_result_end;
493 __first1 = __result_begin;
494 ++__first1;
495 }
496 }
497 }
498 }
499
500 template<forward_range _Range1, forward_range _Range2,
501 typename _Pred = ranges::equal_to,
502 typename _Proj1 = identity, typename _Proj2 = identity>
503 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
504 _Pred, _Proj1, _Proj2>
505 constexpr borrowed_subrange_t<_Range1>
506 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
507 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
508 {
509 return (*this)(ranges::begin(__r1), ranges::end(__r1),
510 ranges::begin(__r2), ranges::end(__r2),
511 std::move(__pred),
512 std::move(__proj1), std::move(__proj2));
513 }
514 };
515
516 inline constexpr __find_end_fn find_end{};
517
518 // adjacent_find is defined in <bits/ranges_util.h>.
519
520 struct __is_permutation_fn
521 {
522 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
523 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
524 typename _Proj1 = identity, typename _Proj2 = identity,
525 indirect_equivalence_relation<projected<_Iter1, _Proj1>,
526 projected<_Iter2, _Proj2>> _Pred
527 = ranges::equal_to>
528 constexpr bool
529 operator()(_Iter1 __first1, _Sent1 __last1,
530 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
531 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
532 {
533 constexpr bool __sized_iters
534 = (sized_sentinel_for<_Sent1, _Iter1>
535 && sized_sentinel_for<_Sent2, _Iter2>);
536 if constexpr (__sized_iters)
537 {
538 auto __d1 = ranges::distance(__first1, __last1);
539 auto __d2 = ranges::distance(__first2, __last2);
540 if (__d1 != __d2)
541 return false;
542 }
543
544 // Efficiently compare identical prefixes: O(N) if sequences
545 // have the same elements in the same order.
546 for (; __first1 != __last1 && __first2 != __last2;
547 ++__first1, (void)++__first2)
548 if (!(bool)std::__invoke(__pred,
549 std::__invoke(__proj1, *__first1),
550 std::__invoke(__proj2, *__first2)))
551 break;
552
553 if constexpr (__sized_iters)
554 {
555 if (__first1 == __last1)
556 return true;
557 }
558 else
559 {
560 auto __d1 = ranges::distance(__first1, __last1);
561 auto __d2 = ranges::distance(__first2, __last2);
562 if (__d1 == 0 && __d2 == 0)
563 return true;
564 if (__d1 != __d2)
565 return false;
566 }
567
568 for (auto __scan = __first1; __scan != __last1; ++__scan)
569 {
570 auto&& __scan_deref = *__scan;
571 auto&& __proj_scan =
572 std::__invoke(__proj1, std::forward<decltype(__scan_deref)>(__scan_deref));
573 auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) -> bool {
574 return std::__invoke(__pred,
575 std::forward<decltype(__proj_scan)>(__proj_scan),
576 std::forward<_Tp>(__arg));
577 };
578 if (__scan != ranges::find_if(__first1, __scan,
579 __comp_scan, __proj1))
580 continue; // We've seen this one before.
581
582 auto __matches = ranges::count_if(__first2, __last2,
583 __comp_scan, __proj2);
584 if (__matches == 0
585 || ranges::count_if(__scan, __last1,
586 __comp_scan, __proj1) != __matches)
587 return false;
588 }
589 return true;
590 }
591
592 template<forward_range _Range1, forward_range _Range2,
593 typename _Proj1 = identity, typename _Proj2 = identity,
594 indirect_equivalence_relation<
595 projected<iterator_t<_Range1>, _Proj1>,
596 projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
597 constexpr bool
598 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
599 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
600 {
601 // _GLIBCXX_RESOLVE_LIB_DEFECTS
602 // 3560. ranges::is_permutation should short-circuit for sized_ranges
603 if constexpr (sized_range<_Range1>)
604 if constexpr (sized_range<_Range2>)
605 if (ranges::distance(__r1) != ranges::distance(__r2))
606 return false;
607
608 return (*this)(ranges::begin(__r1), ranges::end(__r1),
609 ranges::begin(__r2), ranges::end(__r2),
610 std::move(__pred),
611 std::move(__proj1), std::move(__proj2));
612 }
613 };
614
615 inline constexpr __is_permutation_fn is_permutation{};
616
617 template<typename _Iter, typename _Out>
618 using copy_if_result = in_out_result<_Iter, _Out>;
619
620 struct __copy_if_fn
621 {
622 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
623 weakly_incrementable _Out, typename _Proj = identity,
624 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
625 requires indirectly_copyable<_Iter, _Out>
626 constexpr copy_if_result<_Iter, _Out>
627 operator()(_Iter __first, _Sent __last, _Out __result,
628 _Pred __pred, _Proj __proj = {}) const
629 {
630 for (; __first != __last; ++__first)
631 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
632 {
633 *__result = *__first;
634 ++__result;
635 }
636 return {std::move(__first), std::move(__result)};
637 }
638
639 template<input_range _Range, weakly_incrementable _Out,
640 typename _Proj = identity,
641 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
642 _Pred>
643 requires indirectly_copyable<iterator_t<_Range>, _Out>
644 constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
645 operator()(_Range&& __r, _Out __result,
646 _Pred __pred, _Proj __proj = {}) const
647 {
648 return (*this)(ranges::begin(__r), ranges::end(__r),
649 std::move(__result),
650 std::move(__pred), std::move(__proj));
651 }
652 };
653
654 inline constexpr __copy_if_fn copy_if{};
655
656 template<typename _Iter1, typename _Iter2>
657 using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
658
659 struct __swap_ranges_fn
660 {
661 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
662 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
663 requires indirectly_swappable<_Iter1, _Iter2>
664 constexpr swap_ranges_result<_Iter1, _Iter2>
665 operator()(_Iter1 __first1, _Sent1 __last1,
666 _Iter2 __first2, _Sent2 __last2) const
667 {
668 for (; __first1 != __last1 && __first2 != __last2;
669 ++__first1, (void)++__first2)
670 ranges::iter_swap(__first1, __first2);
671 return {std::move(__first1), std::move(__first2)};
672 }
673
674 template<input_range _Range1, input_range _Range2>
675 requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
676 constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
677 borrowed_iterator_t<_Range2>>
678 operator()(_Range1&& __r1, _Range2&& __r2) const
679 {
680 return (*this)(ranges::begin(__r1), ranges::end(__r1),
681 ranges::begin(__r2), ranges::end(__r2));
682 }
683 };
684
685 inline constexpr __swap_ranges_fn swap_ranges{};
686
687 template<typename _Iter, typename _Out>
688 using unary_transform_result = in_out_result<_Iter, _Out>;
689
690 template<typename _Iter1, typename _Iter2, typename _Out>
691 struct in_in_out_result
692 {
693 [[no_unique_address]] _Iter1 in1;
694 [[no_unique_address]] _Iter2 in2;
695 [[no_unique_address]] _Out out;
696
697 template<typename _IIter1, typename _IIter2, typename _OOut>
698 requires convertible_to<const _Iter1&, _IIter1>
699 && convertible_to<const _Iter2&, _IIter2>
700 && convertible_to<const _Out&, _OOut>
701 constexpr
702 operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
703 { return {in1, in2, out}; }
704
705 template<typename _IIter1, typename _IIter2, typename _OOut>
706 requires convertible_to<_Iter1, _IIter1>
707 && convertible_to<_Iter2, _IIter2>
708 && convertible_to<_Out, _OOut>
709 constexpr
710 operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
711 { return {std::move(in1), std::move(in2), std::move(out)}; }
712 };
713
714 template<typename _Iter1, typename _Iter2, typename _Out>
715 using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
716
717 struct __transform_fn
718 {
719 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
720 weakly_incrementable _Out,
721 copy_constructible _Fp, typename _Proj = identity>
722 requires indirectly_writable<_Out,
723 indirect_result_t<_Fp&,
724 projected<_Iter, _Proj>>>
725 constexpr unary_transform_result<_Iter, _Out>
726 operator()(_Iter __first1, _Sent __last1, _Out __result,
727 _Fp __op, _Proj __proj = {}) const
728 {
729 for (; __first1 != __last1; ++__first1, (void)++__result)
730 *__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
731 return {std::move(__first1), std::move(__result)};
732 }
733
734 template<input_range _Range, weakly_incrementable _Out,
735 copy_constructible _Fp, typename _Proj = identity>
736 requires indirectly_writable<_Out,
737 indirect_result_t<_Fp&,
738 projected<iterator_t<_Range>, _Proj>>>
739 constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
740 operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {}) const
741 {
742 return (*this)(ranges::begin(__r), ranges::end(__r),
743 std::move(__result),
744 std::move(__op), std::move(__proj));
745 }
746
747 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
748 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
749 weakly_incrementable _Out, copy_constructible _Fp,
750 typename _Proj1 = identity, typename _Proj2 = identity>
751 requires indirectly_writable<_Out,
752 indirect_result_t<_Fp&,
753 projected<_Iter1, _Proj1>,
754 projected<_Iter2, _Proj2>>>
755 constexpr binary_transform_result<_Iter1, _Iter2, _Out>
756 operator()(_Iter1 __first1, _Sent1 __last1,
757 _Iter2 __first2, _Sent2 __last2,
758 _Out __result, _Fp __binary_op,
759 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
760 {
761 for (; __first1 != __last1 && __first2 != __last2;
762 ++__first1, (void)++__first2, ++__result)
763 *__result = std::__invoke(__binary_op,
764 std::__invoke(__proj1, *__first1),
765 std::__invoke(__proj2, *__first2));
766 return {std::move(__first1), std::move(__first2), std::move(__result)};
767 }
768
769 template<input_range _Range1, input_range _Range2,
770 weakly_incrementable _Out, copy_constructible _Fp,
771 typename _Proj1 = identity, typename _Proj2 = identity>
772 requires indirectly_writable<_Out,
773 indirect_result_t<_Fp&,
774 projected<iterator_t<_Range1>, _Proj1>,
775 projected<iterator_t<_Range2>, _Proj2>>>
776 constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
777 borrowed_iterator_t<_Range2>, _Out>
778 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
779 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
780 {
781 return (*this)(ranges::begin(__r1), ranges::end(__r1),
782 ranges::begin(__r2), ranges::end(__r2),
783 std::move(__result), std::move(__binary_op),
784 std::move(__proj1), std::move(__proj2));
785 }
786 };
787
788 inline constexpr __transform_fn transform{};
789
790 struct __replace_fn
791 {
792 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
793 typename _Proj = identity,
794 typename _Tp1 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
795 typename _Tp2 _GLIBCXX26_DEF_VAL_T(_Tp1)>
796 requires indirectly_writable<_Iter, const _Tp2&>
797 && indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
798 const _Tp1*>
799 constexpr _Iter
800 operator()(_Iter __first, _Sent __last,
801 const _Tp1& __old_value, const _Tp2& __new_value,
802 _Proj __proj = {}) const
803 {
804 for (; __first != __last; ++__first)
805 if (std::__invoke(__proj, *__first) == __old_value)
806 *__first = __new_value;
807 return __first;
808 }
809
810 template<input_range _Range, typename _Proj = identity,
811 typename _Tp1
812 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
813 typename _Tp2 _GLIBCXX26_DEF_VAL_T(_Tp1)>
814 requires indirectly_writable<iterator_t<_Range>, const _Tp2&>
815 && indirect_binary_predicate<ranges::equal_to,
816 projected<iterator_t<_Range>, _Proj>,
817 const _Tp1*>
818 constexpr borrowed_iterator_t<_Range>
819 operator()(_Range&& __r,
820 const _Tp1& __old_value, const _Tp2& __new_value,
821 _Proj __proj = {}) const
822 {
823 return (*this)(ranges::begin(__r), ranges::end(__r),
824 __old_value, __new_value, std::move(__proj));
825 }
826 };
827
828 inline constexpr __replace_fn replace{};
829
830 struct __replace_if_fn
831 {
832 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
833 typename _Proj = identity,
834 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
835 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
836 requires indirectly_writable<_Iter, const _Tp&>
837 constexpr _Iter
838 operator()(_Iter __first, _Sent __last,
839 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
840 {
841 for (; __first != __last; ++__first)
842 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
843 *__first = __new_value;
844 return std::move(__first);
845 }
846
847 template<input_range _Range, typename _Proj = identity,
848 typename _Tp
849 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
850 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
851 _Pred>
852 requires indirectly_writable<iterator_t<_Range>, const _Tp&>
853 constexpr borrowed_iterator_t<_Range>
854 operator()(_Range&& __r,
855 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
856 {
857 return (*this)(ranges::begin(__r), ranges::end(__r),
858 std::move(__pred), __new_value, std::move(__proj));
859 }
860 };
861
862 inline constexpr __replace_if_fn replace_if{};
863
864 template<typename _Iter, typename _Out>
865 using replace_copy_result = in_out_result<_Iter, _Out>;
866
867 struct __replace_copy_fn
868 {
869 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
870 typename _Out, typename _Proj = identity,
871 typename _Tp1 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
872 typename _Tp2 _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
873 requires indirectly_copyable<_Iter, _Out>
874 && indirect_binary_predicate<ranges::equal_to,
875 projected<_Iter, _Proj>, const _Tp1*>
876 && output_iterator<_Out, const _Tp2&>
877 constexpr replace_copy_result<_Iter, _Out>
878 operator()(_Iter __first, _Sent __last, _Out __result,
879 const _Tp1& __old_value, const _Tp2& __new_value,
880 _Proj __proj = {}) const
881 {
882 for (; __first != __last; ++__first, (void)++__result)
883 if (std::__invoke(__proj, *__first) == __old_value)
884 *__result = __new_value;
885 else
886 *__result = *__first;
887 return {std::move(__first), std::move(__result)};
888 }
889
890 template<input_range _Range, typename _Out,
891 typename _Proj = identity,
892 typename _Tp1
893 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
894 typename _Tp2 _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
895 requires indirectly_copyable<iterator_t<_Range>, _Out>
896 && indirect_binary_predicate<ranges::equal_to,
897 projected<iterator_t<_Range>, _Proj>,
898 const _Tp1*>
899 && output_iterator<_Out, const _Tp2&>
900 constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
901 operator()(_Range&& __r, _Out __result,
902 const _Tp1& __old_value, const _Tp2& __new_value,
903 _Proj __proj = {}) const
904 {
905 return (*this)(ranges::begin(__r), ranges::end(__r),
906 std::move(__result), __old_value,
907 __new_value, std::move(__proj));
908 }
909 };
910
911 inline constexpr __replace_copy_fn replace_copy{};
912
913 template<typename _Iter, typename _Out>
914 using replace_copy_if_result = in_out_result<_Iter, _Out>;
915
916 struct __replace_copy_if_fn
917 {
918 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
919 typename _Out,
920 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>),
921 typename _Proj = identity,
922 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
923 requires indirectly_copyable<_Iter, _Out>
924 && output_iterator<_Out, const _Tp&>
925 constexpr replace_copy_if_result<_Iter, _Out>
926 operator()(_Iter __first, _Sent __last, _Out __result,
927 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
928 {
929 for (; __first != __last; ++__first, (void)++__result)
930 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
931 *__result = __new_value;
932 else
933 *__result = *__first;
934 return {std::move(__first), std::move(__result)};
935 }
936
937 template<input_range _Range,
938 typename _Out,
939 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>),
940 typename _Proj = identity,
941 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
942 _Pred>
943 requires indirectly_copyable<iterator_t<_Range>, _Out>
944 && output_iterator<_Out, const _Tp&>
945 constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
946 operator()(_Range&& __r, _Out __result,
947 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
948 {
949 return (*this)(ranges::begin(__r), ranges::end(__r),
950 std::move(__result), std::move(__pred),
951 __new_value, std::move(__proj));
952 }
953 };
954
955 inline constexpr __replace_copy_if_fn replace_copy_if{};
956
957 struct __generate_n_fn
958 {
959 template<input_or_output_iterator _Out, copy_constructible _Fp>
960 requires invocable<_Fp&>
961 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
962 constexpr _Out
963 operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen) const
964 {
965 for (; __n > 0; --__n, (void)++__first)
966 *__first = std::__invoke(__gen);
967 return __first;
968 }
969 };
970
971 inline constexpr __generate_n_fn generate_n{};
972
973 struct __generate_fn
974 {
975 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
976 copy_constructible _Fp>
977 requires invocable<_Fp&>
978 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
979 constexpr _Out
980 operator()(_Out __first, _Sent __last, _Fp __gen) const
981 {
982 for (; __first != __last; ++__first)
983 *__first = std::__invoke(__gen);
984 return __first;
985 }
986
987 template<typename _Range, copy_constructible _Fp>
988 requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
989 constexpr borrowed_iterator_t<_Range>
990 operator()(_Range&& __r, _Fp __gen) const
991 {
992 return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__gen));
993 }
994 };
995
996 inline constexpr __generate_fn generate{};
997
998 struct __remove_if_fn
999 {
1000 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1001 typename _Proj = identity,
1002 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1003 constexpr subrange<_Iter>
1004 operator()(_Iter __first, _Sent __last,
1005 _Pred __pred, _Proj __proj = {}) const
1006 {
1007 __first = ranges::find_if(__first, __last, __pred, __proj);
1008 if (__first == __last)
1009 return {__first, __first};
1010
1011 auto __result = __first;
1012 ++__first;
1013 for (; __first != __last; ++__first)
1014 if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
1015 {
1016 *__result = std::move(*__first);
1017 ++__result;
1018 }
1019
1020 return {__result, __first};
1021 }
1022
1023 template<forward_range _Range, typename _Proj = identity,
1024 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1025 _Pred>
1026 requires permutable<iterator_t<_Range>>
1027 constexpr borrowed_subrange_t<_Range>
1028 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
1029 {
1030 return (*this)(ranges::begin(__r), ranges::end(__r),
1031 std::move(__pred), std::move(__proj));
1032 }
1033 };
1034
1035 inline constexpr __remove_if_fn remove_if{};
1036
1037 struct __remove_fn
1038 {
1039 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1040 typename _Proj = identity,
1041 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
1042 requires indirect_binary_predicate<ranges::equal_to,
1043 projected<_Iter, _Proj>,
1044 const _Tp*>
1045 constexpr subrange<_Iter>
1046 operator()(_Iter __first, _Sent __last,
1047 const _Tp& __value, _Proj __proj = {}) const
1048 {
1049 auto __pred = [&] (auto&& __arg) -> bool {
1050 return std::forward<decltype(__arg)>(__arg) == __value;
1051 };
1052 return ranges::remove_if(__first, __last,
1053 std::move(__pred), std::move(__proj));
1054 }
1055
1056 template<forward_range _Range, typename _Proj = identity,
1057 typename _Tp
1058 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
1059 requires permutable<iterator_t<_Range>>
1060 && indirect_binary_predicate<ranges::equal_to,
1061 projected<iterator_t<_Range>, _Proj>,
1062 const _Tp*>
1063 constexpr borrowed_subrange_t<_Range>
1064 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
1065 {
1066 return (*this)(ranges::begin(__r), ranges::end(__r),
1067 __value, std::move(__proj));
1068 }
1069 };
1070
1071 inline constexpr __remove_fn remove{};
1072
1073 template<typename _Iter, typename _Out>
1074 using remove_copy_if_result = in_out_result<_Iter, _Out>;
1075
1076 struct __remove_copy_if_fn
1077 {
1078 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1079 weakly_incrementable _Out, typename _Proj = identity,
1080 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1081 requires indirectly_copyable<_Iter, _Out>
1082 constexpr remove_copy_if_result<_Iter, _Out>
1083 operator()(_Iter __first, _Sent __last, _Out __result,
1084 _Pred __pred, _Proj __proj = {}) const
1085 {
1086 for (; __first != __last; ++__first)
1087 if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
1088 {
1089 *__result = *__first;
1090 ++__result;
1091 }
1092 return {std::move(__first), std::move(__result)};
1093 }
1094
1095 template<input_range _Range, weakly_incrementable _Out,
1096 typename _Proj = identity,
1097 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1098 _Pred>
1099 requires indirectly_copyable<iterator_t<_Range>, _Out>
1100 constexpr remove_copy_if_result<borrowed_iterator_t<_Range>, _Out>
1101 operator()(_Range&& __r, _Out __result,
1102 _Pred __pred, _Proj __proj = {}) const
1103 {
1104 return (*this)(ranges::begin(__r), ranges::end(__r),
1105 std::move(__result),
1106 std::move(__pred), std::move(__proj));
1107 }
1108 };
1109
1110 inline constexpr __remove_copy_if_fn remove_copy_if{};
1111
1112 template<typename _Iter, typename _Out>
1113 using remove_copy_result = in_out_result<_Iter, _Out>;
1114
1115 struct __remove_copy_fn
1116 {
1117 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1118 weakly_incrementable _Out, typename _Proj = identity,
1119 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
1120 requires indirectly_copyable<_Iter, _Out>
1121 && indirect_binary_predicate<ranges::equal_to,
1122 projected<_Iter, _Proj>,
1123 const _Tp*>
1124 constexpr remove_copy_result<_Iter, _Out>
1125 operator()(_Iter __first, _Sent __last, _Out __result,
1126 const _Tp& __value, _Proj __proj = {}) const
1127 {
1128 for (; __first != __last; ++__first)
1129 if (!(std::__invoke(__proj, *__first) == __value))
1130 {
1131 *__result = *__first;
1132 ++__result;
1133 }
1134 return {std::move(__first), std::move(__result)};
1135 }
1136
1137 template<input_range _Range, weakly_incrementable _Out,
1138 typename _Proj = identity,
1139 typename _Tp
1140 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
1141 requires indirectly_copyable<iterator_t<_Range>, _Out>
1142 && indirect_binary_predicate<ranges::equal_to,
1143 projected<iterator_t<_Range>, _Proj>,
1144 const _Tp*>
1145 constexpr remove_copy_result<borrowed_iterator_t<_Range>, _Out>
1146 operator()(_Range&& __r, _Out __result,
1147 const _Tp& __value, _Proj __proj = {}) const
1148 {
1149 return (*this)(ranges::begin(__r), ranges::end(__r),
1150 std::move(__result), __value, std::move(__proj));
1151 }
1152 };
1153
1154 inline constexpr __remove_copy_fn remove_copy{};
1155
1156 struct __unique_fn
1157 {
1158 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1159 typename _Proj = identity,
1160 indirect_equivalence_relation<
1161 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1162 constexpr subrange<_Iter>
1163 operator()(_Iter __first, _Sent __last,
1164 _Comp __comp = {}, _Proj __proj = {}) const
1165 {
1166 __first = ranges::adjacent_find(__first, __last, __comp, __proj);
1167 if (__first == __last)
1168 return {__first, __first};
1169
1170 auto __dest = __first;
1171 ++__first;
1172 while (++__first != __last)
1173 if (!std::__invoke(__comp,
1174 std::__invoke(__proj, *__dest),
1175 std::__invoke(__proj, *__first)))
1176 *++__dest = std::move(*__first);
1177 return {++__dest, __first};
1178 }
1179
1180 template<forward_range _Range, typename _Proj = identity,
1181 indirect_equivalence_relation<
1182 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1183 requires permutable<iterator_t<_Range>>
1184 constexpr borrowed_subrange_t<_Range>
1185 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1186 {
1187 return (*this)(ranges::begin(__r), ranges::end(__r),
1188 std::move(__comp), std::move(__proj));
1189 }
1190 };
1191
1192 inline constexpr __unique_fn unique{};
1193
1194 namespace __detail
1195 {
1196 template<typename _Out, typename _Tp>
1197 concept __can_reread_output = input_iterator<_Out>
1198 && same_as<_Tp, iter_value_t<_Out>>;
1199 }
1200
1201 template<typename _Iter, typename _Out>
1202 using unique_copy_result = in_out_result<_Iter, _Out>;
1203
1204 struct __unique_copy_fn
1205 {
1206 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1207 weakly_incrementable _Out, typename _Proj = identity,
1208 indirect_equivalence_relation<
1209 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1210 requires indirectly_copyable<_Iter, _Out>
1211 && (forward_iterator<_Iter>
1212 || __detail::__can_reread_output<_Out, iter_value_t<_Iter>>
1213 || indirectly_copyable_storable<_Iter, _Out>)
1214 constexpr unique_copy_result<_Iter, _Out>
1215 operator()(_Iter __first, _Sent __last, _Out __result,
1216 _Comp __comp = {}, _Proj __proj = {}) const
1217 {
1218 if (__first == __last)
1219 return {std::move(__first), std::move(__result)};
1220
1221 // TODO: perform a closer comparison with reference implementations
1222 if constexpr (forward_iterator<_Iter>)
1223 {
1224 auto __next = __first;
1225 *__result = *__next;
1226 while (++__next != __last)
1227 if (!std::__invoke(__comp,
1228 std::__invoke(__proj, *__first),
1229 std::__invoke(__proj, *__next)))
1230 {
1231 __first = __next;
1232 *++__result = *__first;
1233 }
1234 return {__next, std::move(++__result)};
1235 }
1236 else if constexpr (__detail::__can_reread_output<_Out, iter_value_t<_Iter>>)
1237 {
1238 *__result = *__first;
1239 while (++__first != __last)
1240 if (!std::__invoke(__comp,
1241 std::__invoke(__proj, *__result),
1242 std::__invoke(__proj, *__first)))
1243 *++__result = *__first;
1244 return {std::move(__first), std::move(++__result)};
1245 }
1246 else // indirectly_copyable_storable<_Iter, _Out>
1247 {
1248 auto __value = *__first;
1249 *__result = __value;
1250 while (++__first != __last)
1251 {
1252 if (!(bool)std::__invoke(__comp,
1253 std::__invoke(__proj, *__first),
1254 std::__invoke(__proj, __value)))
1255 {
1256 __value = *__first;
1257 *++__result = __value;
1258 }
1259 }
1260 return {std::move(__first), std::move(++__result)};
1261 }
1262 }
1263
1264 template<input_range _Range,
1265 weakly_incrementable _Out, typename _Proj = identity,
1266 indirect_equivalence_relation<
1267 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1268 requires indirectly_copyable<iterator_t<_Range>, _Out>
1269 && (forward_iterator<iterator_t<_Range>>
1270 || __detail::__can_reread_output<_Out, range_value_t<_Range>>
1271 || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
1272 constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
1273 operator()(_Range&& __r, _Out __result,
1274 _Comp __comp = {}, _Proj __proj = {}) const
1275 {
1276 return (*this)(ranges::begin(__r), ranges::end(__r),
1277 std::move(__result),
1278 std::move(__comp), std::move(__proj));
1279 }
1280 };
1281
1282 inline constexpr __unique_copy_fn unique_copy{};
1283
1284 struct __reverse_fn
1285 {
1286 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
1287 requires permutable<_Iter>
1288 constexpr _Iter
1289 operator()(_Iter __first, _Sent __last) const
1290 {
1291 auto __i = ranges::next(__first, __last);
1292 auto __tail = __i;
1293
1294 if constexpr (random_access_iterator<_Iter>)
1295 {
1296 if (__first != __last)
1297 {
1298 --__tail;
1299 while (__first < __tail)
1300 {
1301 ranges::iter_swap(__first, __tail);
1302 ++__first;
1303 --__tail;
1304 }
1305 }
1306 return __i;
1307 }
1308 else
1309 {
1310 for (;;)
1311 if (__first == __tail || __first == --__tail)
1312 break;
1313 else
1314 {
1315 ranges::iter_swap(__first, __tail);
1316 ++__first;
1317 }
1318 return __i;
1319 }
1320 }
1321
1322 template<bidirectional_range _Range>
1323 requires permutable<iterator_t<_Range>>
1324 constexpr borrowed_iterator_t<_Range>
1325 operator()(_Range&& __r) const
1326 {
1327 return (*this)(ranges::begin(__r), ranges::end(__r));
1328 }
1329 };
1330
1331 inline constexpr __reverse_fn reverse{};
1332
1333 template<typename _Iter, typename _Out>
1334 using reverse_copy_result = in_out_result<_Iter, _Out>;
1335
1336 struct __reverse_copy_fn
1337 {
1338 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
1339 weakly_incrementable _Out>
1340 requires indirectly_copyable<_Iter, _Out>
1341 constexpr reverse_copy_result<_Iter, _Out>
1342 operator()(_Iter __first, _Sent __last, _Out __result) const
1343 {
1344 auto __i = ranges::next(__first, __last);
1345 auto __tail = __i;
1346 while (__first != __tail)
1347 {
1348 --__tail;
1349 *__result = *__tail;
1350 ++__result;
1351 }
1352 return {__i, std::move(__result)};
1353 }
1354
1355 template<bidirectional_range _Range, weakly_incrementable _Out>
1356 requires indirectly_copyable<iterator_t<_Range>, _Out>
1357 constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
1358 operator()(_Range&& __r, _Out __result) const
1359 {
1360 return (*this)(ranges::begin(__r), ranges::end(__r),
1361 std::move(__result));
1362 }
1363 };
1364
1365 inline constexpr __reverse_copy_fn reverse_copy{};
1366
1367 struct __rotate_fn
1368 {
1369 template<permutable _Iter, sentinel_for<_Iter> _Sent>
1370 constexpr subrange<_Iter>
1371 operator()(_Iter __first, _Iter __middle, _Sent __last) const
1372 {
1373 auto __lasti = ranges::next(__first, __last);
1374 if (__first == __middle)
1375 return {__lasti, __lasti};
1376 if (__last == __middle)
1377 return {std::move(__first), std::move(__lasti)};
1378
1379 if constexpr (random_access_iterator<_Iter>)
1380 {
1381 auto __n = __lasti - __first;
1382 auto __k = __middle - __first;
1383
1384 if (__k == __n - __k)
1385 {
1386 ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
1387 return {std::move(__middle), std::move(__lasti)};
1388 }
1389
1390 auto __p = __first;
1391 auto __ret = __first + (__lasti - __middle);
1392
1393 for (;;)
1394 {
1395 if (__k < __n - __k)
1396 {
1397 // TODO: is_pod is deprecated, but this condition is
1398 // consistent with the STL implementation.
1399 if constexpr (__is_pod(iter_value_t<_Iter>))
1400 if (__k == 1)
1401 {
1402 auto __t = std::move(*__p);
1403 ranges::move(__p + 1, __p + __n, __p);
1404 *(__p + __n - 1) = std::move(__t);
1405 return {std::move(__ret), std::move(__lasti)};
1406 }
1407 auto __q = __p + __k;
1408 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1409 {
1410 ranges::iter_swap(__p, __q);
1411 ++__p;
1412 ++__q;
1413 }
1414 __n %= __k;
1415 if (__n == 0)
1416 return {std::move(__ret), std::move(__lasti)};
1417 ranges::swap(__n, __k);
1418 __k = __n - __k;
1419 }
1420 else
1421 {
1422 __k = __n - __k;
1423 // TODO: is_pod is deprecated, but this condition is
1424 // consistent with the STL implementation.
1425 if constexpr (__is_pod(iter_value_t<_Iter>))
1426 if (__k == 1)
1427 {
1428 auto __t = std::move(*(__p + __n - 1));
1429 ranges::move_backward(__p, __p + __n - 1, __p + __n);
1430 *__p = std::move(__t);
1431 return {std::move(__ret), std::move(__lasti)};
1432 }
1433 auto __q = __p + __n;
1434 __p = __q - __k;
1435 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1436 {
1437 --__p;
1438 --__q;
1439 ranges::iter_swap(__p, __q);
1440 }
1441 __n %= __k;
1442 if (__n == 0)
1443 return {std::move(__ret), std::move(__lasti)};
1444 std::swap(__n, __k);
1445 }
1446 }
1447 }
1448 else if constexpr (bidirectional_iterator<_Iter>)
1449 {
1450 auto __tail = __lasti;
1451
1452 ranges::reverse(__first, __middle);
1453 ranges::reverse(__middle, __tail);
1454
1455 while (__first != __middle && __middle != __tail)
1456 {
1457 ranges::iter_swap(__first, --__tail);
1458 ++__first;
1459 }
1460
1461 if (__first == __middle)
1462 {
1463 ranges::reverse(__middle, __tail);
1464 return {std::move(__tail), std::move(__lasti)};
1465 }
1466 else
1467 {
1468 ranges::reverse(__first, __middle);
1469 return {std::move(__first), std::move(__lasti)};
1470 }
1471 }
1472 else
1473 {
1474 auto __first2 = __middle;
1475 do
1476 {
1477 ranges::iter_swap(__first, __first2);
1478 ++__first;
1479 ++__first2;
1480 if (__first == __middle)
1481 __middle = __first2;
1482 } while (__first2 != __last);
1483
1484 auto __ret = __first;
1485
1486 __first2 = __middle;
1487
1488 while (__first2 != __last)
1489 {
1490 ranges::iter_swap(__first, __first2);
1491 ++__first;
1492 ++__first2;
1493 if (__first == __middle)
1494 __middle = __first2;
1495 else if (__first2 == __last)
1496 __first2 = __middle;
1497 }
1498 return {std::move(__ret), std::move(__lasti)};
1499 }
1500 }
1501
1502 template<forward_range _Range>
1503 requires permutable<iterator_t<_Range>>
1504 constexpr borrowed_subrange_t<_Range>
1505 operator()(_Range&& __r, iterator_t<_Range> __middle) const
1506 {
1507 return (*this)(ranges::begin(__r), std::move(__middle),
1508 ranges::end(__r));
1509 }
1510 };
1511
1512 inline constexpr __rotate_fn rotate{};
1513
1514 template<typename _Iter, typename _Out>
1515 using rotate_copy_result = in_out_result<_Iter, _Out>;
1516
1517 struct __rotate_copy_fn
1518 {
1519 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1520 weakly_incrementable _Out>
1521 requires indirectly_copyable<_Iter, _Out>
1522 constexpr rotate_copy_result<_Iter, _Out>
1523 operator()(_Iter __first, _Iter __middle, _Sent __last,
1524 _Out __result) const
1525 {
1526 auto __copy1 = ranges::copy(__middle,
1527 std::move(__last),
1528 std::move(__result));
1529 auto __copy2 = ranges::copy(std::move(__first),
1530 std::move(__middle),
1531 std::move(__copy1.out));
1532 return { std::move(__copy1.in), std::move(__copy2.out) };
1533 }
1534
1535 template<forward_range _Range, weakly_incrementable _Out>
1536 requires indirectly_copyable<iterator_t<_Range>, _Out>
1537 constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
1538 operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
1539 {
1540 return (*this)(ranges::begin(__r), std::move(__middle),
1541 ranges::end(__r), std::move(__result));
1542 }
1543 };
1544
1545 inline constexpr __rotate_copy_fn rotate_copy{};
1546
1547 struct __sample_fn
1548 {
1549 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1550 weakly_incrementable _Out, typename _Gen>
1551 requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
1552 && indirectly_copyable<_Iter, _Out>
1553 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1554 _Out
1555 operator()(_Iter __first, _Sent __last, _Out __out,
1556 iter_difference_t<_Iter> __n, _Gen&& __g) const
1557 {
1558 if constexpr (forward_iterator<_Iter>)
1559 {
1560 // FIXME: Forwarding to std::sample here requires computing __lasti
1561 // which may take linear time.
1562 auto __lasti = ranges::next(__first, __last);
1563 return _GLIBCXX_STD_A::
1564 sample(std::move(__first), std::move(__lasti), std::move(__out),
1565 __n, std::forward<_Gen>(__g));
1566 }
1567 else
1568 {
1569 using __distrib_type
1570 = uniform_int_distribution<iter_difference_t<_Iter>>;
1571 using __param_type = typename __distrib_type::param_type;
1572 __distrib_type __d{};
1573 iter_difference_t<_Iter> __sample_sz = 0;
1574 while (__first != __last && __sample_sz != __n)
1575 {
1576 __out[__sample_sz++] = *__first;
1577 ++__first;
1578 }
1579 for (auto __pop_sz = __sample_sz; __first != __last;
1580 ++__first, (void) ++__pop_sz)
1581 {
1582 const auto __k = __d(__g, __param_type{0, __pop_sz});
1583 if (__k < __n)
1584 __out[__k] = *__first;
1585 }
1586 return __out + __sample_sz;
1587 }
1588 }
1589
1590 template<input_range _Range, weakly_incrementable _Out, typename _Gen>
1591 requires (forward_range<_Range> || random_access_iterator<_Out>)
1592 && indirectly_copyable<iterator_t<_Range>, _Out>
1593 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1594 _Out
1595 operator()(_Range&& __r, _Out __out,
1596 range_difference_t<_Range> __n, _Gen&& __g) const
1597 {
1598 return (*this)(ranges::begin(__r), ranges::end(__r),
1599 std::move(__out), __n,
1600 std::forward<_Gen>(__g));
1601 }
1602 };
1603
1604 inline constexpr __sample_fn sample{};
1605
1606 struct __shuffle_fn
1607 {
1608 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1609 typename _Gen>
1610 requires permutable<_Iter>
1611 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1612 _Iter
1613 operator()(_Iter __first, _Sent __last, _Gen&& __g) const
1614 {
1615 auto __lasti = ranges::next(__first, __last);
1616 std::shuffle(std::move(__first), __lasti, std::forward<_Gen>(__g));
1617 return __lasti;
1618 }
1619
1620 template<random_access_range _Range, typename _Gen>
1621 requires permutable<iterator_t<_Range>>
1622 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1623 borrowed_iterator_t<_Range>
1624 operator()(_Range&& __r, _Gen&& __g) const
1625 {
1626 return (*this)(ranges::begin(__r), ranges::end(__r),
1627 std::forward<_Gen>(__g));
1628 }
1629 };
1630
1631 inline constexpr __shuffle_fn shuffle{};
1632
1633 struct __push_heap_fn
1634 {
1635 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1636 typename _Comp = ranges::less, typename _Proj = identity>
1637 requires sortable<_Iter, _Comp, _Proj>
1638 constexpr _Iter
1639 operator()(_Iter __first, _Sent __last,
1640 _Comp __comp = {}, _Proj __proj = {}) const
1641 {
1642 auto __lasti = ranges::next(__first, __last);
1643 std::push_heap(__first, __lasti,
1644 __detail::__make_comp_proj(__comp, __proj));
1645 return __lasti;
1646 }
1647
1648 template<random_access_range _Range,
1649 typename _Comp = ranges::less, typename _Proj = identity>
1650 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1651 constexpr borrowed_iterator_t<_Range>
1652 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1653 {
1654 return (*this)(ranges::begin(__r), ranges::end(__r),
1655 std::move(__comp), std::move(__proj));
1656 }
1657 };
1658
1659 inline constexpr __push_heap_fn push_heap{};
1660
1661 struct __pop_heap_fn
1662 {
1663 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1664 typename _Comp = ranges::less, typename _Proj = identity>
1665 requires sortable<_Iter, _Comp, _Proj>
1666 constexpr _Iter
1667 operator()(_Iter __first, _Sent __last,
1668 _Comp __comp = {}, _Proj __proj = {}) const
1669 {
1670 auto __lasti = ranges::next(__first, __last);
1671 std::pop_heap(__first, __lasti,
1672 __detail::__make_comp_proj(__comp, __proj));
1673 return __lasti;
1674 }
1675
1676 template<random_access_range _Range,
1677 typename _Comp = ranges::less, typename _Proj = identity>
1678 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1679 constexpr borrowed_iterator_t<_Range>
1680 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1681 {
1682 return (*this)(ranges::begin(__r), ranges::end(__r),
1683 std::move(__comp), std::move(__proj));
1684 }
1685 };
1686
1687 inline constexpr __pop_heap_fn pop_heap{};
1688
1689 struct __make_heap_fn
1690 {
1691 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1692 typename _Comp = ranges::less, typename _Proj = identity>
1693 requires sortable<_Iter, _Comp, _Proj>
1694 constexpr _Iter
1695 operator()(_Iter __first, _Sent __last,
1696 _Comp __comp = {}, _Proj __proj = {}) const
1697 {
1698 auto __lasti = ranges::next(__first, __last);
1699 std::make_heap(__first, __lasti,
1700 __detail::__make_comp_proj(__comp, __proj));
1701 return __lasti;
1702 }
1703
1704 template<random_access_range _Range,
1705 typename _Comp = ranges::less, typename _Proj = identity>
1706 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1707 constexpr borrowed_iterator_t<_Range>
1708 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1709 {
1710 return (*this)(ranges::begin(__r), ranges::end(__r),
1711 std::move(__comp), std::move(__proj));
1712 }
1713 };
1714
1715 inline constexpr __make_heap_fn make_heap{};
1716
1717 struct __sort_heap_fn
1718 {
1719 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1720 typename _Comp = ranges::less, typename _Proj = identity>
1721 requires sortable<_Iter, _Comp, _Proj>
1722 constexpr _Iter
1723 operator()(_Iter __first, _Sent __last,
1724 _Comp __comp = {}, _Proj __proj = {}) const
1725 {
1726 auto __lasti = ranges::next(__first, __last);
1727 std::sort_heap(__first, __lasti,
1728 __detail::__make_comp_proj(__comp, __proj));
1729 return __lasti;
1730 }
1731
1732 template<random_access_range _Range,
1733 typename _Comp = ranges::less, typename _Proj = identity>
1734 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1735 constexpr borrowed_iterator_t<_Range>
1736 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1737 {
1738 return (*this)(ranges::begin(__r), ranges::end(__r),
1739 std::move(__comp), std::move(__proj));
1740 }
1741 };
1742
1743 inline constexpr __sort_heap_fn sort_heap{};
1744
1745 struct __is_heap_until_fn
1746 {
1747 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1748 typename _Proj = identity,
1749 indirect_strict_weak_order<projected<_Iter, _Proj>>
1750 _Comp = ranges::less>
1751 constexpr _Iter
1752 operator()(_Iter __first, _Sent __last,
1753 _Comp __comp = {}, _Proj __proj = {}) const
1754 {
1755 iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
1756 iter_difference_t<_Iter> __parent = 0, __child = 1;
1757 for (; __child < __n; ++__child)
1758 if (std::__invoke(__comp,
1759 std::__invoke(__proj, *(__first + __parent)),
1760 std::__invoke(__proj, *(__first + __child))))
1761 return __first + __child;
1762 else if ((__child & 1) == 0)
1763 ++__parent;
1764
1765 return __first + __n;
1766 }
1767
1768 template<random_access_range _Range,
1769 typename _Proj = identity,
1770 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1771 _Comp = ranges::less>
1772 constexpr borrowed_iterator_t<_Range>
1773 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1774 {
1775 return (*this)(ranges::begin(__r), ranges::end(__r),
1776 std::move(__comp), std::move(__proj));
1777 }
1778 };
1779
1780 inline constexpr __is_heap_until_fn is_heap_until{};
1781
1782 struct __is_heap_fn
1783 {
1784 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1785 typename _Proj = identity,
1786 indirect_strict_weak_order<projected<_Iter, _Proj>>
1787 _Comp = ranges::less>
1788 constexpr bool
1789 operator()(_Iter __first, _Sent __last,
1790 _Comp __comp = {}, _Proj __proj = {}) const
1791 {
1792 return (__last
1793 == ranges::is_heap_until(__first, __last,
1794 std::move(__comp),
1795 std::move(__proj)));
1796 }
1797
1798 template<random_access_range _Range,
1799 typename _Proj = identity,
1800 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1801 _Comp = ranges::less>
1802 constexpr bool
1803 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1804 {
1805 return (*this)(ranges::begin(__r), ranges::end(__r),
1806 std::move(__comp), std::move(__proj));
1807 }
1808 };
1809
1810 inline constexpr __is_heap_fn is_heap{};
1811
1812 struct __sort_fn
1813 {
1814 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1815 typename _Comp = ranges::less, typename _Proj = identity>
1816 requires sortable<_Iter, _Comp, _Proj>
1817 constexpr _Iter
1818 operator()(_Iter __first, _Sent __last,
1819 _Comp __comp = {}, _Proj __proj = {}) const
1820 {
1821 auto __lasti = ranges::next(__first, __last);
1822 _GLIBCXX_STD_A::sort(std::move(__first), __lasti,
1823 __detail::__make_comp_proj(__comp, __proj));
1824 return __lasti;
1825 }
1826
1827 template<random_access_range _Range,
1828 typename _Comp = ranges::less, typename _Proj = identity>
1829 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1830 constexpr borrowed_iterator_t<_Range>
1831 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1832 {
1833 return (*this)(ranges::begin(__r), ranges::end(__r),
1834 std::move(__comp), std::move(__proj));
1835 }
1836 };
1837
1838 inline constexpr __sort_fn sort{};
1839
1840 struct __stable_sort_fn
1841 {
1842 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1843 typename _Comp = ranges::less, typename _Proj = identity>
1844 requires sortable<_Iter, _Comp, _Proj>
1845 _Iter
1846 operator()(_Iter __first, _Sent __last,
1847 _Comp __comp = {}, _Proj __proj = {}) const
1848 {
1849 auto __lasti = ranges::next(__first, __last);
1850 std::stable_sort(std::move(__first), __lasti,
1851 __detail::__make_comp_proj(__comp, __proj));
1852 return __lasti;
1853 }
1854
1855 template<random_access_range _Range,
1856 typename _Comp = ranges::less, typename _Proj = identity>
1857 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1858 borrowed_iterator_t<_Range>
1859 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1860 {
1861 return (*this)(ranges::begin(__r), ranges::end(__r),
1862 std::move(__comp), std::move(__proj));
1863 }
1864 };
1865
1866 inline constexpr __stable_sort_fn stable_sort{};
1867
1868 struct __partial_sort_fn
1869 {
1870 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1871 typename _Comp = ranges::less, typename _Proj = identity>
1872 requires sortable<_Iter, _Comp, _Proj>
1873 constexpr _Iter
1874 operator()(_Iter __first, _Iter __middle, _Sent __last,
1875 _Comp __comp = {}, _Proj __proj = {}) const
1876 {
1877 if (__first == __middle)
1878 return ranges::next(__first, __last);
1879
1880 ranges::make_heap(__first, __middle, __comp, __proj);
1881 auto __i = __middle;
1882 for (; __i != __last; ++__i)
1883 if (std::__invoke(__comp,
1884 std::__invoke(__proj, *__i),
1885 std::__invoke(__proj, *__first)))
1886 {
1887 ranges::pop_heap(__first, __middle, __comp, __proj);
1888 ranges::iter_swap(__middle-1, __i);
1889 ranges::push_heap(__first, __middle, __comp, __proj);
1890 }
1891 ranges::sort_heap(__first, __middle, __comp, __proj);
1892
1893 return __i;
1894 }
1895
1896 template<random_access_range _Range,
1897 typename _Comp = ranges::less, typename _Proj = identity>
1898 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1899 constexpr borrowed_iterator_t<_Range>
1900 operator()(_Range&& __r, iterator_t<_Range> __middle,
1901 _Comp __comp = {}, _Proj __proj = {}) const
1902 {
1903 return (*this)(ranges::begin(__r), std::move(__middle),
1904 ranges::end(__r),
1905 std::move(__comp), std::move(__proj));
1906 }
1907 };
1908
1909 inline constexpr __partial_sort_fn partial_sort{};
1910
1911 template<typename _Iter, typename _Out>
1912 using partial_sort_copy_result = in_out_result<_Iter, _Out>;
1913
1914 struct __partial_sort_copy_fn
1915 {
1916 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
1917 random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
1918 typename _Comp = ranges::less,
1919 typename _Proj1 = identity, typename _Proj2 = identity>
1920 requires indirectly_copyable<_Iter1, _Iter2>
1921 && sortable<_Iter2, _Comp, _Proj2>
1922 && indirect_strict_weak_order<_Comp,
1923 projected<_Iter1, _Proj1>,
1924 projected<_Iter2, _Proj2>>
1925 constexpr partial_sort_copy_result<_Iter1, _Iter2>
1926 operator()(_Iter1 __first, _Sent1 __last,
1927 _Iter2 __result_first, _Sent2 __result_last,
1928 _Comp __comp = {},
1929 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1930 {
1931 if (__result_first == __result_last)
1932 {
1933 // TODO: Eliminating the variable __lasti triggers an ICE.
1934 auto __lasti = ranges::next(std::move(__first),
1935 std::move(__last));
1936 return {std::move(__lasti), std::move(__result_first)};
1937 }
1938
1939 auto __result_real_last = __result_first;
1940 while (__first != __last && __result_real_last != __result_last)
1941 {
1942 *__result_real_last = *__first;
1943 ++__result_real_last;
1944 ++__first;
1945 }
1946
1947 ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
1948 for (; __first != __last; ++__first)
1949 if (std::__invoke(__comp,
1950 std::__invoke(__proj1, *__first),
1951 std::__invoke(__proj2, *__result_first)))
1952 {
1953 ranges::pop_heap(__result_first, __result_real_last,
1954 __comp, __proj2);
1955 *(__result_real_last-1) = *__first;
1956 ranges::push_heap(__result_first, __result_real_last,
1957 __comp, __proj2);
1958 }
1959 ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
1960
1961 return {std::move(__first), std::move(__result_real_last)};
1962 }
1963
1964 template<input_range _Range1, random_access_range _Range2,
1965 typename _Comp = ranges::less,
1966 typename _Proj1 = identity, typename _Proj2 = identity>
1967 requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
1968 && sortable<iterator_t<_Range2>, _Comp, _Proj2>
1969 && indirect_strict_weak_order<_Comp,
1970 projected<iterator_t<_Range1>, _Proj1>,
1971 projected<iterator_t<_Range2>, _Proj2>>
1972 constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
1973 borrowed_iterator_t<_Range2>>
1974 operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
1975 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1976 {
1977 return (*this)(ranges::begin(__r), ranges::end(__r),
1978 ranges::begin(__out), ranges::end(__out),
1979 std::move(__comp),
1980 std::move(__proj1), std::move(__proj2));
1981 }
1982 };
1983
1984 inline constexpr __partial_sort_copy_fn partial_sort_copy{};
1985
1986 struct __is_sorted_until_fn
1987 {
1988 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1989 typename _Proj = identity,
1990 indirect_strict_weak_order<projected<_Iter, _Proj>>
1991 _Comp = ranges::less>
1992 constexpr _Iter
1993 operator()(_Iter __first, _Sent __last,
1994 _Comp __comp = {}, _Proj __proj = {}) const
1995 {
1996 if (__first == __last)
1997 return __first;
1998
1999 auto __next = __first;
2000 for (++__next; __next != __last; __first = __next, (void)++__next)
2001 if (std::__invoke(__comp,
2002 std::__invoke(__proj, *__next),
2003 std::__invoke(__proj, *__first)))
2004 return __next;
2005 return __next;
2006 }
2007
2008 template<forward_range _Range, typename _Proj = identity,
2009 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2010 _Comp = ranges::less>
2011 constexpr borrowed_iterator_t<_Range>
2012 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2013 {
2014 return (*this)(ranges::begin(__r), ranges::end(__r),
2015 std::move(__comp), std::move(__proj));
2016 }
2017 };
2018
2019 inline constexpr __is_sorted_until_fn is_sorted_until{};
2020
2021 struct __is_sorted_fn
2022 {
2023 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2024 typename _Proj = identity,
2025 indirect_strict_weak_order<projected<_Iter, _Proj>>
2026 _Comp = ranges::less>
2027 constexpr bool
2028 operator()(_Iter __first, _Sent __last,
2029 _Comp __comp = {}, _Proj __proj = {}) const
2030 {
2031 if (__first == __last)
2032 return true;
2033
2034 auto __next = __first;
2035 for (++__next; __next != __last; __first = __next, (void)++__next)
2036 if (std::__invoke(__comp,
2037 std::__invoke(__proj, *__next),
2038 std::__invoke(__proj, *__first)))
2039 return false;
2040 return true;
2041 }
2042
2043 template<forward_range _Range, typename _Proj = identity,
2044 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2045 _Comp = ranges::less>
2046 constexpr bool
2047 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2048 {
2049 return (*this)(ranges::begin(__r), ranges::end(__r),
2050 std::move(__comp), std::move(__proj));
2051 }
2052 };
2053
2054 inline constexpr __is_sorted_fn is_sorted{};
2055
2056 struct __nth_element_fn
2057 {
2058 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2059 typename _Comp = ranges::less, typename _Proj = identity>
2060 requires sortable<_Iter, _Comp, _Proj>
2061 constexpr _Iter
2062 operator()(_Iter __first, _Iter __nth, _Sent __last,
2063 _Comp __comp = {}, _Proj __proj = {}) const
2064 {
2065 auto __lasti = ranges::next(__first, __last);
2066 _GLIBCXX_STD_A::nth_element(std::move(__first), std::move(__nth),
2067 __lasti,
2068 __detail::__make_comp_proj(__comp, __proj));
2069 return __lasti;
2070 }
2071
2072 template<random_access_range _Range,
2073 typename _Comp = ranges::less, typename _Proj = identity>
2074 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2075 constexpr borrowed_iterator_t<_Range>
2076 operator()(_Range&& __r, iterator_t<_Range> __nth,
2077 _Comp __comp = {}, _Proj __proj = {}) const
2078 {
2079 return (*this)(ranges::begin(__r), std::move(__nth),
2080 ranges::end(__r), std::move(__comp), std::move(__proj));
2081 }
2082 };
2083
2084 inline constexpr __nth_element_fn nth_element{};
2085
2086 struct __lower_bound_fn
2087 {
2088 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2089 typename _Proj = identity,
2090 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2091 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2092 _Comp = ranges::less>
2093 constexpr _Iter
2094 operator()(_Iter __first, _Sent __last,
2095 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2096 {
2097 auto __len = ranges::distance(__first, __last);
2098
2099 while (__len > 0)
2100 {
2101 auto __half = __len / 2;
2102 auto __middle = __first;
2103 ranges::advance(__middle, __half);
2104 if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
2105 {
2106 __first = __middle;
2107 ++__first;
2108 __len = __len - __half - 1;
2109 }
2110 else
2111 __len = __half;
2112 }
2113 return __first;
2114 }
2115
2116 template<forward_range _Range,
2117 typename _Proj = identity,
2118 typename _Tp
2119 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2120 indirect_strict_weak_order<const _Tp*,
2121 projected<iterator_t<_Range>, _Proj>>
2122 _Comp = ranges::less>
2123 constexpr borrowed_iterator_t<_Range>
2124 operator()(_Range&& __r,
2125 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2126 {
2127 return (*this)(ranges::begin(__r), ranges::end(__r),
2128 __value, std::move(__comp), std::move(__proj));
2129 }
2130 };
2131
2132 inline constexpr __lower_bound_fn lower_bound{};
2133
2134 struct __upper_bound_fn
2135 {
2136 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2137 typename _Proj = identity,
2138 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2139 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2140 _Comp = ranges::less>
2141 constexpr _Iter
2142 operator()(_Iter __first, _Sent __last,
2143 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2144 {
2145 auto __len = ranges::distance(__first, __last);
2146
2147 while (__len > 0)
2148 {
2149 auto __half = __len / 2;
2150 auto __middle = __first;
2151 ranges::advance(__middle, __half);
2152 if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
2153 __len = __half;
2154 else
2155 {
2156 __first = __middle;
2157 ++__first;
2158 __len = __len - __half - 1;
2159 }
2160 }
2161 return __first;
2162 }
2163
2164 template<forward_range _Range,
2165 typename _Proj = identity,
2166 typename _Tp
2167 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2168 indirect_strict_weak_order<const _Tp*,
2169 projected<iterator_t<_Range>, _Proj>>
2170 _Comp = ranges::less>
2171 constexpr borrowed_iterator_t<_Range>
2172 operator()(_Range&& __r,
2173 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2174 {
2175 return (*this)(ranges::begin(__r), ranges::end(__r),
2176 __value, std::move(__comp), std::move(__proj));
2177 }
2178 };
2179
2180 inline constexpr __upper_bound_fn upper_bound{};
2181
2182 struct __equal_range_fn
2183 {
2184 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2185 typename _Proj = identity,
2186 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2187 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2188 _Comp = ranges::less>
2189 constexpr subrange<_Iter>
2190 operator()(_Iter __first, _Sent __last,
2191 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2192 {
2193 auto __len = ranges::distance(__first, __last);
2194
2195 while (__len > 0)
2196 {
2197 auto __half = __len / 2;
2198 auto __middle = __first;
2199 ranges::advance(__middle, __half);
2200 if (std::__invoke(__comp,
2201 std::__invoke(__proj, *__middle),
2202 __value))
2203 {
2204 __first = __middle;
2205 ++__first;
2206 __len = __len - __half - 1;
2207 }
2208 else if (std::__invoke(__comp,
2209 __value,
2210 std::__invoke(__proj, *__middle)))
2211 __len = __half;
2212 else
2213 {
2214 auto __left
2215 = ranges::lower_bound(__first, __middle,
2216 __value, __comp, __proj);
2217 ranges::advance(__first, __len);
2218 auto __right
2219 = ranges::upper_bound(++__middle, __first,
2220 __value, __comp, __proj);
2221 return {__left, __right};
2222 }
2223 }
2224 return {__first, __first};
2225 }
2226
2227 template<forward_range _Range,
2228 typename _Proj = identity,
2229 typename _Tp
2230 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2231 indirect_strict_weak_order<const _Tp*,
2232 projected<iterator_t<_Range>, _Proj>>
2233 _Comp = ranges::less>
2234 constexpr borrowed_subrange_t<_Range>
2235 operator()(_Range&& __r, const _Tp& __value,
2236 _Comp __comp = {}, _Proj __proj = {}) const
2237 {
2238 return (*this)(ranges::begin(__r), ranges::end(__r),
2239 __value, std::move(__comp), std::move(__proj));
2240 }
2241 };
2242
2243 inline constexpr __equal_range_fn equal_range{};
2244
2245 struct __binary_search_fn
2246 {
2247 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2248 typename _Proj = identity,
2249 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2250 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2251 _Comp = ranges::less>
2252 constexpr bool
2253 operator()(_Iter __first, _Sent __last,
2254 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2255 {
2256 auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
2257 if (__i == __last)
2258 return false;
2259 return !(bool)std::__invoke(__comp, __value,
2260 std::__invoke(__proj, *__i));
2261 }
2262
2263 template<forward_range _Range,
2264 typename _Proj = identity,
2265 typename _Tp
2266 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2267 indirect_strict_weak_order<const _Tp*,
2268 projected<iterator_t<_Range>, _Proj>>
2269 _Comp = ranges::less>
2270 constexpr bool
2271 operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
2272 _Proj __proj = {}) const
2273 {
2274 return (*this)(ranges::begin(__r), ranges::end(__r),
2275 __value, std::move(__comp), std::move(__proj));
2276 }
2277 };
2278
2279 inline constexpr __binary_search_fn binary_search{};
2280
2281 struct __is_partitioned_fn
2282 {
2283 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2284 typename _Proj = identity,
2285 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2286 constexpr bool
2287 operator()(_Iter __first, _Sent __last,
2288 _Pred __pred, _Proj __proj = {}) const
2289 {
2290 __first = ranges::find_if_not(std::move(__first), __last,
2291 __pred, __proj);
2292 if (__first == __last)
2293 return true;
2294 ++__first;
2295 return ranges::none_of(std::move(__first), std::move(__last),
2296 std::move(__pred), std::move(__proj));
2297 }
2298
2299 template<input_range _Range, typename _Proj = identity,
2300 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2301 _Pred>
2302 constexpr bool
2303 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2304 {
2305 return (*this)(ranges::begin(__r), ranges::end(__r),
2306 std::move(__pred), std::move(__proj));
2307 }
2308 };
2309
2310 inline constexpr __is_partitioned_fn is_partitioned{};
2311
2312 struct __partition_fn
2313 {
2314 template<permutable _Iter, sentinel_for<_Iter> _Sent,
2315 typename _Proj = identity,
2316 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2317 constexpr subrange<_Iter>
2318 operator()(_Iter __first, _Sent __last,
2319 _Pred __pred, _Proj __proj = {}) const
2320 {
2321 if constexpr (bidirectional_iterator<_Iter>)
2322 {
2323 auto __lasti = ranges::next(__first, __last);
2324 auto __tail = __lasti;
2325 for (;;)
2326 {
2327 for (;;)
2328 if (__first == __tail)
2329 return {std::move(__first), std::move(__lasti)};
2330 else if (std::__invoke(__pred,
2331 std::__invoke(__proj, *__first)))
2332 ++__first;
2333 else
2334 break;
2335 --__tail;
2336 for (;;)
2337 if (__first == __tail)
2338 return {std::move(__first), std::move(__lasti)};
2339 else if (!(bool)std::__invoke(__pred,
2340 std::__invoke(__proj, *__tail)))
2341 --__tail;
2342 else
2343 break;
2344 ranges::iter_swap(__first, __tail);
2345 ++__first;
2346 }
2347 }
2348 else
2349 {
2350 if (__first == __last)
2351 return {__first, __first};
2352
2353 while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
2354 if (++__first == __last)
2355 return {__first, __first};
2356
2357 auto __next = __first;
2358 while (++__next != __last)
2359 if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
2360 {
2361 ranges::iter_swap(__first, __next);
2362 ++__first;
2363 }
2364
2365 return {std::move(__first), std::move(__next)};
2366 }
2367 }
2368
2369 template<forward_range _Range, typename _Proj = identity,
2370 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2371 _Pred>
2372 requires permutable<iterator_t<_Range>>
2373 constexpr borrowed_subrange_t<_Range>
2374 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2375 {
2376 return (*this)(ranges::begin(__r), ranges::end(__r),
2377 std::move(__pred), std::move(__proj));
2378 }
2379 };
2380
2381 inline constexpr __partition_fn partition{};
2382
2383#if _GLIBCXX_HOSTED
2384 struct __stable_partition_fn
2385 {
2386 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2387 typename _Proj = identity,
2388 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2389 requires permutable<_Iter>
2390 subrange<_Iter>
2391 operator()(_Iter __first, _Sent __last,
2392 _Pred __pred, _Proj __proj = {}) const
2393 {
2394 auto __lasti = ranges::next(__first, __last);
2395 auto __middle
2396 = std::stable_partition(std::move(__first), __lasti,
2397 __detail::__make_pred_proj(__pred, __proj));
2398 return {std::move(__middle), std::move(__lasti)};
2399 }
2400
2401 template<bidirectional_range _Range, typename _Proj = identity,
2402 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2403 _Pred>
2404 requires permutable<iterator_t<_Range>>
2405 borrowed_subrange_t<_Range>
2406 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2407 {
2408 return (*this)(ranges::begin(__r), ranges::end(__r),
2409 std::move(__pred), std::move(__proj));
2410 }
2411 };
2412
2413 inline constexpr __stable_partition_fn stable_partition{};
2414#endif
2415
2416 template<typename _Iter, typename _Out1, typename _Out2>
2417 struct in_out_out_result
2418 {
2419 [[no_unique_address]] _Iter in;
2420 [[no_unique_address]] _Out1 out1;
2421 [[no_unique_address]] _Out2 out2;
2422
2423 template<typename _IIter, typename _OOut1, typename _OOut2>
2424 requires convertible_to<const _Iter&, _IIter>
2425 && convertible_to<const _Out1&, _OOut1>
2426 && convertible_to<const _Out2&, _OOut2>
2427 constexpr
2428 operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
2429 { return {in, out1, out2}; }
2430
2431 template<typename _IIter, typename _OOut1, typename _OOut2>
2432 requires convertible_to<_Iter, _IIter>
2433 && convertible_to<_Out1, _OOut1>
2434 && convertible_to<_Out2, _OOut2>
2435 constexpr
2436 operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
2437 { return {std::move(in), std::move(out1), std::move(out2)}; }
2438 };
2439
2440 template<typename _Iter, typename _Out1, typename _Out2>
2441 using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
2442
2443 struct __partition_copy_fn
2444 {
2445 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2446 weakly_incrementable _Out1, weakly_incrementable _Out2,
2447 typename _Proj = identity,
2448 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2449 requires indirectly_copyable<_Iter, _Out1>
2450 && indirectly_copyable<_Iter, _Out2>
2451 constexpr partition_copy_result<_Iter, _Out1, _Out2>
2452 operator()(_Iter __first, _Sent __last,
2453 _Out1 __out_true, _Out2 __out_false,
2454 _Pred __pred, _Proj __proj = {}) const
2455 {
2456 for (; __first != __last; ++__first)
2457 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
2458 {
2459 *__out_true = *__first;
2460 ++__out_true;
2461 }
2462 else
2463 {
2464 *__out_false = *__first;
2465 ++__out_false;
2466 }
2467
2468 return {std::move(__first),
2469 std::move(__out_true), std::move(__out_false)};
2470 }
2471
2472 template<input_range _Range, weakly_incrementable _Out1,
2473 weakly_incrementable _Out2,
2474 typename _Proj = identity,
2475 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2476 _Pred>
2477 requires indirectly_copyable<iterator_t<_Range>, _Out1>
2478 && indirectly_copyable<iterator_t<_Range>, _Out2>
2479 constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
2480 operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
2481 _Pred __pred, _Proj __proj = {}) const
2482 {
2483 return (*this)(ranges::begin(__r), ranges::end(__r),
2484 std::move(__out_true), std::move(__out_false),
2485 std::move(__pred), std::move(__proj));
2486 }
2487 };
2488
2489 inline constexpr __partition_copy_fn partition_copy{};
2490
2491 struct __partition_point_fn
2492 {
2493 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2494 typename _Proj = identity,
2495 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2496 constexpr _Iter
2497 operator()(_Iter __first, _Sent __last,
2498 _Pred __pred, _Proj __proj = {}) const
2499 {
2500 auto __len = ranges::distance(__first, __last);
2501
2502 while (__len > 0)
2503 {
2504 auto __half = __len / 2;
2505 auto __middle = __first;
2506 ranges::advance(__middle, __half);
2507 if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
2508 {
2509 __first = __middle;
2510 ++__first;
2511 __len = __len - __half - 1;
2512 }
2513 else
2514 __len = __half;
2515 }
2516 return __first;
2517 }
2518
2519 template<forward_range _Range, typename _Proj = identity,
2520 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2521 _Pred>
2522 constexpr borrowed_iterator_t<_Range>
2523 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2524 {
2525 return (*this)(ranges::begin(__r), ranges::end(__r),
2526 std::move(__pred), std::move(__proj));
2527 }
2528 };
2529
2530 inline constexpr __partition_point_fn partition_point{};
2531
2532 template<typename _Iter1, typename _Iter2, typename _Out>
2533 using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2534
2535 struct __merge_fn
2536 {
2537 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2538 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2539 weakly_incrementable _Out, typename _Comp = ranges::less,
2540 typename _Proj1 = identity, typename _Proj2 = identity>
2541 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2542 constexpr merge_result<_Iter1, _Iter2, _Out>
2543 operator()(_Iter1 __first1, _Sent1 __last1,
2544 _Iter2 __first2, _Sent2 __last2, _Out __result,
2545 _Comp __comp = {},
2546 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2547 {
2548 while (__first1 != __last1 && __first2 != __last2)
2549 {
2550 if (std::__invoke(__comp,
2551 std::__invoke(__proj2, *__first2),
2552 std::__invoke(__proj1, *__first1)))
2553 {
2554 *__result = *__first2;
2555 ++__first2;
2556 }
2557 else
2558 {
2559 *__result = *__first1;
2560 ++__first1;
2561 }
2562 ++__result;
2563 }
2564 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2565 std::move(__result));
2566 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2567 std::move(__copy1.out));
2568 return { std::move(__copy1.in), std::move(__copy2.in),
2569 std::move(__copy2.out) };
2570 }
2571
2572 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2573 typename _Comp = ranges::less,
2574 typename _Proj1 = identity, typename _Proj2 = identity>
2575 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2576 _Comp, _Proj1, _Proj2>
2577 constexpr merge_result<borrowed_iterator_t<_Range1>,
2578 borrowed_iterator_t<_Range2>,
2579 _Out>
2580 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2581 _Comp __comp = {},
2582 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2583 {
2584 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2585 ranges::begin(__r2), ranges::end(__r2),
2586 std::move(__result), std::move(__comp),
2587 std::move(__proj1), std::move(__proj2));
2588 }
2589 };
2590
2591 inline constexpr __merge_fn merge{};
2592
2593 struct __inplace_merge_fn
2594 {
2595 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2596 typename _Comp = ranges::less,
2597 typename _Proj = identity>
2598 requires sortable<_Iter, _Comp, _Proj>
2599 _Iter
2600 operator()(_Iter __first, _Iter __middle, _Sent __last,
2601 _Comp __comp = {}, _Proj __proj = {}) const
2602 {
2603 auto __lasti = ranges::next(__first, __last);
2604 std::inplace_merge(std::move(__first), std::move(__middle), __lasti,
2605 __detail::__make_comp_proj(__comp, __proj));
2606 return __lasti;
2607 }
2608
2609 template<bidirectional_range _Range,
2610 typename _Comp = ranges::less, typename _Proj = identity>
2611 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2612 borrowed_iterator_t<_Range>
2613 operator()(_Range&& __r, iterator_t<_Range> __middle,
2614 _Comp __comp = {}, _Proj __proj = {}) const
2615 {
2616 return (*this)(ranges::begin(__r), std::move(__middle),
2617 ranges::end(__r),
2618 std::move(__comp), std::move(__proj));
2619 }
2620 };
2621
2622 inline constexpr __inplace_merge_fn inplace_merge{};
2623
2624 struct __includes_fn
2625 {
2626 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2627 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2628 typename _Proj1 = identity, typename _Proj2 = identity,
2629 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
2630 projected<_Iter2, _Proj2>>
2631 _Comp = ranges::less>
2632 constexpr bool
2633 operator()(_Iter1 __first1, _Sent1 __last1,
2634 _Iter2 __first2, _Sent2 __last2,
2635 _Comp __comp = {},
2636 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2637 {
2638 while (__first1 != __last1 && __first2 != __last2)
2639 if (std::__invoke(__comp,
2640 std::__invoke(__proj2, *__first2),
2641 std::__invoke(__proj1, *__first1)))
2642 return false;
2643 else if (std::__invoke(__comp,
2644 std::__invoke(__proj1, *__first1),
2645 std::__invoke(__proj2, *__first2)))
2646 ++__first1;
2647 else
2648 {
2649 ++__first1;
2650 ++__first2;
2651 }
2652
2653 return __first2 == __last2;
2654 }
2655
2656 template<input_range _Range1, input_range _Range2,
2657 typename _Proj1 = identity, typename _Proj2 = identity,
2658 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
2659 projected<iterator_t<_Range2>, _Proj2>>
2660 _Comp = ranges::less>
2661 constexpr bool
2662 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
2663 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2664 {
2665 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2666 ranges::begin(__r2), ranges::end(__r2),
2667 std::move(__comp),
2668 std::move(__proj1), std::move(__proj2));
2669 }
2670 };
2671
2672 inline constexpr __includes_fn includes{};
2673
2674 template<typename _Iter1, typename _Iter2, typename _Out>
2675 using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2676
2677 struct __set_union_fn
2678 {
2679 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2680 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2681 weakly_incrementable _Out, typename _Comp = ranges::less,
2682 typename _Proj1 = identity, typename _Proj2 = identity>
2683 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2684 constexpr set_union_result<_Iter1, _Iter2, _Out>
2685 operator()(_Iter1 __first1, _Sent1 __last1,
2686 _Iter2 __first2, _Sent2 __last2,
2687 _Out __result, _Comp __comp = {},
2688 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2689 {
2690 while (__first1 != __last1 && __first2 != __last2)
2691 {
2692 if (std::__invoke(__comp,
2693 std::__invoke(__proj1, *__first1),
2694 std::__invoke(__proj2, *__first2)))
2695 {
2696 *__result = *__first1;
2697 ++__first1;
2698 }
2699 else if (std::__invoke(__comp,
2700 std::__invoke(__proj2, *__first2),
2701 std::__invoke(__proj1, *__first1)))
2702 {
2703 *__result = *__first2;
2704 ++__first2;
2705 }
2706 else
2707 {
2708 *__result = *__first1;
2709 ++__first1;
2710 ++__first2;
2711 }
2712 ++__result;
2713 }
2714 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2715 std::move(__result));
2716 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2717 std::move(__copy1.out));
2718 return {std::move(__copy1.in), std::move(__copy2.in),
2719 std::move(__copy2.out)};
2720 }
2721
2722 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2723 typename _Comp = ranges::less,
2724 typename _Proj1 = identity, typename _Proj2 = identity>
2725 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2726 _Comp, _Proj1, _Proj2>
2727 constexpr set_union_result<borrowed_iterator_t<_Range1>,
2728 borrowed_iterator_t<_Range2>, _Out>
2729 operator()(_Range1&& __r1, _Range2&& __r2,
2730 _Out __result, _Comp __comp = {},
2731 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2732 {
2733 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2734 ranges::begin(__r2), ranges::end(__r2),
2735 std::move(__result), std::move(__comp),
2736 std::move(__proj1), std::move(__proj2));
2737 }
2738 };
2739
2740 inline constexpr __set_union_fn set_union{};
2741
2742 template<typename _Iter1, typename _Iter2, typename _Out>
2743 using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2744
2745 struct __set_intersection_fn
2746 {
2747 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2748 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2749 weakly_incrementable _Out, typename _Comp = ranges::less,
2750 typename _Proj1 = identity, typename _Proj2 = identity>
2751 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2752 constexpr set_intersection_result<_Iter1, _Iter2, _Out>
2753 operator()(_Iter1 __first1, _Sent1 __last1,
2754 _Iter2 __first2, _Sent2 __last2, _Out __result,
2755 _Comp __comp = {},
2756 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2757 {
2758 while (__first1 != __last1 && __first2 != __last2)
2759 if (std::__invoke(__comp,
2760 std::__invoke(__proj1, *__first1),
2761 std::__invoke(__proj2, *__first2)))
2762 ++__first1;
2763 else if (std::__invoke(__comp,
2764 std::__invoke(__proj2, *__first2),
2765 std::__invoke(__proj1, *__first1)))
2766 ++__first2;
2767 else
2768 {
2769 *__result = *__first1;
2770 ++__first1;
2771 ++__first2;
2772 ++__result;
2773 }
2774 // TODO: Eliminating these variables triggers an ICE.
2775 auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
2776 auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
2777 return {std::move(__last1i), std::move(__last2i), std::move(__result)};
2778 }
2779
2780 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2781 typename _Comp = ranges::less,
2782 typename _Proj1 = identity, typename _Proj2 = identity>
2783 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2784 _Comp, _Proj1, _Proj2>
2785 constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
2786 borrowed_iterator_t<_Range2>, _Out>
2787 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2788 _Comp __comp = {},
2789 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2790 {
2791 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2792 ranges::begin(__r2), ranges::end(__r2),
2793 std::move(__result), std::move(__comp),
2794 std::move(__proj1), std::move(__proj2));
2795 }
2796 };
2797
2798 inline constexpr __set_intersection_fn set_intersection{};
2799
2800 template<typename _Iter, typename _Out>
2801 using set_difference_result = in_out_result<_Iter, _Out>;
2802
2803 struct __set_difference_fn
2804 {
2805 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2806 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2807 weakly_incrementable _Out, typename _Comp = ranges::less,
2808 typename _Proj1 = identity, typename _Proj2 = identity>
2809 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2810 constexpr set_difference_result<_Iter1, _Out>
2811 operator()(_Iter1 __first1, _Sent1 __last1,
2812 _Iter2 __first2, _Sent2 __last2, _Out __result,
2813 _Comp __comp = {},
2814 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2815 {
2816 while (__first1 != __last1 && __first2 != __last2)
2817 if (std::__invoke(__comp,
2818 std::__invoke(__proj1, *__first1),
2819 std::__invoke(__proj2, *__first2)))
2820 {
2821 *__result = *__first1;
2822 ++__first1;
2823 ++__result;
2824 }
2825 else if (std::__invoke(__comp,
2826 std::__invoke(__proj2, *__first2),
2827 std::__invoke(__proj1, *__first1)))
2828 ++__first2;
2829 else
2830 {
2831 ++__first1;
2832 ++__first2;
2833 }
2834 return ranges::copy(std::move(__first1), std::move(__last1),
2835 std::move(__result));
2836 }
2837
2838 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2839 typename _Comp = ranges::less,
2840 typename _Proj1 = identity, typename _Proj2 = identity>
2841 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2842 _Comp, _Proj1, _Proj2>
2843 constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
2844 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2845 _Comp __comp = {},
2846 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2847 {
2848 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2849 ranges::begin(__r2), ranges::end(__r2),
2850 std::move(__result), std::move(__comp),
2851 std::move(__proj1), std::move(__proj2));
2852 }
2853 };
2854
2855 inline constexpr __set_difference_fn set_difference{};
2856
2857 template<typename _Iter1, typename _Iter2, typename _Out>
2858 using set_symmetric_difference_result
2859 = in_in_out_result<_Iter1, _Iter2, _Out>;
2860
2861 struct __set_symmetric_difference_fn
2862 {
2863 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2864 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2865 weakly_incrementable _Out, typename _Comp = ranges::less,
2866 typename _Proj1 = identity, typename _Proj2 = identity>
2867 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2868 constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
2869 operator()(_Iter1 __first1, _Sent1 __last1,
2870 _Iter2 __first2, _Sent2 __last2,
2871 _Out __result, _Comp __comp = {},
2872 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2873 {
2874 while (__first1 != __last1 && __first2 != __last2)
2875 if (std::__invoke(__comp,
2876 std::__invoke(__proj1, *__first1),
2877 std::__invoke(__proj2, *__first2)))
2878 {
2879 *__result = *__first1;
2880 ++__first1;
2881 ++__result;
2882 }
2883 else if (std::__invoke(__comp,
2884 std::__invoke(__proj2, *__first2),
2885 std::__invoke(__proj1, *__first1)))
2886 {
2887 *__result = *__first2;
2888 ++__first2;
2889 ++__result;
2890 }
2891 else
2892 {
2893 ++__first1;
2894 ++__first2;
2895 }
2896 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2897 std::move(__result));
2898 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2899 std::move(__copy1.out));
2900 return {std::move(__copy1.in), std::move(__copy2.in),
2901 std::move(__copy2.out)};
2902 }
2903
2904 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2905 typename _Comp = ranges::less,
2906 typename _Proj1 = identity, typename _Proj2 = identity>
2907 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2908 _Comp, _Proj1, _Proj2>
2909 constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
2910 borrowed_iterator_t<_Range2>,
2911 _Out>
2912 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2913 _Comp __comp = {},
2914 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2915 {
2916 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2917 ranges::begin(__r2), ranges::end(__r2),
2918 std::move(__result), std::move(__comp),
2919 std::move(__proj1), std::move(__proj2));
2920 }
2921 };
2922
2923 inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
2924
2925 // min is defined in <bits/ranges_util.h>.
2926
2927 struct __max_fn
2928 {
2929 template<typename _Tp, typename _Proj = identity,
2930 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2931 _Comp = ranges::less>
2932 constexpr const _Tp&
2933 operator()(const _Tp& __a, const _Tp& __b,
2934 _Comp __comp = {}, _Proj __proj = {}) const
2935 {
2936 if (std::__invoke(__comp,
2937 std::__invoke(__proj, __a),
2938 std::__invoke(__proj, __b)))
2939 return __b;
2940 else
2941 return __a;
2942 }
2943
2944 template<input_range _Range, typename _Proj = identity,
2945 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2946 _Comp = ranges::less>
2947 requires indirectly_copyable_storable<iterator_t<_Range>,
2948 range_value_t<_Range>*>
2949 constexpr range_value_t<_Range>
2950 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2951 {
2952 auto __first = ranges::begin(__r);
2953 auto __last = ranges::end(__r);
2954 __glibcxx_assert(__first != __last);
2955 auto __result = *__first;
2956 while (++__first != __last)
2957 {
2958 auto&& __tmp = *__first;
2959 if (std::__invoke(__comp,
2960 std::__invoke(__proj, __result),
2961 std::__invoke(__proj, __tmp)))
2962 __result = std::forward<decltype(__tmp)>(__tmp);
2963 }
2964 return __result;
2965 }
2966
2967 template<copyable _Tp, typename _Proj = identity,
2968 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2969 _Comp = ranges::less>
2970 constexpr _Tp
2971 operator()(initializer_list<_Tp> __r,
2972 _Comp __comp = {}, _Proj __proj = {}) const
2973 {
2974 return (*this)(ranges::subrange(__r),
2975 std::move(__comp), std::move(__proj));
2976 }
2977 };
2978
2979 inline constexpr __max_fn max{};
2980
2981 struct __clamp_fn
2982 {
2983 template<typename _Tp, typename _Proj = identity,
2984 indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
2985 = ranges::less>
2986 constexpr const _Tp&
2987 operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
2988 _Comp __comp = {}, _Proj __proj = {}) const
2989 {
2990 __glibcxx_assert(!(std::__invoke(__comp,
2991 std::__invoke(__proj, __hi),
2992 std::__invoke(__proj, __lo))));
2993 auto&& __proj_val = std::__invoke(__proj, __val);
2994 if (std::__invoke(__comp,
2995 std::forward<decltype(__proj_val)>(__proj_val),
2996 std::__invoke(__proj, __lo)))
2997 return __lo;
2998 else if (std::__invoke(__comp,
2999 std::__invoke(__proj, __hi),
3000 std::forward<decltype(__proj_val)>(__proj_val)))
3001 return __hi;
3002 else
3003 return __val;
3004 }
3005 };
3006
3007 inline constexpr __clamp_fn clamp{};
3008
3009 template<typename _Tp>
3010 struct min_max_result
3011 {
3012 [[no_unique_address]] _Tp min;
3013 [[no_unique_address]] _Tp max;
3014
3015 template<typename _Tp2>
3016 requires convertible_to<const _Tp&, _Tp2>
3017 constexpr
3018 operator min_max_result<_Tp2>() const &
3019 { return {min, max}; }
3020
3021 template<typename _Tp2>
3022 requires convertible_to<_Tp, _Tp2>
3023 constexpr
3024 operator min_max_result<_Tp2>() &&
3025 { return {std::move(min), std::move(max)}; }
3026 };
3027
3028 template<typename _Tp>
3029 using minmax_result = min_max_result<_Tp>;
3030
3031 struct __minmax_fn
3032 {
3033 template<typename _Tp, typename _Proj = identity,
3034 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3035 _Comp = ranges::less>
3036 constexpr minmax_result<const _Tp&>
3037 operator()(const _Tp& __a, const _Tp& __b,
3038 _Comp __comp = {}, _Proj __proj = {}) const
3039 {
3040 if (std::__invoke(__comp,
3041 std::__invoke(__proj, __b),
3042 std::__invoke(__proj, __a)))
3043 return {__b, __a};
3044 else
3045 return {__a, __b};
3046 }
3047
3048 template<input_range _Range, typename _Proj = identity,
3049 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3050 _Comp = ranges::less>
3051 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
3052 constexpr minmax_result<range_value_t<_Range>>
3053 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3054 {
3055 auto __first = ranges::begin(__r);
3056 auto __last = ranges::end(__r);
3057 __glibcxx_assert(__first != __last);
3058 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3059 minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
3060 if (++__first == __last)
3061 return __result;
3062 else
3063 {
3064 // At this point __result.min == __result.max, so a single
3065 // comparison with the next element suffices.
3066 auto&& __val = *__first;
3067 if (__comp_proj(__val, __result.min))
3068 __result.min = std::forward<decltype(__val)>(__val);
3069 else
3070 __result.max = std::forward<decltype(__val)>(__val);
3071 }
3072 while (++__first != __last)
3073 {
3074 // Now process two elements at a time so that we perform at most
3075 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
3076 // iterations of this loop performs three comparisons).
3077 range_value_t<_Range> __val1 = *__first;
3078 if (++__first == __last)
3079 {
3080 // N is odd; in this final iteration, we perform at most two
3081 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
3082 // which is not more than 3*N/2, as required.
3083 if (__comp_proj(__val1, __result.min))
3084 __result.min = std::move(__val1);
3085 else if (!__comp_proj(__val1, __result.max))
3086 __result.max = std::move(__val1);
3087 break;
3088 }
3089 auto&& __val2 = *__first;
3090 if (!__comp_proj(__val2, __val1))
3091 {
3092 if (__comp_proj(__val1, __result.min))
3093 __result.min = std::move(__val1);
3094 if (!__comp_proj(__val2, __result.max))
3095 __result.max = std::forward<decltype(__val2)>(__val2);
3096 }
3097 else
3098 {
3099 if (__comp_proj(__val2, __result.min))
3100 __result.min = std::forward<decltype(__val2)>(__val2);
3101 if (!__comp_proj(__val1, __result.max))
3102 __result.max = std::move(__val1);
3103 }
3104 }
3105 return __result;
3106 }
3107
3108 template<copyable _Tp, typename _Proj = identity,
3109 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3110 _Comp = ranges::less>
3111 constexpr minmax_result<_Tp>
3112 operator()(initializer_list<_Tp> __r,
3113 _Comp __comp = {}, _Proj __proj = {}) const
3114 {
3115 return (*this)(ranges::subrange(__r),
3116 std::move(__comp), std::move(__proj));
3117 }
3118 };
3119
3120 inline constexpr __minmax_fn minmax{};
3121
3122 struct __min_element_fn
3123 {
3124 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3125 typename _Proj = identity,
3126 indirect_strict_weak_order<projected<_Iter, _Proj>>
3127 _Comp = ranges::less>
3128 constexpr _Iter
3129 operator()(_Iter __first, _Sent __last,
3130 _Comp __comp = {}, _Proj __proj = {}) const
3131 {
3132 if (__first == __last)
3133 return __first;
3134
3135 auto __i = __first;
3136 while (++__i != __last)
3137 {
3138 if (std::__invoke(__comp,
3139 std::__invoke(__proj, *__i),
3140 std::__invoke(__proj, *__first)))
3141 __first = __i;
3142 }
3143 return __first;
3144 }
3145
3146 template<forward_range _Range, typename _Proj = identity,
3147 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3148 _Comp = ranges::less>
3149 constexpr borrowed_iterator_t<_Range>
3150 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3151 {
3152 return (*this)(ranges::begin(__r), ranges::end(__r),
3153 std::move(__comp), std::move(__proj));
3154 }
3155 };
3156
3157 inline constexpr __min_element_fn min_element{};
3158
3159 struct __max_element_fn
3160 {
3161 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3162 typename _Proj = identity,
3163 indirect_strict_weak_order<projected<_Iter, _Proj>>
3164 _Comp = ranges::less>
3165 constexpr _Iter
3166 operator()(_Iter __first, _Sent __last,
3167 _Comp __comp = {}, _Proj __proj = {}) const
3168 {
3169 if (__first == __last)
3170 return __first;
3171
3172 auto __i = __first;
3173 while (++__i != __last)
3174 {
3175 if (std::__invoke(__comp,
3176 std::__invoke(__proj, *__first),
3177 std::__invoke(__proj, *__i)))
3178 __first = __i;
3179 }
3180 return __first;
3181 }
3182
3183 template<forward_range _Range, typename _Proj = identity,
3184 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3185 _Comp = ranges::less>
3186 constexpr borrowed_iterator_t<_Range>
3187 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3188 {
3189 return (*this)(ranges::begin(__r), ranges::end(__r),
3190 std::move(__comp), std::move(__proj));
3191 }
3192 };
3193
3194 inline constexpr __max_element_fn max_element{};
3195
3196 template<typename _Iter>
3197 using minmax_element_result = min_max_result<_Iter>;
3198
3199 struct __minmax_element_fn
3200 {
3201 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3202 typename _Proj = identity,
3203 indirect_strict_weak_order<projected<_Iter, _Proj>>
3204 _Comp = ranges::less>
3205 constexpr minmax_element_result<_Iter>
3206 operator()(_Iter __first, _Sent __last,
3207 _Comp __comp = {}, _Proj __proj = {}) const
3208 {
3209 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3210 minmax_element_result<_Iter> __result = {__first, __first};
3211 if (__first == __last || ++__first == __last)
3212 return __result;
3213 else
3214 {
3215 // At this point __result.min == __result.max, so a single
3216 // comparison with the next element suffices.
3217 if (__comp_proj(*__first, *__result.min))
3218 __result.min = __first;
3219 else
3220 __result.max = __first;
3221 }
3222 while (++__first != __last)
3223 {
3224 // Now process two elements at a time so that we perform at most
3225 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
3226 // iterations of this loop performs three comparisons).
3227 auto __prev = __first;
3228 if (++__first == __last)
3229 {
3230 // N is odd; in this final iteration, we perform at most two
3231 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
3232 // which is not more than 3*N/2, as required.
3233 if (__comp_proj(*__prev, *__result.min))
3234 __result.min = __prev;
3235 else if (!__comp_proj(*__prev, *__result.max))
3236 __result.max = __prev;
3237 break;
3238 }
3239 if (!__comp_proj(*__first, *__prev))
3240 {
3241 if (__comp_proj(*__prev, *__result.min))
3242 __result.min = __prev;
3243 if (!__comp_proj(*__first, *__result.max))
3244 __result.max = __first;
3245 }
3246 else
3247 {
3248 if (__comp_proj(*__first, *__result.min))
3249 __result.min = __first;
3250 if (!__comp_proj(*__prev, *__result.max))
3251 __result.max = __prev;
3252 }
3253 }
3254 return __result;
3255 }
3256
3257 template<forward_range _Range, typename _Proj = identity,
3258 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3259 _Comp = ranges::less>
3260 constexpr minmax_element_result<borrowed_iterator_t<_Range>>
3261 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3262 {
3263 return (*this)(ranges::begin(__r), ranges::end(__r),
3264 std::move(__comp), std::move(__proj));
3265 }
3266 };
3267
3268 inline constexpr __minmax_element_fn minmax_element{};
3269
3270 struct __lexicographical_compare_fn
3271 {
3272 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3273 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3274 typename _Proj1 = identity, typename _Proj2 = identity,
3275 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
3276 projected<_Iter2, _Proj2>>
3277 _Comp = ranges::less>
3278 constexpr bool
3279 operator()(_Iter1 __first1, _Sent1 __last1,
3280 _Iter2 __first2, _Sent2 __last2,
3281 _Comp __comp = {},
3282 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3283 {
3284 if constexpr (__detail::__is_normal_iterator<_Iter1>
3285 && same_as<_Iter1, _Sent1>)
3286 return (*this)(__first1.base(), __last1.base(),
3287 std::move(__first2), std::move(__last2),
3288 std::move(__comp),
3289 std::move(__proj1), std::move(__proj2));
3290 else if constexpr (__detail::__is_normal_iterator<_Iter2>
3291 && same_as<_Iter2, _Sent2>)
3292 return (*this)(std::move(__first1), std::move(__last1),
3293 __first2.base(), __last2.base(),
3294 std::move(__comp),
3295 std::move(__proj1), std::move(__proj2));
3296 else
3297 {
3298 constexpr bool __sized_iters
3299 = (sized_sentinel_for<_Sent1, _Iter1>
3300 && sized_sentinel_for<_Sent2, _Iter2>);
3301 if constexpr (__sized_iters)
3302 {
3303 using _ValueType1 = iter_value_t<_Iter1>;
3304 using _ValueType2 = iter_value_t<_Iter2>;
3305 // This condition is consistent with the one in
3306 // __lexicographical_compare_aux in <bits/stl_algobase.h>.
3307 constexpr bool __use_memcmp
3308 = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
3309 && __ptr_to_nonvolatile<_Iter1>
3310 && __ptr_to_nonvolatile<_Iter2>
3311 && (is_same_v<_Comp, ranges::less>
3312 || is_same_v<_Comp, ranges::greater>)
3313 && is_same_v<_Proj1, identity>
3314 && is_same_v<_Proj2, identity>);
3315 if constexpr (__use_memcmp)
3316 {
3317 const auto __d1 = __last1 - __first1;
3318 const auto __d2 = __last2 - __first2;
3319
3320 if (const auto __len = std::min(__d1, __d2))
3321 {
3322 const auto __c
3323 = std::__memcmp(__first1, __first2, __len);
3324 if constexpr (is_same_v<_Comp, ranges::less>)
3325 {
3326 if (__c < 0)
3327 return true;
3328 if (__c > 0)
3329 return false;
3330 }
3331 else if constexpr (is_same_v<_Comp, ranges::greater>)
3332 {
3333 if (__c > 0)
3334 return true;
3335 if (__c < 0)
3336 return false;
3337 }
3338 }
3339 return __d1 < __d2;
3340 }
3341 }
3342
3343 for (; __first1 != __last1 && __first2 != __last2;
3344 ++__first1, (void) ++__first2)
3345 {
3346 if (std::__invoke(__comp,
3347 std::__invoke(__proj1, *__first1),
3348 std::__invoke(__proj2, *__first2)))
3349 return true;
3350 if (std::__invoke(__comp,
3351 std::__invoke(__proj2, *__first2),
3352 std::__invoke(__proj1, *__first1)))
3353 return false;
3354 }
3355 return __first1 == __last1 && __first2 != __last2;
3356 }
3357 }
3358
3359 template<input_range _Range1, input_range _Range2,
3360 typename _Proj1 = identity, typename _Proj2 = identity,
3361 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
3362 projected<iterator_t<_Range2>, _Proj2>>
3363 _Comp = ranges::less>
3364 constexpr bool
3365 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
3366 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3367 {
3368 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3369 ranges::begin(__r2), ranges::end(__r2),
3370 std::move(__comp),
3371 std::move(__proj1), std::move(__proj2));
3372 }
3373
3374 private:
3375 template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
3376 static constexpr bool __ptr_to_nonvolatile
3377 = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
3378 };
3379
3380 inline constexpr __lexicographical_compare_fn lexicographical_compare;
3381
3382 template<typename _Iter>
3383 struct in_found_result
3384 {
3385 [[no_unique_address]] _Iter in;
3386 bool found;
3387
3388 template<typename _Iter2>
3389 requires convertible_to<const _Iter&, _Iter2>
3390 constexpr
3391 operator in_found_result<_Iter2>() const &
3392 { return {in, found}; }
3393
3394 template<typename _Iter2>
3395 requires convertible_to<_Iter, _Iter2>
3396 constexpr
3397 operator in_found_result<_Iter2>() &&
3398 { return {std::move(in), found}; }
3399 };
3400
3401 template<typename _Iter>
3402 using next_permutation_result = in_found_result<_Iter>;
3403
3404 struct __next_permutation_fn
3405 {
3406 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3407 typename _Comp = ranges::less, typename _Proj = identity>
3408 requires sortable<_Iter, _Comp, _Proj>
3409 constexpr next_permutation_result<_Iter>
3410 operator()(_Iter __first, _Sent __last,
3411 _Comp __comp = {}, _Proj __proj = {}) const
3412 {
3413 if (__first == __last)
3414 return {std::move(__first), false};
3415
3416 auto __i = __first;
3417 ++__i;
3418 if (__i == __last)
3419 return {std::move(__i), false};
3420
3421 auto __lasti = ranges::next(__first, __last);
3422 __i = __lasti;
3423 --__i;
3424
3425 for (;;)
3426 {
3427 auto __ii = __i;
3428 --__i;
3429 if (std::__invoke(__comp,
3430 std::__invoke(__proj, *__i),
3431 std::__invoke(__proj, *__ii)))
3432 {
3433 auto __j = __lasti;
3434 while (!(bool)std::__invoke(__comp,
3435 std::__invoke(__proj, *__i),
3436 std::__invoke(__proj, *--__j)))
3437 ;
3438 ranges::iter_swap(__i, __j);
3439 ranges::reverse(__ii, __last);
3440 return {std::move(__lasti), true};
3441 }
3442 if (__i == __first)
3443 {
3444 ranges::reverse(__first, __last);
3445 return {std::move(__lasti), false};
3446 }
3447 }
3448 }
3449
3450 template<bidirectional_range _Range, typename _Comp = ranges::less,
3451 typename _Proj = identity>
3452 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3453 constexpr next_permutation_result<borrowed_iterator_t<_Range>>
3454 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3455 {
3456 return (*this)(ranges::begin(__r), ranges::end(__r),
3457 std::move(__comp), std::move(__proj));
3458 }
3459 };
3460
3461 inline constexpr __next_permutation_fn next_permutation{};
3462
3463 template<typename _Iter>
3464 using prev_permutation_result = in_found_result<_Iter>;
3465
3466 struct __prev_permutation_fn
3467 {
3468 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3469 typename _Comp = ranges::less, typename _Proj = identity>
3470 requires sortable<_Iter, _Comp, _Proj>
3471 constexpr prev_permutation_result<_Iter>
3472 operator()(_Iter __first, _Sent __last,
3473 _Comp __comp = {}, _Proj __proj = {}) const
3474 {
3475 if (__first == __last)
3476 return {std::move(__first), false};
3477
3478 auto __i = __first;
3479 ++__i;
3480 if (__i == __last)
3481 return {std::move(__i), false};
3482
3483 auto __lasti = ranges::next(__first, __last);
3484 __i = __lasti;
3485 --__i;
3486
3487 for (;;)
3488 {
3489 auto __ii = __i;
3490 --__i;
3491 if (std::__invoke(__comp,
3492 std::__invoke(__proj, *__ii),
3493 std::__invoke(__proj, *__i)))
3494 {
3495 auto __j = __lasti;
3496 while (!(bool)std::__invoke(__comp,
3497 std::__invoke(__proj, *--__j),
3498 std::__invoke(__proj, *__i)))
3499 ;
3500 ranges::iter_swap(__i, __j);
3501 ranges::reverse(__ii, __last);
3502 return {std::move(__lasti), true};
3503 }
3504 if (__i == __first)
3505 {
3506 ranges::reverse(__first, __last);
3507 return {std::move(__lasti), false};
3508 }
3509 }
3510 }
3511
3512 template<bidirectional_range _Range, typename _Comp = ranges::less,
3513 typename _Proj = identity>
3514 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3515 constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
3516 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3517 {
3518 return (*this)(ranges::begin(__r), ranges::end(__r),
3519 std::move(__comp), std::move(__proj));
3520 }
3521 };
3522
3523 inline constexpr __prev_permutation_fn prev_permutation{};
3524
3525#if __glibcxx_ranges_contains >= 202207L // C++ >= 23
3526 struct __contains_fn
3527 {
3528 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3529 typename _Proj = identity,
3530 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
3531 requires indirect_binary_predicate<ranges::equal_to,
3532 projected<_Iter, _Proj>, const _Tp*>
3533 constexpr bool
3534 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
3535 { return ranges::find(std::move(__first), __last, __value, std::move(__proj)) != __last; }
3536
3537 template<input_range _Range,
3538 typename _Proj = identity,
3539 typename _Tp
3540 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
3541 requires indirect_binary_predicate<ranges::equal_to,
3542 projected<iterator_t<_Range>, _Proj>, const _Tp*>
3543 constexpr bool
3544 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
3545 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
3546 };
3547
3548 inline constexpr __contains_fn contains{};
3549
3550 struct __contains_subrange_fn
3551 {
3552 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3553 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3554 typename _Pred = ranges::equal_to,
3555 typename _Proj1 = identity, typename _Proj2 = identity>
3556 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
3557 constexpr bool
3558 operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
3559 _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3560 {
3561 return __first2 == __last2
3562 || !ranges::search(__first1, __last1, __first2, __last2,
3563 std::move(__pred), std::move(__proj1), std::move(__proj2)).empty();
3564 }
3565
3566 template<forward_range _Range1, forward_range _Range2,
3567 typename _Pred = ranges::equal_to,
3568 typename _Proj1 = identity, typename _Proj2 = identity>
3569 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
3570 _Pred, _Proj1, _Proj2>
3571 constexpr bool
3572 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
3573 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3574 {
3575 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3576 ranges::begin(__r2), ranges::end(__r2),
3577 std::move(__pred), std::move(__proj1), std::move(__proj2));
3578 }
3579 };
3580
3581 inline constexpr __contains_subrange_fn contains_subrange{};
3582
3583#endif // __glibcxx_ranges_contains
3584
3585#if __glibcxx_ranges_find_last >= 202207L // C++ >= 23
3586
3587 struct __find_last_fn
3588 {
3589 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3590 typename _Proj = identity,
3591 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
3592 requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Tp*>
3593 constexpr subrange<_Iter>
3594 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
3595 {
3596 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3597 {
3598 _Iter __found = ranges::find(reverse_iterator<_Iter>{__last},
3599 reverse_iterator<_Iter>{__first},
3600 __value, std::move(__proj)).base();
3601 if (__found == __first)
3602 return {__last, __last};
3603 else
3604 return {ranges::prev(__found), __last};
3605 }
3606 else
3607 {
3608 _Iter __found = ranges::find(__first, __last, __value, __proj);
3609 if (__found == __last)
3610 return {__found, __found};
3611 __first = __found;
3612 for (;;)
3613 {
3614 __first = ranges::find(ranges::next(__first), __last, __value, __proj);
3615 if (__first == __last)
3616 return {__found, __first};
3617 __found = __first;
3618 }
3619 }
3620 }
3621
3622 template<forward_range _Range, typename _Proj = identity,
3623 typename _Tp
3624 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
3625 requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Tp*>
3626 constexpr borrowed_subrange_t<_Range>
3627 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
3628 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
3629 };
3630
3631 inline constexpr __find_last_fn find_last{};
3632
3633 struct __find_last_if_fn
3634 {
3635 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
3636 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3637 constexpr subrange<_Iter>
3638 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
3639 {
3640 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3641 {
3642 _Iter __found = ranges::find_if(reverse_iterator<_Iter>{__last},
3643 reverse_iterator<_Iter>{__first},
3644 std::move(__pred), std::move(__proj)).base();
3645 if (__found == __first)
3646 return {__last, __last};
3647 else
3648 return {ranges::prev(__found), __last};
3649 }
3650 else
3651 {
3652 _Iter __found = ranges::find_if(__first, __last, __pred, __proj);
3653 if (__found == __last)
3654 return {__found, __found};
3655 __first = __found;
3656 for (;;)
3657 {
3658 __first = ranges::find_if(ranges::next(__first), __last, __pred, __proj);
3659 if (__first == __last)
3660 return {__found, __first};
3661 __found = __first;
3662 }
3663 }
3664 }
3665
3666 template<forward_range _Range, typename _Proj = identity,
3667 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
3668 constexpr borrowed_subrange_t<_Range>
3669 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3670 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
3671 };
3672
3673 inline constexpr __find_last_if_fn find_last_if{};
3674
3675 struct __find_last_if_not_fn
3676 {
3677 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
3678 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3679 constexpr subrange<_Iter>
3680 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
3681 {
3682 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3683 {
3684 _Iter __found = ranges::find_if_not(reverse_iterator<_Iter>{__last},
3685 reverse_iterator<_Iter>{__first},
3686 std::move(__pred), std::move(__proj)).base();
3687 if (__found == __first)
3688 return {__last, __last};
3689 else
3690 return {ranges::prev(__found), __last};
3691 }
3692 else
3693 {
3694 _Iter __found = ranges::find_if_not(__first, __last, __pred, __proj);
3695 if (__found == __last)
3696 return {__found, __found};
3697 __first = __found;
3698 for (;;)
3699 {
3700 __first = ranges::find_if_not(ranges::next(__first), __last, __pred, __proj);
3701 if (__first == __last)
3702 return {__found, __first};
3703 __found = __first;
3704 }
3705 }
3706 }
3707
3708 template<forward_range _Range, typename _Proj = identity,
3709 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
3710 constexpr borrowed_subrange_t<_Range>
3711 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3712 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
3713 };
3714
3715 inline constexpr __find_last_if_not_fn find_last_if_not{};
3716
3717#endif // __glibcxx_ranges_find_last
3718
3719#if __glibcxx_ranges_fold >= 202207L // C++ >= 23
3720
3721 template<typename _Iter, typename _Tp>
3722 struct in_value_result
3723 {
3724 [[no_unique_address]] _Iter in;
3725 [[no_unique_address]] _Tp value;
3726
3727 template<typename _Iter2, typename _Tp2>
3728 requires convertible_to<const _Iter&, _Iter2>
3729 && convertible_to<const _Tp&, _Tp2>
3730 constexpr
3731 operator in_value_result<_Iter2, _Tp2>() const &
3732 { return {in, value}; }
3733
3734 template<typename _Iter2, typename _Tp2>
3735 requires convertible_to<_Iter, _Iter2>
3736 && convertible_to<_Tp, _Tp2>
3737 constexpr
3738 operator in_value_result<_Iter2, _Tp2>() &&
3739 { return {std::move(in), std::move(value)}; }
3740 };
3741
3742 namespace __detail
3743 {
3744 template<typename _Fp>
3745 class __flipped
3746 {
3747 _Fp _M_f;
3748
3749 public:
3750 template<typename _Tp, typename _Up>
3751 requires invocable<_Fp&, _Up, _Tp>
3752 invoke_result_t<_Fp&, _Up, _Tp>
3753 operator()(_Tp&&, _Up&&); // not defined
3754 };
3755
3756 template<typename _Fp, typename _Tp, typename _Iter, typename _Up>
3757 concept __indirectly_binary_left_foldable_impl = movable<_Tp> && movable<_Up>
3758 && convertible_to<_Tp, _Up>
3759 && invocable<_Fp&, _Up, iter_reference_t<_Iter>>
3760 && assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Iter>>>;
3761
3762 template<typename _Fp, typename _Tp, typename _Iter>
3763 concept __indirectly_binary_left_foldable = copy_constructible<_Fp>
3764 && indirectly_readable<_Iter>
3765 && invocable<_Fp&, _Tp, iter_reference_t<_Iter>>
3766 && convertible_to<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>,
3768 && __indirectly_binary_left_foldable_impl
3770
3771 template <typename _Fp, typename _Tp, typename _Iter>
3772 concept __indirectly_binary_right_foldable
3773 = __indirectly_binary_left_foldable<__flipped<_Fp>, _Tp, _Iter>;
3774 } // namespace __detail
3775
3776 template<typename _Iter, typename _Tp>
3777 using fold_left_with_iter_result = in_value_result<_Iter, _Tp>;
3778
3779 struct __fold_left_with_iter_fn
3780 {
3781 template<typename _Ret_iter,
3782 typename _Iter, typename _Sent, typename _Tp, typename _Fp>
3783 static constexpr auto
3784 _S_impl(_Iter __first, _Sent __last, _Tp __init, _Fp __f)
3785 {
3786 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>>;
3787 using _Ret = fold_left_with_iter_result<_Ret_iter, _Up>;
3788
3789 if (__first == __last)
3790 return _Ret{std::move(__first), _Up(std::move(__init))};
3791
3792 _Up __accum = std::__invoke(__f, std::move(__init), *__first);
3793 for (++__first; __first != __last; ++__first)
3794 __accum = std::__invoke(__f, std::move(__accum), *__first);
3795 return _Ret{std::move(__first), std::move(__accum)};
3796 }
3797
3798 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3799 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3800 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
3801 constexpr auto
3802 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3803 {
3804 using _Ret_iter = _Iter;
3805 return _S_impl<_Ret_iter>(std::move(__first), __last,
3806 std::move(__init), std::move(__f));
3807 }
3808
3809 template<input_range _Range,
3810 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3811 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
3812 constexpr auto
3813 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3814 {
3815 using _Ret_iter = borrowed_iterator_t<_Range>;
3816 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r),
3817 std::move(__init), std::move(__f));
3818 }
3819 };
3820
3821 inline constexpr __fold_left_with_iter_fn fold_left_with_iter{};
3822
3823 struct __fold_left_fn
3824 {
3825 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3826 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3827 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
3828 constexpr auto
3829 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3830 {
3831 return ranges::fold_left_with_iter(std::move(__first), __last,
3832 std::move(__init), std::move(__f)).value;
3833 }
3834
3835 template<input_range _Range,
3836 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3837 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
3838 constexpr auto
3839 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3840 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
3841 };
3842
3843 inline constexpr __fold_left_fn fold_left{};
3844
3845 template<typename _Iter, typename _Tp>
3846 using fold_left_first_with_iter_result = in_value_result<_Iter, _Tp>;
3847
3848 struct __fold_left_first_with_iter_fn
3849 {
3850 template<typename _Ret_iter, typename _Iter, typename _Sent, typename _Fp>
3851 static constexpr auto
3852 _S_impl(_Iter __first, _Sent __last, _Fp __f)
3853 {
3854 using _Up = decltype(ranges::fold_left(std::move(__first), __last,
3855 iter_value_t<_Iter>(*__first), __f));
3856 using _Ret = fold_left_first_with_iter_result<_Ret_iter, optional<_Up>>;
3857
3858 if (__first == __last)
3859 return _Ret{std::move(__first), optional<_Up>()};
3860
3861 optional<_Up> __init(in_place, *__first);
3862 for (++__first; __first != __last; ++__first)
3863 *__init = std::__invoke(__f, std::move(*__init), *__first);
3864 return _Ret{std::move(__first), std::move(__init)};
3865 }
3866
3867 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3868 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3869 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3870 constexpr auto
3871 operator()(_Iter __first, _Sent __last, _Fp __f) const
3872 {
3873 using _Ret_iter = _Iter;
3874 return _S_impl<_Ret_iter>(std::move(__first), __last, std::move(__f));
3875 }
3876
3877 template<input_range _Range,
3878 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3879 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3880 constexpr auto
3881 operator()(_Range&& __r, _Fp __f) const
3882 {
3883 using _Ret_iter = borrowed_iterator_t<_Range>;
3884 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r), std::move(__f));
3885 }
3886 };
3887
3888 inline constexpr __fold_left_first_with_iter_fn fold_left_first_with_iter{};
3889
3890 struct __fold_left_first_fn
3891 {
3892 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3893 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3894 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3895 constexpr auto
3896 operator()(_Iter __first, _Sent __last, _Fp __f) const
3897 {
3898 return ranges::fold_left_first_with_iter(std::move(__first), __last,
3899 std::move(__f)).value;
3900 }
3901
3902 template<input_range _Range,
3903 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3904 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3905 constexpr auto
3906 operator()(_Range&& __r, _Fp __f) const
3907 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
3908 };
3909
3910 inline constexpr __fold_left_first_fn fold_left_first{};
3911
3912 struct __fold_right_fn
3913 {
3914 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3915 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3916 __detail::__indirectly_binary_right_foldable<_Tp, _Iter> _Fp>
3917 constexpr auto
3918 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3919 {
3920 using _Up = decay_t<invoke_result_t<_Fp&, iter_reference_t<_Iter>, _Tp>>;
3921
3922 if (__first == __last)
3923 return _Up(std::move(__init));
3924
3925 _Iter __tail = ranges::next(__first, __last);
3926 _Up __accum = std::__invoke(__f, *--__tail, std::move(__init));
3927 while (__first != __tail)
3928 __accum = std::__invoke(__f, *--__tail, std::move(__accum));
3929 return __accum;
3930 }
3931
3932 template<bidirectional_range _Range,
3933 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3934 __detail::__indirectly_binary_right_foldable<_Tp, iterator_t<_Range>> _Fp>
3935 constexpr auto
3936 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3937 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
3938 };
3939
3940 inline constexpr __fold_right_fn fold_right{};
3941
3942 struct __fold_right_last_fn
3943 {
3944 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3945 __detail::__indirectly_binary_right_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3946 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3947 constexpr auto
3948 operator()(_Iter __first, _Sent __last, _Fp __f) const
3949 {
3950 using _Up = decltype(ranges::fold_right(__first, __last,
3951 iter_value_t<_Iter>(*__first), __f));
3952
3953 if (__first == __last)
3954 return optional<_Up>();
3955
3956 _Iter __tail = ranges::prev(ranges::next(__first, std::move(__last)));
3957 return optional<_Up>(in_place,
3958 ranges::fold_right(std::move(__first), __tail,
3959 iter_value_t<_Iter>(*__tail),
3960 std::move(__f)));
3961 }
3962
3963 template<bidirectional_range _Range,
3964 __detail::__indirectly_binary_right_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3965 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3966 constexpr auto
3967 operator()(_Range&& __r, _Fp __f) const
3968 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
3969 };
3970
3971 inline constexpr __fold_right_last_fn fold_right_last{};
3972#endif // __glibcxx_ranges_fold
3973} // namespace ranges
3974
3975 template<typename _ForwardIterator>
3976 constexpr _ForwardIterator
3977 shift_left(_ForwardIterator __first, _ForwardIterator __last,
3979 {
3980 __glibcxx_assert(__n >= 0);
3981 if (__n == 0)
3982 return __last;
3983
3984 auto __mid = ranges::next(__first, __n, __last);
3985 if (__mid == __last)
3986 return __first;
3987 return std::move(std::move(__mid), std::move(__last), std::move(__first));
3988 }
3989
3990 template<typename _ForwardIterator>
3991 constexpr _ForwardIterator
3992 shift_right(_ForwardIterator __first, _ForwardIterator __last,
3994 {
3995 __glibcxx_assert(__n >= 0);
3996 if (__n == 0)
3997 return __first;
3998
3999 using _Cat
4001 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
4002 {
4003 auto __mid = ranges::next(__last, -__n, __first);
4004 if (__mid == __first)
4005 return __last;
4006
4007 return std::move_backward(std::move(__first), std::move(__mid),
4008 std::move(__last));
4009 }
4010 else
4011 {
4012 auto __result = ranges::next(__first, __n, __last);
4013 if (__result == __last)
4014 return __last;
4015
4016 auto __dest_head = __first, __dest_tail = __result;
4017 while (__dest_head != __result)
4018 {
4019 if (__dest_tail == __last)
4020 {
4021 // If we get here, then we must have
4022 // 2*n >= distance(__first, __last)
4023 // i.e. we are shifting out at least half of the range. In
4024 // this case we can safely perform the shift with a single
4025 // move.
4026 std::move(std::move(__first), std::move(__dest_head), __result);
4027 return __result;
4028 }
4029 ++__dest_head;
4030 ++__dest_tail;
4031 }
4032
4033 for (;;)
4034 {
4035 // At the start of each iteration of this outer loop, the range
4036 // [__first, __result) contains those elements that after shifting
4037 // the whole range right by __n, should end up in
4038 // [__dest_head, __dest_tail) in order.
4039
4040 // The below inner loop swaps the elements of [__first, __result)
4041 // and [__dest_head, __dest_tail), while simultaneously shifting
4042 // the latter range by __n.
4043 auto __cursor = __first;
4044 while (__cursor != __result)
4045 {
4046 if (__dest_tail == __last)
4047 {
4048 // At this point the ranges [__first, result) and
4049 // [__dest_head, dest_tail) are disjoint, so we can safely
4050 // move the remaining elements.
4051 __dest_head = std::move(__cursor, __result,
4052 std::move(__dest_head));
4053 std::move(std::move(__first), std::move(__cursor),
4054 std::move(__dest_head));
4055 return __result;
4056 }
4057 std::iter_swap(__cursor, __dest_head);
4058 ++__dest_head;
4059 ++__dest_tail;
4060 ++__cursor;
4061 }
4062 }
4063 }
4064 }
4065
4066_GLIBCXX_END_NAMESPACE_VERSION
4067} // namespace std
4068#endif // concepts
4069#endif // C++20
4070#endif // _RANGES_ALGO_H
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1798
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2831
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
Traits class for iterators.