pstl_fill.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_FILL_H
  9. #define _LIBCPP___ALGORITHM_PSTL_FILL_H
  10. #include <__algorithm/fill_n.h>
  11. #include <__algorithm/pstl_for_each.h>
  12. #include <__algorithm/pstl_frontend_dispatch.h>
  13. #include <__config>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/cpp17_iterator_concepts.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__type_traits/enable_if.h>
  18. #include <__type_traits/is_execution_policy.h>
  19. #include <__type_traits/remove_cvref.h>
  20. #include <__utility/move.h>
  21. #include <optional>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_PUSH_MACROS
  26. #include <__undef_macros>
  27. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. template <class>
  30. void __pstl_fill(); // declaration needed for the frontend dispatch below
  31. template <class _ExecutionPolicy,
  32. class _ForwardIterator,
  33. class _Tp,
  34. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  35. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  36. _LIBCPP_HIDE_FROM_ABI optional<__empty>
  37. __fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
  38. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  39. return std::__pstl_frontend_dispatch(
  40. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill, _RawPolicy),
  41. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) {
  42. return std::__for_each(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
  43. __element = __g_value;
  44. });
  45. },
  46. std::move(__first),
  47. std::move(__last),
  48. __value);
  49. }
  50. template <class _ExecutionPolicy,
  51. class _ForwardIterator,
  52. class _Tp,
  53. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  54. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  55. _LIBCPP_HIDE_FROM_ABI void
  56. fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  57. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  58. if (!std::__fill(__policy, std::move(__first), std::move(__last), __value))
  59. std::__throw_bad_alloc();
  60. }
  61. template <class>
  62. void __pstl_fill_n(); // declaration needed for the frontend dispatch below
  63. template <class _ExecutionPolicy,
  64. class _ForwardIterator,
  65. class _SizeT,
  66. class _Tp,
  67. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  68. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  69. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
  70. __fill_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _SizeT&& __n, const _Tp& __value) noexcept {
  71. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  72. return std::__pstl_frontend_dispatch(
  73. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill_n, _RawPolicy),
  74. [&](_ForwardIterator __g_first, _SizeT __g_n, const _Tp& __g_value) {
  75. if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value)
  76. std::fill(__policy, __g_first, __g_first + __g_n, __g_value);
  77. else
  78. std::fill_n(__g_first, __g_n, __g_value);
  79. return optional<__empty>{__empty{}};
  80. },
  81. std::move(__first),
  82. std::move(__n),
  83. __value);
  84. }
  85. template <class _ExecutionPolicy,
  86. class _ForwardIterator,
  87. class _SizeT,
  88. class _Tp,
  89. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  90. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  91. _LIBCPP_HIDE_FROM_ABI void
  92. fill_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _SizeT __n, const _Tp& __value) {
  93. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  94. if (!std::__fill_n(__policy, std::move(__first), std::move(__n), __value))
  95. std::__throw_bad_alloc();
  96. }
  97. _LIBCPP_END_NAMESPACE_STD
  98. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  99. _LIBCPP_POP_MACROS
  100. #endif // _LIBCPP___ALGORITHM_PSTL_FILL_H