Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:57

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
0011 #define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
0012 
0013 #include <__algorithm/copy.h>
0014 #include <__algorithm/move.h>
0015 #include <__algorithm/unwrap_iter.h>
0016 #include <__algorithm/unwrap_range.h>
0017 #include <__config>
0018 #include <__cstddef/size_t.h>
0019 #include <__iterator/iterator_traits.h>
0020 #include <__iterator/reverse_iterator.h>
0021 #include <__memory/addressof.h>
0022 #include <__memory/allocator_traits.h>
0023 #include <__memory/construct_at.h>
0024 #include <__memory/pointer_traits.h>
0025 #include <__type_traits/enable_if.h>
0026 #include <__type_traits/extent.h>
0027 #include <__type_traits/is_array.h>
0028 #include <__type_traits/is_constant_evaluated.h>
0029 #include <__type_traits/is_same.h>
0030 #include <__type_traits/is_trivially_assignable.h>
0031 #include <__type_traits/is_trivially_constructible.h>
0032 #include <__type_traits/is_trivially_relocatable.h>
0033 #include <__type_traits/is_unbounded_array.h>
0034 #include <__type_traits/negation.h>
0035 #include <__type_traits/remove_const.h>
0036 #include <__type_traits/remove_extent.h>
0037 #include <__utility/exception_guard.h>
0038 #include <__utility/move.h>
0039 #include <__utility/pair.h>
0040 
0041 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0042 #  pragma GCC system_header
0043 #endif
0044 
0045 _LIBCPP_PUSH_MACROS
0046 #include <__undef_macros>
0047 
0048 _LIBCPP_BEGIN_NAMESPACE_STD
0049 
0050 struct __always_false {
0051   template <class... _Args>
0052   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(_Args&&...) const _NOEXCEPT {
0053     return false;
0054   }
0055 };
0056 
0057 // uninitialized_copy
0058 
0059 template <class _ValueType, class _InputIterator, class _Sentinel1, class _ForwardIterator, class _EndPredicate>
0060 inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitialized_copy(
0061     _InputIterator __ifirst, _Sentinel1 __ilast, _ForwardIterator __ofirst, _EndPredicate __stop_copying) {
0062   _ForwardIterator __idx = __ofirst;
0063 #if _LIBCPP_HAS_EXCEPTIONS
0064   try {
0065 #endif
0066     for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx)
0067       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
0068 #if _LIBCPP_HAS_EXCEPTIONS
0069   } catch (...) {
0070     std::__destroy(__ofirst, __idx);
0071     throw;
0072   }
0073 #endif
0074 
0075   return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx));
0076 }
0077 
0078 template <class _InputIterator, class _ForwardIterator>
0079 _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0080 uninitialized_copy(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) {
0081   typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
0082   auto __result = std::__uninitialized_copy<_ValueType>(
0083       std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false());
0084   return std::move(__result.second);
0085 }
0086 
0087 // uninitialized_copy_n
0088 
0089 template <class _ValueType, class _InputIterator, class _Size, class _ForwardIterator, class _EndPredicate>
0090 inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator>
0091 __uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_copying) {
0092   _ForwardIterator __idx = __ofirst;
0093 #if _LIBCPP_HAS_EXCEPTIONS
0094   try {
0095 #endif
0096     for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n)
0097       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
0098 #if _LIBCPP_HAS_EXCEPTIONS
0099   } catch (...) {
0100     std::__destroy(__ofirst, __idx);
0101     throw;
0102   }
0103 #endif
0104 
0105   return pair<_InputIterator, _ForwardIterator>(std::move(__ifirst), std::move(__idx));
0106 }
0107 
0108 template <class _InputIterator, class _Size, class _ForwardIterator>
0109 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0110 uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) {
0111   typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
0112   auto __result =
0113       std::__uninitialized_copy_n<_ValueType>(std::move(__ifirst), __n, std::move(__ofirst), __always_false());
0114   return std::move(__result.second);
0115 }
0116 
0117 // uninitialized_fill
0118 
0119 template <class _ValueType, class _ForwardIterator, class _Sentinel, class _Tp>
0120 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0121 __uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) {
0122   _ForwardIterator __idx = __first;
0123 #if _LIBCPP_HAS_EXCEPTIONS
0124   try {
0125 #endif
0126     for (; __idx != __last; ++__idx)
0127       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x);
0128 #if _LIBCPP_HAS_EXCEPTIONS
0129   } catch (...) {
0130     std::__destroy(__first, __idx);
0131     throw;
0132   }
0133 #endif
0134 
0135   return __idx;
0136 }
0137 
0138 template <class _ForwardIterator, class _Tp>
0139 inline _LIBCPP_HIDE_FROM_ABI void
0140 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x) {
0141   typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
0142   (void)std::__uninitialized_fill<_ValueType>(__first, __last, __x);
0143 }
0144 
0145 // uninitialized_fill_n
0146 
0147 template <class _ValueType, class _ForwardIterator, class _Size, class _Tp>
0148 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0149 __uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
0150   _ForwardIterator __idx = __first;
0151 #if _LIBCPP_HAS_EXCEPTIONS
0152   try {
0153 #endif
0154     for (; __n > 0; ++__idx, (void)--__n)
0155       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x);
0156 #if _LIBCPP_HAS_EXCEPTIONS
0157   } catch (...) {
0158     std::__destroy(__first, __idx);
0159     throw;
0160   }
0161 #endif
0162 
0163   return __idx;
0164 }
0165 
0166 template <class _ForwardIterator, class _Size, class _Tp>
0167 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0168 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
0169   typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
0170   return std::__uninitialized_fill_n<_ValueType>(__first, __n, __x);
0171 }
0172 
0173 #if _LIBCPP_STD_VER >= 17
0174 
0175 // uninitialized_default_construct
0176 
0177 template <class _ValueType, class _ForwardIterator, class _Sentinel>
0178 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0179 __uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) {
0180   auto __idx = __first;
0181 #  if _LIBCPP_HAS_EXCEPTIONS
0182   try {
0183 #  endif
0184     for (; __idx != __last; ++__idx)
0185       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType;
0186 #  if _LIBCPP_HAS_EXCEPTIONS
0187   } catch (...) {
0188     std::__destroy(__first, __idx);
0189     throw;
0190   }
0191 #  endif
0192 
0193   return __idx;
0194 }
0195 
0196 template <class _ForwardIterator>
0197 inline _LIBCPP_HIDE_FROM_ABI void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
0198   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0199   (void)std::__uninitialized_default_construct<_ValueType>(std::move(__first), std::move(__last));
0200 }
0201 
0202 // uninitialized_default_construct_n
0203 
0204 template <class _ValueType, class _ForwardIterator, class _Size>
0205 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
0206   auto __idx = __first;
0207 #  if _LIBCPP_HAS_EXCEPTIONS
0208   try {
0209 #  endif
0210     for (; __n > 0; ++__idx, (void)--__n)
0211       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType;
0212 #  if _LIBCPP_HAS_EXCEPTIONS
0213   } catch (...) {
0214     std::__destroy(__first, __idx);
0215     throw;
0216   }
0217 #  endif
0218 
0219   return __idx;
0220 }
0221 
0222 template <class _ForwardIterator, class _Size>
0223 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
0224   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0225   return std::__uninitialized_default_construct_n<_ValueType>(std::move(__first), __n);
0226 }
0227 
0228 // uninitialized_value_construct
0229 
0230 template <class _ValueType, class _ForwardIterator, class _Sentinel>
0231 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0232 __uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) {
0233   auto __idx = __first;
0234 #  if _LIBCPP_HAS_EXCEPTIONS
0235   try {
0236 #  endif
0237     for (; __idx != __last; ++__idx)
0238       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType();
0239 #  if _LIBCPP_HAS_EXCEPTIONS
0240   } catch (...) {
0241     std::__destroy(__first, __idx);
0242     throw;
0243   }
0244 #  endif
0245 
0246   return __idx;
0247 }
0248 
0249 template <class _ForwardIterator>
0250 inline _LIBCPP_HIDE_FROM_ABI void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
0251   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0252   (void)std::__uninitialized_value_construct<_ValueType>(std::move(__first), std::move(__last));
0253 }
0254 
0255 // uninitialized_value_construct_n
0256 
0257 template <class _ValueType, class _ForwardIterator, class _Size>
0258 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
0259   auto __idx = __first;
0260 #  if _LIBCPP_HAS_EXCEPTIONS
0261   try {
0262 #  endif
0263     for (; __n > 0; ++__idx, (void)--__n)
0264       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType();
0265 #  if _LIBCPP_HAS_EXCEPTIONS
0266   } catch (...) {
0267     std::__destroy(__first, __idx);
0268     throw;
0269   }
0270 #  endif
0271 
0272   return __idx;
0273 }
0274 
0275 template <class _ForwardIterator, class _Size>
0276 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
0277   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0278   return std::__uninitialized_value_construct_n<_ValueType>(std::move(__first), __n);
0279 }
0280 
0281 // uninitialized_move
0282 
0283 template <class _ValueType,
0284           class _InputIterator,
0285           class _Sentinel1,
0286           class _ForwardIterator,
0287           class _EndPredicate,
0288           class _IterMove>
0289 inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitialized_move(
0290     _InputIterator __ifirst,
0291     _Sentinel1 __ilast,
0292     _ForwardIterator __ofirst,
0293     _EndPredicate __stop_moving,
0294     _IterMove __iter_move) {
0295   auto __idx = __ofirst;
0296 #  if _LIBCPP_HAS_EXCEPTIONS
0297   try {
0298 #  endif
0299     for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) {
0300       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst));
0301     }
0302 #  if _LIBCPP_HAS_EXCEPTIONS
0303   } catch (...) {
0304     std::__destroy(__ofirst, __idx);
0305     throw;
0306   }
0307 #  endif
0308 
0309   return {std::move(__ifirst), std::move(__idx)};
0310 }
0311 
0312 template <class _InputIterator, class _ForwardIterator>
0313 inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
0314 uninitialized_move(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) {
0315   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0316   auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::move(*__iter); };
0317 
0318   auto __result = std::__uninitialized_move<_ValueType>(
0319       std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false(), __iter_move);
0320   return std::move(__result.second);
0321 }
0322 
0323 // uninitialized_move_n
0324 
0325 template <class _ValueType,
0326           class _InputIterator,
0327           class _Size,
0328           class _ForwardIterator,
0329           class _EndPredicate,
0330           class _IterMove>
0331 inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitialized_move_n(
0332     _InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_moving, _IterMove __iter_move) {
0333   auto __idx = __ofirst;
0334 #  if _LIBCPP_HAS_EXCEPTIONS
0335   try {
0336 #  endif
0337     for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n)
0338       ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst));
0339 #  if _LIBCPP_HAS_EXCEPTIONS
0340   } catch (...) {
0341     std::__destroy(__ofirst, __idx);
0342     throw;
0343   }
0344 #  endif
0345 
0346   return {std::move(__ifirst), std::move(__idx)};
0347 }
0348 
0349 template <class _InputIterator, class _Size, class _ForwardIterator>
0350 inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator>
0351 uninitialized_move_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) {
0352   using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
0353   auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::move(*__iter); };
0354 
0355   return std::__uninitialized_move_n<_ValueType>(
0356       std::move(__ifirst), __n, std::move(__ofirst), __always_false(), __iter_move);
0357 }
0358 
0359 // TODO: Rewrite this to iterate left to right and use reverse_iterators when calling
0360 // Destroys every element in the range [first, last) FROM RIGHT TO LEFT using allocator
0361 // destruction. If elements are themselves C-style arrays, they are recursively destroyed
0362 // in the same manner.
0363 //
0364 // This function assumes that destructors do not throw, and that the allocator is bound to
0365 // the correct type.
0366 template <class _Alloc,
0367           class _BidirIter,
0368           __enable_if_t<__has_bidirectional_iterator_category<_BidirIter>::value, int> = 0>
0369 _LIBCPP_HIDE_FROM_ABI constexpr void
0370 __allocator_destroy_multidimensional(_Alloc& __alloc, _BidirIter __first, _BidirIter __last) noexcept {
0371   using _ValueType = typename iterator_traits<_BidirIter>::value_type;
0372   static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _ValueType>,
0373                 "The allocator should already be rebound to the correct type");
0374 
0375   if (__first == __last)
0376     return;
0377 
0378   if constexpr (is_array_v<_ValueType>) {
0379     static_assert(!__is_unbounded_array_v<_ValueType>,
0380                   "arrays of unbounded arrays don't exist, but if they did we would mess up here");
0381 
0382     using _Element = remove_extent_t<_ValueType>;
0383     __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
0384     do {
0385       --__last;
0386       decltype(auto) __array = *__last;
0387       std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + extent_v<_ValueType>);
0388     } while (__last != __first);
0389   } else {
0390     do {
0391       --__last;
0392       allocator_traits<_Alloc>::destroy(__alloc, std::addressof(*__last));
0393     } while (__last != __first);
0394   }
0395 }
0396 
0397 // Constructs the object at the given location using the allocator's construct method.
0398 //
0399 // If the object being constructed is an array, each element of the array is allocator-constructed,
0400 // recursively. If an exception is thrown during the construction of an array, the initialized
0401 // elements are destroyed in reverse order of initialization using allocator destruction.
0402 //
0403 // This function assumes that the allocator is bound to the correct type.
0404 template <class _Alloc, class _Tp>
0405 _LIBCPP_HIDE_FROM_ABI constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* __loc) {
0406   static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _Tp>,
0407                 "The allocator should already be rebound to the correct type");
0408 
0409   if constexpr (is_array_v<_Tp>) {
0410     using _Element = remove_extent_t<_Tp>;
0411     __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
0412     size_t __i   = 0;
0413     _Tp& __array = *__loc;
0414 
0415     // If an exception is thrown, destroy what we have constructed so far in reverse order.
0416     auto __guard = std::__make_exception_guard([&]() {
0417       std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
0418     });
0419 
0420     for (; __i != extent_v<_Tp>; ++__i) {
0421       std::__allocator_construct_at_multidimensional(__elem_alloc, std::addressof(__array[__i]));
0422     }
0423     __guard.__complete();
0424   } else {
0425     allocator_traits<_Alloc>::construct(__alloc, __loc);
0426   }
0427 }
0428 
0429 // Constructs the object at the given location using the allocator's construct method, passing along
0430 // the provided argument.
0431 //
0432 // If the object being constructed is an array, the argument is also assumed to be an array. Each
0433 // each element of the array being constructed is allocator-constructed from the corresponding
0434 // element of the argument array. If an exception is thrown during the construction of an array,
0435 // the initialized elements are destroyed in reverse order of initialization using allocator
0436 // destruction.
0437 //
0438 // This function assumes that the allocator is bound to the correct type.
0439 template <class _Alloc, class _Tp, class _Arg>
0440 _LIBCPP_HIDE_FROM_ABI constexpr void
0441 __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* __loc, _Arg const& __arg) {
0442   static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _Tp>,
0443                 "The allocator should already be rebound to the correct type");
0444 
0445   if constexpr (is_array_v<_Tp>) {
0446     static_assert(is_array_v<_Arg>,
0447                   "Provided non-array initialization argument to __allocator_construct_at_multidimensional when "
0448                   "trying to construct an array.");
0449 
0450     using _Element = remove_extent_t<_Tp>;
0451     __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
0452     size_t __i   = 0;
0453     _Tp& __array = *__loc;
0454 
0455     // If an exception is thrown, destroy what we have constructed so far in reverse order.
0456     auto __guard = std::__make_exception_guard([&]() {
0457       std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
0458     });
0459     for (; __i != extent_v<_Tp>; ++__i) {
0460       std::__allocator_construct_at_multidimensional(__elem_alloc, std::addressof(__array[__i]), __arg[__i]);
0461     }
0462     __guard.__complete();
0463   } else {
0464     allocator_traits<_Alloc>::construct(__alloc, __loc, __arg);
0465   }
0466 }
0467 
0468 // Given a range starting at it and containing n elements, initializes each element in the
0469 // range from left to right using the construct method of the allocator (rebound to the
0470 // correct type).
0471 //
0472 // If an exception is thrown, the initialized elements are destroyed in reverse order of
0473 // initialization using allocator_traits destruction. If the elements in the range are C-style
0474 // arrays, they are initialized element-wise using allocator construction, and recursively so.
0475 template <class _Alloc,
0476           class _BidirIter,
0477           class _Tp,
0478           class _Size = typename iterator_traits<_BidirIter>::difference_type>
0479 _LIBCPP_HIDE_FROM_ABI constexpr void
0480 __uninitialized_allocator_fill_n_multidimensional(_Alloc& __alloc, _BidirIter __it, _Size __n, _Tp const& __value) {
0481   using _ValueType = typename iterator_traits<_BidirIter>::value_type;
0482   __allocator_traits_rebind_t<_Alloc, _ValueType> __value_alloc(__alloc);
0483   _BidirIter __begin = __it;
0484 
0485   // If an exception is thrown, destroy what we have constructed so far in reverse order.
0486   auto __guard =
0487       std::__make_exception_guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
0488   for (; __n != 0; --__n, ++__it) {
0489     std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it), __value);
0490   }
0491   __guard.__complete();
0492 }
0493 
0494 // Same as __uninitialized_allocator_fill_n_multidimensional, but doesn't pass any initialization argument
0495 // to the allocator's construct method, which results in value initialization.
0496 template <class _Alloc, class _BidirIter, class _Size = typename iterator_traits<_BidirIter>::difference_type>
0497 _LIBCPP_HIDE_FROM_ABI constexpr void
0498 __uninitialized_allocator_value_construct_n_multidimensional(_Alloc& __alloc, _BidirIter __it, _Size __n) {
0499   using _ValueType = typename iterator_traits<_BidirIter>::value_type;
0500   __allocator_traits_rebind_t<_Alloc, _ValueType> __value_alloc(__alloc);
0501   _BidirIter __begin = __it;
0502 
0503   // If an exception is thrown, destroy what we have constructed so far in reverse order.
0504   auto __guard =
0505       std::__make_exception_guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
0506   for (; __n != 0; --__n, ++__it) {
0507     std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it));
0508   }
0509   __guard.__complete();
0510 }
0511 
0512 #endif // _LIBCPP_STD_VER >= 17
0513 
0514 // Destroy all elements in [__first, __last) from left to right using allocator destruction.
0515 template <class _Alloc, class _Iter, class _Sent>
0516 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0517 __allocator_destroy(_Alloc& __alloc, _Iter __first, _Sent __last) {
0518   for (; __first != __last; ++__first)
0519     allocator_traits<_Alloc>::destroy(__alloc, std::__to_address(__first));
0520 }
0521 
0522 template <class _Alloc, class _Iter>
0523 class _AllocatorDestroyRangeReverse {
0524 public:
0525   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
0526   _AllocatorDestroyRangeReverse(_Alloc& __alloc, _Iter& __first, _Iter& __last)
0527       : __alloc_(__alloc), __first_(__first), __last_(__last) {}
0528 
0529   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void operator()() const {
0530     std::__allocator_destroy(__alloc_, std::reverse_iterator<_Iter>(__last_), std::reverse_iterator<_Iter>(__first_));
0531   }
0532 
0533 private:
0534   _Alloc& __alloc_;
0535   _Iter& __first_;
0536   _Iter& __last_;
0537 };
0538 
0539 // Copy-construct [__first1, __last1) in [__first2, __first2 + N), where N is distance(__first1, __last1).
0540 //
0541 // The caller has to ensure that __first2 can hold at least N uninitialized elements. If an exception is thrown the
0542 // already copied elements are destroyed in reverse order of their construction.
0543 template <class _Alloc, class _Iter1, class _Sent1, class _Iter2>
0544 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
0545 __uninitialized_allocator_copy_impl(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1, _Iter2 __first2) {
0546   auto __destruct_first = __first2;
0547   auto __guard =
0548       std::__make_exception_guard(_AllocatorDestroyRangeReverse<_Alloc, _Iter2>(__alloc, __destruct_first, __first2));
0549   while (__first1 != __last1) {
0550     allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), *__first1);
0551     ++__first1;
0552     ++__first2;
0553   }
0554   __guard.__complete();
0555   return __first2;
0556 }
0557 
0558 template <class _Alloc, class _Type>
0559 struct __allocator_has_trivial_copy_construct : _Not<__has_construct<_Alloc, _Type*, const _Type&> > {};
0560 
0561 template <class _Type>
0562 struct __allocator_has_trivial_copy_construct<allocator<_Type>, _Type> : true_type {};
0563 
0564 template <class _Alloc,
0565           class _In,
0566           class _Out,
0567           __enable_if_t<is_trivially_copy_constructible<_In>::value && is_trivially_copy_assignable<_In>::value &&
0568                             is_same<__remove_const_t<_In>, __remove_const_t<_Out> >::value &&
0569                             __allocator_has_trivial_copy_construct<_Alloc, _In>::value,
0570                         int> = 0>
0571 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Out*
0572 __uninitialized_allocator_copy_impl(_Alloc&, _In* __first1, _In* __last1, _Out* __first2) {
0573   if (__libcpp_is_constant_evaluated()) {
0574     while (__first1 != __last1) {
0575       std::__construct_at(std::__to_address(__first2), *__first1);
0576       ++__first1;
0577       ++__first2;
0578     }
0579     return __first2;
0580   } else {
0581     return std::copy(__first1, __last1, __first2);
0582   }
0583 }
0584 
0585 template <class _Alloc, class _Iter1, class _Sent1, class _Iter2>
0586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
0587 __uninitialized_allocator_copy(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1, _Iter2 __first2) {
0588   auto __unwrapped_range = std::__unwrap_range(std::move(__first1), std::move(__last1));
0589   auto __result          = std::__uninitialized_allocator_copy_impl(
0590       __alloc, std::move(__unwrapped_range.first), std::move(__unwrapped_range.second), std::__unwrap_iter(__first2));
0591   return std::__rewrap_iter(__first2, __result);
0592 }
0593 
0594 template <class _Alloc, class _Type>
0595 struct __allocator_has_trivial_move_construct : _Not<__has_construct<_Alloc, _Type*, _Type&&> > {};
0596 
0597 template <class _Type>
0598 struct __allocator_has_trivial_move_construct<allocator<_Type>, _Type> : true_type {};
0599 
0600 template <class _Alloc, class _Tp>
0601 struct __allocator_has_trivial_destroy : _Not<__has_destroy<_Alloc, _Tp*> > {};
0602 
0603 template <class _Tp, class _Up>
0604 struct __allocator_has_trivial_destroy<allocator<_Tp>, _Up> : true_type {};
0605 
0606 // __uninitialized_allocator_relocate relocates the objects in [__first, __last) into __result.
0607 // Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy,
0608 // except that the move constructor and destructor may never be called if they are known to be equivalent to a memcpy.
0609 //
0610 // Preconditions:  __result doesn't contain any objects and [__first, __last) contains objects
0611 // Postconditions: __result contains the objects from [__first, __last) and
0612 //                 [__first, __last) doesn't contain any objects
0613 //
0614 // The strong exception guarantee is provided if any of the following are true:
0615 // - is_nothrow_move_constructible<_ValueType>
0616 // - is_copy_constructible<_ValueType>
0617 // - __libcpp_is_trivially_relocatable<_ValueType>
0618 template <class _Alloc, class _ContiguousIterator>
0619 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocator_relocate(
0620     _Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) {
0621   static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "");
0622   using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type;
0623   static_assert(__is_cpp17_move_insertable<_Alloc>::value,
0624                 "The specified type does not meet the requirements of Cpp17MoveInsertable");
0625   if (__libcpp_is_constant_evaluated() || !__libcpp_is_trivially_relocatable<_ValueType>::value ||
0626       !__allocator_has_trivial_move_construct<_Alloc, _ValueType>::value ||
0627       !__allocator_has_trivial_destroy<_Alloc, _ValueType>::value) {
0628     auto __destruct_first = __result;
0629     auto __guard          = std::__make_exception_guard(
0630         _AllocatorDestroyRangeReverse<_Alloc, _ContiguousIterator>(__alloc, __destruct_first, __result));
0631     auto __iter = __first;
0632     while (__iter != __last) {
0633 #if _LIBCPP_HAS_EXCEPTIONS
0634       allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move_if_noexcept(*__iter));
0635 #else
0636       allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move(*__iter));
0637 #endif
0638       ++__iter;
0639       ++__result;
0640     }
0641     __guard.__complete();
0642     std::__allocator_destroy(__alloc, __first, __last);
0643   } else {
0644     // Casting to void* to suppress clang complaining that this is technically UB.
0645     __builtin_memcpy(static_cast<void*>(std::__to_address(__result)),
0646                      std::__to_address(__first),
0647                      sizeof(_ValueType) * (__last - __first));
0648   }
0649 }
0650 
0651 _LIBCPP_END_NAMESPACE_STD
0652 
0653 _LIBCPP_POP_MACROS
0654 
0655 #endif // _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H