copy_n.h 2.0 KB

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