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