pstl_merge.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_MERGE_H
  9. #define _LIBCPP___ALGORITHM_PSTL_MERGE_H
  10. #include <__algorithm/pstl_backend.h>
  11. #include <__config>
  12. #include <__functional/operations.h>
  13. #include <__type_traits/enable_if.h>
  14. #include <__type_traits/is_execution_policy.h>
  15. #include <__type_traits/remove_cvref.h>
  16. #include <__utility/move.h>
  17. #include <optional>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. template <class _ExecutionPolicy,
  26. class _ForwardIterator1,
  27. class _ForwardIterator2,
  28. class _ForwardOutIterator,
  29. class _Comp = std::less<>,
  30. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  31. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  32. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
  33. __merge(_ExecutionPolicy&&,
  34. _ForwardIterator1 __first1,
  35. _ForwardIterator1 __last1,
  36. _ForwardIterator2 __first2,
  37. _ForwardIterator2 __last2,
  38. _ForwardOutIterator __result,
  39. _Comp __comp = {}) noexcept {
  40. using _Backend = typename __select_backend<_RawPolicy>::type;
  41. return std::__pstl_merge<_RawPolicy>(
  42. _Backend{},
  43. std::move(__first1),
  44. std::move(__last1),
  45. std::move(__first2),
  46. std::move(__last2),
  47. std::move(__result),
  48. std::move(__comp));
  49. }
  50. template <class _ExecutionPolicy,
  51. class _ForwardIterator1,
  52. class _ForwardIterator2,
  53. class _ForwardOutIterator,
  54. class _Comp = std::less<>,
  55. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  56. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  57. _LIBCPP_HIDE_FROM_ABI _ForwardOutIterator
  58. merge(_ExecutionPolicy&& __policy,
  59. _ForwardIterator1 __first1,
  60. _ForwardIterator1 __last1,
  61. _ForwardIterator2 __first2,
  62. _ForwardIterator2 __last2,
  63. _ForwardOutIterator __result,
  64. _Comp __comp = {}) {
  65. auto __res = std::__merge(
  66. __policy,
  67. std::move(__first1),
  68. std::move(__last1),
  69. std::move(__first2),
  70. std::move(__last2),
  71. std::move(__result),
  72. std::move(__comp));
  73. if (!__res)
  74. std::__throw_bad_alloc();
  75. return *std::move(__res);
  76. }
  77. _LIBCPP_END_NAMESPACE_STD
  78. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  79. _LIBCPP_POP_MACROS
  80. #endif // _LIBCPP___ALGORITHM_PSTL_MERGE_H