File indexing completed on 2026-05-03 08:13:37
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___PSTL_CPU_ALGOS_TRANSFORM_H
0010 #define _LIBCPP___CXX03___PSTL_CPU_ALGOS_TRANSFORM_H
0011
0012 #include <__cxx03/__algorithm/transform.h>
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__iterator/concepts.h>
0016 #include <__cxx03/__iterator/iterator_traits.h>
0017 #include <__cxx03/__pstl/backend_fwd.h>
0018 #include <__cxx03/__pstl/cpu_algos/cpu_traits.h>
0019 #include <__cxx03/__type_traits/is_execution_policy.h>
0020 #include <__cxx03/__utility/move.h>
0021 #include <__cxx03/optional>
0022
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 # pragma GCC system_header
0025 #endif
0026
0027 _LIBCPP_PUSH_MACROS
0028 #include <__cxx03/__undef_macros>
0029
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 namespace __pstl {
0032
0033 template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function>
0034 _LIBCPP_HIDE_FROM_ABI _Iterator2
0035 __simd_transform(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Function __f) noexcept {
0036 _PSTL_PRAGMA_SIMD
0037 for (_DifferenceType __i = 0; __i < __n; ++__i)
0038 __f(__first1[__i], __first2[__i]);
0039 return __first2 + __n;
0040 }
0041
0042 template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Iterator3, class _Function>
0043 _LIBCPP_HIDE_FROM_ABI _Iterator3 __simd_transform(
0044 _Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Iterator3 __first3, _Function __f) noexcept {
0045 _PSTL_PRAGMA_SIMD
0046 for (_DifferenceType __i = 0; __i < __n; ++__i)
0047 __f(__first1[__i], __first2[__i], __first3[__i]);
0048 return __first3 + __n;
0049 }
0050
0051 template <class _Backend, class _RawExecutionPolicy>
0052 struct __cpu_parallel_transform {
0053 template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _UnaryOperation>
0054 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
0055 operator()(_Policy&& __policy,
0056 _ForwardIterator __first,
0057 _ForwardIterator __last,
0058 _ForwardOutIterator __result,
0059 _UnaryOperation __op) const noexcept {
0060 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0061 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
0062 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0063 __cpu_traits<_Backend>::__for_each(
0064 __first,
0065 __last,
0066 [&__policy, __op, __first, __result](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
0067 using _TransformUnseq = __pstl::__transform<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0068 auto __res = _TransformUnseq()(
0069 std::__remove_parallel_policy(__policy),
0070 __brick_first,
0071 __brick_last,
0072 __result + (__brick_first - __first),
0073 __op);
0074 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
0075 return *std::move(__res);
0076 });
0077 return __result + (__last - __first);
0078 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0079 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
0080 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0081 return __pstl::__simd_transform(
0082 __first,
0083 __last - __first,
0084 __result,
0085 [&](__iter_reference<_ForwardIterator> __in_value, __iter_reference<_ForwardOutIterator> __out_value) {
0086 __out_value = __op(__in_value);
0087 });
0088 } else {
0089 return std::transform(__first, __last, __result, __op);
0090 }
0091 }
0092 };
0093
0094 template <class _Backend, class _RawExecutionPolicy>
0095 struct __cpu_parallel_transform_binary {
0096 template <class _Policy,
0097 class _ForwardIterator1,
0098 class _ForwardIterator2,
0099 class _ForwardOutIterator,
0100 class _BinaryOperation>
0101 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
0102 operator()(_Policy&& __policy,
0103 _ForwardIterator1 __first1,
0104 _ForwardIterator1 __last1,
0105 _ForwardIterator2 __first2,
0106 _ForwardOutIterator __result,
0107 _BinaryOperation __op) const noexcept {
0108 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0109 __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
0110 __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
0111 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0112 auto __res = __cpu_traits<_Backend>::__for_each(
0113 __first1,
0114 __last1,
0115 [&__policy, __op, __first1, __first2, __result](
0116 _ForwardIterator1 __brick_first, _ForwardIterator1 __brick_last) {
0117 using _TransformBinaryUnseq =
0118 __pstl::__transform_binary<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0119 return _TransformBinaryUnseq()(
0120 std::__remove_parallel_policy(__policy),
0121 __brick_first,
0122 __brick_last,
0123 __first2 + (__brick_first - __first1),
0124 __result + (__brick_first - __first1),
0125 __op);
0126 });
0127 if (!__res)
0128 return nullopt;
0129 return __result + (__last1 - __first1);
0130 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0131 __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
0132 __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
0133 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0134 return __pstl::__simd_transform(
0135 __first1,
0136 __last1 - __first1,
0137 __first2,
0138 __result,
0139 [&](__iter_reference<_ForwardIterator1> __in1,
0140 __iter_reference<_ForwardIterator2> __in2,
0141 __iter_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in1, __in2); });
0142 } else {
0143 return std::transform(__first1, __last1, __first2, __result, __op);
0144 }
0145 }
0146 };
0147
0148 }
0149 _LIBCPP_END_NAMESPACE_STD
0150
0151 _LIBCPP_POP_MACROS
0152
0153 #endif