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_COPY_H 10 #define _LIBCPP___ALGORITHM_COPY_H 11 12 #include <__algorithm/unwrap_iter.h> 13 #include <__algorithm/unwrap_range.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <__iterator/reverse_iterator.h> 17 #include <__utility/move.h> 18 #include <__utility/pair.h> 19 #include <cstring> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 // copy 29 30 template <class _InIter, class _Sent, class _OutIter> 31 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 32 pair<_InIter, _OutIter> __copy_impl(_InIter __first, _Sent __last, _OutIter __result) { 33 while (__first != __last) { 34 *__result = *__first; 35 ++__first; 36 ++__result; 37 } 38 return pair<_InIter, _OutIter>(std::move(__first), std::move(__result)); 39 } 40 41 template <class _InValueT, 42 class _OutValueT, 43 class = __enable_if_t<is_same<typename remove_const<_InValueT>::type, _OutValueT>::value 44 && is_trivially_copy_assignable<_OutValueT>::value> > 45 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 46 pair<_InValueT*, _OutValueT*> __copy_impl(_InValueT* __first, _InValueT* __last, _OutValueT* __result) { 47 if (__libcpp_is_constant_evaluated() 48 // TODO: Remove this once GCC supports __builtin_memmove during constant evaluation 49 #ifndef _LIBCPP_COMPILER_GCC 50 && !is_trivially_copyable<_InValueT>::value 51 #endif 52 ) 53 return std::__copy_impl<_InValueT*, _InValueT*, _OutValueT*>(__first, __last, __result); 54 const size_t __n = static_cast<size_t>(__last - __first); 55 if (__n > 0) 56 ::__builtin_memmove(__result, __first, __n * sizeof(_OutValueT)); 57 return std::make_pair(__first + __n, __result + __n); 58 } 59 60 template <class _InIter, class _OutIter, 61 __enable_if_t<is_same<typename remove_const<__iter_value_type<_InIter> >::type, __iter_value_type<_OutIter> >::value 62 && __is_cpp17_contiguous_iterator<typename _InIter::iterator_type>::value 63 && __is_cpp17_contiguous_iterator<typename _OutIter::iterator_type>::value 64 && is_trivially_copy_assignable<__iter_value_type<_OutIter> >::value 65 && __is_reverse_iterator<_InIter>::value 66 && __is_reverse_iterator<_OutIter>::value, int> = 0> 67 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 68 pair<_InIter, _OutIter> 69 __copy_impl(_InIter __first, _InIter __last, _OutIter __result) { 70 auto __first_base = std::__unwrap_iter(__first.base()); 71 auto __last_base = std::__unwrap_iter(__last.base()); 72 auto __result_base = std::__unwrap_iter(__result.base()); 73 auto __result_first = __result_base - (__first_base - __last_base); 74 std::__copy_impl(__last_base, __first_base, __result_first); 75 return std::make_pair(__last, _OutIter(std::__rewrap_iter(__result.base(), __result_first))); 76 } 77 78 template <class _InIter, class _Sent, class _OutIter, 79 __enable_if_t<!(is_copy_constructible<_InIter>::value 80 && is_copy_constructible<_Sent>::value 81 && is_copy_constructible<_OutIter>::value), int> = 0 > 82 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 83 pair<_InIter, _OutIter> __copy(_InIter __first, _Sent __last, _OutIter __result) { 84 return std::__copy_impl(std::move(__first), std::move(__last), std::move(__result)); 85 } 86 87 template <class _InIter, class _Sent, class _OutIter, 88 __enable_if_t<is_copy_constructible<_InIter>::value 89 && is_copy_constructible<_Sent>::value 90 && is_copy_constructible<_OutIter>::value, int> = 0> 91 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 92 pair<_InIter, _OutIter> __copy(_InIter __first, _Sent __last, _OutIter __result) { 93 auto __range = std::__unwrap_range(__first, __last); 94 auto __ret = std::__copy_impl(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__result)); 95 return std::make_pair( 96 std::__rewrap_range<_Sent>(__first, __ret.first), std::__rewrap_iter(__result, __ret.second)); 97 } 98 99 template <class _InputIterator, class _OutputIterator> 100 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 101 _OutputIterator 102 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { 103 return std::__copy(__first, __last, __result).second; 104 } 105 106 _LIBCPP_END_NAMESPACE_STD 107 108 #endif // _LIBCPP___ALGORITHM_COPY_H 109