30 #ifndef _RANGES_ALGOBASE_H
31 #define _RANGES_ALGOBASE_H 1
33 #if __cplusplus > 201703L
42 #if __cpp_lib_concepts
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 template<
typename _Tp>
51 constexpr
inline bool __is_normal_iterator =
false;
53 template<
typename _Iterator,
typename _Container>
55 __is_normal_iterator<__gnu_cxx::__normal_iterator<_Iterator,
58 template<
typename _Tp>
59 constexpr
inline bool __is_reverse_iterator =
false;
61 template<
typename _Iterator>
63 __is_reverse_iterator<reverse_iterator<_Iterator>> =
true;
65 template<
typename _Tp>
66 constexpr
inline bool __is_move_iterator =
false;
68 template<
typename _Iterator>
70 __is_move_iterator<move_iterator<_Iterator>> =
true;
75 template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
76 input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
77 typename _Pred = ranges::equal_to,
78 typename _Proj1 =
identity,
typename _Proj2 =
identity>
79 requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
81 operator()(_Iter1 __first1, _Sent1 __last1,
82 _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
83 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
87 if constexpr (__detail::__is_normal_iterator<_Iter1>
88 && same_as<_Iter1, _Sent1>)
89 return (*
this)(__first1.base(), __last1.base(),
93 else if constexpr (__detail::__is_normal_iterator<_Iter2>
94 && same_as<_Iter2, _Sent2>)
96 __first2.base(), __last2.base(),
99 else if constexpr (sized_sentinel_for<_Sent1, _Iter1>
100 && sized_sentinel_for<_Sent2, _Iter2>)
107 using _ValueType1 = iter_value_t<_Iter1>;
108 constexpr
bool __use_memcmp
109 = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
110 && __memcmpable<_Iter1, _Iter2>::__value
111 && is_same_v<_Pred, ranges::equal_to>
112 && is_same_v<_Proj1, identity>
113 && is_same_v<_Proj2, identity>);
114 if constexpr (__use_memcmp)
116 if (
const size_t __len = (__last1 - __first1))
117 return !std::__memcmp(__first1, __first2, __len);
122 for (; __first1 != __last1; ++__first1, (void)++__first2)
132 for (; __first1 != __last1 && __first2 != __last2;
133 ++__first1, (void)++__first2)
138 return __first1 == __last1 && __first2 == __last2;
142 template<input_range _Range1, input_range _Range2,
143 typename _Pred = ranges::equal_to,
144 typename _Proj1 = identity,
typename _Proj2 = identity>
145 requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
146 _Pred, _Proj1, _Proj2>
148 operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
149 _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
const
158 inline constexpr __equal_fn equal{};
160 template<
typename _Iter,
typename _Out>
163 [[no_unique_address]] _Iter in;
164 [[no_unique_address]] _Out out;
166 template<
typename _Iter2,
typename _Out2>
167 requires convertible_to<const _Iter&, _Iter2>
168 && convertible_to<const _Out&, _Out2>
170 operator in_out_result<_Iter2, _Out2>() const &
171 {
return {in, out}; }
173 template<
typename _Iter2,
typename _Out2>
174 requires convertible_to<_Iter, _Iter2>
175 && convertible_to<_Out, _Out2>
177 operator in_out_result<_Iter2, _Out2>() &&
181 template<
typename _Iter,
typename _Out>
182 using copy_result = in_out_result<_Iter, _Out>;
184 template<
typename _Iter,
typename _Out>
185 using move_result = in_out_result<_Iter, _Out>;
187 template<
typename _Iter1,
typename _Iter2>
188 using move_backward_result = in_out_result<_Iter1, _Iter2>;
190 template<
typename _Iter1,
typename _Iter2>
191 using copy_backward_result = in_out_result<_Iter1, _Iter2>;
193 template<
bool _IsMove,
194 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
195 bidirectional_iterator _Out>
197 ? indirectly_movable<_Iter, _Out>
198 : indirectly_copyable<_Iter, _Out>)
200 move_backward_result<_Iter, _Out>,
201 copy_backward_result<_Iter, _Out>>
202 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result);
204 template<
bool _IsMove,
205 input_iterator _Iter, sentinel_for<_Iter> _Sent,
206 weakly_incrementable _Out>
208 ? indirectly_movable<_Iter, _Out>
209 : indirectly_copyable<_Iter, _Out>)
211 move_result<_Iter, _Out>,
212 copy_result<_Iter, _Out>>
213 __copy_or_move(_Iter __first, _Sent __last, _Out __result)
217 using __detail::__is_move_iterator;
218 using __detail::__is_reverse_iterator;
219 using __detail::__is_normal_iterator;
220 if constexpr (__is_move_iterator<_Iter> && same_as<_Iter, _Sent>)
223 = ranges::__copy_or_move<true>(
std::move(__first).base(),
228 else if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
229 && __is_reverse_iterator<_Out>)
232 = ranges::__copy_or_move_backward<_IsMove>(
std::move(__last).base(),
235 return {reverse_iterator{
std::move(__in)},
238 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
241 = ranges::__copy_or_move<_IsMove>(__first.base(), __last.base(),
243 return {decltype(__first){__in},
std::move(__out)};
245 else if constexpr (__is_normal_iterator<_Out>)
248 = ranges::__copy_or_move<_IsMove>(
std::move(__first), __last, __result.base());
249 return {
std::move(__in), decltype(__result){__out}};
251 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
253 #ifdef __cpp_lib_is_constant_evaluated
254 if (!std::is_constant_evaluated())
257 if constexpr (__memcpyable<_Iter, _Out>::__value)
259 using _ValueTypeI = iter_value_t<_Iter>;
260 static_assert(_IsMove
261 ? is_move_assignable_v<_ValueTypeI>
262 : is_copy_assignable_v<_ValueTypeI>);
263 auto __num = __last - __first;
265 __builtin_memmove(__result, __first,
266 sizeof(_ValueTypeI) * __num);
267 return {__first + __num, __result + __num};
271 for (
auto __n = __last - __first; __n > 0; --__n)
273 if constexpr (_IsMove)
276 *__result = *__first;
284 while (__first != __last)
286 if constexpr (_IsMove)
289 *__result = *__first;
299 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
300 weakly_incrementable _Out>
301 requires indirectly_copyable<_Iter, _Out>
302 constexpr copy_result<_Iter, _Out>
303 operator()(_Iter __first, _Sent __last, _Out __result)
const
305 return ranges::__copy_or_move<false>(
std::move(__first),
310 template<input_range _Range, weakly_incrementable _Out>
311 requires indirectly_copyable<iterator_t<_Range>, _Out>
312 constexpr copy_result<borrowed_iterator_t<_Range>, _Out>
313 operator()(_Range&& __r, _Out __result)
const
320 inline constexpr __copy_fn copy{};
324 template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
325 weakly_incrementable _Out>
326 requires indirectly_movable<_Iter, _Out>
327 constexpr move_result<_Iter, _Out>
328 operator()(_Iter __first, _Sent __last, _Out __result)
const
330 return ranges::__copy_or_move<true>(
std::move(__first),
335 template<input_range _Range, weakly_incrementable _Out>
336 requires indirectly_movable<iterator_t<_Range>, _Out>
337 constexpr move_result<borrowed_iterator_t<_Range>, _Out>
338 operator()(_Range&& __r, _Out __result)
const
345 inline constexpr __move_fn
move{};
347 template<
bool _IsMove,
348 bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
349 bidirectional_iterator _Out>
351 ? indirectly_movable<_Iter, _Out>
352 : indirectly_copyable<_Iter, _Out>)
354 move_backward_result<_Iter, _Out>,
355 copy_backward_result<_Iter, _Out>>
356 __copy_or_move_backward(_Iter __first, _Sent __last, _Out __result)
360 using __detail::__is_reverse_iterator;
361 using __detail::__is_normal_iterator;
362 if constexpr (__is_reverse_iterator<_Iter> && same_as<_Iter, _Sent>
363 && __is_reverse_iterator<_Out>)
366 = ranges::__copy_or_move<_IsMove>(
std::move(__last).base(),
369 return {reverse_iterator{
std::move(__in)},
372 else if constexpr (__is_normal_iterator<_Iter> && same_as<_Iter, _Sent>)
375 = ranges::__copy_or_move_backward<_IsMove>(__first.base(),
378 return {decltype(__first){__in},
std::move(__out)};
380 else if constexpr (__is_normal_iterator<_Out>)
383 = ranges::__copy_or_move_backward<_IsMove>(
std::move(__first),
386 return {
std::move(__in), decltype(__result){__out}};
388 else if constexpr (sized_sentinel_for<_Sent, _Iter>)
390 #ifdef __cpp_lib_is_constant_evaluated
391 if (!std::is_constant_evaluated())
394 if constexpr (__memcpyable<_Out, _Iter>::__value)
396 using _ValueTypeI = iter_value_t<_Iter>;
397 static_assert(_IsMove
398 ? is_move_assignable_v<_ValueTypeI>
399 : is_copy_assignable_v<_ValueTypeI>);
400 auto __num = __last - __first;
402 __builtin_memmove(__result - __num, __first,
403 sizeof(_ValueTypeI) * __num);
404 return {__first + __num, __result - __num};
408 auto __lasti = ranges::next(__first, __last);
409 auto __tail = __lasti;
411 for (
auto __n = __last - __first; __n > 0; --__n)
415 if constexpr (_IsMove)
424 auto __lasti = ranges::next(__first, __last);
425 auto __tail = __lasti;
427 while (__first != __tail)
431 if constexpr (_IsMove)
440 struct __copy_backward_fn
442 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
443 b
idirectional_iterator _Iter2>
444 requires indirectly_copyable<_Iter1, _Iter2>
445 constexpr copy_backward_result<_Iter1, _Iter2>
446 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
448 return ranges::__copy_or_move_backward<false>(
std::move(__first),
453 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
454 requires indirectly_copyable<iterator_t<_Range>, _Iter>
455 constexpr copy_backward_result<borrowed_iterator_t<_Range>, _Iter>
456 operator()(_Range&& __r, _Iter __result)
const
463 inline constexpr __copy_backward_fn copy_backward{};
465 struct __move_backward_fn
467 template<b
idirectional_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
468 b
idirectional_iterator _Iter2>
469 requires indirectly_movable<_Iter1, _Iter2>
470 constexpr move_backward_result<_Iter1, _Iter2>
471 operator()(_Iter1 __first, _Sent1 __last, _Iter2 __result)
const
473 return ranges::__copy_or_move_backward<true>(
std::move(__first),
478 template<b
idirectional_range _Range, b
idirectional_iterator _Iter>
479 requires indirectly_movable<iterator_t<_Range>, _Iter>
480 constexpr move_backward_result<borrowed_iterator_t<_Range>, _Iter>
481 operator()(_Range&& __r, _Iter __result)
const
490 template<
typename _Iter,
typename _Out>
491 using copy_n_result = in_out_result<_Iter, _Out>;
495 template<input_iterator _Iter, weakly_incrementable _Out>
496 requires indirectly_copyable<_Iter, _Out>
497 constexpr copy_n_result<_Iter, _Out>
498 operator()(_Iter __first, iter_difference_t<_Iter> __n,
501 if constexpr (random_access_iterator<_Iter>)
504 return ranges::copy(__first, __first + __n,
std::move(__result));
508 for (; __n > 0; --__n, (void)++__result, (
void)++__first)
509 *__result = *__first;
515 inline constexpr __copy_n_fn copy_n{};
519 template<
typename _Tp, output_iterator<const _Tp&> _Out>
521 operator()(_Out __first, iter_difference_t<_Out> __n,
522 const _Tp& __value)
const
529 if constexpr (is_scalar_v<_Tp>)
532 if constexpr (is_pointer_v<_Out>
534 && __is_byte<remove_pointer_t<_Out>>::__value
537 #ifdef __cpp_lib_is_constant_evaluated
538 if (!std::is_constant_evaluated())
541 __builtin_memset(__first,
542 static_cast<unsigned char>(__value),
544 return __first + __n;
548 const auto __tmp = __value;
549 for (; __n > 0; --__n, (void)++__first)
555 for (; __n > 0; --__n, (void)++__first)
562 inline constexpr __fill_n_fn fill_n{};
566 template<
typename _Tp,
567 output_iterator<const _Tp&> _Out, sentinel_for<_Out> _Sent>
569 operator()(_Out __first, _Sent __last,
const _Tp& __value)
const
573 if constexpr (sized_sentinel_for<_Sent, _Out>)
575 const auto __len = __last - __first;
576 return ranges::fill_n(__first, __len, __value);
578 else if constexpr (is_scalar_v<_Tp>)
580 const auto __tmp = __value;
581 for (; __first != __last; ++__first)
587 for (; __first != __last; ++__first)
593 template<
typename _Tp, output_range<const _Tp&> _Range>
594 constexpr borrowed_iterator_t<_Range>
595 operator()(_Range&& __r,
const _Tp& __value)
const
601 inline constexpr __fill_fn fill{};
603 _GLIBCXX_END_NAMESPACE_VERSION
typename conditional< _Cond, _Iftrue, _Iffalse >::type conditional_t
Alias template for conditional.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.