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 = ranges::iter_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 = ranges::iter_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 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1222 // 4269. unique_copy passes arguments to its predicate backwards
1223
1224 // TODO: perform a closer comparison with reference implementations
1225 if constexpr (forward_iterator<_Iter>)
1226 {
1227 auto __next = __first;
1228 *__result = *__next;
1229 while (++__next != __last)
1230 if (!std::__invoke(__comp,
1231 std::__invoke(__proj, *__first),
1232 std::__invoke(__proj, *__next)))
1233 {
1234 __first = __next;
1235 *++__result = *__first;
1236 }
1237 return {__next, std::move(++__result)};
1238 }
1239 else if constexpr (__detail::__can_reread_output<_Out, iter_value_t<_Iter>>)
1240 {
1241 *__result = *__first;
1242 while (++__first != __last)
1243 if (!std::__invoke(__comp,
1244 std::__invoke(__proj, *__result),
1245 std::__invoke(__proj, *__first)))
1246 *++__result = *__first;
1247 return {std::move(__first), std::move(++__result)};
1248 }
1249 else // indirectly_copyable_storable<_Iter, _Out>
1250 {
1251 auto __value = *__first;
1252 *__result = __value;
1253 while (++__first != __last)
1254 {
1255 if (!(bool)std::__invoke(__comp,
1256 std::__invoke(__proj, __value),
1257 std::__invoke(__proj, *__first)))
1258 {
1259 __value = *__first;
1260 *++__result = __value;
1261 }
1262 }
1263 return {std::move(__first), std::move(++__result)};
1264 }
1265 }
1266
1267 template<input_range _Range,
1268 weakly_incrementable _Out, typename _Proj = identity,
1269 indirect_equivalence_relation<
1270 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1271 requires indirectly_copyable<iterator_t<_Range>, _Out>
1272 && (forward_iterator<iterator_t<_Range>>
1273 || __detail::__can_reread_output<_Out, range_value_t<_Range>>
1274 || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
1275 constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
1276 operator()(_Range&& __r, _Out __result,
1277 _Comp __comp = {}, _Proj __proj = {}) const
1278 {
1279 return (*this)(ranges::begin(__r), ranges::end(__r),
1280 std::move(__result),
1281 std::move(__comp), std::move(__proj));
1282 }
1283 };
1284
1285 inline constexpr __unique_copy_fn unique_copy{};
1286
1287 struct __reverse_fn
1288 {
1289 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
1290 requires permutable<_Iter>
1291 constexpr _Iter
1292 operator()(_Iter __first, _Sent __last) const
1293 {
1294 auto __i = ranges::next(__first, __last);
1295 auto __tail = __i;
1296
1297 if constexpr (random_access_iterator<_Iter>)
1298 {
1299 if (__first != __last)
1300 {
1301 --__tail;
1302 while (__first < __tail)
1303 {
1304 ranges::iter_swap(__first, __tail);
1305 ++__first;
1306 --__tail;
1307 }
1308 }
1309 return __i;
1310 }
1311 else
1312 {
1313 for (;;)
1314 if (__first == __tail || __first == --__tail)
1315 break;
1316 else
1317 {
1318 ranges::iter_swap(__first, __tail);
1319 ++__first;
1320 }
1321 return __i;
1322 }
1323 }
1324
1325 template<bidirectional_range _Range>
1326 requires permutable<iterator_t<_Range>>
1327 constexpr borrowed_iterator_t<_Range>
1328 operator()(_Range&& __r) const
1329 {
1330 return (*this)(ranges::begin(__r), ranges::end(__r));
1331 }
1332 };
1333
1334 inline constexpr __reverse_fn reverse{};
1335
1336 template<typename _Iter, typename _Out>
1337 using reverse_copy_result = in_out_result<_Iter, _Out>;
1338
1339 struct __reverse_copy_fn
1340 {
1341 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
1342 weakly_incrementable _Out>
1343 requires indirectly_copyable<_Iter, _Out>
1344 constexpr reverse_copy_result<_Iter, _Out>
1345 operator()(_Iter __first, _Sent __last, _Out __result) const
1346 {
1347 auto __i = ranges::next(__first, __last);
1348 auto __tail = __i;
1349 while (__first != __tail)
1350 {
1351 --__tail;
1352 *__result = *__tail;
1353 ++__result;
1354 }
1355 return {__i, std::move(__result)};
1356 }
1357
1358 template<bidirectional_range _Range, weakly_incrementable _Out>
1359 requires indirectly_copyable<iterator_t<_Range>, _Out>
1360 constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
1361 operator()(_Range&& __r, _Out __result) const
1362 {
1363 return (*this)(ranges::begin(__r), ranges::end(__r),
1364 std::move(__result));
1365 }
1366 };
1367
1368 inline constexpr __reverse_copy_fn reverse_copy{};
1369
1370 struct __rotate_fn
1371 {
1372 template<permutable _Iter, sentinel_for<_Iter> _Sent>
1373 constexpr subrange<_Iter>
1374 operator()(_Iter __first, _Iter __middle, _Sent __last) const
1375 {
1376 auto __lasti = ranges::next(__first, __last);
1377 if (__first == __middle)
1378 return {__lasti, __lasti};
1379 if (__last == __middle)
1380 return {std::move(__first), std::move(__lasti)};
1381
1382 if constexpr (random_access_iterator<_Iter>)
1383 {
1384 auto __n = __lasti - __first;
1385 auto __k = __middle - __first;
1386
1387 if (__k == __n - __k)
1388 {
1389 ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
1390 return {std::move(__middle), std::move(__lasti)};
1391 }
1392
1393 auto __p = __first;
1394 auto __ret = __first + (__lasti - __middle);
1395
1396 for (;;)
1397 {
1398 if (__k < __n - __k)
1399 {
1400 // TODO: is_pod is deprecated, but this condition is
1401 // consistent with the STL implementation.
1402 if constexpr (__is_pod(iter_value_t<_Iter>))
1403 if (__k == 1)
1404 {
1405 auto __t = std::move(*__p);
1406 ranges::move(__p + 1, __p + __n, __p);
1407 *(__p + __n - 1) = std::move(__t);
1408 return {std::move(__ret), std::move(__lasti)};
1409 }
1410 auto __q = __p + __k;
1411 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1412 {
1413 ranges::iter_swap(__p, __q);
1414 ++__p;
1415 ++__q;
1416 }
1417 __n %= __k;
1418 if (__n == 0)
1419 return {std::move(__ret), std::move(__lasti)};
1420 ranges::swap(__n, __k);
1421 __k = __n - __k;
1422 }
1423 else
1424 {
1425 __k = __n - __k;
1426 // TODO: is_pod is deprecated, but this condition is
1427 // consistent with the STL implementation.
1428 if constexpr (__is_pod(iter_value_t<_Iter>))
1429 if (__k == 1)
1430 {
1431 auto __t = std::move(*(__p + __n - 1));
1432 ranges::move_backward(__p, __p + __n - 1, __p + __n);
1433 *__p = std::move(__t);
1434 return {std::move(__ret), std::move(__lasti)};
1435 }
1436 auto __q = __p + __n;
1437 __p = __q - __k;
1438 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1439 {
1440 --__p;
1441 --__q;
1442 ranges::iter_swap(__p, __q);
1443 }
1444 __n %= __k;
1445 if (__n == 0)
1446 return {std::move(__ret), std::move(__lasti)};
1447 std::swap(__n, __k);
1448 }
1449 }
1450 }
1451 else if constexpr (bidirectional_iterator<_Iter>)
1452 {
1453 auto __tail = __lasti;
1454
1455 ranges::reverse(__first, __middle);
1456 ranges::reverse(__middle, __tail);
1457
1458 while (__first != __middle && __middle != __tail)
1459 {
1460 ranges::iter_swap(__first, --__tail);
1461 ++__first;
1462 }
1463
1464 if (__first == __middle)
1465 {
1466 ranges::reverse(__middle, __tail);
1467 return {std::move(__tail), std::move(__lasti)};
1468 }
1469 else
1470 {
1471 ranges::reverse(__first, __middle);
1472 return {std::move(__first), std::move(__lasti)};
1473 }
1474 }
1475 else
1476 {
1477 auto __first2 = __middle;
1478 do
1479 {
1480 ranges::iter_swap(__first, __first2);
1481 ++__first;
1482 ++__first2;
1483 if (__first == __middle)
1484 __middle = __first2;
1485 } while (__first2 != __last);
1486
1487 auto __ret = __first;
1488
1489 __first2 = __middle;
1490
1491 while (__first2 != __last)
1492 {
1493 ranges::iter_swap(__first, __first2);
1494 ++__first;
1495 ++__first2;
1496 if (__first == __middle)
1497 __middle = __first2;
1498 else if (__first2 == __last)
1499 __first2 = __middle;
1500 }
1501 return {std::move(__ret), std::move(__lasti)};
1502 }
1503 }
1504
1505 template<forward_range _Range>
1506 requires permutable<iterator_t<_Range>>
1507 constexpr borrowed_subrange_t<_Range>
1508 operator()(_Range&& __r, iterator_t<_Range> __middle) const
1509 {
1510 return (*this)(ranges::begin(__r), std::move(__middle),
1511 ranges::end(__r));
1512 }
1513 };
1514
1515 inline constexpr __rotate_fn rotate{};
1516
1517 template<typename _Iter, typename _Out>
1518 using rotate_copy_result = in_out_result<_Iter, _Out>;
1519
1520 struct __rotate_copy_fn
1521 {
1522 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1523 weakly_incrementable _Out>
1524 requires indirectly_copyable<_Iter, _Out>
1525 constexpr rotate_copy_result<_Iter, _Out>
1526 operator()(_Iter __first, _Iter __middle, _Sent __last,
1527 _Out __result) const
1528 {
1529 auto __copy1 = ranges::copy(__middle,
1530 std::move(__last),
1531 std::move(__result));
1532 auto __copy2 = ranges::copy(std::move(__first),
1533 std::move(__middle),
1534 std::move(__copy1.out));
1535 return { std::move(__copy1.in), std::move(__copy2.out) };
1536 }
1537
1538 template<forward_range _Range, weakly_incrementable _Out>
1539 requires indirectly_copyable<iterator_t<_Range>, _Out>
1540 constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
1541 operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
1542 {
1543 return (*this)(ranges::begin(__r), std::move(__middle),
1544 ranges::end(__r), std::move(__result));
1545 }
1546 };
1547
1548 inline constexpr __rotate_copy_fn rotate_copy{};
1549
1550 struct __sample_fn
1551 {
1552 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1553 weakly_incrementable _Out, typename _Gen>
1554 requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
1555 && indirectly_copyable<_Iter, _Out>
1556 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1557 _Out
1558 operator()(_Iter __first, _Sent __last, _Out __out,
1559 iter_difference_t<_Iter> __n, _Gen&& __g) const
1560 {
1561 if constexpr (forward_iterator<_Iter>)
1562 {
1563 // FIXME: Forwarding to std::sample here requires computing __lasti
1564 // which may take linear time.
1565 auto __lasti = ranges::next(__first, __last);
1566 return _GLIBCXX_STD_A::
1567 sample(std::move(__first), std::move(__lasti), std::move(__out),
1568 __n, std::forward<_Gen>(__g));
1569 }
1570 else
1571 {
1572 using __distrib_type
1573 = uniform_int_distribution<iter_difference_t<_Iter>>;
1574 using __param_type = typename __distrib_type::param_type;
1575 __distrib_type __d{};
1576 iter_difference_t<_Iter> __sample_sz = 0;
1577 while (__first != __last && __sample_sz != __n)
1578 {
1579 __out[__sample_sz++] = *__first;
1580 ++__first;
1581 }
1582 for (auto __pop_sz = __sample_sz; __first != __last;
1583 ++__first, (void) ++__pop_sz)
1584 {
1585 const auto __k = __d(__g, __param_type{0, __pop_sz});
1586 if (__k < __n)
1587 __out[__k] = *__first;
1588 }
1589 return __out + __sample_sz;
1590 }
1591 }
1592
1593 template<input_range _Range, weakly_incrementable _Out, typename _Gen>
1594 requires (forward_range<_Range> || random_access_iterator<_Out>)
1595 && indirectly_copyable<iterator_t<_Range>, _Out>
1596 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1597 _Out
1598 operator()(_Range&& __r, _Out __out,
1599 range_difference_t<_Range> __n, _Gen&& __g) const
1600 {
1601 return (*this)(ranges::begin(__r), ranges::end(__r),
1602 std::move(__out), __n,
1603 std::forward<_Gen>(__g));
1604 }
1605 };
1606
1607 inline constexpr __sample_fn sample{};
1608
1609 struct __shuffle_fn
1610 {
1611 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1612 typename _Gen>
1613 requires permutable<_Iter>
1614 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1615 _Iter
1616 operator()(_Iter __first, _Sent __last, _Gen&& __g) const
1617 {
1618 auto __lasti = ranges::next(__first, __last);
1619 std::shuffle(std::move(__first), __lasti, std::forward<_Gen>(__g));
1620 return __lasti;
1621 }
1622
1623 template<random_access_range _Range, typename _Gen>
1624 requires permutable<iterator_t<_Range>>
1625 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1626 borrowed_iterator_t<_Range>
1627 operator()(_Range&& __r, _Gen&& __g) const
1628 {
1629 return (*this)(ranges::begin(__r), ranges::end(__r),
1630 std::forward<_Gen>(__g));
1631 }
1632 };
1633
1634 inline constexpr __shuffle_fn shuffle{};
1635
1636 struct __push_heap_fn
1637 {
1638 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1639 typename _Comp = ranges::less, typename _Proj = identity>
1640 requires sortable<_Iter, _Comp, _Proj>
1641 constexpr _Iter
1642 operator()(_Iter __first, _Sent __last,
1643 _Comp __comp = {}, _Proj __proj = {}) const
1644 {
1645 auto __lasti = ranges::next(__first, __last);
1646 std::push_heap(__first, __lasti,
1647 __detail::__make_comp_proj(__comp, __proj));
1648 return __lasti;
1649 }
1650
1651 template<random_access_range _Range,
1652 typename _Comp = ranges::less, typename _Proj = identity>
1653 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1654 constexpr borrowed_iterator_t<_Range>
1655 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1656 {
1657 return (*this)(ranges::begin(__r), ranges::end(__r),
1658 std::move(__comp), std::move(__proj));
1659 }
1660 };
1661
1662 inline constexpr __push_heap_fn push_heap{};
1663
1664 struct __pop_heap_fn
1665 {
1666 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1667 typename _Comp = ranges::less, typename _Proj = identity>
1668 requires sortable<_Iter, _Comp, _Proj>
1669 constexpr _Iter
1670 operator()(_Iter __first, _Sent __last,
1671 _Comp __comp = {}, _Proj __proj = {}) const
1672 {
1673 auto __lasti = ranges::next(__first, __last);
1674 std::pop_heap(__first, __lasti,
1675 __detail::__make_comp_proj(__comp, __proj));
1676 return __lasti;
1677 }
1678
1679 template<random_access_range _Range,
1680 typename _Comp = ranges::less, typename _Proj = identity>
1681 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1682 constexpr borrowed_iterator_t<_Range>
1683 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1684 {
1685 return (*this)(ranges::begin(__r), ranges::end(__r),
1686 std::move(__comp), std::move(__proj));
1687 }
1688 };
1689
1690 inline constexpr __pop_heap_fn pop_heap{};
1691
1692 struct __make_heap_fn
1693 {
1694 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1695 typename _Comp = ranges::less, typename _Proj = identity>
1696 requires sortable<_Iter, _Comp, _Proj>
1697 constexpr _Iter
1698 operator()(_Iter __first, _Sent __last,
1699 _Comp __comp = {}, _Proj __proj = {}) const
1700 {
1701 auto __lasti = ranges::next(__first, __last);
1702 std::make_heap(__first, __lasti,
1703 __detail::__make_comp_proj(__comp, __proj));
1704 return __lasti;
1705 }
1706
1707 template<random_access_range _Range,
1708 typename _Comp = ranges::less, typename _Proj = identity>
1709 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1710 constexpr borrowed_iterator_t<_Range>
1711 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1712 {
1713 return (*this)(ranges::begin(__r), ranges::end(__r),
1714 std::move(__comp), std::move(__proj));
1715 }
1716 };
1717
1718 inline constexpr __make_heap_fn make_heap{};
1719
1720 struct __sort_heap_fn
1721 {
1722 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1723 typename _Comp = ranges::less, typename _Proj = identity>
1724 requires sortable<_Iter, _Comp, _Proj>
1725 constexpr _Iter
1726 operator()(_Iter __first, _Sent __last,
1727 _Comp __comp = {}, _Proj __proj = {}) const
1728 {
1729 auto __lasti = ranges::next(__first, __last);
1730 std::sort_heap(__first, __lasti,
1731 __detail::__make_comp_proj(__comp, __proj));
1732 return __lasti;
1733 }
1734
1735 template<random_access_range _Range,
1736 typename _Comp = ranges::less, typename _Proj = identity>
1737 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1738 constexpr borrowed_iterator_t<_Range>
1739 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1740 {
1741 return (*this)(ranges::begin(__r), ranges::end(__r),
1742 std::move(__comp), std::move(__proj));
1743 }
1744 };
1745
1746 inline constexpr __sort_heap_fn sort_heap{};
1747
1748 struct __is_heap_until_fn
1749 {
1750 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1751 typename _Proj = identity,
1752 indirect_strict_weak_order<projected<_Iter, _Proj>>
1753 _Comp = ranges::less>
1754 constexpr _Iter
1755 operator()(_Iter __first, _Sent __last,
1756 _Comp __comp = {}, _Proj __proj = {}) const
1757 {
1758 iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
1759 iter_difference_t<_Iter> __parent = 0, __child = 1;
1760 for (; __child < __n; ++__child)
1761 if (std::__invoke(__comp,
1762 std::__invoke(__proj, *(__first + __parent)),
1763 std::__invoke(__proj, *(__first + __child))))
1764 return __first + __child;
1765 else if ((__child & 1) == 0)
1766 ++__parent;
1767
1768 return __first + __n;
1769 }
1770
1771 template<random_access_range _Range,
1772 typename _Proj = identity,
1773 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1774 _Comp = ranges::less>
1775 constexpr borrowed_iterator_t<_Range>
1776 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1777 {
1778 return (*this)(ranges::begin(__r), ranges::end(__r),
1779 std::move(__comp), std::move(__proj));
1780 }
1781 };
1782
1783 inline constexpr __is_heap_until_fn is_heap_until{};
1784
1785 struct __is_heap_fn
1786 {
1787 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1788 typename _Proj = identity,
1789 indirect_strict_weak_order<projected<_Iter, _Proj>>
1790 _Comp = ranges::less>
1791 constexpr bool
1792 operator()(_Iter __first, _Sent __last,
1793 _Comp __comp = {}, _Proj __proj = {}) const
1794 {
1795 return (__last
1796 == ranges::is_heap_until(__first, __last,
1797 std::move(__comp),
1798 std::move(__proj)));
1799 }
1800
1801 template<random_access_range _Range,
1802 typename _Proj = identity,
1803 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1804 _Comp = ranges::less>
1805 constexpr bool
1806 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1807 {
1808 return (*this)(ranges::begin(__r), ranges::end(__r),
1809 std::move(__comp), std::move(__proj));
1810 }
1811 };
1812
1813 inline constexpr __is_heap_fn is_heap{};
1814
1815 struct __sort_fn
1816 {
1817 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1818 typename _Comp = ranges::less, typename _Proj = identity>
1819 requires sortable<_Iter, _Comp, _Proj>
1820 constexpr _Iter
1821 operator()(_Iter __first, _Sent __last,
1822 _Comp __comp = {}, _Proj __proj = {}) const
1823 {
1824 auto __lasti = ranges::next(__first, __last);
1825 _GLIBCXX_STD_A::sort(std::move(__first), __lasti,
1826 __detail::__make_comp_proj(__comp, __proj));
1827 return __lasti;
1828 }
1829
1830 template<random_access_range _Range,
1831 typename _Comp = ranges::less, typename _Proj = identity>
1832 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1833 constexpr borrowed_iterator_t<_Range>
1834 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1835 {
1836 return (*this)(ranges::begin(__r), ranges::end(__r),
1837 std::move(__comp), std::move(__proj));
1838 }
1839 };
1840
1841 inline constexpr __sort_fn sort{};
1842
1843 struct __stable_sort_fn
1844 {
1845 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1846 typename _Comp = ranges::less, typename _Proj = identity>
1847 requires sortable<_Iter, _Comp, _Proj>
1848 _GLIBCXX26_CONSTEXPR
1849 _Iter
1850 operator()(_Iter __first, _Sent __last,
1851 _Comp __comp = {}, _Proj __proj = {}) const
1852 {
1853 auto __lasti = ranges::next(__first, __last);
1854 std::stable_sort(std::move(__first), __lasti,
1855 __detail::__make_comp_proj(__comp, __proj));
1856 return __lasti;
1857 }
1858
1859 template<random_access_range _Range,
1860 typename _Comp = ranges::less, typename _Proj = identity>
1861 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1862 _GLIBCXX26_CONSTEXPR
1863 borrowed_iterator_t<_Range>
1864 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1865 {
1866 return (*this)(ranges::begin(__r), ranges::end(__r),
1867 std::move(__comp), std::move(__proj));
1868 }
1869 };
1870
1871 inline constexpr __stable_sort_fn stable_sort{};
1872
1873 struct __partial_sort_fn
1874 {
1875 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1876 typename _Comp = ranges::less, typename _Proj = identity>
1877 requires sortable<_Iter, _Comp, _Proj>
1878 constexpr _Iter
1879 operator()(_Iter __first, _Iter __middle, _Sent __last,
1880 _Comp __comp = {}, _Proj __proj = {}) const
1881 {
1882 if (__first == __middle)
1883 return ranges::next(__first, __last);
1884
1885 ranges::make_heap(__first, __middle, __comp, __proj);
1886 auto __i = __middle;
1887 for (; __i != __last; ++__i)
1888 if (std::__invoke(__comp,
1889 std::__invoke(__proj, *__i),
1890 std::__invoke(__proj, *__first)))
1891 {
1892 ranges::pop_heap(__first, __middle, __comp, __proj);
1893 ranges::iter_swap(__middle-1, __i);
1894 ranges::push_heap(__first, __middle, __comp, __proj);
1895 }
1896 ranges::sort_heap(__first, __middle, __comp, __proj);
1897
1898 return __i;
1899 }
1900
1901 template<random_access_range _Range,
1902 typename _Comp = ranges::less, typename _Proj = identity>
1903 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1904 constexpr borrowed_iterator_t<_Range>
1905 operator()(_Range&& __r, iterator_t<_Range> __middle,
1906 _Comp __comp = {}, _Proj __proj = {}) const
1907 {
1908 return (*this)(ranges::begin(__r), std::move(__middle),
1909 ranges::end(__r),
1910 std::move(__comp), std::move(__proj));
1911 }
1912 };
1913
1914 inline constexpr __partial_sort_fn partial_sort{};
1915
1916 template<typename _Iter, typename _Out>
1917 using partial_sort_copy_result = in_out_result<_Iter, _Out>;
1918
1919 struct __partial_sort_copy_fn
1920 {
1921 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
1922 random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
1923 typename _Comp = ranges::less,
1924 typename _Proj1 = identity, typename _Proj2 = identity>
1925 requires indirectly_copyable<_Iter1, _Iter2>
1926 && sortable<_Iter2, _Comp, _Proj2>
1927 && indirect_strict_weak_order<_Comp,
1928 projected<_Iter1, _Proj1>,
1929 projected<_Iter2, _Proj2>>
1930 constexpr partial_sort_copy_result<_Iter1, _Iter2>
1931 operator()(_Iter1 __first, _Sent1 __last,
1932 _Iter2 __result_first, _Sent2 __result_last,
1933 _Comp __comp = {},
1934 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1935 {
1936 if (__result_first == __result_last)
1937 {
1938 // TODO: Eliminating the variable __lasti triggers an ICE.
1939 auto __lasti = ranges::next(std::move(__first),
1940 std::move(__last));
1941 return {std::move(__lasti), std::move(__result_first)};
1942 }
1943
1944 auto __result_real_last = __result_first;
1945 while (__first != __last && __result_real_last != __result_last)
1946 {
1947 *__result_real_last = *__first;
1948 ++__result_real_last;
1949 ++__first;
1950 }
1951
1952 ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
1953 for (; __first != __last; ++__first)
1954 if (std::__invoke(__comp,
1955 std::__invoke(__proj1, *__first),
1956 std::__invoke(__proj2, *__result_first)))
1957 {
1958 ranges::pop_heap(__result_first, __result_real_last,
1959 __comp, __proj2);
1960 *(__result_real_last-1) = *__first;
1961 ranges::push_heap(__result_first, __result_real_last,
1962 __comp, __proj2);
1963 }
1964 ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
1965
1966 return {std::move(__first), std::move(__result_real_last)};
1967 }
1968
1969 template<input_range _Range1, random_access_range _Range2,
1970 typename _Comp = ranges::less,
1971 typename _Proj1 = identity, typename _Proj2 = identity>
1972 requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
1973 && sortable<iterator_t<_Range2>, _Comp, _Proj2>
1974 && indirect_strict_weak_order<_Comp,
1975 projected<iterator_t<_Range1>, _Proj1>,
1976 projected<iterator_t<_Range2>, _Proj2>>
1977 constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
1978 borrowed_iterator_t<_Range2>>
1979 operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
1980 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1981 {
1982 return (*this)(ranges::begin(__r), ranges::end(__r),
1983 ranges::begin(__out), ranges::end(__out),
1984 std::move(__comp),
1985 std::move(__proj1), std::move(__proj2));
1986 }
1987 };
1988
1989 inline constexpr __partial_sort_copy_fn partial_sort_copy{};
1990
1991 struct __is_sorted_until_fn
1992 {
1993 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1994 typename _Proj = identity,
1995 indirect_strict_weak_order<projected<_Iter, _Proj>>
1996 _Comp = ranges::less>
1997 constexpr _Iter
1998 operator()(_Iter __first, _Sent __last,
1999 _Comp __comp = {}, _Proj __proj = {}) const
2000 {
2001 if (__first == __last)
2002 return __first;
2003
2004 auto __next = __first;
2005 for (++__next; __next != __last; __first = __next, (void)++__next)
2006 if (std::__invoke(__comp,
2007 std::__invoke(__proj, *__next),
2008 std::__invoke(__proj, *__first)))
2009 return __next;
2010 return __next;
2011 }
2012
2013 template<forward_range _Range, typename _Proj = identity,
2014 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2015 _Comp = ranges::less>
2016 constexpr borrowed_iterator_t<_Range>
2017 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2018 {
2019 return (*this)(ranges::begin(__r), ranges::end(__r),
2020 std::move(__comp), std::move(__proj));
2021 }
2022 };
2023
2024 inline constexpr __is_sorted_until_fn is_sorted_until{};
2025
2026 struct __is_sorted_fn
2027 {
2028 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2029 typename _Proj = identity,
2030 indirect_strict_weak_order<projected<_Iter, _Proj>>
2031 _Comp = ranges::less>
2032 constexpr bool
2033 operator()(_Iter __first, _Sent __last,
2034 _Comp __comp = {}, _Proj __proj = {}) const
2035 {
2036 if (__first == __last)
2037 return true;
2038
2039 auto __next = __first;
2040 for (++__next; __next != __last; __first = __next, (void)++__next)
2041 if (std::__invoke(__comp,
2042 std::__invoke(__proj, *__next),
2043 std::__invoke(__proj, *__first)))
2044 return false;
2045 return true;
2046 }
2047
2048 template<forward_range _Range, typename _Proj = identity,
2049 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2050 _Comp = ranges::less>
2051 constexpr bool
2052 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2053 {
2054 return (*this)(ranges::begin(__r), ranges::end(__r),
2055 std::move(__comp), std::move(__proj));
2056 }
2057 };
2058
2059 inline constexpr __is_sorted_fn is_sorted{};
2060
2061 struct __nth_element_fn
2062 {
2063 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2064 typename _Comp = ranges::less, typename _Proj = identity>
2065 requires sortable<_Iter, _Comp, _Proj>
2066 constexpr _Iter
2067 operator()(_Iter __first, _Iter __nth, _Sent __last,
2068 _Comp __comp = {}, _Proj __proj = {}) const
2069 {
2070 auto __lasti = ranges::next(__first, __last);
2071 _GLIBCXX_STD_A::nth_element(std::move(__first), std::move(__nth),
2072 __lasti,
2073 __detail::__make_comp_proj(__comp, __proj));
2074 return __lasti;
2075 }
2076
2077 template<random_access_range _Range,
2078 typename _Comp = ranges::less, typename _Proj = identity>
2079 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2080 constexpr borrowed_iterator_t<_Range>
2081 operator()(_Range&& __r, iterator_t<_Range> __nth,
2082 _Comp __comp = {}, _Proj __proj = {}) const
2083 {
2084 return (*this)(ranges::begin(__r), std::move(__nth),
2085 ranges::end(__r), std::move(__comp), std::move(__proj));
2086 }
2087 };
2088
2089 inline constexpr __nth_element_fn nth_element{};
2090
2091 struct __lower_bound_fn
2092 {
2093 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2094 typename _Proj = identity,
2095 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2096 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2097 _Comp = ranges::less>
2098 constexpr _Iter
2099 operator()(_Iter __first, _Sent __last,
2100 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2101 {
2102 auto __len = ranges::distance(__first, __last);
2103
2104 while (__len > 0)
2105 {
2106 auto __half = __len / 2;
2107 auto __middle = __first;
2108 ranges::advance(__middle, __half);
2109 if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
2110 {
2111 __first = __middle;
2112 ++__first;
2113 __len = __len - __half - 1;
2114 }
2115 else
2116 __len = __half;
2117 }
2118 return __first;
2119 }
2120
2121 template<forward_range _Range,
2122 typename _Proj = identity,
2123 typename _Tp
2124 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2125 indirect_strict_weak_order<const _Tp*,
2126 projected<iterator_t<_Range>, _Proj>>
2127 _Comp = ranges::less>
2128 constexpr borrowed_iterator_t<_Range>
2129 operator()(_Range&& __r,
2130 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2131 {
2132 return (*this)(ranges::begin(__r), ranges::end(__r),
2133 __value, std::move(__comp), std::move(__proj));
2134 }
2135 };
2136
2137 inline constexpr __lower_bound_fn lower_bound{};
2138
2139 struct __upper_bound_fn
2140 {
2141 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2142 typename _Proj = identity,
2143 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2144 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2145 _Comp = ranges::less>
2146 constexpr _Iter
2147 operator()(_Iter __first, _Sent __last,
2148 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2149 {
2150 auto __len = ranges::distance(__first, __last);
2151
2152 while (__len > 0)
2153 {
2154 auto __half = __len / 2;
2155 auto __middle = __first;
2156 ranges::advance(__middle, __half);
2157 if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
2158 __len = __half;
2159 else
2160 {
2161 __first = __middle;
2162 ++__first;
2163 __len = __len - __half - 1;
2164 }
2165 }
2166 return __first;
2167 }
2168
2169 template<forward_range _Range,
2170 typename _Proj = identity,
2171 typename _Tp
2172 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2173 indirect_strict_weak_order<const _Tp*,
2174 projected<iterator_t<_Range>, _Proj>>
2175 _Comp = ranges::less>
2176 constexpr borrowed_iterator_t<_Range>
2177 operator()(_Range&& __r,
2178 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2179 {
2180 return (*this)(ranges::begin(__r), ranges::end(__r),
2181 __value, std::move(__comp), std::move(__proj));
2182 }
2183 };
2184
2185 inline constexpr __upper_bound_fn upper_bound{};
2186
2187 struct __equal_range_fn
2188 {
2189 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2190 typename _Proj = identity,
2191 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2192 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2193 _Comp = ranges::less>
2194 constexpr subrange<_Iter>
2195 operator()(_Iter __first, _Sent __last,
2196 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2197 {
2198 auto __len = ranges::distance(__first, __last);
2199
2200 while (__len > 0)
2201 {
2202 auto __half = __len / 2;
2203 auto __middle = __first;
2204 ranges::advance(__middle, __half);
2205 if (std::__invoke(__comp,
2206 std::__invoke(__proj, *__middle),
2207 __value))
2208 {
2209 __first = __middle;
2210 ++__first;
2211 __len = __len - __half - 1;
2212 }
2213 else if (std::__invoke(__comp,
2214 __value,
2215 std::__invoke(__proj, *__middle)))
2216 __len = __half;
2217 else
2218 {
2219 auto __left
2220 = ranges::lower_bound(__first, __middle,
2221 __value, __comp, __proj);
2222 ranges::advance(__first, __len);
2223 auto __right
2224 = ranges::upper_bound(++__middle, __first,
2225 __value, __comp, __proj);
2226 return {__left, __right};
2227 }
2228 }
2229 return {__first, __first};
2230 }
2231
2232 template<forward_range _Range,
2233 typename _Proj = identity,
2234 typename _Tp
2235 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2236 indirect_strict_weak_order<const _Tp*,
2237 projected<iterator_t<_Range>, _Proj>>
2238 _Comp = ranges::less>
2239 constexpr borrowed_subrange_t<_Range>
2240 operator()(_Range&& __r, const _Tp& __value,
2241 _Comp __comp = {}, _Proj __proj = {}) const
2242 {
2243 return (*this)(ranges::begin(__r), ranges::end(__r),
2244 __value, std::move(__comp), std::move(__proj));
2245 }
2246 };
2247
2248 inline constexpr __equal_range_fn equal_range{};
2249
2250 struct __binary_search_fn
2251 {
2252 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2253 typename _Proj = identity,
2254 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2255 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2256 _Comp = ranges::less>
2257 constexpr bool
2258 operator()(_Iter __first, _Sent __last,
2259 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2260 {
2261 auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
2262 if (__i == __last)
2263 return false;
2264 return !(bool)std::__invoke(__comp, __value,
2265 std::__invoke(__proj, *__i));
2266 }
2267
2268 template<forward_range _Range,
2269 typename _Proj = identity,
2270 typename _Tp
2271 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
2272 indirect_strict_weak_order<const _Tp*,
2273 projected<iterator_t<_Range>, _Proj>>
2274 _Comp = ranges::less>
2275 constexpr bool
2276 operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
2277 _Proj __proj = {}) const
2278 {
2279 return (*this)(ranges::begin(__r), ranges::end(__r),
2280 __value, std::move(__comp), std::move(__proj));
2281 }
2282 };
2283
2284 inline constexpr __binary_search_fn binary_search{};
2285
2286 struct __is_partitioned_fn
2287 {
2288 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2289 typename _Proj = identity,
2290 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2291 constexpr bool
2292 operator()(_Iter __first, _Sent __last,
2293 _Pred __pred, _Proj __proj = {}) const
2294 {
2295 __first = ranges::find_if_not(std::move(__first), __last,
2296 __pred, __proj);
2297 if (__first == __last)
2298 return true;
2299 ++__first;
2300 return ranges::none_of(std::move(__first), std::move(__last),
2301 std::move(__pred), std::move(__proj));
2302 }
2303
2304 template<input_range _Range, typename _Proj = identity,
2305 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2306 _Pred>
2307 constexpr bool
2308 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2309 {
2310 return (*this)(ranges::begin(__r), ranges::end(__r),
2311 std::move(__pred), std::move(__proj));
2312 }
2313 };
2314
2315 inline constexpr __is_partitioned_fn is_partitioned{};
2316
2317 struct __partition_fn
2318 {
2319 template<permutable _Iter, sentinel_for<_Iter> _Sent,
2320 typename _Proj = identity,
2321 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2322 constexpr subrange<_Iter>
2323 operator()(_Iter __first, _Sent __last,
2324 _Pred __pred, _Proj __proj = {}) const
2325 {
2326 if constexpr (bidirectional_iterator<_Iter>)
2327 {
2328 auto __lasti = ranges::next(__first, __last);
2329 auto __tail = __lasti;
2330 for (;;)
2331 {
2332 for (;;)
2333 if (__first == __tail)
2334 return {std::move(__first), std::move(__lasti)};
2335 else if (std::__invoke(__pred,
2336 std::__invoke(__proj, *__first)))
2337 ++__first;
2338 else
2339 break;
2340 --__tail;
2341 for (;;)
2342 if (__first == __tail)
2343 return {std::move(__first), std::move(__lasti)};
2344 else if (!(bool)std::__invoke(__pred,
2345 std::__invoke(__proj, *__tail)))
2346 --__tail;
2347 else
2348 break;
2349 ranges::iter_swap(__first, __tail);
2350 ++__first;
2351 }
2352 }
2353 else
2354 {
2355 if (__first == __last)
2356 return {__first, __first};
2357
2358 while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
2359 if (++__first == __last)
2360 return {__first, __first};
2361
2362 auto __next = __first;
2363 while (++__next != __last)
2364 if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
2365 {
2366 ranges::iter_swap(__first, __next);
2367 ++__first;
2368 }
2369
2370 return {std::move(__first), std::move(__next)};
2371 }
2372 }
2373
2374 template<forward_range _Range, typename _Proj = identity,
2375 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2376 _Pred>
2377 requires permutable<iterator_t<_Range>>
2378 constexpr borrowed_subrange_t<_Range>
2379 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2380 {
2381 return (*this)(ranges::begin(__r), ranges::end(__r),
2382 std::move(__pred), std::move(__proj));
2383 }
2384 };
2385
2386 inline constexpr __partition_fn partition{};
2387
2388#if _GLIBCXX_HOSTED
2389 struct __stable_partition_fn
2390 {
2391 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2392 typename _Proj = identity,
2393 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2394 requires permutable<_Iter>
2395 _GLIBCXX26_CONSTEXPR
2396 subrange<_Iter>
2397 operator()(_Iter __first, _Sent __last,
2398 _Pred __pred, _Proj __proj = {}) const
2399 {
2400 auto __lasti = ranges::next(__first, __last);
2401 auto __middle
2402 = std::stable_partition(std::move(__first), __lasti,
2403 __detail::__make_pred_proj(__pred, __proj));
2404 return {std::move(__middle), std::move(__lasti)};
2405 }
2406
2407 template<bidirectional_range _Range, typename _Proj = identity,
2408 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2409 _Pred>
2410 requires permutable<iterator_t<_Range>>
2411 _GLIBCXX26_CONSTEXPR
2412 borrowed_subrange_t<_Range>
2413 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2414 {
2415 return (*this)(ranges::begin(__r), ranges::end(__r),
2416 std::move(__pred), std::move(__proj));
2417 }
2418 };
2419
2420 inline constexpr __stable_partition_fn stable_partition{};
2421#endif
2422
2423 template<typename _Iter, typename _Out1, typename _Out2>
2424 struct in_out_out_result
2425 {
2426 [[no_unique_address]] _Iter in;
2427 [[no_unique_address]] _Out1 out1;
2428 [[no_unique_address]] _Out2 out2;
2429
2430 template<typename _IIter, typename _OOut1, typename _OOut2>
2431 requires convertible_to<const _Iter&, _IIter>
2432 && convertible_to<const _Out1&, _OOut1>
2433 && convertible_to<const _Out2&, _OOut2>
2434 constexpr
2435 operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
2436 { return {in, out1, out2}; }
2437
2438 template<typename _IIter, typename _OOut1, typename _OOut2>
2439 requires convertible_to<_Iter, _IIter>
2440 && convertible_to<_Out1, _OOut1>
2441 && convertible_to<_Out2, _OOut2>
2442 constexpr
2443 operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
2444 { return {std::move(in), std::move(out1), std::move(out2)}; }
2445 };
2446
2447 template<typename _Iter, typename _Out1, typename _Out2>
2448 using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
2449
2450 struct __partition_copy_fn
2451 {
2452 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2453 weakly_incrementable _Out1, weakly_incrementable _Out2,
2454 typename _Proj = identity,
2455 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2456 requires indirectly_copyable<_Iter, _Out1>
2457 && indirectly_copyable<_Iter, _Out2>
2458 constexpr partition_copy_result<_Iter, _Out1, _Out2>
2459 operator()(_Iter __first, _Sent __last,
2460 _Out1 __out_true, _Out2 __out_false,
2461 _Pred __pred, _Proj __proj = {}) const
2462 {
2463 for (; __first != __last; ++__first)
2464 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
2465 {
2466 *__out_true = *__first;
2467 ++__out_true;
2468 }
2469 else
2470 {
2471 *__out_false = *__first;
2472 ++__out_false;
2473 }
2474
2475 return {std::move(__first),
2476 std::move(__out_true), std::move(__out_false)};
2477 }
2478
2479 template<input_range _Range, weakly_incrementable _Out1,
2480 weakly_incrementable _Out2,
2481 typename _Proj = identity,
2482 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2483 _Pred>
2484 requires indirectly_copyable<iterator_t<_Range>, _Out1>
2485 && indirectly_copyable<iterator_t<_Range>, _Out2>
2486 constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
2487 operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
2488 _Pred __pred, _Proj __proj = {}) const
2489 {
2490 return (*this)(ranges::begin(__r), ranges::end(__r),
2491 std::move(__out_true), std::move(__out_false),
2492 std::move(__pred), std::move(__proj));
2493 }
2494 };
2495
2496 inline constexpr __partition_copy_fn partition_copy{};
2497
2498 struct __partition_point_fn
2499 {
2500 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2501 typename _Proj = identity,
2502 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2503 constexpr _Iter
2504 operator()(_Iter __first, _Sent __last,
2505 _Pred __pred, _Proj __proj = {}) const
2506 {
2507 auto __len = ranges::distance(__first, __last);
2508
2509 while (__len > 0)
2510 {
2511 auto __half = __len / 2;
2512 auto __middle = __first;
2513 ranges::advance(__middle, __half);
2514 if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
2515 {
2516 __first = __middle;
2517 ++__first;
2518 __len = __len - __half - 1;
2519 }
2520 else
2521 __len = __half;
2522 }
2523 return __first;
2524 }
2525
2526 template<forward_range _Range, typename _Proj = identity,
2527 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2528 _Pred>
2529 constexpr borrowed_iterator_t<_Range>
2530 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
2531 {
2532 return (*this)(ranges::begin(__r), ranges::end(__r),
2533 std::move(__pred), std::move(__proj));
2534 }
2535 };
2536
2537 inline constexpr __partition_point_fn partition_point{};
2538
2539 template<typename _Iter1, typename _Iter2, typename _Out>
2540 using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2541
2542 struct __merge_fn
2543 {
2544 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2545 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2546 weakly_incrementable _Out, typename _Comp = ranges::less,
2547 typename _Proj1 = identity, typename _Proj2 = identity>
2548 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2549 constexpr merge_result<_Iter1, _Iter2, _Out>
2550 operator()(_Iter1 __first1, _Sent1 __last1,
2551 _Iter2 __first2, _Sent2 __last2, _Out __result,
2552 _Comp __comp = {},
2553 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2554 {
2555 while (__first1 != __last1 && __first2 != __last2)
2556 {
2557 if (std::__invoke(__comp,
2558 std::__invoke(__proj2, *__first2),
2559 std::__invoke(__proj1, *__first1)))
2560 {
2561 *__result = *__first2;
2562 ++__first2;
2563 }
2564 else
2565 {
2566 *__result = *__first1;
2567 ++__first1;
2568 }
2569 ++__result;
2570 }
2571 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2572 std::move(__result));
2573 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2574 std::move(__copy1.out));
2575 return { std::move(__copy1.in), std::move(__copy2.in),
2576 std::move(__copy2.out) };
2577 }
2578
2579 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2580 typename _Comp = ranges::less,
2581 typename _Proj1 = identity, typename _Proj2 = identity>
2582 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2583 _Comp, _Proj1, _Proj2>
2584 constexpr merge_result<borrowed_iterator_t<_Range1>,
2585 borrowed_iterator_t<_Range2>,
2586 _Out>
2587 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2588 _Comp __comp = {},
2589 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2590 {
2591 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2592 ranges::begin(__r2), ranges::end(__r2),
2593 std::move(__result), std::move(__comp),
2594 std::move(__proj1), std::move(__proj2));
2595 }
2596 };
2597
2598 inline constexpr __merge_fn merge{};
2599
2600 struct __inplace_merge_fn
2601 {
2602 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2603 typename _Comp = ranges::less,
2604 typename _Proj = identity>
2605 requires sortable<_Iter, _Comp, _Proj>
2606 _GLIBCXX26_CONSTEXPR
2607 _Iter
2608 operator()(_Iter __first, _Iter __middle, _Sent __last,
2609 _Comp __comp = {}, _Proj __proj = {}) const
2610 {
2611 auto __lasti = ranges::next(__first, __last);
2612 std::inplace_merge(std::move(__first), std::move(__middle), __lasti,
2613 __detail::__make_comp_proj(__comp, __proj));
2614 return __lasti;
2615 }
2616
2617 template<bidirectional_range _Range,
2618 typename _Comp = ranges::less, typename _Proj = identity>
2619 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2620 _GLIBCXX26_CONSTEXPR
2621 borrowed_iterator_t<_Range>
2622 operator()(_Range&& __r, iterator_t<_Range> __middle,
2623 _Comp __comp = {}, _Proj __proj = {}) const
2624 {
2625 return (*this)(ranges::begin(__r), std::move(__middle),
2626 ranges::end(__r),
2627 std::move(__comp), std::move(__proj));
2628 }
2629 };
2630
2631 inline constexpr __inplace_merge_fn inplace_merge{};
2632
2633 struct __includes_fn
2634 {
2635 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2636 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2637 typename _Proj1 = identity, typename _Proj2 = identity,
2638 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
2639 projected<_Iter2, _Proj2>>
2640 _Comp = ranges::less>
2641 constexpr bool
2642 operator()(_Iter1 __first1, _Sent1 __last1,
2643 _Iter2 __first2, _Sent2 __last2,
2644 _Comp __comp = {},
2645 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2646 {
2647 while (__first1 != __last1 && __first2 != __last2)
2648 if (std::__invoke(__comp,
2649 std::__invoke(__proj2, *__first2),
2650 std::__invoke(__proj1, *__first1)))
2651 return false;
2652 else if (std::__invoke(__comp,
2653 std::__invoke(__proj1, *__first1),
2654 std::__invoke(__proj2, *__first2)))
2655 ++__first1;
2656 else
2657 {
2658 ++__first1;
2659 ++__first2;
2660 }
2661
2662 return __first2 == __last2;
2663 }
2664
2665 template<input_range _Range1, input_range _Range2,
2666 typename _Proj1 = identity, typename _Proj2 = identity,
2667 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
2668 projected<iterator_t<_Range2>, _Proj2>>
2669 _Comp = ranges::less>
2670 constexpr bool
2671 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
2672 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2673 {
2674 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2675 ranges::begin(__r2), ranges::end(__r2),
2676 std::move(__comp),
2677 std::move(__proj1), std::move(__proj2));
2678 }
2679 };
2680
2681 inline constexpr __includes_fn includes{};
2682
2683 template<typename _Iter1, typename _Iter2, typename _Out>
2684 using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2685
2686 struct __set_union_fn
2687 {
2688 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2689 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2690 weakly_incrementable _Out, typename _Comp = ranges::less,
2691 typename _Proj1 = identity, typename _Proj2 = identity>
2692 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2693 constexpr set_union_result<_Iter1, _Iter2, _Out>
2694 operator()(_Iter1 __first1, _Sent1 __last1,
2695 _Iter2 __first2, _Sent2 __last2,
2696 _Out __result, _Comp __comp = {},
2697 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2698 {
2699 while (__first1 != __last1 && __first2 != __last2)
2700 {
2701 if (std::__invoke(__comp,
2702 std::__invoke(__proj1, *__first1),
2703 std::__invoke(__proj2, *__first2)))
2704 {
2705 *__result = *__first1;
2706 ++__first1;
2707 }
2708 else if (std::__invoke(__comp,
2709 std::__invoke(__proj2, *__first2),
2710 std::__invoke(__proj1, *__first1)))
2711 {
2712 *__result = *__first2;
2713 ++__first2;
2714 }
2715 else
2716 {
2717 *__result = *__first1;
2718 ++__first1;
2719 ++__first2;
2720 }
2721 ++__result;
2722 }
2723 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2724 std::move(__result));
2725 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2726 std::move(__copy1.out));
2727 return {std::move(__copy1.in), std::move(__copy2.in),
2728 std::move(__copy2.out)};
2729 }
2730
2731 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2732 typename _Comp = ranges::less,
2733 typename _Proj1 = identity, typename _Proj2 = identity>
2734 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2735 _Comp, _Proj1, _Proj2>
2736 constexpr set_union_result<borrowed_iterator_t<_Range1>,
2737 borrowed_iterator_t<_Range2>, _Out>
2738 operator()(_Range1&& __r1, _Range2&& __r2,
2739 _Out __result, _Comp __comp = {},
2740 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2741 {
2742 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2743 ranges::begin(__r2), ranges::end(__r2),
2744 std::move(__result), std::move(__comp),
2745 std::move(__proj1), std::move(__proj2));
2746 }
2747 };
2748
2749 inline constexpr __set_union_fn set_union{};
2750
2751 template<typename _Iter1, typename _Iter2, typename _Out>
2752 using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2753
2754 struct __set_intersection_fn
2755 {
2756 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2757 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2758 weakly_incrementable _Out, typename _Comp = ranges::less,
2759 typename _Proj1 = identity, typename _Proj2 = identity>
2760 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2761 constexpr set_intersection_result<_Iter1, _Iter2, _Out>
2762 operator()(_Iter1 __first1, _Sent1 __last1,
2763 _Iter2 __first2, _Sent2 __last2, _Out __result,
2764 _Comp __comp = {},
2765 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2766 {
2767 while (__first1 != __last1 && __first2 != __last2)
2768 if (std::__invoke(__comp,
2769 std::__invoke(__proj1, *__first1),
2770 std::__invoke(__proj2, *__first2)))
2771 ++__first1;
2772 else if (std::__invoke(__comp,
2773 std::__invoke(__proj2, *__first2),
2774 std::__invoke(__proj1, *__first1)))
2775 ++__first2;
2776 else
2777 {
2778 *__result = *__first1;
2779 ++__first1;
2780 ++__first2;
2781 ++__result;
2782 }
2783 // TODO: Eliminating these variables triggers an ICE.
2784 auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
2785 auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
2786 return {std::move(__last1i), std::move(__last2i), std::move(__result)};
2787 }
2788
2789 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2790 typename _Comp = ranges::less,
2791 typename _Proj1 = identity, typename _Proj2 = identity>
2792 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2793 _Comp, _Proj1, _Proj2>
2794 constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
2795 borrowed_iterator_t<_Range2>, _Out>
2796 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2797 _Comp __comp = {},
2798 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2799 {
2800 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2801 ranges::begin(__r2), ranges::end(__r2),
2802 std::move(__result), std::move(__comp),
2803 std::move(__proj1), std::move(__proj2));
2804 }
2805 };
2806
2807 inline constexpr __set_intersection_fn set_intersection{};
2808
2809 template<typename _Iter, typename _Out>
2810 using set_difference_result = in_out_result<_Iter, _Out>;
2811
2812 struct __set_difference_fn
2813 {
2814 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2815 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2816 weakly_incrementable _Out, typename _Comp = ranges::less,
2817 typename _Proj1 = identity, typename _Proj2 = identity>
2818 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2819 constexpr set_difference_result<_Iter1, _Out>
2820 operator()(_Iter1 __first1, _Sent1 __last1,
2821 _Iter2 __first2, _Sent2 __last2, _Out __result,
2822 _Comp __comp = {},
2823 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2824 {
2825 while (__first1 != __last1 && __first2 != __last2)
2826 if (std::__invoke(__comp,
2827 std::__invoke(__proj1, *__first1),
2828 std::__invoke(__proj2, *__first2)))
2829 {
2830 *__result = *__first1;
2831 ++__first1;
2832 ++__result;
2833 }
2834 else if (std::__invoke(__comp,
2835 std::__invoke(__proj2, *__first2),
2836 std::__invoke(__proj1, *__first1)))
2837 ++__first2;
2838 else
2839 {
2840 ++__first1;
2841 ++__first2;
2842 }
2843 return ranges::copy(std::move(__first1), std::move(__last1),
2844 std::move(__result));
2845 }
2846
2847 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2848 typename _Comp = ranges::less,
2849 typename _Proj1 = identity, typename _Proj2 = identity>
2850 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2851 _Comp, _Proj1, _Proj2>
2852 constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
2853 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2854 _Comp __comp = {},
2855 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2856 {
2857 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2858 ranges::begin(__r2), ranges::end(__r2),
2859 std::move(__result), std::move(__comp),
2860 std::move(__proj1), std::move(__proj2));
2861 }
2862 };
2863
2864 inline constexpr __set_difference_fn set_difference{};
2865
2866 template<typename _Iter1, typename _Iter2, typename _Out>
2867 using set_symmetric_difference_result
2868 = in_in_out_result<_Iter1, _Iter2, _Out>;
2869
2870 struct __set_symmetric_difference_fn
2871 {
2872 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2873 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2874 weakly_incrementable _Out, typename _Comp = ranges::less,
2875 typename _Proj1 = identity, typename _Proj2 = identity>
2876 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2877 constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
2878 operator()(_Iter1 __first1, _Sent1 __last1,
2879 _Iter2 __first2, _Sent2 __last2,
2880 _Out __result, _Comp __comp = {},
2881 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2882 {
2883 while (__first1 != __last1 && __first2 != __last2)
2884 if (std::__invoke(__comp,
2885 std::__invoke(__proj1, *__first1),
2886 std::__invoke(__proj2, *__first2)))
2887 {
2888 *__result = *__first1;
2889 ++__first1;
2890 ++__result;
2891 }
2892 else if (std::__invoke(__comp,
2893 std::__invoke(__proj2, *__first2),
2894 std::__invoke(__proj1, *__first1)))
2895 {
2896 *__result = *__first2;
2897 ++__first2;
2898 ++__result;
2899 }
2900 else
2901 {
2902 ++__first1;
2903 ++__first2;
2904 }
2905 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
2906 std::move(__result));
2907 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
2908 std::move(__copy1.out));
2909 return {std::move(__copy1.in), std::move(__copy2.in),
2910 std::move(__copy2.out)};
2911 }
2912
2913 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2914 typename _Comp = ranges::less,
2915 typename _Proj1 = identity, typename _Proj2 = identity>
2916 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2917 _Comp, _Proj1, _Proj2>
2918 constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
2919 borrowed_iterator_t<_Range2>,
2920 _Out>
2921 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2922 _Comp __comp = {},
2923 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2924 {
2925 return (*this)(ranges::begin(__r1), ranges::end(__r1),
2926 ranges::begin(__r2), ranges::end(__r2),
2927 std::move(__result), std::move(__comp),
2928 std::move(__proj1), std::move(__proj2));
2929 }
2930 };
2931
2932 inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
2933
2934 // min is defined in <bits/ranges_util.h>.
2935
2936 struct __max_fn
2937 {
2938 template<typename _Tp, typename _Proj = identity,
2939 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2940 _Comp = ranges::less>
2941 constexpr const _Tp&
2942 operator()(const _Tp& __a, const _Tp& __b,
2943 _Comp __comp = {}, _Proj __proj = {}) const
2944 {
2945 if (std::__invoke(__comp,
2946 std::__invoke(__proj, __a),
2947 std::__invoke(__proj, __b)))
2948 return __b;
2949 else
2950 return __a;
2951 }
2952
2953 template<input_range _Range, typename _Proj = identity,
2954 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2955 _Comp = ranges::less>
2956 requires indirectly_copyable_storable<iterator_t<_Range>,
2957 range_value_t<_Range>*>
2958 constexpr range_value_t<_Range>
2959 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2960 {
2961 auto __first = ranges::begin(__r);
2962 auto __last = ranges::end(__r);
2963 __glibcxx_assert(__first != __last);
2964 auto __result = *__first;
2965 while (++__first != __last)
2966 {
2967 auto&& __tmp = *__first;
2968 if (std::__invoke(__comp,
2969 std::__invoke(__proj, __result),
2970 std::__invoke(__proj, __tmp)))
2971 __result = std::forward<decltype(__tmp)>(__tmp);
2972 }
2973 return __result;
2974 }
2975
2976 template<copyable _Tp, typename _Proj = identity,
2977 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2978 _Comp = ranges::less>
2979 constexpr _Tp
2980 operator()(initializer_list<_Tp> __r,
2981 _Comp __comp = {}, _Proj __proj = {}) const
2982 {
2983 return (*this)(ranges::subrange(__r),
2984 std::move(__comp), std::move(__proj));
2985 }
2986 };
2987
2988 inline constexpr __max_fn max{};
2989
2990 struct __clamp_fn
2991 {
2992 template<typename _Tp, typename _Proj = identity,
2993 indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
2994 = ranges::less>
2995 constexpr const _Tp&
2996 operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
2997 _Comp __comp = {}, _Proj __proj = {}) const
2998 {
2999 __glibcxx_assert(!(std::__invoke(__comp,
3000 std::__invoke(__proj, __hi),
3001 std::__invoke(__proj, __lo))));
3002 auto&& __proj_val = std::__invoke(__proj, __val);
3003 if (std::__invoke(__comp,
3004 std::forward<decltype(__proj_val)>(__proj_val),
3005 std::__invoke(__proj, __lo)))
3006 return __lo;
3007 else if (std::__invoke(__comp,
3008 std::__invoke(__proj, __hi),
3009 std::forward<decltype(__proj_val)>(__proj_val)))
3010 return __hi;
3011 else
3012 return __val;
3013 }
3014 };
3015
3016 inline constexpr __clamp_fn clamp{};
3017
3018 template<typename _Tp>
3019 struct min_max_result
3020 {
3021 [[no_unique_address]] _Tp min;
3022 [[no_unique_address]] _Tp max;
3023
3024 template<typename _Tp2>
3025 requires convertible_to<const _Tp&, _Tp2>
3026 constexpr
3027 operator min_max_result<_Tp2>() const &
3028 { return {min, max}; }
3029
3030 template<typename _Tp2>
3031 requires convertible_to<_Tp, _Tp2>
3032 constexpr
3033 operator min_max_result<_Tp2>() &&
3034 { return {std::move(min), std::move(max)}; }
3035 };
3036
3037 template<typename _Tp>
3038 using minmax_result = min_max_result<_Tp>;
3039
3040 struct __minmax_fn
3041 {
3042 template<typename _Tp, typename _Proj = identity,
3043 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3044 _Comp = ranges::less>
3045 constexpr minmax_result<const _Tp&>
3046 operator()(const _Tp& __a, const _Tp& __b,
3047 _Comp __comp = {}, _Proj __proj = {}) const
3048 {
3049 if (std::__invoke(__comp,
3050 std::__invoke(__proj, __b),
3051 std::__invoke(__proj, __a)))
3052 return {__b, __a};
3053 else
3054 return {__a, __b};
3055 }
3056
3057 template<input_range _Range, typename _Proj = identity,
3058 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3059 _Comp = ranges::less>
3060 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
3061 constexpr minmax_result<range_value_t<_Range>>
3062 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3063 {
3064 auto __first = ranges::begin(__r);
3065 auto __last = ranges::end(__r);
3066 __glibcxx_assert(__first != __last);
3067 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3068 minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
3069 if (++__first == __last)
3070 return __result;
3071 else
3072 {
3073 // At this point __result.min == __result.max, so a single
3074 // comparison with the next element suffices.
3075 auto&& __val = *__first;
3076 if (__comp_proj(__val, __result.min))
3077 __result.min = std::forward<decltype(__val)>(__val);
3078 else
3079 __result.max = std::forward<decltype(__val)>(__val);
3080 }
3081 while (++__first != __last)
3082 {
3083 // Now process two elements at a time so that we perform at most
3084 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
3085 // iterations of this loop performs three comparisons).
3086 range_value_t<_Range> __val1 = *__first;
3087 if (++__first == __last)
3088 {
3089 // N is odd; in this final iteration, we perform at most two
3090 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
3091 // which is not more than 3*N/2, as required.
3092 if (__comp_proj(__val1, __result.min))
3093 __result.min = std::move(__val1);
3094 else if (!__comp_proj(__val1, __result.max))
3095 __result.max = std::move(__val1);
3096 break;
3097 }
3098 auto&& __val2 = *__first;
3099 if (!__comp_proj(__val2, __val1))
3100 {
3101 if (__comp_proj(__val1, __result.min))
3102 __result.min = std::move(__val1);
3103 if (!__comp_proj(__val2, __result.max))
3104 __result.max = std::forward<decltype(__val2)>(__val2);
3105 }
3106 else
3107 {
3108 if (__comp_proj(__val2, __result.min))
3109 __result.min = std::forward<decltype(__val2)>(__val2);
3110 if (!__comp_proj(__val1, __result.max))
3111 __result.max = std::move(__val1);
3112 }
3113 }
3114 return __result;
3115 }
3116
3117 template<copyable _Tp, typename _Proj = identity,
3118 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3119 _Comp = ranges::less>
3120 constexpr minmax_result<_Tp>
3121 operator()(initializer_list<_Tp> __r,
3122 _Comp __comp = {}, _Proj __proj = {}) const
3123 {
3124 return (*this)(ranges::subrange(__r),
3125 std::move(__comp), std::move(__proj));
3126 }
3127 };
3128
3129 inline constexpr __minmax_fn minmax{};
3130
3131 struct __min_element_fn
3132 {
3133 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3134 typename _Proj = identity,
3135 indirect_strict_weak_order<projected<_Iter, _Proj>>
3136 _Comp = ranges::less>
3137 constexpr _Iter
3138 operator()(_Iter __first, _Sent __last,
3139 _Comp __comp = {}, _Proj __proj = {}) const
3140 {
3141 if (__first == __last)
3142 return __first;
3143
3144 auto __i = __first;
3145 while (++__i != __last)
3146 {
3147 if (std::__invoke(__comp,
3148 std::__invoke(__proj, *__i),
3149 std::__invoke(__proj, *__first)))
3150 __first = __i;
3151 }
3152 return __first;
3153 }
3154
3155 template<forward_range _Range, typename _Proj = identity,
3156 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3157 _Comp = ranges::less>
3158 constexpr borrowed_iterator_t<_Range>
3159 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3160 {
3161 return (*this)(ranges::begin(__r), ranges::end(__r),
3162 std::move(__comp), std::move(__proj));
3163 }
3164 };
3165
3166 inline constexpr __min_element_fn min_element{};
3167
3168 struct __max_element_fn
3169 {
3170 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3171 typename _Proj = identity,
3172 indirect_strict_weak_order<projected<_Iter, _Proj>>
3173 _Comp = ranges::less>
3174 constexpr _Iter
3175 operator()(_Iter __first, _Sent __last,
3176 _Comp __comp = {}, _Proj __proj = {}) const
3177 {
3178 if (__first == __last)
3179 return __first;
3180
3181 auto __i = __first;
3182 while (++__i != __last)
3183 {
3184 if (std::__invoke(__comp,
3185 std::__invoke(__proj, *__first),
3186 std::__invoke(__proj, *__i)))
3187 __first = __i;
3188 }
3189 return __first;
3190 }
3191
3192 template<forward_range _Range, typename _Proj = identity,
3193 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3194 _Comp = ranges::less>
3195 constexpr borrowed_iterator_t<_Range>
3196 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3197 {
3198 return (*this)(ranges::begin(__r), ranges::end(__r),
3199 std::move(__comp), std::move(__proj));
3200 }
3201 };
3202
3203 inline constexpr __max_element_fn max_element{};
3204
3205 template<typename _Iter>
3206 using minmax_element_result = min_max_result<_Iter>;
3207
3208 struct __minmax_element_fn
3209 {
3210 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3211 typename _Proj = identity,
3212 indirect_strict_weak_order<projected<_Iter, _Proj>>
3213 _Comp = ranges::less>
3214 constexpr minmax_element_result<_Iter>
3215 operator()(_Iter __first, _Sent __last,
3216 _Comp __comp = {}, _Proj __proj = {}) const
3217 {
3218 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3219 minmax_element_result<_Iter> __result = {__first, __first};
3220 if (__first == __last || ++__first == __last)
3221 return __result;
3222 else
3223 {
3224 // At this point __result.min == __result.max, so a single
3225 // comparison with the next element suffices.
3226 if (__comp_proj(*__first, *__result.min))
3227 __result.min = __first;
3228 else
3229 __result.max = __first;
3230 }
3231 while (++__first != __last)
3232 {
3233 // Now process two elements at a time so that we perform at most
3234 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
3235 // iterations of this loop performs three comparisons).
3236 auto __prev = __first;
3237 if (++__first == __last)
3238 {
3239 // N is odd; in this final iteration, we perform at most two
3240 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
3241 // which is not more than 3*N/2, as required.
3242 if (__comp_proj(*__prev, *__result.min))
3243 __result.min = __prev;
3244 else if (!__comp_proj(*__prev, *__result.max))
3245 __result.max = __prev;
3246 break;
3247 }
3248 if (!__comp_proj(*__first, *__prev))
3249 {
3250 if (__comp_proj(*__prev, *__result.min))
3251 __result.min = __prev;
3252 if (!__comp_proj(*__first, *__result.max))
3253 __result.max = __first;
3254 }
3255 else
3256 {
3257 if (__comp_proj(*__first, *__result.min))
3258 __result.min = __first;
3259 if (!__comp_proj(*__prev, *__result.max))
3260 __result.max = __prev;
3261 }
3262 }
3263 return __result;
3264 }
3265
3266 template<forward_range _Range, typename _Proj = identity,
3267 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3268 _Comp = ranges::less>
3269 constexpr minmax_element_result<borrowed_iterator_t<_Range>>
3270 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3271 {
3272 return (*this)(ranges::begin(__r), ranges::end(__r),
3273 std::move(__comp), std::move(__proj));
3274 }
3275 };
3276
3277 inline constexpr __minmax_element_fn minmax_element{};
3278
3279 struct __lexicographical_compare_fn
3280 {
3281 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3282 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3283 typename _Proj1 = identity, typename _Proj2 = identity,
3284 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
3285 projected<_Iter2, _Proj2>>
3286 _Comp = ranges::less>
3287 constexpr bool
3288 operator()(_Iter1 __first1, _Sent1 __last1,
3289 _Iter2 __first2, _Sent2 __last2,
3290 _Comp __comp = {},
3291 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3292 {
3293 if constexpr (__detail::__is_normal_iterator<_Iter1>
3294 && same_as<_Iter1, _Sent1>)
3295 return (*this)(__first1.base(), __last1.base(),
3296 std::move(__first2), std::move(__last2),
3297 std::move(__comp),
3298 std::move(__proj1), std::move(__proj2));
3299 else if constexpr (__detail::__is_normal_iterator<_Iter2>
3300 && same_as<_Iter2, _Sent2>)
3301 return (*this)(std::move(__first1), std::move(__last1),
3302 __first2.base(), __last2.base(),
3303 std::move(__comp),
3304 std::move(__proj1), std::move(__proj2));
3305 else
3306 {
3307 constexpr bool __sized_iters
3308 = (sized_sentinel_for<_Sent1, _Iter1>
3309 && sized_sentinel_for<_Sent2, _Iter2>);
3310 if constexpr (__sized_iters)
3311 {
3312 using _ValueType1 = iter_value_t<_Iter1>;
3313 using _ValueType2 = iter_value_t<_Iter2>;
3314 // This condition is consistent with the one in
3315 // __lexicographical_compare_aux in <bits/stl_algobase.h>.
3316 constexpr bool __use_memcmp
3317 = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
3318 && __ptr_to_nonvolatile<_Iter1>
3319 && __ptr_to_nonvolatile<_Iter2>
3320 && (is_same_v<_Comp, ranges::less>
3321 || is_same_v<_Comp, ranges::greater>)
3322 && is_same_v<_Proj1, identity>
3323 && is_same_v<_Proj2, identity>);
3324 if constexpr (__use_memcmp)
3325 {
3326 const auto __d1 = __last1 - __first1;
3327 const auto __d2 = __last2 - __first2;
3328
3329 if (const auto __len = std::min(__d1, __d2))
3330 {
3331 const auto __c
3332 = std::__memcmp(__first1, __first2, __len);
3333 if constexpr (is_same_v<_Comp, ranges::less>)
3334 {
3335 if (__c < 0)
3336 return true;
3337 if (__c > 0)
3338 return false;
3339 }
3340 else if constexpr (is_same_v<_Comp, ranges::greater>)
3341 {
3342 if (__c > 0)
3343 return true;
3344 if (__c < 0)
3345 return false;
3346 }
3347 }
3348 return __d1 < __d2;
3349 }
3350 }
3351
3352 for (; __first1 != __last1 && __first2 != __last2;
3353 ++__first1, (void) ++__first2)
3354 {
3355 if (std::__invoke(__comp,
3356 std::__invoke(__proj1, *__first1),
3357 std::__invoke(__proj2, *__first2)))
3358 return true;
3359 if (std::__invoke(__comp,
3360 std::__invoke(__proj2, *__first2),
3361 std::__invoke(__proj1, *__first1)))
3362 return false;
3363 }
3364 return __first1 == __last1 && __first2 != __last2;
3365 }
3366 }
3367
3368 template<input_range _Range1, input_range _Range2,
3369 typename _Proj1 = identity, typename _Proj2 = identity,
3370 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
3371 projected<iterator_t<_Range2>, _Proj2>>
3372 _Comp = ranges::less>
3373 constexpr bool
3374 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
3375 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3376 {
3377 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3378 ranges::begin(__r2), ranges::end(__r2),
3379 std::move(__comp),
3380 std::move(__proj1), std::move(__proj2));
3381 }
3382
3383 private:
3384 template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
3385 static constexpr bool __ptr_to_nonvolatile
3386 = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
3387 };
3388
3389 inline constexpr __lexicographical_compare_fn lexicographical_compare;
3390
3391 template<typename _Iter>
3392 struct in_found_result
3393 {
3394 [[no_unique_address]] _Iter in;
3395 bool found;
3396
3397 template<typename _Iter2>
3398 requires convertible_to<const _Iter&, _Iter2>
3399 constexpr
3400 operator in_found_result<_Iter2>() const &
3401 { return {in, found}; }
3402
3403 template<typename _Iter2>
3404 requires convertible_to<_Iter, _Iter2>
3405 constexpr
3406 operator in_found_result<_Iter2>() &&
3407 { return {std::move(in), found}; }
3408 };
3409
3410 template<typename _Iter>
3411 using next_permutation_result = in_found_result<_Iter>;
3412
3413 struct __next_permutation_fn
3414 {
3415 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3416 typename _Comp = ranges::less, typename _Proj = identity>
3417 requires sortable<_Iter, _Comp, _Proj>
3418 constexpr next_permutation_result<_Iter>
3419 operator()(_Iter __first, _Sent __last,
3420 _Comp __comp = {}, _Proj __proj = {}) const
3421 {
3422 if (__first == __last)
3423 return {std::move(__first), false};
3424
3425 auto __i = __first;
3426 ++__i;
3427 if (__i == __last)
3428 return {std::move(__i), false};
3429
3430 auto __lasti = ranges::next(__first, __last);
3431 __i = __lasti;
3432 --__i;
3433
3434 for (;;)
3435 {
3436 auto __ii = __i;
3437 --__i;
3438 if (std::__invoke(__comp,
3439 std::__invoke(__proj, *__i),
3440 std::__invoke(__proj, *__ii)))
3441 {
3442 auto __j = __lasti;
3443 while (!(bool)std::__invoke(__comp,
3444 std::__invoke(__proj, *__i),
3445 std::__invoke(__proj, *--__j)))
3446 ;
3447 ranges::iter_swap(__i, __j);
3448 ranges::reverse(__ii, __last);
3449 return {std::move(__lasti), true};
3450 }
3451 if (__i == __first)
3452 {
3453 ranges::reverse(__first, __last);
3454 return {std::move(__lasti), false};
3455 }
3456 }
3457 }
3458
3459 template<bidirectional_range _Range, typename _Comp = ranges::less,
3460 typename _Proj = identity>
3461 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3462 constexpr next_permutation_result<borrowed_iterator_t<_Range>>
3463 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3464 {
3465 return (*this)(ranges::begin(__r), ranges::end(__r),
3466 std::move(__comp), std::move(__proj));
3467 }
3468 };
3469
3470 inline constexpr __next_permutation_fn next_permutation{};
3471
3472 template<typename _Iter>
3473 using prev_permutation_result = in_found_result<_Iter>;
3474
3475 struct __prev_permutation_fn
3476 {
3477 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3478 typename _Comp = ranges::less, typename _Proj = identity>
3479 requires sortable<_Iter, _Comp, _Proj>
3480 constexpr prev_permutation_result<_Iter>
3481 operator()(_Iter __first, _Sent __last,
3482 _Comp __comp = {}, _Proj __proj = {}) const
3483 {
3484 if (__first == __last)
3485 return {std::move(__first), false};
3486
3487 auto __i = __first;
3488 ++__i;
3489 if (__i == __last)
3490 return {std::move(__i), false};
3491
3492 auto __lasti = ranges::next(__first, __last);
3493 __i = __lasti;
3494 --__i;
3495
3496 for (;;)
3497 {
3498 auto __ii = __i;
3499 --__i;
3500 if (std::__invoke(__comp,
3501 std::__invoke(__proj, *__ii),
3502 std::__invoke(__proj, *__i)))
3503 {
3504 auto __j = __lasti;
3505 while (!(bool)std::__invoke(__comp,
3506 std::__invoke(__proj, *--__j),
3507 std::__invoke(__proj, *__i)))
3508 ;
3509 ranges::iter_swap(__i, __j);
3510 ranges::reverse(__ii, __last);
3511 return {std::move(__lasti), true};
3512 }
3513 if (__i == __first)
3514 {
3515 ranges::reverse(__first, __last);
3516 return {std::move(__lasti), false};
3517 }
3518 }
3519 }
3520
3521 template<bidirectional_range _Range, typename _Comp = ranges::less,
3522 typename _Proj = identity>
3523 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3524 constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
3525 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
3526 {
3527 return (*this)(ranges::begin(__r), ranges::end(__r),
3528 std::move(__comp), std::move(__proj));
3529 }
3530 };
3531
3532 inline constexpr __prev_permutation_fn prev_permutation{};
3533
3534#if __glibcxx_ranges_contains >= 202207L // C++ >= 23
3535 struct __contains_fn
3536 {
3537 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3538 typename _Proj = identity,
3539 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
3540 requires indirect_binary_predicate<ranges::equal_to,
3541 projected<_Iter, _Proj>, const _Tp*>
3542 constexpr bool
3543 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
3544 { return ranges::find(std::move(__first), __last, __value, std::move(__proj)) != __last; }
3545
3546 template<input_range _Range,
3547 typename _Proj = identity,
3548 typename _Tp
3549 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
3550 requires indirect_binary_predicate<ranges::equal_to,
3551 projected<iterator_t<_Range>, _Proj>, const _Tp*>
3552 constexpr bool
3553 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
3554 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
3555 };
3556
3557 inline constexpr __contains_fn contains{};
3558
3559 struct __contains_subrange_fn
3560 {
3561 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3562 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3563 typename _Pred = ranges::equal_to,
3564 typename _Proj1 = identity, typename _Proj2 = identity>
3565 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
3566 constexpr bool
3567 operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
3568 _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3569 {
3570 return __first2 == __last2
3571 || !ranges::search(__first1, __last1, __first2, __last2,
3572 std::move(__pred), std::move(__proj1), std::move(__proj2)).empty();
3573 }
3574
3575 template<forward_range _Range1, forward_range _Range2,
3576 typename _Pred = ranges::equal_to,
3577 typename _Proj1 = identity, typename _Proj2 = identity>
3578 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
3579 _Pred, _Proj1, _Proj2>
3580 constexpr bool
3581 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
3582 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3583 {
3584 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3585 ranges::begin(__r2), ranges::end(__r2),
3586 std::move(__pred), std::move(__proj1), std::move(__proj2));
3587 }
3588 };
3589
3590 inline constexpr __contains_subrange_fn contains_subrange{};
3591
3592#endif // __glibcxx_ranges_contains
3593
3594#if __glibcxx_ranges_find_last >= 202207L // C++ >= 23
3595
3596 struct __find_last_fn
3597 {
3598 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3599 typename _Proj = identity,
3600 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
3601 requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Tp*>
3602 constexpr subrange<_Iter>
3603 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
3604 {
3605 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3606 {
3607 _Iter __found = ranges::find(reverse_iterator<_Iter>{__last},
3608 reverse_iterator<_Iter>{__first},
3609 __value, std::move(__proj)).base();
3610 if (__found == __first)
3611 return {__last, __last};
3612 else
3613 return {ranges::prev(__found), __last};
3614 }
3615 else
3616 {
3617 _Iter __found = ranges::find(__first, __last, __value, __proj);
3618 if (__found == __last)
3619 return {__found, __found};
3620 __first = __found;
3621 for (;;)
3622 {
3623 __first = ranges::find(ranges::next(__first), __last, __value, __proj);
3624 if (__first == __last)
3625 return {__found, __first};
3626 __found = __first;
3627 }
3628 }
3629 }
3630
3631 template<forward_range _Range, typename _Proj = identity,
3632 typename _Tp
3633 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
3634 requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Tp*>
3635 constexpr borrowed_subrange_t<_Range>
3636 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
3637 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
3638 };
3639
3640 inline constexpr __find_last_fn find_last{};
3641
3642 struct __find_last_if_fn
3643 {
3644 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
3645 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3646 constexpr subrange<_Iter>
3647 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
3648 {
3649 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3650 {
3651 _Iter __found = ranges::find_if(reverse_iterator<_Iter>{__last},
3652 reverse_iterator<_Iter>{__first},
3653 std::move(__pred), std::move(__proj)).base();
3654 if (__found == __first)
3655 return {__last, __last};
3656 else
3657 return {ranges::prev(__found), __last};
3658 }
3659 else
3660 {
3661 _Iter __found = ranges::find_if(__first, __last, __pred, __proj);
3662 if (__found == __last)
3663 return {__found, __found};
3664 __first = __found;
3665 for (;;)
3666 {
3667 __first = ranges::find_if(ranges::next(__first), __last, __pred, __proj);
3668 if (__first == __last)
3669 return {__found, __first};
3670 __found = __first;
3671 }
3672 }
3673 }
3674
3675 template<forward_range _Range, typename _Proj = identity,
3676 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
3677 constexpr borrowed_subrange_t<_Range>
3678 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3679 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
3680 };
3681
3682 inline constexpr __find_last_if_fn find_last_if{};
3683
3684 struct __find_last_if_not_fn
3685 {
3686 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
3687 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3688 constexpr subrange<_Iter>
3689 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
3690 {
3691 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
3692 {
3693 _Iter __found = ranges::find_if_not(reverse_iterator<_Iter>{__last},
3694 reverse_iterator<_Iter>{__first},
3695 std::move(__pred), std::move(__proj)).base();
3696 if (__found == __first)
3697 return {__last, __last};
3698 else
3699 return {ranges::prev(__found), __last};
3700 }
3701 else
3702 {
3703 _Iter __found = ranges::find_if_not(__first, __last, __pred, __proj);
3704 if (__found == __last)
3705 return {__found, __found};
3706 __first = __found;
3707 for (;;)
3708 {
3709 __first = ranges::find_if_not(ranges::next(__first), __last, __pred, __proj);
3710 if (__first == __last)
3711 return {__found, __first};
3712 __found = __first;
3713 }
3714 }
3715 }
3716
3717 template<forward_range _Range, typename _Proj = identity,
3718 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
3719 constexpr borrowed_subrange_t<_Range>
3720 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3721 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
3722 };
3723
3724 inline constexpr __find_last_if_not_fn find_last_if_not{};
3725
3726#endif // __glibcxx_ranges_find_last
3727
3728#if __glibcxx_ranges_fold >= 202207L // C++ >= 23
3729
3730 template<typename _Iter, typename _Tp>
3731 struct in_value_result
3732 {
3733 [[no_unique_address]] _Iter in;
3734 [[no_unique_address]] _Tp value;
3735
3736 template<typename _Iter2, typename _Tp2>
3737 requires convertible_to<const _Iter&, _Iter2>
3738 && convertible_to<const _Tp&, _Tp2>
3739 constexpr
3740 operator in_value_result<_Iter2, _Tp2>() const &
3741 { return {in, value}; }
3742
3743 template<typename _Iter2, typename _Tp2>
3744 requires convertible_to<_Iter, _Iter2>
3745 && convertible_to<_Tp, _Tp2>
3746 constexpr
3747 operator in_value_result<_Iter2, _Tp2>() &&
3748 { return {std::move(in), std::move(value)}; }
3749 };
3750
3751 namespace __detail
3752 {
3753 template<typename _Fp>
3754 class __flipped
3755 {
3756 _Fp _M_f;
3757
3758 public:
3759 template<typename _Tp, typename _Up>
3760 requires invocable<_Fp&, _Up, _Tp>
3761 invoke_result_t<_Fp&, _Up, _Tp>
3762 operator()(_Tp&&, _Up&&); // not defined
3763 };
3764
3765 template<typename _Fp, typename _Tp, typename _Iter, typename _Up>
3766 concept __indirectly_binary_left_foldable_impl = movable<_Tp> && movable<_Up>
3767 && convertible_to<_Tp, _Up>
3768 && invocable<_Fp&, _Up, iter_reference_t<_Iter>>
3769 && assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Iter>>>;
3770
3771 template<typename _Fp, typename _Tp, typename _Iter>
3772 concept __indirectly_binary_left_foldable = copy_constructible<_Fp>
3773 && indirectly_readable<_Iter>
3774 && invocable<_Fp&, _Tp, iter_reference_t<_Iter>>
3775 && convertible_to<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>,
3777 && __indirectly_binary_left_foldable_impl
3779
3780 template <typename _Fp, typename _Tp, typename _Iter>
3781 concept __indirectly_binary_right_foldable
3782 = __indirectly_binary_left_foldable<__flipped<_Fp>, _Tp, _Iter>;
3783 } // namespace __detail
3784
3785 template<typename _Iter, typename _Tp>
3786 using fold_left_with_iter_result = in_value_result<_Iter, _Tp>;
3787
3788 struct __fold_left_with_iter_fn
3789 {
3790 template<typename _Ret_iter,
3791 typename _Iter, typename _Sent, typename _Tp, typename _Fp>
3792 static constexpr auto
3793 _S_impl(_Iter __first, _Sent __last, _Tp __init, _Fp __f)
3794 {
3795 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>>;
3796 using _Ret = fold_left_with_iter_result<_Ret_iter, _Up>;
3797
3798 if (__first == __last)
3799 return _Ret{std::move(__first), _Up(std::move(__init))};
3800
3801 _Up __accum = std::__invoke(__f, std::move(__init), *__first);
3802 for (++__first; __first != __last; ++__first)
3803 __accum = std::__invoke(__f, std::move(__accum), *__first);
3804 return _Ret{std::move(__first), std::move(__accum)};
3805 }
3806
3807 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3808 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3809 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
3810 constexpr auto
3811 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3812 {
3813 using _Ret_iter = _Iter;
3814 return _S_impl<_Ret_iter>(std::move(__first), __last,
3815 std::move(__init), std::move(__f));
3816 }
3817
3818 template<input_range _Range,
3819 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3820 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
3821 constexpr auto
3822 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3823 {
3824 using _Ret_iter = borrowed_iterator_t<_Range>;
3825 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r),
3826 std::move(__init), std::move(__f));
3827 }
3828 };
3829
3830 inline constexpr __fold_left_with_iter_fn fold_left_with_iter{};
3831
3832 struct __fold_left_fn
3833 {
3834 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3835 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3836 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
3837 constexpr auto
3838 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3839 {
3840 return ranges::fold_left_with_iter(std::move(__first), __last,
3841 std::move(__init), std::move(__f)).value;
3842 }
3843
3844 template<input_range _Range,
3845 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3846 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
3847 constexpr auto
3848 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3849 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
3850 };
3851
3852 inline constexpr __fold_left_fn fold_left{};
3853
3854 template<typename _Iter, typename _Tp>
3855 using fold_left_first_with_iter_result = in_value_result<_Iter, _Tp>;
3856
3857 struct __fold_left_first_with_iter_fn
3858 {
3859 template<typename _Ret_iter, typename _Iter, typename _Sent, typename _Fp>
3860 static constexpr auto
3861 _S_impl(_Iter __first, _Sent __last, _Fp __f)
3862 {
3863 using _Up = decltype(ranges::fold_left(std::move(__first), __last,
3864 iter_value_t<_Iter>(*__first), __f));
3865 using _Ret = fold_left_first_with_iter_result<_Ret_iter, optional<_Up>>;
3866
3867 if (__first == __last)
3868 return _Ret{std::move(__first), optional<_Up>()};
3869
3870 optional<_Up> __init(in_place, *__first);
3871 for (++__first; __first != __last; ++__first)
3872 *__init = std::__invoke(__f, std::move(*__init), *__first);
3873 return _Ret{std::move(__first), std::move(__init)};
3874 }
3875
3876 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3877 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3878 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3879 constexpr auto
3880 operator()(_Iter __first, _Sent __last, _Fp __f) const
3881 {
3882 using _Ret_iter = _Iter;
3883 return _S_impl<_Ret_iter>(std::move(__first), __last, std::move(__f));
3884 }
3885
3886 template<input_range _Range,
3887 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3888 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3889 constexpr auto
3890 operator()(_Range&& __r, _Fp __f) const
3891 {
3892 using _Ret_iter = borrowed_iterator_t<_Range>;
3893 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r), std::move(__f));
3894 }
3895 };
3896
3897 inline constexpr __fold_left_first_with_iter_fn fold_left_first_with_iter{};
3898
3899 struct __fold_left_first_fn
3900 {
3901 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3902 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3903 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3904 constexpr auto
3905 operator()(_Iter __first, _Sent __last, _Fp __f) const
3906 {
3907 return ranges::fold_left_first_with_iter(std::move(__first), __last,
3908 std::move(__f)).value;
3909 }
3910
3911 template<input_range _Range,
3912 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3913 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3914 constexpr auto
3915 operator()(_Range&& __r, _Fp __f) const
3916 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
3917 };
3918
3919 inline constexpr __fold_left_first_fn fold_left_first{};
3920
3921 struct __fold_right_fn
3922 {
3923 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3924 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
3925 __detail::__indirectly_binary_right_foldable<_Tp, _Iter> _Fp>
3926 constexpr auto
3927 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
3928 {
3929 using _Up = decay_t<invoke_result_t<_Fp&, iter_reference_t<_Iter>, _Tp>>;
3930
3931 if (__first == __last)
3932 return _Up(std::move(__init));
3933
3934 _Iter __tail = ranges::next(__first, __last);
3935 _Up __accum = std::__invoke(__f, *--__tail, std::move(__init));
3936 while (__first != __tail)
3937 __accum = std::__invoke(__f, *--__tail, std::move(__accum));
3938 return __accum;
3939 }
3940
3941 template<bidirectional_range _Range,
3942 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
3943 __detail::__indirectly_binary_right_foldable<_Tp, iterator_t<_Range>> _Fp>
3944 constexpr auto
3945 operator()(_Range&& __r, _Tp __init, _Fp __f) const
3946 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
3947 };
3948
3949 inline constexpr __fold_right_fn fold_right{};
3950
3951 struct __fold_right_last_fn
3952 {
3953 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3954 __detail::__indirectly_binary_right_foldable<iter_value_t<_Iter>, _Iter> _Fp>
3955 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
3956 constexpr auto
3957 operator()(_Iter __first, _Sent __last, _Fp __f) const
3958 {
3959 using _Up = decltype(ranges::fold_right(__first, __last,
3960 iter_value_t<_Iter>(*__first), __f));
3961
3962 if (__first == __last)
3963 return optional<_Up>();
3964
3965 _Iter __tail = ranges::prev(ranges::next(__first, std::move(__last)));
3966 return optional<_Up>(in_place,
3967 ranges::fold_right(std::move(__first), __tail,
3968 iter_value_t<_Iter>(*__tail),
3969 std::move(__f)));
3970 }
3971
3972 template<bidirectional_range _Range,
3973 __detail::__indirectly_binary_right_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
3974 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
3975 constexpr auto
3976 operator()(_Range&& __r, _Fp __f) const
3977 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
3978 };
3979
3980 inline constexpr __fold_right_last_fn fold_right_last{};
3981#endif // __glibcxx_ranges_fold
3982} // namespace ranges
3983
3984 template<typename _ForwardIterator>
3985 constexpr _ForwardIterator
3986 shift_left(_ForwardIterator __first, _ForwardIterator __last,
3988 {
3989 __glibcxx_assert(__n >= 0);
3990 if (__n == 0)
3991 return __last;
3992
3993 auto __mid = ranges::next(__first, __n, __last);
3994 if (__mid == __last)
3995 return __first;
3996 return std::move(std::move(__mid), std::move(__last), std::move(__first));
3997 }
3998
3999 template<typename _ForwardIterator>
4000 constexpr _ForwardIterator
4001 shift_right(_ForwardIterator __first, _ForwardIterator __last,
4003 {
4004 __glibcxx_assert(__n >= 0);
4005 if (__n == 0)
4006 return __first;
4007
4008 using _Cat
4010 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
4011 {
4012 auto __mid = ranges::next(__last, -__n, __first);
4013 if (__mid == __first)
4014 return __last;
4015
4016 return std::move_backward(std::move(__first), std::move(__mid),
4017 std::move(__last));
4018 }
4019 else
4020 {
4021 auto __result = ranges::next(__first, __n, __last);
4022 if (__result == __last)
4023 return __last;
4024
4025 auto __dest_head = __first, __dest_tail = __result;
4026 while (__dest_head != __result)
4027 {
4028 if (__dest_tail == __last)
4029 {
4030 // If we get here, then we must have
4031 // 2*n >= distance(__first, __last)
4032 // i.e. we are shifting out at least half of the range. In
4033 // this case we can safely perform the shift with a single
4034 // move.
4035 std::move(std::move(__first), std::move(__dest_head), __result);
4036 return __result;
4037 }
4038 ++__dest_head;
4039 ++__dest_tail;
4040 }
4041
4042 for (;;)
4043 {
4044 // At the start of each iteration of this outer loop, the range
4045 // [__first, __result) contains those elements that after shifting
4046 // the whole range right by __n, should end up in
4047 // [__dest_head, __dest_tail) in order.
4048
4049 // The below inner loop swaps the elements of [__first, __result)
4050 // and [__dest_head, __dest_tail), while simultaneously shifting
4051 // the latter range by __n.
4052 auto __cursor = __first;
4053 while (__cursor != __result)
4054 {
4055 if (__dest_tail == __last)
4056 {
4057 // At this point the ranges [__first, result) and
4058 // [__dest_head, dest_tail) are disjoint, so we can safely
4059 // move the remaining elements.
4060 __dest_head = std::move(__cursor, __result,
4061 std::move(__dest_head));
4062 std::move(std::move(__first), std::move(__cursor),
4063 std::move(__dest_head));
4064 return __result;
4065 }
4066 std::iter_swap(__cursor, __dest_head);
4067 ++__dest_head;
4068 ++__dest_tail;
4069 ++__cursor;
4070 }
4071 }
4072 }
4073 }
4074
4075_GLIBCXX_END_NAMESPACE_VERSION
4076} // namespace std
4077#endif // concepts
4078#endif // C++20
4079#endif // _RANGES_ALGO_H
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1799
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2832
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.