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