pstl_move.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_MOVE_H
  9. #define _LIBCPP___ALGORITHM_PSTL_MOVE_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/iterator_traits.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 <optional>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_PUSH_MACROS
  27. #include <__undef_macros>
  28. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. // TODO: Use the std::copy/move shenanigans to forward to std::memmove
  31. // Investigate whether we want to still forward to std::transform(policy)
  32. // in that case for the execution::par part, or whether we actually want
  33. // to run everything serially in that case.
  34. template <class>
  35. void __pstl_move();
  36. template <class _ExecutionPolicy,
  37. class _ForwardIterator,
  38. class _ForwardOutIterator,
  39. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  40. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  41. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
  42. __move(_ExecutionPolicy&& __policy,
  43. _ForwardIterator&& __first,
  44. _ForwardIterator&& __last,
  45. _ForwardOutIterator&& __result) noexcept {
  46. return std::__pstl_frontend_dispatch(
  47. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_move, _RawPolicy),
  48. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _ForwardOutIterator __g_result) {
  49. return std::__transform(__policy, __g_first, __g_last, __g_result, [](auto&& __v) { return std::move(__v); });
  50. },
  51. std::move(__first),
  52. std::move(__last),
  53. std::move(__result));
  54. }
  55. template <class _ExecutionPolicy,
  56. class _ForwardIterator,
  57. class _ForwardOutIterator,
  58. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  59. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  60. _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
  61. move(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __result) {
  62. auto __res = std::__move(__policy, std::move(__first), std::move(__last), std::move(__result));
  63. if (!__res)
  64. std::__throw_bad_alloc();
  65. return *__res;
  66. }
  67. _LIBCPP_END_NAMESPACE_STD
  68. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  69. _LIBCPP_POP_MACROS
  70. #endif // _LIBCPP___ALGORITHM_PSTL_MOVE_H