1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_MOVE_H 10 #define _LIBCPP___ALGORITHM_MOVE_H 11 12 #include <__algorithm/iterator_operations.h> 13 #include <__algorithm/unwrap_iter.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <__iterator/reverse_iterator.h> 17 #include <__type_traits/is_constant_evaluated.h> 18 #include <__type_traits/is_copy_constructible.h> 19 #include <__type_traits/is_trivially_copyable.h> 20 #include <__type_traits/is_trivially_move_assignable.h> 21 #include <__type_traits/remove_const.h> 22 #include <__utility/move.h> 23 #include <__utility/pair.h> 24 #include <cstring> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 // move 33 34 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 35 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 36 pair<_InIter, _OutIter> __move_impl(_InIter __first, _Sent __last, _OutIter __result) { 37 while (__first != __last) { 38 *__result = _IterOps<_AlgPolicy>::__iter_move(__first); 39 ++__first; 40 ++__result; 41 } 42 return std::make_pair(std::move(__first), std::move(__result)); 43 } 44 45 template <class _AlgPolicy, 46 class _InType, 47 class _OutType, 48 class = __enable_if_t<is_same<__remove_const_t<_InType>, _OutType>::value 49 && is_trivially_move_assignable<_OutType>::value> > 50 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 51 pair<_InType*, _OutType*> __move_impl(_InType* __first, _InType* __last, _OutType* __result) { 52 if (__libcpp_is_constant_evaluated() 53 // TODO: Remove this once GCC supports __builtin_memmove during constant evaluation 54 #ifndef _LIBCPP_COMPILER_GCC 55 && !is_trivially_copyable<_InType>::value 56 #endif 57 ) 58 return std::__move_impl<_AlgPolicy, _InType*, _InType*, _OutType*>(__first, __last, __result); 59 const size_t __n = static_cast<size_t>(__last - __first); 60 ::__builtin_memmove(__result, __first, __n * sizeof(_OutType)); 61 return std::make_pair(__first + __n, __result + __n); 62 } 63 64 template <class> 65 struct __is_trivially_move_assignable_unwrapped_impl : false_type {}; 66 67 template <class _Type> 68 struct __is_trivially_move_assignable_unwrapped_impl<_Type*> : is_trivially_move_assignable<_Type> {}; 69 70 template <class _Iter> 71 struct __is_trivially_move_assignable_unwrapped 72 : __is_trivially_move_assignable_unwrapped_impl<decltype(std::__unwrap_iter<_Iter>(std::declval<_Iter>()))> {}; 73 74 template <class _AlgPolicy, 75 class _InIter, 76 class _OutIter, 77 __enable_if_t<is_same<__remove_const_t<typename iterator_traits<_InIter>::value_type>, 78 typename iterator_traits<_OutIter>::value_type>::value 79 && __is_cpp17_contiguous_iterator<_InIter>::value 80 && __is_cpp17_contiguous_iterator<_OutIter>::value 81 && is_trivially_move_assignable<__iter_value_type<_OutIter> >::value, int> = 0> 82 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 83 pair<reverse_iterator<_InIter>, reverse_iterator<_OutIter> > 84 __move_impl(reverse_iterator<_InIter> __first, 85 reverse_iterator<_InIter> __last, 86 reverse_iterator<_OutIter> __result) { 87 auto __first_base = std::__unwrap_iter(__first.base()); 88 auto __last_base = std::__unwrap_iter(__last.base()); 89 auto __result_base = std::__unwrap_iter(__result.base()); 90 auto __result_first = __result_base - (__first_base - __last_base); 91 std::__move_impl<_AlgPolicy>(__last_base, __first_base, __result_first); 92 return std::make_pair(__last, reverse_iterator<_OutIter>(std::__rewrap_iter(__result.base(), __result_first))); 93 } 94 95 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 96 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 97 __enable_if_t<is_copy_constructible<_InIter>::value 98 && is_copy_constructible<_Sent>::value 99 && is_copy_constructible<_OutIter>::value, pair<_InIter, _OutIter> > 100 __move(_InIter __first, _Sent __last, _OutIter __result) { 101 auto __ret = std::__move_impl<_AlgPolicy>( 102 std::__unwrap_iter(__first), std::__unwrap_iter(__last), std::__unwrap_iter(__result)); 103 return std::make_pair(std::__rewrap_iter(__first, __ret.first), std::__rewrap_iter(__result, __ret.second)); 104 } 105 106 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 107 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 108 __enable_if_t<!is_copy_constructible<_InIter>::value 109 || !is_copy_constructible<_Sent>::value 110 || !is_copy_constructible<_OutIter>::value, pair<_InIter, _OutIter> > 111 __move(_InIter __first, _Sent __last, _OutIter __result) { 112 return std::__move_impl<_AlgPolicy>(std::move(__first), std::move(__last), std::move(__result)); 113 } 114 115 template <class _InputIterator, class _OutputIterator> 116 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 117 _OutputIterator move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { 118 return std::__move<_ClassicAlgPolicy>(__first, __last, __result).second; 119 } 120 121 _LIBCPP_END_NAMESPACE_STD 122 123 #endif // _LIBCPP___ALGORITHM_MOVE_H 124