xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/move_backward.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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_MOVE_BACKWARD_H
10 #define _LIBCPP___ALGORITHM_MOVE_BACKWARD_H
11 
12 #include <__algorithm/iterator_operations.h>
13 #include <__algorithm/unwrap_iter.h>
14 #include <__config>
15 #include <__utility/move.h>
16 #include <cstring>
17 #include <type_traits>
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, class _InputIterator, class _OutputIterator>
26 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
27 _OutputIterator
28 __move_backward_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
29 {
30     while (__first != __last)
31         *--__result = _IterOps<_AlgPolicy>::__iter_move(--__last);
32     return __result;
33 }
34 
35 template <class _AlgPolicy, class _InputIterator, class _OutputIterator>
36 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
37 _OutputIterator
38 __move_backward_impl(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
39 {
40     return _VSTD::__move_backward_constexpr<_AlgPolicy>(__first, __last, __result);
41 }
42 
43 template <class _AlgPolicy, class _Tp, class _Up>
44 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
45 typename enable_if
46 <
47     is_same<typename remove_const<_Tp>::type, _Up>::value &&
48     is_trivially_move_assignable<_Up>::value,
49     _Up*
50 >::type
51 __move_backward_impl(_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 _AlgPolicy, class _BidirectionalIterator1, class _BidirectionalIterator2>
63 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
64 _BidirectionalIterator2
65 __move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
66                 _BidirectionalIterator2 __result)
67 {
68     if (__libcpp_is_constant_evaluated()) {
69         return _VSTD::__move_backward_constexpr<_AlgPolicy>(__first, __last, __result);
70     } else {
71         return _VSTD::__rewrap_iter(__result,
72             _VSTD::__move_backward_impl<_AlgPolicy>(_VSTD::__unwrap_iter(__first),
73                                                     _VSTD::__unwrap_iter(__last),
74                                                     _VSTD::__unwrap_iter(__result)));
75     }
76 }
77 
78 template <class _BidirectionalIterator1, class _BidirectionalIterator2>
79 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
80 _BidirectionalIterator2
81 move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
82               _BidirectionalIterator2 __result)
83 {
84   return std::__move_backward<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result));
85 }
86 
87 _LIBCPP_END_NAMESPACE_STD
88 
89 #endif // _LIBCPP___ALGORITHM_MOVE_BACKWARD_H
90