1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_COPY_BACKWARD_H 10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_COPY_BACKWARD_H 11fe6060f1SDimitry Andric 12bdd1243dSDimitry Andric #include <__algorithm/copy_move_common.h> 13972a253aSDimitry Andric #include <__algorithm/iterator_operations.h> 14bdd1243dSDimitry Andric #include <__algorithm/min.h> 1504eeddc0SDimitry Andric #include <__config> 16bdd1243dSDimitry Andric #include <__iterator/segmented_iterator.h> 17bdd1243dSDimitry Andric #include <__type_traits/common_type.h> 18*0fca6ea1SDimitry Andric #include <__type_traits/is_constructible.h> 19972a253aSDimitry Andric #include <__utility/move.h> 20972a253aSDimitry Andric #include <__utility/pair.h> 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23fe6060f1SDimitry Andric # pragma GCC system_header 24fe6060f1SDimitry Andric #endif 25fe6060f1SDimitry Andric 26bdd1243dSDimitry Andric _LIBCPP_PUSH_MACROS 27bdd1243dSDimitry Andric #include <__undef_macros> 28bdd1243dSDimitry Andric 29fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 30fe6060f1SDimitry Andric 31bdd1243dSDimitry Andric template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 32bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InIter, _OutIter> 33bdd1243dSDimitry Andric __copy_backward(_InIter __first, _Sent __last, _OutIter __result); 34bdd1243dSDimitry Andric 35bdd1243dSDimitry Andric template <class _AlgPolicy> 36*0fca6ea1SDimitry Andric struct __copy_backward_impl { 37bdd1243dSDimitry Andric template <class _InIter, class _Sent, class _OutIter> 38bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> 39bdd1243dSDimitry Andric operator()(_InIter __first, _Sent __last, _OutIter __result) const { 40bdd1243dSDimitry Andric auto __last_iter = _IterOps<_AlgPolicy>::next(__first, __last); 41bdd1243dSDimitry Andric auto __original_last_iter = __last_iter; 42bdd1243dSDimitry Andric 43bdd1243dSDimitry Andric while (__first != __last_iter) { 44bdd1243dSDimitry Andric *--__result = *--__last_iter; 45fe6060f1SDimitry Andric } 46fe6060f1SDimitry Andric 47bdd1243dSDimitry Andric return std::make_pair(std::move(__original_last_iter), std::move(__result)); 48fe6060f1SDimitry Andric } 49bdd1243dSDimitry Andric 50bdd1243dSDimitry Andric template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0> 51bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> 52bdd1243dSDimitry Andric operator()(_InIter __first, _InIter __last, _OutIter __result) const { 53bdd1243dSDimitry Andric using _Traits = __segmented_iterator_traits<_InIter>; 54bdd1243dSDimitry Andric auto __sfirst = _Traits::__segment(__first); 55bdd1243dSDimitry Andric auto __slast = _Traits::__segment(__last); 56bdd1243dSDimitry Andric if (__sfirst == __slast) { 57bdd1243dSDimitry Andric auto __iters = 58bdd1243dSDimitry Andric std::__copy_backward<_AlgPolicy>(_Traits::__local(__first), _Traits::__local(__last), std::move(__result)); 59bdd1243dSDimitry Andric return std::make_pair(__last, __iters.second); 60bdd1243dSDimitry Andric } 61bdd1243dSDimitry Andric 62bdd1243dSDimitry Andric __result = 63bdd1243dSDimitry Andric std::__copy_backward<_AlgPolicy>(_Traits::__begin(__slast), _Traits::__local(__last), std::move(__result)) 64bdd1243dSDimitry Andric .second; 65bdd1243dSDimitry Andric --__slast; 66bdd1243dSDimitry Andric while (__sfirst != __slast) { 67bdd1243dSDimitry Andric __result = 68bdd1243dSDimitry Andric std::__copy_backward<_AlgPolicy>(_Traits::__begin(__slast), _Traits::__end(__slast), std::move(__result)) 69bdd1243dSDimitry Andric .second; 70bdd1243dSDimitry Andric --__slast; 71bdd1243dSDimitry Andric } 72bdd1243dSDimitry Andric __result = std::__copy_backward<_AlgPolicy>(_Traits::__local(__first), _Traits::__end(__slast), std::move(__result)) 73bdd1243dSDimitry Andric .second; 74bdd1243dSDimitry Andric return std::make_pair(__last, std::move(__result)); 75bdd1243dSDimitry Andric } 76bdd1243dSDimitry Andric 77bdd1243dSDimitry Andric template <class _InIter, 78bdd1243dSDimitry Andric class _OutIter, 7906c3fb27SDimitry Andric __enable_if_t<__has_random_access_iterator_category<_InIter>::value && 80bdd1243dSDimitry Andric !__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value, 81bdd1243dSDimitry Andric int> = 0> 82bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> 8306c3fb27SDimitry Andric operator()(_InIter __first, _InIter __last, _OutIter __result) const { 84bdd1243dSDimitry Andric using _Traits = __segmented_iterator_traits<_OutIter>; 85bdd1243dSDimitry Andric auto __orig_last = __last; 86bdd1243dSDimitry Andric auto __segment_iterator = _Traits::__segment(__result); 87bdd1243dSDimitry Andric 88bdd1243dSDimitry Andric // When the range contains no elements, __result might not be a valid iterator 89bdd1243dSDimitry Andric if (__first == __last) 90bdd1243dSDimitry Andric return std::make_pair(__first, __result); 91bdd1243dSDimitry Andric 92bdd1243dSDimitry Andric auto __local_last = _Traits::__local(__result); 93bdd1243dSDimitry Andric while (true) { 94bdd1243dSDimitry Andric using _DiffT = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type; 95bdd1243dSDimitry Andric 96bdd1243dSDimitry Andric auto __local_first = _Traits::__begin(__segment_iterator); 97bdd1243dSDimitry Andric auto __size = std::min<_DiffT>(__local_last - __local_first, __last - __first); 98bdd1243dSDimitry Andric auto __iter = std::__copy_backward<_AlgPolicy>(__last - __size, __last, __local_last).second; 99bdd1243dSDimitry Andric __last -= __size; 100bdd1243dSDimitry Andric 101bdd1243dSDimitry Andric if (__first == __last) 102bdd1243dSDimitry Andric return std::make_pair(std::move(__orig_last), _Traits::__compose(__segment_iterator, std::move(__iter))); 103bdd1243dSDimitry Andric --__segment_iterator; 104bdd1243dSDimitry Andric __local_last = _Traits::__end(__segment_iterator); 105bdd1243dSDimitry Andric } 106bdd1243dSDimitry Andric } 107bdd1243dSDimitry Andric 108bdd1243dSDimitry Andric // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer. 109cb14a3feSDimitry Andric template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0> 110bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 111bdd1243dSDimitry Andric operator()(_In* __first, _In* __last, _Out* __result) const { 112bdd1243dSDimitry Andric return std::__copy_backward_trivial_impl(__first, __last, __result); 113bdd1243dSDimitry Andric } 114bdd1243dSDimitry Andric }; 115bdd1243dSDimitry Andric 116bdd1243dSDimitry Andric template <class _AlgPolicy, class _BidirectionalIterator1, class _Sentinel, class _BidirectionalIterator2> 117bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator1, _BidirectionalIterator2> 118bdd1243dSDimitry Andric __copy_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result) { 119*0fca6ea1SDimitry Andric return std::__copy_move_unwrap_iters<__copy_backward_impl<_AlgPolicy> >( 120bdd1243dSDimitry Andric std::move(__first), std::move(__last), std::move(__result)); 121bdd1243dSDimitry Andric } 122fe6060f1SDimitry Andric 123fe6060f1SDimitry Andric template <class _BidirectionalIterator1, class _BidirectionalIterator2> 124cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator2 125cb14a3feSDimitry Andric copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, _BidirectionalIterator2 __result) { 126bdd1243dSDimitry Andric static_assert(std::is_copy_constructible<_BidirectionalIterator1>::value && 127cb14a3feSDimitry Andric std::is_copy_constructible<_BidirectionalIterator1>::value, 128cb14a3feSDimitry Andric "Iterators must be copy constructible."); 129bdd1243dSDimitry Andric 130cb14a3feSDimitry Andric return std::__copy_backward<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second; 131fe6060f1SDimitry Andric } 132fe6060f1SDimitry Andric 133fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 134fe6060f1SDimitry Andric 135bdd1243dSDimitry Andric _LIBCPP_POP_MACROS 136bdd1243dSDimitry Andric 137fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_COPY_BACKWARD_H 138