xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/copy_n.h (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
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_N_H
10 #define _LIBCPP___ALGORITHM_COPY_N_H
11 
12 #include <__config>
13 #include <__algorithm/copy.h>
14 #include <__algorithm/unwrap_iter.h>
15 #include <__iterator/iterator_traits.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_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template<class _InputIterator, class _Size, class _OutputIterator>
29 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
30 typename enable_if
31 <
32     __is_cpp17_input_iterator<_InputIterator>::value &&
33    !__is_cpp17_random_access_iterator<_InputIterator>::value,
34     _OutputIterator
35 >::type
36 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
37 {
38     typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
39     _IntegralSize __n = __orig_n;
40     if (__n > 0)
41     {
42         *__result = *__first;
43         ++__result;
44         for (--__n; __n > 0; --__n)
45         {
46             ++__first;
47             *__result = *__first;
48             ++__result;
49         }
50     }
51     return __result;
52 }
53 
54 template<class _InputIterator, class _Size, class _OutputIterator>
55 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
56 typename enable_if
57 <
58     __is_cpp17_random_access_iterator<_InputIterator>::value,
59     _OutputIterator
60 >::type
61 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
62 {
63     typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
64     _IntegralSize __n = __orig_n;
65     return _VSTD::copy(__first, __first + __n, __result);
66 }
67 
68 _LIBCPP_END_NAMESPACE_STD
69 
70 _LIBCPP_POP_MACROS
71 
72 #endif // _LIBCPP___ALGORITHM_COPY_N_H
73