xref: /llvm-project/libcxx/include/__algorithm/copy_backward.h (revision 7ae66e5e9593f0bc470b5796d50c5b8979f62665)
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.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__algorithm/ranges_copy.h>
15 #include <__algorithm/unwrap_iter.h>
16 #include <__concepts/same_as.h>
17 #include <__config>
18 #include <__iterator/iterator_traits.h>
19 #include <__iterator/reverse_iterator.h>
20 #include <__ranges/subrange.h>
21 #include <__utility/move.h>
22 #include <__utility/pair.h>
23 #include <cstring>
24 #include <type_traits>
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 template <class _AlgPolicy, class _InputIterator, class _OutputIterator,
33           __enable_if_t<is_same<_AlgPolicy, _ClassicAlgPolicy>::value, int> = 0>
34 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_InputIterator, _OutputIterator>
35 __copy_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
36   auto __ret = std::__copy(
37       __unconstrained_reverse_iterator<_InputIterator>(__last),
38       __unconstrained_reverse_iterator<_InputIterator>(__first),
39       __unconstrained_reverse_iterator<_OutputIterator>(__result));
40   return pair<_InputIterator, _OutputIterator>(__ret.first.base(), __ret.second.base());
41 }
42 
43 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
44 template <class _AlgPolicy, class _Iter1, class _Sent1, class _Iter2,
45           __enable_if_t<is_same<_AlgPolicy, _RangeAlgPolicy>::value, int> = 0>
46 _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter1, _Iter2> __copy_backward(_Iter1 __first, _Sent1 __last, _Iter2 __result) {
47   auto __last_iter     = _IterOps<_AlgPolicy>::next(__first, std::move(__last));
48   auto __reverse_range = std::__reverse_range(std::ranges::subrange(std::move(__first), __last_iter));
49   auto __ret           = ranges::copy(std::move(__reverse_range), std::make_reverse_iterator(__result));
50   return std::make_pair(__last_iter, __ret.out.base());
51 }
52 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
53 
54 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
55 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator2
56 copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, _BidirectionalIterator2 __result) {
57   return std::__copy_backward<_ClassicAlgPolicy>(__first, __last, __result).second;
58 }
59 
60 _LIBCPP_END_NAMESPACE_STD
61 
62 #endif // _LIBCPP___ALGORITHM_COPY_BACKWARD_H
63