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 <__algorithm/in_out_result.h>
14#include <__algorithm/iterator_operations.h>
15#include <__config>
16#include <__iterator/iterator_traits.h>
17#include <__type_traits/enable_if.h>
18#include <__utility/convert_to_integral.h>
19#include <__utility/move.h>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_PUSH_MACROS
26#include <__undef_macros>
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30template <class _AlgPolicy,
31 class _InIter,
32 class _OutIter,
33 __enable_if_t<__has_random_access_iterator_category<_InIter>::value, int> = 0>
34_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __in_out_result<_InIter, _OutIter>
35__copy_n(_InIter __first, typename _IterOps<_AlgPolicy>::template __difference_type<_InIter> __n, _OutIter __result) {
36 if (__n <= 0)
37 return {std::move(__first), std::move(__result)};
38 return std::__copy(__first, __first + __n, std::move(__result));
39}
40
41template <class _AlgPolicy,
42 class _InIter,
43 class _OutIter,
44 __enable_if_t<!__has_random_access_iterator_category<_InIter>::value, int> = 0>
45_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __in_out_result<_InIter, _OutIter>
46__copy_n(_InIter __first, typename _IterOps<_AlgPolicy>::template __difference_type<_InIter> __n, _OutIter __result) {
47 while (__n > 0) {
48 *__result = *__first;
49 ++__first;
50 ++__result;
51 --__n;
52 }
53 return {std::move(__first), std::move(__result)};
54}
55
56// The InputIterator case is handled specially here because it's been written in a way to avoid incrementing __first
57// if not absolutely required. This was done to allow its use with istream_iterator and we want to avoid breaking
58// people, at least currently.
59// See https://github.com/llvm/llvm-project/commit/99847d2bf132854fffa019bab19818768102ccad
60template <class _InputIterator,
61 class _Size,
62 class _OutputIterator,
63 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
64_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
65copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) {
66 using _IntegralSize = decltype(std::__convert_to_integral(__n));
67 _IntegralSize __converted = __n;
68 if (__converted > 0) {
69 *__result = *__first;
70 ++__result;
71 for (--__converted; __converted > 0; --__converted) {
72 ++__first;
73 *__result = *__first;
74 ++__result;
75 }
76 }
77 return __result;
78}
79
80template <class _InputIterator,
81 class _Size,
82 class _OutputIterator,
83 __enable_if_t<!__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
84_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
85copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) {
86 using _IntegralSize = decltype(std::__convert_to_integral(__n));
87 _IntegralSize __converted = __n;
88 return std::__copy_n<_ClassicAlgPolicy>(__first, __iterator_difference_type<_InputIterator>(__converted), __result)
89 .__out_;
90}
91
92_LIBCPP_END_NAMESPACE_STD
93
94_LIBCPP_POP_MACROS
95
96#endif // _LIBCPP___ALGORITHM_COPY_N_H
97