30#ifndef _RANGES_ALGOBASE_H
31#define _RANGES_ALGOBASE_H 1
33#if __cplusplus > 201703L
44namespace std _GLIBCXX_VISIBILITY(default)
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
51 template<
typename _Tp>
52 constexpr inline bool __is_normal_iterator =
false;
54 template<
typename _Iterator,
typename _Container>
56 __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
59 template<
typename _Tp>
60 constexpr inline bool __is_reverse_iterator =
false;
62 template<
typename _Iterator>
64 __is_reverse_iterator<reverse_iterator<_Iterator>> =
true;
66 template<
typename _Tp>
67 constexpr inline bool __is_move_iterator =
false;
69 template<
typename _Iterator>
71 __is_move_iterator<move_iterator<_Iterator>> =
true;
76 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
77 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
78 typename _Pred = ranges::equal_to,
79 typename _Proj1 =
identity,
typename _Proj2 =
identity>
80 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
82 operator()(_Iter1 __first1, _Sent1 __last1,
83 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
84 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
88 if constexpr (__detail::__is_normal_iterator<_Iter1>
89 && same_as<_Iter1, _Sent1>)
90 return (*
this)(__first1.base(), __last1.base(),
94 else if constexpr (__detail::__is_normal_iterator<_Iter2>
95 && same_as<_Iter2, _Sent2>)
97 __first2.base(), __last2.base(),
100 else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
101 && sized_sentinel_for<_Sent2, _Iter2>)
103 auto __d1 = ranges::distance(__first1, __last1);
104 auto __d2 = ranges::distance(__first2, __last2);
108 using _ValueType1 = iter_value_t<_Iter1>;
109 constexpr bool __use_memcmp
110 = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
111 && __memcmpable<_Iter1, _Iter2>::__value
112 && is_same_v<_Pred, ranges::equal_to>
113 && is_same_v<_Proj1, identity>
114 && is_same_v<_Proj2, identity>);
115 if constexpr (__use_memcmp)
117 if (
const size_t __len = (__last1 - __first1))
118 return !std::__memcmp(__first1, __first2, __len);
123 for (; __first1 != __last1; ++__first1, (void)++__first2)
133 for (; __first1 != __last1 && __first2 != __last2;
134 ++__first1, (void)++__first2)
139 return __first1 == __last1 && __first2 == __last2;
143 template<input_range _Range1, input_range _Range2,
144 typename _Pred = ranges::equal_to,
145 typename _Proj1 = identity,
typename _Proj2 = identity>
146 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
147 _Pred, _Proj1, _Proj2>
149 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
150 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
152 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
153 ranges::begin(__r2), ranges::end(__r2),
159 inline constexpr __equal_fn equal{};
161 template<
typename _Iter,
typename _Out>
164 [[no_unique_address]] _Iter in;
165 [[no_unique_address]] _Out out;
167 template<
typename _Iter2,
typename _Out2>
168 requires convertible_to<const _Iter&, _Iter2>
169 && convertible_to<const _Out&, _Out2>
171 operator in_out_result<_Iter2, _Out2>() const &
172 {
return {in, out}; }
174 template<
typename _Iter2,
typename _Out2>
175 requires convertible_to<_Iter, _Iter2>
176 && convertible_to<_Out, _Out2>
178 operator in_out_result<_Iter2, _Out2>() &&
182 template<
typename _Iter,
typename _Out>
183 using copy_result = in_out_result<_Iter, _Out>;
185 template<
typename _Iter,
typename _Out>
186 using move_result = in_out_result<_Iter, _Out>;
188 template<
typename _Iter1,
typename _Iter2>
189 using move_backward_result = in_out_result<_Iter1, _Iter2>;
191 template<
typename _Iter1,
typename _Iter2>
192 using copy_backward_result = in_out_result<_Iter1, _Iter2>;
194 template<
bool _IsMove,
195 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
196 bidirectional_iterator _Out>
198 ? indirectly_movable<_Iter, _Out>
199 : indirectly_copyable<_Iter, _Out>)
200 constexpr __conditional_t<_IsMove,
201 move_backward_result<_Iter, _Out>,
202 copy_backward_result<_Iter, _Out>>
203 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
205 template<
bool _IsMove,
206 input_iterator _Iter, sentinel_for<_Iter> _Sent,
207 weakly_incrementable _Out>
209 ? indirectly_movable<_Iter, _Out>
210 : indirectly_copyable<_Iter, _Out>)
211 constexpr __conditional_t<_IsMove,
212 move_result<_Iter, _Out>,
213 copy_result<_Iter, _Out>>
214 __copy_or_move(_Iter __first, _Sent __last, _Out __result)
218 using __detail::__is_move_iterator;
219 using __detail::__is_reverse_iterator;
220 using __detail::__is_normal_iterator;
221 if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
224 = ranges::__copy_or_move<true>(
std::move(__first).base(),
229 else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
230 && __is_reverse_iterator<_Out>)
233 = ranges::__copy_or_move_backward<_IsMove>(
std::move(__last).base(),
236 return {reverse_iterator{
std::move(__in)},
239 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
242 = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
244 return {
decltype(__first){__in},
std::move(__out)};
246 else if constexpr (__is_normal_iterator<_Out>)
249 = ranges::__copy_or_move<_IsMove>(
std::move(__first), __last, __result.base());
250 return {
std::move(__in),
decltype(__result){__out}};
252 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
254 if (!std::__is_constant_evaluated())
256 if constexpr (__memcpyable<_Iter, _Out>::__value)
258 using _ValueTypeI = iter_value_t<_Iter>;
259 static_assert(_IsMove
260 ? is_move_assignable_v<_ValueTypeI>
261 : is_copy_assignable_v<_ValueTypeI>);
262 auto __num = __last - __first;
264 __builtin_memmove(__result, __first,
265 sizeof(_ValueTypeI) * __num);
266 return {__first + __num, __result + __num};
270 for (
auto __n = __last - __first; __n > 0; --__n)
272 if constexpr (_IsMove)
275 *__result = *__first;
283 while (__first != __last)
285 if constexpr (_IsMove)
288 *__result = *__first;
298 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
299 weakly_incrementable _Out>
300 requires indirectly_copyable<_Iter, _Out>
301 constexpr copy_result<_Iter, _Out>
302 operator()(_Iter __first, _Sent __last, _Out __result)
const
304 return ranges::__copy_or_move<false>(
std::move(__first),
309 template<input_range _Range, weakly_incrementable _Out>
310 requires indirectly_copyable<iterator_t<_Range>, _Out>
311 constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
312 operator()(_Range&& __r, _Out __result)
const
314 return (*
this)(ranges::begin(__r), ranges::end(__r),
319 inline constexpr __copy_fn copy{};
323 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
324 weakly_incrementable _Out>
325 requires indirectly_movable<_Iter, _Out>
326 constexpr move_result<_Iter, _Out>
327 operator()(_Iter __first, _Sent __last, _Out __result)
const
329 return ranges::__copy_or_move<true>(
std::move(__first),
334 template<input_range _Range, weakly_incrementable _Out>
335 requires indirectly_movable<iterator_t<_Range>, _Out>
336 constexpr move_result<borrowed_iterator_t<_Range>, _Out>
337 operator()(_Range&& __r, _Out __result)
const
339 return (*
this)(ranges::begin(__r), ranges::end(__r),
344 inline constexpr __move_fn move{};
346 template<
bool _IsMove,
347 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
348 bidirectional_iterator _Out>
350 ? indirectly_movable<_Iter, _Out>
351 : indirectly_copyable<_Iter, _Out>)
352 constexpr __conditional_t<_IsMove,
353 move_backward_result<_Iter, _Out>,
354 copy_backward_result<_Iter, _Out>>
355 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
359 using __detail::__is_reverse_iterator;
360 using __detail::__is_normal_iterator;
361 if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
362 && __is_reverse_iterator<_Out>)
365 = ranges::__copy_or_move<_IsMove>(
std::move(__last).base(),
368 return {reverse_iterator{
std::move(__in)},
371 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
374 = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
377 return {
decltype(__first){__in},
std::move(__out)};
379 else if constexpr (__is_normal_iterator<_Out>)
382 = ranges::__copy_or_move_backward<_IsMove>(
std::move(__first),
385 return {
std::move(__in),
decltype(__result){__out}};
387 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
389 if (!std::__is_constant_evaluated())
391 if constexpr (__memcpyable<_Out, _Iter>::__value)
393 using _ValueTypeI = iter_value_t<_Iter>;
394 static_assert(_IsMove
395 ? is_move_assignable_v<_ValueTypeI>
396 : is_copy_assignable_v<_ValueTypeI>);
397 auto __num = __last - __first;
399 __builtin_memmove(__result - __num, __first,
400 sizeof(_ValueTypeI) * __num);
401 return {__first + __num, __result - __num};
405 auto __lasti = ranges::next(__first, __last);
406 auto __tail = __lasti;
408 for (
auto __n = __last - __first; __n > 0; --__n)
412 if constexpr (_IsMove)
421 auto __lasti = ranges::next(__first, __last);
422 auto __tail = __lasti;
424 while (__first != __tail)
428 if constexpr (_IsMove)
437 struct __copy_backward_fn
439 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
440 b
idirectional_iterator _Iter2>
441 requires indirectly_copyable<_Iter1, _Iter2>
442 constexpr copy_backward_result<_Iter1, _Iter2>
443 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
445 return ranges::__copy_or_move_backward<false>(
std::move(__first),
450 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
451 requires indirectly_copyable<iterator_t<_Range>, _Iter>
452 constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
453 operator()(_Range&& __r, _Iter __result)
const
455 return (*
this)(ranges::begin(__r), ranges::end(__r),
460 inline constexpr __copy_backward_fn copy_backward{};
462 struct __move_backward_fn
464 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
465 b
idirectional_iterator _Iter2>
466 requires indirectly_movable<_Iter1, _Iter2>
467 constexpr move_backward_result<_Iter1, _Iter2>
468 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
470 return ranges::__copy_or_move_backward<true>(
std::move(__first),
475 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
476 requires indirectly_movable<iterator_t<_Range>, _Iter>
477 constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
478 operator()(_Range&& __r, _Iter __result)
const
480 return (*
this)(ranges::begin(__r), ranges::end(__r),
485 inline constexpr __move_backward_fn move_backward{};
487 template<
typename _Iter,
typename _Out>
488 using copy_n_result = in_out_result<_Iter, _Out>;
492 template<input_iterator _Iter, weakly_incrementable _Out>
493 requires indirectly_copyable<_Iter, _Out>
494 constexpr copy_n_result<_Iter, _Out>
495 operator()(_Iter __first, iter_difference_t<_Iter> __n,
498 if constexpr (random_access_iterator<_Iter>)
501 return ranges::copy(__first, __first + __n,
std::move(__result));
505 for (; __n > 0; --__n, (void)++__result, (
void)++__first)
506 *__result = *__first;
512 inline constexpr __copy_n_fn copy_n{};
516 template<
typename _Tp, output_iterator<const _Tp&> _Out>
518 operator()(_Out __first, iter_difference_t<_Out> __n,
519 const _Tp& __value)
const
526 if constexpr (is_scalar_v<_Tp>)
529 if constexpr (is_pointer_v<_Out>
531 && __is_byte<remove_pointer_t<_Out>>::__value
534 if (!std::__is_constant_evaluated())
536 __builtin_memset(__first,
537 static_cast<unsigned char>(__value),
539 return __first + __n;
543 const auto __tmp = __value;
544 for (; __n > 0; --__n, (void)++__first)
550 for (; __n > 0; --__n, (void)++__first)
557 inline constexpr __fill_n_fn fill_n{};
561 template<
typename _Tp,
562 output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
564 operator()(_Out __first, _Sent __last,
const _Tp& __value)
const
568 if constexpr (sized_sentinel_for<_Sent, _Out>)
570 const auto __len = __last - __first;
571 return ranges::fill_n(__first, __len, __value);
573 else if constexpr (is_scalar_v<_Tp>)
575 const auto __tmp = __value;
576 for (; __first != __last; ++__first)
582 for (; __first != __last; ++__first)
588 template<
typename _Tp, output_range<const _Tp&> _Range>
589 constexpr borrowed_iterator_t<_Range>
590 operator()(_Range&& __r,
const _Tp& __value)
const
592 return (*
this)(ranges::begin(__r), ranges::end(__r), __value);
596 inline constexpr __fill_fn fill{};
598_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.
ISO C++ entities toplevel namespace is std.