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 __t = std::move(*__p);
1687 ranges::move(__p + 1, __p + __n, __p);
1688 *(__p + __n - 1) = std::move(__t);
1689 return {std::move(__ret), std::move(__lasti)};
1690 }
1691 auto __q = __p + __k;
1692 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1693 {
1694 ranges::iter_swap(__p, __q);
1695 ++__p;
1696 ++__q;
1697 }
1698 __n %= __k;
1699 if (__n == 0)
1700 return {std::move(__ret), std::move(__lasti)};
1701 ranges::swap(__n, __k);
1702 __k = __n - __k;
1703 }
1704 else
1705 {
1706 __k = __n - __k;
1707 // TODO: is_pod is deprecated, but this condition is
1708 // consistent with the STL implementation.
1709 if constexpr (__is_pod(iter_value_t<_Iter>))
1710 if (__k == 1)
1711 {
1712 auto __t = std::move(*(__p + __n - 1));
1713 ranges::move_backward(__p, __p + __n - 1, __p + __n);
1714 *__p = std::move(__t);
1715 return {std::move(__ret), std::move(__lasti)};
1716 }
1717 auto __q = __p + __n;
1718 __p = __q - __k;
1719 for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1720 {
1721 --__p;
1722 --__q;
1723 ranges::iter_swap(__p, __q);
1724 }
1725 __n %= __k;
1726 if (__n == 0)
1727 return {std::move(__ret), std::move(__lasti)};
1728 std::swap(__n, __k);
1729 }
1730 }
1731 }
1732 else if constexpr (bidirectional_iterator<_Iter>)
1733 {
1734 auto __tail = __lasti;
1735
1736 ranges::reverse(__first, __middle);
1737 ranges::reverse(__middle, __tail);
1738
1739 while (__first != __middle && __middle != __tail)
1740 {
1741 ranges::iter_swap(__first, --__tail);
1742 ++__first;
1743 }
1744
1745 if (__first == __middle)
1746 {
1747 ranges::reverse(__middle, __tail);
1748 return {std::move(__tail), std::move(__lasti)};
1749 }
1750 else
1751 {
1752 ranges::reverse(__first, __middle);
1753 return {std::move(__first), std::move(__lasti)};
1754 }
1755 }
1756 else
1757 {
1758 auto __first2 = __middle;
1759 do
1760 {
1761 ranges::iter_swap(__first, __first2);
1762 ++__first;
1763 ++__first2;
1764 if (__first == __middle)
1765 __middle = __first2;
1766 } while (__first2 != __last);
1767
1768 auto __ret = __first;
1769
1770 __first2 = __middle;
1771
1772 while (__first2 != __last)
1773 {
1774 ranges::iter_swap(__first, __first2);
1775 ++__first;
1776 ++__first2;
1777 if (__first == __middle)
1778 __middle = __first2;
1779 else if (__first2 == __last)
1780 __first2 = __middle;
1781 }
1782 return {std::move(__ret), std::move(__lasti)};
1783 }
1784 }
1785
1786 template<forward_range _Range>
1787 requires permutable<iterator_t<_Range>>
1788 constexpr borrowed_subrange_t<_Range>
1789 operator()(_Range&& __r, iterator_t<_Range> __middle) const
1790 {
1791 return (*this)(ranges::begin(__r), std::move(__middle),
1792 ranges::end(__r));
1793 }
1794 };
1795
1796 inline constexpr __rotate_fn rotate{};
1797
1798 template<typename _Iter, typename _Out>
1799 using rotate_copy_result = in_out_result<_Iter, _Out>;
1800
1801 struct __rotate_copy_fn
1802 {
1803 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1804 weakly_incrementable _Out>
1805 requires indirectly_copyable<_Iter, _Out>
1806 constexpr rotate_copy_result<_Iter, _Out>
1807 operator()(_Iter __first, _Iter __middle, _Sent __last,
1808 _Out __result) const
1809 {
1810 auto __copy1 = ranges::copy(__middle,
1811 std::move(__last),
1812 std::move(__result));
1813 auto __copy2 = ranges::copy(std::move(__first),
1814 std::move(__middle),
1815 std::move(__copy1.out));
1816 return { std::move(__copy1.in), std::move(__copy2.out) };
1817 }
1818
1819 template<forward_range _Range, weakly_incrementable _Out>
1820 requires indirectly_copyable<iterator_t<_Range>, _Out>
1821 constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
1822 operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
1823 {
1824 return (*this)(ranges::begin(__r), std::move(__middle),
1825 ranges::end(__r), std::move(__result));
1826 }
1827 };
1828
1829 inline constexpr __rotate_copy_fn rotate_copy{};
1830
1831 struct __sample_fn
1832 {
1833 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1834 weakly_incrementable _Out, typename _Gen>
1835 requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
1836 && indirectly_copyable<_Iter, _Out>
1837 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1838 _Out
1839 operator()(_Iter __first, _Sent __last, _Out __out,
1840 iter_difference_t<_Iter> __n, _Gen&& __g) const
1841 {
1842 // FIXME: Correctly handle integer-class difference types.
1843 if constexpr (forward_iterator<_Iter>)
1844 {
1845 using _Size = iter_difference_t<_Iter>;
1846 using __distrib_type = uniform_int_distribution<_Size>;
1847 using __param_type = typename __distrib_type::param_type;
1848 using _USize = __detail::__make_unsigned_like_t<_Size>;
1849 using __uc_type
1850 = common_type_t<typename remove_reference_t<_Gen>::result_type, _USize>;
1851
1852 if (__first == __last)
1853 return __out;
1854
1855 __distrib_type __d{};
1856 _Size __unsampled_sz = ranges::distance(__first, __last);
1857 __n = std::min(__n, __unsampled_sz);
1858
1859 // If possible, we use __gen_two_uniform_ints to efficiently produce
1860 // two random numbers using a single distribution invocation:
1861
1862 const __uc_type __urngrange = __g.max() - __g.min();
1863 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
1864 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
1865 // wrapping issues.
1866 {
1867 while (__n != 0 && __unsampled_sz >= 2)
1868 {
1869 const pair<_Size, _Size> __p =
1870 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
1871
1872 --__unsampled_sz;
1873 if (__p.first < __n)
1874 {
1875 *__out = *__first;
1876 ++__out;
1877 --__n;
1878 }
1879
1880 ++__first;
1881
1882 if (__n == 0) break;
1883
1884 --__unsampled_sz;
1885 if (__p.second < __n)
1886 {
1887 *__out = *__first;
1888 ++__out;
1889 --__n;
1890 }
1891
1892 ++__first;
1893 }
1894 }
1895
1896 // The loop above is otherwise equivalent to this one-at-a-time version:
1897
1898 for (; __n != 0; ++__first)
1899 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
1900 {
1901 *__out = *__first;
1902 ++__out;
1903 --__n;
1904 }
1905 return __out;
1906 }
1907 else
1908 {
1909 using __distrib_type
1910 = uniform_int_distribution<iter_difference_t<_Iter>>;
1911 using __param_type = typename __distrib_type::param_type;
1912 __distrib_type __d{};
1913 iter_difference_t<_Iter> __sample_sz = 0;
1914 while (__first != __last && __sample_sz != __n)
1915 {
1916 __out[__sample_sz++] = *__first;
1917 ++__first;
1918 }
1919 for (auto __pop_sz = __sample_sz; __first != __last;
1920 ++__first, (void) ++__pop_sz)
1921 {
1922 const auto __k = __d(__g, __param_type{0, __pop_sz});
1923 if (__k < __n)
1924 __out[__k] = *__first;
1925 }
1926 return __out + iter_difference_t<_Out>(__sample_sz);
1927 }
1928 }
1929
1930 template<input_range _Range, weakly_incrementable _Out, typename _Gen>
1931 requires (forward_range<_Range> || random_access_iterator<_Out>)
1932 && indirectly_copyable<iterator_t<_Range>, _Out>
1933 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1934 _Out
1935 operator()(_Range&& __r, _Out __out,
1936 range_difference_t<_Range> __n, _Gen&& __g) const
1937 {
1938 return (*this)(ranges::begin(__r), ranges::end(__r),
1939 std::move(__out), __n,
1940 std::forward<_Gen>(__g));
1941 }
1942 };
1943
1944 inline constexpr __sample_fn sample{};
1945
1946 struct __shuffle_fn
1947 {
1948 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1949 typename _Gen>
1950 requires permutable<_Iter>
1951 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1952 _Iter
1953 operator()(_Iter __first, _Sent __last, _Gen&& __g) const
1954 {
1955 // FIXME: Correctly handle integer-class difference types.
1956 if (__first == __last)
1957 return __first;
1958
1959 using _DistanceType = iter_difference_t<_Iter>;
1960 using __ud_type = __detail::__make_unsigned_like_t<_DistanceType>;
1961 using __distr_type = std::uniform_int_distribution<__ud_type>;
1962 using __p_type = typename __distr_type::param_type;
1963
1964 using __uc_type
1965 = common_type_t<typename remove_reference_t<_Gen>::result_type, __ud_type>;
1966
1967 const __uc_type __urngrange = __g.max() - __g.min();
1968 const __uc_type __urange = __uc_type(__last - __first);
1969
1970 if (__urngrange / __urange >= __urange)
1971 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
1972 {
1973 _Iter __i = __first + 1;
1974
1975 // Since we know the range isn't empty, an even number of elements
1976 // means an uneven number of elements /to swap/, in which case we
1977 // do the first one up front:
1978
1979 if ((__urange % 2) == 0)
1980 {
1981 __distr_type __d{0, 1};
1982 ranges::iter_swap(__i++, __first + __d(__g));
1983 }
1984
1985 // Now we know that __last - __i is even, so we do the rest in pairs,
1986 // using a single distribution invocation to produce swap positions
1987 // for two successive elements at a time:
1988
1989 while (__i != __last)
1990 {
1991 const __uc_type __swap_range = __uc_type(__i - __first) + 1;
1992
1993 const pair<__uc_type, __uc_type> __pospos =
1994 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
1995
1996 ranges::iter_swap(__i++, __first + __pospos.first);
1997 ranges::iter_swap(__i++, __first + __pospos.second);
1998 }
1999
2000 return __i;
2001 }
2002
2003 __distr_type __d;
2004
2005 _Iter __i = __first + 1;
2006 for (; __i != __last; ++__i)
2007 ranges::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
2008
2009 return __i;
2010 }
2011
2012 template<random_access_range _Range, typename _Gen>
2013 requires permutable<iterator_t<_Range>>
2014 && uniform_random_bit_generator<remove_reference_t<_Gen>>
2015 borrowed_iterator_t<_Range>
2016 operator()(_Range&& __r, _Gen&& __g) const
2017 {
2018 return (*this)(ranges::begin(__r), ranges::end(__r),
2019 std::forward<_Gen>(__g));
2020 }
2021 };
2022
2023 inline constexpr __shuffle_fn shuffle{};
2024
2025 namespace __detail
2026 {
2027 template<typename _Iter, typename _Comp>
2028 constexpr void
2029 __push_heap(_Iter __first,
2030 iter_difference_t<_Iter> __holeIndex,
2031 iter_difference_t<_Iter> __topIndex,
2032 iter_value_t<_Iter> __value,
2033 _Comp __comp)
2034 {
2035 auto __parent = (__holeIndex - 1) / 2;
2036 while (__holeIndex > __topIndex
2037 && __comp(*(__first + __parent), __value))
2038 {
2039 *(__first + __holeIndex) = ranges::iter_move(__first + __parent);
2040 __holeIndex = __parent;
2041 __parent = (__holeIndex - 1) / 2;
2042 }
2043 *(__first + __holeIndex) = std::move(__value);
2044 }
2045 } // namespace __detail
2046
2047 struct __push_heap_fn
2048 {
2049 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2050 typename _Comp = ranges::less, typename _Proj = identity>
2051 requires sortable<_Iter, _Comp, _Proj>
2052 constexpr _Iter
2053 operator()(_Iter __first, _Sent __last,
2054 _Comp __comp = {}, _Proj __proj = {}) const
2055 {
2056 if constexpr (!same_as<_Iter, _Sent>)
2057 return (*this)(__first, ranges::next(__first, __last),
2058 std::move(__comp), std::move(__proj));
2059 else
2060 {
2061 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2062 __detail::__push_heap(__first, (__last - __first) - 1,
2063 0, ranges::iter_move(__last - 1),
2064 __comp_proj);
2065 return __last;
2066 }
2067 }
2068
2069 template<random_access_range _Range,
2070 typename _Comp = ranges::less, typename _Proj = identity>
2071 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2072 constexpr borrowed_iterator_t<_Range>
2073 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2074 {
2075 return (*this)(ranges::begin(__r), ranges::end(__r),
2076 std::move(__comp), std::move(__proj));
2077 }
2078 };
2079
2080 inline constexpr __push_heap_fn push_heap{};
2081
2082 namespace __detail
2083 {
2084 template<typename _Iter, typename _Comp>
2085 constexpr void
2086 __adjust_heap(_Iter __first,
2087 iter_difference_t<_Iter> __holeIndex,
2088 iter_difference_t<_Iter> __len,
2089 iter_value_t<_Iter> __value,
2090 _Comp __comp)
2091 {
2092 auto __topIndex = __holeIndex;
2093 auto __secondChild = __holeIndex;
2094 while (__secondChild < (__len - 1) / 2)
2095 {
2096 __secondChild = 2 * (__secondChild + 1);
2097 if (__comp(*(__first + __secondChild),
2098 *(__first + (__secondChild - 1))))
2099 __secondChild--;
2100 *(__first + __holeIndex) = ranges::iter_move(__first + __secondChild);
2101 __holeIndex = __secondChild;
2102 }
2103 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
2104 {
2105 __secondChild = 2 * (__secondChild + 1);
2106 *(__first + __holeIndex) = ranges::iter_move(__first + (__secondChild - 1));
2107 __holeIndex = __secondChild - 1;
2108 }
2109 __detail::__push_heap(__first, __holeIndex, __topIndex,
2110 std::move(__value), __comp);
2111 }
2112
2113 template<typename _Iter, typename _Comp>
2114 constexpr void
2115 __pop_heap(_Iter __first, _Iter __last, _Iter __result, _Comp __comp)
2116 {
2117 iter_value_t<_Iter> __value = ranges::iter_move(__result);
2118 *__result = ranges::iter_move(__first);
2119 __detail::__adjust_heap(__first, 0, __last - __first,
2120 std::move(__value), __comp);
2121 }
2122 } // namespace __detail
2123
2124 struct __pop_heap_fn
2125 {
2126 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2127 typename _Comp = ranges::less, typename _Proj = identity>
2128 requires sortable<_Iter, _Comp, _Proj>
2129 constexpr _Iter
2130 operator()(_Iter __first, _Sent __last,
2131 _Comp __comp = {}, _Proj __proj = {}) const
2132 {
2133 if constexpr (!same_as<_Iter, _Sent>)
2134 return (*this)(__first, ranges::next(__first, __last),
2135 std::move(__comp), std::move(__proj));
2136 else
2137 {
2138 if (__last - __first > 1)
2139 {
2140 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2141 __detail::__pop_heap(__first, __last - 1, __last - 1, __comp_proj);
2142 }
2143 return __last;
2144 }
2145 }
2146
2147 template<random_access_range _Range,
2148 typename _Comp = ranges::less, typename _Proj = identity>
2149 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2150 constexpr borrowed_iterator_t<_Range>
2151 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2152 {
2153 return (*this)(ranges::begin(__r), ranges::end(__r),
2154 std::move(__comp), std::move(__proj));
2155 }
2156 };
2157
2158 inline constexpr __pop_heap_fn pop_heap{};
2159
2160 struct __make_heap_fn
2161 {
2162 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2163 typename _Comp = ranges::less, typename _Proj = identity>
2164 requires sortable<_Iter, _Comp, _Proj>
2165 constexpr _Iter
2166 operator()(_Iter __first, _Sent __last,
2167 _Comp __comp = {}, _Proj __proj = {}) const
2168 {
2169 if constexpr (!same_as<_Iter, _Sent>)
2170 return (*this)(__first, ranges::next(__first, __last),
2171 std::move(__comp), std::move(__proj));
2172 else
2173 {
2174 const auto __len = __last - __first;
2175 if (__len < 2)
2176 return __last;
2177
2178 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2179 auto __parent = (__len - 2) / 2;
2180 while (true)
2181 {
2182 iter_value_t<_Iter> __value = ranges::iter_move(__first + __parent);
2183 __detail::__adjust_heap(__first, __parent, __len,
2184 std::move(__value),
2185 __comp_proj);
2186 if (__parent == 0)
2187 break;
2188 __parent--;
2189 }
2190 return __last;
2191 }
2192 }
2193
2194 template<random_access_range _Range,
2195 typename _Comp = ranges::less, typename _Proj = identity>
2196 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2197 constexpr borrowed_iterator_t<_Range>
2198 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2199 {
2200 return (*this)(ranges::begin(__r), ranges::end(__r),
2201 std::move(__comp), std::move(__proj));
2202 }
2203 };
2204
2205 inline constexpr __make_heap_fn make_heap{};
2206
2207 struct __sort_heap_fn
2208 {
2209 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2210 typename _Comp = ranges::less, typename _Proj = identity>
2211 requires sortable<_Iter, _Comp, _Proj>
2212 constexpr _Iter
2213 operator()(_Iter __first, _Sent __last,
2214 _Comp __comp = {}, _Proj __proj = {}) const
2215 {
2216 if constexpr (!same_as<_Iter, _Sent>)
2217 return (*this)(__first, ranges::next(__first, __last),
2218 std::move(__comp), std::move(__proj));
2219 else
2220 {
2221 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2222 _Iter __ret = __last;
2223 while (__last - __first > 1)
2224 {
2225 --__last;
2226 __detail::__pop_heap(__first, __last, __last, __comp_proj);
2227 }
2228 return __ret;
2229 }
2230 }
2231
2232 template<random_access_range _Range,
2233 typename _Comp = ranges::less, typename _Proj = identity>
2234 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2235 constexpr borrowed_iterator_t<_Range>
2236 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2237 {
2238 return (*this)(ranges::begin(__r), ranges::end(__r),
2239 std::move(__comp), std::move(__proj));
2240 }
2241 };
2242
2243 inline constexpr __sort_heap_fn sort_heap{};
2244
2245 struct __is_heap_until_fn
2246 {
2247 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2248 typename _Proj = identity,
2249 indirect_strict_weak_order<projected<_Iter, _Proj>>
2250 _Comp = ranges::less>
2251 constexpr _Iter
2252 operator()(_Iter __first, _Sent __last,
2253 _Comp __comp = {}, _Proj __proj = {}) const
2254 {
2255 iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
2256 iter_difference_t<_Iter> __parent = 0, __child = 1;
2257 for (; __child < __n; ++__child)
2258 if (std::__invoke(__comp,
2259 std::__invoke(__proj, *(__first + __parent)),
2260 std::__invoke(__proj, *(__first + __child))))
2261 return __first + __child;
2262 else if ((__child & 1) == 0)
2263 ++__parent;
2264
2265 return __first + __n;
2266 }
2267
2268 template<random_access_range _Range,
2269 typename _Proj = identity,
2270 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2271 _Comp = ranges::less>
2272 constexpr borrowed_iterator_t<_Range>
2273 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2274 {
2275 return (*this)(ranges::begin(__r), ranges::end(__r),
2276 std::move(__comp), std::move(__proj));
2277 }
2278 };
2279
2280 inline constexpr __is_heap_until_fn is_heap_until{};
2281
2282 struct __is_heap_fn
2283 {
2284 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2285 typename _Proj = identity,
2286 indirect_strict_weak_order<projected<_Iter, _Proj>>
2287 _Comp = ranges::less>
2288 constexpr bool
2289 operator()(_Iter __first, _Sent __last,
2290 _Comp __comp = {}, _Proj __proj = {}) const
2291 {
2292 return (__last
2293 == ranges::is_heap_until(__first, __last,
2294 std::move(__comp),
2295 std::move(__proj)));
2296 }
2297
2298 template<random_access_range _Range,
2299 typename _Proj = identity,
2300 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2301 _Comp = ranges::less>
2302 constexpr bool
2303 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2304 {
2305 return (*this)(ranges::begin(__r), ranges::end(__r),
2306 std::move(__comp), std::move(__proj));
2307 }
2308 };
2309
2310 inline constexpr __is_heap_fn is_heap{};
2311
2312 namespace __detail
2313 {
2314 template<typename _Iter, typename _Comp>
2315 constexpr void
2316 __move_median_to_first(_Iter __result, _Iter __a, _Iter __b, _Iter __c,
2317 _Comp __comp)
2318 {
2319 if (__comp(*__a, *__b))
2320 {
2321 if (__comp(*__b, *__c))
2322 ranges::iter_swap(__result, __b);
2323 else if (__comp(*__a, *__c))
2324 ranges::iter_swap(__result, __c);
2325 else
2326 ranges::iter_swap(__result, __a);
2327 }
2328 else if (__comp(*__a, *__c))
2329 ranges::iter_swap(__result, __a);
2330 else if (__comp(*__b, *__c))
2331 ranges::iter_swap(__result, __c);
2332 else
2333 ranges::iter_swap(__result, __b);
2334 }
2335
2336 template<typename _Iter, typename _Comp>
2337 constexpr void
2338 __unguarded_linear_insert(_Iter __last, _Comp __comp)
2339 {
2340 iter_value_t<_Iter> __val = ranges::iter_move(__last);
2341 _Iter __next = __last;
2342 --__next;
2343 while (__comp(__val, *__next))
2344 {
2345 *__last = ranges::iter_move(__next);
2346 __last = __next;
2347 --__next;
2348 }
2349 *__last = std::move(__val);
2350 }
2351
2352 template<typename _Iter, typename _Comp>
2353 constexpr void
2354 __insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2355 {
2356 if (__first == __last)
2357 return;
2358
2359 for (_Iter __i = __first + 1; __i != __last; ++__i)
2360 {
2361 if (__comp(*__i, *__first))
2362 {
2363 iter_value_t<_Iter> __val = ranges::iter_move(__i);
2364 ranges::move_backward(__first, __i, __i + 1);
2365 *__first = std::move(__val);
2366 }
2367 else
2368 __detail::__unguarded_linear_insert(__i, __comp);
2369 }
2370 }
2371
2372 template<typename _Iter, typename _Comp>
2373 constexpr void
2374 __unguarded_insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2375 {
2376 for (_Iter __i = __first; __i != __last; ++__i)
2377 __detail::__unguarded_linear_insert(__i, __comp);
2378 }
2379
2380 inline constexpr int __sort_threshold = 16;
2381
2382 template<typename _Iter, typename _Comp>
2383 constexpr void
2384 __final_insertion_sort(_Iter __first, _Iter __last, _Comp __comp)
2385 {
2386 if (__last - __first > __sort_threshold)
2387 {
2388 __detail::__insertion_sort(__first, __first + __sort_threshold, __comp);
2389 __detail::__unguarded_insertion_sort(__first + __sort_threshold, __last,
2390 __comp);
2391 }
2392 else
2393 __detail::__insertion_sort(__first, __last, __comp);
2394 }
2395
2396 template<typename _Iter, typename _Comp>
2397 constexpr _Iter
2398 __unguarded_partition(_Iter __first, _Iter __last, _Iter __pivot, _Comp __comp)
2399 {
2400 while (true)
2401 {
2402 while (__comp(*__first, *__pivot))
2403 ++__first;
2404 --__last;
2405 while (__comp(*__pivot, *__last))
2406 --__last;
2407 if (!(__first < __last))
2408 return __first;
2409 ranges::iter_swap(__first, __last);
2410 ++__first;
2411 }
2412 }
2413
2414 template<typename _Iter, typename _Comp>
2415 constexpr _Iter
2416 __unguarded_partition_pivot(_Iter __first, _Iter __last, _Comp __comp)
2417 {
2418 _Iter __mid = __first + (__last - __first) / 2;
2419 __detail::__move_median_to_first(__first, __first + 1, __mid, __last - 1, __comp);
2420 return __detail::__unguarded_partition(__first + 1, __last, __first, __comp);
2421 }
2422
2423 template<typename _Iter, typename _Comp>
2424 constexpr void
2425 __heap_select(_Iter __first, _Iter __middle, _Iter __last, _Comp __comp)
2426 {
2427 ranges::make_heap(__first, __middle, __comp);
2428 for (_Iter __i = __middle; __i < __last; ++__i)
2429 if (__comp(*__i, *__first))
2430 __detail::__pop_heap(__first, __middle, __i, __comp);
2431 }
2432
2433 template<typename _Iter, typename _Comp>
2434 constexpr void
2435 __partial_sort(_Iter __first, _Iter __middle, _Iter __last, _Comp __comp)
2436 {
2437 __detail::__heap_select(__first, __middle, __last, __comp);
2438 ranges::sort_heap(__first, __middle, __comp);
2439 }
2440
2441 template<typename _Iter, typename _Comp>
2442 constexpr void
2443 __introsort_loop(_Iter __first, _Iter __last, unsigned __depth_limit, _Comp __comp)
2444 {
2445 while (__last - __first > __sort_threshold)
2446 {
2447 if (__depth_limit == 0)
2448 {
2449 __detail::__partial_sort(__first, __last, __last, __comp);
2450 return;
2451 }
2452 --__depth_limit;
2453 _Iter __cut = __detail::__unguarded_partition_pivot(__first, __last, __comp);
2454 __detail::__introsort_loop(__cut, __last, __depth_limit, __comp);
2455 __last = __cut;
2456 }
2457 }
2458 } // namespace __detail
2459
2460 struct __sort_fn
2461 {
2462 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2463 typename _Comp = ranges::less, typename _Proj = identity>
2464 requires sortable<_Iter, _Comp, _Proj>
2465 constexpr _Iter
2466 operator()(_Iter __first, _Sent __last,
2467 _Comp __comp = {}, _Proj __proj = {}) const
2468 {
2469 if constexpr (!same_as<_Iter, _Sent>)
2470 return (*this)(__first, ranges::next(__first, __last),
2471 std::move(__comp), std::move(__proj));
2472 else
2473 {
2474 if (__first != __last)
2475 {
2476 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2477 auto __n = __detail::__to_unsigned_like(__last - __first);
2478 unsigned __depth_limit = (std::__bit_width(__n) - 1) * 2;
2479 __detail::__introsort_loop(__first, __last, __depth_limit, __comp_proj);
2480 __detail::__final_insertion_sort(__first, __last, __comp_proj);
2481 }
2482 return __last;
2483 }
2484 }
2485
2486 template<random_access_range _Range,
2487 typename _Comp = ranges::less, typename _Proj = identity>
2488 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2489 constexpr borrowed_iterator_t<_Range>
2490 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2491 {
2492 return (*this)(ranges::begin(__r), ranges::end(__r),
2493 std::move(__comp), std::move(__proj));
2494 }
2495 };
2496
2497 inline constexpr __sort_fn sort{};
2498
2499 namespace __detail
2500 {
2501 // This is a helper function for the __merge_sort_loop routines.
2502 template<typename _Iter, typename _Out, typename _Comp>
2503 _Out
2504 __move_merge(_Iter __first1, _Iter __last1,
2505 _Iter __first2, _Iter __last2,
2506 _Out __result, _Comp __comp)
2507 {
2508 while (__first1 != __last1 && __first2 != __last2)
2509 {
2510 if (__comp(*__first2, *__first1))
2511 {
2512 *__result = ranges::iter_move(__first2);
2513 ++__first2;
2514 }
2515 else
2516 {
2517 *__result = ranges::iter_move(__first1);
2518 ++__first1;
2519 }
2520 ++__result;
2521 }
2522 return ranges::move(__first2, __last2,
2523 ranges::move(__first1, __last1, __result).out).out;
2524 }
2525
2526 template<typename _Iter, typename _Out, typename _Distance, typename _Comp>
2527 void
2528 __merge_sort_loop(_Iter __first, _Iter __last, _Out __result,
2529 _Distance __step_size, _Comp __comp)
2530 {
2531 const _Distance __two_step = 2 * __step_size;
2532
2533 while (__last - __first >= __two_step)
2534 {
2535 __result = __detail::__move_merge(__first, __first + __step_size,
2536 __first + __step_size,
2537 __first + __two_step,
2538 __result, __comp);
2539 __first += __two_step;
2540 }
2541 __step_size = ranges::min(_Distance(__last - __first), __step_size);
2542
2543 __detail::__move_merge(__first, __first + __step_size,
2544 __first + __step_size, __last, __result, __comp);
2545 }
2546
2547 template<typename _Iter, typename _Distance, typename _Compare>
2548 constexpr void
2549 __chunk_insertion_sort(_Iter __first, _Iter __last,
2550 _Distance __chunk_size, _Compare __comp)
2551 {
2552 while (__last - __first >= __chunk_size)
2553 {
2554 __detail::__insertion_sort(__first, __first + __chunk_size, __comp);
2555 __first += __chunk_size;
2556 }
2557 __detail::__insertion_sort(__first, __last, __comp);
2558 }
2559
2560 template<typename _Iter, typename _Pointer, typename _Comp>
2561 void
2562 __merge_sort_with_buffer(_Iter __first, _Iter __last,
2563 _Pointer __buffer, _Comp __comp)
2564 {
2565 using _Distance = iter_difference_t<_Iter>;
2566
2567 const _Distance __len = __last - __first;
2568 const _Pointer __buffer_last = __buffer + ptrdiff_t(__len);
2569
2570 constexpr int __chunk_size = 7;
2571 _Distance __step_size = __chunk_size;
2572 __detail::__chunk_insertion_sort(__first, __last, __step_size, __comp);
2573
2574 while (__step_size < __len)
2575 {
2576 __detail::__merge_sort_loop(__first, __last, __buffer,
2577 __step_size, __comp);
2578 __step_size *= 2;
2579 __detail::__merge_sort_loop(__buffer, __buffer_last, __first,
2580 ptrdiff_t(__step_size), __comp);
2581 __step_size *= 2;
2582 }
2583 }
2584
2585 template<typename _Iter, typename _Pointer, typename _Comp>
2586 void
2587 __merge_adaptive(_Iter __first, _Iter __middle, _Iter __last,
2588 iter_difference_t<_Iter> __len1,
2589 iter_difference_t<_Iter> __len2,
2590 _Pointer __buffer, _Comp __comp); // defined near inplace_merge
2591
2592 template<typename _Iter, typename _Distance, typename _Pointer, typename _Comp>
2593 void
2594 __merge_adaptive_resize(_Iter __first, _Iter __middle, _Iter __last,
2595 _Distance __len1, _Distance __len2,
2596 _Pointer __buffer, _Distance __buffer_size,
2597 _Comp __comp); // defined near inplace_merge
2598
2599 template<typename _Iter, typename _Distance, typename _Comp>
2600 constexpr void
2601 __merge_without_buffer(_Iter __first, _Iter __middle, _Iter __last,
2602 _Distance __len1, _Distance __len2,
2603 _Comp __comp); // defined near inplace_merge
2604
2605 template<typename _Iter, typename _Pointer, typename _Comp>
2606 void
2607 __stable_sort_adaptive(_Iter __first, _Iter __middle, _Iter __last,
2608 _Pointer __buffer, _Comp __comp)
2609 {
2610 __detail::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2611 __detail::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2612
2613 __detail::__merge_adaptive(__first, __middle, __last,
2614 __middle - __first, __last - __middle,
2615 __buffer, __comp);
2616 }
2617
2618 template<typename _Iter, typename _Pointer, typename _Distance, typename _Comp>
2619 void
2620 __stable_sort_adaptive_resize(_Iter __first, _Iter __last,
2621 _Pointer __buffer, _Distance __buffer_size,
2622 _Comp __comp)
2623 {
2624 const _Distance __len = (__last - __first + 1) / 2;
2625 const _Iter __middle = __first + __len;
2626 if (__len > __buffer_size)
2627 {
2628 __detail::__stable_sort_adaptive_resize(__first, __middle, __buffer,
2629 __buffer_size, __comp);
2630 __detail::__stable_sort_adaptive_resize(__middle, __last, __buffer,
2631 __buffer_size, __comp);
2632 __detail::__merge_adaptive_resize(__first, __middle, __last,
2633 _Distance(__middle - __first),
2634 _Distance(__last - __middle),
2635 __buffer, __buffer_size,
2636 __comp);
2637 }
2638 else
2639 __detail::__stable_sort_adaptive(__first, __middle, __last,
2640 __buffer, __comp);
2641 }
2642
2643 template<typename _Iter, typename _Comp>
2644 constexpr void
2645 __inplace_stable_sort(_Iter __first, _Iter __last, _Comp __comp)
2646 {
2647 if (__last - __first < 15)
2648 {
2649 __detail::__insertion_sort(__first, __last, __comp);
2650 return;
2651 }
2652 _Iter __middle = __first + (__last - __first) / 2;
2653 __detail::__inplace_stable_sort(__first, __middle, __comp);
2654 __detail::__inplace_stable_sort(__middle, __last, __comp);
2655 __detail::__merge_without_buffer(__first, __middle, __last,
2656 __middle - __first,
2657 __last - __middle,
2658 __comp);
2659 }
2660 } // namespace __detail
2661
2662 struct __stable_sort_fn
2663 {
2664 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2665 typename _Comp = ranges::less, typename _Proj = identity>
2666 requires sortable<_Iter, _Comp, _Proj>
2667 _GLIBCXX26_CONSTEXPR
2668 _Iter
2669 operator()(_Iter __first, _Sent __last,
2670 _Comp __comp = {}, _Proj __proj = {}) const
2671 {
2672 if constexpr (!same_as<_Iter, _Sent>)
2673 return (*this)(__first, ranges::next(__first, __last),
2674 std::move(__comp), std::move(__proj));
2675 else
2676 {
2677 using _DistanceType = iter_difference_t<_Iter>;
2678
2679 if (__first == __last)
2680 return __last;
2681
2682 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2683
2684#if _GLIBCXX_HOSTED
2685# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
2686 if consteval {
2687 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2688 return __last;
2689 }
2690# endif
2691
2692 typedef _Temporary_buffer<_Iter, iter_value_t<_Iter>> _TmpBuf;
2693 // __stable_sort_adaptive sorts the range in two halves,
2694 // so the buffer only needs to fit half the range at once.
2695 _TmpBuf __buf(__first, ptrdiff_t((__last - __first + 1) / 2));
2696
2697 if (__buf._M_requested_size() == __buf.size()) [[likely]]
2698 __detail::__stable_sort_adaptive(__first,
2699 __first + _DistanceType(__buf.size()),
2700 __last, __buf.begin(), __comp_proj);
2701 else if (__buf.begin()) [[unlikely]]
2702 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2703 else
2704 __detail::__stable_sort_adaptive_resize(__first, __last, __buf.begin(),
2705 _DistanceType(__buf.size()),
2706 __comp_proj);
2707#else
2708 __detail::__inplace_stable_sort(__first, __last, __comp_proj);
2709#endif
2710 return __last;
2711 }
2712 }
2713
2714 template<random_access_range _Range,
2715 typename _Comp = ranges::less, typename _Proj = identity>
2716 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2717 _GLIBCXX26_CONSTEXPR
2718 borrowed_iterator_t<_Range>
2719 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2720 {
2721 return (*this)(ranges::begin(__r), ranges::end(__r),
2722 std::move(__comp), std::move(__proj));
2723 }
2724 };
2725
2726 inline constexpr __stable_sort_fn stable_sort{};
2727
2728 struct __partial_sort_fn
2729 {
2730 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2731 typename _Comp = ranges::less, typename _Proj = identity>
2732 requires sortable<_Iter, _Comp, _Proj>
2733 constexpr _Iter
2734 operator()(_Iter __first, _Iter __middle, _Sent __last,
2735 _Comp __comp = {}, _Proj __proj = {}) const
2736 {
2737 if (__first == __middle)
2738 return ranges::next(__first, __last);
2739
2740 ranges::make_heap(__first, __middle, __comp, __proj);
2741 auto __i = __middle;
2742 for (; __i != __last; ++__i)
2743 if (std::__invoke(__comp,
2744 std::__invoke(__proj, *__i),
2745 std::__invoke(__proj, *__first)))
2746 {
2747 ranges::pop_heap(__first, __middle, __comp, __proj);
2748 ranges::iter_swap(__middle-1, __i);
2749 ranges::push_heap(__first, __middle, __comp, __proj);
2750 }
2751 ranges::sort_heap(__first, __middle, __comp, __proj);
2752
2753 return __i;
2754 }
2755
2756 template<random_access_range _Range,
2757 typename _Comp = ranges::less, typename _Proj = identity>
2758 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2759 constexpr borrowed_iterator_t<_Range>
2760 operator()(_Range&& __r, iterator_t<_Range> __middle,
2761 _Comp __comp = {}, _Proj __proj = {}) const
2762 {
2763 return (*this)(ranges::begin(__r), std::move(__middle),
2764 ranges::end(__r),
2765 std::move(__comp), std::move(__proj));
2766 }
2767 };
2768
2769 inline constexpr __partial_sort_fn partial_sort{};
2770
2771 template<typename _Iter, typename _Out>
2772 using partial_sort_copy_result = in_out_result<_Iter, _Out>;
2773
2774 struct __partial_sort_copy_fn
2775 {
2776 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2777 random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2778 typename _Comp = ranges::less,
2779 typename _Proj1 = identity, typename _Proj2 = identity>
2780 requires indirectly_copyable<_Iter1, _Iter2>
2781 && sortable<_Iter2, _Comp, _Proj2>
2782 && indirect_strict_weak_order<_Comp,
2783 projected<_Iter1, _Proj1>,
2784 projected<_Iter2, _Proj2>>
2785 constexpr partial_sort_copy_result<_Iter1, _Iter2>
2786 operator()(_Iter1 __first, _Sent1 __last,
2787 _Iter2 __result_first, _Sent2 __result_last,
2788 _Comp __comp = {},
2789 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2790 {
2791 if (__result_first == __result_last)
2792 {
2793 // TODO: Eliminating the variable __lasti triggers an ICE.
2794 auto __lasti = ranges::next(std::move(__first),
2795 std::move(__last));
2796 return {std::move(__lasti), std::move(__result_first)};
2797 }
2798
2799 auto __result_real_last = __result_first;
2800 while (__first != __last && __result_real_last != __result_last)
2801 {
2802 *__result_real_last = *__first;
2803 ++__result_real_last;
2804 ++__first;
2805 }
2806
2807 ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
2808 for (; __first != __last; ++__first)
2809 if (std::__invoke(__comp,
2810 std::__invoke(__proj1, *__first),
2811 std::__invoke(__proj2, *__result_first)))
2812 {
2813 ranges::pop_heap(__result_first, __result_real_last,
2814 __comp, __proj2);
2815 *(__result_real_last-1) = *__first;
2816 ranges::push_heap(__result_first, __result_real_last,
2817 __comp, __proj2);
2818 }
2819 ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
2820
2821 return {std::move(__first), std::move(__result_real_last)};
2822 }
2823
2824 template<input_range _Range1, random_access_range _Range2,
2825 typename _Comp = ranges::less,
2826 typename _Proj1 = identity, typename _Proj2 = identity>
2827 requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
2828 && sortable<iterator_t<_Range2>, _Comp, _Proj2>
2829 && indirect_strict_weak_order<_Comp,
2830 projected<iterator_t<_Range1>, _Proj1>,
2831 projected<iterator_t<_Range2>, _Proj2>>
2832 constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
2833 borrowed_iterator_t<_Range2>>
2834 operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
2835 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
2836 {
2837 return (*this)(ranges::begin(__r), ranges::end(__r),
2838 ranges::begin(__out), ranges::end(__out),
2839 std::move(__comp),
2840 std::move(__proj1), std::move(__proj2));
2841 }
2842 };
2843
2844 inline constexpr __partial_sort_copy_fn partial_sort_copy{};
2845
2846 struct __is_sorted_until_fn
2847 {
2848 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2849 typename _Proj = identity,
2850 indirect_strict_weak_order<projected<_Iter, _Proj>>
2851 _Comp = ranges::less>
2852 [[nodiscard]] constexpr _Iter
2853 operator()(_Iter __first, _Sent __last,
2854 _Comp __comp = {}, _Proj __proj = {}) const
2855 {
2856 if (__first == __last)
2857 return __first;
2858
2859 auto __next = __first;
2860 for (++__next; __next != __last; __first = __next, (void)++__next)
2861 if (std::__invoke(__comp,
2862 std::__invoke(__proj, *__next),
2863 std::__invoke(__proj, *__first)))
2864 return __next;
2865 return __next;
2866 }
2867
2868 template<forward_range _Range, typename _Proj = identity,
2869 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2870 _Comp = ranges::less>
2871 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
2872 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2873 {
2874 return (*this)(ranges::begin(__r), ranges::end(__r),
2875 std::move(__comp), std::move(__proj));
2876 }
2877 };
2878
2879 inline constexpr __is_sorted_until_fn is_sorted_until{};
2880
2881 struct __is_sorted_fn
2882 {
2883 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2884 typename _Proj = identity,
2885 indirect_strict_weak_order<projected<_Iter, _Proj>>
2886 _Comp = ranges::less>
2887 [[nodiscard]] constexpr bool
2888 operator()(_Iter __first, _Sent __last,
2889 _Comp __comp = {}, _Proj __proj = {}) const
2890 {
2891 if (__first == __last)
2892 return true;
2893
2894 auto __next = __first;
2895 for (++__next; __next != __last; __first = __next, (void)++__next)
2896 if (std::__invoke(__comp,
2897 std::__invoke(__proj, *__next),
2898 std::__invoke(__proj, *__first)))
2899 return false;
2900 return true;
2901 }
2902
2903 template<forward_range _Range, typename _Proj = identity,
2904 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2905 _Comp = ranges::less>
2906 [[nodiscard]] constexpr bool
2907 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
2908 {
2909 return (*this)(ranges::begin(__r), ranges::end(__r),
2910 std::move(__comp), std::move(__proj));
2911 }
2912 };
2913
2914 inline constexpr __is_sorted_fn is_sorted{};
2915
2916 namespace __detail
2917 {
2918 template<typename _Iter, typename _Comp>
2919 constexpr void
2920 __introselect(_Iter __first, _Iter __nth, _Iter __last,
2921 iter_difference_t<_Iter> __depth_limit, _Comp __comp)
2922 {
2923 while (__last - __first > 3)
2924 {
2925 if (__depth_limit == 0)
2926 {
2927 __detail::__heap_select(__first, __nth + 1, __last, __comp);
2928 // Place the nth largest element in its final position.
2929 ranges::iter_swap(__first, __nth);
2930 return;
2931 }
2932 --__depth_limit;
2933 _Iter __cut = __detail::__unguarded_partition_pivot(__first, __last, __comp);
2934 if (__cut <= __nth)
2935 __first = __cut;
2936 else
2937 __last = __cut;
2938 }
2939 __detail::__insertion_sort(__first, __last, __comp);
2940 }
2941 } // namespace __detail
2942
2943 struct __nth_element_fn
2944 {
2945 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2946 typename _Comp = ranges::less, typename _Proj = identity>
2947 requires sortable<_Iter, _Comp, _Proj>
2948 constexpr _Iter
2949 operator()(_Iter __first, _Iter __nth, _Sent __last,
2950 _Comp __comp = {}, _Proj __proj = {}) const
2951 {
2952 if constexpr (!same_as<_Iter, _Sent>)
2953 return (*this)(__first, __nth, ranges::next(__first, __last),
2954 std::move(__comp), std::move(__proj));
2955 else
2956 {
2957 if (__first == __last || __nth == __last)
2958 return __last;
2959
2960 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
2961 auto __n = __detail::__to_unsigned_like(__last - __first);
2962 __detail::__introselect(__first, __nth, __last,
2963 std::__bit_width(__n) * 2,
2964 __comp_proj);
2965 return __last;
2966 }
2967 }
2968
2969 template<random_access_range _Range,
2970 typename _Comp = ranges::less, typename _Proj = identity>
2971 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2972 constexpr borrowed_iterator_t<_Range>
2973 operator()(_Range&& __r, iterator_t<_Range> __nth,
2974 _Comp __comp = {}, _Proj __proj = {}) const
2975 {
2976 return (*this)(ranges::begin(__r), std::move(__nth),
2977 ranges::end(__r), std::move(__comp), std::move(__proj));
2978 }
2979 };
2980
2981 inline constexpr __nth_element_fn nth_element{};
2982
2983 struct __lower_bound_fn
2984 {
2985 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2986 typename _Proj = identity,
2987 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
2988 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2989 _Comp = ranges::less>
2990 [[nodiscard]] constexpr _Iter
2991 operator()(_Iter __first, _Sent __last,
2992 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
2993 {
2994 auto __len = ranges::distance(__first, __last);
2995
2996 while (__len > 0)
2997 {
2998 auto __half = __len / 2;
2999 auto __middle = __first;
3000 ranges::advance(__middle, __half);
3001 if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
3002 {
3003 __first = __middle;
3004 ++__first;
3005 __len = __len - __half - 1;
3006 }
3007 else
3008 __len = __half;
3009 }
3010 return __first;
3011 }
3012
3013 template<forward_range _Range,
3014 typename _Proj = identity,
3015 typename _Tp
3016 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3017 indirect_strict_weak_order<const _Tp*,
3018 projected<iterator_t<_Range>, _Proj>>
3019 _Comp = ranges::less>
3020 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3021 operator()(_Range&& __r,
3022 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3023 {
3024 return (*this)(ranges::begin(__r), ranges::end(__r),
3025 __value, std::move(__comp), std::move(__proj));
3026 }
3027 };
3028
3029 inline constexpr __lower_bound_fn lower_bound{};
3030
3031 struct __upper_bound_fn
3032 {
3033 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3034 typename _Proj = identity,
3035 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3036 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3037 _Comp = ranges::less>
3038 [[nodiscard]] constexpr _Iter
3039 operator()(_Iter __first, _Sent __last,
3040 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3041 {
3042 auto __len = ranges::distance(__first, __last);
3043
3044 while (__len > 0)
3045 {
3046 auto __half = __len / 2;
3047 auto __middle = __first;
3048 ranges::advance(__middle, __half);
3049 if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
3050 __len = __half;
3051 else
3052 {
3053 __first = __middle;
3054 ++__first;
3055 __len = __len - __half - 1;
3056 }
3057 }
3058 return __first;
3059 }
3060
3061 template<forward_range _Range,
3062 typename _Proj = identity,
3063 typename _Tp
3064 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3065 indirect_strict_weak_order<const _Tp*,
3066 projected<iterator_t<_Range>, _Proj>>
3067 _Comp = ranges::less>
3068 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3069 operator()(_Range&& __r,
3070 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3071 {
3072 return (*this)(ranges::begin(__r), ranges::end(__r),
3073 __value, std::move(__comp), std::move(__proj));
3074 }
3075 };
3076
3077 inline constexpr __upper_bound_fn upper_bound{};
3078
3079 struct __equal_range_fn
3080 {
3081 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3082 typename _Proj = identity,
3083 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3084 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3085 _Comp = ranges::less>
3086 [[nodiscard]] constexpr subrange<_Iter>
3087 operator()(_Iter __first, _Sent __last,
3088 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3089 {
3090 auto __len = ranges::distance(__first, __last);
3091
3092 while (__len > 0)
3093 {
3094 auto __half = __len / 2;
3095 auto __middle = __first;
3096 ranges::advance(__middle, __half);
3097 if (std::__invoke(__comp,
3098 std::__invoke(__proj, *__middle),
3099 __value))
3100 {
3101 __first = __middle;
3102 ++__first;
3103 __len = __len - __half - 1;
3104 }
3105 else if (std::__invoke(__comp,
3106 __value,
3107 std::__invoke(__proj, *__middle)))
3108 __len = __half;
3109 else
3110 {
3111 auto __left
3112 = ranges::lower_bound(__first, __middle,
3113 __value, __comp, __proj);
3114 ranges::advance(__first, __len);
3115 auto __right
3116 = ranges::upper_bound(++__middle, __first,
3117 __value, __comp, __proj);
3118 return {__left, __right};
3119 }
3120 }
3121 return {__first, __first};
3122 }
3123
3124 template<forward_range _Range,
3125 typename _Proj = identity,
3126 typename _Tp
3127 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3128 indirect_strict_weak_order<const _Tp*,
3129 projected<iterator_t<_Range>, _Proj>>
3130 _Comp = ranges::less>
3131 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
3132 operator()(_Range&& __r, const _Tp& __value,
3133 _Comp __comp = {}, _Proj __proj = {}) const
3134 {
3135 return (*this)(ranges::begin(__r), ranges::end(__r),
3136 __value, std::move(__comp), std::move(__proj));
3137 }
3138 };
3139
3140 inline constexpr __equal_range_fn equal_range{};
3141
3142 struct __binary_search_fn
3143 {
3144 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3145 typename _Proj = identity,
3146 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj),
3147 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
3148 _Comp = ranges::less>
3149 [[nodiscard]] constexpr bool
3150 operator()(_Iter __first, _Sent __last,
3151 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
3152 {
3153 auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
3154 if (__i == __last)
3155 return false;
3156 return !(bool)std::__invoke(__comp, __value,
3157 std::__invoke(__proj, *__i));
3158 }
3159
3160 template<forward_range _Range,
3161 typename _Proj = identity,
3162 typename _Tp
3163 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj),
3164 indirect_strict_weak_order<const _Tp*,
3165 projected<iterator_t<_Range>, _Proj>>
3166 _Comp = ranges::less>
3167 [[nodiscard]] constexpr bool
3168 operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
3169 _Proj __proj = {}) const
3170 {
3171 return (*this)(ranges::begin(__r), ranges::end(__r),
3172 __value, std::move(__comp), std::move(__proj));
3173 }
3174 };
3175
3176 inline constexpr __binary_search_fn binary_search{};
3177
3178 struct __is_partitioned_fn
3179 {
3180 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3181 typename _Proj = identity,
3182 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3183 [[nodiscard]] constexpr bool
3184 operator()(_Iter __first, _Sent __last,
3185 _Pred __pred, _Proj __proj = {}) const
3186 {
3187 __first = ranges::find_if_not(std::move(__first), __last,
3188 __pred, __proj);
3189 if (__first == __last)
3190 return true;
3191 ++__first;
3192 return ranges::none_of(std::move(__first), std::move(__last),
3193 std::move(__pred), std::move(__proj));
3194 }
3195
3196 template<input_range _Range, typename _Proj = identity,
3197 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3198 _Pred>
3199 [[nodiscard]] constexpr bool
3200 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3201 {
3202 return (*this)(ranges::begin(__r), ranges::end(__r),
3203 std::move(__pred), std::move(__proj));
3204 }
3205 };
3206
3207 inline constexpr __is_partitioned_fn is_partitioned{};
3208
3209 struct __partition_fn
3210 {
3211 template<permutable _Iter, sentinel_for<_Iter> _Sent,
3212 typename _Proj = identity,
3213 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3214 constexpr subrange<_Iter>
3215 operator()(_Iter __first, _Sent __last,
3216 _Pred __pred, _Proj __proj = {}) const
3217 {
3218 if constexpr (bidirectional_iterator<_Iter>)
3219 {
3220 auto __lasti = ranges::next(__first, __last);
3221 auto __tail = __lasti;
3222 for (;;)
3223 {
3224 for (;;)
3225 if (__first == __tail)
3226 return {std::move(__first), std::move(__lasti)};
3227 else if (std::__invoke(__pred,
3228 std::__invoke(__proj, *__first)))
3229 ++__first;
3230 else
3231 break;
3232 --__tail;
3233 for (;;)
3234 if (__first == __tail)
3235 return {std::move(__first), std::move(__lasti)};
3236 else if (!(bool)std::__invoke(__pred,
3237 std::__invoke(__proj, *__tail)))
3238 --__tail;
3239 else
3240 break;
3241 ranges::iter_swap(__first, __tail);
3242 ++__first;
3243 }
3244 }
3245 else
3246 {
3247 if (__first == __last)
3248 return {__first, __first};
3249
3250 while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
3251 if (++__first == __last)
3252 return {__first, __first};
3253
3254 auto __next = __first;
3255 while (++__next != __last)
3256 if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
3257 {
3258 ranges::iter_swap(__first, __next);
3259 ++__first;
3260 }
3261
3262 return {std::move(__first), std::move(__next)};
3263 }
3264 }
3265
3266 template<forward_range _Range, typename _Proj = identity,
3267 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3268 _Pred>
3269 requires permutable<iterator_t<_Range>>
3270 constexpr borrowed_subrange_t<_Range>
3271 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3272 {
3273 return (*this)(ranges::begin(__r), ranges::end(__r),
3274 std::move(__pred), std::move(__proj));
3275 }
3276 };
3277
3278 inline constexpr __partition_fn partition{};
3279
3280#if _GLIBCXX_HOSTED
3281 namespace __detail
3282 {
3283 // Like find_if_not(), but uses and updates a count of the
3284 // remaining range length instead of comparing against an end
3285 // iterator.
3286 template<typename _Iter, typename _Pred, typename _Distance>
3287 constexpr _Iter
3288 __find_if_not_n(_Iter __first, _Distance& __len, _Pred __pred)
3289 {
3290 for (; __len; --__len, (void) ++__first)
3291 if (!__pred(*__first))
3292 break;
3293 return __first;
3294 }
3295
3296 template<typename _Iter, typename _Sent, typename _Pointer,
3297 typename _Pred, typename _Distance>
3298 constexpr subrange<_Iter>
3299 __stable_partition_adaptive(_Iter __first, _Sent __last,
3300 _Pred __pred, _Distance __len,
3301 _Pointer __buffer,
3302 _Distance __buffer_size)
3303 {
3304 if (__len == 1)
3305 return {__first, ranges::next(__first, 1)};
3306
3307 if (__len <= __buffer_size)
3308 {
3309 _Iter __result1 = __first;
3310 _Pointer __result2 = __buffer;
3311
3312 // The precondition guarantees that !__pred(__first), so
3313 // move that element to the buffer before starting the loop.
3314 // This ensures that we only call __pred once per element.
3315 *__result2 = ranges::iter_move(__first);
3316 ++__result2;
3317 ++__first;
3318 for (; __first != __last; ++__first)
3319 if (__pred(*__first))
3320 {
3321 *__result1 = ranges::iter_move(__first);
3322 ++__result1;
3323 }
3324 else
3325 {
3326 *__result2 = ranges::iter_move(__first);
3327 ++__result2;
3328 }
3329
3330 ranges::move(__buffer, __result2, __result1);
3331 return {__result1, __first};
3332 }
3333
3334 _Iter __middle = __first;
3335 ranges::advance(__middle, __len / 2);
3336 _Iter __left_split
3337 = __detail::__stable_partition_adaptive(__first, __middle, __pred,
3338 __len / 2, __buffer,
3339 __buffer_size).begin();
3340
3341 // Advance past true-predicate values to satisfy this
3342 // function's preconditions.
3343 _Distance __right_len = __len - __len / 2;
3344 _Iter __right_split = __detail::__find_if_not_n(__middle, __right_len, __pred);
3345
3346 if (__right_len)
3347 __right_split
3348 = __detail::__stable_partition_adaptive(__right_split, __last, __pred,
3349 __right_len, __buffer, __buffer_size).begin();
3350
3351 return ranges::rotate(__left_split, __middle, __right_split);
3352 }
3353 } // namespace __detail
3354
3355 struct __stable_partition_fn
3356 {
3357 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3358 typename _Proj = identity,
3359 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3360 requires permutable<_Iter>
3361 _GLIBCXX26_CONSTEXPR
3362 subrange<_Iter>
3363 operator()(_Iter __first, _Sent __last,
3364 _Pred __pred, _Proj __proj = {}) const
3365 {
3366 __first = ranges::find_if_not(__first, __last, __pred, __proj);
3367
3368 if (__first == __last)
3369 return {__first, __first};
3370
3371 using _DistanceType = iter_difference_t<_Iter>;
3372 const _DistanceType __len = ranges::distance(__first, __last);
3373
3374 auto __pred_proj = __detail::__make_pred_proj(__pred, __proj);
3375
3376#if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
3377 if consteval {
3378 // Simulate a _Temporary_buffer of length 1:
3379 iter_value_t<_Iter> __buf = ranges::iter_move(__first);
3380 *__first = std::move(__buf);
3381 return __detail::__stable_partition_adaptive(__first, __last,
3382 __pred_proj,
3383 __len, &__buf,
3384 _DistanceType(1));
3385 }
3386#endif
3387
3388 _Temporary_buffer<_Iter, iter_value_t<_Iter>> __buf(__first, ptrdiff_t(__len));
3389 return __detail::__stable_partition_adaptive(__first, __last,
3390 __pred_proj,
3391 __len, __buf.begin(),
3392 _DistanceType(__buf.size()));
3393 }
3394
3395 template<bidirectional_range _Range, typename _Proj = identity,
3396 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3397 _Pred>
3398 requires permutable<iterator_t<_Range>>
3399 _GLIBCXX26_CONSTEXPR
3400 borrowed_subrange_t<_Range>
3401 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3402 {
3403 return (*this)(ranges::begin(__r), ranges::end(__r),
3404 std::move(__pred), std::move(__proj));
3405 }
3406 };
3407
3408 inline constexpr __stable_partition_fn stable_partition{};
3409#endif
3410
3411 template<typename _Iter, typename _Out1, typename _Out2>
3412 struct in_out_out_result
3413 {
3414 [[no_unique_address]] _Iter in;
3415 [[no_unique_address]] _Out1 out1;
3416 [[no_unique_address]] _Out2 out2;
3417
3418 template<typename _IIter, typename _OOut1, typename _OOut2>
3419 requires convertible_to<const _Iter&, _IIter>
3420 && convertible_to<const _Out1&, _OOut1>
3421 && convertible_to<const _Out2&, _OOut2>
3422 constexpr
3423 operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
3424 { return {in, out1, out2}; }
3425
3426 template<typename _IIter, typename _OOut1, typename _OOut2>
3427 requires convertible_to<_Iter, _IIter>
3428 && convertible_to<_Out1, _OOut1>
3429 && convertible_to<_Out2, _OOut2>
3430 constexpr
3431 operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
3432 { return {std::move(in), std::move(out1), std::move(out2)}; }
3433 };
3434
3435 template<typename _Iter, typename _Out1, typename _Out2>
3436 using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
3437
3438 struct __partition_copy_fn
3439 {
3440 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
3441 weakly_incrementable _Out1, weakly_incrementable _Out2,
3442 typename _Proj = identity,
3443 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3444 requires indirectly_copyable<_Iter, _Out1>
3445 && indirectly_copyable<_Iter, _Out2>
3446 constexpr partition_copy_result<_Iter, _Out1, _Out2>
3447 operator()(_Iter __first, _Sent __last,
3448 _Out1 __out_true, _Out2 __out_false,
3449 _Pred __pred, _Proj __proj = {}) const
3450 {
3451 for (; __first != __last; ++__first)
3452 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
3453 {
3454 *__out_true = *__first;
3455 ++__out_true;
3456 }
3457 else
3458 {
3459 *__out_false = *__first;
3460 ++__out_false;
3461 }
3462
3463 return {std::move(__first),
3464 std::move(__out_true), std::move(__out_false)};
3465 }
3466
3467 template<input_range _Range, weakly_incrementable _Out1,
3468 weakly_incrementable _Out2,
3469 typename _Proj = identity,
3470 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3471 _Pred>
3472 requires indirectly_copyable<iterator_t<_Range>, _Out1>
3473 && indirectly_copyable<iterator_t<_Range>, _Out2>
3474 constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
3475 operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
3476 _Pred __pred, _Proj __proj = {}) const
3477 {
3478 return (*this)(ranges::begin(__r), ranges::end(__r),
3479 std::move(__out_true), std::move(__out_false),
3480 std::move(__pred), std::move(__proj));
3481 }
3482 };
3483
3484 inline constexpr __partition_copy_fn partition_copy{};
3485
3486 struct __partition_point_fn
3487 {
3488 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3489 typename _Proj = identity,
3490 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
3491 [[nodiscard]] constexpr _Iter
3492 operator()(_Iter __first, _Sent __last,
3493 _Pred __pred, _Proj __proj = {}) const
3494 {
3495 auto __len = ranges::distance(__first, __last);
3496
3497 while (__len > 0)
3498 {
3499 auto __half = __len / 2;
3500 auto __middle = __first;
3501 ranges::advance(__middle, __half);
3502 if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
3503 {
3504 __first = __middle;
3505 ++__first;
3506 __len = __len - __half - 1;
3507 }
3508 else
3509 __len = __half;
3510 }
3511 return __first;
3512 }
3513
3514 template<forward_range _Range, typename _Proj = identity,
3515 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
3516 _Pred>
3517 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
3518 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
3519 {
3520 return (*this)(ranges::begin(__r), ranges::end(__r),
3521 std::move(__pred), std::move(__proj));
3522 }
3523 };
3524
3525 inline constexpr __partition_point_fn partition_point{};
3526
3527 template<typename _Iter1, typename _Iter2, typename _Out>
3528 using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
3529
3530 struct __merge_fn
3531 {
3532 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3533 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3534 weakly_incrementable _Out, typename _Comp = ranges::less,
3535 typename _Proj1 = identity, typename _Proj2 = identity>
3536 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
3537 constexpr merge_result<_Iter1, _Iter2, _Out>
3538 operator()(_Iter1 __first1, _Sent1 __last1,
3539 _Iter2 __first2, _Sent2 __last2, _Out __result,
3540 _Comp __comp = {},
3541 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3542 {
3543 while (__first1 != __last1 && __first2 != __last2)
3544 {
3545 if (std::__invoke(__comp,
3546 std::__invoke(__proj2, *__first2),
3547 std::__invoke(__proj1, *__first1)))
3548 {
3549 *__result = *__first2;
3550 ++__first2;
3551 }
3552 else
3553 {
3554 *__result = *__first1;
3555 ++__first1;
3556 }
3557 ++__result;
3558 }
3559 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
3560 std::move(__result));
3561 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
3562 std::move(__copy1.out));
3563 return { std::move(__copy1.in), std::move(__copy2.in),
3564 std::move(__copy2.out) };
3565 }
3566
3567 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
3568 typename _Comp = ranges::less,
3569 typename _Proj1 = identity, typename _Proj2 = identity>
3570 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
3571 _Comp, _Proj1, _Proj2>
3572 constexpr merge_result<borrowed_iterator_t<_Range1>,
3573 borrowed_iterator_t<_Range2>,
3574 _Out>
3575 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
3576 _Comp __comp = {},
3577 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3578 {
3579 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3580 ranges::begin(__r2), ranges::end(__r2),
3581 std::move(__result), std::move(__comp),
3582 std::move(__proj1), std::move(__proj2));
3583 }
3584 };
3585
3586 inline constexpr __merge_fn merge{};
3587
3588 namespace __detail
3589 {
3590 template<typename _Iter1, typename _Iter2, typename _Out, typename _Comp>
3591 void
3592 __move_merge_adaptive(_Iter1 __first1, _Iter1 __last1,
3593 _Iter2 __first2, _Iter2 __last2,
3594 _Out __result, _Comp __comp)
3595 {
3596 while (__first1 != __last1 && __first2 != __last2)
3597 {
3598 if (__comp(*__first2, *__first1))
3599 {
3600 *__result = ranges::iter_move(__first2);
3601 ++__first2;
3602 }
3603 else
3604 {
3605 *__result = ranges::iter_move(__first1);
3606 ++__first1;
3607 }
3608 ++__result;
3609 }
3610 if (__first1 != __last1)
3611 ranges::move(__first1, __last1, __result);
3612 }
3613
3614 template<typename _Iter1, typename _Iter2, typename _Iter3, typename _Comp>
3615 void
3616 __move_merge_adaptive_backward(_Iter1 __first1, _Iter1 __last1,
3617 _Iter2 __first2, _Iter2 __last2,
3618 _Iter3 __result, _Comp __comp)
3619 {
3620 if (__first1 == __last1)
3621 {
3622 ranges::move_backward(__first2, __last2, __result);
3623 return;
3624 }
3625 else if (__first2 == __last2)
3626 return;
3627
3628 --__last1;
3629 --__last2;
3630 while (true)
3631 {
3632 if (__comp(*__last2, *__last1))
3633 {
3634 *--__result = ranges::iter_move(__last1);
3635 if (__first1 == __last1)
3636 {
3637 ranges::move_backward(__first2, ++__last2, __result);
3638 return;
3639 }
3640 --__last1;
3641 }
3642 else
3643 {
3644 *--__result = ranges::iter_move(__last2);
3645 if (__first2 == __last2)
3646 return;
3647 --__last2;
3648 }
3649 }
3650 }
3651
3652 template<typename _Iter1, typename _Iter2>
3653 _Iter1
3654 __rotate_adaptive(_Iter1 __first, _Iter1 __middle, _Iter1 __last,
3655 iter_difference_t<_Iter1> __len1,
3656 iter_difference_t<_Iter1> __len2,
3657 _Iter2 __buffer,
3658 iter_difference_t<_Iter1> __buffer_size)
3659 {
3660 _Iter2 __buffer_end;
3661 if (__len1 > __len2 && __len2 <= __buffer_size)
3662 {
3663 if (__len2)
3664 {
3665 __buffer_end = ranges::move(__middle, __last, __buffer).out;
3666 ranges::move_backward(__first, __middle, __last);
3667 return ranges::move(__buffer, __buffer_end, __first).out;
3668 }
3669 else
3670 return __first;
3671 }
3672 else if (__len1 <= __buffer_size)
3673 {
3674 if (__len1)
3675 {
3676 __buffer_end = ranges::move(__first, __middle, __buffer).out;
3677 ranges::move(__middle, __last, __first);
3678 return ranges::move_backward(__buffer, __buffer_end, __last).out;
3679 }
3680 else
3681 return __last;
3682 }
3683 else
3684 return ranges::rotate(__first, __middle, __last).begin();
3685 }
3686
3687 template<typename _Iter, typename _Pointer, typename _Comp>
3688 void
3689 __merge_adaptive(_Iter __first, _Iter __middle, _Iter __last,
3690 iter_difference_t<_Iter> __len1,
3691 iter_difference_t<_Iter> __len2,
3692 _Pointer __buffer, _Comp __comp)
3693 {
3694 if (__len1 <= __len2)
3695 {
3696 _Pointer __buffer_end = ranges::move(__first, __middle, __buffer).out;
3697 __detail::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
3698 __first, __comp);
3699 }
3700 else
3701 {
3702 _Pointer __buffer_end = ranges::move(__middle, __last, __buffer).out;
3703 __detail::__move_merge_adaptive_backward(__first, __middle, __buffer,
3704 __buffer_end, __last, __comp);
3705 }
3706 }
3707
3708 template<typename _Iter, typename _Distance, typename _Pointer, typename _Comp>
3709 void
3710 __merge_adaptive_resize(_Iter __first, _Iter __middle, _Iter __last,
3711 _Distance __len1, _Distance __len2,
3712 _Pointer __buffer, _Distance __buffer_size,
3713 _Comp __comp)
3714 {
3715 if (__len1 <= __buffer_size || __len2 <= __buffer_size)
3716 __detail::__merge_adaptive(__first, __middle, __last,
3717 __len1, __len2, __buffer, __comp);
3718 else
3719 {
3720 _Iter __first_cut = __first;
3721 _Iter __second_cut = __middle;
3722 _Distance __len11 = 0;
3723 _Distance __len22 = 0;
3724 if (__len1 > __len2)
3725 {
3726 __len11 = __len1 / 2;
3727 ranges::advance(__first_cut, __len11);
3728 __second_cut = ranges::lower_bound(__middle, __last, *__first_cut,
3729 __comp);
3730 __len22 = ranges::distance(__middle, __second_cut);
3731 }
3732 else
3733 {
3734 __len22 = __len2 / 2;
3735 ranges::advance(__second_cut, __len22);
3736 __first_cut = ranges::upper_bound(__first, __middle, *__second_cut,
3737 __comp);
3738 __len11 = ranges::distance(__first, __first_cut);
3739 }
3740
3741 _Iter __new_middle
3742 = __detail::__rotate_adaptive(__first_cut, __middle, __second_cut,
3743 _Distance(__len1 - __len11), __len22,
3744 __buffer, __buffer_size);
3745 __detail::__merge_adaptive_resize(__first, __first_cut, __new_middle,
3746 __len11, __len22,
3747 __buffer, __buffer_size, __comp);
3748 __detail::__merge_adaptive_resize(__new_middle, __second_cut, __last,
3749 _Distance(__len1 - __len11),
3750 _Distance(__len2 - __len22),
3751 __buffer, __buffer_size, __comp);
3752 }
3753 }
3754
3755 template<typename _Iter, typename _Distance, typename _Comp>
3756 constexpr void
3757 __merge_without_buffer(_Iter __first, _Iter __middle, _Iter __last,
3758 _Distance __len1, _Distance __len2, _Comp __comp)
3759 {
3760 if (__len1 == 0 || __len2 == 0)
3761 return;
3762
3763 if (__len1 + __len2 == 2)
3764 {
3765 if (__comp(*__middle, *__first))
3766 ranges::iter_swap(__first, __middle);
3767 return;
3768 }
3769
3770 _Iter __first_cut = __first;
3771 _Iter __second_cut = __middle;
3772 _Distance __len11 = 0;
3773 _Distance __len22 = 0;
3774 if (__len1 > __len2)
3775 {
3776 __len11 = __len1 / 2;
3777 ranges::advance(__first_cut, __len11);
3778 __second_cut = ranges::lower_bound(__middle, __last, *__first_cut, __comp);
3779 __len22 = ranges::distance(__middle, __second_cut);
3780 }
3781 else
3782 {
3783 __len22 = __len2 / 2;
3784 ranges::advance(__second_cut, __len22);
3785 __first_cut = ranges::upper_bound(__first, __middle, *__second_cut, __comp);
3786 __len11 = ranges::distance(__first, __first_cut);
3787 }
3788
3789 _Iter __new_middle = ranges::rotate(__first_cut, __middle, __second_cut).begin();
3790 __detail::__merge_without_buffer(__first, __first_cut, __new_middle,
3791 __len11, __len22, __comp);
3792 __detail::__merge_without_buffer(__new_middle, __second_cut, __last,
3793 __len1 - __len11, __len2 - __len22, __comp);
3794 }
3795 } // namespace __detail
3796
3797 struct __inplace_merge_fn
3798 {
3799 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3800 typename _Comp = ranges::less,
3801 typename _Proj = identity>
3802 requires sortable<_Iter, _Comp, _Proj>
3803 _GLIBCXX26_CONSTEXPR
3804 _Iter
3805 operator()(_Iter __first, _Iter __middle, _Sent __last,
3806 _Comp __comp = {}, _Proj __proj = {}) const
3807 {
3808 if constexpr (!same_as<_Iter, _Sent>)
3809 return (*this)(__first, __middle, ranges::next(__middle, __last),
3810 std::move(__comp), std::move(__proj));
3811 else
3812 {
3813 using _DistanceType = iter_difference_t<_Iter>;
3814
3815 if (__first == __middle || __middle == __last)
3816 return __last;
3817
3818 const _DistanceType __len1 = ranges::distance(__first, __middle);
3819 const _DistanceType __len2 = ranges::distance(__middle, __last);
3820
3821 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3822
3823#if _GLIBCXX_HOSTED
3824# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
3825 if consteval {
3826 __detail::__merge_without_buffer(__first, __middle, __last,
3827 __len1, __len2, __comp_proj);
3828 return __last;
3829 }
3830# endif
3831 using _TmpBuf = _Temporary_buffer<_Iter, iter_value_t<_Iter>>;
3832 // __merge_adaptive will use a buffer for the smaller of
3833 // [first,middle) and [middle,last).
3834 _TmpBuf __buf(__first, ptrdiff_t(ranges::min(__len1, __len2)));
3835
3836 if (__buf.size() == __buf._M_requested_size()) [[likely]]
3837 __detail::__merge_adaptive
3838 (__first, __middle, __last, __len1, __len2, __buf.begin(), __comp_proj);
3839 else if (__buf.begin() == 0) [[unlikely]]
3840 __detail::__merge_without_buffer
3841 (__first, __middle, __last, __len1, __len2, __comp_proj);
3842 else
3843 __detail::__merge_adaptive_resize
3844 (__first, __middle, __last, __len1, __len2, __buf.begin(),
3845 _DistanceType(__buf.size()), __comp_proj);
3846#else
3847 __detail::__merge_without_buffer
3848 (__first, __middle, __last, __len1, __len2, __comp_proj);
3849#endif
3850 return __last;
3851 }
3852 }
3853
3854 template<bidirectional_range _Range,
3855 typename _Comp = ranges::less, typename _Proj = identity>
3856 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3857 _GLIBCXX26_CONSTEXPR
3858 borrowed_iterator_t<_Range>
3859 operator()(_Range&& __r, iterator_t<_Range> __middle,
3860 _Comp __comp = {}, _Proj __proj = {}) const
3861 {
3862 return (*this)(ranges::begin(__r), std::move(__middle),
3863 ranges::end(__r),
3864 std::move(__comp), std::move(__proj));
3865 }
3866 };
3867
3868 inline constexpr __inplace_merge_fn inplace_merge{};
3869
3870 struct __includes_fn
3871 {
3872 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3873 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3874 typename _Proj1 = identity, typename _Proj2 = identity,
3875 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
3876 projected<_Iter2, _Proj2>>
3877 _Comp = ranges::less>
3878 [[nodiscard]] constexpr bool
3879 operator()(_Iter1 __first1, _Sent1 __last1,
3880 _Iter2 __first2, _Sent2 __last2,
3881 _Comp __comp = {},
3882 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3883 {
3884 while (__first1 != __last1 && __first2 != __last2)
3885 if (std::__invoke(__comp,
3886 std::__invoke(__proj2, *__first2),
3887 std::__invoke(__proj1, *__first1)))
3888 return false;
3889 else if (std::__invoke(__comp,
3890 std::__invoke(__proj1, *__first1),
3891 std::__invoke(__proj2, *__first2)))
3892 ++__first1;
3893 else
3894 {
3895 ++__first1;
3896 ++__first2;
3897 }
3898
3899 return __first2 == __last2;
3900 }
3901
3902 template<input_range _Range1, input_range _Range2,
3903 typename _Proj1 = identity, typename _Proj2 = identity,
3904 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
3905 projected<iterator_t<_Range2>, _Proj2>>
3906 _Comp = ranges::less>
3907 [[nodiscard]] constexpr bool
3908 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
3909 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3910 {
3911 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3912 ranges::begin(__r2), ranges::end(__r2),
3913 std::move(__comp),
3914 std::move(__proj1), std::move(__proj2));
3915 }
3916 };
3917
3918 inline constexpr __includes_fn includes{};
3919
3920 template<typename _Iter1, typename _Iter2, typename _Out>
3921 using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
3922
3923 struct __set_union_fn
3924 {
3925 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3926 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3927 weakly_incrementable _Out, typename _Comp = ranges::less,
3928 typename _Proj1 = identity, typename _Proj2 = identity>
3929 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
3930 constexpr set_union_result<_Iter1, _Iter2, _Out>
3931 operator()(_Iter1 __first1, _Sent1 __last1,
3932 _Iter2 __first2, _Sent2 __last2,
3933 _Out __result, _Comp __comp = {},
3934 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3935 {
3936 while (__first1 != __last1 && __first2 != __last2)
3937 {
3938 if (std::__invoke(__comp,
3939 std::__invoke(__proj1, *__first1),
3940 std::__invoke(__proj2, *__first2)))
3941 {
3942 *__result = *__first1;
3943 ++__first1;
3944 }
3945 else if (std::__invoke(__comp,
3946 std::__invoke(__proj2, *__first2),
3947 std::__invoke(__proj1, *__first1)))
3948 {
3949 *__result = *__first2;
3950 ++__first2;
3951 }
3952 else
3953 {
3954 *__result = *__first1;
3955 ++__first1;
3956 ++__first2;
3957 }
3958 ++__result;
3959 }
3960 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
3961 std::move(__result));
3962 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
3963 std::move(__copy1.out));
3964 return {std::move(__copy1.in), std::move(__copy2.in),
3965 std::move(__copy2.out)};
3966 }
3967
3968 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
3969 typename _Comp = ranges::less,
3970 typename _Proj1 = identity, typename _Proj2 = identity>
3971 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
3972 _Comp, _Proj1, _Proj2>
3973 constexpr set_union_result<borrowed_iterator_t<_Range1>,
3974 borrowed_iterator_t<_Range2>, _Out>
3975 operator()(_Range1&& __r1, _Range2&& __r2,
3976 _Out __result, _Comp __comp = {},
3977 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
3978 {
3979 return (*this)(ranges::begin(__r1), ranges::end(__r1),
3980 ranges::begin(__r2), ranges::end(__r2),
3981 std::move(__result), std::move(__comp),
3982 std::move(__proj1), std::move(__proj2));
3983 }
3984 };
3985
3986 inline constexpr __set_union_fn set_union{};
3987
3988 template<typename _Iter1, typename _Iter2, typename _Out>
3989 using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
3990
3991 struct __set_intersection_fn
3992 {
3993 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3994 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3995 weakly_incrementable _Out, typename _Comp = ranges::less,
3996 typename _Proj1 = identity, typename _Proj2 = identity>
3997 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
3998 constexpr set_intersection_result<_Iter1, _Iter2, _Out>
3999 operator()(_Iter1 __first1, _Sent1 __last1,
4000 _Iter2 __first2, _Sent2 __last2, _Out __result,
4001 _Comp __comp = {},
4002 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4003 {
4004 while (__first1 != __last1 && __first2 != __last2)
4005 if (std::__invoke(__comp,
4006 std::__invoke(__proj1, *__first1),
4007 std::__invoke(__proj2, *__first2)))
4008 ++__first1;
4009 else if (std::__invoke(__comp,
4010 std::__invoke(__proj2, *__first2),
4011 std::__invoke(__proj1, *__first1)))
4012 ++__first2;
4013 else
4014 {
4015 *__result = *__first1;
4016 ++__first1;
4017 ++__first2;
4018 ++__result;
4019 }
4020 // TODO: Eliminating these variables triggers an ICE.
4021 auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
4022 auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
4023 return {std::move(__last1i), std::move(__last2i), std::move(__result)};
4024 }
4025
4026 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4027 typename _Comp = ranges::less,
4028 typename _Proj1 = identity, typename _Proj2 = identity>
4029 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4030 _Comp, _Proj1, _Proj2>
4031 constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
4032 borrowed_iterator_t<_Range2>, _Out>
4033 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4034 _Comp __comp = {},
4035 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4036 {
4037 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4038 ranges::begin(__r2), ranges::end(__r2),
4039 std::move(__result), std::move(__comp),
4040 std::move(__proj1), std::move(__proj2));
4041 }
4042 };
4043
4044 inline constexpr __set_intersection_fn set_intersection{};
4045
4046 template<typename _Iter, typename _Out>
4047 using set_difference_result = in_out_result<_Iter, _Out>;
4048
4049 struct __set_difference_fn
4050 {
4051 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4052 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4053 weakly_incrementable _Out, typename _Comp = ranges::less,
4054 typename _Proj1 = identity, typename _Proj2 = identity>
4055 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
4056 constexpr set_difference_result<_Iter1, _Out>
4057 operator()(_Iter1 __first1, _Sent1 __last1,
4058 _Iter2 __first2, _Sent2 __last2, _Out __result,
4059 _Comp __comp = {},
4060 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4061 {
4062 while (__first1 != __last1 && __first2 != __last2)
4063 if (std::__invoke(__comp,
4064 std::__invoke(__proj1, *__first1),
4065 std::__invoke(__proj2, *__first2)))
4066 {
4067 *__result = *__first1;
4068 ++__first1;
4069 ++__result;
4070 }
4071 else if (std::__invoke(__comp,
4072 std::__invoke(__proj2, *__first2),
4073 std::__invoke(__proj1, *__first1)))
4074 ++__first2;
4075 else
4076 {
4077 ++__first1;
4078 ++__first2;
4079 }
4080 return ranges::copy(std::move(__first1), std::move(__last1),
4081 std::move(__result));
4082 }
4083
4084 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4085 typename _Comp = ranges::less,
4086 typename _Proj1 = identity, typename _Proj2 = identity>
4087 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4088 _Comp, _Proj1, _Proj2>
4089 constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
4090 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4091 _Comp __comp = {},
4092 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4093 {
4094 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4095 ranges::begin(__r2), ranges::end(__r2),
4096 std::move(__result), std::move(__comp),
4097 std::move(__proj1), std::move(__proj2));
4098 }
4099 };
4100
4101 inline constexpr __set_difference_fn set_difference{};
4102
4103 template<typename _Iter1, typename _Iter2, typename _Out>
4104 using set_symmetric_difference_result
4105 = in_in_out_result<_Iter1, _Iter2, _Out>;
4106
4107 struct __set_symmetric_difference_fn
4108 {
4109 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4110 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4111 weakly_incrementable _Out, typename _Comp = ranges::less,
4112 typename _Proj1 = identity, typename _Proj2 = identity>
4113 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
4114 constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
4115 operator()(_Iter1 __first1, _Sent1 __last1,
4116 _Iter2 __first2, _Sent2 __last2,
4117 _Out __result, _Comp __comp = {},
4118 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4119 {
4120 while (__first1 != __last1 && __first2 != __last2)
4121 if (std::__invoke(__comp,
4122 std::__invoke(__proj1, *__first1),
4123 std::__invoke(__proj2, *__first2)))
4124 {
4125 *__result = *__first1;
4126 ++__first1;
4127 ++__result;
4128 }
4129 else if (std::__invoke(__comp,
4130 std::__invoke(__proj2, *__first2),
4131 std::__invoke(__proj1, *__first1)))
4132 {
4133 *__result = *__first2;
4134 ++__first2;
4135 ++__result;
4136 }
4137 else
4138 {
4139 ++__first1;
4140 ++__first2;
4141 }
4142 auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
4143 std::move(__result));
4144 auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
4145 std::move(__copy1.out));
4146 return {std::move(__copy1.in), std::move(__copy2.in),
4147 std::move(__copy2.out)};
4148 }
4149
4150 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
4151 typename _Comp = ranges::less,
4152 typename _Proj1 = identity, typename _Proj2 = identity>
4153 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
4154 _Comp, _Proj1, _Proj2>
4155 constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
4156 borrowed_iterator_t<_Range2>,
4157 _Out>
4158 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
4159 _Comp __comp = {},
4160 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4161 {
4162 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4163 ranges::begin(__r2), ranges::end(__r2),
4164 std::move(__result), std::move(__comp),
4165 std::move(__proj1), std::move(__proj2));
4166 }
4167 };
4168
4169 inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
4170
4171 // min is defined in <bits/ranges_util.h>.
4172
4173 struct __max_fn
4174 {
4175 template<typename _Tp, typename _Proj = identity,
4176 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4177 _Comp = ranges::less>
4178 [[nodiscard]] constexpr const _Tp&
4179 operator()(const _Tp& __a, const _Tp& __b,
4180 _Comp __comp = {}, _Proj __proj = {}) const
4181 {
4182 if (std::__invoke(__comp,
4183 std::__invoke(__proj, __a),
4184 std::__invoke(__proj, __b)))
4185 return __b;
4186 else
4187 return __a;
4188 }
4189
4190 template<input_range _Range, typename _Proj = identity,
4191 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4192 _Comp = ranges::less>
4193 requires indirectly_copyable_storable<iterator_t<_Range>,
4194 range_value_t<_Range>*>
4195 [[nodiscard]] constexpr range_value_t<_Range>
4196 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4197 {
4198 auto __first = ranges::begin(__r);
4199 auto __last = ranges::end(__r);
4200 __glibcxx_assert(__first != __last);
4201 auto __result = *__first;
4202 while (++__first != __last)
4203 {
4204 auto&& __tmp = *__first;
4205 if (std::__invoke(__comp,
4206 std::__invoke(__proj, __result),
4207 std::__invoke(__proj, __tmp)))
4208 __result = std::forward<decltype(__tmp)>(__tmp);
4209 }
4210 return __result;
4211 }
4212
4213 template<copyable _Tp, typename _Proj = identity,
4214 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4215 _Comp = ranges::less>
4216 [[nodiscard]] constexpr _Tp
4217 operator()(initializer_list<_Tp> __r,
4218 _Comp __comp = {}, _Proj __proj = {}) const
4219 {
4220 return (*this)(ranges::subrange(__r),
4221 std::move(__comp), std::move(__proj));
4222 }
4223 };
4224
4225 inline constexpr __max_fn max{};
4226
4227 struct __clamp_fn
4228 {
4229 template<typename _Tp, typename _Proj = identity,
4230 indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
4231 = ranges::less>
4232 [[nodiscard]] constexpr const _Tp&
4233 operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
4234 _Comp __comp = {}, _Proj __proj = {}) const
4235 {
4236 __glibcxx_assert(!(std::__invoke(__comp,
4237 std::__invoke(__proj, __hi),
4238 std::__invoke(__proj, __lo))));
4239 auto&& __proj_val = std::__invoke(__proj, __val);
4240 if (std::__invoke(__comp,
4241 std::forward<decltype(__proj_val)>(__proj_val),
4242 std::__invoke(__proj, __lo)))
4243 return __lo;
4244 else if (std::__invoke(__comp,
4245 std::__invoke(__proj, __hi),
4246 std::forward<decltype(__proj_val)>(__proj_val)))
4247 return __hi;
4248 else
4249 return __val;
4250 }
4251 };
4252
4253 inline constexpr __clamp_fn clamp{};
4254
4255 template<typename _Tp>
4256 struct min_max_result
4257 {
4258 [[no_unique_address]] _Tp min;
4259 [[no_unique_address]] _Tp max;
4260
4261 template<typename _Tp2>
4262 requires convertible_to<const _Tp&, _Tp2>
4263 constexpr
4264 operator min_max_result<_Tp2>() const &
4265 { return {min, max}; }
4266
4267 template<typename _Tp2>
4268 requires convertible_to<_Tp, _Tp2>
4269 constexpr
4270 operator min_max_result<_Tp2>() &&
4271 { return {std::move(min), std::move(max)}; }
4272 };
4273
4274 template<typename _Tp>
4275 using minmax_result = min_max_result<_Tp>;
4276
4277 struct __minmax_fn
4278 {
4279 template<typename _Tp, typename _Proj = identity,
4280 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4281 _Comp = ranges::less>
4282 [[nodiscard]] constexpr minmax_result<const _Tp&>
4283 operator()(const _Tp& __a, const _Tp& __b,
4284 _Comp __comp = {}, _Proj __proj = {}) const
4285 {
4286 if (std::__invoke(__comp,
4287 std::__invoke(__proj, __b),
4288 std::__invoke(__proj, __a)))
4289 return {__b, __a};
4290 else
4291 return {__a, __b};
4292 }
4293
4294 template<input_range _Range, typename _Proj = identity,
4295 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4296 _Comp = ranges::less>
4297 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
4298 [[nodiscard]] constexpr minmax_result<range_value_t<_Range>>
4299 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4300 {
4301 auto __first = ranges::begin(__r);
4302 auto __last = ranges::end(__r);
4303 __glibcxx_assert(__first != __last);
4304 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
4305 minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
4306 if (++__first == __last)
4307 return __result;
4308 else
4309 {
4310 // At this point __result.min == __result.max, so a single
4311 // comparison with the next element suffices.
4312 auto&& __val = *__first;
4313 if (__comp_proj(__val, __result.min))
4314 __result.min = std::forward<decltype(__val)>(__val);
4315 else
4316 __result.max = std::forward<decltype(__val)>(__val);
4317 }
4318 while (++__first != __last)
4319 {
4320 // Now process two elements at a time so that we perform at most
4321 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
4322 // iterations of this loop performs three comparisons).
4323 range_value_t<_Range> __val1 = *__first;
4324 if (++__first == __last)
4325 {
4326 // N is odd; in this final iteration, we perform at most two
4327 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
4328 // which is not more than 3*N/2, as required.
4329 if (__comp_proj(__val1, __result.min))
4330 __result.min = std::move(__val1);
4331 else if (!__comp_proj(__val1, __result.max))
4332 __result.max = std::move(__val1);
4333 break;
4334 }
4335 auto&& __val2 = *__first;
4336 if (!__comp_proj(__val2, __val1))
4337 {
4338 if (__comp_proj(__val1, __result.min))
4339 __result.min = std::move(__val1);
4340 if (!__comp_proj(__val2, __result.max))
4341 __result.max = std::forward<decltype(__val2)>(__val2);
4342 }
4343 else
4344 {
4345 if (__comp_proj(__val2, __result.min))
4346 __result.min = std::forward<decltype(__val2)>(__val2);
4347 if (!__comp_proj(__val1, __result.max))
4348 __result.max = std::move(__val1);
4349 }
4350 }
4351 return __result;
4352 }
4353
4354 template<copyable _Tp, typename _Proj = identity,
4355 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
4356 _Comp = ranges::less>
4357 [[nodiscard]] constexpr minmax_result<_Tp>
4358 operator()(initializer_list<_Tp> __r,
4359 _Comp __comp = {}, _Proj __proj = {}) const
4360 {
4361 return (*this)(ranges::subrange(__r),
4362 std::move(__comp), std::move(__proj));
4363 }
4364 };
4365
4366 inline constexpr __minmax_fn minmax{};
4367
4368 struct __min_element_fn
4369 {
4370 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4371 typename _Proj = identity,
4372 indirect_strict_weak_order<projected<_Iter, _Proj>>
4373 _Comp = ranges::less>
4374 [[nodiscard]] constexpr _Iter
4375 operator()(_Iter __first, _Sent __last,
4376 _Comp __comp = {}, _Proj __proj = {}) const
4377 {
4378 if (__first == __last)
4379 return __first;
4380
4381 auto __i = __first;
4382 while (++__i != __last)
4383 {
4384 if (std::__invoke(__comp,
4385 std::__invoke(__proj, *__i),
4386 std::__invoke(__proj, *__first)))
4387 __first = __i;
4388 }
4389 return __first;
4390 }
4391
4392 template<forward_range _Range, typename _Proj = identity,
4393 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4394 _Comp = ranges::less>
4395 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
4396 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4397 {
4398 return (*this)(ranges::begin(__r), ranges::end(__r),
4399 std::move(__comp), std::move(__proj));
4400 }
4401 };
4402
4403 inline constexpr __min_element_fn min_element{};
4404
4405 struct __max_element_fn
4406 {
4407 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4408 typename _Proj = identity,
4409 indirect_strict_weak_order<projected<_Iter, _Proj>>
4410 _Comp = ranges::less>
4411 [[nodiscard]] constexpr _Iter
4412 operator()(_Iter __first, _Sent __last,
4413 _Comp __comp = {}, _Proj __proj = {}) const
4414 {
4415 if (__first == __last)
4416 return __first;
4417
4418 auto __i = __first;
4419 while (++__i != __last)
4420 {
4421 if (std::__invoke(__comp,
4422 std::__invoke(__proj, *__first),
4423 std::__invoke(__proj, *__i)))
4424 __first = __i;
4425 }
4426 return __first;
4427 }
4428
4429 template<forward_range _Range, typename _Proj = identity,
4430 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4431 _Comp = ranges::less>
4432 [[nodiscard]] constexpr borrowed_iterator_t<_Range>
4433 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4434 {
4435 return (*this)(ranges::begin(__r), ranges::end(__r),
4436 std::move(__comp), std::move(__proj));
4437 }
4438 };
4439
4440 inline constexpr __max_element_fn max_element{};
4441
4442 template<typename _Iter>
4443 using minmax_element_result = min_max_result<_Iter>;
4444
4445 struct __minmax_element_fn
4446 {
4447 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4448 typename _Proj = identity,
4449 indirect_strict_weak_order<projected<_Iter, _Proj>>
4450 _Comp = ranges::less>
4451 [[nodiscard]] constexpr minmax_element_result<_Iter>
4452 operator()(_Iter __first, _Sent __last,
4453 _Comp __comp = {}, _Proj __proj = {}) const
4454 {
4455 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
4456 minmax_element_result<_Iter> __result = {__first, __first};
4457 if (__first == __last || ++__first == __last)
4458 return __result;
4459 else
4460 {
4461 // At this point __result.min == __result.max, so a single
4462 // comparison with the next element suffices.
4463 if (__comp_proj(*__first, *__result.min))
4464 __result.min = __first;
4465 else
4466 __result.max = __first;
4467 }
4468 while (++__first != __last)
4469 {
4470 // Now process two elements at a time so that we perform at most
4471 // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
4472 // iterations of this loop performs three comparisons).
4473 auto __prev = __first;
4474 if (++__first == __last)
4475 {
4476 // N is odd; in this final iteration, we perform at most two
4477 // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
4478 // which is not more than 3*N/2, as required.
4479 if (__comp_proj(*__prev, *__result.min))
4480 __result.min = __prev;
4481 else if (!__comp_proj(*__prev, *__result.max))
4482 __result.max = __prev;
4483 break;
4484 }
4485 if (!__comp_proj(*__first, *__prev))
4486 {
4487 if (__comp_proj(*__prev, *__result.min))
4488 __result.min = __prev;
4489 if (!__comp_proj(*__first, *__result.max))
4490 __result.max = __first;
4491 }
4492 else
4493 {
4494 if (__comp_proj(*__first, *__result.min))
4495 __result.min = __first;
4496 if (!__comp_proj(*__prev, *__result.max))
4497 __result.max = __prev;
4498 }
4499 }
4500 return __result;
4501 }
4502
4503 template<forward_range _Range, typename _Proj = identity,
4504 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
4505 _Comp = ranges::less>
4506 [[nodiscard]] constexpr minmax_element_result<borrowed_iterator_t<_Range>>
4507 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4508 {
4509 return (*this)(ranges::begin(__r), ranges::end(__r),
4510 std::move(__comp), std::move(__proj));
4511 }
4512 };
4513
4514 inline constexpr __minmax_element_fn minmax_element{};
4515
4516 struct __lexicographical_compare_fn
4517 {
4518 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4519 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4520 typename _Proj1 = identity, typename _Proj2 = identity,
4521 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
4522 projected<_Iter2, _Proj2>>
4523 _Comp = ranges::less>
4524 [[nodiscard]] constexpr bool
4525 operator()(_Iter1 __first1, _Sent1 __last1,
4526 _Iter2 __first2, _Sent2 __last2,
4527 _Comp __comp = {},
4528 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4529 {
4530 if constexpr (__detail::__is_normal_iterator<_Iter1>
4531 && same_as<_Iter1, _Sent1>)
4532 return (*this)(__first1.base(), __last1.base(),
4533 std::move(__first2), std::move(__last2),
4534 std::move(__comp),
4535 std::move(__proj1), std::move(__proj2));
4536 else if constexpr (__detail::__is_normal_iterator<_Iter2>
4537 && same_as<_Iter2, _Sent2>)
4538 return (*this)(std::move(__first1), std::move(__last1),
4539 __first2.base(), __last2.base(),
4540 std::move(__comp),
4541 std::move(__proj1), std::move(__proj2));
4542 else
4543 {
4544 constexpr bool __sized_iters
4545 = (sized_sentinel_for<_Sent1, _Iter1>
4546 && sized_sentinel_for<_Sent2, _Iter2>);
4547 if constexpr (__sized_iters)
4548 {
4549 using _ValueType1 = iter_value_t<_Iter1>;
4550 using _ValueType2 = iter_value_t<_Iter2>;
4551 // This condition is consistent with the one in
4552 // __lexicographical_compare_aux in <bits/stl_algobase.h>.
4553 constexpr bool __use_memcmp
4554 = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
4555 && __ptr_to_nonvolatile<_Iter1>
4556 && __ptr_to_nonvolatile<_Iter2>
4557 && (is_same_v<_Comp, ranges::less>
4558 || is_same_v<_Comp, ranges::greater>)
4559 && is_same_v<_Proj1, identity>
4560 && is_same_v<_Proj2, identity>);
4561 if constexpr (__use_memcmp)
4562 {
4563 const auto __d1 = __last1 - __first1;
4564 const auto __d2 = __last2 - __first2;
4565
4566 if (const auto __len = std::min(__d1, __d2))
4567 {
4568 const auto __c
4569 = std::__memcmp(__first1, __first2, __len);
4570 if constexpr (is_same_v<_Comp, ranges::less>)
4571 {
4572 if (__c < 0)
4573 return true;
4574 if (__c > 0)
4575 return false;
4576 }
4577 else if constexpr (is_same_v<_Comp, ranges::greater>)
4578 {
4579 if (__c > 0)
4580 return true;
4581 if (__c < 0)
4582 return false;
4583 }
4584 }
4585 return __d1 < __d2;
4586 }
4587 }
4588
4589 for (; __first1 != __last1 && __first2 != __last2;
4590 ++__first1, (void) ++__first2)
4591 {
4592 if (std::__invoke(__comp,
4593 std::__invoke(__proj1, *__first1),
4594 std::__invoke(__proj2, *__first2)))
4595 return true;
4596 if (std::__invoke(__comp,
4597 std::__invoke(__proj2, *__first2),
4598 std::__invoke(__proj1, *__first1)))
4599 return false;
4600 }
4601 return __first1 == __last1 && __first2 != __last2;
4602 }
4603 }
4604
4605 template<input_range _Range1, input_range _Range2,
4606 typename _Proj1 = identity, typename _Proj2 = identity,
4607 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
4608 projected<iterator_t<_Range2>, _Proj2>>
4609 _Comp = ranges::less>
4610 [[nodiscard]] constexpr bool
4611 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
4612 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4613 {
4614 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4615 ranges::begin(__r2), ranges::end(__r2),
4616 std::move(__comp),
4617 std::move(__proj1), std::move(__proj2));
4618 }
4619
4620 private:
4621 template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
4622 static constexpr bool __ptr_to_nonvolatile
4623 = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
4624 };
4625
4626 inline constexpr __lexicographical_compare_fn lexicographical_compare;
4627
4628 template<typename _Iter>
4629 struct in_found_result
4630 {
4631 [[no_unique_address]] _Iter in;
4632 bool found;
4633
4634 template<typename _Iter2>
4635 requires convertible_to<const _Iter&, _Iter2>
4636 constexpr
4637 operator in_found_result<_Iter2>() const &
4638 { return {in, found}; }
4639
4640 template<typename _Iter2>
4641 requires convertible_to<_Iter, _Iter2>
4642 constexpr
4643 operator in_found_result<_Iter2>() &&
4644 { return {std::move(in), found}; }
4645 };
4646
4647 template<typename _Iter>
4648 using next_permutation_result = in_found_result<_Iter>;
4649
4650 struct __next_permutation_fn
4651 {
4652 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
4653 typename _Comp = ranges::less, typename _Proj = identity>
4654 requires sortable<_Iter, _Comp, _Proj>
4655 constexpr next_permutation_result<_Iter>
4656 operator()(_Iter __first, _Sent __last,
4657 _Comp __comp = {}, _Proj __proj = {}) const
4658 {
4659 if (__first == __last)
4660 return {std::move(__first), false};
4661
4662 auto __i = __first;
4663 ++__i;
4664 if (__i == __last)
4665 return {std::move(__i), false};
4666
4667 auto __lasti = ranges::next(__first, __last);
4668 __i = __lasti;
4669 --__i;
4670
4671 for (;;)
4672 {
4673 auto __ii = __i;
4674 --__i;
4675 if (std::__invoke(__comp,
4676 std::__invoke(__proj, *__i),
4677 std::__invoke(__proj, *__ii)))
4678 {
4679 auto __j = __lasti;
4680 while (!(bool)std::__invoke(__comp,
4681 std::__invoke(__proj, *__i),
4682 std::__invoke(__proj, *--__j)))
4683 ;
4684 ranges::iter_swap(__i, __j);
4685 ranges::reverse(__ii, __last);
4686 return {std::move(__lasti), true};
4687 }
4688 if (__i == __first)
4689 {
4690 ranges::reverse(__first, __last);
4691 return {std::move(__lasti), false};
4692 }
4693 }
4694 }
4695
4696 template<bidirectional_range _Range, typename _Comp = ranges::less,
4697 typename _Proj = identity>
4698 requires sortable<iterator_t<_Range>, _Comp, _Proj>
4699 constexpr next_permutation_result<borrowed_iterator_t<_Range>>
4700 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4701 {
4702 return (*this)(ranges::begin(__r), ranges::end(__r),
4703 std::move(__comp), std::move(__proj));
4704 }
4705 };
4706
4707 inline constexpr __next_permutation_fn next_permutation{};
4708
4709 template<typename _Iter>
4710 using prev_permutation_result = in_found_result<_Iter>;
4711
4712 struct __prev_permutation_fn
4713 {
4714 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
4715 typename _Comp = ranges::less, typename _Proj = identity>
4716 requires sortable<_Iter, _Comp, _Proj>
4717 constexpr prev_permutation_result<_Iter>
4718 operator()(_Iter __first, _Sent __last,
4719 _Comp __comp = {}, _Proj __proj = {}) const
4720 {
4721 if (__first == __last)
4722 return {std::move(__first), false};
4723
4724 auto __i = __first;
4725 ++__i;
4726 if (__i == __last)
4727 return {std::move(__i), false};
4728
4729 auto __lasti = ranges::next(__first, __last);
4730 __i = __lasti;
4731 --__i;
4732
4733 for (;;)
4734 {
4735 auto __ii = __i;
4736 --__i;
4737 if (std::__invoke(__comp,
4738 std::__invoke(__proj, *__ii),
4739 std::__invoke(__proj, *__i)))
4740 {
4741 auto __j = __lasti;
4742 while (!(bool)std::__invoke(__comp,
4743 std::__invoke(__proj, *--__j),
4744 std::__invoke(__proj, *__i)))
4745 ;
4746 ranges::iter_swap(__i, __j);
4747 ranges::reverse(__ii, __last);
4748 return {std::move(__lasti), true};
4749 }
4750 if (__i == __first)
4751 {
4752 ranges::reverse(__first, __last);
4753 return {std::move(__lasti), false};
4754 }
4755 }
4756 }
4757
4758 template<bidirectional_range _Range, typename _Comp = ranges::less,
4759 typename _Proj = identity>
4760 requires sortable<iterator_t<_Range>, _Comp, _Proj>
4761 constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
4762 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
4763 {
4764 return (*this)(ranges::begin(__r), ranges::end(__r),
4765 std::move(__comp), std::move(__proj));
4766 }
4767 };
4768
4769 inline constexpr __prev_permutation_fn prev_permutation{};
4770
4771#if __glibcxx_ranges_contains >= 202207L // C++ >= 23
4772 struct __contains_fn
4773 {
4774 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
4775 typename _Proj = identity,
4776 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
4777 requires indirect_binary_predicate<ranges::equal_to,
4778 projected<_Iter, _Proj>, const _Tp*>
4779 constexpr bool
4780 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
4781 { return ranges::find(std::move(__first), __last, __value, std::move(__proj)) != __last; }
4782
4783 template<input_range _Range,
4784 typename _Proj = identity,
4785 typename _Tp
4786 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
4787 requires indirect_binary_predicate<ranges::equal_to,
4788 projected<iterator_t<_Range>, _Proj>, const _Tp*>
4789 constexpr bool
4790 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
4791 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
4792 };
4793
4794 inline constexpr __contains_fn contains{};
4795
4796 struct __contains_subrange_fn
4797 {
4798 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
4799 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
4800 typename _Pred = ranges::equal_to,
4801 typename _Proj1 = identity, typename _Proj2 = identity>
4802 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
4803 constexpr bool
4804 operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
4805 _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4806 {
4807 return __first2 == __last2
4808 || !ranges::search(__first1, __last1, __first2, __last2,
4809 std::move(__pred), std::move(__proj1), std::move(__proj2)).empty();
4810 }
4811
4812 template<forward_range _Range1, forward_range _Range2,
4813 typename _Pred = ranges::equal_to,
4814 typename _Proj1 = identity, typename _Proj2 = identity>
4815 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
4816 _Pred, _Proj1, _Proj2>
4817 constexpr bool
4818 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
4819 _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
4820 {
4821 return (*this)(ranges::begin(__r1), ranges::end(__r1),
4822 ranges::begin(__r2), ranges::end(__r2),
4823 std::move(__pred), std::move(__proj1), std::move(__proj2));
4824 }
4825 };
4826
4827 inline constexpr __contains_subrange_fn contains_subrange{};
4828
4829#endif // __glibcxx_ranges_contains
4830
4831#if __glibcxx_ranges_find_last >= 202207L // C++ >= 23
4832
4833 struct __find_last_fn
4834 {
4835 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
4836 typename _Proj = identity,
4837 typename _Tp _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(_Iter, _Proj)>
4838 requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Tp*>
4839 [[nodiscard]] constexpr subrange<_Iter>
4840 operator()(_Iter __first, _Sent __last, const _Tp& __value, _Proj __proj = {}) const
4841 {
4842 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4843 {
4844 _Iter __found = ranges::find(reverse_iterator<_Iter>{__last},
4845 reverse_iterator<_Iter>{__first},
4846 __value, std::move(__proj)).base();
4847 if (__found == __first)
4848 return {__last, __last};
4849 else
4850 return {ranges::prev(__found), __last};
4851 }
4852 else
4853 {
4854 _Iter __found = ranges::find(__first, __last, __value, __proj);
4855 if (__found == __last)
4856 return {__found, __found};
4857 __first = __found;
4858 for (;;)
4859 {
4860 __first = ranges::find(ranges::next(__first), __last, __value, __proj);
4861 if (__first == __last)
4862 return {__found, __first};
4863 __found = __first;
4864 }
4865 }
4866 }
4867
4868 template<forward_range _Range, typename _Proj = identity,
4869 typename _Tp
4870 _GLIBCXX26_RANGE_ALGO_DEF_VAL_T(iterator_t<_Range>, _Proj)>
4871 requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Tp*>
4872 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4873 operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
4874 { return (*this)(ranges::begin(__r), ranges::end(__r), __value, std::move(__proj)); }
4875 };
4876
4877 inline constexpr __find_last_fn find_last{};
4878
4879 struct __find_last_if_fn
4880 {
4881 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
4882 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
4883 [[nodiscard]] constexpr subrange<_Iter>
4884 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
4885 {
4886 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4887 {
4888 _Iter __found = ranges::find_if(reverse_iterator<_Iter>{__last},
4889 reverse_iterator<_Iter>{__first},
4890 std::move(__pred), std::move(__proj)).base();
4891 if (__found == __first)
4892 return {__last, __last};
4893 else
4894 return {ranges::prev(__found), __last};
4895 }
4896 else
4897 {
4898 _Iter __found = ranges::find_if(__first, __last, __pred, __proj);
4899 if (__found == __last)
4900 return {__found, __found};
4901 __first = __found;
4902 for (;;)
4903 {
4904 __first = ranges::find_if(ranges::next(__first), __last, __pred, __proj);
4905 if (__first == __last)
4906 return {__found, __first};
4907 __found = __first;
4908 }
4909 }
4910 }
4911
4912 template<forward_range _Range, typename _Proj = identity,
4913 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
4914 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4915 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
4916 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
4917 };
4918
4919 inline constexpr __find_last_if_fn find_last_if{};
4920
4921 struct __find_last_if_not_fn
4922 {
4923 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Proj = identity,
4924 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
4925 [[nodiscard]] constexpr subrange<_Iter>
4926 operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const
4927 {
4928 if constexpr (same_as<_Iter, _Sent> && bidirectional_iterator<_Iter>)
4929 {
4930 _Iter __found = ranges::find_if_not(reverse_iterator<_Iter>{__last},
4931 reverse_iterator<_Iter>{__first},
4932 std::move(__pred), std::move(__proj)).base();
4933 if (__found == __first)
4934 return {__last, __last};
4935 else
4936 return {ranges::prev(__found), __last};
4937 }
4938 else
4939 {
4940 _Iter __found = ranges::find_if_not(__first, __last, __pred, __proj);
4941 if (__found == __last)
4942 return {__found, __found};
4943 __first = __found;
4944 for (;;)
4945 {
4946 __first = ranges::find_if_not(ranges::next(__first), __last, __pred, __proj);
4947 if (__first == __last)
4948 return {__found, __first};
4949 __found = __first;
4950 }
4951 }
4952 }
4953
4954 template<forward_range _Range, typename _Proj = identity,
4955 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
4956 [[nodiscard]] constexpr borrowed_subrange_t<_Range>
4957 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
4958 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__pred), std::move(__proj)); }
4959 };
4960
4961 inline constexpr __find_last_if_not_fn find_last_if_not{};
4962
4963#endif // __glibcxx_ranges_find_last
4964
4965#if __glibcxx_ranges_fold >= 202207L // C++ >= 23
4966
4967 template<typename _Iter, typename _Tp>
4968 struct in_value_result
4969 {
4970 [[no_unique_address]] _Iter in;
4971 [[no_unique_address]] _Tp value;
4972
4973 template<typename _Iter2, typename _Tp2>
4974 requires convertible_to<const _Iter&, _Iter2>
4975 && convertible_to<const _Tp&, _Tp2>
4976 constexpr
4977 operator in_value_result<_Iter2, _Tp2>() const &
4978 { return {in, value}; }
4979
4980 template<typename _Iter2, typename _Tp2>
4981 requires convertible_to<_Iter, _Iter2>
4982 && convertible_to<_Tp, _Tp2>
4983 constexpr
4984 operator in_value_result<_Iter2, _Tp2>() &&
4985 { return {std::move(in), std::move(value)}; }
4986 };
4987
4988 namespace __detail
4989 {
4990 template<typename _Fp>
4991 class __flipped
4992 {
4993 _Fp _M_f;
4994
4995 public:
4996 template<typename _Tp, typename _Up>
4997 requires invocable<_Fp&, _Up, _Tp>
4998 invoke_result_t<_Fp&, _Up, _Tp>
4999 operator()(_Tp&&, _Up&&); // not defined
5000 };
5001
5002 template<typename _Fp, typename _Tp, typename _Iter, typename _Up>
5003 concept __indirectly_binary_left_foldable_impl = movable<_Tp> && movable<_Up>
5004 && convertible_to<_Tp, _Up>
5005 && invocable<_Fp&, _Up, iter_reference_t<_Iter>>
5006 && assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Iter>>>;
5007
5008 template<typename _Fp, typename _Tp, typename _Iter>
5009 concept __indirectly_binary_left_foldable = copy_constructible<_Fp>
5010 && indirectly_readable<_Iter>
5011 && invocable<_Fp&, _Tp, iter_reference_t<_Iter>>
5012 && convertible_to<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>,
5014 && __indirectly_binary_left_foldable_impl
5016
5017 template <typename _Fp, typename _Tp, typename _Iter>
5018 concept __indirectly_binary_right_foldable
5019 = __indirectly_binary_left_foldable<__flipped<_Fp>, _Tp, _Iter>;
5020 } // namespace __detail
5021
5022 template<typename _Iter, typename _Tp>
5023 using fold_left_with_iter_result = in_value_result<_Iter, _Tp>;
5024
5025 struct __fold_left_with_iter_fn
5026 {
5027 template<typename _Ret_iter,
5028 typename _Iter, typename _Sent, typename _Tp, typename _Fp>
5029 static constexpr auto
5030 _S_impl(_Iter __first, _Sent __last, _Tp __init, _Fp __f)
5031 {
5032 using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Iter>>>;
5033 using _Ret = fold_left_with_iter_result<_Ret_iter, _Up>;
5034
5035 if (__first == __last)
5036 return _Ret{std::move(__first), _Up(std::move(__init))};
5037
5038 _Up __accum = std::__invoke(__f, std::move(__init), *__first);
5039 for (++__first; __first != __last; ++__first)
5040 __accum = std::__invoke(__f, std::move(__accum), *__first);
5041 return _Ret{std::move(__first), std::move(__accum)};
5042 }
5043
5044 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5045 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5046 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
5047 constexpr auto
5048 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5049 {
5050 using _Ret_iter = _Iter;
5051 return _S_impl<_Ret_iter>(std::move(__first), __last,
5052 std::move(__init), std::move(__f));
5053 }
5054
5055 template<input_range _Range,
5056 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5057 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
5058 constexpr auto
5059 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5060 {
5061 using _Ret_iter = borrowed_iterator_t<_Range>;
5062 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r),
5063 std::move(__init), std::move(__f));
5064 }
5065 };
5066
5067 inline constexpr __fold_left_with_iter_fn fold_left_with_iter{};
5068
5069 struct __fold_left_fn
5070 {
5071 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5072 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5073 __detail::__indirectly_binary_left_foldable<_Tp, _Iter> _Fp>
5074 constexpr auto
5075 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5076 {
5077 return ranges::fold_left_with_iter(std::move(__first), __last,
5078 std::move(__init), std::move(__f)).value;
5079 }
5080
5081 template<input_range _Range,
5082 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5083 __detail::__indirectly_binary_left_foldable<_Tp, iterator_t<_Range>> _Fp>
5084 constexpr auto
5085 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5086 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
5087 };
5088
5089 inline constexpr __fold_left_fn fold_left{};
5090
5091 template<typename _Iter, typename _Tp>
5092 using fold_left_first_with_iter_result = in_value_result<_Iter, _Tp>;
5093
5094 struct __fold_left_first_with_iter_fn
5095 {
5096 template<typename _Ret_iter, typename _Iter, typename _Sent, typename _Fp>
5097 static constexpr auto
5098 _S_impl(_Iter __first, _Sent __last, _Fp __f)
5099 {
5100 using _Up = decltype(ranges::fold_left(std::move(__first), __last,
5101 iter_value_t<_Iter>(*__first), __f));
5102 using _Ret = fold_left_first_with_iter_result<_Ret_iter, optional<_Up>>;
5103
5104 if (__first == __last)
5105 return _Ret{std::move(__first), optional<_Up>()};
5106
5107 optional<_Up> __init(in_place, *__first);
5108 for (++__first; __first != __last; ++__first)
5109 *__init = std::__invoke(__f, std::move(*__init), *__first);
5110 return _Ret{std::move(__first), std::move(__init)};
5111 }
5112
5113 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5114 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5115 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5116 constexpr auto
5117 operator()(_Iter __first, _Sent __last, _Fp __f) const
5118 {
5119 using _Ret_iter = _Iter;
5120 return _S_impl<_Ret_iter>(std::move(__first), __last, std::move(__f));
5121 }
5122
5123 template<input_range _Range,
5124 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5125 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5126 constexpr auto
5127 operator()(_Range&& __r, _Fp __f) const
5128 {
5129 using _Ret_iter = borrowed_iterator_t<_Range>;
5130 return _S_impl<_Ret_iter>(ranges::begin(__r), ranges::end(__r), std::move(__f));
5131 }
5132 };
5133
5134 inline constexpr __fold_left_first_with_iter_fn fold_left_first_with_iter{};
5135
5136 struct __fold_left_first_fn
5137 {
5138 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
5139 __detail::__indirectly_binary_left_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5140 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5141 constexpr auto
5142 operator()(_Iter __first, _Sent __last, _Fp __f) const
5143 {
5144 return ranges::fold_left_first_with_iter(std::move(__first), __last,
5145 std::move(__f)).value;
5146 }
5147
5148 template<input_range _Range,
5149 __detail::__indirectly_binary_left_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5150 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5151 constexpr auto
5152 operator()(_Range&& __r, _Fp __f) const
5153 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
5154 };
5155
5156 inline constexpr __fold_left_first_fn fold_left_first{};
5157
5158 struct __fold_right_fn
5159 {
5160 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
5161 typename _Tp _GLIBCXX26_DEF_VAL_T(iter_value_t<_Iter>),
5162 __detail::__indirectly_binary_right_foldable<_Tp, _Iter> _Fp>
5163 constexpr auto
5164 operator()(_Iter __first, _Sent __last, _Tp __init, _Fp __f) const
5165 {
5166 using _Up = decay_t<invoke_result_t<_Fp&, iter_reference_t<_Iter>, _Tp>>;
5167
5168 if (__first == __last)
5169 return _Up(std::move(__init));
5170
5171 _Iter __tail = ranges::next(__first, __last);
5172 _Up __accum = std::__invoke(__f, *--__tail, std::move(__init));
5173 while (__first != __tail)
5174 __accum = std::__invoke(__f, *--__tail, std::move(__accum));
5175 return __accum;
5176 }
5177
5178 template<bidirectional_range _Range,
5179 typename _Tp _GLIBCXX26_DEF_VAL_T(range_value_t<_Range>),
5180 __detail::__indirectly_binary_right_foldable<_Tp, iterator_t<_Range>> _Fp>
5181 constexpr auto
5182 operator()(_Range&& __r, _Tp __init, _Fp __f) const
5183 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::move(__f)); }
5184 };
5185
5186 inline constexpr __fold_right_fn fold_right{};
5187
5188 struct __fold_right_last_fn
5189 {
5190 template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
5191 __detail::__indirectly_binary_right_foldable<iter_value_t<_Iter>, _Iter> _Fp>
5192 requires constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>>
5193 constexpr auto
5194 operator()(_Iter __first, _Sent __last, _Fp __f) const
5195 {
5196 using _Up = decltype(ranges::fold_right(__first, __last,
5197 iter_value_t<_Iter>(*__first), __f));
5198
5199 if (__first == __last)
5200 return optional<_Up>();
5201
5202 _Iter __tail = ranges::prev(ranges::next(__first, std::move(__last)));
5203 return optional<_Up>(in_place,
5204 ranges::fold_right(std::move(__first), __tail,
5205 iter_value_t<_Iter>(*__tail),
5206 std::move(__f)));
5207 }
5208
5209 template<bidirectional_range _Range,
5210 __detail::__indirectly_binary_right_foldable<range_value_t<_Range>, iterator_t<_Range>> _Fp>
5211 requires constructible_from<range_value_t<_Range>, range_reference_t<_Range>>
5212 constexpr auto
5213 operator()(_Range&& __r, _Fp __f) const
5214 { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__f)); }
5215 };
5216
5217 inline constexpr __fold_right_last_fn fold_right_last{};
5218#endif // __glibcxx_ranges_fold
5219} // namespace ranges
5220
5221#if __glibcxx_shift >= 201806L // C++ >= 20
5222 template<typename _ForwardIterator>
5223 constexpr _ForwardIterator
5224 shift_left(_ForwardIterator __first, _ForwardIterator __last,
5226 {
5227 __glibcxx_assert(__n >= 0);
5228 if (__n == 0)
5229 return __last;
5230
5231 auto __mid = ranges::next(__first, __n, __last);
5232 if (__mid == __last)
5233 return __first;
5234 return std::move(std::move(__mid), std::move(__last), std::move(__first));
5235 }
5236
5237 template<typename _ForwardIterator>
5238 constexpr _ForwardIterator
5239 shift_right(_ForwardIterator __first, _ForwardIterator __last,
5241 {
5242 __glibcxx_assert(__n >= 0);
5243 if (__n == 0)
5244 return __first;
5245
5246 using _Cat
5248 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
5249 {
5250 auto __mid = ranges::next(__last, -__n, __first);
5251 if (__mid == __first)
5252 return __last;
5253
5254 return std::move_backward(std::move(__first), std::move(__mid),
5255 std::move(__last));
5256 }
5257 else
5258 {
5259 auto __result = ranges::next(__first, __n, __last);
5260 if (__result == __last)
5261 return __last;
5262
5263 auto __dest_head = __first, __dest_tail = __result;
5264 while (__dest_head != __result)
5265 {
5266 if (__dest_tail == __last)
5267 {
5268 // If we get here, then we must have
5269 // 2*n >= distance(__first, __last)
5270 // i.e. we are shifting out at least half of the range. In
5271 // this case we can safely perform the shift with a single
5272 // move.
5273 std::move(std::move(__first), std::move(__dest_head), __result);
5274 return __result;
5275 }
5276 ++__dest_head;
5277 ++__dest_tail;
5278 }
5279
5280 for (;;)
5281 {
5282 // At the start of each iteration of this outer loop, the range
5283 // [__first, __result) contains those elements that after shifting
5284 // the whole range right by __n, should end up in
5285 // [__dest_head, __dest_tail) in order.
5286
5287 // The below inner loop swaps the elements of [__first, __result)
5288 // and [__dest_head, __dest_tail), while simultaneously shifting
5289 // the latter range by __n.
5290 auto __cursor = __first;
5291 while (__cursor != __result)
5292 {
5293 if (__dest_tail == __last)
5294 {
5295 // At this point the ranges [__first, result) and
5296 // [__dest_head, dest_tail) are disjoint, so we can safely
5297 // move the remaining elements.
5298 __dest_head = std::move(__cursor, __result,
5299 std::move(__dest_head));
5300 std::move(std::move(__first), std::move(__cursor),
5301 std::move(__dest_head));
5302 return __result;
5303 }
5304 std::iter_swap(__cursor, __dest_head);
5305 ++__dest_head;
5306 ++__dest_tail;
5307 ++__cursor;
5308 }
5309 }
5310 }
5311 }
5312#endif
5313
5314namespace ranges
5315{
5316#if __glibcxx_shift >= 202202L // C++ >= 23
5317 struct __shift_left_fn
5318 {
5319 template<permutable _Iter, sentinel_for<_Iter> _Sent>
5320 constexpr subrange<_Iter>
5321 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const
5322 {
5323 __glibcxx_assert(__n >= 0);
5324 if (__n == 0)
5325 return {__first, ranges::next(__first, __last)};
5326
5327 auto __mid = ranges::next(__first, __n, __last);
5328 if (__mid == __last)
5329 return {__first, __first};
5330 return {__first, ranges::move(__mid, __last, __first).out};
5331 }
5332
5333 template<forward_range _Range>
5334 requires permutable<iterator_t<_Range>>
5335 constexpr borrowed_subrange_t<_Range>
5336 operator()(_Range&& __r, range_difference_t<_Range> __n) const
5337 { return (*this)(ranges::begin(__r), ranges::end(__r), __n); }
5338 };
5339
5340 inline constexpr __shift_left_fn shift_left{};
5341
5342 struct __shift_right_fn
5343 {
5344 template<permutable _Iter, sentinel_for<_Iter> _Sent>
5345 constexpr subrange<_Iter>
5346 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const
5347 {
5348 __glibcxx_assert(__n >= 0);
5349 if (__n == 0)
5350 return {__first, ranges::next(__first, __last)};
5351
5352 if constexpr (bidirectional_iterator<_Iter> && same_as<_Iter, _Sent>)
5353 {
5354 auto __mid = ranges::next(__last, -__n, __first);
5355 if (__mid == __first)
5356 return {__last, __last};
5357
5358 return {ranges::move_backward(__first, __mid, __last).out, __last};
5359 }
5360 else
5361 {
5362 auto __result = ranges::next(__first, __n, __last);
5363 if (__result == __last)
5364 return {__result, __result};
5365
5366 auto __dest_head = __first, __dest_tail = __result;
5367 while (__dest_head != __result)
5368 {
5369 if (__dest_tail == __last)
5370 {
5371 // If we get here, then we must have
5372 // 2*n >= distance(__first, __last)
5373 // i.e. we are shifting out at least half of the range. In
5374 // this case we can safely perform the shift with a single
5375 // move.
5376 auto __lasti = ranges::move(__first, __dest_head, __result).out;
5377 // __glibcxx_assert(__lasti == __last);
5378 return {__result, __lasti};
5379 }
5380 ++__dest_head;
5381 ++__dest_tail;
5382 }
5383
5384 for (;;)
5385 {
5386 // At the start of each iteration of this outer loop, the range
5387 // [__first, __result) contains those elements that after shifting
5388 // the whole range right by __n, should end up in
5389 // [__dest_head, __dest_tail) in order.
5390
5391 // The below inner loop swaps the elements of [__first, __result)
5392 // and [__dest_head, __dest_tail), while simultaneously shifting
5393 // the latter range by __n.
5394 auto __cursor = __first;
5395 while (__cursor != __result)
5396 {
5397 if (__dest_tail == __last)
5398 {
5399 // At this point the ranges [__first, result) and
5400 // [__dest_head, dest_tail) are disjoint, so we can safely
5401 // move the remaining elements.
5402 __dest_head = ranges::move(__cursor, __result, __dest_head).out;
5403 auto __lasti = ranges::move(__first, __cursor, __dest_head).out;
5404 // __glibcxx_assert(__lasti == __last);
5405 return {__result, __lasti};
5406 }
5407 ranges::iter_swap(__cursor, __dest_head);
5408 ++__dest_head;
5409 ++__dest_tail;
5410 ++__cursor;
5411 }
5412 }
5413 }
5414 }
5415
5416 template<forward_range _Range>
5417 requires permutable<iterator_t<_Range>>
5418 constexpr borrowed_subrange_t<_Range>
5419 operator()(_Range&& __r, range_difference_t<_Range> __n) const
5420 { return (*this)(ranges::begin(__r), ranges::end(__r), __n); }
5421 };
5422
5423 inline constexpr __shift_right_fn shift_right{};
5424#endif // C++23
5425} // namespace ranges
5426
5427_GLIBCXX_END_NAMESPACE_VERSION
5428} // namespace std
5429#endif // concepts
5430#endif // C++20
5431#endif // _RANGES_ALGO_H
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1842
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2892
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:3685
Traits class for iterators.