copy_n.h 2.0 KB

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