pstl_copy.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_PSTL_COPY_H
  9. #define _LIBCPP___ALGORITHM_PSTL_COPY_H
  10. #include <__algorithm/copy_n.h>
  11. #include <__algorithm/pstl_backend.h>
  12. #include <__algorithm/pstl_frontend_dispatch.h>
  13. #include <__algorithm/pstl_transform.h>
  14. #include <__config>
  15. #include <__functional/identity.h>
  16. #include <__iterator/concepts.h>
  17. #include <__type_traits/enable_if.h>
  18. #include <__type_traits/is_constant_evaluated.h>
  19. #include <__type_traits/is_execution_policy.h>
  20. #include <__type_traits/is_trivially_copyable.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__utility/move.h>
  23. #include <optional>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. // TODO: Use the std::copy/move shenanigans to forward to std::memmove
  32. template <class>
  33. void __pstl_copy();
  34. template <class _ExecutionPolicy,
  35. class _ForwardIterator,
  36. class _ForwardOutIterator,
  37. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  38. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  39. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
  40. __copy(_ExecutionPolicy&& __policy,
  41. _ForwardIterator&& __first,
  42. _ForwardIterator&& __last,
  43. _ForwardOutIterator&& __result) noexcept {
  44. return std::__pstl_frontend_dispatch(
  45. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy, _RawPolicy),
  46. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _ForwardOutIterator __g_result) {
  47. return std::__transform(__policy, __g_first, __g_last, __g_result, __identity());
  48. },
  49. std::move(__first),
  50. std::move(__last),
  51. std::move(__result));
  52. }
  53. template <class _ExecutionPolicy,
  54. class _ForwardIterator,
  55. class _ForwardOutIterator,
  56. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  57. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  58. _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
  59. copy(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __result) {
  60. auto __res = std::__copy(__policy, std::move(__first), std::move(__last), std::move(__result));
  61. if (!__res)
  62. std::__throw_bad_alloc();
  63. return *std::move(__res);
  64. }
  65. template <class>
  66. void __pstl_copy_n();
  67. template <class _ExecutionPolicy,
  68. class _ForwardIterator,
  69. class _ForwardOutIterator,
  70. class _Size,
  71. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  72. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  73. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> __copy_n(
  74. _ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __n, _ForwardOutIterator&& __result) noexcept {
  75. return std::__pstl_frontend_dispatch(
  76. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy_n, _RawPolicy),
  77. [&__policy](
  78. _ForwardIterator __g_first, _Size __g_n, _ForwardOutIterator __g_result) -> optional<_ForwardIterator> {
  79. if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value)
  80. return std::__copy(__policy, std::move(__g_first), std::move(__g_first + __g_n), std::move(__g_result));
  81. else
  82. return std::copy_n(__g_first, __g_n, __g_result);
  83. },
  84. std::move(__first),
  85. std::move(__n),
  86. std::move(__result));
  87. }
  88. template <class _ExecutionPolicy,
  89. class _ForwardIterator,
  90. class _ForwardOutIterator,
  91. class _Size,
  92. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  93. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  94. _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
  95. copy_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n, _ForwardOutIterator __result) {
  96. auto __res = std::__copy_n(__policy, std::move(__first), std::move(__n), std::move(__result));
  97. if (!__res)
  98. std::__throw_bad_alloc();
  99. return *std::move(__res);
  100. }
  101. _LIBCPP_END_NAMESPACE_STD
  102. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  103. _LIBCPP_POP_MACROS
  104. #endif // _LIBCPP___ALGORITHM_PSTL_COPY_H