copy.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_H
  9. #define _LIBCPP___ALGORITHM_COPY_H
  10. #include <__algorithm/unwrap_iter.h>
  11. #include <__config>
  12. #include <__iterator/iterator_traits.h>
  13. #include <cstring>
  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. // copy
  20. template <class _InputIterator, class _OutputIterator>
  21. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  22. _OutputIterator
  23. __copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  24. {
  25. for (; __first != __last; ++__first, (void) ++__result)
  26. *__result = *__first;
  27. return __result;
  28. }
  29. template <class _InputIterator, class _OutputIterator>
  30. inline _LIBCPP_INLINE_VISIBILITY
  31. _OutputIterator
  32. __copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  33. {
  34. return _VSTD::__copy_constexpr(__first, __last, __result);
  35. }
  36. template <class _Tp, class _Up>
  37. inline _LIBCPP_INLINE_VISIBILITY
  38. typename enable_if
  39. <
  40. is_same<typename remove_const<_Tp>::type, _Up>::value &&
  41. is_trivially_copy_assignable<_Up>::value,
  42. _Up*
  43. >::type
  44. __copy(_Tp* __first, _Tp* __last, _Up* __result)
  45. {
  46. const size_t __n = static_cast<size_t>(__last - __first);
  47. if (__n > 0)
  48. _VSTD::memmove(__result, __first, __n * sizeof(_Up));
  49. return __result + __n;
  50. }
  51. template <class _InputIterator, class _OutputIterator>
  52. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  53. _OutputIterator
  54. copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  55. {
  56. if (__libcpp_is_constant_evaluated()) {
  57. return _VSTD::__copy_constexpr(__first, __last, __result);
  58. } else {
  59. return _VSTD::__rewrap_iter(__result,
  60. _VSTD::__copy(_VSTD::__unwrap_iter(__first),
  61. _VSTD::__unwrap_iter(__last),
  62. _VSTD::__unwrap_iter(__result)));
  63. }
  64. }
  65. _LIBCPP_END_NAMESPACE_STD
  66. #endif // _LIBCPP___ALGORITHM_COPY_H