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/copy_move_common.h> 13 #include <__algorithm/iterator_operations.h> 14 #include <__config> 15 #include <__type_traits/is_copy_constructible.h> 16 #include <__utility/move.h> 17 #include <__utility/pair.h> 18 19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20 # pragma GCC system_header 21 #endif 22 23 _LIBCPP_BEGIN_NAMESPACE_STD 24 25 template <class _AlgPolicy> 26 struct __copy_backward_loop { 27 template <class _InIter, class _Sent, class _OutIter> 28 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> 29 operator()(_InIter __first, _Sent __last, _OutIter __result) const { 30 auto __last_iter = _IterOps<_AlgPolicy>::next(__first, __last); 31 auto __original_last_iter = __last_iter; 32 33 while (__first != __last_iter) { 34 *--__result = *--__last_iter; 35 } 36 37 return std::make_pair(std::move(__original_last_iter), std::move(__result)); 38 } 39 }; 40 41 struct __copy_backward_trivial { 42 // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer. 43 template <class _In, class _Out, 44 __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0> 45 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 46 operator()(_In* __first, _In* __last, _Out* __result) const { 47 return std::__copy_backward_trivial_impl(__first, __last, __result); 48 } 49 }; 50 51 template <class _AlgPolicy, class _BidirectionalIterator1, class _Sentinel, class _BidirectionalIterator2> 52 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 53 pair<_BidirectionalIterator1, _BidirectionalIterator2> 54 __copy_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result) { 55 return std::__dispatch_copy_or_move<_AlgPolicy, __copy_backward_loop<_AlgPolicy>, __copy_backward_trivial>( 56 std::move(__first), std::move(__last), std::move(__result)); 57 } 58 59 template <class _BidirectionalIterator1, class _BidirectionalIterator2> 60 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 61 _BidirectionalIterator2 62 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 63 _BidirectionalIterator2 __result) 64 { 65 static_assert(std::is_copy_constructible<_BidirectionalIterator1>::value && 66 std::is_copy_constructible<_BidirectionalIterator1>::value, "Iterators must be copy constructible."); 67 68 return std::__copy_backward<_ClassicAlgPolicy>( 69 std::move(__first), std::move(__last), std::move(__result)).second; 70 } 71 72 _LIBCPP_END_NAMESPACE_STD 73 74 #endif // _LIBCPP___ALGORITHM_COPY_BACKWARD_H 75