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_RANGES_COPY_N_H
10 #define _LIBCPP___ALGORITHM_RANGES_COPY_N_H
11 
12 #include <__algorithm/copy.h>
13 #include <__algorithm/in_out_result.h>
14 #include <__algorithm/ranges_copy.h>
15 #include <__config>
16 #include <__functional/identity.h>
17 #include <__iterator/concepts.h>
18 #include <__iterator/incrementable_traits.h>
19 #include <__iterator/unreachable_sentinel.h>
20 #include <__iterator/wrap_iter.h>
21 #include <__utility/move.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
30 
31 namespace ranges {
32 
33 template <class _Ip, class _Op>
34 using copy_n_result = in_out_result<_Ip, _Op>;
35 
36 namespace __copy_n {
37 struct __fn {
38 
39   template <class _InIter, class _DiffType, class _OutIter>
40   _LIBCPP_HIDE_FROM_ABI constexpr static
41   copy_n_result<_InIter, _OutIter> __go(_InIter __first, _DiffType __n, _OutIter __result) {
42     while (__n != 0) {
43       *__result = *__first;
44       ++__first;
45       ++__result;
46       --__n;
47     }
48     return {std::move(__first), std::move(__result)};
49   }
50 
51   template <random_access_iterator _InIter, class _DiffType, random_access_iterator _OutIter>
52   _LIBCPP_HIDE_FROM_ABI constexpr static
53   copy_n_result<_InIter, _OutIter> __go(_InIter __first, _DiffType __n, _OutIter __result) {
54     auto __ret = std::__copy(__first, __first + __n, __result);
55     return {__ret.first, __ret.second};
56   }
57 
58   template <input_iterator _Ip, weakly_incrementable _Op>
59     requires indirectly_copyable<_Ip, _Op>
60   _LIBCPP_HIDE_FROM_ABI constexpr
61   copy_n_result<_Ip, _Op> operator()(_Ip __first, iter_difference_t<_Ip> __n, _Op __result) const {
62     return __go(std::move(__first), __n, std::move(__result));
63   }
64 };
65 } // namespace __copy_n
66 
67 inline namespace __cpo {
68   inline constexpr auto copy_n = __copy_n::__fn{};
69 } // namespace __cpo
70 } // namespace ranges
71 
72 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
73 
74 _LIBCPP_END_NAMESPACE_STD
75 
76 #endif // _LIBCPP___ALGORITHM_RANGES_COPY_N_H
77