xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/copy.h (revision a2464ee12761660f50d0b6f59f233949ebcacc87)
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_H
10 #define _LIBCPP___ALGORITHM_COPY_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 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 // copy
25 
26 template <class _InputIterator, class _OutputIterator>
27 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
28 _OutputIterator
29 __copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
30 {
31     for (; __first != __last; ++__first, (void) ++__result)
32         *__result = *__first;
33     return __result;
34 }
35 
36 template <class _InputIterator, class _OutputIterator>
37 inline _LIBCPP_INLINE_VISIBILITY
38 _OutputIterator
39 __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
40 {
41     return _VSTD::__copy_constexpr(__first, __last, __result);
42 }
43 
44 template <class _Tp, class _Up>
45 inline _LIBCPP_INLINE_VISIBILITY
46 typename enable_if
47 <
48     is_same<typename remove_const<_Tp>::type, _Up>::value &&
49     is_trivially_copy_assignable<_Up>::value,
50     _Up*
51 >::type
52 __copy(_Tp* __first, _Tp* __last, _Up* __result)
53 {
54     const size_t __n = static_cast<size_t>(__last - __first);
55     if (__n > 0)
56         _VSTD::memmove(__result, __first, __n * sizeof(_Up));
57     return __result + __n;
58 }
59 
60 template <class _InputIterator, class _OutputIterator>
61 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
62 _OutputIterator
63 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
64 {
65     if (__libcpp_is_constant_evaluated()) {
66         return _VSTD::__copy_constexpr(__first, __last, __result);
67     } else {
68         return _VSTD::__rewrap_iter(__result,
69             _VSTD::__copy(_VSTD::__unwrap_iter(__first),
70                           _VSTD::__unwrap_iter(__last),
71                           _VSTD::__unwrap_iter(__result)));
72     }
73 }
74 
75 _LIBCPP_END_NAMESPACE_STD
76 
77 #endif // _LIBCPP___ALGORITHM_COPY_H
78