30#ifndef _RANGES_UNINITIALIZED_H
31#define _RANGES_UNINITIALIZED_H 1
33#if __cplusplus > 201703L
38namespace std _GLIBCXX_VISIBILITY(default)
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
45 template<
typename _Tp>
47 __voidify(_Tp& __obj)
noexcept
49 return const_cast<void*
>
53 template<
typename _Iter>
54 concept __nothrow_input_iterator
55 = (input_iterator<_Iter>
56 && is_lvalue_reference_v<iter_reference_t<_Iter>>
57 && same_as<remove_cvref_t<iter_reference_t<_Iter>>,
58 iter_value_t<_Iter>>);
60 template<
typename _Sent,
typename _Iter>
61 concept __nothrow_sentinel = sentinel_for<_Sent, _Iter>;
63 template<
typename _Range>
64 concept __nothrow_input_range
66 && __nothrow_input_iterator<iterator_t<_Range>>
67 && __nothrow_sentinel<sentinel_t<_Range>, iterator_t<_Range>>);
69 template<
typename _Iter>
70 concept __nothrow_forward_iterator
71 = (__nothrow_input_iterator<_Iter>
72 && forward_iterator<_Iter>
73 && __nothrow_sentinel<_Iter, _Iter>);
75 template<
typename _Range>
76 concept __nothrow_forward_range
77 = (__nothrow_input_range<_Range>
78 && __nothrow_forward_iterator<iterator_t<_Range>>);
83 template<__detail::__nothrow_input_iterator _Iter,
84 __detail::__nothrow_sentinel<_Iter> _Sent>
85 requires destructible<iter_value_t<_Iter>>
87 operator()(_Iter __first, _Sent __last)
const noexcept;
89 template<__detail::__nothrow_input_range _Range>
90 requires destructible<range_value_t<_Range>>
91 constexpr borrowed_iterator_t<_Range>
92 operator()(_Range&& __r)
const noexcept;
95 inline constexpr __destroy_fn destroy{};
99 template<
typename _Iter>
100 requires destructible<iter_value_t<_Iter>>
110 _DestroyGuard(
const _Iter& __iter)
111 : _M_first(__iter), _M_cur(std::
__addressof(__iter))
117 { _M_cur =
nullptr; }
122 if (_M_cur !=
nullptr)
123 ranges::destroy(
std::move(_M_first), *_M_cur);
127 template<
typename _Iter>
128 requires destructible<iter_value_t<_Iter>>
129 && is_trivially_destructible_v<iter_value_t<_Iter>>
130 struct _DestroyGuard<_Iter>
134 _DestroyGuard(
const _Iter&)
144 struct __uninitialized_default_construct_fn
146 template<__detail::__nothrow_forward_iterator _Iter,
147 __detail::__nothrow_sentinel<_Iter> _Sent>
148 requires default_initializable<iter_value_t<_Iter>>
151 operator()(_Iter __first, _Sent __last)
const
153 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
154 if constexpr (is_trivially_default_constructible_v<_ValueType>)
155 return ranges::next(__first, __last);
158 auto __guard = __detail::_DestroyGuard(__first);
159 for (; __first != __last; ++__first)
160 ::new (__detail::__voidify(*__first)) _ValueType;
166 template<__detail::__nothrow_forward_range _Range>
167 requires default_initializable<range_value_t<_Range>>
169 borrowed_iterator_t<_Range>
170 operator()(_Range&& __r)
const
172 return (*
this)(ranges::begin(__r), ranges::end(__r));
176 inline constexpr __uninitialized_default_construct_fn
179 struct __uninitialized_default_construct_n_fn
181 template<__detail::__nothrow_forward_iterator _Iter>
182 requires default_initializable<iter_value_t<_Iter>>
185 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const
187 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
188 if constexpr (is_trivially_default_constructible_v<_ValueType>)
189 return ranges::next(__first, __n);
192 auto __guard = __detail::_DestroyGuard(__first);
193 for (; __n > 0; ++__first, (void) --__n)
194 ::new (__detail::__voidify(*__first)) _ValueType;
201 inline constexpr __uninitialized_default_construct_n_fn
204 struct __uninitialized_value_construct_fn
206 template<__detail::__nothrow_forward_iterator _Iter,
207 __detail::__nothrow_sentinel<_Iter> _Sent>
208 requires default_initializable<iter_value_t<_Iter>>
211 operator()(_Iter __first, _Sent __last)
const
213 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
214 if constexpr (is_trivially_default_constructible_v<_ValueType>
215 && is_trivially_copy_assignable_v<_ValueType>)
216 return ranges::fill(__first, __last, _ValueType());
219 auto __guard = __detail::_DestroyGuard(__first);
220 for (; __first != __last; ++__first)
221 ::new (__detail::__voidify(*__first)) _ValueType();
227 template<__detail::__nothrow_forward_range _Range>
228 requires default_initializable<range_value_t<_Range>>
230 borrowed_iterator_t<_Range>
231 operator()(_Range&& __r)
const
233 return (*
this)(ranges::begin(__r), ranges::end(__r));
237 inline constexpr __uninitialized_value_construct_fn
240 struct __uninitialized_value_construct_n_fn
242 template<__detail::__nothrow_forward_iterator _Iter>
243 requires default_initializable<iter_value_t<_Iter>>
246 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const
248 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
249 if constexpr (is_trivially_default_constructible_v<_ValueType>
250 && is_trivially_copy_assignable_v<_ValueType>)
251 return ranges::fill_n(__first, __n, _ValueType());
254 auto __guard = __detail::_DestroyGuard(__first);
255 for (; __n > 0; ++__first, (void) --__n)
256 ::new (__detail::__voidify(*__first)) _ValueType();
263 inline constexpr __uninitialized_value_construct_n_fn
266 template<
typename _Iter,
typename _Out>
267 using uninitialized_copy_result = in_out_result<_Iter, _Out>;
269 struct __uninitialized_copy_fn
271 template<input_iterator _Iter, sentinel_for<_Iter> _ISent,
272 __detail::__nothrow_forward_iterator _Out,
273 __detail::__nothrow_sentinel<_Out> _OSent>
274 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>>
276 uninitialized_copy_result<_Iter, _Out>
277 operator()(_Iter __ifirst, _ISent __ilast,
278 _Out __ofirst, _OSent __olast)
const
280 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
281 if constexpr (sized_sentinel_for<_ISent, _Iter>
282 && sized_sentinel_for<_OSent, _Out>
283 && is_trivially_constructible_v<_OutType, iter_reference_t<_Iter>>
284 && is_trivially_default_constructible_v<_OutType>
285 && is_trivially_assignable_v<_OutType&,
286 iter_reference_t<_Iter>>)
288 auto __d = __ilast - __ifirst;
289 if (
auto __d2 = __olast - __ofirst; __d2 < __d)
290 __d =
static_cast<iter_difference_t<_Iter>
>(__d2);
291 return ranges::copy_n(
std::move(__ifirst), __d, __ofirst);
295 auto __guard = __detail::_DestroyGuard(__ofirst);
296 for (; __ifirst != __ilast && __ofirst != __olast;
297 ++__ofirst, (void)++__ifirst)
298 ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst);
304 template<input_range _IRange, __detail::__nothrow_forward_range _ORange>
305 requires constructible_from<range_value_t<_ORange>,
306 range_reference_t<_IRange>>
308 uninitialized_copy_result<borrowed_iterator_t<_IRange>,
309 borrowed_iterator_t<_ORange>>
310 operator()(_IRange&& __inr, _ORange&& __outr)
const
312 return (*
this)(ranges::begin(__inr), ranges::end(__inr),
313 ranges::begin(__outr), ranges::end(__outr));
319 template<
typename _Iter,
typename _Out>
320 using uninitialized_copy_n_result = in_out_result<_Iter, _Out>;
322 struct __uninitialized_copy_n_fn
324 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out,
325 __detail::__nothrow_sentinel<_Out> _Sent>
326 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>>
328 uninitialized_copy_n_result<_Iter, _Out>
329 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n,
330 _Out __ofirst, _Sent __olast)
const
332 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
333 if constexpr (sized_sentinel_for<_Sent, _Out>
334 && is_trivially_constructible_v<_OutType, iter_reference_t<_Iter>>
335 && is_trivially_default_constructible_v<_OutType>
336 && is_trivially_assignable_v<_OutType&,
337 iter_reference_t<_Iter>>)
339 if (
auto __d = __olast - __ofirst; __d < __n)
340 __n =
static_cast<iter_difference_t<_Iter>
>(__d);
341 return ranges::copy_n(
std::move(__ifirst), __n, __ofirst);
345 auto __guard = __detail::_DestroyGuard(__ofirst);
346 for (; __n > 0 && __ofirst != __olast;
347 ++__ofirst, (void)++__ifirst, (void)--__n)
348 ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst);
357 template<
typename _Iter,
typename _Out>
358 using uninitialized_move_result = in_out_result<_Iter, _Out>;
360 struct __uninitialized_move_fn
362 template<input_iterator _Iter, sentinel_for<_Iter> _ISent,
363 __detail::__nothrow_forward_iterator _Out,
364 __detail::__nothrow_sentinel<_Out> _OSent>
365 requires constructible_from<iter_value_t<_Out>,
366 iter_rvalue_reference_t<_Iter>>
368 uninitialized_move_result<_Iter, _Out>
369 operator()(_Iter __ifirst, _ISent __ilast,
370 _Out __ofirst, _OSent __olast)
const
372 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
373 if constexpr (sized_sentinel_for<_ISent, _Iter>
374 && sized_sentinel_for<_OSent, _Out>
375 && is_trivially_constructible_v<_OutType, iter_rvalue_reference_t<_Iter>>
376 && is_trivially_default_constructible_v<_OutType>
377 && is_trivially_assignable_v<_OutType&,
378 iter_rvalue_reference_t<_Iter>>)
380 auto __d = __ilast - __ifirst;
381 if (
auto __d2 = __olast - __ofirst; __d2 < __d)
382 __d =
static_cast<iter_difference_t<_Iter>
>(__d2);
384 = ranges::copy_n(std::make_move_iterator(
std::move(__ifirst)),
390 auto __guard = __detail::_DestroyGuard(__ofirst);
391 for (; __ifirst != __ilast && __ofirst != __olast;
392 ++__ofirst, (void)++__ifirst)
393 ::new (__detail::__voidify(*__ofirst))
394 _OutType(ranges::iter_move(__ifirst));
400 template<input_range _IRange, __detail::__nothrow_forward_range _ORange>
401 requires constructible_from<range_value_t<_ORange>,
402 range_rvalue_reference_t<_IRange>>
404 uninitialized_move_result<borrowed_iterator_t<_IRange>,
405 borrowed_iterator_t<_ORange>>
406 operator()(_IRange&& __inr, _ORange&& __outr)
const
408 return (*
this)(ranges::begin(__inr), ranges::end(__inr),
409 ranges::begin(__outr), ranges::end(__outr));
415 template<
typename _Iter,
typename _Out>
416 using uninitialized_move_n_result = in_out_result<_Iter, _Out>;
418 struct __uninitialized_move_n_fn
420 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out,
421 __detail::__nothrow_sentinel<_Out> _Sent>
422 requires constructible_from<iter_value_t<_Out>,
423 iter_rvalue_reference_t<_Iter>>
425 uninitialized_move_n_result<_Iter, _Out>
426 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n,
427 _Out __ofirst, _Sent __olast)
const
429 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
430 if constexpr (sized_sentinel_for<_Sent, _Out>
431 && is_trivially_constructible_v<_OutType, iter_rvalue_reference_t<_Iter>>
432 && is_trivially_default_constructible_v<_OutType>
433 && is_trivially_assignable_v<_OutType&,
434 iter_rvalue_reference_t<_Iter>>)
436 if (
auto __d = __olast - __ofirst; __d < __n)
437 __n =
static_cast<iter_difference_t<_Iter>
>(__d);
439 = ranges::copy_n(std::make_move_iterator(
std::move(__ifirst)),
445 auto __guard = __detail::_DestroyGuard(__ofirst);
446 for (; __n > 0 && __ofirst != __olast;
447 ++__ofirst, (void)++__ifirst, (void)--__n)
448 ::new (__detail::__voidify(*__ofirst))
449 _OutType(ranges::iter_move(__ifirst));
458 struct __uninitialized_fill_fn
460 template<__detail::__nothrow_forward_iterator _Iter,
461 __detail::__nothrow_sentinel<_Iter> _Sent,
typename _Tp>
462 requires constructible_from<iter_value_t<_Iter>,
const _Tp&>
465 operator()(_Iter __first, _Sent __last,
const _Tp& __x)
const
467 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
468 if constexpr (is_trivially_constructible_v<_ValueType, const _Tp&>
469 && is_trivially_assignable_v<_ValueType&, const _Tp&>)
470 return ranges::fill(__first, __last, __x);
473 auto __guard = __detail::_DestroyGuard(__first);
474 for (; __first != __last; ++__first)
475 ::new (__detail::__voidify(*__first)) _ValueType(__x);
481 template<__detail::__nothrow_forward_range _Range,
typename _Tp>
482 requires constructible_from<range_value_t<_Range>,
const _Tp&>
484 borrowed_iterator_t<_Range>
485 operator()(_Range&& __r,
const _Tp& __x)
const
487 return (*
this)(ranges::begin(__r), ranges::end(__r), __x);
493 struct __uninitialized_fill_n_fn
495 template<__detail::__nothrow_forward_iterator _Iter,
typename _Tp>
496 requires constructible_from<iter_value_t<_Iter>,
const _Tp&>
499 operator()(_Iter __first, iter_difference_t<_Iter> __n,
500 const _Tp& __x)
const
502 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
503 if constexpr (is_trivially_constructible_v<_ValueType, const _Tp&>
504 && is_trivially_assignable_v<_ValueType&, const _Tp&>)
505 return ranges::fill_n(__first, __n, __x);
508 auto __guard = __detail::_DestroyGuard(__first);
509 for (; __n > 0; ++__first, (void)--__n)
510 ::new (__detail::__voidify(*__first)) _ValueType(__x);
519 struct __construct_at_fn
521 template<
typename _Tp,
typename... _Args>
526 operator()(_Tp* __location, _Args&&... __args) const
527 noexcept(noexcept(std::construct_at(__location,
528 std::forward<_Args>(__args)...)))
530 return std::construct_at(__location,
535 inline constexpr __construct_at_fn construct_at{};
537 struct __destroy_at_fn
539 template<destructible _Tp>
541 operator()(_Tp* __location)
const noexcept
543 if constexpr (is_array_v<_Tp>)
544 ranges::destroy(ranges::begin(*__location), ranges::end(*__location));
550 inline constexpr __destroy_at_fn destroy_at{};
552 template<__detail::__nothrow_input_iterator _Iter,
553 __detail::__nothrow_sentinel<_Iter> _Sent>
554 requires destructible<iter_value_t<_Iter>>
556 __destroy_fn::operator()(_Iter __first, _Sent __last)
const noexcept
558 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>)
559 if (!is_constant_evaluated())
560 return ranges::next(
std::move(__first), __last);
562 for (; __first != __last; ++__first)
567 template<__detail::__nothrow_input_range _Range>
568 requires destructible<range_value_t<_Range>>
569 constexpr borrowed_iterator_t<_Range>
570 __destroy_fn::operator()(_Range&& __r)
const noexcept
572 return (*
this)(ranges::begin(__r), ranges::end(__r));
575 struct __destroy_n_fn
577 template<__detail::__nothrow_input_iterator _Iter>
578 requires destructible<iter_value_t<_Iter>>
580 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const noexcept
582 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>)
583 if (!is_constant_evaluated())
584 return ranges::next(
std::move(__first), __n);
586 for (; __n > 0; ++__first, (void)--__n)
592 inline constexpr __destroy_n_fn destroy_n{};
594_GLIBCXX_END_NAMESPACE_VERSION
_ForwardIterator uninitialized_copy_n(_InputIterator __first, _Size __n, _ForwardIterator __result)
Copies the range [first,first+n) into result.
void uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__x)
Copies the value x into the range [first,last).
_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
Value-initializes objects in the range [first,first+count).
_ForwardIterator uninitialized_move(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Move-construct from the range [first,last) into result.
_ForwardIterator uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp &__x)
Copies the value x into the range [first,first+n).
_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last)
Default-initializes objects in the range [first,last).
_ForwardIterator uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Copies the range [first,last) into result.
void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last)
Value-initializes objects in the range [first,last).
pair< _InputIterator, _ForwardIterator > uninitialized_move_n(_InputIterator __first, _Size __count, _ForwardIterator __result)
Move-construct from the range [first,first+count) into result.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.