[libcxx-commits] [libcxx] ed27a4e - [libc++][PSTL] Implement std::equal (#72448)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 28 13:02:23 PST 2023


Author: Nikolas Klauser
Date: 2023-11-28T16:02:18-05:00
New Revision: ed27a4edb03809f0fe38f780765acfb97346c127

URL: https://github.com/llvm/llvm-project/commit/ed27a4edb03809f0fe38f780765acfb97346c127
DIFF: https://github.com/llvm/llvm-project/commit/ed27a4edb03809f0fe38f780765acfb97346c127.diff

LOG: [libc++][PSTL] Implement std::equal (#72448)

Differential Revision: https://reviews.llvm.org/D157131

Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>

Added: 
    libcxx/include/__algorithm/pstl_equal.h
    libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp
    libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp

Modified: 
    libcxx/docs/Status/PSTLPaper.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/__algorithm/pstl_backend.h
    libcxx/include/algorithm
    libcxx/include/module.modulemap.in

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/PSTLPaper.csv b/libcxx/docs/Status/PSTLPaper.csv
index f8cf950847c62cf..3fb5adfe3ec46aa 100644
--- a/libcxx/docs/Status/PSTLPaper.csv
+++ b/libcxx/docs/Status/PSTLPaper.csv
@@ -8,7 +8,7 @@ Section,Description,Assignee,Complete
 | `[alg.copy] <https://wg21.link/alg.copy>`_,std::copy_n,Nikolas Klauser,|Complete|
 | `[alg.count] <https://wg21.link/alg.count>`_,std::count,Nikolas Klauser,|Complete|
 | `[alg.count] <https://wg21.link/alg.count>`_,std::count_if,Nikolas Klauser,|Complete|
-| `[alg.equal] <https://wg21.link/alg.equal>`_,std::equal,Nikolas Klauser,|Not Started|
+| `[alg.equal] <https://wg21.link/alg.equal>`_,std::equal,Nikolas Klauser,|Complete|
 | `[alg.exclusive.scan] <https://wg21.link/alg.exclusive.scan>`_,std::exclusive_scan,Nikolas Klauser,|Not Started|
 | `[alg.exclusive.scan] <https://wg21.link/alg.exclusive.scan>`_,std::exclusive_scan,Nikolas Klauser,|Not Started|
 | `[alg.fill] <https://wg21.link/alg.fill>`_,std::fill,Nikolas Klauser,|Complete|

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index b0bc3718576f66b..f2f052d3b61ad47 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -87,6 +87,7 @@ set(files
   __algorithm/pstl_backends/cpu_backends/transform_reduce.h
   __algorithm/pstl_copy.h
   __algorithm/pstl_count.h
+  __algorithm/pstl_equal.h
   __algorithm/pstl_fill.h
   __algorithm/pstl_find.h
   __algorithm/pstl_for_each.h

diff  --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h
index a28e47dad8f399a..3af03ce2fbc8ee2 100644
--- a/libcxx/include/__algorithm/pstl_backend.h
+++ b/libcxx/include/__algorithm/pstl_backend.h
@@ -177,6 +177,9 @@ implemented, all the algorithms will eventually forward to the basis algorithms
   template <class _ExecutionPolicy, class _Iterator, class _Comp>
   optional<__empty> __pstl_sort(_Backend, _Iterator __first, _Iterator __last, _Comp __comp);
 
+  template <class _ExecutionPolicy, class _Iterator1, class _Iterator2, class _Comp>
+  optional<bool> __pstl_equal(_Backend, _Iterator1 first1, _Iterator1 last1, _Iterator2 first2, _Comp __comp);
+
 // TODO: Complete this list
 
 Exception handling

diff  --git a/libcxx/include/__algorithm/pstl_equal.h b/libcxx/include/__algorithm/pstl_equal.h
new file mode 100644
index 000000000000000..b343d2675980c9c
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -0,0 +1,170 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_PSTL_EQUAL_H
+#define _LIBCPP___ALGORITHM_PSTL_EQUAL_H
+
+#include <__algorithm/equal.h>
+#include <__algorithm/pstl_frontend_dispatch.h>
+#include <__config>
+#include <__functional/operations.h>
+#include <__iterator/iterator_traits.h>
+#include <__numeric/pstl_transform_reduce.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class>
+void __pstl_equal();
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          class _Pred,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
+__equal(_ExecutionPolicy&& __policy,
+        _ForwardIterator1&& __first1,
+        _ForwardIterator1&& __last1,
+        _ForwardIterator2&& __first2,
+        _Pred&& __pred) noexcept {
+  return std::__pstl_frontend_dispatch(
+      _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
+      [&__policy](
+          _ForwardIterator1 __g_first1, _ForwardIterator1 __g_last1, _ForwardIterator2 __g_first2, _Pred __g_pred) {
+        return std::__transform_reduce(
+            __policy,
+            std::move(__g_first1),
+            std::move(__g_last1),
+            std::move(__g_first2),
+            true,
+            std::logical_and{},
+            std::move(__g_pred));
+      },
+      std::move(__first1),
+      std::move(__last1),
+      std::move(__first2),
+      std::move(__pred));
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          class _Pred,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI bool
+equal(_ExecutionPolicy&& __policy,
+      _ForwardIterator1 __first1,
+      _ForwardIterator1 __last1,
+      _ForwardIterator2 __first2,
+      _Pred __pred) {
+  auto __res = std::__equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__pred));
+  if (!__res)
+    std::__throw_bad_alloc();
+  return *__res;
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI bool
+equal(_ExecutionPolicy&& __policy, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
+  return std::equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::equal_to{});
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          class _Pred,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
+__equal(_ExecutionPolicy&& __policy,
+        _ForwardIterator1&& __first1,
+        _ForwardIterator1&& __last1,
+        _ForwardIterator2&& __first2,
+        _ForwardIterator2&& __last2,
+        _Pred&& __pred) noexcept {
+  return std::__pstl_frontend_dispatch(
+      _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
+      [&__policy](_ForwardIterator1 __g_first1,
+                  _ForwardIterator1 __g_last1,
+                  _ForwardIterator2 __g_first2,
+                  _ForwardIterator2 __g_last2,
+                  _Pred __g_pred) -> optional<bool> {
+        if constexpr (__has_random_access_iterator_category<_ForwardIterator1>::value &&
+                      __has_random_access_iterator_category<_ForwardIterator2>::value) {
+          if (__g_last1 - __g_first1 != __g_last2 - __g_first2)
+            return false;
+          return std::__equal(
+              __policy, std::move(__g_first1), std::move(__g_last1), std::move(__g_first2), std::move(__g_pred));
+        } else {
+          (void)__policy; // Avoid unused lambda capture warning
+          return std::equal(
+              std::move(__g_first1),
+              std::move(__g_last1),
+              std::move(__g_first2),
+              std::move(__g_last2),
+              std::move(__g_pred));
+        }
+      },
+      std::move(__first1),
+      std::move(__last1),
+      std::move(__first2),
+      std::move(__last2),
+      std::move(__pred));
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          class _Pred,
+          class _RawPolicy                                    = __remove_cvref_t<_ExecutionPolicy>,
+          enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI bool
+equal(_ExecutionPolicy&& __policy,
+      _ForwardIterator1 __first1,
+      _ForwardIterator1 __last1,
+      _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2,
+      _Pred __pred) {
+  auto __res = std::__equal(
+      __policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::move(__pred));
+  if (!__res)
+    std::__throw_bad_alloc();
+  return *__res;
+}
+
+template <class _ExecutionPolicy,
+          class _ForwardIterator1,
+          class _ForwardIterator2,
+          enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
+_LIBCPP_HIDE_FROM_ABI bool
+equal(_ExecutionPolicy&& __policy,
+      _ForwardIterator1 __first1,
+      _ForwardIterator1 __last1,
+      _ForwardIterator2 __first2,
+      _ForwardIterator2 __last2) {
+  return std::equal(
+      __policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::equal_to{});
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+#endif // _LIBCPP___ALGORITHM_PSTL_EQUAL_H

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 578de74d401dc2c..627e7d20213fe80 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -1826,6 +1826,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/pstl_any_all_none_of.h>
 #include <__algorithm/pstl_copy.h>
 #include <__algorithm/pstl_count.h>
+#include <__algorithm/pstl_equal.h>
 #include <__algorithm/pstl_fill.h>
 #include <__algorithm/pstl_find.h>
 #include <__algorithm/pstl_for_each.h>

diff  --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 90381f4f7f720d4..cf7475b0b86af8b 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -748,6 +748,7 @@ module std_private_algorithm_pstl_backends_cpu_backends_transform        [system
 module std_private_algorithm_pstl_backends_cpu_backends_transform_reduce [system] { header "__algorithm/pstl_backends/cpu_backends/transform_reduce.h" }
 module std_private_algorithm_pstl_copy                                   [system] { header "__algorithm/pstl_copy.h" }
 module std_private_algorithm_pstl_count                                  [system] { header "__algorithm/pstl_count.h" }
+module std_private_algorithm_pstl_equal                                  [system] { header "__algorithm/pstl_equal.h" }
 module std_private_algorithm_pstl_fill                                   [system] { header "__algorithm/pstl_fill.h" }
 module std_private_algorithm_pstl_find                                   [system] {
   header "__algorithm/pstl_find.h"

diff  --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp
new file mode 100644
index 000000000000000..e2f4dafe1a05d80
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp
@@ -0,0 +1,154 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
+//   bool equal(ExecutionPolicy&& exec,
+//              ForwardIterator1 first1, ForwardIterator1 last1,
+//              ForwardIterator2 first2);
+//
+// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
+//          class BinaryPredicate>
+//   bool equal(ExecutionPolicy&& exec,
+//              ForwardIterator1 first1, ForwardIterator1 last1,
+//              ForwardIterator2 first2, BinaryPredicate pred);
+//
+// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
+//   bool equal(ExecutionPolicy&& exec,
+//              ForwardIterator1 first1, ForwardIterator1 last1,
+//              ForwardIterator2 first2, ForwardIterator2 last2);
+//
+// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
+//          class BinaryPredicate>
+//   bool equal(ExecutionPolicy&& exec,
+//              ForwardIterator1 first1, ForwardIterator1 last1,
+//              ForwardIterator2 first2, ForwardIterator2 last2,
+//              BinaryPredicate pred);
+
+#include <algorithm>
+#include <cassert>
+#include <iterator>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+template <class It1, class It2>
+struct Test {
+  template <class Policy>
+  void operator()(Policy&& policy) {
+    { // 3 iter overloads
+      // check with equal ranges
+      {
+        int a[] = {1, 2, 3, 4};
+        int b[] = {1, 2, 3, 4};
+        assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b))));
+      }
+
+      // check with an empty range
+      {
+        int a[] = {999};
+        int b[] = {1, 2, 3};
+        assert(std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b))));
+      }
+
+      // check with 
diff erent ranges
+      {
+        int a[] = {1, 2, 3};
+        int b[] = {3, 2, 1};
+        assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b))));
+      }
+
+      // check that the predicate is used
+      {
+        int a[] = {2, 4, 6, 8, 10};
+        int b[] = {12, 14, 16, 18, 20};
+        assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), [](int lhs, int rhs) {
+          return lhs % 2 == rhs % 2;
+        }));
+      }
+    }
+
+    { // 4 iter overloads
+      // check with equal ranges of equal size
+      {
+        int a[] = {1, 2, 3, 4};
+        int b[] = {1, 2, 3, 4};
+        assert(std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
+      }
+
+      // check with unequal ranges of equal size
+      {
+        int a[] = {1, 2, 3, 4};
+        int b[] = {4, 3, 2, 1};
+        assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
+      }
+
+      // check with equal ranges of unequal size
+      {
+        {
+          int a[] = {1, 2, 3, 4};
+          int b[] = {1, 2, 3, 4, 5};
+          assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
+        }
+        {
+          int a[] = {1, 2, 3, 4, 5};
+          int b[] = {1, 2, 3, 4};
+          assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b))));
+        }
+      }
+
+      // check empty ranges
+      {
+        // empty/empty
+        {
+          int a[] = {888};
+          int b[] = {999};
+          assert(std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b)), It2(std::begin(b))));
+        }
+        // empty/non-empty
+        {
+          int a[] = {999};
+          int b[] = {999};
+          assert(!std::equal(policy, It1(std::begin(a)), It1(std::begin(a)), It2(std::begin(b)), It2(std::end(b))));
+        }
+        // non-empty/empty
+        {
+          int a[] = {999};
+          int b[] = {999};
+          assert(!std::equal(policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::begin(b))));
+        }
+      }
+
+      // check that the predicate is used
+      {
+        int a[] = {2, 4, 6, 8, 10};
+        int b[] = {12, 14, 16, 18, 20};
+        assert(std::equal(
+            policy, It1(std::begin(a)), It1(std::end(a)), It2(std::begin(b)), It2(std::end(b)), [](int lhs, int rhs) {
+              return lhs % 2 == rhs % 2;
+            }));
+      }
+    }
+  }
+};
+
+int main(int, char**) {
+  types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
+                    using It1 = typename decltype(v)::type;
+                    types::for_each(
+                        types::forward_iterator_list<int*>{},
+                        TestIteratorWithPolicies<types::partial_instantiation<Test, It1>::template apply>{});
+                  }});
+  return 0;
+}

diff  --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp
new file mode 100644
index 000000000000000..4c2f9946165e7a9
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: no-exceptions
+// REQUIRES: has-unix-headers
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// check that std::equal(ExecutionPolicy) terminates on user-thrown exceptions
+
+#include <algorithm>
+
+#include "check_assertion.h"
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+
+int main(int, char**) {
+  test_execution_policies([](auto&& policy) {
+    EXPECT_STD_TERMINATE([&] {
+      try {
+        int a[] = {1, 2};
+        int b[] = {1, 2};
+        (void)std::equal(policy,
+                         util::throw_on_move_iterator(std::begin(a), 1),
+                         util::throw_on_move_iterator(std::end(a), 1),
+                         util::throw_on_move_iterator(std::begin(b), 1));
+      } catch (const util::iterator_error&) {
+        assert(false);
+      }
+    });
+    EXPECT_STD_TERMINATE([&] {
+      try {
+        int a[] = {1, 2};
+        int b[] = {1, 2};
+        (void)std::equal(
+            policy,
+            util::throw_on_move_iterator(std::begin(a), 1),
+            util::throw_on_move_iterator(std::end(a), 1),
+            util::throw_on_move_iterator(std::begin(b), 1),
+            util::throw_on_move_iterator(std::end(b), 1));
+      } catch (const util::iterator_error&) {
+        assert(false);
+      }
+    });
+  });
+}


        


More information about the libcxx-commits mailing list