xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__algorithm/copy_n.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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___CXX03___ALGORITHM_COPY_N_H
10 #define _LIBCPP___CXX03___ALGORITHM_COPY_N_H
11 
12 #include <__cxx03/__algorithm/copy.h>
13 #include <__cxx03/__config>
14 #include <__cxx03/__iterator/iterator_traits.h>
15 #include <__cxx03/__type_traits/enable_if.h>
16 #include <__cxx03/__utility/convert_to_integral.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 template <class _InputIterator,
25           class _Size,
26           class _OutputIterator,
27           __enable_if_t<__has_input_iterator_category<_InputIterator>::value &&
28                             !__has_random_access_iterator_category<_InputIterator>::value,
29                         int> = 0>
copy_n(_InputIterator __first,_Size __orig_n,_OutputIterator __result)30 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result) {
31   typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
32   _IntegralSize __n = __orig_n;
33   if (__n > 0) {
34     *__result = *__first;
35     ++__result;
36     for (--__n; __n > 0; --__n) {
37       ++__first;
38       *__result = *__first;
39       ++__result;
40     }
41   }
42   return __result;
43 }
44 
45 template <class _InputIterator,
46           class _Size,
47           class _OutputIterator,
48           __enable_if_t<__has_random_access_iterator_category<_InputIterator>::value, int> = 0>
copy_n(_InputIterator __first,_Size __orig_n,_OutputIterator __result)49 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result) {
50   typedef typename iterator_traits<_InputIterator>::difference_type difference_type;
51   typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
52   _IntegralSize __n = __orig_n;
53   return std::copy(__first, __first + difference_type(__n), __result);
54 }
55 
56 _LIBCPP_END_NAMESPACE_STD
57 
58 #endif // _LIBCPP___CXX03___ALGORITHM_COPY_N_H
59