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#include <bit> // __bit_width
36#if __cplusplus > 202002L
37#include <optional>
38#endif
40#include <bits/ranges_util.h>
41#include <bits/uniform_int_dist.h> // concept uniform_random_bit_generator
42
43#if __glibcxx_concepts
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47namespace ranges
48{
49 namespace __detail
50 {
51 template<typename _Fp>
52 using __by_ref_or_value_fn
53 = __conditional_t<is_scalar_v<_Fp> || is_empty_v<_Fp>, _Fp, _Fp&>;
54
55 template<typename _Comp, typename _Proj>
56 struct _Comp_proj
57 {
58 [[no_unique_address]] __by_ref_or_value_fn<_Comp> _M_comp;
59 [[no_unique_address]] __by_ref_or_value_fn<_Proj> _M_proj;
60
61 constexpr
62 _Comp_proj(_Comp& __comp, _Proj& __proj)
63 : _M_comp(__comp), _M_proj(__proj)
64 { }
65
66 template<typename _Tp, typename _Up>
67 constexpr bool
68 operator()(_Tp&& __x, _Up&& __y)
69 {
70 return std::__invoke(_M_comp,
71 std::__invoke(_M_proj, std::forward<_Tp>(__x)),
72 std::__invoke(_M_proj, std::forward<_Up>(__y)));
73 }
74 };
75
76 template<typename _Comp, typename _Proj>
77 constexpr _Comp_proj<_Comp, _Proj>
78 __make_comp_proj(_Comp& __comp, _Proj& __proj)
79 { return {__comp, __proj}; }
80
81 template<typename _Pred, typename _Proj>
82 struct _Pred_proj
83 {
84 [[no_unique_address]] __by_ref_or_value_fn<_Pred> _M_pred;
85 [[no_unique_address]] __by_ref_or_value_fn<_Proj> _M_proj;
86
87 constexpr
88 _Pred_proj(_Pred& __pred, _Proj& __proj)
89 : _M_pred(__pred), _M_proj(__proj)
90 { }
91
92 template<typename _Tp>
93 constexpr bool
94 operator()(_Tp&& __x)
95 {
96 return std::__invoke(_M_pred,
97 std::__invoke(_M_proj, std::forward<_Tp>(__x)));
98 }
99 };
100
101 template<typename _Pred, typename _Proj>
102 constexpr _Pred_proj<_Pred, _Proj>
103 __make_pred_proj(_Pred& __pred, _Proj& __proj)
104 { return {__pred, __proj}; }
105 } // namespace __detail
106
107 struct __all_of_fn
108 {
109 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
110 typename _Proj = identity,
111 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
112 [[nodiscard]] constexpr bool
113 operator()(_Iter __first, _Sent __last,
114 _Pred __pred, _Proj __proj = {}) const
115 {
116 for (; __first != __last; ++__first)
117 if (!(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
118 return false;
119 return true;
120 }
121
122 template<input_range _Range, typename _Proj = identity,
123 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
124 _Pred>
125 [[nodiscard]] constexpr bool
126 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
127 {
128 return (*this)(ranges::begin(__r), ranges::end(__r),
129 std::move(__pred), std::move(__proj));
130 }
131 };
132
133 inline constexpr __all_of_fn all_of{};
134
135 struct __any_of_fn
136 {
137 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
138 typename _Proj = identity,
139 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
140 [[nodiscard]] constexpr bool
141 operator()(_Iter __first, _Sent __last,
142 _Pred __pred, _Proj __proj = {}) const
143 {
144 for (; __first != __last; ++__first)
145 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
146 return true;
147 return false;
148 }
149
150 template<input_range _Range, typename _Proj = identity,
151 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
152 _Pred>
153 [[nodiscard]] constexpr bool
154 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
155 {
156 return (*this)(ranges::begin(__r), ranges::end(__r),
157 std::move(__pred), std::move(__proj));
158 }
159 };
160
161 inline constexpr __any_of_fn any_of{};
162
163 struct __none_of_fn
164 {
165 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
166 typename _Proj = identity,
167 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
168 [[nodiscard]] constexpr bool
169 operator()(_Iter __first, _Sent __last,
170 _Pred __pred, _Proj __proj = {}) const
171 {
172 for (; __first != __last; ++__first)
173 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
174 return false;
175 return true;
176 }
177
178 template<input_range _Range, typename _Proj = identity,
179 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
180 _Pred>
181 [[nodiscard]] constexpr bool
182 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
183 {
184 return (*this)(ranges::begin(__r), ranges::end(__r),
185 std::move(__pred), std::move(__proj));
186 }
187 };
188
189 inline constexpr __none_of_fn none_of{};
190
191 template<typename _Iter, typename _Fp>
192 struct in_fun_result
193 {
194 [[no_unique_address]] _Iter in;
195 [[no_unique_address]] _Fp fun;
196
197 template<typename _Iter2, typename _F2p>
198 requires convertible_to<const _Iter&, _Iter2>
199 && convertible_to<const _Fp&, _F2p>
200 constexpr
201 operator in_fun_result<_Iter2, _F2p>() const &
202 { return {in, fun}; }
203
204 template<typename _Iter2, typename _F2p>
205 requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
206 constexpr
207 operator in_fun_result<_Iter2, _F2p>() &&
208 { return {std::move(in), std::move(fun)}; }
209 };
210
211 template<typename _Iter, typename _Fp>
212 using for_each_result = in_fun_result<_Iter, _Fp>;
213
214 struct __for_each_fn
215 {
216 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
217 typename _Proj = identity,
218 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
219 constexpr for_each_result<_Iter, _Fun>
220 operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {}) const
221 {
222 for (; __first != __last; ++__first)
223 std::__invoke(__f, std::__invoke(__proj, *__first));
224 return { std::move(__first), std::move(__f) };
225 }
226
227 template<input_range _Range, typename _Proj = identity,
228 indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
229 _Fun>
230 constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
231 operator()(_Range&& __r, _Fun __f, _Proj __proj = {}) const
232 {
233 return (*this)(ranges::begin(__r), ranges::end(__r),
234 std::move(__f), std::move(__proj));
235 }
236 };
237
238 inline constexpr __for_each_fn for_each{};
239
240 template<typename _Iter, typename _Fp>
241 using for_each_n_result = in_fun_result<_Iter, _Fp>;
242
243 struct __for_each_n_fn
244 {
245 template<input_iterator _Iter, typename _Proj = identity,
246 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
247 constexpr for_each_n_result<_Iter, _Fun>
248 operator()(_Iter __first, iter_difference_t<_Iter> __n,
249 _Fun __f, _Proj __proj = {}) const
250 {
251 if constexpr (random_access_iterator<_Iter>)
252 {
253 if (__n <= 0)
254 return {std::move(__first), std::move(__f)};
255 auto __last = __first + __n;
256 return ranges::for_each(std::move(__first), std::move(__last),
257 std::move(__f), std::move(__proj));
258 }
259 else
260 {
261 while (__n-- > 0)
262 {
263 std::__invoke(__f, std::__invoke(__proj, *__first));
264 ++__first;
265 }
266 return {std::move(__first), std::move(__f)};
267 }
268 }
269 };
270
271 inline constexpr __for_each_n_fn for_each_n{};
272
273 // find, find_if and find_if_not are defined in <bits/ranges_util.h>.
274
275 struct __find_first_of_fn
276 {
277 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
278 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
279 typename _Pred = ranges::equal_to,
280 typename _Proj1 = identity, typename _Proj2 = identity>
281 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
282 [[nodiscard]] constexpr _Iter1
283 operator()(_Iter1 __first1, _Sent1 __last1,
284 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
285 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
286 {
287 for (; __first1 != __last1; ++__first1)
288 for (auto __iter = __first2; __iter != __last2; ++__iter)
289 if (std::__invoke(__pred,
290 std::__invoke(__proj1, *__first1),
291 std::__invoke(__proj2, *__iter)))
292 return __first1;
293 return __first1;
294 }
295
296 template<input_range _Range1, forward_range _Range2,
297 typename _Pred = ranges::equal_to,
298 typename _Proj1 = identity, typename _Proj2 = identity>
299 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
300 _Pred, _Proj1, _Proj2>
301 [[nodiscard]] constexpr borrowed_iterator_t<_Range1>
302 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
303 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
304 {
305 return (*this)(ranges::begin(__r1), ranges::end(__r1),
306 ranges::begin(__r2), ranges::end(__r2),
307 std::move(__pred),
308 std::move(__proj1), std::move(__proj2));
309 }
310 };
311
312 inline constexpr __find_first_of_fn find_first_of{};
313
314 struct __count_fn
315 {
316 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
317 typename _Proj = identity,
318 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
319 requires indirect_binary_predicate<ranges::equal_to,
320 projected<_Iter, _Proj>,
321 const _Tp*>
322 [[nodiscard]] constexpr iter_difference_t<_Iter>
323 operator()(_Iter __first, _Sent __last,
324 const _Tp& __value, _Proj __proj = {}) const
325 {
326 iter_difference_t<_Iter> __n = 0;
327 for (; __first != __last; ++__first)
328 if (std::__invoke(__proj, *__first) == __value)
329 ++__n;
330 return __n;
331 }
332
333 template<input_range _Range, typename _Proj = identity,
334 typename _Tp
335 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
336 requires indirect_binary_predicate<ranges::equal_to,
337 projected<iterator_t<_Range>, _Proj>,
338 const _Tp*>
339 [[nodiscard]] constexpr range_difference_t<_Range>
340 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
341 {
342 return (*this)(ranges::begin(__r), ranges::end(__r),
343 __value, std::move(__proj));
344 }
345 };
346
347 inline constexpr __count_fn count{};
348
349 struct __count_if_fn
350 {
351 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
352 typename _Proj = identity,
353 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
354 constexpr iter_difference_t<_Iter>
355 operator()(_Iter __first, _Sent __last,
356 _Pred __pred, _Proj __proj = {}) const
357 {
358 iter_difference_t<_Iter> __n = 0;
359 for (; __first != __last; ++__first)
360 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
361 ++__n;
362 return __n;
363 }
364
365 template<input_range _Range,
366 typename _Proj = identity,
367 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
368 _Pred>
369 constexpr range_difference_t<_Range>
370 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
371 {
372 return (*this)(ranges::begin(__r), ranges::end(__r),
373 std::move(__pred), std::move(__proj));
374 }
375 };
376
377 inline constexpr __count_if_fn count_if{};
378
379 // in_in_result, mismatch and search are defined in <bits/ranges_util.h>.
380
381 struct __search_n_fn
382 {
383 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
384 typename _Pred = ranges::equal_to, typename _Proj = identity,
385 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
386 requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
387 constexpr subrange<_Iter>
388 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
389 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
390 {
391 if (__count <= 0)
392 return {__first, __first};
393
394 auto __value_comp = [&] <typename _Rp> (_Rp&& __arg) -> bool {
395 return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
396 };
397 if (__count == 1)
398 {
399 __first = ranges::find_if(std::move(__first), __last,
400 std::move(__value_comp),
401 std::move(__proj));
402 if (__first == __last)
403 return {__first, __first};
404 else
405 {
406 auto __end = __first;
407 return {__first, ++__end};
408 }
409 }
410
411 if constexpr (sized_sentinel_for<_Sent, _Iter>
412 && random_access_iterator<_Iter>)
413 {
414 auto __tail_size = __last - __first;
415 auto __remainder = __count;
416
417 while (__remainder <= __tail_size)
418 {
419 __first += __remainder;
420 __tail_size -= __remainder;
421 auto __backtrack = __first;
422 while (__value_comp(std::__invoke(__proj, *--__backtrack)))
423 {
424 if (--__remainder == 0)
425 return {__first - __count, __first};
426 }
427 __remainder = __count + 1 - (__first - __backtrack);
428 }
429 auto __i = __first + __tail_size;
430 return {__i, __i};
431 }
432 else
433 {
434 __first = ranges::find_if(__first, __last, __value_comp, __proj);
435 while (__first != __last)
436 {
437 auto __n = __count;
438 auto __i = __first;
439 ++__i;
440 while (__i != __last && __n != 1
441 && __value_comp(std::__invoke(__proj, *__i)))
442 {
443 ++__i;
444 --__n;
445 }
446 if (__n == 1)
447 return {__first, __i};
448 if (__i == __last)
449 return {__i, __i};
450 __first = ranges::find_if(++__i, __last, __value_comp, __proj);
451 }
452 return {__first, __first};
453 }
454 }
455
456 template<forward_range _Range,
457 typename _Pred = ranges::equal_to, typename _Proj = identity,
458 typename _Tp
459 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
460 requires indirectly_comparable<iterator_t<_Range>, const _Tp*,
461 _Pred, _Proj>
462 constexpr borrowed_subrange_t<_Range>
463 operator()(_Range&& __r, range_difference_t<_Range> __count,
464 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
465 {
466 return (*this)(ranges::begin(__r), ranges::end(__r),
467 std::move(__count), __value,
468 std::move(__pred), std::move(__proj));
469 }
470 };
471
472 inline constexpr __search_n_fn search_n{};
473
474#if __glibcxx_ranges_starts_ends_with // C++ >= 23
475 struct __starts_with_fn
476 {
477 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
478 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
479 typename _Pred = ranges::equal_to,
480 typename _Proj1 = identity, typename _Proj2 = identity>
481 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
482 constexpr bool
483 operator()(_Iter1 __first1, _Sent1 __last1,
484 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
485 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
486 {
487 iter_difference_t<_Iter1> __n1 = -1;
488 iter_difference_t<_Iter2> __n2 = -1;
489 if constexpr (sized_sentinel_for<_Sent1, _Iter1>)
490 __n1 = __last1 - __first1;
491 if constexpr (sized_sentinel_for<_Sent2, _Iter2>)
492 __n2 = __last2 - __first2;
493 return _S_impl(std::move(__first1), __last1, __n1,
494 std::move(__first2), __last2, __n2,
495 std::move(__pred),
496 std::move(__proj1), std::move(__proj2));
497 }
498
499 template<input_range _Range1, input_range _Range2,
500 typename _Pred = ranges::equal_to,
501 typename _Proj1 = identity, typename _Proj2 = identity>
502 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
503 _Pred, _Proj1, _Proj2>
504 constexpr bool
505 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
506 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
507 {
508 range_difference_t<_Range1> __n1 = -1;
509 range_difference_t<_Range2> __n2 = -1;
510 if constexpr (sized_range<_Range1>)
511 __n1 = ranges::size(__r1);
512 if constexpr (sized_range<_Range2>)
513 __n2 = ranges::size(__r2);
514 return _S_impl(ranges::begin(__r1), ranges::end(__r1), __n1,
515 ranges::begin(__r2), ranges::end(__r2), __n2,
516 std::move(__pred),
517 std::move(__proj1), std::move(__proj2));
518 }
519
520 private:
521 template<typename _Iter1, typename _Sent1, typename _Iter2, typename _Sent2,
522 typename _Pred,
523 typename _Proj1, typename _Proj2>
524 static constexpr bool
525 _S_impl(_Iter1 __first1, _Sent1 __last1, iter_difference_t<_Iter1> __n1,
526 _Iter2 __first2, _Sent2 __last2, iter_difference_t<_Iter2> __n2,
527 _Pred __pred, _Proj1 __proj1, _Proj2 __proj2)
528 {
529 if (__first2 == __last2) [[unlikely]]
530 return true;
531 else if (__n1 == -1 || __n2 == -1)
532 return ranges::mismatch(std::move(__first1), __last1,
533 std::move(__first2), __last2,
534 std::move(__pred),
535 std::move(__proj1), std::move(__proj2)).in2 == __last2;
536 else if (__n1 < __n2)
537 return false;
538 else if constexpr (random_access_iterator<_Iter1>)
539 return ranges::equal(__first1, __first1 + iter_difference_t<_Iter1>(__n2),
540 std::move(__first2), __last2,
541 std::move(__pred),
542 std::move(__proj1), std::move(__proj2));
543 else
544 return ranges::equal(counted_iterator(std::move(__first1),
545 iter_difference_t<_Iter1>(__n2)),
546 default_sentinel,
547 std::move(__first2), __last2,
548 std::move(__pred),
549 std::move(__proj1), std::move(__proj2));
550 }
551
552 friend struct __ends_with_fn;
553 };
554
555 inline constexpr __starts_with_fn starts_with{};
556
557 struct __ends_with_fn
558 {
559 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
560 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
561 typename _Pred = ranges::equal_to,
562 typename _Proj1 = identity, typename _Proj2 = identity>
563 requires (forward_iterator<_Iter1> || sized_sentinel_for<_Sent1, _Iter1>)
564 && (forward_iterator<_Iter2> || sized_sentinel_for<_Sent2, _Iter2>)
565 && indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
566 constexpr bool
567 operator()(_Iter1 __first1, _Sent1 __last1,
568 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
569 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
570 {
571 iter_difference_t<_Iter1> __n1 = -1;
572 iter_difference_t<_Iter2> __n2 = -1;
573 if constexpr (sized_sentinel_for<_Sent1, _Iter1>)
574 __n1 = __last1 - __first1;
575 if constexpr (sized_sentinel_for<_Sent2, _Iter2>)
576 __n2 = __last2 - __first2;
577 return _S_impl(std::move(__first1), __last1, __n1,
578 std::move(__first2), __last2, __n2,
579 std::move(__pred),
580 std::move(__proj1), std::move(__proj2));
581 }
582
583 template<input_range _Range1, input_range _Range2,
584 typename _Pred = ranges::equal_to,
585 typename _Proj1 = identity, typename _Proj2 = identity>
586 requires (forward_range<_Range1> || sized_range<_Range1>)
587 && (forward_range<_Range2> || sized_range<_Range2>)
588 && indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
589 _Pred, _Proj1, _Proj2>
590 constexpr bool
591 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
592 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
593 {
594 range_difference_t<_Range1> __n1 = -1;
595 range_difference_t<_Range2> __n2 = -1;
596 if constexpr (sized_range<_Range1>)
597 __n1 = ranges::size(__r1);
598 if constexpr (sized_range<_Range2>)
599 __n2 = ranges::size(__r2);
600 return _S_impl(ranges::begin(__r1), ranges::end(__r1), __n1,
601 ranges::begin(__r2), ranges::end(__r2), __n2,
602 std::move(__pred),
603 std::move(__proj1), std::move(__proj2));
604 }
605
606 private:
607 template<typename _Iter1, typename _Sent1,
608 typename _Iter2, typename _Sent2,
609 typename _Pred,
610 typename _Proj1, typename _Proj2>
611 static constexpr bool
612 _S_impl(_Iter1 __first1, _Sent1 __last1, iter_difference_t<_Iter1> __n1,
613 _Iter2 __first2, _Sent2 __last2, iter_difference_t<_Iter2> __n2,
614 _Pred __pred, _Proj1 __proj1, _Proj2 __proj2)
615 {
616 if constexpr (!random_access_iterator<_Iter1>
617 && bidirectional_iterator<_Iter1> && same_as<_Iter1, _Sent1>
618 && bidirectional_iterator<_Iter2> && same_as<_Iter2, _Sent2>)
619 return starts_with._S_impl(std::make_reverse_iterator(__last1),
621 __n1,
624 __n2,
625 std::move(__pred),
626 std::move(__proj1), std::move(__proj2));
627
628 if (__first2 == __last2) [[unlikely]]
629 return true;
630
631 if constexpr (forward_iterator<_Iter2>)
632 if (__n2 == -1)
633 __n2 = ranges::distance(__first2, __last2);
634
635 // __glibcxx_assert(__n2 != -1);
636
637 if (__n1 != -1)
638 {
639 if (__n1 < __n2)
640 return false;
641 auto __shift = __n1 - iter_difference_t<_Iter1>(__n2);
642 if (random_access_iterator<_Iter1>
643 || !bidirectional_iterator<_Iter1>
644 || !same_as<_Iter1, _Sent1>
645 || __shift < __n2)
646 {
647 ranges::advance(__first1, __shift);
648 return ranges::equal(std::move(__first1), __last1,
649 std::move(__first2), __last2,
650 std::move(__pred),
651 std::move(__proj1), std::move(__proj2));
652 }
653 }
654
655 if constexpr (bidirectional_iterator<_Iter1> && same_as<_Iter1, _Sent1>)
656 {
657 _Iter1 __it1 = __last1;
658 if (__n1 != -1)
659 ranges::advance(__it1, -iter_difference_t<_Iter1>(__n2));
660 else
661 {
662 // We can't use ranges::advance if the haystack size is
663 // unknown, since we need to detect and return false if
664 // it's smaller than the needle.
665 iter_difference_t<_Iter2> __m = __n2;
666 while (__m != 0 && __it1 != __first1)
667 {
668 --__m;
669 --__it1;
670 }
671 if (__m != 0)
672 return false;
673 }
674 return ranges::equal(__it1, __last1,
675 std::move(__first2), __last2,
676 std::move(__pred),
677 std::move(__proj1), std::move(__proj2));
678 }
679 else if constexpr (forward_iterator<_Iter1>)
680 {
681 // __glibcxx_assert(__n1 == -1);
682 _Iter1 __prev_first1;
683 __n1 = 0;
684 while (true)
685 {
686 iter_difference_t<_Iter2> __m = __n2;
687 _Iter1 __it1 = __first1;
688 while (__m != 0 && __it1 != __last1)
689 {
690 ++__n1;
691 --__m;
692 ++__it1;
693 }
694 if (__m != 0)
695 {
696 // __glibcxx_assert(__it1 == __last1);
697 if (__n1 < __n2)
698 return false;
699 __first1 = ranges::next(__prev_first1,
700 iter_difference_t<_Iter1>(__n2 - __m));
701 break;
702 }
703 __prev_first1 = __first1;
704 __first1 = __it1;
705 }
706 return ranges::equal(__first1, __last1,
707 std::move(__first2), __last2,
708 std::move(__pred),
709 std::move(__proj1), std::move(__proj2));
710 }
711 else
712 // If the haystack is non-forward then it must be sized, in which case
713 // we already returned via the __n1 != 1 case.
714 __builtin_unreachable();
715 }
716
717 };
718
719 inline constexpr __ends_with_fn ends_with{};
720#endif // __glibcxx_ranges_starts_ends_with
721
722 struct __find_end_fn
723 {
724 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
725 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
726 typename _Pred = ranges::equal_to,
727 typename _Proj1 = identity, typename _Proj2 = identity>
728 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
729 [[nodiscard]] constexpr subrange<_Iter1>
730 operator()(_Iter1 __first1, _Sent1 __last1,
731 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
732 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
733 {
734 if constexpr (bidirectional_iterator<_Iter1>
735 && bidirectional_iterator<_Iter2>)
736 {
737 auto __i1 = ranges::next(__first1, __last1);
738 auto __i2 = ranges::next(__first2, __last2);
739 auto __rresult
740 = ranges::search(reverse_iterator<_Iter1>{__i1},
741 reverse_iterator<_Iter1>{__first1},
742 reverse_iterator<_Iter2>{__i2},
743 reverse_iterator<_Iter2>{__first2},
744 std::move(__pred),
745 std::move(__proj1), std::move(__proj2));
746 auto __result_first = ranges::end(__rresult).base();
747 auto __result_last = ranges::begin(__rresult).base();
748 if (__result_last == __first1)
749 return {__i1, __i1};
750 else
751 return {__result_first, __result_last};
752 }
753 else
754 {
755 auto __i = ranges::next(__first1, __last1);
756 if (__first2 == __last2)
757 return {__i, __i};
758
759 auto __result_begin = __i;
760 auto __result_end = __i;
761 for (;;)
762 {
763 auto __new_range = ranges::search(__first1, __last1,
764 __first2, __last2,
765 __pred, __proj1, __proj2);
766 auto __new_result_begin = ranges::begin(__new_range);
767 auto __new_result_end = ranges::end(__new_range);
768 if (__new_result_begin == __last1)
769 return {__result_begin, __result_end};
770 else
771 {
772 __result_begin = __new_result_begin;
773 __result_end = __new_result_end;
774 __first1 = __result_begin;
775 ++__first1;
776 }
777 }
778 }
779 }
780
781 template<forward_range _Range1, forward_range _Range2,
782 typename _Pred = ranges::equal_to,
783 typename _Proj1 = identity, typename _Proj2 = identity>
784 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
785 _Pred, _Proj1, _Proj2>
786 [[nodiscard]] constexpr borrowed_subrange_t<_Range1>
787 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
788 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
789 {
790 return (*this)(ranges::begin(__r1), ranges::end(__r1),
791 ranges::begin(__r2), ranges::end(__r2),
792 std::move(__pred),
793 std::move(__proj1), std::move(__proj2));
794 }
795 };
796
797 inline constexpr __find_end_fn find_end{};
798
799 // adjacent_find is defined in <bits/ranges_util.h>.
800
801 struct __is_permutation_fn
802 {
803 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
804 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
805 typename _Proj1 = identity, typename _Proj2 = identity,
806 indirect_equivalence_relation<projected<_Iter1, _Proj1>,
807 projected<_Iter2, _Proj2>> _Pred
808 = ranges::equal_to>
809 [[nodiscard]] constexpr bool
810 operator()(_Iter1 __first1, _Sent1 __last1,
811 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
812 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
813 {
814 constexpr bool __sized_iters
815 = (sized_sentinel_for<_Sent1, _Iter1>
816 && sized_sentinel_for<_Sent2, _Iter2>);
817 if constexpr (__sized_iters)
818 {
819 auto __d1 = ranges::distance(__first1, __last1);
820 auto __d2 = ranges::distance(__first2, __last2);
821 if (__d1 != __d2)
822 return false;
823 }
824
825 // Efficiently compare identical prefixes: O(N) if sequences
826 // have the same elements in the same order.
827 for (; __first1 != __last1 && __first2 != __last2;
828 ++__first1, (void)++__first2)
829 if (!(bool)std::__invoke(__pred,
830 std::__invoke(__proj1, *__first1),
831 std::__invoke(__proj2, *__first2)))
832 break;
833
834 if constexpr (__sized_iters)
835 {
836 if (__first1 == __last1)
837 return true;
838 }
839 else
840 {
841 auto __d1 = ranges::distance(__first1, __last1);
842 auto __d2 = ranges::distance(__first2, __last2);
843 if (__d1 == 0 && __d2 == 0)
844 return true;
845 if (__d1 != __d2)
846 return false;
847 }
848
849 for (auto __scan = __first1; __scan != __last1; ++__scan)
850 {
851 auto&& __scan_deref = *__scan;
852 auto&& __proj_scan =
853 std::__invoke(__proj1, std::forward<decltype(__scan_deref)>(__scan_deref));
854 auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) -> bool {
855 return std::__invoke(__pred,
856 std::forward<decltype(__proj_scan)>(__proj_scan),
857 std::forward<_Tp>(__arg));
858 };
859 if (__scan != ranges::find_if(__first1, __scan,
860 __comp_scan, __proj1))
861 continue; // We've seen this one before.
862
863 auto __matches = ranges::count_if(__first2, __last2,
864 __comp_scan, __proj2);
865 if (__matches == 0
866 || ranges::count_if(__scan, __last1,
867 __comp_scan, __proj1) != __matches)
868 return false;
869 }
870 return true;
871 }
872
873 template<forward_range _Range1, forward_range _Range2,
874 typename _Proj1 = identity, typename _Proj2 = identity,
875 indirect_equivalence_relation<
876 projected<iterator_t<_Range1>, _Proj1>,
877 projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
878 [[nodiscard]] constexpr bool
879 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
880 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
881 {
882 // _GLIBCXX_RESOLVE_LIB_DEFECTS
883 // 3560. ranges::is_permutation should short-circuit for sized_ranges
884 if constexpr (sized_range<_Range1>)
885 if constexpr (sized_range<_Range2>)
886 if (ranges::distance(__r1) != ranges::distance(__r2))
887 return false;
888
889 return (*this)(ranges::begin(__r1), ranges::end(__r1),
890 ranges::begin(__r2), ranges::end(__r2),
891 std::move(__pred),
892 std::move(__proj1), std::move(__proj2));
893 }
894 };
895
896 inline constexpr __is_permutation_fn is_permutation{};
897
898 template<typename _Iter, typename _Out>
899 using copy_if_result = in_out_result<_Iter, _Out>;
900
901 struct __copy_if_fn
902 {
903 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
904 weakly_incrementable _Out, typename _Proj = identity,
905 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
906 requires indirectly_copyable<_Iter, _Out>
907 constexpr copy_if_result<_Iter, _Out>
908 operator()(_Iter __first, _Sent __last, _Out __result,
909 _Pred __pred, _Proj __proj = {}) const
910 {
911 for (; __first != __last; ++__first)
912 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
913 {
914 *__result = *__first;
915 ++__result;
916 }
917 return {std::move(__first), std::move(__result)};
918 }
919
920 template<input_range _Range, weakly_incrementable _Out,
921 typename _Proj = identity,
922 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
923 _Pred>
924 requires indirectly_copyable<iterator_t<_Range>, _Out>
925 constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
926 operator()(_Range&& __r, _Out __result,
927 _Pred __pred, _Proj __proj = {}) const
928 {
929 return (*this)(ranges::begin(__r), ranges::end(__r),
930 std::move(__result),
931 std::move(__pred), std::move(__proj));
932 }
933 };
934
935 inline constexpr __copy_if_fn copy_if{};
936
937 template<typename _Iter1, typename _Iter2>
938 using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
939
940 struct __swap_ranges_fn
941 {
942 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
943 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
944 requires indirectly_swappable<_Iter1, _Iter2>
945 constexpr swap_ranges_result<_Iter1, _Iter2>
946 operator()(_Iter1 __first1, _Sent1 __last1,
947 _Iter2 __first2, _Sent2 __last2) const
948 {
949 for (; __first1 != __last1 && __first2 != __last2;
950 ++__first1, (void)++__first2)
951 ranges::iter_swap(__first1, __first2);
952 return {std::move(__first1), std::move(__first2)};
953 }
954
955 template<input_range _Range1, input_range _Range2>
956 requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
957 constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
958 borrowed_iterator_t<_Range2>>
959 operator()(_Range1&& __r1, _Range2&& __r2) const
960 {
961 return (*this)(ranges::begin(__r1), ranges::end(__r1),
962 ranges::begin(__r2), ranges::end(__r2));
963 }
964 };
965
966 inline constexpr __swap_ranges_fn swap_ranges{};
967
968 template<typename _Iter, typename _Out>
969 using unary_transform_result = in_out_result<_Iter, _Out>;
970
971 template<typename _Iter1, typename _Iter2, typename _Out>
972 struct in_in_out_result
973 {
974 [[no_unique_address]] _Iter1 in1;
975 [[no_unique_address]] _Iter2 in2;
976 [[no_unique_address]] _Out out;
977
978 template<typename _IIter1, typename _IIter2, typename _OOut>
979 requires convertible_to<const _Iter1&, _IIter1>
980 && convertible_to<const _Iter2&, _IIter2>
981 && convertible_to<const _Out&, _OOut>
982 constexpr
983 operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
984 { return {in1, in2, out}; }
985
986 template<typename _IIter1, typename _IIter2, typename _OOut>
987 requires convertible_to<_Iter1, _IIter1>
988 && convertible_to<_Iter2, _IIter2>
989 && convertible_to<_Out, _OOut>
990 constexpr
991 operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
992 { return {std::move(in1), std::move(in2), std::move(out)}; }
993 };
994
995 template<typename _Iter1, typename _Iter2, typename _Out>
996 using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
997
998 struct __transform_fn
999 {
1000 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1001 weakly_incrementable _Out,
1002 copy_constructible _Fp, typename _Proj = identity>
1003 requires indirectly_writable<_Out,
1004 indirect_result_t<_Fp&,
1005 projected<_Iter, _Proj>>>
1006 constexpr unary_transform_result<_Iter, _Out>
1007 operator()(_Iter __first1, _Sent __last1, _Out __result,
1008 _Fp __op, _Proj __proj = {}) const
1009 {
1010 for (; __first1 != __last1; ++__first1, (void)++__result)
1011 *__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
1012 return {std::move(__first1), std::move(__result)};
1013 }
1014
1015 template<input_range _Range, weakly_incrementable _Out,
1016 copy_constructible _Fp, typename _Proj = identity>
1017 requires indirectly_writable<_Out,
1018 indirect_result_t<_Fp&,
1019 projected<iterator_t<_Range>, _Proj>>>
1020 constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
1021 operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {}) const
1022 {
1023 return (*this)(ranges::begin(__r), ranges::end(__r),
1024 std::move(__result),
1025 std::move(__op), std::move(__proj));
1026 }
1027
1028 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
1029 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
1030 weakly_incrementable _Out, copy_constructible _Fp,
1031 typename _Proj1 = identity, typename _Proj2 = identity>
1032 requires indirectly_writable<_Out,
1033 indirect_result_t<_Fp&,
1034 projected<_Iter1, _Proj1>,
1035 projected<_Iter2, _Proj2>>>
1036 constexpr binary_transform_result<_Iter1, _Iter2, _Out>
1037 operator()(_Iter1 __first1, _Sent1 __last1,
1038 _Iter2 __first2, _Sent2 __last2,
1039 _Out __result, _Fp __binary_op,
1040 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1041 {
1042 for (; __first1 != __last1 && __first2 != __last2;
1043 ++__first1, (void)++__first2, ++__result)
1044 *__result = std::__invoke(__binary_op,
1045 std::__invoke(__proj1, *__first1),
1046 std::__invoke(__proj2, *__first2));
1047 return {std::move(__first1), std::move(__first2), std::move(__result)};
1048 }
1049
1050 template<input_range _Range1, input_range _Range2,
1051 weakly_incrementable _Out, copy_constructible _Fp,
1052 typename _Proj1 = identity, typename _Proj2 = identity>
1053 requires indirectly_writable<_Out,
1054 indirect_result_t<_Fp&,
1055 projected<iterator_t<_Range1>, _Proj1>,
1056 projected<iterator_t<_Range2>, _Proj2>>>
1057 constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
1058 borrowed_iterator_t<_Range2>, _Out>
1059 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
1060 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
1061 {
1062 return (*this)(ranges::begin(__r1), ranges::end(__r1),
1063 ranges::begin(__r2), ranges::end(__r2),
1064 std::move(__result), std::move(__binary_op),
1065 std::move(__proj1), std::move(__proj2));
1066 }
1067 };
1068
1069 inline constexpr __transform_fn transform{};
1070
1071 struct __replace_fn
1072 {
1073 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1074 typename _Proj = identity,
1075 typename _Tp1 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
1076 typename _Tp2 _GLIBCXX26_DEF_VAL_T(_Tp1)>
1077 requires indirectly_writable<_Iter, const _Tp2&>
1078 && indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
1079 const _Tp1*>
1080 constexpr _Iter
1081 operator()(_Iter __first, _Sent __last,
1082 const _Tp1& __old_value, const _Tp2& __new_value,
1083 _Proj __proj = {}) const
1084 {
1085 for (; __first != __last; ++__first)
1086 if (std::__invoke(__proj, *__first) == __old_value)
1087 *__first = __new_value;
1088 return __first;
1089 }
1090
1091 template<input_range _Range, typename _Proj = identity,
1092 typename _Tp1
1093 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
1094 typename _Tp2 _GLIBCXX26_DEF_VAL_T(_Tp1)>
1095 requires indirectly_writable<iterator_t<_Range>, const _Tp2&>
1096 && indirect_binary_predicate<ranges::equal_to,
1097 projected<iterator_t<_Range>, _Proj>,
1098 const _Tp1*>
1099 constexpr borrowed_iterator_t<_Range>
1100 operator()(_Range&& __r,
1101 const _Tp1& __old_value, const _Tp2& __new_value,
1102 _Proj __proj = {}) const
1103 {
1104 return (*this)(ranges::begin(__r), ranges::end(__r),
1105 __old_value, __new_value, std::move(__proj));
1106 }
1107 };
1108
1109 inline constexpr __replace_fn replace{};
1110
1111 struct __replace_if_fn
1112 {
1113 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1114 typename _Proj = identity,
1115 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
1116 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1117 requires indirectly_writable<_Iter, const _Tp&>
1118 constexpr _Iter
1119 operator()(_Iter __first, _Sent __last,
1120 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
1121 {
1122 for (; __first != __last; ++__first)
1123 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
1124 *__first = __new_value;
1125 return std::move(__first);
1126 }
1127
1128 template<input_range _Range, typename _Proj = identity,
1129 typename _Tp
1130 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
1131 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1132 _Pred>
1133 requires indirectly_writable<iterator_t<_Range>, const _Tp&>
1134 constexpr borrowed_iterator_t<_Range>
1135 operator()(_Range&& __r,
1136 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
1137 {
1138 return (*this)(ranges::begin(__r), ranges::end(__r),
1139 std::move(__pred), __new_value, std::move(__proj));
1140 }
1141 };
1142
1143 inline constexpr __replace_if_fn replace_if{};
1144
1145 template<typename _Iter, typename _Out>
1146 using replace_copy_result = in_out_result<_Iter, _Out>;
1147
1148 struct __replace_copy_fn
1149 {
1150 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1151 typename _Out, typename _Proj = identity,
1152 typename _Tp1 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
1153 typename _Tp2 _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
1154 requires indirectly_copyable<_Iter, _Out>
1155 && indirect_binary_predicate<ranges::equal_to,
1156 projected<_Iter, _Proj>, const _Tp1*>
1157 && output_iterator<_Out, const _Tp2&>
1158 constexpr replace_copy_result<_Iter, _Out>
1159 operator()(_Iter __first, _Sent __last, _Out __result,
1160 const _Tp1& __old_value, const _Tp2& __new_value,
1161 _Proj __proj = {}) const
1162 {
1163 for (; __first != __last; ++__first, (void)++__result)
1164 if (std::__invoke(__proj, *__first) == __old_value)
1165 *__result = __new_value;
1166 else
1167 *__result = *__first;
1168 return {std::move(__first), std::move(__result)};
1169 }
1170
1171 template<input_range _Range, typename _Out,
1172 typename _Proj = identity,
1173 typename _Tp1
1174 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
1175 typename _Tp2 _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>)>
1176 requires indirectly_copyable<iterator_t<_Range>, _Out>
1177 && indirect_binary_predicate<ranges::equal_to,
1178 projected<iterator_t<_Range>, _Proj>,
1179 const _Tp1*>
1180 && output_iterator<_Out, const _Tp2&>
1181 constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
1182 operator()(_Range&& __r, _Out __result,
1183 const _Tp1& __old_value, const _Tp2& __new_value,
1184 _Proj __proj = {}) const
1185 {
1186 return (*this)(ranges::begin(__r), ranges::end(__r),
1187 std::move(__result), __old_value,
1188 __new_value, std::move(__proj));
1189 }
1190 };
1191
1192 inline constexpr __replace_copy_fn replace_copy{};
1193
1194 template<typename _Iter, typename _Out>
1195 using replace_copy_if_result = in_out_result<_Iter, _Out>;
1196
1197 struct __replace_copy_if_fn
1198 {
1199 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1200 typename _Out,
1201 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>),
1202 typename _Proj = identity,
1203 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1204 requires indirectly_copyable<_Iter, _Out>
1205 && output_iterator<_Out, const _Tp&>
1206 constexpr replace_copy_if_result<_Iter, _Out>
1207 operator()(_Iter __first, _Sent __last, _Out __result,
1208 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
1209 {
1210 for (; __first != __last; ++__first, (void)++__result)
1211 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
1212 *__result = __new_value;
1213 else
1214 *__result = *__first;
1215 return {std::move(__first), std::move(__result)};
1216 }
1217
1218 template<input_range _Range,
1219 typename _Out,
1220 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Out>),
1221 typename _Proj = identity,
1222 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1223 _Pred>
1224 requires indirectly_copyable<iterator_t<_Range>, _Out>
1225 && output_iterator<_Out, const _Tp&>
1226 constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
1227 operator()(_Range&& __r, _Out __result,
1228 _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
1229 {
1230 return (*this)(ranges::begin(__r), ranges::end(__r),
1231 std::move(__result), std::move(__pred),
1232 __new_value, std::move(__proj));
1233 }
1234 };
1235
1236 inline constexpr __replace_copy_if_fn replace_copy_if{};
1237
1238 struct __generate_n_fn
1239 {
1240 template<input_or_output_iterator _Out, copy_constructible _Fp>
1241 requires invocable<_Fp&>
1242 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
1243 constexpr _Out
1244 operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen) const
1245 {
1246 for (; __n > 0; --__n, (void)++__first)
1247 *__first = std::__invoke(__gen);
1248 return __first;
1249 }
1250 };
1251
1252 inline constexpr __generate_n_fn generate_n{};
1253
1254 struct __generate_fn
1255 {
1256 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
1257 copy_constructible _Fp>
1258 requires invocable<_Fp&>
1259 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
1260 constexpr _Out
1261 operator()(_Out __first, _Sent __last, _Fp __gen) const
1262 {
1263 for (; __first != __last; ++__first)
1264 *__first = std::__invoke(__gen);
1265 return __first;
1266 }
1267
1268 template<typename _Range, copy_constructible _Fp>
1269 requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
1270 constexpr borrowed_iterator_t<_Range>
1271 operator()(_Range&& __r, _Fp __gen) const
1272 {
1273 return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__gen));
1274 }
1275 };
1276
1277 inline constexpr __generate_fn generate{};
1278
1279 struct __remove_if_fn
1280 {
1281 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1282 typename _Proj = identity,
1283 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1284 [[nodiscard]] constexpr subrange<_Iter>
1285 operator()(_Iter __first, _Sent __last,
1286 _Pred __pred, _Proj __proj = {}) const
1287 {
1288 __first = ranges::find_if(__first, __last, __pred, __proj);
1289 if (__first == __last)
1290 return {__first, __first};
1291
1292 auto __result = __first;
1293 ++__first;
1294 for (; __first != __last; ++__first)
1295 if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
1296 {
1297 *__result = ranges::iter_move(__first);
1298 ++__result;
1299 }
1300
1301 return {__result, __first};
1302 }
1303
1304 template<forward_range _Range, typename _Proj = identity,
1305 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1306 _Pred>
1307 requires permutable<iterator_t<_Range>>
1308 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
1309 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
1310 {
1311 return (*this)(ranges::begin(__r), ranges::end(__r),
1312 std::move(__pred), std::move(__proj));
1313 }
1314 };
1315
1316 inline constexpr __remove_if_fn remove_if{};
1317
1318 struct __remove_fn
1319 {
1320 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1321 typename _Proj = identity,
1322 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
1323 requires indirect_binary_predicate<ranges::equal_to,
1324 projected<_Iter, _Proj>,
1325 const _Tp*>
1326 [[nodiscard]] constexpr subrange<_Iter>
1327 operator()(_Iter __first, _Sent __last,
1328 const _Tp& __value, _Proj __proj = {}) const
1329 {
1330 auto __pred = [&] (auto&& __arg) -> bool {
1331 return std::forward<decltype(__arg)>(__arg) == __value;
1332 };
1333 return ranges::remove_if(__first, __last,
1334 std::move(__pred), std::move(__proj));
1335 }
1336
1337 template<forward_range _Range, typename _Proj = identity,
1338 typename _Tp
1339 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
1340 requires permutable<iterator_t<_Range>>
1341 && indirect_binary_predicate<ranges::equal_to,
1342 projected<iterator_t<_Range>, _Proj>,
1343 const _Tp*>
1344 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
1345 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
1346 {
1347 return (*this)(ranges::begin(__r), ranges::end(__r),
1348 __value, std::move(__proj));
1349 }
1350 };
1351
1352 inline constexpr __remove_fn remove{};
1353
1354 template<typename _Iter, typename _Out>
1355 using remove_copy_if_result = in_out_result<_Iter, _Out>;
1356
1357 struct __remove_copy_if_fn
1358 {
1359 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1360 weakly_incrementable _Out, typename _Proj = identity,
1361 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1362 requires indirectly_copyable<_Iter, _Out>
1363 constexpr remove_copy_if_result<_Iter, _Out>
1364 operator()(_Iter __first, _Sent __last, _Out __result,
1365 _Pred __pred, _Proj __proj = {}) const
1366 {
1367 for (; __first != __last; ++__first)
1368 if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
1369 {
1370 *__result = *__first;
1371 ++__result;
1372 }
1373 return {std::move(__first), std::move(__result)};
1374 }
1375
1376 template<input_range _Range, weakly_incrementable _Out,
1377 typename _Proj = identity,
1378 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1379 _Pred>
1380 requires indirectly_copyable<iterator_t<_Range>, _Out>
1381 constexpr remove_copy_if_result<borrowed_iterator_t<_Range>, _Out>
1382 operator()(_Range&& __r, _Out __result,
1383 _Pred __pred, _Proj __proj = {}) const
1384 {
1385 return (*this)(ranges::begin(__r), ranges::end(__r),
1386 std::move(__result),
1387 std::move(__pred), std::move(__proj));
1388 }
1389 };
1390
1391 inline constexpr __remove_copy_if_fn remove_copy_if{};
1392
1393 template<typename _Iter, typename _Out>
1394 using remove_copy_result = in_out_result<_Iter, _Out>;
1395
1396 struct __remove_copy_fn
1397 {
1398 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1399 weakly_incrementable _Out, typename _Proj = identity,
1400 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
1401 requires indirectly_copyable<_Iter, _Out>
1402 && indirect_binary_predicate<ranges::equal_to,
1403 projected<_Iter, _Proj>,
1404 const _Tp*>
1405 constexpr remove_copy_result<_Iter, _Out>
1406 operator()(_Iter __first, _Sent __last, _Out __result,
1407 const _Tp& __value, _Proj __proj = {}) const
1408 {
1409 for (; __first != __last; ++__first)
1410 if (!(std::__invoke(__proj, *__first) == __value))
1411 {
1412 *__result = *__first;
1413 ++__result;
1414 }
1415 return {std::move(__first), std::move(__result)};
1416 }
1417
1418 template<input_range _Range, weakly_incrementable _Out,
1419 typename _Proj = identity,
1420 typename _Tp
1421 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
1422 requires indirectly_copyable<iterator_t<_Range>, _Out>
1423 && indirect_binary_predicate<ranges::equal_to,
1424 projected<iterator_t<_Range>, _Proj>,
1425 const _Tp*>
1426 constexpr remove_copy_result<borrowed_iterator_t<_Range>, _Out>
1427 operator()(_Range&& __r, _Out __result,
1428 const _Tp& __value, _Proj __proj = {}) const
1429 {
1430 return (*this)(ranges::begin(__r), ranges::end(__r),
1431 std::move(__result), __value, std::move(__proj));
1432 }
1433 };
1434
1435 inline constexpr __remove_copy_fn remove_copy{};
1436
1437 struct __unique_fn
1438 {
1439 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1440 typename _Proj = identity,
1441 indirect_equivalence_relation<
1442 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1443 [[nodiscard]] constexpr subrange<_Iter>
1444 operator()(_Iter __first, _Sent __last,
1445 _Comp __comp = {}, _Proj __proj = {}) const
1446 {
1447 __first = ranges::adjacent_find(__first, __last, __comp, __proj);
1448 if (__first == __last)
1449 return {__first, __first};
1450
1451 auto __dest = __first;
1452 ++__first;
1453 while (++__first != __last)
1454 if (!std::__invoke(__comp,
1455 std::__invoke(__proj, *__dest),
1456 std::__invoke(__proj, *__first)))
1457 *++__dest = ranges::iter_move(__first);
1458 return {++__dest, __first};
1459 }
1460
1461 template<forward_range _Range, typename _Proj = identity,
1462 indirect_equivalence_relation<
1463 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1464 requires permutable<iterator_t<_Range>>
1465 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
1466 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
1467 {
1468 return (*this)(ranges::begin(__r), ranges::end(__r),
1469 std::move(__comp), std::move(__proj));
1470 }
1471 };
1472
1473 inline constexpr __unique_fn unique{};
1474
1475 namespace __detail
1476 {
1477 template<typename _Out, typename _Tp>
1478 concept __can_reread_output = input_iterator<_Out>
1479 && same_as<_Tp, iter_value_t<_Out>>;
1480 }
1481
1482 template<typename _Iter, typename _Out>
1483 using unique_copy_result = in_out_result<_Iter, _Out>;
1484
1485 struct __unique_copy_fn
1486 {
1487 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1488 weakly_incrementable _Out, typename _Proj = identity,
1489 indirect_equivalence_relation<
1490 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1491 requires indirectly_copyable<_Iter, _Out>
1492 && (forward_iterator<_Iter>
1493 || __detail::__can_reread_output<_Out, iter_value_t<_Iter>>
1494 || indirectly_copyable_storable<_Iter, _Out>)
1495 constexpr unique_copy_result<_Iter, _Out>
1496 operator()(_Iter __first, _Sent __last, _Out __result,
1497 _Comp __comp = {}, _Proj __proj = {}) const
1498 {
1499 if (__first == __last)
1500 return {std::move(__first), std::move(__result)};
1501
1502 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1503 // 4269. unique_copy passes arguments to its predicate backwards
1504
1505 // TODO: perform a closer comparison with reference implementations
1506 if constexpr (forward_iterator<_Iter>)
1507 {
1508 auto __next = __first;
1509 *__result = *__next;
1510 while (++__next != __last)
1511 if (!std::__invoke(__comp,
1512 std::__invoke(__proj, *__first),
1513 std::__invoke(__proj, *__next)))
1514 {
1515 __first = __next;
1516 *++__result = *__first;
1517 }
1518 return {__next, std::move(++__result)};
1519 }
1520 else if constexpr (__detail::__can_reread_output<_Out, iter_value_t<_Iter>>)
1521 {
1522 *__result = *__first;
1523 while (++__first != __last)
1524 if (!std::__invoke(__comp,
1525 std::__invoke(__proj, *__result),
1526 std::__invoke(__proj, *__first)))
1527 *++__result = *__first;
1528 return {std::move(__first), std::move(++__result)};
1529 }
1530 else // indirectly_copyable_storable<_Iter, _Out>
1531 {
1532 auto __value = *__first;
1533 *__result = __value;
1534 while (++__first != __last)
1535 {
1536 if (!(bool)std::__invoke(__comp,
1537 std::__invoke(__proj, __value),
1538 std::__invoke(__proj, *__first)))
1539 {
1540 __value = *__first;
1541 *++__result = __value;
1542 }
1543 }
1544 return {std::move(__first), std::move(++__result)};
1545 }
1546 }
1547
1548 template<input_range _Range,
1549 weakly_incrementable _Out, typename _Proj = identity,
1550 indirect_equivalence_relation<
1551 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1552 requires indirectly_copyable<iterator_t<_Range>, _Out>
1553 && (forward_iterator<iterator_t<_Range>>
1554 || __detail::__can_reread_output<_Out, range_value_t<_Range>>
1555 || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
1556 constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
1557 operator()(_Range&& __r, _Out __result,
1558 _Comp __comp = {}, _Proj __proj = {}) const
1559 {
1560 return (*this)(ranges::begin(__r), ranges::end(__r),
1561 std::move(__result),
1562 std::move(__comp), std::move(__proj));
1563 }
1564 };
1565
1566 inline constexpr __unique_copy_fn unique_copy{};
1567
1568 struct __reverse_fn
1569 {
1570 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
1571 requires permutable<_Iter>
1572 constexpr _Iter
1573 operator()(_Iter __first, _Sent __last) const
1574 {
1575 auto __i = ranges::next(__first, __last);
1576 auto __tail = __i;
1577
1578 if constexpr (random_access_iterator<_Iter>)
1579 {
1580 if (__first != __last)
1581 {
1582 --__tail;
1583 while (__first < __tail)
1584 {
1585 ranges::iter_swap(__first, __tail);
1586 ++__first;
1587 --__tail;
1588 }
1589 }
1590 return __i;
1591 }
1592 else
1593 {
1594 for (;;)
1595 if (__first == __tail || __first == --__tail)
1596 break;
1597 else
1598 {
1599 ranges::iter_swap(__first, __tail);
1600 ++__first;
1601 }
1602 return __i;
1603 }
1604 }
1605
1606 template<bidirectional_range _Range>
1607 requires permutable<iterator_t<_Range>>
1608 constexpr borrowed_iterator_t<_Range>
1609 operator()(_Range&& __r) const
1610 {
1611 return (*this)(ranges::begin(__r), ranges::end(__r));
1612 }
1613 };
1614
1615 inline constexpr __reverse_fn reverse{};
1616
1617 template<typename _Iter, typename _Out>
1618 using reverse_copy_result = in_out_result<_Iter, _Out>;
1619
1620 struct __reverse_copy_fn
1621 {
1622 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
1623 weakly_incrementable _Out>
1624 requires indirectly_copyable<_Iter, _Out>
1625 constexpr reverse_copy_result<_Iter, _Out>
1626 operator()(_Iter __first, _Sent __last, _Out __result) const
1627 {
1628 auto __i = ranges::next(__first, __last);
1629 auto __tail = __i;
1630 while (__first != __tail)
1631 {
1632 --__tail;
1633 *__result = *__tail;
1634 ++__result;
1635 }
1636 return {__i, std::move(__result)};
1637 }
1638
1639 template<bidirectional_range _Range, weakly_incrementable _Out>
1640 requires indirectly_copyable<iterator_t<_Range>, _Out>
1641 constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
1642 operator()(_Range&& __r, _Out __result) const
1643 {
1644 return (*this)(ranges::begin(__r), ranges::end(__r),
1645 std::move(__result));
1646 }
1647 };
1648
1649 inline constexpr __reverse_copy_fn reverse_copy{};
1650
1651 struct __rotate_fn
1652 {
1653 template<permutable _Iter, sentinel_for<_Iter> _Sent>
1654 constexpr subrange<_Iter>
1655 operator()(_Iter __first, _Iter __middle, _Sent __last) const
1656 {
1657 auto __lasti = ranges::next(__first, __last);
1658 if (__first == __middle)
1659 return {__lasti, __lasti};
1660 if (__last == __middle)
1661 return {std::move(__first), std::move(__lasti)};
1662
1663 if constexpr (random_access_iterator<_Iter>)
1664 {
1665 auto __n = __lasti - __first;
1666 auto __k = __middle - __first;
1667
1668 if (__k == __n - __k)
1669 {
1670 ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
1671 return {std::move(__middle), std::move(__lasti)};
1672 }
1673
1674 auto __p = __first;
1675 auto __ret = __first + (__lasti - __middle);
1676
1677 for (;;)
1678 {
1679 if (__k < __n - __k)
1680 {
1681 // TODO: is_pod is deprecated, but this condition is
1682 // consistent with the STL implementation.
1683 if constexpr (__is_pod(iter_value_t<_Iter>))
1684 if (__k == 1)
1685 {
1686 auto __mid = ranges::next(__p, __n - 1);
1687 auto __end = ranges::next(__mid);
1688 auto __t = ranges::iter_move(__p);
1689 ranges::move(ranges::next(__p), __end, __p);
1690 *__mid = std::move(__t);
1691 return {std::move(__ret), std::move(__lasti)};
1692 }
1693 auto __q = __p + __k;
1694 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1695 {
1696 ranges::iter_swap(__p, __q);
1697 ++__p;
1698 ++__q;
1699 }
1700 __n %= __k;
1701 if (__n == 0)
1702 return {std::move(__ret), std::move(__lasti)};
1703 ranges::swap(__n, __k);
1704 __k = __n - __k;
1705 }
1706 else
1707 {
1708 __k = __n - __k;
1709 // TODO: is_pod is deprecated, but this condition is
1710 // consistent with the STL implementation.
1711 if constexpr (__is_pod(iter_value_t<_Iter>))
1712 if (__k == 1)
1713 {
1714 auto __mid = ranges::next(__p, __n - 1);
1715 auto __end = ranges::next(__mid);
1716 auto __t = ranges::iter_move(__mid);
1717 ranges::move_backward(__p, __mid, __end);
1718 *__p = std::move(__t);
1719 return {std::move(__ret), std::move(__lasti)};
1720 }
1721 auto __q = __p + __n;
1722 __p = __q - __k;
1723 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1724 {
1725 --__p;
1726 --__q;
1727 ranges::iter_swap(__p, __q);
1728 }
1729 __n %= __k;
1730 if (__n == 0)
1731 return {std::move(__ret), std::move(__lasti)};
1732 std::swap(__n, __k);
1733 }
1734 }
1735 }
1736 else if constexpr (bidirectional_iterator<_Iter>)
1737 {
1738 auto __tail = __lasti;
1739
1740 ranges::reverse(__first, __middle);
1741 ranges::reverse(__middle, __tail);
1742
1743 while (__first != __middle && __middle != __tail)
1744 {
1745 ranges::iter_swap(__first, --__tail);
1746 ++__first;
1747 }
1748
1749 if (__first == __middle)
1750 {
1751 ranges::reverse(__middle, __tail);
1752 return {std::move(__tail), std::move(__lasti)};
1753 }
1754 else
1755 {
1756 ranges::reverse(__first, __middle);
1757 return {std::move(__first), std::move(__lasti)};
1758 }
1759 }
1760 else
1761 {
1762 auto __first2 = __middle;
1763 do
1764 {
1765 ranges::iter_swap(__first, __first2);
1766 ++__first;
1767 ++__first2;
1768 if (__first == __middle)
1769 __middle = __first2;
1770 } while (__first2 != __last);
1771
1772 auto __ret = __first;
1773
1774 __first2 = __middle;
1775
1776 while (__first2 != __last)
1777 {
1778 ranges::iter_swap(__first, __first2);
1779 ++__first;
1780 ++__first2;
1781 if (__first == __middle)
1782 __middle = __first2;
1783 else if (__first2 == __last)
1784 __first2 = __middle;
1785 }
1786 return {std::move(__ret), std::move(__lasti)};
1787 }
1788 }
1789
1790 template<forward_range _Range>
1791 requires permutable<iterator_t<_Range>>
1792 constexpr borrowed_subrange_t<_Range>
1793 operator()(_Range&& __r, iterator_t<_Range> __middle) const
1794 {
1795 return (*this)(ranges::begin(__r), std::move(__middle),
1796 ranges::end(__r));
1797 }
1798 };
1799
1800 inline constexpr __rotate_fn rotate{};
1801
1802 template<typename _Iter, typename _Out>
1803 using rotate_copy_result = in_out_result<_Iter, _Out>;
1804
1805 struct __rotate_copy_fn
1806 {
1807 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1808 weakly_incrementable _Out>
1809 requires indirectly_copyable<_Iter, _Out>
1810 constexpr rotate_copy_result<_Iter, _Out>
1811 operator()(_Iter __first, _Iter __middle, _Sent __last,
1812 _Out __result) const
1813 {
1814 auto __copy1 = ranges::copy(__middle,
1815 std::move(__last),
1816 std::move(__result));
1817 auto __copy2 = ranges::copy(std::move(__first),
1818 std::move(__middle),
1819 std::move(__copy1.out));
1820 return { std::move(__copy1.in), std::move(__copy2.out) };
1821 }
1822
1823 template<forward_range _Range, weakly_incrementable _Out>
1824 requires indirectly_copyable<iterator_t<_Range>, _Out>
1825 constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
1826 operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
1827 {
1828 return (*this)(ranges::begin(__r), std::move(__middle),
1829 ranges::end(__r), std::move(__result));
1830 }
1831 };
1832
1833 inline constexpr __rotate_copy_fn rotate_copy{};
1834
1835 struct __sample_fn
1836 {
1837 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1838 weakly_incrementable _Out, typename _Gen>
1839 requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
1840 && indirectly_copyable<_Iter, _Out>
1841 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1842 _Out
1843 operator()(_Iter __first, _Sent __last, _Out __out,
1844 iter_difference_t<_Iter> __n, _Gen&& __g) const
1845 {
1846 // FIXME: Correctly handle integer-class difference types.
1847 if constexpr (forward_iterator<_Iter>)
1848 {
1849 using _Size = iter_difference_t<_Iter>;
1850 using __distrib_type = uniform_int_distribution<_Size>;
1851 using __param_type = typename __distrib_type::param_type;
1852 using _USize = __detail::__make_unsigned_like_t<_Size>;
1853 using __uc_type
1854 = common_type_t<typename remove_reference_t<_Gen>::result_type, _USize>;
1855
1856 if (__first == __last)
1857 return __out;
1858
1859 __distrib_type __d{};
1860 _Size __unsampled_sz = ranges::distance(__first, __last);
1861 __n = std::min(__n, __unsampled_sz);
1862
1863 // If possible, we use __gen_two_uniform_ints to efficiently produce
1864 // two random numbers using a single distribution invocation:
1865
1866 const __uc_type __urngrange = __g.max() - __g.min();
1867 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
1868 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
1869 // wrapping issues.
1870 {
1871 while (__n != 0 && __unsampled_sz >= 2)
1872 {
1873 const pair<_Size, _Size> __p =
1874 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
1875
1876 --__unsampled_sz;
1877 if (__p.first < __n)
1878 {
1879 *__out = *__first;
1880 ++__out;
1881 --__n;
1882 }
1883
1884 ++__first;
1885
1886 if (__n == 0) break;
1887
1888 --__unsampled_sz;
1889 if (__p.second < __n)
1890 {
1891 *__out = *__first;
1892 ++__out;
1893 --__n;
1894 }
1895
1896 ++__first;
1897 }
1898 }
1899
1900 // The loop above is otherwise equivalent to this one-at-a-time version:
1901
1902 for (; __n != 0; ++__first)
1903 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
1904 {
1905 *__out = *__first;
1906 ++__out;
1907 --__n;
1908 }
1909 return __out;
1910 }
1911 else
1912 {
1913 using __distrib_type
1914 = uniform_int_distribution<iter_difference_t<_Iter>>;
1915 using __param_type = typename __distrib_type::param_type;
1916 __distrib_type __d{};
1917 iter_difference_t<_Iter> __sample_sz = 0;
1918 while (__first != __last && __sample_sz != __n)
1919 {
1920 __out[__sample_sz++] = *__first;
1921 ++__first;
1922 }
1923 for (auto __pop_sz = __sample_sz; __first != __last;
1924 ++__first, (void) ++__pop_sz)
1925 {
1926 const auto __k = __d(__g, __param_type{0, __pop_sz});
1927 if (__k < __n)
1928 __out[__k] = *__first;
1929 }
1930 return __out + iter_difference_t<_Out>(__sample_sz);
1931 }
1932 }
1933
1934 template<input_range _Range, weakly_incrementable _Out, typename _Gen>
1935 requires (forward_range<_Range> || random_access_iterator<_Out>)
1936 && indirectly_copyable<iterator_t<_Range>, _Out>
1937 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1938 _Out
1939 operator()(_Range&& __r, _Out __out,
1940 range_difference_t<_Range> __n, _Gen&& __g) const
1941 {
1942 return (*this)(ranges::begin(__r), ranges::end(__r),
1943 std::move(__out), __n,
1944 std::forward<_Gen>(__g));
1945 }
1946 };
1947
1948 inline constexpr __sample_fn sample{};
1949
1950 struct __shuffle_fn
1951 {
1952 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1953 typename _Gen>
1954 requires permutable<_Iter>
1955 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1956 _Iter
1957 operator()(_Iter __first, _Sent __last, _Gen&& __g) const
1958 {
1959 // FIXME: Correctly handle integer-class difference types.
1960 if (__first == __last)
1961 return __first;
1962
1963 using _DistanceType = iter_difference_t<_Iter>;
1964 using __ud_type = __detail::__make_unsigned_like_t<_DistanceType>;
1965 using __distr_type = std::uniform_int_distribution<__ud_type>;
1966 using __p_type = typename __distr_type::param_type;
1967
1968 using __uc_type
1969 = common_type_t<typename remove_reference_t<_Gen>::result_type, __ud_type>;
1970
1971 const __uc_type __urngrange = __g.max() - __g.min();
1972 const __uc_type __urange = __uc_type(__last - __first);
1973
1974 if (__urngrange / __urange >= __urange)
1975 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
1976 {
1977 _Iter __i = ranges::next(__first);
1978
1979 // Since we know the range isn't empty, an even number of elements
1980 // means an uneven number of elements /to swap/, in which case we
1981 // do the first one up front:
1982
1983 if ((__urange % 2) == 0)
1984 {
1985 __distr_type __d{0, 1};
1986 ranges::iter_swap(__i++, ranges::next(__first, __d(__g)));
1987 }
1988
1989 // Now we know that __last - __i is even, so we do the rest in pairs,
1990 // using a single distribution invocation to produce swap positions
1991 // for two successive elements at a time:
1992
1993 while (__i != __last)
1994 {
1995 const __uc_type __swap_range = __uc_type(__i - __first) + 1;
1996
1997 const pair<_DistanceType, _DistanceType> __pospos =
1998 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
1999
2000 ranges::iter_swap(__i++, ranges::next(__first, __pospos.first));
2001 ranges::iter_swap(__i++, ranges::next(__first, __pospos.second));
2002 }
2003
2004 return __i;
2005 }
2006
2007 __distr_type __d;
2008
2009 _Iter __i = ranges::next(__first);
2010 for (; __i != __last; ++__i)
2011 ranges::iter_swap(__i,
2012 ranges::next(__first,
2013 __d(__g, __p_type(0, __i - __first))));
2014
2015 return __i;
2016 }
2017
2018 template<random_access_range _Range, typename _Gen>
2019 requires permutable<iterator_t<_Range>>
2020 && uniform_random_bit_generator<remove_reference_t<_Gen>>
2021 borrowed_iterator_t<_Range>
2022 operator()(_Range&& __r, _Gen&& __g) const
2023 {
2024 return (*this)(ranges::begin(__r), ranges::end(__r),
2025 std::forward<_Gen>(__g));
2026 }
2027 };
2028
2029 inline constexpr __shuffle_fn shuffle{};
2030
2031 namespace __detail
2032 {
2033 template<typename _Iter, typename _Comp>
2034 constexpr void
2035 __push_heap(_Iter __first,
2036 iter_difference_t<_Iter> __holeIndex,
2037 iter_difference_t<_Iter> __topIndex,
2038 iter_value_t<_Iter> __value,
2039 _Comp __comp)
2040 {
2041 auto __parent = (__holeIndex - 1) / 2;
2042 while (__holeIndex > __topIndex
2043 && __comp(*(__first + __parent), __value))
2044 {
2045 *(__first + __holeIndex) = ranges::iter_move(__first + __parent);
2046 __holeIndex = __parent;
2047 __parent = (__holeIndex - 1) / 2;
2048 }
2049 *(__first + __holeIndex) = std::move(__value);
2050 }
2051 } // namespace __detail
2052
2053 struct __push_heap_fn
2054 {
2055 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2056 typename _Comp = ranges::less, typename _Proj = identity>
2057 requires sortable<_Iter, _Comp, _Proj>
2058 constexpr _Iter
2059 operator()(_Iter __first, _Sent __last,
2060 _Comp __comp = {}, _Proj __proj = {}) const
2061 {
2062 if constexpr (!same_as<_Iter, _Sent>)
2063 return (*this)(__first, ranges::next(__first, __last),
2064 std::move(__comp), std::move(__proj));
2065 else
2066 {
2067 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2068 __detail::__push_heap(__first, (__last - __first) - 1,
2069 0, ranges::iter_move(ranges::prev(__last)),
2070 __comp_proj);
2071 return __last;
2072 }
2073 }
2074
2075 template<random_access_range _Range,
2076 typename _Comp = ranges::less, typename _Proj = identity>
2077 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2078 constexpr borrowed_iterator_t<_Range>
2079 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2080 {
2081 return (*this)(ranges::begin(__r), ranges::end(__r),
2082 std::move(__comp), std::move(__proj));
2083 }
2084 };
2085
2086 inline constexpr __push_heap_fn push_heap{};
2087
2088 namespace __detail
2089 {
2090 template<typename _Iter, typename _Comp>
2091 constexpr void
2092 __adjust_heap(_Iter __first,
2093 iter_difference_t<_Iter> __holeIndex,
2094 iter_difference_t<_Iter> __len,
2095 iter_value_t<_Iter> __value,
2096 _Comp __comp)
2097 {
2098 auto __topIndex = __holeIndex;
2099 auto __secondChild = __holeIndex;
2100 while (__secondChild < (__len - 1) / 2)
2101 {
2102 __secondChild = 2 * (__secondChild + 1);
2103 if (__comp(*(__first + __secondChild),
2104 *(__first + (__secondChild - 1))))
2105 __secondChild--;
2106 *(__first + __holeIndex) = ranges::iter_move(__first + __secondChild);
2107 __holeIndex = __secondChild;
2108 }
2109 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
2110 {
2111 __secondChild = 2 * (__secondChild + 1);
2112 *(__first + __holeIndex) = ranges::iter_move(__first + (__secondChild - 1));
2113 __holeIndex = __secondChild - 1;
2114 }
2115 __detail::__push_heap(__first, __holeIndex, __topIndex,
2116 std::move(__value), __comp);
2117 }
2118
2119 template<typename _Iter, typename _Comp>
2120 constexpr void
2121 __pop_heap(_Iter __first, _Iter __last, _Iter __result, _Comp __comp)
2122 {
2123 iter_value_t<_Iter> __value = ranges::iter_move(__result);
2124 *__result = ranges::iter_move(__first);
2125 __detail::__adjust_heap(__first, 0, __last - __first,
2126 std::move(__value), __comp);
2127 }
2128 } // namespace __detail
2129
2130 struct __pop_heap_fn
2131 {
2132 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2133 typename _Comp = ranges::less, typename _Proj = identity>
2134 requires sortable<_Iter, _Comp, _Proj>
2135 constexpr _Iter
2136 operator()(_Iter __first, _Sent __last,
2137 _Comp __comp = {}, _Proj __proj = {}) const
2138 {
2139 if constexpr (!same_as<_Iter, _Sent>)
2140 return (*this)(__first, ranges::next(__first, __last),
2141 std::move(__comp), std::move(__proj));
2142 else
2143 {
2144 if (__last - __first > 1)
2145 {
2146 auto __back = ranges::prev(__last);
2147 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2148 __detail::__pop_heap(__first, __back, __back, __comp_proj);
2149 }
2150 return __last;
2151 }
2152 }
2153
2154 template<random_access_range _Range,
2155 typename _Comp = ranges::less, typename _Proj = identity>
2156 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2157 constexpr borrowed_iterator_t<_Range>
2158 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2159 {
2160 return (*this)(ranges::begin(__r), ranges::end(__r),
2161 std::move(__comp), std::move(__proj));
2162 }
2163 };
2164
2165 inline constexpr __pop_heap_fn pop_heap{};
2166
2167 struct __make_heap_fn
2168 {
2169 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2170 typename _Comp = ranges::less, typename _Proj = identity>
2171 requires sortable<_Iter, _Comp, _Proj>
2172 constexpr _Iter
2173 operator()(_Iter __first, _Sent __last,
2174 _Comp __comp = {}, _Proj __proj = {}) const
2175 {
2176 if constexpr (!same_as<_Iter, _Sent>)
2177 return (*this)(__first, ranges::next(__first, __last),
2178 std::move(__comp), std::move(__proj));
2179 else
2180 {
2181 const auto __len = __last - __first;
2182 if (__len < 2)
2183 return __last;
2184
2185 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2186 auto __parent = (__len - 2) / 2;
2187 while (true)
2188 {
2189 iter_value_t<_Iter> __value = ranges::iter_move(__first + __parent);
2190 __detail::__adjust_heap(__first, __parent, __len,
2191 std::move(__value),
2192 __comp_proj);
2193 if (__parent == 0)
2194 break;
2195 __parent--;
2196 }
2197 return __last;
2198 }
2199 }
2200
2201 template<random_access_range _Range,
2202 typename _Comp = ranges::less, typename _Proj = identity>
2203 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2204 constexpr borrowed_iterator_t<_Range>
2205 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2206 {
2207 return (*this)(ranges::begin(__r), ranges::end(__r),
2208 std::move(__comp), std::move(__proj));
2209 }
2210 };
2211
2212 inline constexpr __make_heap_fn make_heap{};
2213
2214 struct __sort_heap_fn
2215 {
2216 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2217 typename _Comp = ranges::less, typename _Proj = identity>
2218 requires sortable<_Iter, _Comp, _Proj>
2219 constexpr _Iter
2220 operator()(_Iter __first, _Sent __last,
2221 _Comp __comp = {}, _Proj __proj = {}) const
2222 {
2223 if constexpr (!same_as<_Iter, _Sent>)
2224 return (*this)(__first, ranges::next(__first, __last),
2225 std::move(__comp), std::move(__proj));
2226 else
2227 {
2228 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2229 _Iter __ret = __last;
2230 while (__last - __first > 1)
2231 {
2232 --__last;
2233 __detail::__pop_heap(__first, __last, __last, __comp_proj);
2234 }
2235 return __ret;
2236 }
2237 }
2238
2239 template<random_access_range _Range,
2240 typename _Comp = ranges::less, typename _Proj = identity>
2241 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2242 constexpr borrowed_iterator_t<_Range>
2243 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2244 {
2245 return (*this)(ranges::begin(__r), ranges::end(__r),
2246 std::move(__comp), std::move(__proj));
2247 }
2248 };
2249
2250 inline constexpr __sort_heap_fn sort_heap{};
2251
2252 struct __is_heap_until_fn
2253 {
2254 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2255 typename _Proj = identity,
2256 indirect_strict_weak_order<projected<_Iter, _Proj>>
2257 _Comp = ranges::less>
2258 constexpr _Iter
2259 operator()(_Iter __first, _Sent __last,
2260 _Comp __comp = {}, _Proj __proj = {}) const
2261 {
2262 iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
2263 iter_difference_t<_Iter> __parent = 0, __child = 1;
2264 for (; __child < __n; ++__child)
2265 if (std::__invoke(__comp,
2266 std::__invoke(__proj, *(__first + __parent)),
2267 std::__invoke(__proj, *(__first + __child))))
2268 return __first + __child;
2269 else if ((__child & 1) == 0)
2270 ++__parent;
2271
2272 return __first + __n;
2273 }
2274
2275 template<random_access_range _Range,
2276 typename _Proj = identity,
2277 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2278 _Comp = ranges::less>
2279 constexpr borrowed_iterator_t<_Range>
2280 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2281 {
2282 return (*this)(ranges::begin(__r), ranges::end(__r),
2283 std::move(__comp), std::move(__proj));
2284 }
2285 };
2286
2287 inline constexpr __is_heap_until_fn is_heap_until{};
2288
2289 struct __is_heap_fn
2290 {
2291 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2292 typename _Proj = identity,
2293 indirect_strict_weak_order<projected<_Iter, _Proj>>
2294 _Comp = ranges::less>
2295 constexpr bool
2296 operator()(_Iter __first, _Sent __last,
2297 _Comp __comp = {}, _Proj __proj = {}) const
2298 {
2299 return (__last
2300 == ranges::is_heap_until(__first, __last,
2301 std::move(__comp),
2302 std::move(__proj)));
2303 }
2304
2305 template<random_access_range _Range,
2306 typename _Proj = identity,
2307 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2308 _Comp = ranges::less>
2309 constexpr bool
2310 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2311 {
2312 return (*this)(ranges::begin(__r), ranges::end(__r),
2313 std::move(__comp), std::move(__proj));
2314 }
2315 };
2316
2317 inline constexpr __is_heap_fn is_heap{};
2318
2319 namespace __detail
2320 {
2321 template<typename _Iter, typename _Comp>
2322 constexpr void
2323 __move_median_to_first(_Iter __result, _Iter __a, _Iter __b, _Iter __c,
2324 _Comp __comp)
2325 {
2326 if (__comp(*__a, *__b))
2327 {
2328 if (__comp(*__b, *__c))
2329 ranges::iter_swap(__result, __b);
2330 else if (__comp(*__a, *__c))
2331 ranges::iter_swap(__result, __c);
2332 else
2333 ranges::iter_swap(__result, __a);
2334 }
2335 else if (__comp(*__a, *__c))
2336 ranges::iter_swap(__result, __a);
2337 else if (__comp(*__b, *__c))
2338 ranges::iter_swap(__result, __c);
2339 else
2340 ranges::iter_swap(__result, __b);
2341 }
2342
2343 template<typename _Iter, typename _Comp>
2344 constexpr void
2345 __unguarded_linear_insert(_Iter __last, _Comp __comp)
2346 {
2347 iter_value_t<_Iter> __val = ranges::iter_move(__last);
2348 _Iter __next = __last;
2349 --__next;
2350 while (__comp(__val, *__next))
2351 {
2352 *__last = ranges::iter_move(__next);
2353 __last = __next;
2354 --__next;
2355 }
2356 *__last = std::move(__val);
2357 }
2358
2359 template<typename _Iter, typename _Comp>
2360 constexpr void
2361 __insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2362 {
2363 if (__first == __last)
2364 return;
2365
2366 for (_Iter __i = ranges::next(__first); __i != __last; ++__i)
2367 {
2368 if (__comp(*__i, *__first))
2369 {
2370 iter_value_t<_Iter> __val = ranges::iter_move(__i);
2371 ranges::move_backward(__first, __i, ranges::next(__i));
2372 *__first = std::move(__val);
2373 }
2374 else
2375 __detail::__unguarded_linear_insert(__i, __comp);
2376 }
2377 }
2378
2379 template<typename _Iter, typename _Comp>
2380 constexpr void
2381 __unguarded_insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2382 {
2383 for (_Iter __i = __first; __i != __last; ++__i)
2384 __detail::__unguarded_linear_insert(__i, __comp);
2385 }
2386
2387 inline constexpr int __sort_threshold = 16;
2388
2389 template<typename _Iter, typename _Comp>
2390 constexpr void
2391 __final_insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2392 {
2393 constexpr iter_difference_t<_Iter> __threshold = __sort_threshold;
2394 if (__last - __first > __threshold)
2395 {
2396 __detail::__insertion_sort(__first, __first + __threshold, __comp);
2397 __detail::__unguarded_insertion_sort(__first + __threshold, __last,
2398 __comp);
2399 }
2400 else
2401 __detail::__insertion_sort(__first, __last, __comp);
2402 }
2403
2404 template<typename _Iter, typename _Comp>
2405 constexpr _Iter
2406 __unguarded_partition(_Iter __first, _Iter __last, _Iter __pivot, _Comp __comp)
2407 {
2408 while (true)
2409 {
2410 while (__comp(*__first, *__pivot))
2411 ++__first;
2412 --__last;
2413 while (__comp(*__pivot, *__last))
2414 --__last;
2415 if (!(__first < __last))
2416 return __first;
2417 ranges::iter_swap(__first, __last);
2418 ++__first;
2419 }
2420 }
2421
2422 template<typename _Iter, typename _Comp>
2423 constexpr _Iter
2424 __unguarded_partition_pivot(_Iter __first, _Iter __last, _Comp __comp)
2425 {
2426 _Iter __mid = __first + (__last - __first) / 2;
2427 __detail::__move_median_to_first(__first, ranges::next(__first), __mid,
2428 ranges::prev(__last), __comp);
2429 return __detail::__unguarded_partition(ranges::next(__first), __last,
2430 __first, __comp);
2431 }
2432
2433 template<typename _Iter, typename _Comp>
2434 constexpr void
2435 __heap_select(_Iter __first, _Iter __middle, _Iter __last, _Comp __comp)
2436 {
2437 ranges::make_heap(__first, __middle, __comp);
2438 for (_Iter __i = __middle; __i < __last; ++__i)
2439 if (__comp(*__i, *__first))
2440 __detail::__pop_heap(__first, __middle, __i, __comp);
2441 }
2442
2443 template<typename _Iter, typename _Comp>
2444 constexpr void
2445 __partial_sort(_Iter __first, _Iter __middle, _Iter __last, _Comp __comp)
2446 {
2447 __detail::__heap_select(__first, __middle, __last, __comp);
2448 ranges::sort_heap(__first, __middle, __comp);
2449 }
2450
2451 template<typename _Iter, typename _Comp>
2452 constexpr void
2453 __introsort_loop(_Iter __first, _Iter __last, unsigned __depth_limit, _Comp __comp)
2454 {
2455 while (__last - __first > __sort_threshold)
2456 {
2457 if (__depth_limit == 0)
2458 {
2459 __detail::__partial_sort(__first, __last, __last, __comp);
2460 return;
2461 }
2462 --__depth_limit;
2463 _Iter __cut = __detail::__unguarded_partition_pivot(__first, __last, __comp);
2464 __detail::__introsort_loop(__cut, __last, __depth_limit, __comp);
2465 __last = __cut;
2466 }
2467 }
2468 } // namespace __detail
2469
2470 struct __sort_fn
2471 {
2472 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2473 typename _Comp = ranges::less, typename _Proj = identity>
2474 requires sortable<_Iter, _Comp, _Proj>
2475 constexpr _Iter
2476 operator()(_Iter __first, _Sent __last,
2477 _Comp __comp = {}, _Proj __proj = {}) const
2478 {
2479 if constexpr (!same_as<_Iter, _Sent>)
2480 return (*this)(__first, ranges::next(__first, __last),
2481 std::move(__comp), std::move(__proj));
2482 else
2483 {
2484 if (__first != __last)
2485 {
2486 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2487 auto __n = __detail::__to_unsigned_like(__last - __first);
2488 unsigned __depth_limit = (std::__bit_width(__n) - 1) * 2;
2489 __detail::__introsort_loop(__first, __last, __depth_limit, __comp_proj);
2490 __detail::__final_insertion_sort(__first, __last, __comp_proj);
2491 }
2492 return __last;
2493 }
2494 }
2495
2496 template<random_access_range _Range,
2497 typename _Comp = ranges::less, typename _Proj = identity>
2498 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2499 constexpr borrowed_iterator_t<_Range>
2500 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2501 {
2502 return (*this)(ranges::begin(__r), ranges::end(__r),
2503 std::move(__comp), std::move(__proj));
2504 }
2505 };
2506
2507 inline constexpr __sort_fn sort{};
2508
2509 namespace __detail
2510 {
2511 // This is a helper function for the __merge_sort_loop routines.
2512 template<typename _Iter, typename _Out, typename _Comp>
2513 _Out
2514 __move_merge(_Iter __first1, _Iter __last1,
2515 _Iter __first2, _Iter __last2,
2516 _Out __result, _Comp __comp)
2517 {
2518 while (__first1 != __last1 && __first2 != __last2)
2519 {
2520 if (__comp(*__first2, *__first1))
2521 {
2522 *__result = ranges::iter_move(__first2);
2523 ++__first2;
2524 }
2525 else
2526 {
2527 *__result = ranges::iter_move(__first1);
2528 ++__first1;
2529 }
2530 ++__result;
2531 }
2532 return ranges::move(__first2, __last2,
2533 ranges::move(__first1, __last1, __result).out).out;
2534 }
2535
2536 template<typename _Iter, typename _Out, typename _Distance, typename _Comp>
2537 void
2538 __merge_sort_loop(_Iter __first, _Iter __last, _Out __result,
2539 _Distance __step_size, _Comp __comp)
2540 {
2541 const _Distance __two_step = 2 * __step_size;
2542
2543 while (__last - __first >= __two_step)
2544 {
2545 __result = __detail::__move_merge(__first, __first + __step_size,
2546 __first + __step_size,
2547 __first + __two_step,
2548 __result, __comp);
2549 __first += __two_step;
2550 }
2551 __step_size = ranges::min(_Distance(__last - __first), __step_size);
2552
2553 __detail::__move_merge(__first, __first + __step_size,
2554 __first + __step_size, __last, __result, __comp);
2555 }
2556
2557 template<typename _Iter, typename _Distance, typename _Compare>
2558 constexpr void
2559 __chunk_insertion_sort(_Iter __first, _Iter __last,
2560 _Distance __chunk_size, _Compare __comp)
2561 {
2562 while (__last - __first >= __chunk_size)
2563 {
2564 __detail::__insertion_sort(__first, __first + __chunk_size, __comp);
2565 __first += __chunk_size;
2566 }
2567 __detail::__insertion_sort(__first, __last, __comp);
2568 }
2569
2570 template<typename _Iter, typename _Pointer, typename _Comp>
2571 void
2572 __merge_sort_with_buffer(_Iter __first, _Iter __last,
2573 _Pointer __buffer, _Comp __comp)
2574 {
2575 using _Distance = iter_difference_t<_Iter>;
2576
2577 const _Distance __len = __last - __first;
2578 const _Pointer __buffer_last = __buffer + ptrdiff_t(__len);
2579
2580 constexpr int __chunk_size = 7;
2581 _Distance __step_size = __chunk_size;
2582 __detail::__chunk_insertion_sort(__first, __last, __step_size, __comp);
2583
2584 while (__step_size < __len)
2585 {
2586 __detail::__merge_sort_loop(__first, __last, __buffer,
2587 __step_size, __comp);
2588 __step_size *= 2;
2589 __detail::__merge_sort_loop(__buffer, __buffer_last, __first,
2590 ptrdiff_t(__step_size), __comp);
2591 __step_size *= 2;
2592 }
2593 }
2594
2595 template<typename _Iter, typename _Pointer, typename _Comp>
2596 void
2597 __merge_adaptive(_Iter __first, _Iter __middle, _Iter __last,
2598 iter_difference_t<_Iter> __len1,
2599 iter_difference_t<_Iter> __len2,
2600 _Pointer __buffer, _Comp __comp); // defined near inplace_merge
2601
2602 template<typename _Iter, typename _Distance, typename _Pointer, typename _Comp>
2603 void
2604 __merge_adaptive_resize(_Iter __first, _Iter __middle, _Iter __last,
2605 _Distance __len1, _Distance __len2,
2606 _Pointer __buffer, _Distance __buffer_size,
2607 _Comp __comp); // defined near inplace_merge
2608
2609 template<typename _Iter, typename _Distance, typename _Comp>
2610 constexpr void
2611 __merge_without_buffer(_Iter __first, _Iter __middle, _Iter __last,
2612 _Distance __len1, _Distance __len2,
2613 _Comp __comp); // defined near inplace_merge
2614
2615 template<typename _Iter, typename _Pointer, typename _Comp>
2616 void
2617 __stable_sort_adaptive(_Iter __first, _Iter __middle, _Iter __last,
2618 _Pointer __buffer, _Comp __comp)
2619 {
2620 __detail::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2621 __detail::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2622
2623 __detail::__merge_adaptive(__first, __middle, __last,
2624 __middle - __first, __last - __middle,
2625 __buffer, __comp);
2626 }
2627
2628 template<typename _Iter, typename _Pointer, typename _Distance, typename _Comp>
2629 void
2630 __stable_sort_adaptive_resize(_Iter __first, _Iter __last,
2631 _Pointer __buffer, _Distance __buffer_size,
2632 _Comp __comp)
2633 {
2634 const _Distance __len = (__last - __first + 1) / 2;
2635 const _Iter __middle = __first + __len;
2636 if (__len > __buffer_size)
2637 {
2638 __detail::__stable_sort_adaptive_resize(__first, __middle, __buffer,
2639 __buffer_size, __comp);
2640 __detail::__stable_sort_adaptive_resize(__middle, __last, __buffer,
2641 __buffer_size, __comp);
2642 __detail::__merge_adaptive_resize(__first, __middle, __last,
2643 _Distance(__middle - __first),
2644 _Distance(__last - __middle),
2645 __buffer, __buffer_size,
2646 __comp);
2647 }
2648 else
2649 __detail::__stable_sort_adaptive(__first, __middle, __last,
2650 __buffer, __comp);
2651 }
2652
2653 template<typename _Iter, typename _Comp>
2654 constexpr void
2655 __inplace_stable_sort(_Iter __first, _Iter __last, _Comp __comp)
2656 {
2657 if (__last - __first < 15)
2658 {
2659 __detail::__insertion_sort(__first, __last, __comp);
2660 return;
2661 }
2662 _Iter __middle = __first + (__last - __first) / 2;
2663 __detail::__inplace_stable_sort(__first, __middle, __comp);
2664 __detail::__inplace_stable_sort(__middle, __last, __comp);
2665 __detail::__merge_without_buffer(__first, __middle, __last,
2666 __middle - __first,
2667 __last - __middle,
2668 __comp);
2669 }
2670 } // namespace __detail
2671
2672 struct __stable_sort_fn
2673 {
2674 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2675 typename _Comp = ranges::less, typename _Proj = identity>
2676 requires sortable<_Iter, _Comp, _Proj>
2677 _GLIBCXX26_CONSTEXPR
2678 _Iter
2679 operator()(_Iter __first, _Sent __last,
2680 _Comp __comp = {}, _Proj __proj = {}) const
2681 {
2682 if constexpr (!same_as<_Iter, _Sent>)
2683 return (*this)(__first, ranges::next(__first, __last),
2684 std::move(__comp), std::move(__proj));
2685 else
2686 {
2687 using _DistanceType = iter_difference_t<_Iter>;
2688
2689 if (__first == __last)
2690 return __last;
2691
2692 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2693
2694#if _GLIBCXX_HOSTED
2695# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
2696 if consteval {
2697 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2698 return __last;
2699 }
2700# endif
2701
2702 typedef _Temporary_buffer<_Iter, iter_value_t<_Iter>> _TmpBuf;
2703 // __stable_sort_adaptive sorts the range in two halves,
2704 // so the buffer only needs to fit half the range at once.
2705 _TmpBuf __buf(__first, ptrdiff_t((__last - __first + 1) / 2));
2706
2707 if (__buf._M_requested_size() == __buf.size()) [[likely]]
2708 __detail::__stable_sort_adaptive(__first,
2709 __first + _DistanceType(__buf.size()),
2710 __last, __buf.begin(), __comp_proj);
2711 else if (__buf.begin()) [[unlikely]]
2712 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2713 else
2714 __detail::__stable_sort_adaptive_resize(__first, __last, __buf.begin(),
2715 _DistanceType(__buf.size()),
2716 __comp_proj);
2717#else
2718 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2719#endif
2720 return __last;
2721 }
2722 }
2723
2724 template<random_access_range _Range,
2725 typename _Comp = ranges::less, typename _Proj = identity>
2726 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2727 _GLIBCXX26_CONSTEXPR
2728 borrowed_iterator_t<_Range>
2729 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2730 {
2731 return (*this)(ranges::begin(__r), ranges::end(__r),
2732 std::move(__comp), std::move(__proj));
2733 }
2734 };
2735
2736 inline constexpr __stable_sort_fn stable_sort{};
2737
2738 struct __partial_sort_fn
2739 {
2740 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2741 typename _Comp = ranges::less, typename _Proj = identity>
2742 requires sortable<_Iter, _Comp, _Proj>
2743 constexpr _Iter
2744 operator()(_Iter __first, _Iter __middle, _Sent __last,
2745 _Comp __comp = {}, _Proj __proj = {}) const
2746 {
2747 if (__first == __middle)
2748 return ranges::next(__first, __last);
2749
2750 ranges::make_heap(__first, __middle, __comp, __proj);
2751 auto __i = __middle;
2752 for (; __i != __last; ++__i)
2753 if (std::__invoke(__comp,
2754 std::__invoke(__proj, *__i),
2755 std::__invoke(__proj, *__first)))
2756 {
2757 ranges::pop_heap(__first, __middle, __comp, __proj);
2758 ranges::iter_swap(std::prev(__middle), __i);
2759 ranges::push_heap(__first, __middle, __comp, __proj);
2760 }
2761 ranges::sort_heap(__first, __middle, __comp, __proj);
2762
2763 return __i;
2764 }
2765
2766 template<random_access_range _Range,
2767 typename _Comp = ranges::less, typename _Proj = identity>
2768 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2769 constexpr borrowed_iterator_t<_Range>
2770 operator()(_Range&& __r, iterator_t<_Range> __middle,
2771 _Comp __comp = {}, _Proj __proj = {}) const
2772 {
2773 return (*this)(ranges::begin(__r), std::move(__middle),
2774 ranges::end(__r),
2775 std::move(__comp), std::move(__proj));
2776 }
2777 };
2778
2779 inline constexpr __partial_sort_fn partial_sort{};
2780
2781 template<typename _Iter, typename _Out>
2782 using partial_sort_copy_result = in_out_result<_Iter, _Out>;
2783
2784 struct __partial_sort_copy_fn
2785 {
2786 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2787 random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2788 typename _Comp = ranges::less,
2789 typename _Proj1 = identity, typename _Proj2 = identity>
2790 requires indirectly_copyable<_Iter1, _Iter2>
2791 && sortable<_Iter2, _Comp, _Proj2>
2792 && indirect_strict_weak_order<_Comp,
2793 projected<_Iter1, _Proj1>,
2794 projected<_Iter2, _Proj2>>
2795 constexpr partial_sort_copy_result<_Iter1, _Iter2>
2796 operator()(_Iter1 __first, _Sent1 __last,
2797 _Iter2 __result_first, _Sent2 __result_last,
2798 _Comp __comp = {},
2799 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2800 {
2801 if (__result_first == __result_last)
2802 {
2803 // TODO: Eliminating the variable __lasti triggers an ICE.
2804 auto __lasti = ranges::next(std::move(__first),
2805 std::move(__last));
2806 return {std::move(__lasti), std::move(__result_first)};
2807 }
2808
2809 auto __result_real_last = __result_first;
2810 while (__first != __last && __result_real_last != __result_last)
2811 {
2812 *__result_real_last = *__first;
2813 ++__result_real_last;
2814 ++__first;
2815 }
2816
2817 ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
2818 for (; __first != __last; ++__first)
2819 if (std::__invoke(__comp,
2820 std::__invoke(__proj1, *__first),
2821 std::__invoke(__proj2, *__result_first)))
2822 {
2823 ranges::pop_heap(__result_first, __result_real_last,
2824 __comp, __proj2);
2825 *ranges::prev(__result_real_last) = *__first;
2826 ranges::push_heap(__result_first, __result_real_last,
2827 __comp, __proj2);
2828 }
2829 ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
2830
2831 return {std::move(__first), std::move(__result_real_last)};
2832 }
2833
2834 template<input_range _Range1, random_access_range _Range2,
2835 typename _Comp = ranges::less,
2836 typename _Proj1 = identity, typename _Proj2 = identity>
2837 requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
2838 && sortable<iterator_t<_Range2>, _Comp, _Proj2>
2839 && indirect_strict_weak_order<_Comp,
2840 projected<iterator_t<_Range1>, _Proj1>,
2841 projected<iterator_t<_Range2>, _Proj2>>
2842 constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
2843 borrowed_iterator_t<_Range2>>
2844 operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
2845 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2846 {
2847 return (*this)(ranges::begin(__r), ranges::end(__r),
2848 ranges::begin(__out), ranges::end(__out),
2849 std::move(__comp),
2850 std::move(__proj1), std::move(__proj2));
2851 }
2852 };
2853
2854 inline constexpr __partial_sort_copy_fn partial_sort_copy{};
2855
2856 struct __is_sorted_until_fn
2857 {
2858 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2859 typename _Proj = identity,
2860 indirect_strict_weak_order<projected<_Iter, _Proj>>
2861 _Comp = ranges::less>
2862 [[nodiscard]] constexpr _Iter
2863 operator()(_Iter __first, _Sent __last,
2864 _Comp __comp = {}, _Proj __proj = {}) const
2865 {
2866 if (__first == __last)
2867 return __first;
2868
2869 auto __next = __first;
2870 for (++__next; __next != __last; __first = __next, (void)++__next)
2871 if (std::__invoke(__comp,
2872 std::__invoke(__proj, *__next),
2873 std::__invoke(__proj, *__first)))
2874 return __next;
2875 return __next;
2876 }
2877
2878 template<forward_range _Range, typename _Proj = identity,
2879 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2880 _Comp = ranges::less>
2881 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
2882 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2883 {
2884 return (*this)(ranges::begin(__r), ranges::end(__r),
2885 std::move(__comp), std::move(__proj));
2886 }
2887 };
2888
2889 inline constexpr __is_sorted_until_fn is_sorted_until{};
2890
2891 struct __is_sorted_fn
2892 {
2893 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2894 typename _Proj = identity,
2895 indirect_strict_weak_order<projected<_Iter, _Proj>>
2896 _Comp = ranges::less>
2897 [[nodiscard]] constexpr bool
2898 operator()(_Iter __first, _Sent __last,
2899 _Comp __comp = {}, _Proj __proj = {}) const
2900 {
2901 if (__first == __last)
2902 return true;
2903
2904 auto __next = __first;
2905 for (++__next; __next != __last; __first = __next, (void)++__next)
2906 if (std::__invoke(__comp,
2907 std::__invoke(__proj, *__next),
2908 std::__invoke(__proj, *__first)))
2909 return false;
2910 return true;
2911 }
2912
2913 template<forward_range _Range, typename _Proj = identity,
2914 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2915 _Comp = ranges::less>
2916 [[nodiscard]] constexpr bool
2917 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2918 {
2919 return (*this)(ranges::begin(__r), ranges::end(__r),
2920 std::move(__comp), std::move(__proj));
2921 }
2922 };
2923
2924 inline constexpr __is_sorted_fn is_sorted{};
2925
2926 namespace __detail
2927 {
2928 template<typename _Iter, typename _Comp>
2929 constexpr void
2930 __introselect(_Iter __first, _Iter __nth, _Iter __last,
2931 iter_difference_t<_Iter> __depth_limit, _Comp __comp)
2932 {
2933 while (__last - __first > 3)
2934 {
2935 if (__depth_limit == 0)
2936 {
2937 __detail::__heap_select(__first, ranges::next(__nth), __last,
2938 __comp);
2939 // Place the nth largest element in its final position.
2940 ranges::iter_swap(__first, __nth);
2941 return;
2942 }
2943 --__depth_limit;
2944 _Iter __cut = __detail::__unguarded_partition_pivot(__first, __last, __comp);
2945 if (__cut <= __nth)
2946 __first = __cut;
2947 else
2948 __last = __cut;
2949 }
2950 __detail::__insertion_sort(__first, __last, __comp);
2951 }
2952 } // namespace __detail
2953
2954 struct __nth_element_fn
2955 {
2956 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2957 typename _Comp = ranges::less, typename _Proj = identity>
2958 requires sortable<_Iter, _Comp, _Proj>
2959 constexpr _Iter
2960 operator()(_Iter __first, _Iter __nth, _Sent __last,
2961 _Comp __comp = {}, _Proj __proj = {}) const
2962 {
2963 if constexpr (!same_as<_Iter, _Sent>)
2964 return (*this)(__first, __nth, ranges::next(__first, __last),
2965 std::move(__comp), std::move(__proj));
2966 else
2967 {
2968 if (__first == __last || __nth == __last)
2969 return __last;
2970
2971 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2972 auto __n = __detail::__to_unsigned_like(__last - __first);
2973 __detail::__introselect(__first, __nth, __last,
2974 std::__bit_width(__n) * 2,
2975 __comp_proj);
2976 return __last;
2977 }
2978 }
2979
2980 template<random_access_range _Range,
2981 typename _Comp = ranges::less, typename _Proj = identity>
2982 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2983 constexpr borrowed_iterator_t<_Range>
2984 operator()(_Range&& __r, iterator_t<_Range> __nth,
2985 _Comp __comp = {}, _Proj __proj = {}) const
2986 {
2987 return (*this)(ranges::begin(__r), std::move(__nth),
2988 ranges::end(__r), std::move(__comp), std::move(__proj));
2989 }
2990 };
2991
2992 inline constexpr __nth_element_fn nth_element{};
2993
2994 struct __lower_bound_fn
2995 {
2996 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2997 typename _Proj = identity,
2998 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2999 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3000 _Comp = ranges::less>
3001 [[nodiscard]] constexpr _Iter
3002 operator()(_Iter __first, _Sent __last,
3003 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3004 {
3005 auto __len = ranges::distance(__first, __last);
3006
3007 while (__len > 0)
3008 {
3009 auto __half = __len / 2;
3010 auto __middle = __first;
3011 ranges::advance(__middle, __half);
3012 if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
3013 {
3014 __first = __middle;
3015 ++__first;
3016 __len = __len - __half - 1;
3017 }
3018 else
3019 __len = __half;
3020 }
3021 return __first;
3022 }
3023
3024 template<forward_range _Range,
3025 typename _Proj = identity,
3026 typename _Tp
3027 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3028 indirect_strict_weak_order<const _Tp*,
3029 projected<iterator_t<_Range>, _Proj>>
3030 _Comp = ranges::less>
3031 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3032 operator()(_Range&& __r,
3033 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3034 {
3035 return (*this)(ranges::begin(__r), ranges::end(__r),
3036 __value, std::move(__comp), std::move(__proj));
3037 }
3038 };
3039
3040 inline constexpr __lower_bound_fn lower_bound{};
3041
3042 struct __upper_bound_fn
3043 {
3044 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3045 typename _Proj = identity,
3046 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3047 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3048 _Comp = ranges::less>
3049 [[nodiscard]] constexpr _Iter
3050 operator()(_Iter __first, _Sent __last,
3051 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3052 {
3053 auto __len = ranges::distance(__first, __last);
3054
3055 while (__len > 0)
3056 {
3057 auto __half = __len / 2;
3058 auto __middle = __first;
3059 ranges::advance(__middle, __half);
3060 if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
3061 __len = __half;
3062 else
3063 {
3064 __first = __middle;
3065 ++__first;
3066 __len = __len - __half - 1;
3067 }
3068 }
3069 return __first;
3070 }
3071
3072 template<forward_range _Range,
3073 typename _Proj = identity,
3074 typename _Tp
3075 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3076 indirect_strict_weak_order<const _Tp*,
3077 projected<iterator_t<_Range>, _Proj>>
3078 _Comp = ranges::less>
3079 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3080 operator()(_Range&& __r,
3081 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3082 {
3083 return (*this)(ranges::begin(__r), ranges::end(__r),
3084 __value, std::move(__comp), std::move(__proj));
3085 }
3086 };
3087
3088 inline constexpr __upper_bound_fn upper_bound{};
3089
3090 struct __equal_range_fn
3091 {
3092 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3093 typename _Proj = identity,
3094 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3095 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3096 _Comp = ranges::less>
3097 [[nodiscard]] constexpr subrange<_Iter>
3098 operator()(_Iter __first, _Sent __last,
3099 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3100 {
3101 auto __len = ranges::distance(__first, __last);
3102
3103 while (__len > 0)
3104 {
3105 auto __half = __len / 2;
3106 auto __middle = __first;
3107 ranges::advance(__middle, __half);
3108 if (std::__invoke(__comp,
3109 std::__invoke(__proj, *__middle),
3110 __value))
3111 {
3112 __first = __middle;
3113 ++__first;
3114 __len = __len - __half - 1;
3115 }
3116 else if (std::__invoke(__comp,
3117 __value,
3118 std::__invoke(__proj, *__middle)))
3119 __len = __half;
3120 else
3121 {
3122 auto __left
3123 = ranges::lower_bound(__first, __middle,
3124 __value, __comp, __proj);
3125 ranges::advance(__first, __len);
3126 auto __right
3127 = ranges::upper_bound(++__middle, __first,
3128 __value, __comp, __proj);
3129 return {__left, __right};
3130 }
3131 }
3132 return {__first, __first};
3133 }
3134
3135 template<forward_range _Range,
3136 typename _Proj = identity,
3137 typename _Tp
3138 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3139 indirect_strict_weak_order<const _Tp*,
3140 projected<iterator_t<_Range>, _Proj>>
3141 _Comp = ranges::less>
3142 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
3143 operator()(_Range&& __r, const _Tp& __value,
3144 _Comp __comp = {}, _Proj __proj = {}) const
3145 {
3146 return (*this)(ranges::begin(__r), ranges::end(__r),
3147 __value, std::move(__comp), std::move(__proj));
3148 }
3149 };
3150
3151 inline constexpr __equal_range_fn equal_range{};
3152
3153 struct __binary_search_fn
3154 {
3155 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3156 typename _Proj = identity,
3157 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3158 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3159 _Comp = ranges::less>
3160 [[nodiscard]] constexpr bool
3161 operator()(_Iter __first, _Sent __last,
3162 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3163 {
3164 auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
3165 if (__i == __last)
3166 return false;
3167 return !(bool)std::__invoke(__comp, __value,
3168 std::__invoke(__proj, *__i));
3169 }
3170
3171 template<forward_range _Range,
3172 typename _Proj = identity,
3173 typename _Tp
3174 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3175 indirect_strict_weak_order<const _Tp*,
3176 projected<iterator_t<_Range>, _Proj>>
3177 _Comp = ranges::less>
3178 [[nodiscard]] constexpr bool
3179 operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
3180 _Proj __proj = {}) const
3181 {
3182 return (*this)(ranges::begin(__r), ranges::end(__r),
3183 __value, std::move(__comp), std::move(__proj));
3184 }
3185 };
3186
3187 inline constexpr __binary_search_fn binary_search{};
3188
3189 struct __is_partitioned_fn
3190 {
3191 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3192 typename _Proj = identity,
3193 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3194 [[nodiscard]] constexpr bool
3195 operator()(_Iter __first, _Sent __last,
3196 _Pred __pred, _Proj __proj = {}) const
3197 {
3198 __first = ranges::find_if_not(std::move(__first), __last,
3199 __pred, __proj);
3200 if (__first == __last)
3201 return true;
3202 ++__first;
3203 return ranges::none_of(std::move(__first), std::move(__last),
3204 std::move(__pred), std::move(__proj));
3205 }
3206
3207 template<input_range _Range, typename _Proj = identity,
3208 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3209 _Pred>
3210 [[nodiscard]] constexpr bool
3211 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3212 {
3213 return (*this)(ranges::begin(__r), ranges::end(__r),
3214 std::move(__pred), std::move(__proj));
3215 }
3216 };
3217
3218 inline constexpr __is_partitioned_fn is_partitioned{};
3219
3220 struct __partition_fn
3221 {
3222 template<permutable _Iter, sentinel_for<_Iter> _Sent,
3223 typename _Proj = identity,
3224 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3225 constexpr subrange<_Iter>
3226 operator()(_Iter __first, _Sent __last,
3227 _Pred __pred, _Proj __proj = {}) const
3228 {
3229 if constexpr (bidirectional_iterator<_Iter>)
3230 {
3231 auto __lasti = ranges::next(__first, __last);
3232 auto __tail = __lasti;
3233 for (;;)
3234 {
3235 for (;;)
3236 if (__first == __tail)
3237 return {std::move(__first), std::move(__lasti)};
3238 else if (std::__invoke(__pred,
3239 std::__invoke(__proj, *__first)))
3240 ++__first;
3241 else
3242 break;
3243 --__tail;
3244 for (;;)
3245 if (__first == __tail)
3246 return {std::move(__first), std::move(__lasti)};
3247 else if (!(bool)std::__invoke(__pred,
3248 std::__invoke(__proj, *__tail)))
3249 --__tail;
3250 else
3251 break;
3252 ranges::iter_swap(__first, __tail);
3253 ++__first;
3254 }
3255 }
3256 else
3257 {
3258 if (__first == __last)
3259 return {__first, __first};
3260
3261 while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
3262 if (++__first == __last)
3263 return {__first, __first};
3264
3265 auto __next = __first;
3266 while (++__next != __last)
3267 if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
3268 {
3269 ranges::iter_swap(__first, __next);
3270 ++__first;
3271 }
3272
3273 return {std::move(__first), std::move(__next)};
3274 }
3275 }
3276
3277 template<forward_range _Range, typename _Proj = identity,
3278 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3279 _Pred>
3280 requires permutable<iterator_t<_Range>>
3281 constexpr borrowed_subrange_t<_Range>
3282 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3283 {
3284 return (*this)(ranges::begin(__r), ranges::end(__r),
3285 std::move(__pred), std::move(__proj));
3286 }
3287 };
3288
3289 inline constexpr __partition_fn partition{};
3290
3291#if _GLIBCXX_HOSTED
3292 namespace __detail
3293 {
3294 // Like find_if_not(), but uses and updates a count of the
3295 // remaining range length instead of comparing against an end
3296 // iterator.
3297 template<typename _Iter, typename _Pred, typename _Distance>
3298 constexpr _Iter
3299 __find_if_not_n(_Iter __first, _Distance& __len, _Pred __pred)
3300 {
3301 for (; __len; --__len, (void) ++__first)
3302 if (!__pred(*__first))
3303 break;
3304 return __first;
3305 }
3306
3307 template<typename _Iter, typename _Sent, typename _Pointer,
3308 typename _Pred, typename _Distance>
3309 constexpr subrange<_Iter>
3310 __stable_partition_adaptive(_Iter __first, _Sent __last,
3311 _Pred __pred, _Distance __len,
3312 _Pointer __buffer,
3313 _Distance __buffer_size)
3314 {
3315 if (__len == 1)
3316 return {__first, ranges::next(__first, 1)};
3317
3318 if (__len <= __buffer_size)
3319 {
3320 _Iter __result1 = __first;
3321 _Pointer __result2 = __buffer;
3322
3323 // The precondition guarantees that !__pred(__first), so
3324 // move that element to the buffer before starting the loop.
3325 // This ensures that we only call __pred once per element.
3326 *__result2 = ranges::iter_move(__first);
3327 ++__result2;
3328 ++__first;
3329 for (; __first != __last; ++__first)
3330 if (__pred(*__first))
3331 {
3332 *__result1 = ranges::iter_move(__first);
3333 ++__result1;
3334 }
3335 else
3336 {
3337 *__result2 = ranges::iter_move(__first);
3338 ++__result2;
3339 }
3340
3341 ranges::move(__buffer, __result2, __result1);
3342 return {__result1, __first};
3343 }
3344
3345 _Iter __middle = __first;
3346 ranges::advance(__middle, __len / 2);
3347 _Iter __left_split
3348 = __detail::__stable_partition_adaptive(__first, __middle, __pred,
3349 __len / 2, __buffer,
3350 __buffer_size).begin();
3351
3352 // Advance past true-predicate values to satisfy this
3353 // function's preconditions.
3354 _Distance __right_len = __len - __len / 2;
3355 _Iter __right_split = __detail::__find_if_not_n(__middle, __right_len, __pred);
3356
3357 if (__right_len)
3358 __right_split
3359 = __detail::__stable_partition_adaptive(__right_split, __last, __pred,
3360 __right_len, __buffer, __buffer_size).begin();
3361
3362 return ranges::rotate(__left_split, __middle, __right_split);
3363 }
3364 } // namespace __detail
3365
3366 struct __stable_partition_fn
3367 {
3368 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3369 typename _Proj = identity,
3370 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3371 requires permutable<_Iter>
3372 _GLIBCXX26_CONSTEXPR
3373 subrange<_Iter>
3374 operator()(_Iter __first, _Sent __last,
3375 _Pred __pred, _Proj __proj = {}) const
3376 {
3377 __first = ranges::find_if_not(__first, __last, __pred, __proj);
3378
3379 if (__first == __last)
3380 return {__first, __first};
3381
3382 using _DistanceType = iter_difference_t<_Iter>;
3383 const _DistanceType __len = ranges::distance(__first, __last);
3384
3385 auto __pred_proj = __detail::__make_pred_proj(__pred, __proj);
3386
3387#if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
3388 if consteval {
3389 // Simulate a _Temporary_buffer of length 1:
3390 iter_value_t<_Iter> __buf = ranges::iter_move(__first);
3391 *__first = std::move(__buf);
3392 return __detail::__stable_partition_adaptive(__first, __last,
3393 __pred_proj,
3394 __len, &__buf,
3395 _DistanceType(1));
3396 }
3397#endif
3398
3399 _Temporary_buffer<_Iter, iter_value_t<_Iter>> __buf(__first, ptrdiff_t(__len));
3400 return __detail::__stable_partition_adaptive(__first, __last,
3401 __pred_proj,
3402 __len, __buf.begin(),
3403 _DistanceType(__buf.size()));
3404 }
3405
3406 template<bidirectional_range _Range, typename _Proj = identity,
3407 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3408 _Pred>
3409 requires permutable<iterator_t<_Range>>
3410 _GLIBCXX26_CONSTEXPR
3411 borrowed_subrange_t<_Range>
3412 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3413 {
3414 return (*this)(ranges::begin(__r), ranges::end(__r),
3415 std::move(__pred), std::move(__proj));
3416 }
3417 };
3418
3419 inline constexpr __stable_partition_fn stable_partition{};
3420#endif
3421
3422 template<typename _Iter, typename _Out1, typename _Out2>
3423 struct in_out_out_result
3424 {
3425 [[no_unique_address]] _Iter in;
3426 [[no_unique_address]] _Out1 out1;
3427 [[no_unique_address]] _Out2 out2;
3428
3429 template<typename _IIter, typename _OOut1, typename _OOut2>
3430 requires convertible_to<const _Iter&, _IIter>
3431 && convertible_to<const _Out1&, _OOut1>
3432 && convertible_to<const _Out2&, _OOut2>
3433 constexpr
3434 operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
3435 { return {in, out1, out2}; }
3436
3437 template<typename _IIter, typename _OOut1, typename _OOut2>
3438 requires convertible_to<_Iter, _IIter>
3439 && convertible_to<_Out1, _OOut1>
3440 && convertible_to<_Out2, _OOut2>
3441 constexpr
3442 operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
3443 { return {std::move(in), std::move(out1), std::move(out2)}; }
3444 };
3445
3446 template<typename _Iter, typename _Out1, typename _Out2>
3447 using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
3448
3449 struct __partition_copy_fn
3450 {
3451 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3452 weakly_incrementable _Out1, weakly_incrementable _Out2,
3453 typename _Proj = identity,
3454 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3455 requires indirectly_copyable<_Iter, _Out1>
3456 && indirectly_copyable<_Iter, _Out2>
3457 constexpr partition_copy_result<_Iter, _Out1, _Out2>
3458 operator()(_Iter __first, _Sent __last,
3459 _Out1 __out_true, _Out2 __out_false,
3460 _Pred __pred, _Proj __proj = {}) const
3461 {
3462 for (; __first != __last; ++__first)
3463 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
3464 {
3465 *__out_true = *__first;
3466 ++__out_true;
3467 }
3468 else
3469 {
3470 *__out_false = *__first;
3471 ++__out_false;
3472 }
3473
3474 return {std::move(__first),
3475 std::move(__out_true), std::move(__out_false)};
3476 }
3477
3478 template<input_range _Range, weakly_incrementable _Out1,
3479 weakly_incrementable _Out2,
3480 typename _Proj = identity,
3481 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3482 _Pred>
3483 requires indirectly_copyable<iterator_t<_Range>, _Out1>
3484 && indirectly_copyable<iterator_t<_Range>, _Out2>
3485 constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
3486 operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
3487 _Pred __pred, _Proj __proj = {}) const
3488 {
3489 return (*this)(ranges::begin(__r), ranges::end(__r),
3490 std::move(__out_true), std::move(__out_false),
3491 std::move(__pred), std::move(__proj));
3492 }
3493 };
3494
3495 inline constexpr __partition_copy_fn partition_copy{};
3496
3497 struct __partition_point_fn
3498 {
3499 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3500 typename _Proj = identity,
3501 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3502 [[nodiscard]] constexpr _Iter
3503 operator()(_Iter __first, _Sent __last,
3504 _Pred __pred, _Proj __proj = {}) const
3505 {
3506 auto __len = ranges::distance(__first, __last);
3507
3508 while (__len > 0)
3509 {
3510 auto __half = __len / 2;
3511 auto __middle = __first;
3512 ranges::advance(__middle, __half);
3513 if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
3514 {
3515 __first = __middle;
3516 ++__first;
3517 __len = __len - __half - 1;
3518 }
3519 else
3520 __len = __half;
3521 }
3522 return __first;
3523 }
3524
3525 template<forward_range _Range, typename _Proj = identity,
3526 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3527 _Pred>
3528 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3529 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3530 {
3531 return (*this)(ranges::begin(__r), ranges::end(__r),
3532 std::move(__pred), std::move(__proj));
3533 }
3534 };
3535
3536 inline constexpr __partition_point_fn partition_point{};
3537
3538 template<typename _Iter1, typename _Iter2, typename _Out>
3539 using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
3540
3541 struct __merge_fn
3542 {
3543 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3544 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3545 weakly_incrementable _Out, typename _Comp = ranges::less,
3546 typename _Proj1 = identity, typename _Proj2 = identity>
3547 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
3548 constexpr merge_result<_Iter1, _Iter2, _Out>
3549 operator()(_Iter1 __first1, _Sent1 __last1,
3550 _Iter2 __first2, _Sent2 __last2, _Out __result,
3551 _Comp __comp = {},
3552 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3553 {
3554 while (__first1 != __last1 && __first2 != __last2)
3555 {
3556 if (std::__invoke(__comp,
3557 std::__invoke(__proj2, *__first2),
3558 std::__invoke(__proj1, *__first1)))
3559 {
3560 *__result = *__first2;
3561 ++__first2;
3562 }
3563 else
3564 {
3565 *__result = *__first1;
3566 ++__first1;
3567 }
3568 ++__result;
3569 }
3570 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
3571 std::move(__result));
3572 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
3573 std::move(__copy1.out));
3574 return { std::move(__copy1.in), std::move(__copy2.in),
3575 std::move(__copy2.out) };
3576 }
3577
3578 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
3579 typename _Comp = ranges::less,
3580 typename _Proj1 = identity, typename _Proj2 = identity>
3581 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
3582 _Comp, _Proj1, _Proj2>
3583 constexpr merge_result<borrowed_iterator_t<_Range1>,
3584 borrowed_iterator_t<_Range2>,
3585 _Out>
3586 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
3587 _Comp __comp = {},
3588 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3589 {
3590 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3591 ranges::begin(__r2), ranges::end(__r2),
3592 std::move(__result), std::move(__comp),
3593 std::move(__proj1), std::move(__proj2));
3594 }
3595 };
3596
3597 inline constexpr __merge_fn merge{};
3598
3599 namespace __detail
3600 {
3601 template<typename _Iter1, typename _Iter2, typename _Out, typename _Comp>
3602 void
3603 __move_merge_adaptive(_Iter1 __first1, _Iter1 __last1,
3604 _Iter2 __first2, _Iter2 __last2,
3605 _Out __result, _Comp __comp)
3606 {
3607 while (__first1 != __last1 && __first2 != __last2)
3608 {
3609 if (__comp(*__first2, *__first1))
3610 {
3611 *__result = ranges::iter_move(__first2);
3612 ++__first2;
3613 }
3614 else
3615 {
3616 *__result = ranges::iter_move(__first1);
3617 ++__first1;
3618 }
3619 ++__result;
3620 }
3621 if (__first1 != __last1)
3622 ranges::move(__first1, __last1, __result);
3623 }
3624
3625 template<typename _Iter1, typename _Iter2, typename _Iter3, typename _Comp>
3626 void
3627 __move_merge_adaptive_backward(_Iter1 __first1, _Iter1 __last1,
3628 _Iter2 __first2, _Iter2 __last2,
3629 _Iter3 __result, _Comp __comp)
3630 {
3631 if (__first1 == __last1)
3632 {
3633 ranges::move_backward(__first2, __last2, __result);
3634 return;
3635 }
3636 else if (__first2 == __last2)
3637 return;
3638
3639 --__last1;
3640 --__last2;
3641 while (true)
3642 {
3643 if (__comp(*__last2, *__last1))
3644 {
3645 *--__result = ranges::iter_move(__last1);
3646 if (__first1 == __last1)
3647 {
3648 ranges::move_backward(__first2, ++__last2, __result);
3649 return;
3650 }
3651 --__last1;
3652 }
3653 else
3654 {
3655 *--__result = ranges::iter_move(__last2);
3656 if (__first2 == __last2)
3657 return;
3658 --__last2;
3659 }
3660 }
3661 }
3662
3663 template<typename _Iter1, typename _Iter2>
3664 _Iter1
3665 __rotate_adaptive(_Iter1 __first, _Iter1 __middle, _Iter1 __last,
3666 iter_difference_t<_Iter1> __len1,
3667 iter_difference_t<_Iter1> __len2,
3668 _Iter2 __buffer,
3669 iter_difference_t<_Iter1> __buffer_size)
3670 {
3671 _Iter2 __buffer_end;
3672 if (__len1 > __len2 && __len2 <= __buffer_size)
3673 {
3674 if (__len2)
3675 {
3676 __buffer_end = ranges::move(__middle, __last, __buffer).out;
3677 ranges::move_backward(__first, __middle, __last);
3678 return ranges::move(__buffer, __buffer_end, __first).out;
3679 }
3680 else
3681 return __first;
3682 }
3683 else if (__len1 <= __buffer_size)
3684 {
3685 if (__len1)
3686 {
3687 __buffer_end = ranges::move(__first, __middle, __buffer).out;
3688 ranges::move(__middle, __last, __first);
3689 return ranges::move_backward(__buffer, __buffer_end, __last).out;
3690 }
3691 else
3692 return __last;
3693 }
3694 else
3695 return ranges::rotate(__first, __middle, __last).begin();
3696 }
3697
3698 template<typename _Iter, typename _Pointer, typename _Comp>
3699 void
3700 __merge_adaptive(_Iter __first, _Iter __middle, _Iter __last,
3701 iter_difference_t<_Iter> __len1,
3702 iter_difference_t<_Iter> __len2,
3703 _Pointer __buffer, _Comp __comp)
3704 {
3705 if (__len1 <= __len2)
3706 {
3707 _Pointer __buffer_end = ranges::move(__first, __middle, __buffer).out;
3708 __detail::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
3709 __first, __comp);
3710 }
3711 else
3712 {
3713 _Pointer __buffer_end = ranges::move(__middle, __last, __buffer).out;
3714 __detail::__move_merge_adaptive_backward(__first, __middle, __buffer,
3715 __buffer_end, __last, __comp);
3716 }
3717 }
3718
3719 template<typename _Iter, typename _Distance, typename _Pointer, typename _Comp>
3720 void
3721 __merge_adaptive_resize(_Iter __first, _Iter __middle, _Iter __last,
3722 _Distance __len1, _Distance __len2,
3723 _Pointer __buffer, _Distance __buffer_size,
3724 _Comp __comp)
3725 {
3726 if (__len1 <= __buffer_size || __len2 <= __buffer_size)
3727 __detail::__merge_adaptive(__first, __middle, __last,
3728 __len1, __len2, __buffer, __comp);
3729 else
3730 {
3731 _Iter __first_cut = __first;
3732 _Iter __second_cut = __middle;
3733 _Distance __len11 = 0;
3734 _Distance __len22 = 0;
3735 if (__len1 > __len2)
3736 {
3737 __len11 = __len1 / 2;
3738 ranges::advance(__first_cut, __len11);
3739 __second_cut = ranges::lower_bound(__middle, __last, *__first_cut,
3740 __comp);
3741 __len22 = ranges::distance(__middle, __second_cut);
3742 }
3743 else
3744 {
3745 __len22 = __len2 / 2;
3746 ranges::advance(__second_cut, __len22);
3747 __first_cut = ranges::upper_bound(__first, __middle, *__second_cut,
3748 __comp);
3749 __len11 = ranges::distance(__first, __first_cut);
3750 }
3751
3752 _Iter __new_middle
3753 = __detail::__rotate_adaptive(__first_cut, __middle, __second_cut,
3754 _Distance(__len1 - __len11), __len22,
3755 __buffer, __buffer_size);
3756 __detail::__merge_adaptive_resize(__first, __first_cut, __new_middle,
3757 __len11, __len22,
3758 __buffer, __buffer_size, __comp);
3759 __detail::__merge_adaptive_resize(__new_middle, __second_cut, __last,
3760 _Distance(__len1 - __len11),
3761 _Distance(__len2 - __len22),
3762 __buffer, __buffer_size, __comp);
3763 }
3764 }
3765
3766 template<typename _Iter, typename _Distance, typename _Comp>
3767 constexpr void
3768 __merge_without_buffer(_Iter __first, _Iter __middle, _Iter __last,
3769 _Distance __len1, _Distance __len2, _Comp __comp)
3770 {
3771 if (__len1 == 0 || __len2 == 0)
3772 return;
3773
3774 if (__len1 + __len2 == 2)
3775 {
3776 if (__comp(*__middle, *__first))
3777 ranges::iter_swap(__first, __middle);
3778 return;
3779 }
3780
3781 _Iter __first_cut = __first;
3782 _Iter __second_cut = __middle;
3783 _Distance __len11 = 0;
3784 _Distance __len22 = 0;
3785 if (__len1 > __len2)
3786 {
3787 __len11 = __len1 / 2;
3788 ranges::advance(__first_cut, __len11);
3789 __second_cut = ranges::lower_bound(__middle, __last, *__first_cut, __comp);
3790 __len22 = ranges::distance(__middle, __second_cut);
3791 }
3792 else
3793 {
3794 __len22 = __len2 / 2;
3795 ranges::advance(__second_cut, __len22);
3796 __first_cut = ranges::upper_bound(__first, __middle, *__second_cut, __comp);
3797 __len11 = ranges::distance(__first, __first_cut);
3798 }
3799
3800 _Iter __new_middle = ranges::rotate(__first_cut, __middle, __second_cut).begin();
3801 __detail::__merge_without_buffer(__first, __first_cut, __new_middle,
3802 __len11, __len22, __comp);
3803 __detail::__merge_without_buffer(__new_middle, __second_cut, __last,
3804 __len1 - __len11, __len2 - __len22, __comp);
3805 }
3806 } // namespace __detail
3807
3808 struct __inplace_merge_fn
3809 {
3810 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3811 typename _Comp = ranges::less,
3812 typename _Proj = identity>
3813 requires sortable<_Iter, _Comp, _Proj>
3814 _GLIBCXX26_CONSTEXPR
3815 _Iter
3816 operator()(_Iter __first, _Iter __middle, _Sent __last,
3817 _Comp __comp = {}, _Proj __proj = {}) const
3818 {
3819 if constexpr (!same_as<_Iter, _Sent>)
3820 return (*this)(__first, __middle, ranges::next(__middle, __last),
3821 std::move(__comp), std::move(__proj));
3822 else
3823 {
3824 using _DistanceType = iter_difference_t<_Iter>;
3825
3826 if (__first == __middle || __middle == __last)
3827 return __last;
3828
3829 const _DistanceType __len1 = ranges::distance(__first, __middle);
3830 const _DistanceType __len2 = ranges::distance(__middle, __last);
3831
3832 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3833
3834#if _GLIBCXX_HOSTED
3835# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
3836 if consteval {
3837 __detail::__merge_without_buffer(__first, __middle, __last,
3838 __len1, __len2, __comp_proj);
3839 return __last;
3840 }
3841# endif
3842 using _TmpBuf = _Temporary_buffer<_Iter, iter_value_t<_Iter>>;
3843 // __merge_adaptive will use a buffer for the smaller of
3844 // [first,middle) and [middle,last).
3845 _TmpBuf __buf(__first, ptrdiff_t(ranges::min(__len1, __len2)));
3846
3847 if (__buf.size() == __buf._M_requested_size()) [[likely]]
3848 __detail::__merge_adaptive
3849 (__first, __middle, __last, __len1, __len2, __buf.begin(), __comp_proj);
3850 else if (__buf.begin() == 0) [[unlikely]]
3851 __detail::__merge_without_buffer
3852 (__first, __middle, __last, __len1, __len2, __comp_proj);
3853 else
3854 __detail::__merge_adaptive_resize
3855 (__first, __middle, __last, __len1, __len2, __buf.begin(),
3856 _DistanceType(__buf.size()), __comp_proj);
3857#else
3858 __detail::__merge_without_buffer
3859 (__first, __middle, __last, __len1, __len2, __comp_proj);
3860#endif
3861 return __last;
3862 }
3863 }
3864
3865 template<bidirectional_range _Range,
3866 typename _Comp = ranges::less, typename _Proj = identity>
3867 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3868 _GLIBCXX26_CONSTEXPR
3869 borrowed_iterator_t<_Range>
3870 operator()(_Range&& __r, iterator_t<_Range> __middle,
3871 _Comp __comp = {}, _Proj __proj = {}) const
3872 {
3873 return (*this)(ranges::begin(__r), std::move(__middle),
3874 ranges::end(__r),
3875 std::move(__comp), std::move(__proj));
3876 }
3877 };
3878
3879 inline constexpr __inplace_merge_fn inplace_merge{};
3880
3881 struct __includes_fn
3882 {
3883 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3884 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3885 typename _Proj1 = identity, typename _Proj2 = identity,
3886 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
3887 projected<_Iter2, _Proj2>>
3888 _Comp = ranges::less>
3889 [[nodiscard]] constexpr bool
3890 operator()(_Iter1 __first1, _Sent1 __last1,
3891 _Iter2 __first2, _Sent2 __last2,
3892 _Comp __comp = {},
3893 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3894 {
3895 while (__first1 != __last1 && __first2 != __last2)
3896 if (std::__invoke(__comp,
3897 std::__invoke(__proj2, *__first2),
3898 std::__invoke(__proj1, *__first1)))
3899 return false;
3900 else if (std::__invoke(__comp,
3901 std::__invoke(__proj1, *__first1),
3902 std::__invoke(__proj2, *__first2)))
3903 ++__first1;
3904 else
3905 {
3906 ++__first1;
3907 ++__first2;
3908 }
3909
3910 return __first2 == __last2;
3911 }
3912
3913 template<input_range _Range1, input_range _Range2,
3914 typename _Proj1 = identity, typename _Proj2 = identity,
3915 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
3916 projected<iterator_t<_Range2>, _Proj2>>
3917 _Comp = ranges::less>
3918 [[nodiscard]] constexpr bool
3919 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
3920 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3921 {
3922 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3923 ranges::begin(__r2), ranges::end(__r2),
3924 std::move(__comp),
3925 std::move(__proj1), std::move(__proj2));
3926 }
3927 };
3928
3929 inline constexpr __includes_fn includes{};
3930
3931 template<typename _Iter1, typename _Iter2, typename _Out>
3932 using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
3933
3934 struct __set_union_fn
3935 {
3936 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3937 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3938 weakly_incrementable _Out, typename _Comp = ranges::less,
3939 typename _Proj1 = identity, typename _Proj2 = identity>
3940 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
3941 constexpr set_union_result<_Iter1, _Iter2, _Out>
3942 operator()(_Iter1 __first1, _Sent1 __last1,
3943 _Iter2 __first2, _Sent2 __last2,
3944 _Out __result, _Comp __comp = {},
3945 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3946 {
3947 while (__first1 != __last1 && __first2 != __last2)
3948 {
3949 if (std::__invoke(__comp,
3950 std::__invoke(__proj1, *__first1),
3951 std::__invoke(__proj2, *__first2)))
3952 {
3953 *__result = *__first1;
3954 ++__first1;
3955 }
3956 else if (std::__invoke(__comp,
3957 std::__invoke(__proj2, *__first2),
3958 std::__invoke(__proj1, *__first1)))
3959 {
3960 *__result = *__first2;
3961 ++__first2;
3962 }
3963 else
3964 {
3965 *__result = *__first1;
3966 ++__first1;
3967 ++__first2;
3968 }
3969 ++__result;
3970 }
3971 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
3972 std::move(__result));
3973 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
3974 std::move(__copy1.out));
3975 return {std::move(__copy1.in), std::move(__copy2.in),
3976 std::move(__copy2.out)};
3977 }
3978
3979 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
3980 typename _Comp = ranges::less,
3981 typename _Proj1 = identity, typename _Proj2 = identity>
3982 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
3983 _Comp, _Proj1, _Proj2>
3984 constexpr set_union_result<borrowed_iterator_t<_Range1>,
3985 borrowed_iterator_t<_Range2>, _Out>
3986 operator()(_Range1&& __r1, _Range2&& __r2,
3987 _Out __result, _Comp __comp = {},
3988 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3989 {
3990 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3991 ranges::begin(__r2), ranges::end(__r2),
3992 std::move(__result), std::move(__comp),
3993 std::move(__proj1), std::move(__proj2));
3994 }
3995 };
3996
3997 inline constexpr __set_union_fn set_union{};
3998
3999 template<typename _Iter1, typename _Iter2, typename _Out>
4000 using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
4001
4002 struct __set_intersection_fn
4003 {
4004 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4005 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4006 weakly_incrementable _Out, typename _Comp = ranges::less,
4007 typename _Proj1 = identity, typename _Proj2 = identity>
4008 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
4009 constexpr set_intersection_result<_Iter1, _Iter2, _Out>
4010 operator()(_Iter1 __first1, _Sent1 __last1,
4011 _Iter2 __first2, _Sent2 __last2, _Out __result,
4012 _Comp __comp = {},
4013 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4014 {
4015 while (__first1 != __last1 && __first2 != __last2)
4016 if (std::__invoke(__comp,
4017 std::__invoke(__proj1, *__first1),
4018 std::__invoke(__proj2, *__first2)))
4019 ++__first1;
4020 else if (std::__invoke(__comp,
4021 std::__invoke(__proj2, *__first2),
4022 std::__invoke(__proj1, *__first1)))
4023 ++__first2;
4024 else
4025 {
4026 *__result = *__first1;
4027 ++__first1;
4028 ++__first2;
4029 ++__result;
4030 }
4031 // TODO: Eliminating these variables triggers an ICE.
4032 auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
4033 auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
4034 return {std::move(__last1i), std::move(__last2i), std::move(__result)};
4035 }
4036
4037 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4038 typename _Comp = ranges::less,
4039 typename _Proj1 = identity, typename _Proj2 = identity>
4040 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4041 _Comp, _Proj1, _Proj2>
4042 constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
4043 borrowed_iterator_t<_Range2>, _Out>
4044 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4045 _Comp __comp = {},
4046 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4047 {
4048 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4049 ranges::begin(__r2), ranges::end(__r2),
4050 std::move(__result), std::move(__comp),
4051 std::move(__proj1), std::move(__proj2));
4052 }
4053 };
4054
4055 inline constexpr __set_intersection_fn set_intersection{};
4056
4057 template<typename _Iter, typename _Out>
4058 using set_difference_result = in_out_result<_Iter, _Out>;
4059
4060 struct __set_difference_fn
4061 {
4062 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4063 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4064 weakly_incrementable _Out, typename _Comp = ranges::less,
4065 typename _Proj1 = identity, typename _Proj2 = identity>
4066 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
4067 constexpr set_difference_result<_Iter1, _Out>
4068 operator()(_Iter1 __first1, _Sent1 __last1,
4069 _Iter2 __first2, _Sent2 __last2, _Out __result,
4070 _Comp __comp = {},
4071 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4072 {
4073 while (__first1 != __last1 && __first2 != __last2)
4074 if (std::__invoke(__comp,
4075 std::__invoke(__proj1, *__first1),
4076 std::__invoke(__proj2, *__first2)))
4077 {
4078 *__result = *__first1;
4079 ++__first1;
4080 ++__result;
4081 }
4082 else if (std::__invoke(__comp,
4083 std::__invoke(__proj2, *__first2),
4084 std::__invoke(__proj1, *__first1)))
4085 ++__first2;
4086 else
4087 {
4088 ++__first1;
4089 ++__first2;
4090 }
4091 return ranges::copy(std::move(__first1), std::move(__last1),
4092 std::move(__result));
4093 }
4094
4095 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4096 typename _Comp = ranges::less,
4097 typename _Proj1 = identity, typename _Proj2 = identity>
4098 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4099 _Comp, _Proj1, _Proj2>
4100 constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
4101 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4102 _Comp __comp = {},
4103 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4104 {
4105 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4106 ranges::begin(__r2), ranges::end(__r2),
4107 std::move(__result), std::move(__comp),
4108 std::move(__proj1), std::move(__proj2));
4109 }
4110 };
4111
4112 inline constexpr __set_difference_fn set_difference{};
4113
4114 template<typename _Iter1, typename _Iter2, typename _Out>
4115 using set_symmetric_difference_result
4116 = in_in_out_result<_Iter1, _Iter2, _Out>;
4117
4118 struct __set_symmetric_difference_fn
4119 {
4120 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4121 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4122 weakly_incrementable _Out, typename _Comp = ranges::less,
4123 typename _Proj1 = identity, typename _Proj2 = identity>
4124 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
4125 constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
4126 operator()(_Iter1 __first1, _Sent1 __last1,
4127 _Iter2 __first2, _Sent2 __last2,
4128 _Out __result, _Comp __comp = {},
4129 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4130 {
4131 while (__first1 != __last1 && __first2 != __last2)
4132 if (std::__invoke(__comp,
4133 std::__invoke(__proj1, *__first1),
4134 std::__invoke(__proj2, *__first2)))
4135 {
4136 *__result = *__first1;
4137 ++__first1;
4138 ++__result;
4139 }
4140 else if (std::__invoke(__comp,
4141 std::__invoke(__proj2, *__first2),
4142 std::__invoke(__proj1, *__first1)))
4143 {
4144 *__result = *__first2;
4145 ++__first2;
4146 ++__result;
4147 }
4148 else
4149 {
4150 ++__first1;
4151 ++__first2;
4152 }
4153 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
4154 std::move(__result));
4155 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
4156 std::move(__copy1.out));
4157 return {std::move(__copy1.in), std::move(__copy2.in),
4158 std::move(__copy2.out)};
4159 }
4160
4161 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4162 typename _Comp = ranges::less,
4163 typename _Proj1 = identity, typename _Proj2 = identity>
4164 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4165 _Comp, _Proj1, _Proj2>
4166 constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
4167 borrowed_iterator_t<_Range2>,
4168 _Out>
4169 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4170 _Comp __comp = {},
4171 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4172 {
4173 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4174 ranges::begin(__r2), ranges::end(__r2),
4175 std::move(__result), std::move(__comp),
4176 std::move(__proj1), std::move(__proj2));
4177 }
4178 };
4179
4180 inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
4181
4182 // min is defined in <bits/ranges_util.h>.
4183
4184 struct __max_fn
4185 {
4186 template<typename _Tp, typename _Proj = identity,
4187 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4188 _Comp = ranges::less>
4189 [[nodiscard]] constexpr const _Tp&
4190 operator()(const _Tp& __a, const _Tp& __b,
4191 _Comp __comp = {}, _Proj __proj = {}) const
4192 {
4193 if (std::__invoke(__comp,
4194 std::__invoke(__proj, __a),
4195 std::__invoke(__proj, __b)))
4196 return __b;
4197 else
4198 return __a;
4199 }
4200
4201 template<input_range _Range, typename _Proj = identity,
4202 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4203 _Comp = ranges::less>
4204 requires indirectly_copyable_storable<iterator_t<_Range>,
4205 range_value_t<_Range>*>
4206 [[nodiscard]] constexpr range_value_t<_Range>
4207 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4208 {
4209 auto __first = ranges::begin(__r);
4210 auto __last = ranges::end(__r);
4211 __glibcxx_assert(__first != __last);
4212 auto __result = *__first;
4213 while (++__first != __last)
4214 {
4215 auto&& __tmp = *__first;
4216 if (std::__invoke(__comp,
4217 std::__invoke(__proj, __result),
4218 std::__invoke(__proj, __tmp)))
4219 __result = std::forward<decltype(__tmp)>(__tmp);
4220 }
4221 return __result;
4222 }
4223
4224 template<copyable _Tp, typename _Proj = identity,
4225 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4226 _Comp = ranges::less>
4227 [[nodiscard]] constexpr _Tp
4228 operator()(initializer_list<_Tp> __r,
4229 _Comp __comp = {}, _Proj __proj = {}) const
4230 {
4231 return (*this)(ranges::subrange(__r),
4232 std::move(__comp), std::move(__proj));
4233 }
4234 };
4235
4236 inline constexpr __max_fn max{};
4237
4238 struct __clamp_fn
4239 {
4240 template<typename _Tp, typename _Proj = identity,
4241 indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
4242 = ranges::less>
4243 [[nodiscard]] constexpr const _Tp&
4244 operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
4245 _Comp __comp = {}, _Proj __proj = {}) const
4246 {
4247 __glibcxx_assert(!(std::__invoke(__comp,
4248 std::__invoke(__proj, __hi),
4249 std::__invoke(__proj, __lo))));
4250 auto&& __proj_val = std::__invoke(__proj, __val);
4251 if (std::__invoke(__comp,
4252 std::forward<decltype(__proj_val)>(__proj_val),
4253 std::__invoke(__proj, __lo)))
4254 return __lo;
4255 else if (std::__invoke(__comp,
4256 std::__invoke(__proj, __hi),
4257 std::forward<decltype(__proj_val)>(__proj_val)))
4258 return __hi;
4259 else
4260 return __val;
4261 }
4262 };
4263
4264 inline constexpr __clamp_fn clamp{};
4265
4266 template<typename _Tp>
4267 struct min_max_result
4268 {
4269 [[no_unique_address]] _Tp min;
4270 [[no_unique_address]] _Tp max;
4271
4272 template<typename _Tp2>
4273 requires convertible_to<const _Tp&, _Tp2>
4274 constexpr
4275 operator min_max_result<_Tp2>() const &
4276 { return {min, max}; }
4277
4278 template<typename _Tp2>
4279 requires convertible_to<_Tp, _Tp2>
4280 constexpr
4281 operator min_max_result<_Tp2>() &&
4282 { return {std::move(min), std::move(max)}; }
4283 };
4284
4285 template<typename _Tp>
4286 using minmax_result = min_max_result<_Tp>;
4287
4288 struct __minmax_fn
4289 {
4290 template<typename _Tp, typename _Proj = identity,
4291 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4292 _Comp = ranges::less>
4293 [[nodiscard]] constexpr minmax_result<const _Tp&>
4294 operator()(const _Tp& __a, const _Tp& __b,
4295 _Comp __comp = {}, _Proj __proj = {}) const
4296 {
4297 if (std::__invoke(__comp,
4298 std::__invoke(__proj, __b),
4299 std::__invoke(__proj, __a)))
4300 return {__b, __a};
4301 else
4302 return {__a, __b};
4303 }
4304
4305 template<input_range _Range, typename _Proj = identity,
4306 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4307 _Comp = ranges::less>
4308 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
4309 [[nodiscard]] constexpr minmax_result<range_value_t<_Range>>
4310 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4311 {
4312 auto __first = ranges::begin(__r);
4313 auto __last = ranges::end(__r);
4314 __glibcxx_assert(__first != __last);
4315 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
4316 minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
4317 if (++__first == __last)
4318 return __result;
4319 else
4320 {
4321 // At this point __result.min == __result.max, so a single
4322 // comparison with the next element suffices.
4323 auto&& __val = *__first;
4324 if (__comp_proj(__val, __result.min))
4325 __result.min = std::forward<decltype(__val)>(__val);
4326 else
4327 __result.max = std::forward<decltype(__val)>(__val);
4328 }
4329 while (++__first != __last)
4330 {
4331 // Now process two elements at a time so that we perform at most
4332 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
4333 // iterations of this loop performs three comparisons).
4334 range_value_t<_Range> __val1 = *__first;
4335 if (++__first == __last)
4336 {
4337 // N is odd; in this final iteration, we perform at most two
4338 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
4339 // which is not more than 3*N/2, as required.
4340 if (__comp_proj(__val1, __result.min))
4341 __result.min = std::move(__val1);
4342 else if (!__comp_proj(__val1, __result.max))
4343 __result.max = std::move(__val1);
4344 break;
4345 }
4346 auto&& __val2 = *__first;
4347 if (!__comp_proj(__val2, __val1))
4348 {
4349 if (__comp_proj(__val1, __result.min))
4350 __result.min = std::move(__val1);
4351 if (!__comp_proj(__val2, __result.max))
4352 __result.max = std::forward<decltype(__val2)>(__val2);
4353 }
4354 else
4355 {
4356 if (__comp_proj(__val2, __result.min))
4357 __result.min = std::forward<decltype(__val2)>(__val2);
4358 if (!__comp_proj(__val1, __result.max))
4359 __result.max = std::move(__val1);
4360 }
4361 }
4362 return __result;
4363 }
4364
4365 template<copyable _Tp, typename _Proj = identity,
4366 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4367 _Comp = ranges::less>
4368 [[nodiscard]] constexpr minmax_result<_Tp>
4369 operator()(initializer_list<_Tp> __r,
4370 _Comp __comp = {}, _Proj __proj = {}) const
4371 {
4372 return (*this)(ranges::subrange(__r),
4373 std::move(__comp), std::move(__proj));
4374 }
4375 };
4376
4377 inline constexpr __minmax_fn minmax{};
4378
4379 struct __min_element_fn
4380 {
4381 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4382 typename _Proj = identity,
4383 indirect_strict_weak_order<projected<_Iter, _Proj>>
4384 _Comp = ranges::less>
4385 [[nodiscard]] constexpr _Iter
4386 operator()(_Iter __first, _Sent __last,
4387 _Comp __comp = {}, _Proj __proj = {}) const
4388 {
4389 if (__first == __last)
4390 return __first;
4391
4392 auto __i = __first;
4393 while (++__i != __last)
4394 {
4395 if (std::__invoke(__comp,
4396 std::__invoke(__proj, *__i),
4397 std::__invoke(__proj, *__first)))
4398 __first = __i;
4399 }
4400 return __first;
4401 }
4402
4403 template<forward_range _Range, typename _Proj = identity,
4404 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4405 _Comp = ranges::less>
4406 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
4407 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4408 {
4409 return (*this)(ranges::begin(__r), ranges::end(__r),
4410 std::move(__comp), std::move(__proj));
4411 }
4412 };
4413
4414 inline constexpr __min_element_fn min_element{};
4415
4416 struct __max_element_fn
4417 {
4418 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4419 typename _Proj = identity,
4420 indirect_strict_weak_order<projected<_Iter, _Proj>>
4421 _Comp = ranges::less>
4422 [[nodiscard]] constexpr _Iter
4423 operator()(_Iter __first, _Sent __last,
4424 _Comp __comp = {}, _Proj __proj = {}) const
4425 {
4426 if (__first == __last)
4427 return __first;
4428
4429 auto __i = __first;
4430 while (++__i != __last)
4431 {
4432 if (std::__invoke(__comp,
4433 std::__invoke(__proj, *__first),
4434 std::__invoke(__proj, *__i)))
4435 __first = __i;
4436 }
4437 return __first;
4438 }
4439
4440 template<forward_range _Range, typename _Proj = identity,
4441 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4442 _Comp = ranges::less>
4443 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
4444 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4445 {
4446 return (*this)(ranges::begin(__r), ranges::end(__r),
4447 std::move(__comp), std::move(__proj));
4448 }
4449 };
4450
4451 inline constexpr __max_element_fn max_element{};
4452
4453 template<typename _Iter>
4454 using minmax_element_result = min_max_result<_Iter>;
4455
4456 struct __minmax_element_fn
4457 {
4458 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4459 typename _Proj = identity,
4460 indirect_strict_weak_order<projected<_Iter, _Proj>>
4461 _Comp = ranges::less>
4462 [[nodiscard]] constexpr minmax_element_result<_Iter>
4463 operator()(_Iter __first, _Sent __last,
4464 _Comp __comp = {}, _Proj __proj = {}) const
4465 {
4466 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
4467 minmax_element_result<_Iter> __result = {__first, __first};
4468 if (__first == __last || ++__first == __last)
4469 return __result;
4470 else
4471 {
4472 // At this point __result.min == __result.max, so a single
4473 // comparison with the next element suffices.
4474 if (__comp_proj(*__first, *__result.min))
4475 __result.min = __first;
4476 else
4477 __result.max = __first;
4478 }
4479 while (++__first != __last)
4480 {
4481 // Now process two elements at a time so that we perform at most
4482 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
4483 // iterations of this loop performs three comparisons).
4484 auto __prev = __first;
4485 if (++__first == __last)
4486 {
4487 // N is odd; in this final iteration, we perform at most two
4488 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
4489 // which is not more than 3*N/2, as required.
4490 if (__comp_proj(*__prev, *__result.min))
4491 __result.min = __prev;
4492 else if (!__comp_proj(*__prev, *__result.max))
4493 __result.max = __prev;
4494 break;
4495 }
4496 if (!__comp_proj(*__first, *__prev))
4497 {
4498 if (__comp_proj(*__prev, *__result.min))
4499 __result.min = __prev;
4500 if (!__comp_proj(*__first, *__result.max))
4501 __result.max = __first;
4502 }
4503 else
4504 {
4505 if (__comp_proj(*__first, *__result.min))
4506 __result.min = __first;
4507 if (!__comp_proj(*__prev, *__result.max))
4508 __result.max = __prev;
4509 }
4510 }
4511 return __result;
4512 }
4513
4514 template<forward_range _Range, typename _Proj = identity,
4515 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4516 _Comp = ranges::less>
4517 [[nodiscard]] constexpr minmax_element_result<borrowed_iterator_t<_Range>>
4518 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4519 {
4520 return (*this)(ranges::begin(__r), ranges::end(__r),
4521 std::move(__comp), std::move(__proj));
4522 }
4523 };
4524
4525 inline constexpr __minmax_element_fn minmax_element{};
4526
4527 struct __lexicographical_compare_fn
4528 {
4529 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4530 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4531 typename _Proj1 = identity, typename _Proj2 = identity,
4532 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
4533 projected<_Iter2, _Proj2>>
4534 _Comp = ranges::less>
4535 [[nodiscard]] constexpr bool
4536 operator()(_Iter1 __first1, _Sent1 __last1,
4537 _Iter2 __first2, _Sent2 __last2,
4538 _Comp __comp = {},
4539 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4540 {
4541 if constexpr (__detail::__is_normal_iterator<_Iter1>
4542 && same_as<_Iter1, _Sent1>)
4543 return (*this)(__first1.base(), __last1.base(),
4544 std::move(__first2), std::move(__last2),
4545 std::move(__comp),
4546 std::move(__proj1), std::move(__proj2));
4547 else if constexpr (__detail::__is_normal_iterator<_Iter2>
4548 && same_as<_Iter2, _Sent2>)
4549 return (*this)(std::move(__first1), std::move(__last1),
4550 __first2.base(), __last2.base(),
4551 std::move(__comp),
4552 std::move(__proj1), std::move(__proj2));
4553 else
4554 {
4555 constexpr bool __sized_iters
4556 = (sized_sentinel_for<_Sent1, _Iter1>
4557 && sized_sentinel_for<_Sent2, _Iter2>);
4558 if constexpr (__sized_iters)
4559 {
4560 using _ValueType1 = iter_value_t<_Iter1>;
4561 using _ValueType2 = iter_value_t<_Iter2>;
4562 // This condition is consistent with the one in
4563 // __lexicographical_compare_aux in <bits/stl_algobase.h>.
4564 constexpr bool __use_memcmp
4565 = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
4566 && __ptr_to_nonvolatile<_Iter1>
4567 && __ptr_to_nonvolatile<_Iter2>
4568 && (is_same_v<_Comp, ranges::less>
4569 || is_same_v<_Comp, ranges::greater>)
4570 && is_same_v<_Proj1, identity>
4571 && is_same_v<_Proj2, identity>);
4572 if constexpr (__use_memcmp)
4573 {
4574 const auto __d1 = __last1 - __first1;
4575 const auto __d2 = __last2 - __first2;
4576
4577 if (const auto __len = std::min(__d1, __d2))
4578 {
4579 const auto __c
4580 = std::__memcmp(__first1, __first2, __len);
4581 if constexpr (is_same_v<_Comp, ranges::less>)
4582 {
4583 if (__c < 0)
4584 return true;
4585 if (__c > 0)
4586 return false;
4587 }
4588 else if constexpr (is_same_v<_Comp, ranges::greater>)
4589 {
4590 if (__c > 0)
4591 return true;
4592 if (__c < 0)
4593 return false;
4594 }
4595 }
4596 return __d1 < __d2;
4597 }
4598 }
4599
4600 for (; __first1 != __last1 && __first2 != __last2;
4601 ++__first1, (void) ++__first2)
4602 {
4603 if (std::__invoke(__comp,
4604 std::__invoke(__proj1, *__first1),
4605 std::__invoke(__proj2, *__first2)))
4606 return true;
4607 if (std::__invoke(__comp,
4608 std::__invoke(__proj2, *__first2),
4609 std::__invoke(__proj1, *__first1)))
4610 return false;
4611 }
4612 return __first1 == __last1 && __first2 != __last2;
4613 }
4614 }
4615
4616 template<input_range _Range1, input_range _Range2,
4617 typename _Proj1 = identity, typename _Proj2 = identity,
4618 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
4619 projected<iterator_t<_Range2>, _Proj2>>
4620 _Comp = ranges::less>
4621 [[nodiscard]] constexpr bool
4622 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
4623 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4624 {
4625 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4626 ranges::begin(__r2), ranges::end(__r2),
4627 std::move(__comp),
4628 std::move(__proj1), std::move(__proj2));
4629 }
4630
4631 private:
4632 template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
4633 static constexpr bool __ptr_to_nonvolatile
4634 = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
4635 };
4636
4637 inline constexpr __lexicographical_compare_fn lexicographical_compare;
4638
4639 template<typename _Iter>
4640 struct in_found_result
4641 {
4642 [[no_unique_address]] _Iter in;
4643 bool found;
4644
4645 template<typename _Iter2>
4646 requires convertible_to<const _Iter&, _Iter2>
4647 constexpr
4648 operator in_found_result<_Iter2>() const &
4649 { return {in, found}; }
4650
4651 template<typename _Iter2>
4652 requires convertible_to<_Iter, _Iter2>
4653 constexpr
4654 operator in_found_result<_Iter2>() &&
4655 { return {std::move(in), found}; }
4656 };
4657
4658 template<typename _Iter>
4659 using next_permutation_result = in_found_result<_Iter>;
4660
4661 struct __next_permutation_fn
4662 {
4663 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
4664 typename _Comp = ranges::less, typename _Proj = identity>
4665 requires sortable<_Iter, _Comp, _Proj>
4666 constexpr next_permutation_result<_Iter>
4667 operator()(_Iter __first, _Sent __last,
4668 _Comp __comp = {}, _Proj __proj = {}) const
4669 {
4670 if (__first == __last)
4671 return {std::move(__first), false};
4672
4673 auto __i = __first;
4674 ++__i;
4675 if (__i == __last)
4676 return {std::move(__i), false};
4677
4678 auto __lasti = ranges::next(__first, __last);
4679 __i = __lasti;
4680 --__i;
4681
4682 for (;;)
4683 {
4684 auto __ii = __i;
4685 --__i;
4686 if (std::__invoke(__comp,
4687 std::__invoke(__proj, *__i),
4688 std::__invoke(__proj, *__ii)))
4689 {
4690 auto __j = __lasti;
4691 while (!(bool)std::__invoke(__comp,
4692 std::__invoke(__proj, *__i),
4693 std::__invoke(__proj, *--__j)))
4694 ;
4695 ranges::iter_swap(__i, __j);
4696 ranges::reverse(__ii, __last);
4697 return {std::move(__lasti), true};
4698 }
4699 if (__i == __first)
4700 {
4701 ranges::reverse(__first, __last);
4702 return {std::move(__lasti), false};
4703 }
4704 }
4705 }
4706
4707 template<bidirectional_range _Range, typename _Comp = ranges::less,
4708 typename _Proj = identity>
4709 requires sortable<iterator_t<_Range>, _Comp, _Proj>
4710 constexpr next_permutation_result<borrowed_iterator_t<_Range>>
4711 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4712 {
4713 return (*this)(ranges::begin(__r), ranges::end(__r),
4714 std::move(__comp), std::move(__proj));
4715 }
4716 };
4717
4718 inline constexpr __next_permutation_fn next_permutation{};
4719
4720 template<typename _Iter>
4721 using prev_permutation_result = in_found_result<_Iter>;
4722
4723 struct __prev_permutation_fn
4724 {
4725 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
4726 typename _Comp = ranges::less, typename _Proj = identity>
4727 requires sortable<_Iter, _Comp, _Proj>
4728 constexpr prev_permutation_result<_Iter>
4729 operator()(_Iter __first, _Sent __last,
4730 _Comp __comp = {}, _Proj __proj = {}) const
4731 {
4732 if (__first == __last)
4733 return {std::move(__first), false};
4734
4735 auto __i = __first;
4736 ++__i;
4737 if (__i == __last)
4738 return {std::move(__i), false};
4739
4740 auto __lasti = ranges::next(__first, __last);
4741 __i = __lasti;
4742 --__i;
4743
4744 for (;;)
4745 {
4746 auto __ii = __i;
4747 --__i;
4748 if (std::__invoke(__comp,
4749 std::__invoke(__proj, *__ii),
4750 std::__invoke(__proj, *__i)))
4751 {
4752 auto __j = __lasti;
4753 while (!(bool)std::__invoke(__comp,
4754 std::__invoke(__proj, *--__j),
4755 std::__invoke(__proj, *__i)))
4756 ;
4757 ranges::iter_swap(__i, __j);
4758 ranges::reverse(__ii, __last);
4759 return {std::move(__lasti), true};
4760 }
4761 if (__i == __first)
4762 {
4763 ranges::reverse(__first, __last);
4764 return {std::move(__lasti), false};
4765 }
4766 }
4767 }
4768
4769 template<bidirectional_range _Range, typename _Comp = ranges::less,
4770 typename _Proj = identity>
4771 requires sortable<iterator_t<_Range>, _Comp, _Proj>
4772 constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
4773 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4774 {
4775 return (*this)(ranges::begin(__r), ranges::end(__r),
4776 std::move(__comp), std::move(__proj));
4777 }
4778 };
4779
4780 inline constexpr __prev_permutation_fn prev_permutation{};
4781
4782#if __glibcxx_ranges_contains >= 202207L // C++ >= 23
4783 struct __contains_fn
4784 {
4785 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
4786 typename _Proj = identity,
4787 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
4788 requires indirect_binary_predicate<ranges::equal_to,
4789 projected<_Iter, _Proj>, const _Tp*>
4790 constexpr bool
4791 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
4792 { return ranges::find(std::move(__first), __last, __value, std::move(__proj)) != __last; }
4793
4794 template<input_range _Range,
4795 typename _Proj = identity,
4796 typename _Tp
4797 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
4798 requires indirect_binary_predicate<ranges::equal_to,
4799 projected<iterator_t<_Range>, _Proj>, const _Tp*>
4800 constexpr bool
4801 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
4802 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
4803 };
4804
4805 inline constexpr __contains_fn contains{};
4806
4807 struct __contains_subrange_fn
4808 {
4809 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4810 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4811 typename _Pred = ranges::equal_to,
4812 typename _Proj1 = identity, typename _Proj2 = identity>
4813 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
4814 constexpr bool
4815 operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
4816 _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4817 {
4818 return __first2 == __last2
4819 || !ranges::search(__first1, __last1, __first2, __last2,
4820 std::move(__pred), std::move(__proj1), std::move(__proj2)).empty();
4821 }
4822
4823 template<forward_range _Range1, forward_range _Range2,
4824 typename _Pred = ranges::equal_to,
4825 typename _Proj1 = identity, typename _Proj2 = identity>
4826 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
4827 _Pred, _Proj1, _Proj2>
4828 constexpr bool
4829 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
4830 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4831 {
4832 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4833 ranges::begin(__r2), ranges::end(__r2),
4834 std::move(__pred), std::move(__proj1), std::move(__proj2));
4835 }
4836 };
4837
4838 inline constexpr __contains_subrange_fn contains_subrange{};
4839
4840#endif // __glibcxx_ranges_contains
4841
4842#if __glibcxx_ranges_find_last >= 202207L // C++ >= 23
4843
4844 struct __find_last_fn
4845 {
4846 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4847 typename _Proj = identity,
4848 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
4849 requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Tp*>
4850 [[nodiscard]] constexpr subrange<_Iter>
4851 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
4852 {
4853 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4854 {
4855 _Iter __found = ranges::find(reverse_iterator<_Iter>{__last},
4856 reverse_iterator<_Iter>{__first},
4857 __value, std::move(__proj)).base();
4858 if (__found == __first)
4859 return {__last, __last};
4860 else
4861 return {ranges::prev(__found), __last};
4862 }
4863 else
4864 {
4865 _Iter __found = ranges::find(__first, __last, __value, __proj);
4866 if (__found == __last)
4867 return {__found, __found};
4868 __first = __found;
4869 for (;;)
4870 {
4871 __first = ranges::find(ranges::next(__first), __last, __value, __proj);
4872 if (__first == __last)
4873 return {__found, __first};
4874 __found = __first;
4875 }
4876 }
4877 }
4878
4879 template<forward_range _Range, typename _Proj = identity,
4880 typename _Tp
4881 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
4882 requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Tp*>
4883 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4884 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
4885 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
4886 };
4887
4888 inline constexpr __find_last_fn find_last{};
4889
4890 struct __find_last_if_fn
4891 {
4892 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
4893 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
4894 [[nodiscard]] constexpr subrange<_Iter>
4895 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
4896 {
4897 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4898 {
4899 _Iter __found = ranges::find_if(reverse_iterator<_Iter>{__last},
4900 reverse_iterator<_Iter>{__first},
4901 std::move(__pred), std::move(__proj)).base();
4902 if (__found == __first)
4903 return {__last, __last};
4904 else
4905 return {ranges::prev(__found), __last};
4906 }
4907 else
4908 {
4909 _Iter __found = ranges::find_if(__first, __last, __pred, __proj);
4910 if (__found == __last)
4911 return {__found, __found};
4912 __first = __found;
4913 for (;;)
4914 {
4915 __first = ranges::find_if(ranges::next(__first), __last, __pred, __proj);
4916 if (__first == __last)
4917 return {__found, __first};
4918 __found = __first;
4919 }
4920 }
4921 }
4922
4923 template<forward_range _Range, typename _Proj = identity,
4924 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
4925 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4926 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
4927 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
4928 };
4929
4930 inline constexpr __find_last_if_fn find_last_if{};
4931
4932 struct __find_last_if_not_fn
4933 {
4934 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
4935 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
4936 [[nodiscard]] constexpr subrange<_Iter>
4937 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
4938 {
4939 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4940 {
4941 _Iter __found = ranges::find_if_not(reverse_iterator<_Iter>{__last},
4942 reverse_iterator<_Iter>{__first},
4943 std::move(__pred), std::move(__proj)).base();
4944 if (__found == __first)
4945 return {__last, __last};
4946 else
4947 return {ranges::prev(__found), __last};
4948 }
4949 else
4950 {
4951 _Iter __found = ranges::find_if_not(__first, __last, __pred, __proj);
4952 if (__found == __last)
4953 return {__found, __found};
4954 __first = __found;
4955 for (;;)
4956 {
4957 __first = ranges::find_if_not(ranges::next(__first), __last, __pred, __proj);
4958 if (__first == __last)
4959 return {__found, __first};
4960 __found = __first;
4961 }
4962 }
4963 }
4964
4965 template<forward_range _Range, typename _Proj = identity,
4966 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
4967 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4968 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
4969 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
4970 };
4971
4972 inline constexpr __find_last_if_not_fn find_last_if_not{};
4973
4974#endif // __glibcxx_ranges_find_last
4975
4976#if __glibcxx_ranges_fold >= 202207L // C++ >= 23
4977
4978 template<typename _Iter, typename _Tp>
4979 struct in_value_result
4980 {
4981 [[no_unique_address]] _Iter in;
4982 [[no_unique_address]] _Tp value;
4983
4984 template<typename _Iter2, typename _Tp2>
4985 requires convertible_to<const _Iter&, _Iter2>
4986 && convertible_to<const _Tp&, _Tp2>
4987 constexpr
4988 operator in_value_result<_Iter2, _Tp2>() const &
4989 { return {in, value}; }
4990
4991 template<typename _Iter2, typename _Tp2>
4992 requires convertible_to<_Iter, _Iter2>
4993 && convertible_to<_Tp, _Tp2>
4994 constexpr
4995 operator in_value_result<_Iter2, _Tp2>() &&
4996 { return {std::move(in), std::move(value)}; }
4997 };
4998
4999 namespace __detail
5000 {
5001 template<typename _Fp>
5002 class __flipped
5003 {
5004 _Fp _M_f;
5005
5006 public:
5007 template<typename _Tp, typename _Up>
5008 requires invocable<_Fp&, _Up, _Tp>
5009 invoke_result_t<_Fp&, _Up, _Tp>
5010 operator()(_Tp&&, _Up&&); // not defined
5011 };
5012
5013 template<typename _Fp, typename _Tp, typename _Iter, typename _Up>
5014 concept __indirectly_binary_left_foldable_impl = movable<_Tp> && movable<_Up>
5015 && convertible_to<_Tp, _Up>
5016 && invocable<_Fp&, _Up, iter_reference_t<_Iter>>
5017 && assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Iter>>>;
5018
5019 template<typename _Fp, typename _Tp, typename _Iter>
5020 concept __indirectly_binary_left_foldable = copy_constructible<_Fp>
5021 && indirectly_readable<_Iter>
5022 && invocable<_Fp&, _Tp, iter_reference_t<_Iter>>
5023 && convertible_to<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>,
5025 && __indirectly_binary_left_foldable_impl
5027
5028 template <typename _Fp, typename _Tp, typename _Iter>
5029 concept __indirectly_binary_right_foldable
5030 = __indirectly_binary_left_foldable<__flipped<_Fp>, _Tp, _Iter>;
5031 } // namespace __detail
5032
5033 template<typename _Iter, typename _Tp>
5034 using fold_left_with_iter_result = in_value_result<_Iter, _Tp>;
5035
5036 struct __fold_left_with_iter_fn
5037 {
5038 template<typename _Ret_iter,
5039 typename _Iter, typename _Sent, typename _Tp, typename _Fp>
5040 static constexpr auto
5041 _S_impl(_Iter __first, _Sent __last, _Tp __init, _Fp __f)
5042 {
5043 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>>;
5044 using _Ret = fold_left_with_iter_result<_Ret_iter, _Up>;
5045
5046 if (__first == __last)
5047 return _Ret{std::move(__first), _Up(std::move(__init))};
5048
5049 _Up __accum = std::__invoke(__f, std::move(__init), *__first);
5050 for (++__first; __first != __last; ++__first)
5051 __accum = std::__invoke(__f, std::move(__accum), *__first);
5052 return _Ret{std::move(__first), std::move(__accum)};
5053 }
5054
5055 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5056 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5057 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
5058 constexpr auto
5059 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5060 {
5061 using _Ret_iter = _Iter;
5062 return _S_impl<_Ret_iter>(std::move(__first), __last,
5063 std::move(__init), std::move(__f));
5064 }
5065
5066 template<input_range _Range,
5067 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5068 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
5069 constexpr auto
5070 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5071 {
5072 using _Ret_iter = borrowed_iterator_t<_Range>;
5073 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r),
5074 std::move(__init), std::move(__f));
5075 }
5076 };
5077
5078 inline constexpr __fold_left_with_iter_fn fold_left_with_iter{};
5079
5080 struct __fold_left_fn
5081 {
5082 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5083 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5084 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
5085 constexpr auto
5086 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5087 {
5088 return ranges::fold_left_with_iter(std::move(__first), __last,
5089 std::move(__init), std::move(__f)).value;
5090 }
5091
5092 template<input_range _Range,
5093 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5094 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
5095 constexpr auto
5096 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5097 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
5098 };
5099
5100 inline constexpr __fold_left_fn fold_left{};
5101
5102 template<typename _Iter, typename _Tp>
5103 using fold_left_first_with_iter_result = in_value_result<_Iter, _Tp>;
5104
5105 struct __fold_left_first_with_iter_fn
5106 {
5107 template<typename _Ret_iter, typename _Iter, typename _Sent, typename _Fp>
5108 static constexpr auto
5109 _S_impl(_Iter __first, _Sent __last, _Fp __f)
5110 {
5111 using _Up = decltype(ranges::fold_left(std::move(__first), __last,
5112 iter_value_t<_Iter>(*__first), __f));
5113 using _Ret = fold_left_first_with_iter_result<_Ret_iter, optional<_Up>>;
5114
5115 if (__first == __last)
5116 return _Ret{std::move(__first), optional<_Up>()};
5117
5118 optional<_Up> __init(in_place, *__first);
5119 for (++__first; __first != __last; ++__first)
5120 *__init = std::__invoke(__f, std::move(*__init), *__first);
5121 return _Ret{std::move(__first), std::move(__init)};
5122 }
5123
5124 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5125 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5126 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5127 constexpr auto
5128 operator()(_Iter __first, _Sent __last, _Fp __f) const
5129 {
5130 using _Ret_iter = _Iter;
5131 return _S_impl<_Ret_iter>(std::move(__first), __last, std::move(__f));
5132 }
5133
5134 template<input_range _Range,
5135 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5136 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5137 constexpr auto
5138 operator()(_Range&& __r, _Fp __f) const
5139 {
5140 using _Ret_iter = borrowed_iterator_t<_Range>;
5141 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r), std::move(__f));
5142 }
5143 };
5144
5145 inline constexpr __fold_left_first_with_iter_fn fold_left_first_with_iter{};
5146
5147 struct __fold_left_first_fn
5148 {
5149 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5150 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5151 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5152 constexpr auto
5153 operator()(_Iter __first, _Sent __last, _Fp __f) const
5154 {
5155 return ranges::fold_left_first_with_iter(std::move(__first), __last,
5156 std::move(__f)).value;
5157 }
5158
5159 template<input_range _Range,
5160 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5161 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5162 constexpr auto
5163 operator()(_Range&& __r, _Fp __f) const
5164 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
5165 };
5166
5167 inline constexpr __fold_left_first_fn fold_left_first{};
5168
5169 struct __fold_right_fn
5170 {
5171 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
5172 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5173 __detail::__indirectly_binary_right_foldable<_Tp, _Iter> _Fp>
5174 constexpr auto
5175 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5176 {
5177 using _Up = decay_t<invoke_result_t<_Fp&, iter_reference_t<_Iter>, _Tp>>;
5178
5179 if (__first == __last)
5180 return _Up(std::move(__init));
5181
5182 _Iter __tail = ranges::next(__first, __last);
5183 _Up __accum = std::__invoke(__f, *--__tail, std::move(__init));
5184 while (__first != __tail)
5185 __accum = std::__invoke(__f, *--__tail, std::move(__accum));
5186 return __accum;
5187 }
5188
5189 template<bidirectional_range _Range,
5190 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5191 __detail::__indirectly_binary_right_foldable<_Tp, iterator_t<_Range>> _Fp>
5192 constexpr auto
5193 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5194 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
5195 };
5196
5197 inline constexpr __fold_right_fn fold_right{};
5198
5199 struct __fold_right_last_fn
5200 {
5201 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
5202 __detail::__indirectly_binary_right_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5203 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5204 constexpr auto
5205 operator()(_Iter __first, _Sent __last, _Fp __f) const
5206 {
5207 using _Up = decltype(ranges::fold_right(__first, __last,
5208 iter_value_t<_Iter>(*__first), __f));
5209
5210 if (__first == __last)
5211 return optional<_Up>();
5212
5213 _Iter __tail = ranges::prev(ranges::next(__first, std::move(__last)));
5214 return optional<_Up>(in_place,
5215 ranges::fold_right(std::move(__first), __tail,
5216 iter_value_t<_Iter>(*__tail),
5217 std::move(__f)));
5218 }
5219
5220 template<bidirectional_range _Range,
5221 __detail::__indirectly_binary_right_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5222 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5223 constexpr auto
5224 operator()(_Range&& __r, _Fp __f) const
5225 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
5226 };
5227
5228 inline constexpr __fold_right_last_fn fold_right_last{};
5229#endif // __glibcxx_ranges_fold
5230} // namespace ranges
5231
5232#if __glibcxx_shift >= 201806L // C++ >= 20
5233 template<typename _ForwardIterator>
5234 constexpr _ForwardIterator
5235 shift_left(_ForwardIterator __first, _ForwardIterator __last,
5237 {
5238 __glibcxx_assert(__n >= 0);
5239 if (__n == 0)
5240 return __last;
5241
5242 auto __mid = ranges::next(__first, __n, __last);
5243 if (__mid == __last)
5244 return __first;
5245 return std::move(std::move(__mid), std::move(__last), std::move(__first));
5246 }
5247
5248 template<typename _ForwardIterator>
5249 constexpr _ForwardIterator
5250 shift_right(_ForwardIterator __first, _ForwardIterator __last,
5252 {
5253 __glibcxx_assert(__n >= 0);
5254 if (__n == 0)
5255 return __first;
5256
5257 using _Cat
5259 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
5260 {
5261 auto __mid = ranges::next(__last, -__n, __first);
5262 if (__mid == __first)
5263 return __last;
5264
5265 return std::move_backward(std::move(__first), std::move(__mid),
5266 std::move(__last));
5267 }
5268 else
5269 {
5270 auto __result = ranges::next(__first, __n, __last);
5271 if (__result == __last)
5272 return __last;
5273
5274 auto __dest_head = __first, __dest_tail = __result;
5275 while (__dest_head != __result)
5276 {
5277 if (__dest_tail == __last)
5278 {
5279 // If we get here, then we must have
5280 // 2*n >= distance(__first, __last)
5281 // i.e. we are shifting out at least half of the range. In
5282 // this case we can safely perform the shift with a single
5283 // move.
5284 std::move(std::move(__first), std::move(__dest_head), __result);
5285 return __result;
5286 }
5287 ++__dest_head;
5288 ++__dest_tail;
5289 }
5290
5291 for (;;)
5292 {
5293 // At the start of each iteration of this outer loop, the range
5294 // [__first, __result) contains those elements that after shifting
5295 // the whole range right by __n, should end up in
5296 // [__dest_head, __dest_tail) in order.
5297
5298 // The below inner loop swaps the elements of [__first, __result)
5299 // and [__dest_head, __dest_tail), while simultaneously shifting
5300 // the latter range by __n.
5301 auto __cursor = __first;
5302 while (__cursor != __result)
5303 {
5304 if (__dest_tail == __last)
5305 {
5306 // At this point the ranges [__first, result) and
5307 // [__dest_head, dest_tail) are disjoint, so we can safely
5308 // move the remaining elements.
5309 __dest_head = std::move(__cursor, __result,
5310 std::move(__dest_head));
5311 std::move(std::move(__first), std::move(__cursor),
5312 std::move(__dest_head));
5313 return __result;
5314 }
5315 std::iter_swap(__cursor, __dest_head);
5316 ++__dest_head;
5317 ++__dest_tail;
5318 ++__cursor;
5319 }
5320 }
5321 }
5322 }
5323#endif
5324
5325namespace ranges
5326{
5327#if __glibcxx_shift >= 202202L // C++ >= 23
5328 struct __shift_left_fn
5329 {
5330 template<permutable _Iter, sentinel_for<_Iter> _Sent>
5331 constexpr subrange<_Iter>
5332 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const
5333 {
5334 __glibcxx_assert(__n >= 0);
5335 if (__n == 0)
5336 return {__first, ranges::next(__first, __last)};
5337
5338 auto __mid = ranges::next(__first, __n, __last);
5339 if (__mid == __last)
5340 return {__first, __first};
5341 return {__first, ranges::move(__mid, __last, __first).out};
5342 }
5343
5344 template<forward_range _Range>
5345 requires permutable<iterator_t<_Range>>
5346 constexpr borrowed_subrange_t<_Range>
5347 operator()(_Range&& __r, range_difference_t<_Range> __n) const
5348 { return (*this)(ranges::begin(__r), ranges::end(__r), __n); }
5349 };
5350
5351 inline constexpr __shift_left_fn shift_left{};
5352
5353 struct __shift_right_fn
5354 {
5355 template<permutable _Iter, sentinel_for<_Iter> _Sent>
5356 constexpr subrange<_Iter>
5357 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const
5358 {
5359 __glibcxx_assert(__n >= 0);
5360 if (__n == 0)
5361 return {__first, ranges::next(__first, __last)};
5362
5363 if constexpr (bidirectional_iterator<_Iter> && same_as<_Iter, _Sent>)
5364 {
5365 auto __mid = ranges::next(__last, -__n, __first);
5366 if (__mid == __first)
5367 return {__last, __last};
5368
5369 return {ranges::move_backward(__first, __mid, __last).out, __last};
5370 }
5371 else
5372 {
5373 auto __result = ranges::next(__first, __n, __last);
5374 if (__result == __last)
5375 return {__result, __result};
5376
5377 auto __dest_head = __first, __dest_tail = __result;
5378 while (__dest_head != __result)
5379 {
5380 if (__dest_tail == __last)
5381 {
5382 // If we get here, then we must have
5383 // 2*n >= distance(__first, __last)
5384 // i.e. we are shifting out at least half of the range. In
5385 // this case we can safely perform the shift with a single
5386 // move.
5387 auto __lasti = ranges::move(__first, __dest_head, __result).out;
5388 // __glibcxx_assert(__lasti == __last);
5389 return {__result, __lasti};
5390 }
5391 ++__dest_head;
5392 ++__dest_tail;
5393 }
5394
5395 for (;;)
5396 {
5397 // At the start of each iteration of this outer loop, the range
5398 // [__first, __result) contains those elements that after shifting
5399 // the whole range right by __n, should end up in
5400 // [__dest_head, __dest_tail) in order.
5401
5402 // The below inner loop swaps the elements of [__first, __result)
5403 // and [__dest_head, __dest_tail), while simultaneously shifting
5404 // the latter range by __n.
5405 auto __cursor = __first;
5406 while (__cursor != __result)
5407 {
5408 if (__dest_tail == __last)
5409 {
5410 // At this point the ranges [__first, result) and
5411 // [__dest_head, dest_tail) are disjoint, so we can safely
5412 // move the remaining elements.
5413 __dest_head = ranges::move(__cursor, __result, __dest_head).out;
5414 auto __lasti = ranges::move(__first, __cursor, __dest_head).out;
5415 // __glibcxx_assert(__lasti == __last);
5416 return {__result, __lasti};
5417 }
5418 ranges::iter_swap(__cursor, __dest_head);
5419 ++__dest_head;
5420 ++__dest_tail;
5421 ++__cursor;
5422 }
5423 }
5424 }
5425 }
5426
5427 template<forward_range _Range>
5428 requires permutable<iterator_t<_Range>>
5429 constexpr borrowed_subrange_t<_Range>
5430 operator()(_Range&& __r, range_difference_t<_Range> __n) const
5431 { return (*this)(ranges::begin(__r), ranges::end(__r), __n); }
5432 };
5433
5434 inline constexpr __shift_right_fn shift_right{};
5435#endif // C++23
5436} // namespace ranges
5437
5438_GLIBCXX_END_NAMESPACE_VERSION
5439} // namespace std
5440#endif // concepts
5441#endif // C++20
5442#endif // _RANGES_ALGO_H
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1843
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2893
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
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.
constexpr reverse_iterator< _Iterator > make_reverse_iterator(_Iterator __i)
Generator function for reverse_iterator.
ISO C++ entities toplevel namespace is std.
pair< _IntType, _IntType > __gen_two_uniform_ints(_IntType __b0, _IntType __b1, _UniformRandomBitGenerator &&__g)
Generate two uniformly distributed integers using a single distribution invocation.
Definition stl_algo.h:3704
Traits class for iterators.