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