xref: /llvm-project/libcxx/include/__algorithm/copy.h (revision 005916de58f73aa5c4264c084ba7b0e21040d88f)
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/copy_move_common.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__config>
15 #include <__utility/move.h>
16 #include <__utility/pair.h>
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 struct __copy_loop {
25   template <class _InIter, class _Sent, class _OutIter>
26   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
27   operator()(_InIter __first, _Sent __last, _OutIter __result) const {
28     while (__first != __last) {
29       *__result = *__first;
30       ++__first;
31       ++__result;
32     }
33 
34     return std::make_pair(std::move(__first), std::move(__result));
35   }
36 };
37 
38 struct __copy_trivial {
39   // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
40   template <class _In, class _Out, __enable_if_t< is_trivially_assignable<_Out&, _In&>::value, int > = 0>
41   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
42   operator()(_In* __first, _In* __last, _Out* __result) const {
43     return std::__copy_trivial_impl(__first, __last, __result);
44   }
45 };
46 
47 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
48 pair<_InIter, _OutIter>
49 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
50 __copy(_InIter __first, _Sent __last, _OutIter __result) {
51   return std::__dispatch_copy_or_move<_AlgPolicy, __copy_loop, __copy_trivial>(
52       std::move(__first), std::move(__last), std::move(__result));
53 }
54 
55 template <class _InputIterator, class _OutputIterator>
56 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
57 _OutputIterator
58 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
59   return std::__copy<_ClassicAlgPolicy>(__first, __last, __result).second;
60 }
61 
62 _LIBCPP_END_NAMESPACE_STD
63 
64 #endif // _LIBCPP___ALGORITHM_COPY_H
65