31#define _RANGES_ALGO_H 1
33#if __cplusplus > 201703L
40namespace std _GLIBCXX_VISIBILITY(default)
42_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<
typename _Comp,
typename _Proj>
49 __make_comp_proj(_Comp& __comp, _Proj& __proj)
51 return [&] (
auto&& __lhs,
auto&& __rhs) ->
bool {
52 using _TL =
decltype(__lhs);
53 using _TR =
decltype(__rhs);
60 template<
typename _Pred,
typename _Proj>
62 __make_pred_proj(_Pred& __pred, _Proj& __proj)
64 return [&] <
typename _Tp> (_Tp&& __arg) ->
bool {
73 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
74 typename _Proj =
identity,
75 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
77 operator()(_Iter __first, _Sent __last,
78 _Pred __pred, _Proj __proj = {})
const
80 for (; __first != __last; ++__first)
86 template<input_range _Range,
typename _Proj = identity,
87 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
90 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
92 return (*
this)(ranges::begin(__r), ranges::end(__r),
97 inline constexpr __all_of_fn all_of{};
101 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
102 typename _Proj =
identity,
103 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
105 operator()(_Iter __first, _Sent __last,
106 _Pred __pred, _Proj __proj = {})
const
108 for (; __first != __last; ++__first)
114 template<input_range _Range,
typename _Proj = identity,
115 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
118 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
120 return (*
this)(ranges::begin(__r), ranges::end(__r),
125 inline constexpr __any_of_fn any_of{};
129 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
130 typename _Proj =
identity,
131 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
133 operator()(_Iter __first, _Sent __last,
134 _Pred __pred, _Proj __proj = {})
const
136 for (; __first != __last; ++__first)
142 template<input_range _Range,
typename _Proj = identity,
143 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
146 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
148 return (*
this)(ranges::begin(__r), ranges::end(__r),
153 inline constexpr __none_of_fn none_of{};
155 template<
typename _Iter,
typename _Fp>
158 [[no_unique_address]] _Iter in;
159 [[no_unique_address]] _Fp fun;
161 template<
typename _Iter2,
typename _F2p>
162 requires convertible_to<const _Iter&, _Iter2>
163 && convertible_to<const _Fp&, _F2p>
165 operator in_fun_result<_Iter2, _F2p>() const &
166 {
return {in, fun}; }
168 template<
typename _Iter2,
typename _F2p>
169 requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
171 operator in_fun_result<_Iter2, _F2p>() &&
175 template<
typename _Iter,
typename _Fp>
176 using for_each_result = in_fun_result<_Iter, _Fp>;
180 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
181 typename _Proj =
identity,
182 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
183 constexpr for_each_result<_Iter, _Fun>
184 operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {})
const
186 for (; __first != __last; ++__first)
191 template<input_range _Range,
typename _Proj = identity,
192 indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
194 constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
195 operator()(_Range&& __r, _Fun __f, _Proj __proj = {})
const
197 return (*
this)(ranges::begin(__r), ranges::end(__r),
202 inline constexpr __for_each_fn for_each{};
204 template<
typename _Iter,
typename _Fp>
205 using for_each_n_result = in_fun_result<_Iter, _Fp>;
207 struct __for_each_n_fn
209 template<input_iterator _Iter,
typename _Proj = identity,
210 indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
211 constexpr for_each_n_result<_Iter, _Fun>
212 operator()(_Iter __first, iter_difference_t<_Iter> __n,
213 _Fun __f, _Proj __proj = {})
const
215 if constexpr (random_access_iterator<_Iter>)
219 auto __last = __first + __n;
235 inline constexpr __for_each_n_fn
for_each_n{};
239 struct __find_first_of_fn
241 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
242 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
243 typename _Pred = ranges::equal_to,
244 typename _Proj1 =
identity,
typename _Proj2 =
identity>
245 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
247 operator()(_Iter1 __first1, _Sent1 __last1,
248 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
249 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
251 for (; __first1 != __last1; ++__first1)
252 for (
auto __iter = __first2; __iter != __last2; ++__iter)
260 template<input_range _Range1, forward_range _Range2,
261 typename _Pred = ranges::equal_to,
262 typename _Proj1 = identity,
typename _Proj2 = identity>
263 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
264 _Pred, _Proj1, _Proj2>
265 constexpr borrowed_iterator_t<_Range1>
266 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
267 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
269 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
270 ranges::begin(__r2), ranges::end(__r2),
276 inline constexpr __find_first_of_fn find_first_of{};
280 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
281 typename _Tp,
typename _Proj =
identity>
282 requires indirect_binary_predicate<ranges::equal_to,
283 projected<_Iter, _Proj>,
285 constexpr iter_difference_t<_Iter>
286 operator()(_Iter __first, _Sent __last,
287 const _Tp& __value, _Proj __proj = {})
const
289 iter_difference_t<_Iter> __n = 0;
290 for (; __first != __last; ++__first)
296 template<input_range _Range,
typename _Tp,
typename _Proj =
identity>
297 requires indirect_binary_predicate<ranges::equal_to,
298 projected<iterator_t<_Range>, _Proj>,
300 constexpr range_difference_t<_Range>
301 operator()(_Range&& __r,
const _Tp& __value, _Proj __proj = {})
const
303 return (*
this)(ranges::begin(__r), ranges::end(__r),
308 inline constexpr __count_fn count{};
312 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
313 typename _Proj =
identity,
314 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
315 constexpr iter_difference_t<_Iter>
316 operator()(_Iter __first, _Sent __last,
317 _Pred __pred, _Proj __proj = {})
const
319 iter_difference_t<_Iter> __n = 0;
320 for (; __first != __last; ++__first)
326 template<input_range _Range,
327 typename _Proj = identity,
328 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
330 constexpr range_difference_t<_Range>
331 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
333 return (*
this)(ranges::begin(__r), ranges::end(__r),
338 inline constexpr __count_if_fn count_if{};
344 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp,
345 typename _Pred = ranges::equal_to,
typename _Proj =
identity>
346 requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
347 constexpr subrange<_Iter>
348 operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
349 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {})
const
352 return {__first, __first};
354 auto __value_comp = [&] <
typename _Rp> (_Rp&& __arg) ->
bool {
355 return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
359 __first = ranges::find_if(
std::move(__first), __last,
362 if (__first == __last)
363 return {__first, __first};
366 auto __end = __first;
367 return {__first, ++__end};
371 if constexpr (sized_sentinel_for<_Sent, _Iter>
372 && random_access_iterator<_Iter>)
374 auto __tail_size = __last - __first;
375 auto __remainder = __count;
377 while (__remainder <= __tail_size)
379 __first += __remainder;
380 __tail_size -= __remainder;
381 auto __backtrack = __first;
384 if (--__remainder == 0)
385 return {__first - __count, __first};
387 __remainder = __count + 1 - (__first - __backtrack);
389 auto __i = __first + __tail_size;
394 __first = ranges::find_if(__first, __last, __value_comp, __proj);
395 while (__first != __last)
400 while (__i != __last && __n != 1
407 return {__first, __i};
410 __first = ranges::find_if(++__i, __last, __value_comp, __proj);
412 return {__first, __first};
416 template<forward_range _Range,
typename _Tp,
417 typename _Pred = ranges::equal_to,
typename _Proj = identity>
418 requires indirectly_comparable<iterator_t<_Range>,
const _Tp*,
420 constexpr borrowed_subrange_t<_Range>
421 operator()(_Range&& __r, range_difference_t<_Range> __count,
422 const _Tp& __value, _Pred __pred = {}, _Proj __proj = {})
const
424 return (*
this)(ranges::begin(__r), ranges::end(__r),
430 inline constexpr __search_n_fn search_n{};
434 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
435 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
436 typename _Pred = ranges::equal_to,
437 typename _Proj1 =
identity,
typename _Proj2 =
identity>
438 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
439 constexpr subrange<_Iter1>
440 operator()(_Iter1 __first1, _Sent1 __last1,
441 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
442 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
444 if constexpr (bidirectional_iterator<_Iter1>
445 && bidirectional_iterator<_Iter2>)
447 auto __i1 = ranges::next(__first1, __last1);
448 auto __i2 = ranges::next(__first2, __last2);
450 = ranges::search(reverse_iterator<_Iter1>{__i1},
451 reverse_iterator<_Iter1>{__first1},
452 reverse_iterator<_Iter2>{__i2},
453 reverse_iterator<_Iter2>{__first2},
456 auto __result_first = ranges::end(__rresult).base();
457 auto __result_last = ranges::begin(__rresult).base();
458 if (__result_last == __first1)
461 return {__result_first, __result_last};
465 auto __i = ranges::next(__first1, __last1);
466 if (__first2 == __last2)
469 auto __result_begin = __i;
470 auto __result_end = __i;
473 auto __new_range = ranges::search(__first1, __last1,
475 __pred, __proj1, __proj2);
476 auto __new_result_begin = ranges::begin(__new_range);
477 auto __new_result_end = ranges::end(__new_range);
478 if (__new_result_begin == __last1)
479 return {__result_begin, __result_end};
482 __result_begin = __new_result_begin;
483 __result_end = __new_result_end;
484 __first1 = __result_begin;
491 template<forward_range _Range1, forward_range _Range2,
492 typename _Pred = ranges::equal_to,
493 typename _Proj1 = identity,
typename _Proj2 = identity>
494 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
495 _Pred, _Proj1, _Proj2>
496 constexpr borrowed_subrange_t<_Range1>
497 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
498 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
500 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
501 ranges::begin(__r2), ranges::end(__r2),
507 inline constexpr __find_end_fn find_end{};
509 struct __adjacent_find_fn
511 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
512 typename _Proj =
identity,
513 indirect_binary_predicate<projected<_Iter, _Proj>,
514 projected<_Iter, _Proj>> _Pred
517 operator()(_Iter __first, _Sent __last,
518 _Pred __pred = {}, _Proj __proj = {})
const
520 if (__first == __last)
522 auto __next = __first;
523 for (; ++__next != __last; __first = __next)
533 template<forward_range _Range,
typename _Proj = identity,
534 indirect_binary_predicate<
535 projected<iterator_t<_Range>, _Proj>,
536 projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
537 constexpr borrowed_iterator_t<_Range>
538 operator()(_Range&& __r, _Pred __pred = {}, _Proj __proj = {})
const
540 return (*
this)(ranges::begin(__r), ranges::end(__r),
545 inline constexpr __adjacent_find_fn adjacent_find{};
547 struct __is_permutation_fn
549 template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
550 forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
551 typename _Proj1 =
identity,
typename _Proj2 =
identity,
552 indirect_equivalence_relation<projected<_Iter1, _Proj1>,
553 projected<_Iter2, _Proj2>> _Pred
556 operator()(_Iter1 __first1, _Sent1 __last1,
557 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
558 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
560 constexpr bool __sized_iters
561 = (sized_sentinel_for<_Sent1, _Iter1>
562 && sized_sentinel_for<_Sent2, _Iter2>);
563 if constexpr (__sized_iters)
565 auto __d1 = ranges::distance(__first1, __last1);
566 auto __d2 = ranges::distance(__first2, __last2);
573 for (; __first1 != __last1 && __first2 != __last2;
574 ++__first1, (void)++__first2)
580 if constexpr (__sized_iters)
582 if (__first1 == __last1)
587 auto __d1 = ranges::distance(__first1, __last1);
588 auto __d2 = ranges::distance(__first2, __last2);
589 if (__d1 == 0 && __d2 == 0)
595 for (
auto __scan = __first1; __scan != __last1; ++__scan)
598 auto __comp_scan = [&] <
typename _Tp> (_Tp&& __arg) ->
bool {
600 std::forward<_Tp>(__arg));
602 if (__scan != ranges::find_if(__first1, __scan,
603 __comp_scan, __proj1))
606 auto __matches = ranges::count_if(__first2, __last2,
607 __comp_scan, __proj2);
609 || ranges::count_if(__scan, __last1,
610 __comp_scan, __proj1) != __matches)
616 template<forward_range _Range1, forward_range _Range2,
617 typename _Proj1 = identity,
typename _Proj2 = identity,
618 indirect_equivalence_relation<
619 projected<iterator_t<_Range1>, _Proj1>,
620 projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
622 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
623 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
625 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
626 ranges::begin(__r2), ranges::end(__r2),
632 inline constexpr __is_permutation_fn is_permutation{};
634 template<
typename _Iter,
typename _Out>
635 using copy_if_result = in_out_result<_Iter, _Out>;
639 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
640 weakly_incrementable _Out,
typename _Proj =
identity,
641 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
642 requires indirectly_copyable<_Iter, _Out>
643 constexpr copy_if_result<_Iter, _Out>
644 operator()(_Iter __first, _Sent __last, _Out __result,
645 _Pred __pred, _Proj __proj = {})
const
647 for (; __first != __last; ++__first)
650 *__result = *__first;
656 template<input_range _Range, weakly_incrementable _Out,
657 typename _Proj = identity,
658 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
660 requires indirectly_copyable<iterator_t<_Range>, _Out>
661 constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
662 operator()(_Range&& __r, _Out __result,
663 _Pred __pred, _Proj __proj = {})
const
665 return (*
this)(ranges::begin(__r), ranges::end(__r),
671 inline constexpr __copy_if_fn copy_if{};
673 template<
typename _Iter1,
typename _Iter2>
674 using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
676 struct __swap_ranges_fn
678 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
679 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
680 requires indirectly_swappable<_Iter1, _Iter2>
681 constexpr swap_ranges_result<_Iter1, _Iter2>
682 operator()(_Iter1 __first1, _Sent1 __last1,
683 _Iter2 __first2, _Sent2 __last2)
const
685 for (; __first1 != __last1 && __first2 != __last2;
686 ++__first1, (void)++__first2)
687 ranges::iter_swap(__first1, __first2);
691 template<input_range _Range1, input_range _Range2>
692 requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
693 constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
694 borrowed_iterator_t<_Range2>>
695 operator()(_Range1&& __r1, _Range2&& __r2)
const
697 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
698 ranges::begin(__r2), ranges::end(__r2));
702 inline constexpr __swap_ranges_fn swap_ranges{};
704 template<
typename _Iter,
typename _Out>
705 using unary_transform_result = in_out_result<_Iter, _Out>;
707 template<
typename _Iter1,
typename _Iter2,
typename _Out>
708 struct in_in_out_result
710 [[no_unique_address]] _Iter1 in1;
711 [[no_unique_address]] _Iter2 in2;
712 [[no_unique_address]] _Out out;
714 template<
typename _IIter1,
typename _IIter2,
typename _OOut>
715 requires convertible_to<const _Iter1&, _IIter1>
716 && convertible_to<const _Iter2&, _IIter2>
717 && convertible_to<const _Out&, _OOut>
719 operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
720 {
return {in1, in2, out}; }
722 template<
typename _IIter1,
typename _IIter2,
typename _OOut>
723 requires convertible_to<_Iter1, _IIter1>
724 && convertible_to<_Iter2, _IIter2>
725 && convertible_to<_Out, _OOut>
727 operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
731 template<
typename _Iter1,
typename _Iter2,
typename _Out>
732 using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
734 struct __transform_fn
736 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
737 weakly_incrementable _Out,
738 copy_constructible _Fp,
typename _Proj =
identity>
739 requires indirectly_writable<_Out,
740 indirect_result_t<_Fp&,
741 projected<_Iter, _Proj>>>
742 constexpr unary_transform_result<_Iter, _Out>
743 operator()(_Iter __first1, _Sent __last1, _Out __result,
744 _Fp __op, _Proj __proj = {})
const
746 for (; __first1 != __last1; ++__first1, (void)++__result)
751 template<input_range _Range, weakly_incrementable _Out,
752 copy_constructible _Fp,
typename _Proj = identity>
753 requires indirectly_writable<_Out,
754 indirect_result_t<_Fp&,
755 projected<iterator_t<_Range>, _Proj>>>
756 constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
757 operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {})
const
759 return (*
this)(ranges::begin(__r), ranges::end(__r),
764 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
765 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
766 weakly_incrementable _Out, copy_constructible _Fp,
767 typename _Proj1 =
identity,
typename _Proj2 =
identity>
768 requires indirectly_writable<_Out,
769 indirect_result_t<_Fp&,
770 projected<_Iter1, _Proj1>,
771 projected<_Iter2, _Proj2>>>
772 constexpr binary_transform_result<_Iter1, _Iter2, _Out>
773 operator()(_Iter1 __first1, _Sent1 __last1,
774 _Iter2 __first2, _Sent2 __last2,
775 _Out __result, _Fp __binary_op,
776 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
778 for (; __first1 != __last1 && __first2 != __last2;
779 ++__first1, (void)++__first2, ++__result)
786 template<input_range _Range1, input_range _Range2,
787 weakly_incrementable _Out, copy_constructible _Fp,
788 typename _Proj1 = identity,
typename _Proj2 = identity>
789 requires indirectly_writable<_Out,
790 indirect_result_t<_Fp&,
791 projected<iterator_t<_Range1>, _Proj1>,
792 projected<iterator_t<_Range2>, _Proj2>>>
793 constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
794 borrowed_iterator_t<_Range2>, _Out>
795 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
796 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
798 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
799 ranges::begin(__r2), ranges::end(__r2),
805 inline constexpr __transform_fn transform{};
809 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
810 typename _Tp1,
typename _Tp2,
typename _Proj =
identity>
811 requires indirectly_writable<_Iter, const _Tp2&>
812 && indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
815 operator()(_Iter __first, _Sent __last,
816 const _Tp1& __old_value,
const _Tp2& __new_value,
817 _Proj __proj = {})
const
819 for (; __first != __last; ++__first)
821 *__first = __new_value;
825 template<input_range _Range,
826 typename _Tp1,
typename _Tp2,
typename _Proj = identity>
827 requires indirectly_writable<iterator_t<_Range>,
const _Tp2&>
828 && indirect_binary_predicate<ranges::equal_to,
829 projected<iterator_t<_Range>, _Proj>,
831 constexpr borrowed_iterator_t<_Range>
832 operator()(_Range&& __r,
833 const _Tp1& __old_value,
const _Tp2& __new_value,
834 _Proj __proj = {})
const
836 return (*
this)(ranges::begin(__r), ranges::end(__r),
837 __old_value, __new_value,
std::move(__proj));
841 inline constexpr __replace_fn replace{};
843 struct __replace_if_fn
845 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
846 typename _Tp,
typename _Proj =
identity,
847 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
848 requires indirectly_writable<_Iter, const _Tp&>
850 operator()(_Iter __first, _Sent __last,
851 _Pred __pred,
const _Tp& __new_value, _Proj __proj = {})
const
853 for (; __first != __last; ++__first)
855 *__first = __new_value;
859 template<input_range _Range,
typename _Tp,
typename _Proj = identity,
860 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
862 requires indirectly_writable<iterator_t<_Range>,
const _Tp&>
863 constexpr borrowed_iterator_t<_Range>
864 operator()(_Range&& __r,
865 _Pred __pred,
const _Tp& __new_value, _Proj __proj = {})
const
867 return (*
this)(ranges::begin(__r), ranges::end(__r),
872 inline constexpr __replace_if_fn replace_if{};
874 template<
typename _Iter,
typename _Out>
875 using replace_copy_result = in_out_result<_Iter, _Out>;
877 struct __replace_copy_fn
879 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
880 typename _Tp1,
typename _Tp2, output_iterator<const _Tp2&> _Out,
881 typename _Proj =
identity>
882 requires indirectly_copyable<_Iter, _Out>
883 && indirect_binary_predicate<ranges::equal_to,
884 projected<_Iter, _Proj>,
const _Tp1*>
885 constexpr replace_copy_result<_Iter, _Out>
886 operator()(_Iter __first, _Sent __last, _Out __result,
887 const _Tp1& __old_value,
const _Tp2& __new_value,
888 _Proj __proj = {})
const
890 for (; __first != __last; ++__first, (void)++__result)
892 *__result = __new_value;
894 *__result = *__first;
898 template<input_range _Range,
typename _Tp1,
typename _Tp2,
899 output_iterator<const _Tp2&> _Out,
typename _Proj = identity>
900 requires indirectly_copyable<iterator_t<_Range>, _Out>
901 && indirect_binary_predicate<ranges::equal_to,
902 projected<iterator_t<_Range>, _Proj>,
904 constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
905 operator()(_Range&& __r, _Out __result,
906 const _Tp1& __old_value,
const _Tp2& __new_value,
907 _Proj __proj = {})
const
909 return (*
this)(ranges::begin(__r), ranges::end(__r),
915 inline constexpr __replace_copy_fn replace_copy{};
917 template<
typename _Iter,
typename _Out>
918 using replace_copy_if_result = in_out_result<_Iter, _Out>;
920 struct __replace_copy_if_fn
922 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
923 typename _Tp, output_iterator<const _Tp&> _Out,
924 typename _Proj =
identity,
925 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
926 requires indirectly_copyable<_Iter, _Out>
927 constexpr replace_copy_if_result<_Iter, _Out>
928 operator()(_Iter __first, _Sent __last, _Out __result,
929 _Pred __pred,
const _Tp& __new_value, _Proj __proj = {})
const
931 for (; __first != __last; ++__first, (void)++__result)
933 *__result = __new_value;
935 *__result = *__first;
939 template<input_range _Range,
940 typename _Tp, output_iterator<const _Tp&> _Out,
941 typename _Proj = identity,
942 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
944 requires indirectly_copyable<iterator_t<_Range>, _Out>
945 constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
946 operator()(_Range&& __r, _Out __result,
947 _Pred __pred,
const _Tp& __new_value, _Proj __proj = {})
const
949 return (*
this)(ranges::begin(__r), ranges::end(__r),
955 inline constexpr __replace_copy_if_fn replace_copy_if{};
957 struct __generate_n_fn
959 template<input_or_output_iterator _Out, copy_constructible _Fp>
960 requires invocable<_Fp&>
961 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
963 operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen)
const
965 for (; __n > 0; --__n, (void)++__first)
971 inline constexpr __generate_n_fn generate_n{};
975 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
976 copy_constructible _Fp>
977 requires invocable<_Fp&>
978 && indirectly_writable<_Out, invoke_result_t<_Fp&>>
980 operator()(_Out __first, _Sent __last, _Fp __gen)
const
982 for (; __first != __last; ++__first)
987 template<
typename _Range, copy_constructible _Fp>
988 requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
989 constexpr borrowed_iterator_t<_Range>
990 operator()(_Range&& __r, _Fp __gen)
const
992 return (*
this)(ranges::begin(__r), ranges::end(__r),
std::move(__gen));
996 inline constexpr __generate_fn generate{};
998 struct __remove_if_fn
1000 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1001 typename _Proj =
identity,
1002 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1003 constexpr subrange<_Iter>
1004 operator()(_Iter __first, _Sent __last,
1005 _Pred __pred, _Proj __proj = {})
const
1007 __first = ranges::find_if(__first, __last, __pred, __proj);
1008 if (__first == __last)
1009 return {__first, __first};
1011 auto __result = __first;
1013 for (; __first != __last; ++__first)
1020 return {__result, __first};
1023 template<forward_range _Range,
typename _Proj = identity,
1024 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1026 requires permutable<iterator_t<_Range>>
1027 constexpr borrowed_subrange_t<_Range>
1028 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
1030 return (*
this)(ranges::begin(__r), ranges::end(__r),
1035 inline constexpr __remove_if_fn remove_if{};
1039 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1040 typename _Tp,
typename _Proj =
identity>
1041 requires indirect_binary_predicate<ranges::equal_to,
1042 projected<_Iter, _Proj>,
1044 constexpr subrange<_Iter>
1045 operator()(_Iter __first, _Sent __last,
1046 const _Tp& __value, _Proj __proj = {})
const
1048 auto __pred = [&] (
auto&& __arg) ->
bool {
1049 return std::forward<decltype(__arg)>(__arg) == __value;
1051 return ranges::remove_if(__first, __last,
1055 template<forward_range _Range,
typename _Tp,
typename _Proj =
identity>
1056 requires permutable<iterator_t<_Range>>
1057 && indirect_binary_predicate<ranges::equal_to,
1058 projected<iterator_t<_Range>, _Proj>,
1060 constexpr borrowed_subrange_t<_Range>
1061 operator()(_Range&& __r,
const _Tp& __value, _Proj __proj = {})
const
1063 return (*
this)(ranges::begin(__r), ranges::end(__r),
1068 inline constexpr __remove_fn remove{};
1070 template<
typename _Iter,
typename _Out>
1071 using remove_copy_if_result = in_out_result<_Iter, _Out>;
1073 struct __remove_copy_if_fn
1075 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1076 weakly_incrementable _Out,
typename _Proj =
identity,
1077 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
1078 requires indirectly_copyable<_Iter, _Out>
1079 constexpr remove_copy_if_result<_Iter, _Out>
1080 operator()(_Iter __first, _Sent __last, _Out __result,
1081 _Pred __pred, _Proj __proj = {})
const
1083 for (; __first != __last; ++__first)
1086 *__result = *__first;
1092 template<input_range _Range, weakly_incrementable _Out,
1093 typename _Proj = identity,
1094 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
1096 requires indirectly_copyable<iterator_t<_Range>, _Out>
1097 constexpr remove_copy_if_result<borrowed_iterator_t<_Range>, _Out>
1098 operator()(_Range&& __r, _Out __result,
1099 _Pred __pred, _Proj __proj = {})
const
1101 return (*
this)(ranges::begin(__r), ranges::end(__r),
1107 inline constexpr __remove_copy_if_fn remove_copy_if{};
1109 template<
typename _Iter,
typename _Out>
1110 using remove_copy_result = in_out_result<_Iter, _Out>;
1112 struct __remove_copy_fn
1114 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1115 weakly_incrementable _Out,
typename _Tp,
typename _Proj =
identity>
1116 requires indirectly_copyable<_Iter, _Out>
1117 && indirect_binary_predicate<ranges::equal_to,
1118 projected<_Iter, _Proj>,
1120 constexpr remove_copy_result<_Iter, _Out>
1121 operator()(_Iter __first, _Sent __last, _Out __result,
1122 const _Tp& __value, _Proj __proj = {})
const
1124 for (; __first != __last; ++__first)
1127 *__result = *__first;
1133 template<input_range _Range, weakly_incrementable _Out,
1134 typename _Tp,
typename _Proj = identity>
1135 requires indirectly_copyable<iterator_t<_Range>, _Out>
1136 && indirect_binary_predicate<ranges::equal_to,
1137 projected<iterator_t<_Range>, _Proj>,
1139 constexpr remove_copy_result<borrowed_iterator_t<_Range>, _Out>
1140 operator()(_Range&& __r, _Out __result,
1141 const _Tp& __value, _Proj __proj = {})
const
1143 return (*
this)(ranges::begin(__r), ranges::end(__r),
1148 inline constexpr __remove_copy_fn remove_copy{};
1152 template<permutable _Iter, sentinel_for<_Iter> _Sent,
1153 typename _Proj =
identity,
1154 indirect_equivalence_relation<
1155 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1156 constexpr subrange<_Iter>
1157 operator()(_Iter __first, _Sent __last,
1158 _Comp __comp = {}, _Proj __proj = {})
const
1160 __first = ranges::adjacent_find(__first, __last, __comp, __proj);
1161 if (__first == __last)
1162 return {__first, __first};
1164 auto __dest = __first;
1166 while (++__first != __last)
1171 return {++__dest, __first};
1174 template<forward_range _Range,
typename _Proj = identity,
1175 indirect_equivalence_relation<
1176 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1177 requires permutable<iterator_t<_Range>>
1178 constexpr borrowed_subrange_t<_Range>
1179 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1181 return (*
this)(ranges::begin(__r), ranges::end(__r),
1186 inline constexpr __unique_fn unique{};
1190 template<
typename _Out,
typename _Tp>
1191 concept __can_reread_output = input_iterator<_Out>
1192 && same_as<_Tp, iter_value_t<_Out>>;
1195 template<
typename _Iter,
typename _Out>
1196 using unique_copy_result = in_out_result<_Iter, _Out>;
1198 struct __unique_copy_fn
1200 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1201 weakly_incrementable _Out,
typename _Proj =
identity,
1202 indirect_equivalence_relation<
1203 projected<_Iter, _Proj>> _Comp = ranges::equal_to>
1204 requires indirectly_copyable<_Iter, _Out>
1205 && (forward_iterator<_Iter>
1206 || __detail::__can_reread_output<_Out, iter_value_t<_Iter>>
1207 || indirectly_copyable_storable<_Iter, _Out>)
1208 constexpr unique_copy_result<_Iter, _Out>
1209 operator()(_Iter __first, _Sent __last, _Out __result,
1210 _Comp __comp = {}, _Proj __proj = {})
const
1212 if (__first == __last)
1216 if constexpr (forward_iterator<_Iter>)
1218 auto __next = __first;
1219 *__result = *__next;
1220 while (++__next != __last)
1226 *++__result = *__first;
1230 else if constexpr (__detail::__can_reread_output<_Out, iter_value_t<_Iter>>)
1232 *__result = *__first;
1233 while (++__first != __last)
1237 *++__result = *__first;
1242 auto __value = *__first;
1243 *__result = __value;
1244 while (++__first != __last)
1251 *++__result = __value;
1258 template<input_range _Range,
1259 weakly_incrementable _Out,
typename _Proj = identity,
1260 indirect_equivalence_relation<
1261 projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
1262 requires indirectly_copyable<iterator_t<_Range>, _Out>
1263 && (forward_iterator<iterator_t<_Range>>
1264 || __detail::__can_reread_output<_Out, range_value_t<_Range>>
1265 || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
1266 constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
1267 operator()(_Range&& __r, _Out __result,
1268 _Comp __comp = {}, _Proj __proj = {})
const
1270 return (*
this)(ranges::begin(__r), ranges::end(__r),
1276 inline constexpr __unique_copy_fn unique_copy{};
1280 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
1281 requires permutable<_Iter>
1283 operator()(_Iter __first, _Sent __last)
const
1285 auto __i = ranges::next(__first, __last);
1288 if constexpr (random_access_iterator<_Iter>)
1290 if (__first != __last)
1293 while (__first < __tail)
1295 ranges::iter_swap(__first, __tail);
1305 if (__first == __tail || __first == --__tail)
1309 ranges::iter_swap(__first, __tail);
1316 template<b
idirectional_range _Range>
1317 requires permutable<iterator_t<_Range>>
1318 constexpr borrowed_iterator_t<_Range>
1319 operator()(_Range&& __r)
const
1321 return (*
this)(ranges::begin(__r), ranges::end(__r));
1325 inline constexpr __reverse_fn reverse{};
1327 template<
typename _Iter,
typename _Out>
1328 using reverse_copy_result = in_out_result<_Iter, _Out>;
1330 struct __reverse_copy_fn
1332 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
1333 weakly_incrementable _Out>
1334 requires indirectly_copyable<_Iter, _Out>
1335 constexpr reverse_copy_result<_Iter, _Out>
1336 operator()(_Iter __first, _Sent __last, _Out __result)
const
1338 auto __i = ranges::next(__first, __last);
1340 while (__first != __tail)
1343 *__result = *__tail;
1349 template<b
idirectional_range _Range, weakly_incrementable _Out>
1350 requires indirectly_copyable<iterator_t<_Range>, _Out>
1351 constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
1352 operator()(_Range&& __r, _Out __result)
const
1354 return (*
this)(ranges::begin(__r), ranges::end(__r),
1359 inline constexpr __reverse_copy_fn reverse_copy{};
1363 template<permutable _Iter, sentinel_for<_Iter> _Sent>
1364 constexpr subrange<_Iter>
1365 operator()(_Iter __first, _Iter __middle, _Sent __last)
const
1367 auto __lasti = ranges::next(__first, __last);
1368 if (__first == __middle)
1369 return {__lasti, __lasti};
1370 if (__last == __middle)
1373 if constexpr (random_access_iterator<_Iter>)
1375 auto __n = __lasti - __first;
1376 auto __k = __middle - __first;
1378 if (__k == __n - __k)
1380 ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
1385 auto __ret = __first + (__lasti - __middle);
1389 if (__k < __n - __k)
1393 if constexpr (__is_pod(iter_value_t<_Iter>))
1397 ranges::move(__p + 1, __p + __n, __p);
1401 auto __q = __p + __k;
1402 for (
decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1404 ranges::iter_swap(__p, __q);
1411 ranges::swap(__n, __k);
1419 if constexpr (__is_pod(iter_value_t<_Iter>))
1423 ranges::move_backward(__p, __p + __n - 1, __p + __n);
1427 auto __q = __p + __n;
1429 for (
decltype(__n) __i = 0; __i < __n - __k; ++ __i)
1433 ranges::iter_swap(__p, __q);
1442 else if constexpr (bidirectional_iterator<_Iter>)
1444 auto __tail = __lasti;
1446 ranges::reverse(__first, __middle);
1447 ranges::reverse(__middle, __tail);
1449 while (__first != __middle && __middle != __tail)
1451 ranges::iter_swap(__first, --__tail);
1455 if (__first == __middle)
1457 ranges::reverse(__middle, __tail);
1462 ranges::reverse(__first, __middle);
1468 auto __first2 = __middle;
1471 ranges::iter_swap(__first, __first2);
1474 if (__first == __middle)
1475 __middle = __first2;
1476 }
while (__first2 != __last);
1478 auto __ret = __first;
1480 __first2 = __middle;
1482 while (__first2 != __last)
1484 ranges::iter_swap(__first, __first2);
1487 if (__first == __middle)
1488 __middle = __first2;
1489 else if (__first2 == __last)
1490 __first2 = __middle;
1496 template<forward_range _Range>
1497 requires permutable<iterator_t<_Range>>
1498 constexpr borrowed_subrange_t<_Range>
1499 operator()(_Range&& __r, iterator_t<_Range> __middle)
const
1501 return (*
this)(ranges::begin(__r),
std::move(__middle),
1506 inline constexpr __rotate_fn rotate{};
1508 template<
typename _Iter,
typename _Out>
1509 using rotate_copy_result = in_out_result<_Iter, _Out>;
1511 struct __rotate_copy_fn
1513 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1514 weakly_incrementable _Out>
1515 requires indirectly_copyable<_Iter, _Out>
1516 constexpr rotate_copy_result<_Iter, _Out>
1517 operator()(_Iter __first, _Iter __middle, _Sent __last,
1518 _Out __result)
const
1520 auto __copy1 = ranges::copy(__middle,
1523 auto __copy2 = ranges::copy(
std::move(__first),
1529 template<forward_range _Range, weakly_incrementable _Out>
1530 requires indirectly_copyable<iterator_t<_Range>, _Out>
1531 constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
1532 operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result)
const
1534 return (*
this)(ranges::begin(__r),
std::move(__middle),
1539 inline constexpr __rotate_copy_fn rotate_copy{};
1543 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
1544 weakly_incrementable _Out,
typename _Gen>
1545 requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
1546 && indirectly_copyable<_Iter, _Out>
1547 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1549 operator()(_Iter __first, _Sent __last, _Out __out,
1550 iter_difference_t<_Iter> __n, _Gen&& __g)
const
1552 if constexpr (forward_iterator<_Iter>)
1556 auto __lasti = ranges::next(__first, __last);
1557 return _GLIBCXX_STD_A::
1559 __n, std::forward<_Gen>(__g));
1563 using __distrib_type
1564 = uniform_int_distribution<iter_difference_t<_Iter>>;
1565 using __param_type =
typename __distrib_type::param_type;
1566 __distrib_type __d{};
1567 iter_difference_t<_Iter> __sample_sz = 0;
1568 while (__first != __last && __sample_sz != __n)
1570 __out[__sample_sz++] = *__first;
1573 for (
auto __pop_sz = __sample_sz; __first != __last;
1574 ++__first, (void) ++__pop_sz)
1576 const auto __k = __d(__g, __param_type{0, __pop_sz});
1578 __out[__k] = *__first;
1580 return __out + __sample_sz;
1584 template<input_range _Range, weakly_incrementable _Out,
typename _Gen>
1585 requires (forward_range<_Range> || random_access_iterator<_Out>)
1586 && indirectly_copyable<iterator_t<_Range>, _Out>
1587 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1589 operator()(_Range&& __r, _Out __out,
1590 range_difference_t<_Range> __n, _Gen&& __g)
const
1592 return (*
this)(ranges::begin(__r), ranges::end(__r),
1594 std::forward<_Gen>(__g));
1598 inline constexpr __sample_fn
sample{};
1600#ifdef _GLIBCXX_USE_C99_STDINT_TR1
1603 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1605 requires permutable<_Iter>
1606 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1608 operator()(_Iter __first, _Sent __last, _Gen&& __g)
const
1610 auto __lasti = ranges::next(__first, __last);
1611 std::shuffle(
std::move(__first), __lasti, std::forward<_Gen>(__g));
1615 template<random_access_range _Range,
typename _Gen>
1616 requires permutable<iterator_t<_Range>>
1617 && uniform_random_bit_generator<remove_reference_t<_Gen>>
1618 borrowed_iterator_t<_Range>
1619 operator()(_Range&& __r, _Gen&& __g)
const
1621 return (*
this)(ranges::begin(__r), ranges::end(__r),
1622 std::forward<_Gen>(__g));
1626 inline constexpr __shuffle_fn shuffle{};
1629 struct __push_heap_fn
1631 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1632 typename _Comp = ranges::less,
typename _Proj =
identity>
1633 requires sortable<_Iter, _Comp, _Proj>
1635 operator()(_Iter __first, _Sent __last,
1636 _Comp __comp = {}, _Proj __proj = {})
const
1638 auto __lasti = ranges::next(__first, __last);
1639 std::push_heap(__first, __lasti,
1640 __detail::__make_comp_proj(__comp, __proj));
1644 template<random_access_range _Range,
1645 typename _Comp = ranges::less,
typename _Proj = identity>
1646 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1647 constexpr borrowed_iterator_t<_Range>
1648 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1650 return (*
this)(ranges::begin(__r), ranges::end(__r),
1655 inline constexpr __push_heap_fn push_heap{};
1657 struct __pop_heap_fn
1659 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1660 typename _Comp = ranges::less,
typename _Proj =
identity>
1661 requires sortable<_Iter, _Comp, _Proj>
1663 operator()(_Iter __first, _Sent __last,
1664 _Comp __comp = {}, _Proj __proj = {})
const
1666 auto __lasti = ranges::next(__first, __last);
1667 std::pop_heap(__first, __lasti,
1668 __detail::__make_comp_proj(__comp, __proj));
1672 template<random_access_range _Range,
1673 typename _Comp = ranges::less,
typename _Proj = identity>
1674 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1675 constexpr borrowed_iterator_t<_Range>
1676 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1678 return (*
this)(ranges::begin(__r), ranges::end(__r),
1683 inline constexpr __pop_heap_fn pop_heap{};
1685 struct __make_heap_fn
1687 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1688 typename _Comp = ranges::less,
typename _Proj =
identity>
1689 requires sortable<_Iter, _Comp, _Proj>
1691 operator()(_Iter __first, _Sent __last,
1692 _Comp __comp = {}, _Proj __proj = {})
const
1694 auto __lasti = ranges::next(__first, __last);
1695 std::make_heap(__first, __lasti,
1696 __detail::__make_comp_proj(__comp, __proj));
1700 template<random_access_range _Range,
1701 typename _Comp = ranges::less,
typename _Proj = identity>
1702 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1703 constexpr borrowed_iterator_t<_Range>
1704 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1706 return (*
this)(ranges::begin(__r), ranges::end(__r),
1711 inline constexpr __make_heap_fn make_heap{};
1713 struct __sort_heap_fn
1715 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1716 typename _Comp = ranges::less,
typename _Proj =
identity>
1717 requires sortable<_Iter, _Comp, _Proj>
1719 operator()(_Iter __first, _Sent __last,
1720 _Comp __comp = {}, _Proj __proj = {})
const
1722 auto __lasti = ranges::next(__first, __last);
1723 std::sort_heap(__first, __lasti,
1724 __detail::__make_comp_proj(__comp, __proj));
1728 template<random_access_range _Range,
1729 typename _Comp = ranges::less,
typename _Proj = identity>
1730 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1731 constexpr borrowed_iterator_t<_Range>
1732 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1734 return (*
this)(ranges::begin(__r), ranges::end(__r),
1739 inline constexpr __sort_heap_fn sort_heap{};
1741 struct __is_heap_until_fn
1743 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1744 typename _Proj =
identity,
1745 indirect_strict_weak_order<projected<_Iter, _Proj>>
1746 _Comp = ranges::less>
1748 operator()(_Iter __first, _Sent __last,
1749 _Comp __comp = {}, _Proj __proj = {})
const
1751 iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
1752 iter_difference_t<_Iter> __parent = 0, __child = 1;
1753 for (; __child < __n; ++__child)
1757 return __first + __child;
1758 else if ((__child & 1) == 0)
1761 return __first + __n;
1764 template<random_access_range _Range,
1765 typename _Proj = identity,
1766 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1767 _Comp = ranges::less>
1768 constexpr borrowed_iterator_t<_Range>
1769 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1771 return (*
this)(ranges::begin(__r), ranges::end(__r),
1776 inline constexpr __is_heap_until_fn is_heap_until{};
1780 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1781 typename _Proj =
identity,
1782 indirect_strict_weak_order<projected<_Iter, _Proj>>
1783 _Comp = ranges::less>
1785 operator()(_Iter __first, _Sent __last,
1786 _Comp __comp = {}, _Proj __proj = {})
const
1789 == ranges::is_heap_until(__first, __last,
1794 template<random_access_range _Range,
1795 typename _Proj = identity,
1796 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
1797 _Comp = ranges::less>
1799 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1801 return (*
this)(ranges::begin(__r), ranges::end(__r),
1806 inline constexpr __is_heap_fn is_heap{};
1810 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1811 typename _Comp = ranges::less,
typename _Proj =
identity>
1812 requires sortable<_Iter, _Comp, _Proj>
1814 operator()(_Iter __first, _Sent __last,
1815 _Comp __comp = {}, _Proj __proj = {})
const
1817 auto __lasti = ranges::next(__first, __last);
1818 _GLIBCXX_STD_A::sort(
std::move(__first), __lasti,
1819 __detail::__make_comp_proj(__comp, __proj));
1823 template<random_access_range _Range,
1824 typename _Comp = ranges::less,
typename _Proj = identity>
1825 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1826 constexpr borrowed_iterator_t<_Range>
1827 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1829 return (*
this)(ranges::begin(__r), ranges::end(__r),
1834 inline constexpr __sort_fn sort{};
1836 struct __stable_sort_fn
1838 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1839 typename _Comp = ranges::less,
typename _Proj =
identity>
1840 requires sortable<_Iter, _Comp, _Proj>
1842 operator()(_Iter __first, _Sent __last,
1843 _Comp __comp = {}, _Proj __proj = {})
const
1845 auto __lasti = ranges::next(__first, __last);
1846 std::stable_sort(
std::move(__first), __lasti,
1847 __detail::__make_comp_proj(__comp, __proj));
1851 template<random_access_range _Range,
1852 typename _Comp = ranges::less,
typename _Proj = identity>
1853 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1854 borrowed_iterator_t<_Range>
1855 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
1857 return (*
this)(ranges::begin(__r), ranges::end(__r),
1862 inline constexpr __stable_sort_fn stable_sort{};
1864 struct __partial_sort_fn
1866 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
1867 typename _Comp = ranges::less,
typename _Proj =
identity>
1868 requires sortable<_Iter, _Comp, _Proj>
1870 operator()(_Iter __first, _Iter __middle, _Sent __last,
1871 _Comp __comp = {}, _Proj __proj = {})
const
1873 if (__first == __middle)
1874 return ranges::next(__first, __last);
1876 ranges::make_heap(__first, __middle, __comp, __proj);
1877 auto __i = __middle;
1878 for (; __i != __last; ++__i)
1883 ranges::pop_heap(__first, __middle, __comp, __proj);
1884 ranges::iter_swap(__middle-1, __i);
1885 ranges::push_heap(__first, __middle, __comp, __proj);
1887 ranges::sort_heap(__first, __middle, __comp, __proj);
1892 template<random_access_range _Range,
1893 typename _Comp = ranges::less,
typename _Proj = identity>
1894 requires sortable<iterator_t<_Range>, _Comp, _Proj>
1895 constexpr borrowed_iterator_t<_Range>
1896 operator()(_Range&& __r, iterator_t<_Range> __middle,
1897 _Comp __comp = {}, _Proj __proj = {})
const
1899 return (*
this)(ranges::begin(__r),
std::move(__middle),
1905 inline constexpr __partial_sort_fn partial_sort{};
1907 template<
typename _Iter,
typename _Out>
1908 using partial_sort_copy_result = in_out_result<_Iter, _Out>;
1910 struct __partial_sort_copy_fn
1912 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
1913 random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
1914 typename _Comp = ranges::less,
1915 typename _Proj1 =
identity,
typename _Proj2 =
identity>
1916 requires indirectly_copyable<_Iter1, _Iter2>
1917 && sortable<_Iter2, _Comp, _Proj2>
1918 && indirect_strict_weak_order<_Comp,
1919 projected<_Iter1, _Proj1>,
1920 projected<_Iter2, _Proj2>>
1921 constexpr partial_sort_copy_result<_Iter1, _Iter2>
1922 operator()(_Iter1 __first, _Sent1 __last,
1923 _Iter2 __result_first, _Sent2 __result_last,
1925 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
1927 if (__result_first == __result_last)
1930 auto __lasti = ranges::next(
std::move(__first),
1935 auto __result_real_last = __result_first;
1936 while (__first != __last && __result_real_last != __result_last)
1938 *__result_real_last = *__first;
1939 ++__result_real_last;
1943 ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
1944 for (; __first != __last; ++__first)
1949 ranges::pop_heap(__result_first, __result_real_last,
1951 *(__result_real_last-1) = *__first;
1952 ranges::push_heap(__result_first, __result_real_last,
1955 ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
1960 template<input_range _Range1, random_access_range _Range2,
1961 typename _Comp = ranges::less,
1962 typename _Proj1 = identity,
typename _Proj2 = identity>
1963 requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
1964 && sortable<iterator_t<_Range2>, _Comp, _Proj2>
1965 && indirect_strict_weak_order<_Comp,
1966 projected<iterator_t<_Range1>, _Proj1>,
1967 projected<iterator_t<_Range2>, _Proj2>>
1968 constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
1969 borrowed_iterator_t<_Range2>>
1970 operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
1971 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
1973 return (*
this)(ranges::begin(__r), ranges::end(__r),
1974 ranges::begin(__out), ranges::end(__out),
1980 inline constexpr __partial_sort_copy_fn partial_sort_copy{};
1982 struct __is_sorted_until_fn
1984 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
1985 typename _Proj =
identity,
1986 indirect_strict_weak_order<projected<_Iter, _Proj>>
1987 _Comp = ranges::less>
1989 operator()(_Iter __first, _Sent __last,
1990 _Comp __comp = {}, _Proj __proj = {})
const
1992 if (__first == __last)
1995 auto __next = __first;
1996 for (++__next; __next != __last; __first = __next, (void)++__next)
2004 template<forward_range _Range,
typename _Proj = identity,
2005 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2006 _Comp = ranges::less>
2007 constexpr borrowed_iterator_t<_Range>
2008 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
2010 return (*
this)(ranges::begin(__r), ranges::end(__r),
2015 inline constexpr __is_sorted_until_fn is_sorted_until{};
2017 struct __is_sorted_fn
2019 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2020 typename _Proj =
identity,
2021 indirect_strict_weak_order<projected<_Iter, _Proj>>
2022 _Comp = ranges::less>
2024 operator()(_Iter __first, _Sent __last,
2025 _Comp __comp = {}, _Proj __proj = {})
const
2027 if (__first == __last)
2030 auto __next = __first;
2031 for (++__next; __next != __last; __first = __next, (void)++__next)
2039 template<forward_range _Range,
typename _Proj = identity,
2040 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2041 _Comp = ranges::less>
2043 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
2045 return (*
this)(ranges::begin(__r), ranges::end(__r),
2050 inline constexpr __is_sorted_fn is_sorted{};
2052 struct __nth_element_fn
2054 template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
2055 typename _Comp = ranges::less,
typename _Proj =
identity>
2056 requires sortable<_Iter, _Comp, _Proj>
2058 operator()(_Iter __first, _Iter __nth, _Sent __last,
2059 _Comp __comp = {}, _Proj __proj = {})
const
2061 auto __lasti = ranges::next(__first, __last);
2064 __detail::__make_comp_proj(__comp, __proj));
2068 template<random_access_range _Range,
2069 typename _Comp = ranges::less,
typename _Proj = identity>
2070 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2071 constexpr borrowed_iterator_t<_Range>
2072 operator()(_Range&& __r, iterator_t<_Range> __nth,
2073 _Comp __comp = {}, _Proj __proj = {})
const
2075 return (*
this)(ranges::begin(__r),
std::move(__nth),
2080 inline constexpr __nth_element_fn nth_element{};
2082 struct __lower_bound_fn
2084 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2085 typename _Tp,
typename _Proj =
identity,
2086 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2087 _Comp = ranges::less>
2089 operator()(_Iter __first, _Sent __last,
2090 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2092 auto __len = ranges::distance(__first, __last);
2096 auto __half = __len / 2;
2097 auto __middle = __first;
2098 ranges::advance(__middle, __half);
2103 __len = __len - __half - 1;
2111 template<forward_range _Range,
typename _Tp,
typename _Proj = identity,
2112 indirect_strict_weak_order<
const _Tp*,
2113 projected<iterator_t<_Range>, _Proj>>
2114 _Comp = ranges::less>
2115 constexpr borrowed_iterator_t<_Range>
2116 operator()(_Range&& __r,
2117 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2119 return (*
this)(ranges::begin(__r), ranges::end(__r),
2124 inline constexpr __lower_bound_fn lower_bound{};
2126 struct __upper_bound_fn
2128 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2129 typename _Tp,
typename _Proj =
identity,
2130 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2131 _Comp = ranges::less>
2133 operator()(_Iter __first, _Sent __last,
2134 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2136 auto __len = ranges::distance(__first, __last);
2140 auto __half = __len / 2;
2141 auto __middle = __first;
2142 ranges::advance(__middle, __half);
2149 __len = __len - __half - 1;
2155 template<forward_range _Range,
typename _Tp,
typename _Proj = identity,
2156 indirect_strict_weak_order<
const _Tp*,
2157 projected<iterator_t<_Range>, _Proj>>
2158 _Comp = ranges::less>
2159 constexpr borrowed_iterator_t<_Range>
2160 operator()(_Range&& __r,
2161 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2163 return (*
this)(ranges::begin(__r), ranges::end(__r),
2168 inline constexpr __upper_bound_fn upper_bound{};
2170 struct __equal_range_fn
2172 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2173 typename _Tp,
typename _Proj =
identity,
2174 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2175 _Comp = ranges::less>
2176 constexpr subrange<_Iter>
2177 operator()(_Iter __first, _Sent __last,
2178 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2180 auto __len = ranges::distance(__first, __last);
2184 auto __half = __len / 2;
2185 auto __middle = __first;
2186 ranges::advance(__middle, __half);
2193 __len = __len - __half - 1;
2202 = ranges::lower_bound(__first, __middle,
2203 __value, __comp, __proj);
2204 ranges::advance(__first, __len);
2206 = ranges::upper_bound(++__middle, __first,
2207 __value, __comp, __proj);
2208 return {__left, __right};
2211 return {__first, __first};
2214 template<forward_range _Range,
2215 typename _Tp,
typename _Proj = identity,
2216 indirect_strict_weak_order<
const _Tp*,
2217 projected<iterator_t<_Range>, _Proj>>
2218 _Comp = ranges::less>
2219 constexpr borrowed_subrange_t<_Range>
2220 operator()(_Range&& __r,
const _Tp& __value,
2221 _Comp __comp = {}, _Proj __proj = {})
const
2223 return (*
this)(ranges::begin(__r), ranges::end(__r),
2228 inline constexpr __equal_range_fn equal_range{};
2230 struct __binary_search_fn
2232 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2233 typename _Tp,
typename _Proj =
identity,
2234 indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
2235 _Comp = ranges::less>
2237 operator()(_Iter __first, _Sent __last,
2238 const _Tp& __value, _Comp __comp = {}, _Proj __proj = {})
const
2240 auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
2247 template<forward_range _Range,
2248 typename _Tp,
typename _Proj = identity,
2249 indirect_strict_weak_order<
const _Tp*,
2250 projected<iterator_t<_Range>, _Proj>>
2251 _Comp = ranges::less>
2253 operator()(_Range&& __r,
const _Tp& __value, _Comp __comp = {},
2254 _Proj __proj = {})
const
2256 return (*
this)(ranges::begin(__r), ranges::end(__r),
2261 inline constexpr __binary_search_fn binary_search{};
2263 struct __is_partitioned_fn
2265 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2266 typename _Proj =
identity,
2267 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2269 operator()(_Iter __first, _Sent __last,
2270 _Pred __pred, _Proj __proj = {})
const
2272 __first = ranges::find_if_not(
std::move(__first), __last,
2274 if (__first == __last)
2281 template<input_range _Range,
typename _Proj = identity,
2282 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2285 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
2287 return (*
this)(ranges::begin(__r), ranges::end(__r),
2292 inline constexpr __is_partitioned_fn is_partitioned{};
2294 struct __partition_fn
2296 template<permutable _Iter, sentinel_for<_Iter> _Sent,
2297 typename _Proj =
identity,
2298 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2299 constexpr subrange<_Iter>
2300 operator()(_Iter __first, _Sent __last,
2301 _Pred __pred, _Proj __proj = {})
const
2303 if constexpr (bidirectional_iterator<_Iter>)
2305 auto __lasti = ranges::next(__first, __last);
2306 auto __tail = __lasti;
2310 if (__first == __tail)
2319 if (__first == __tail)
2326 ranges::iter_swap(__first, __tail);
2332 if (__first == __last)
2333 return {__first, __first};
2336 if (++__first == __last)
2337 return {__first, __first};
2339 auto __next = __first;
2340 while (++__next != __last)
2343 ranges::iter_swap(__first, __next);
2351 template<forward_range _Range,
typename _Proj = identity,
2352 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2354 requires permutable<iterator_t<_Range>>
2355 constexpr borrowed_subrange_t<_Range>
2356 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
2358 return (*
this)(ranges::begin(__r), ranges::end(__r),
2363 inline constexpr __partition_fn partition{};
2365 struct __stable_partition_fn
2367 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2368 typename _Proj =
identity,
2369 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2370 requires permutable<_Iter>
2372 operator()(_Iter __first, _Sent __last,
2373 _Pred __pred, _Proj __proj = {})
const
2375 auto __lasti = ranges::next(__first, __last);
2377 = std::stable_partition(
std::move(__first), __lasti,
2378 __detail::__make_pred_proj(__pred, __proj));
2382 template<bidirectional_range _Range,
typename _Proj = identity,
2383 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2385 requires permutable<iterator_t<_Range>>
2386 borrowed_subrange_t<_Range>
2387 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
2389 return (*
this)(ranges::begin(__r), ranges::end(__r),
2394 inline constexpr __stable_partition_fn stable_partition{};
2396 template<
typename _Iter,
typename _Out1,
typename _Out2>
2397 struct in_out_out_result
2399 [[no_unique_address]] _Iter in;
2400 [[no_unique_address]] _Out1 out1;
2401 [[no_unique_address]] _Out2 out2;
2403 template<
typename _IIter,
typename _OOut1,
typename _OOut2>
2404 requires convertible_to<const _Iter&, _IIter>
2405 && convertible_to<const _Out1&, _OOut1>
2406 && convertible_to<const _Out2&, _OOut2>
2408 operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
2409 {
return {in, out1, out2}; }
2411 template<
typename _IIter,
typename _OOut1,
typename _OOut2>
2412 requires convertible_to<_Iter, _IIter>
2413 && convertible_to<_Out1, _OOut1>
2414 && convertible_to<_Out2, _OOut2>
2416 operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
2420 template<
typename _Iter,
typename _Out1,
typename _Out2>
2421 using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
2423 struct __partition_copy_fn
2425 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
2426 weakly_incrementable _Out1, weakly_incrementable _Out2,
2427 typename _Proj =
identity,
2428 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2429 requires indirectly_copyable<_Iter, _Out1>
2430 && indirectly_copyable<_Iter, _Out2>
2431 constexpr partition_copy_result<_Iter, _Out1, _Out2>
2432 operator()(_Iter __first, _Sent __last,
2433 _Out1 __out_true, _Out2 __out_false,
2434 _Pred __pred, _Proj __proj = {})
const
2436 for (; __first != __last; ++__first)
2439 *__out_true = *__first;
2444 *__out_false = *__first;
2452 template<input_range _Range, weakly_incrementable _Out1,
2453 weakly_incrementable _Out2,
2454 typename _Proj = identity,
2455 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2457 requires indirectly_copyable<iterator_t<_Range>, _Out1>
2458 && indirectly_copyable<iterator_t<_Range>, _Out2>
2459 constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
2460 operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
2461 _Pred __pred, _Proj __proj = {})
const
2463 return (*
this)(ranges::begin(__r), ranges::end(__r),
2469 inline constexpr __partition_copy_fn partition_copy{};
2471 struct __partition_point_fn
2473 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
2474 typename _Proj =
identity,
2475 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
2477 operator()(_Iter __first, _Sent __last,
2478 _Pred __pred, _Proj __proj = {})
const
2480 auto __len = ranges::distance(__first, __last);
2484 auto __half = __len / 2;
2485 auto __middle = __first;
2486 ranges::advance(__middle, __half);
2491 __len = __len - __half - 1;
2499 template<forward_range _Range,
typename _Proj = identity,
2500 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
2502 constexpr borrowed_iterator_t<_Range>
2503 operator()(_Range&& __r, _Pred __pred, _Proj __proj = {})
const
2505 return (*
this)(ranges::begin(__r), ranges::end(__r),
2510 inline constexpr __partition_point_fn partition_point{};
2512 template<
typename _Iter1,
typename _Iter2,
typename _Out>
2513 using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2517 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2518 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2519 weakly_incrementable _Out,
typename _Comp = ranges::less,
2520 typename _Proj1 =
identity,
typename _Proj2 =
identity>
2521 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2522 constexpr merge_result<_Iter1, _Iter2, _Out>
2523 operator()(_Iter1 __first1, _Sent1 __last1,
2524 _Iter2 __first2, _Sent2 __last2, _Out __result,
2526 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2528 while (__first1 != __last1 && __first2 != __last2)
2534 *__result = *__first2;
2539 *__result = *__first1;
2552 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2553 typename _Comp = ranges::less,
2554 typename _Proj1 = identity,
typename _Proj2 = identity>
2555 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2556 _Comp, _Proj1, _Proj2>
2557 constexpr merge_result<borrowed_iterator_t<_Range1>,
2558 borrowed_iterator_t<_Range2>,
2560 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2562 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2564 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2565 ranges::begin(__r2), ranges::end(__r2),
2571 inline constexpr __merge_fn merge{};
2573 struct __inplace_merge_fn
2575 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
2576 typename _Comp = ranges::less,
2577 typename _Proj =
identity>
2578 requires sortable<_Iter, _Comp, _Proj>
2580 operator()(_Iter __first, _Iter __middle, _Sent __last,
2581 _Comp __comp = {}, _Proj __proj = {})
const
2583 auto __lasti = ranges::next(__first, __last);
2585 __detail::__make_comp_proj(__comp, __proj));
2589 template<bidirectional_range _Range,
2590 typename _Comp = ranges::less,
typename _Proj = identity>
2591 requires sortable<iterator_t<_Range>, _Comp, _Proj>
2592 borrowed_iterator_t<_Range>
2593 operator()(_Range&& __r, iterator_t<_Range> __middle,
2594 _Comp __comp = {}, _Proj __proj = {})
const
2596 return (*
this)(ranges::begin(__r),
std::move(__middle),
2602 inline constexpr __inplace_merge_fn inplace_merge{};
2604 struct __includes_fn
2606 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2607 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2608 typename _Proj1 =
identity,
typename _Proj2 =
identity,
2609 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
2610 projected<_Iter2, _Proj2>>
2611 _Comp = ranges::less>
2613 operator()(_Iter1 __first1, _Sent1 __last1,
2614 _Iter2 __first2, _Sent2 __last2,
2616 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2618 while (__first1 != __last1 && __first2 != __last2)
2633 return __first2 == __last2;
2636 template<input_range _Range1, input_range _Range2,
2637 typename _Proj1 = identity,
typename _Proj2 = identity,
2638 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
2639 projected<iterator_t<_Range2>, _Proj2>>
2640 _Comp = ranges::less>
2642 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
2643 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2645 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2646 ranges::begin(__r2), ranges::end(__r2),
2652 inline constexpr __includes_fn includes{};
2654 template<
typename _Iter1,
typename _Iter2,
typename _Out>
2655 using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2657 struct __set_union_fn
2659 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2660 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2661 weakly_incrementable _Out,
typename _Comp = ranges::less,
2662 typename _Proj1 =
identity,
typename _Proj2 =
identity>
2663 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2664 constexpr set_union_result<_Iter1, _Iter2, _Out>
2665 operator()(_Iter1 __first1, _Sent1 __last1,
2666 _Iter2 __first2, _Sent2 __last2,
2667 _Out __result, _Comp __comp = {},
2668 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2670 while (__first1 != __last1 && __first2 != __last2)
2676 *__result = *__first1;
2683 *__result = *__first2;
2688 *__result = *__first1;
2702 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2703 typename _Comp = ranges::less,
2704 typename _Proj1 = identity,
typename _Proj2 = identity>
2705 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2706 _Comp, _Proj1, _Proj2>
2707 constexpr set_union_result<borrowed_iterator_t<_Range1>,
2708 borrowed_iterator_t<_Range2>, _Out>
2709 operator()(_Range1&& __r1, _Range2&& __r2,
2710 _Out __result, _Comp __comp = {},
2711 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2713 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2714 ranges::begin(__r2), ranges::end(__r2),
2720 inline constexpr __set_union_fn set_union{};
2722 template<
typename _Iter1,
typename _Iter2,
typename _Out>
2723 using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
2725 struct __set_intersection_fn
2727 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2728 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2729 weakly_incrementable _Out,
typename _Comp = ranges::less,
2730 typename _Proj1 =
identity,
typename _Proj2 =
identity>
2731 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2732 constexpr set_intersection_result<_Iter1, _Iter2, _Out>
2733 operator()(_Iter1 __first1, _Sent1 __last1,
2734 _Iter2 __first2, _Sent2 __last2, _Out __result,
2736 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2738 while (__first1 != __last1 && __first2 != __last2)
2749 *__result = *__first1;
2760 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2761 typename _Comp = ranges::less,
2762 typename _Proj1 = identity,
typename _Proj2 = identity>
2763 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2764 _Comp, _Proj1, _Proj2>
2765 constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
2766 borrowed_iterator_t<_Range2>, _Out>
2767 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2769 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2771 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2772 ranges::begin(__r2), ranges::end(__r2),
2778 inline constexpr __set_intersection_fn set_intersection{};
2780 template<
typename _Iter,
typename _Out>
2781 using set_difference_result = in_out_result<_Iter, _Out>;
2783 struct __set_difference_fn
2785 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2786 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2787 weakly_incrementable _Out,
typename _Comp = ranges::less,
2788 typename _Proj1 =
identity,
typename _Proj2 =
identity>
2789 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2790 constexpr set_difference_result<_Iter1, _Out>
2791 operator()(_Iter1 __first1, _Sent1 __last1,
2792 _Iter2 __first2, _Sent2 __last2, _Out __result,
2794 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2796 while (__first1 != __last1 && __first2 != __last2)
2801 *__result = *__first1;
2818 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2819 typename _Comp = ranges::less,
2820 typename _Proj1 = identity,
typename _Proj2 = identity>
2821 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2822 _Comp, _Proj1, _Proj2>
2823 constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
2824 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2826 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2828 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2829 ranges::begin(__r2), ranges::end(__r2),
2835 inline constexpr __set_difference_fn set_difference{};
2837 template<
typename _Iter1,
typename _Iter2,
typename _Out>
2838 using set_symmetric_difference_result
2839 = in_in_out_result<_Iter1, _Iter2, _Out>;
2841 struct __set_symmetric_difference_fn
2843 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
2844 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
2845 weakly_incrementable _Out,
typename _Comp = ranges::less,
2846 typename _Proj1 =
identity,
typename _Proj2 =
identity>
2847 requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
2848 constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
2849 operator()(_Iter1 __first1, _Sent1 __last1,
2850 _Iter2 __first2, _Sent2 __last2,
2851 _Out __result, _Comp __comp = {},
2852 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2854 while (__first1 != __last1 && __first2 != __last2)
2859 *__result = *__first1;
2867 *__result = *__first2;
2884 template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
2885 typename _Comp = ranges::less,
2886 typename _Proj1 = identity,
typename _Proj2 = identity>
2887 requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
2888 _Comp, _Proj1, _Proj2>
2889 constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
2890 borrowed_iterator_t<_Range2>,
2892 operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
2894 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
2896 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
2897 ranges::begin(__r2), ranges::end(__r2),
2903 inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
2907 template<
typename _Tp,
typename _Proj = identity,
2908 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2909 _Comp = ranges::less>
2910 constexpr const _Tp&
2911 operator()(
const _Tp& __a,
const _Tp& __b,
2912 _Comp __comp = {}, _Proj __proj = {})
const
2922 template<input_range _Range,
typename _Proj = identity,
2923 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2924 _Comp = ranges::less>
2925 requires indirectly_copyable_storable<iterator_t<_Range>,
2926 range_value_t<_Range>*>
2927 constexpr range_value_t<_Range>
2928 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
2930 auto __first = ranges::begin(__r);
2931 auto __last = ranges::end(__r);
2932 __glibcxx_assert(__first != __last);
2933 auto __result = *__first;
2934 while (++__first != __last)
2936 auto __tmp = *__first;
2945 template<copyable _Tp,
typename _Proj = identity,
2946 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2947 _Comp = ranges::less>
2949 operator()(initializer_list<_Tp> __r,
2950 _Comp __comp = {}, _Proj __proj = {})
const
2952 return (*
this)(ranges::subrange(__r),
2957 inline constexpr __min_fn
min{};
2961 template<
typename _Tp,
typename _Proj = identity,
2962 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
2963 _Comp = ranges::less>
2964 constexpr const _Tp&
2965 operator()(
const _Tp& __a,
const _Tp& __b,
2966 _Comp __comp = {}, _Proj __proj = {})
const
2976 template<input_range _Range,
typename _Proj = identity,
2977 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
2978 _Comp = ranges::less>
2979 requires indirectly_copyable_storable<iterator_t<_Range>,
2980 range_value_t<_Range>*>
2981 constexpr range_value_t<_Range>
2982 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
2984 auto __first = ranges::begin(__r);
2985 auto __last = ranges::end(__r);
2986 __glibcxx_assert(__first != __last);
2987 auto __result = *__first;
2988 while (++__first != __last)
2990 auto __tmp = *__first;
2999 template<copyable _Tp,
typename _Proj = identity,
3000 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3001 _Comp = ranges::less>
3003 operator()(initializer_list<_Tp> __r,
3004 _Comp __comp = {}, _Proj __proj = {})
const
3006 return (*
this)(ranges::subrange(__r),
3011 inline constexpr __max_fn
max{};
3015 template<
typename _Tp,
typename _Proj = identity,
3016 indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
3018 constexpr const _Tp&
3019 operator()(
const _Tp& __val,
const _Tp& __lo,
const _Tp& __hi,
3020 _Comp __comp = {}, _Proj __proj = {})
const
3035 inline constexpr __clamp_fn
clamp{};
3037 template<
typename _Tp>
3038 struct min_max_result
3040 [[no_unique_address]] _Tp
min;
3041 [[no_unique_address]] _Tp
max;
3043 template<
typename _Tp2>
3044 requires convertible_to<const _Tp&, _Tp2>
3046 operator min_max_result<_Tp2>() const &
3049 template<
typename _Tp2>
3050 requires convertible_to<_Tp, _Tp2>
3052 operator min_max_result<_Tp2>() &&
3056 template<
typename _Tp>
3057 using minmax_result = min_max_result<_Tp>;
3061 template<
typename _Tp,
typename _Proj = identity,
3062 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3063 _Comp = ranges::less>
3064 constexpr minmax_result<const _Tp&>
3065 operator()(
const _Tp& __a,
const _Tp& __b,
3066 _Comp __comp = {}, _Proj __proj = {})
const
3076 template<input_range _Range,
typename _Proj = identity,
3077 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3078 _Comp = ranges::less>
3079 requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
3080 constexpr minmax_result<range_value_t<_Range>>
3081 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3083 auto __first = ranges::begin(__r);
3084 auto __last = ranges::end(__r);
3085 __glibcxx_assert(__first != __last);
3086 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3087 minmax_result<range_value_t<_Range>> __result = {*__first, *__first};
3088 if (++__first == __last)
3094 auto&& __val = *__first;
3095 if (__comp_proj(__val, __result.min))
3096 __result.min = std::forward<decltype(__val)>(__val);
3098 __result.max = std::forward<decltype(__val)>(__val);
3100 while (++__first != __last)
3105 range_value_t<_Range> __val1 = *__first;
3106 if (++__first == __last)
3111 if (__comp_proj(__val1, __result.min))
3113 else if (!__comp_proj(__val1, __result.max))
3117 auto&& __val2 = *__first;
3118 if (!__comp_proj(__val2, __val1))
3120 if (__comp_proj(__val1, __result.min))
3122 if (!__comp_proj(__val2, __result.max))
3123 __result.max = std::forward<decltype(__val2)>(__val2);
3127 if (__comp_proj(__val2, __result.min))
3128 __result.min = std::forward<decltype(__val2)>(__val2);
3129 if (!__comp_proj(__val1, __result.max))
3136 template<copyable _Tp,
typename _Proj = identity,
3137 indirect_strict_weak_order<projected<const _Tp*, _Proj>>
3138 _Comp = ranges::less>
3139 constexpr minmax_result<_Tp>
3140 operator()(initializer_list<_Tp> __r,
3141 _Comp __comp = {}, _Proj __proj = {})
const
3143 return (*
this)(ranges::subrange(__r),
3148 inline constexpr __minmax_fn
minmax{};
3150 struct __min_element_fn
3152 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3153 typename _Proj =
identity,
3154 indirect_strict_weak_order<projected<_Iter, _Proj>>
3155 _Comp = ranges::less>
3157 operator()(_Iter __first, _Sent __last,
3158 _Comp __comp = {}, _Proj __proj = {})
const
3160 if (__first == __last)
3164 while (++__i != __last)
3174 template<forward_range _Range,
typename _Proj = identity,
3175 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3176 _Comp = ranges::less>
3177 constexpr borrowed_iterator_t<_Range>
3178 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3180 return (*
this)(ranges::begin(__r), ranges::end(__r),
3185 inline constexpr __min_element_fn min_element{};
3187 struct __max_element_fn
3189 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3190 typename _Proj =
identity,
3191 indirect_strict_weak_order<projected<_Iter, _Proj>>
3192 _Comp = ranges::less>
3194 operator()(_Iter __first, _Sent __last,
3195 _Comp __comp = {}, _Proj __proj = {})
const
3197 if (__first == __last)
3201 while (++__i != __last)
3211 template<forward_range _Range,
typename _Proj = identity,
3212 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3213 _Comp = ranges::less>
3214 constexpr borrowed_iterator_t<_Range>
3215 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3217 return (*
this)(ranges::begin(__r), ranges::end(__r),
3222 inline constexpr __max_element_fn max_element{};
3224 template<
typename _Iter>
3225 using minmax_element_result = min_max_result<_Iter>;
3227 struct __minmax_element_fn
3229 template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
3230 typename _Proj =
identity,
3231 indirect_strict_weak_order<projected<_Iter, _Proj>>
3232 _Comp = ranges::less>
3233 constexpr minmax_element_result<_Iter>
3234 operator()(_Iter __first, _Sent __last,
3235 _Comp __comp = {}, _Proj __proj = {})
const
3237 auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
3238 minmax_element_result<_Iter> __result = {__first, __first};
3239 if (__first == __last || ++__first == __last)
3245 if (__comp_proj(*__first, *__result.min))
3246 __result.min = __first;
3248 __result.max = __first;
3250 while (++__first != __last)
3255 auto __prev = __first;
3256 if (++__first == __last)
3261 if (__comp_proj(*__prev, *__result.min))
3262 __result.min = __prev;
3263 else if (!__comp_proj(*__prev, *__result.max))
3264 __result.max = __prev;
3267 if (!__comp_proj(*__first, *__prev))
3269 if (__comp_proj(*__prev, *__result.min))
3270 __result.min = __prev;
3271 if (!__comp_proj(*__first, *__result.max))
3272 __result.max = __first;
3276 if (__comp_proj(*__first, *__result.min))
3277 __result.min = __first;
3278 if (!__comp_proj(*__prev, *__result.max))
3279 __result.max = __prev;
3285 template<forward_range _Range,
typename _Proj = identity,
3286 indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
3287 _Comp = ranges::less>
3288 constexpr minmax_element_result<borrowed_iterator_t<_Range>>
3289 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3291 return (*
this)(ranges::begin(__r), ranges::end(__r),
3296 inline constexpr __minmax_element_fn minmax_element{};
3298 struct __lexicographical_compare_fn
3300 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
3301 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
3302 typename _Proj1 =
identity,
typename _Proj2 =
identity,
3303 indirect_strict_weak_order<projected<_Iter1, _Proj1>,
3304 projected<_Iter2, _Proj2>>
3305 _Comp = ranges::less>
3307 operator()(_Iter1 __first1, _Sent1 __last1,
3308 _Iter2 __first2, _Sent2 __last2,
3310 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
3312 if constexpr (__detail::__is_normal_iterator<_Iter1>
3313 && same_as<_Iter1, _Sent1>)
3314 return (*
this)(__first1.base(), __last1.base(),
3318 else if constexpr (__detail::__is_normal_iterator<_Iter2>
3319 && same_as<_Iter2, _Sent2>)
3321 __first2.base(), __last2.base(),
3326 constexpr bool __sized_iters
3327 = (sized_sentinel_for<_Sent1, _Iter1>
3328 && sized_sentinel_for<_Sent2, _Iter2>);
3329 if constexpr (__sized_iters)
3331 using _ValueType1 = iter_value_t<_Iter1>;
3332 using _ValueType2 = iter_value_t<_Iter2>;
3335 constexpr bool __use_memcmp
3336 = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
3337 && __ptr_to_nonvolatile<_Iter1>
3338 && __ptr_to_nonvolatile<_Iter2>
3339 && (is_same_v<_Comp, ranges::less>
3340 || is_same_v<_Comp, ranges::greater>)
3341 && is_same_v<_Proj1, identity>
3342 && is_same_v<_Proj2, identity>);
3343 if constexpr (__use_memcmp)
3345 const auto __d1 = __last1 - __first1;
3346 const auto __d2 = __last2 - __first2;
3348 if (
const auto __len =
std::min(__d1, __d2))
3351 = std::__memcmp(__first1, __first2, __len);
3352 if constexpr (is_same_v<_Comp, ranges::less>)
3359 else if constexpr (is_same_v<_Comp, ranges::greater>)
3371 for (; __first1 != __last1 && __first2 != __last2;
3372 ++__first1, (void) ++__first2)
3383 return __first1 == __last1 && __first2 != __last2;
3387 template<input_range _Range1, input_range _Range2,
3388 typename _Proj1 = identity,
typename _Proj2 = identity,
3389 indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
3390 projected<iterator_t<_Range2>, _Proj2>>
3391 _Comp = ranges::less>
3393 operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
3394 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
3396 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
3397 ranges::begin(__r2), ranges::end(__r2),
3403 template<
typename _Iter,
typename _Ref = iter_reference_t<_Iter>>
3404 static constexpr bool __ptr_to_nonvolatile
3405 = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
3408 inline constexpr __lexicographical_compare_fn lexicographical_compare;
3410 template<
typename _Iter>
3411 struct in_found_result
3413 [[no_unique_address]] _Iter in;
3416 template<
typename _Iter2>
3417 requires convertible_to<const _Iter&, _Iter2>
3419 operator in_found_result<_Iter2>() const &
3420 {
return {in, found}; }
3422 template<
typename _Iter2>
3423 requires convertible_to<_Iter, _Iter2>
3425 operator in_found_result<_Iter2>() &&
3429 template<
typename _Iter>
3430 using next_permutation_result = in_found_result<_Iter>;
3432 struct __next_permutation_fn
3434 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3435 typename _Comp = ranges::less,
typename _Proj =
identity>
3436 requires sortable<_Iter, _Comp, _Proj>
3437 constexpr next_permutation_result<_Iter>
3438 operator()(_Iter __first, _Sent __last,
3439 _Comp __comp = {}, _Proj __proj = {})
const
3441 if (__first == __last)
3449 auto __lasti = ranges::next(__first, __last);
3466 ranges::iter_swap(__i, __j);
3467 ranges::reverse(__ii, __last);
3472 ranges::reverse(__first, __last);
3478 template<bidirectional_range _Range,
typename _Comp = ranges::less,
3479 typename _Proj = identity>
3480 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3481 constexpr next_permutation_result<borrowed_iterator_t<_Range>>
3482 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3484 return (*
this)(ranges::begin(__r), ranges::end(__r),
3489 inline constexpr __next_permutation_fn next_permutation{};
3491 template<
typename _Iter>
3492 using prev_permutation_result = in_found_result<_Iter>;
3494 struct __prev_permutation_fn
3496 template<b
idirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
3497 typename _Comp = ranges::less,
typename _Proj =
identity>
3498 requires sortable<_Iter, _Comp, _Proj>
3499 constexpr prev_permutation_result<_Iter>
3500 operator()(_Iter __first, _Sent __last,
3501 _Comp __comp = {}, _Proj __proj = {})
const
3503 if (__first == __last)
3511 auto __lasti = ranges::next(__first, __last);
3528 ranges::iter_swap(__i, __j);
3529 ranges::reverse(__ii, __last);
3534 ranges::reverse(__first, __last);
3540 template<bidirectional_range _Range,
typename _Comp = ranges::less,
3541 typename _Proj = identity>
3542 requires sortable<iterator_t<_Range>, _Comp, _Proj>
3543 constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
3544 operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {})
const
3546 return (*
this)(ranges::begin(__r), ranges::end(__r),
3551 inline constexpr __prev_permutation_fn prev_permutation{};
3555#define __cpp_lib_shift 201806L
3556 template<
typename _ForwardIterator>
3557 constexpr _ForwardIterator
3558 shift_left(_ForwardIterator __first, _ForwardIterator __last,
3559 typename iterator_traits<_ForwardIterator>::difference_type __n)
3561 __glibcxx_assert(__n >= 0);
3565 auto __mid = ranges::next(__first, __n, __last);
3566 if (__mid == __last)
3571 template<
typename _ForwardIterator>
3572 constexpr _ForwardIterator
3573 shift_right(_ForwardIterator __first, _ForwardIterator __last,
3574 typename iterator_traits<_ForwardIterator>::difference_type __n)
3576 __glibcxx_assert(__n >= 0);
3581 =
typename iterator_traits<_ForwardIterator>::iterator_category;
3582 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
3584 auto __mid = ranges::next(__last, -__n, __first);
3585 if (__mid == __first)
3593 auto __result = ranges::next(__first, __n, __last);
3594 if (__result == __last)
3597 auto __dest_head = __first, __dest_tail = __result;
3598 while (__dest_head != __result)
3600 if (__dest_tail == __last)
3624 auto __cursor = __first;
3625 while (__cursor != __result)
3627 if (__dest_tail == __last)
3632 __dest_head =
std::move(__cursor, __result,
3638 std::iter_swap(__cursor, __dest_head);
3647_GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
constexpr _InputIterator for_each_n(_InputIterator __first, _Size __n, _Function __f)
Apply a function to every element of a sequence.
constexpr const _Tp & clamp(const _Tp &, const _Tp &, const _Tp &)
Returns the value clamped between lo and hi.
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr pair< const _Tp &, const _Tp & > minmax(const _Tp &, const _Tp &)
Determines min and max at once as an ordered pair.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
_SampleIterator sample(_PopulationIterator __first, _PopulationIterator __last, _SampleIterator __out, _Distance __n, _UniformRandomBitGenerator &&__g)
Take a random sample from a population.