30#ifndef _RANGES_ALGOBASE_H
31#define _RANGES_ALGOBASE_H 1
33#if __cplusplus > 201703L
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
49 template<
typename _Tp>
50 constexpr inline bool __is_normal_iterator =
false;
52 template<
typename _Iterator,
typename _Container>
54 __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
57 template<
typename _Tp>
58 constexpr inline bool __is_reverse_iterator =
false;
60 template<
typename _Iterator>
62 __is_reverse_iterator<reverse_iterator<_Iterator>> =
true;
64 template<
typename _Tp>
65 constexpr inline bool __is_move_iterator =
false;
67 template<
typename _Iterator>
69 __is_move_iterator<move_iterator<_Iterator>> =
true;
74 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
75 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
76 typename _Pred = ranges::equal_to,
77 typename _Proj1 =
identity,
typename _Proj2 =
identity>
78 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
80 operator()(_Iter1 __first1, _Sent1 __last1,
81 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
82 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
86 if constexpr (__detail::__is_normal_iterator<_Iter1>
87 && same_as<_Iter1, _Sent1>)
88 return (*
this)(__first1.base(), __last1.base(),
92 else if constexpr (__detail::__is_normal_iterator<_Iter2>
93 && same_as<_Iter2, _Sent2>)
95 __first2.base(), __last2.base(),
98 else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
99 && sized_sentinel_for<_Sent2, _Iter2>)
101 auto __d1 = ranges::distance(__first1, __last1);
102 auto __d2 = ranges::distance(__first2, __last2);
106 using _ValueType1 = iter_value_t<_Iter1>;
107 constexpr bool __use_memcmp
108 = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
109 && __memcmpable<_Iter1, _Iter2>::__value
110 && is_same_v<_Pred, ranges::equal_to>
111 && is_same_v<_Proj1, identity>
112 && is_same_v<_Proj2, identity>);
113 if constexpr (__use_memcmp)
115 if (
const size_t __len = (__last1 - __first1))
116 return !std::__memcmp(__first1, __first2, __len);
121 for (; __first1 != __last1; ++__first1, (void)++__first2)
131 for (; __first1 != __last1 && __first2 != __last2;
132 ++__first1, (void)++__first2)
137 return __first1 == __last1 && __first2 == __last2;
141 template<input_range _Range1, input_range _Range2,
142 typename _Pred = ranges::equal_to,
143 typename _Proj1 = identity,
typename _Proj2 = identity>
144 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
145 _Pred, _Proj1, _Proj2>
147 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
148 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
150 return (*
this)(ranges::begin(__r1), ranges::end(__r1),
151 ranges::begin(__r2), ranges::end(__r2),
157 inline constexpr __equal_fn equal{};
159 template<
typename _Iter,
typename _Out>
162 [[no_unique_address]] _Iter in;
163 [[no_unique_address]] _Out out;
165 template<
typename _Iter2,
typename _Out2>
166 requires convertible_to<const _Iter&, _Iter2>
167 && convertible_to<const _Out&, _Out2>
169 operator in_out_result<_Iter2, _Out2>() const &
170 {
return {in, out}; }
172 template<
typename _Iter2,
typename _Out2>
173 requires convertible_to<_Iter, _Iter2>
174 && convertible_to<_Out, _Out2>
176 operator in_out_result<_Iter2, _Out2>() &&
180 template<
typename _Iter,
typename _Out>
181 using copy_result = in_out_result<_Iter, _Out>;
183 template<
typename _Iter,
typename _Out>
184 using move_result = in_out_result<_Iter, _Out>;
186 template<
typename _Iter1,
typename _Iter2>
187 using move_backward_result = in_out_result<_Iter1, _Iter2>;
189 template<
typename _Iter1,
typename _Iter2>
190 using copy_backward_result = in_out_result<_Iter1, _Iter2>;
192 template<
bool _IsMove,
193 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
194 bidirectional_iterator _Out>
196 ? indirectly_movable<_Iter, _Out>
197 : indirectly_copyable<_Iter, _Out>)
198 constexpr conditional_t<_IsMove,
199 move_backward_result<_Iter, _Out>,
200 copy_backward_result<_Iter, _Out>>
201 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
203 template<
bool _IsMove,
204 input_iterator _Iter, sentinel_for<_Iter> _Sent,
205 weakly_incrementable _Out>
207 ? indirectly_movable<_Iter, _Out>
208 : indirectly_copyable<_Iter, _Out>)
209 constexpr conditional_t<_IsMove,
210 move_result<_Iter, _Out>,
211 copy_result<_Iter, _Out>>
212 __copy_or_move(_Iter __first, _Sent __last, _Out __result)
216 using __detail::__is_move_iterator;
217 using __detail::__is_reverse_iterator;
218 using __detail::__is_normal_iterator;
219 if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
222 = ranges::__copy_or_move<true>(
std::move(__first).base(),
227 else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
228 && __is_reverse_iterator<_Out>)
231 = ranges::__copy_or_move_backward<_IsMove>(
std::move(__last).base(),
234 return {reverse_iterator{
std::move(__in)},
237 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
240 = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
242 return {
decltype(__first){__in},
std::move(__out)};
244 else if constexpr (__is_normal_iterator<_Out>)
247 = ranges::__copy_or_move<_IsMove>(
std::move(__first), __last, __result.base());
248 return {
std::move(__in),
decltype(__result){__out}};
250 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
252#ifdef __cpp_lib_is_constant_evaluated
253 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#ifdef __cpp_lib_is_constant_evaluated
390 if (!std::is_constant_evaluated())
393 if constexpr (__memcpyable<_Out, _Iter>::__value)
395 using _ValueTypeI = iter_value_t<_Iter>;
396 static_assert(_IsMove
397 ? is_move_assignable_v<_ValueTypeI>
398 : is_copy_assignable_v<_ValueTypeI>);
399 auto __num = __last - __first;
401 __builtin_memmove(__result - __num, __first,
402 sizeof(_ValueTypeI) * __num);
403 return {__first + __num, __result - __num};
407 auto __lasti = ranges::next(__first, __last);
408 auto __tail = __lasti;
410 for (
auto __n = __last - __first; __n > 0; --__n)
414 if constexpr (_IsMove)
423 auto __lasti = ranges::next(__first, __last);
424 auto __tail = __lasti;
426 while (__first != __tail)
430 if constexpr (_IsMove)
439 struct __copy_backward_fn
441 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
442 b
idirectional_iterator _Iter2>
443 requires indirectly_copyable<_Iter1, _Iter2>
444 constexpr copy_backward_result<_Iter1, _Iter2>
445 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
447 return ranges::__copy_or_move_backward<false>(
std::move(__first),
452 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
453 requires indirectly_copyable<iterator_t<_Range>, _Iter>
454 constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
455 operator()(_Range&& __r, _Iter __result)
const
457 return (*
this)(ranges::begin(__r), ranges::end(__r),
462 inline constexpr __copy_backward_fn copy_backward{};
464 struct __move_backward_fn
466 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
467 b
idirectional_iterator _Iter2>
468 requires indirectly_movable<_Iter1, _Iter2>
469 constexpr move_backward_result<_Iter1, _Iter2>
470 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
472 return ranges::__copy_or_move_backward<true>(
std::move(__first),
477 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
478 requires indirectly_movable<iterator_t<_Range>, _Iter>
479 constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
480 operator()(_Range&& __r, _Iter __result)
const
482 return (*
this)(ranges::begin(__r), ranges::end(__r),
489 template<
typename _Iter,
typename _Out>
490 using copy_n_result = in_out_result<_Iter, _Out>;
494 template<input_iterator _Iter, weakly_incrementable _Out>
495 requires indirectly_copyable<_Iter, _Out>
496 constexpr copy_n_result<_Iter, _Out>
497 operator()(_Iter __first, iter_difference_t<_Iter> __n,
500 if constexpr (random_access_iterator<_Iter>)
503 return ranges::copy(__first, __first + __n,
std::move(__result));
507 for (; __n > 0; --__n, (void)++__result, (
void)++__first)
508 *__result = *__first;
514 inline constexpr __copy_n_fn copy_n{};
518 template<
typename _Tp, output_iterator<const _Tp&> _Out>
520 operator()(_Out __first, iter_difference_t<_Out> __n,
521 const _Tp& __value)
const
528 if constexpr (is_scalar_v<_Tp>)
531 if constexpr (is_pointer_v<_Out>
533 && __is_byte<remove_pointer_t<_Out>>::__value
536#ifdef __cpp_lib_is_constant_evaluated
537 if (!std::is_constant_evaluated())
540 __builtin_memset(__first,
541 static_cast<unsigned char>(__value),
543 return __first + __n;
547 const auto __tmp = __value;
548 for (; __n > 0; --__n, (void)++__first)
554 for (; __n > 0; --__n, (void)++__first)
561 inline constexpr __fill_n_fn fill_n{};
565 template<
typename _Tp,
566 output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
568 operator()(_Out __first, _Sent __last,
const _Tp& __value)
const
572 if constexpr (sized_sentinel_for<_Sent, _Out>)
574 const auto __len = __last - __first;
575 return ranges::fill_n(__first, __len, __value);
577 else if constexpr (is_scalar_v<_Tp>)
579 const auto __tmp = __value;
580 for (; __first != __last; ++__first)
586 for (; __first != __last; ++__first)
592 template<
typename _Tp, output_range<const _Tp&> _Range>
593 constexpr borrowed_iterator_t<_Range>
594 operator()(_Range&& __r,
const _Tp& __value)
const
596 return (*
this)(ranges::begin(__r), ranges::end(__r), __value);
600 inline constexpr __fill_fn fill{};
602_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.
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
ISO C++ entities toplevel namespace is std.