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