[libcxx-commits] [libcxx] [libc++][pstl] Default implementation of parallel std::adjacent_difference (PR #207585)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 8 08:48:27 PDT 2026


================
@@ -529,6 +530,34 @@ struct __rotate_copy<__default_backend_tag, _ExecutionPolicy> {
   }
 };
 
+template <class _ExecutionPolicy>
+struct __adjacent_difference<__default_backend_tag, _ExecutionPolicy> {
+  template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator2>
+  operator()(_Policy&& __policy,
+             _ForwardIterator1 __first1,
+             _ForwardIterator1 __last1,
+             _ForwardIterator2 __first2,
+             _BinaryOperation&& __op) const noexcept {
+    using _TransformBinary = __dispatch<__transform_binary, __current_configuration, _ExecutionPolicy>;
+    if (__first1 == __last1)
+      return __first2; // edge case: empty input range, just return the output iterator
+    *__first2 = *__first1;
+    ++__first2;
+    _ForwardIterator1 __first_left = std::next(__first1);
+    if (__first_left == __last1)
+      return __first2; // edge case: not enough elements to perform adjacent difference, just return the output iterator
+    // Process as a binary transform of two iterator ranges: [__first1 + 1, __last1) and [__first1, __last1 - 1)
+    return _TransformBinary()(
+        __policy,
+        std::move(__first_left),
+        std::move(__last1),
+        std::move(__first1),
+        std::move(__first2),
----------------
ldionne wrote:

```suggestion
        std::move(__first2),
        std::move(__last1),
        std::move(__first1),
        std::move(__result),
```

https://github.com/llvm/llvm-project/pull/207585


More information about the libcxx-commits mailing list