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_BACKWARD_H 10 #define _LIBCPP___ALGORITHM_COPY_BACKWARD_H 11 12 #include <__algorithm/unwrap_iter.h> 13 #include <__config> 14 #include <__iterator/iterator_traits.h> 15 #include <cstring> 16 #include <type_traits> 17 18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19 # pragma GCC system_header 20 # pragma clang include_instead(<algorithm>) 21 #endif 22 23 _LIBCPP_BEGIN_NAMESPACE_STD 24 25 template <class _BidirectionalIterator, class _OutputIterator> 26 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 27 _OutputIterator 28 __copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 29 { 30 while (__first != __last) 31 *--__result = *--__last; 32 return __result; 33 } 34 35 template <class _BidirectionalIterator, class _OutputIterator> 36 inline _LIBCPP_INLINE_VISIBILITY 37 _OutputIterator 38 __copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 39 { 40 return _VSTD::__copy_backward_constexpr(__first, __last, __result); 41 } 42 43 template <class _Tp, class _Up> 44 inline _LIBCPP_INLINE_VISIBILITY 45 typename enable_if 46 < 47 is_same<typename remove_const<_Tp>::type, _Up>::value && 48 is_trivially_copy_assignable<_Up>::value, 49 _Up* 50 >::type 51 __copy_backward(_Tp* __first, _Tp* __last, _Up* __result) 52 { 53 const size_t __n = static_cast<size_t>(__last - __first); 54 if (__n > 0) 55 { 56 __result -= __n; 57 _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 58 } 59 return __result; 60 } 61 62 template <class _BidirectionalIterator1, class _BidirectionalIterator2> 63 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 64 _BidirectionalIterator2 65 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 66 _BidirectionalIterator2 __result) 67 { 68 if (__libcpp_is_constant_evaluated()) { 69 return _VSTD::__copy_backward_constexpr(__first, __last, __result); 70 } else { 71 return _VSTD::__rewrap_iter(__result, 72 _VSTD::__copy_backward(_VSTD::__unwrap_iter(__first), 73 _VSTD::__unwrap_iter(__last), 74 _VSTD::__unwrap_iter(__result))); 75 } 76 } 77 78 _LIBCPP_END_NAMESPACE_STD 79 80 #endif // _LIBCPP___ALGORITHM_COPY_BACKWARD_H 81