[libcxx-commits] [libcxx] [libc++][PSTL] Implement std::equal (PR #72448)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 28 06:49:51 PST 2023
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/72448
>From e26d027e424e2b2a9467ef6727cf8b9ff29ba759 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 14 Nov 2023 23:56:49 +0100
Subject: [PATCH 1/6] [libc++][PSTL] Implement std::equal
Spies: ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D157131
---
libcxx/include/CMakeLists.txt | 1 +
libcxx/include/__algorithm/pstl_equal.h | 167 ++++++++++++++++++
libcxx/include/algorithm | 1 +
libcxx/include/module.modulemap.in | 1 +
.../alg.equal/pstl.equal.pass.cpp | 98 ++++++++++
.../pstl.exception_handling.pass.cpp | 59 +++++++
6 files changed, 327 insertions(+)
create mode 100644 libcxx/include/__algorithm/pstl_equal.h
create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp
create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp
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_equal.h b/libcxx/include/__algorithm/pstl_equal.h
new file mode 100644
index 000000000000000..9a296a37a153e9c
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -0,0 +1,167 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/pstl_frontend_dispatch.h>
+#include <__config>
+#include <__functional/operations.h>
+#include <__numeric/pstl_transform_reduce.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),
+ [&__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),
+ [&__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 captue 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..4788e564e953980
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp
@@ -0,0 +1,98 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// UNSUPPORETD: 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 "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+template <class Iter1, class Iter2>
+struct Test {
+ template <class Policy>
+ void operator()(Policy&& policy) {
+ { // 3 iter overloads
+ int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int b[] = {1, 2, 3, 5, 6, 7, 8, 9};
+
+ // simple test
+ assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a))));
+
+ // check that false is returned on different ranges
+ assert(!std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b))));
+
+ // check that the predicate is used
+ assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b)), [](int lhs, int rhs) {
+ return lhs == rhs || lhs + 1 == rhs || rhs + 1 == lhs;
+ }));
+ }
+
+ { // 4 iter overloads
+ int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ int b[] = {1, 2, 3, 5, 6, 7, 8, 9};
+
+ // simple test
+ assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a)), Iter2(std::end(a))));
+
+ // check that false is returned on different ranges
+ assert(!std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b)), Iter2(std::end(b))));
+
+ // check that false is returned on different sized ranges
+ assert(
+ !std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a)), Iter2(std::end(a) - 1)));
+
+ // check that the predicate is used
+ assert(std::equal(
+ policy,
+ Iter1(std::begin(a)),
+ Iter1(std::end(a)),
+ Iter2(std::begin(b)),
+ Iter2(std::end(b)),
+ [](int lhs, int rhs) { return lhs == rhs || lhs + 1 == rhs || rhs + 1 == lhs; }));
+ }
+ }
+};
+
+int main(int, char**) {
+ types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {
+ using Iter1 = typename decltype(v)::type;
+ types::for_each(
+ types::forward_iterator_list<int*>{},
+ TestIteratorWithPolicies<types::partial_instantiation<Test, Iter1>::template apply>{});
+ }});
+}
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..4d93850ca96f1f3
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// 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"
+
+struct ThrowOnCompare {};
+
+#ifndef TEST_HAS_NO_EXCEPTIONS
+bool operator==(ThrowOnCompare, ThrowOnCompare) { throw int{}; }
+#endif
+
+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);
+ }
+ std::terminate(); // make the test pass in case the algorithm didn't move the iterator
+ });
+ 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);
+ }
+ std::terminate(); // make the test pass in case the algorithm didn't move the iterator
+ });
+ });
+}
>From aa8938865209b0964266eb694968e7c9c4272040 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 23 Nov 2023 13:44:28 -0500
Subject: [PATCH 2/6] Apply review comments
---
libcxx/docs/Status/PSTLPaper.csv | 2 +-
libcxx/include/__algorithm/pstl_backend.h | 3 +
libcxx/include/__algorithm/pstl_equal.h | 7 +-
.../alg.equal/pstl.equal.pass.cpp | 122 +++++++++++++-----
.../pstl.exception_handling.pass.cpp | 8 --
5 files changed, 97 insertions(+), 45 deletions(-)
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/__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
index 9a296a37a153e9c..3403ee60a344654 100644
--- a/libcxx/include/__algorithm/pstl_equal.h
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -13,6 +13,7 @@
#include <__config>
#include <__functional/operations.h>
#include <__numeric/pstl_transform_reduce.h>
+#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -38,7 +39,7 @@ __equal(_ExecutionPolicy&& __policy,
_ForwardIterator2&& __first2,
_Pred&& __pred) noexcept {
return std::__pstl_frontend_dispatch(
- _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal),
+ _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
[&__policy](
_ForwardIterator1 __g_first1, _ForwardIterator1 __g_last1, _ForwardIterator2 __g_first2, _Pred __g_pred) {
return std::__transform_reduce(
@@ -97,7 +98,7 @@ __equal(_ExecutionPolicy&& __policy,
_ForwardIterator2&& __last2,
_Pred&& __pred) noexcept {
return std::__pstl_frontend_dispatch(
- _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal),
+ _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
[&__policy](_ForwardIterator1 __g_first1,
_ForwardIterator1 __g_last1,
_ForwardIterator2 __g_first2,
@@ -110,7 +111,7 @@ __equal(_ExecutionPolicy&& __policy,
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 captue warning
+ (void)__policy; // Avoid unused lambda capture warning
return std::equal(
std::move(__g_first1),
std::move(__g_last1),
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
index 4788e564e953980..9583b44b17321a9 100644
--- 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
@@ -37,62 +37,118 @@
#include <algorithm>
#include <cassert>
+#include <iterator>
#include "test_execution_policies.h"
#include "test_iterators.h"
#include "test_macros.h"
-template <class Iter1, class Iter2>
+template <class It1, class It2>
struct Test {
template <class Policy>
void operator()(Policy&& policy) {
{ // 3 iter overloads
- int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
- int b[] = {1, 2, 3, 5, 6, 7, 8, 9};
-
- // simple test
- assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a))));
-
- // check that false is returned on different ranges
- assert(!std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b))));
+ // 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 different 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
- assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b)), [](int lhs, int rhs) {
- return lhs == rhs || lhs + 1 == rhs || rhs + 1 == lhs;
- }));
+ {
+ 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
- int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
- int b[] = {1, 2, 3, 5, 6, 7, 8, 9};
-
- // simple test
- assert(std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a)), Iter2(std::end(a))));
-
- // check that false is returned on different ranges
- assert(!std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(b)), Iter2(std::end(b))));
-
- // check that false is returned on different sized ranges
- assert(
- !std::equal(policy, Iter1(std::begin(a)), Iter1(std::end(a)), Iter2(std::begin(a)), Iter2(std::end(a) - 1)));
+ // 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
- assert(std::equal(
- policy,
- Iter1(std::begin(a)),
- Iter1(std::end(a)),
- Iter2(std::begin(b)),
- Iter2(std::end(b)),
- [](int lhs, int rhs) { return lhs == rhs || lhs + 1 == rhs || rhs + 1 == lhs; }));
+ {
+ 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 Iter1 = typename decltype(v)::type;
+ using It1 = typename decltype(v)::type;
types::for_each(
types::forward_iterator_list<int*>{},
- TestIteratorWithPolicies<types::partial_instantiation<Test, Iter1>::template apply>{});
+ 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
index 4d93850ca96f1f3..e3486b50742af14 100644
--- 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
@@ -19,12 +19,6 @@
#include "test_execution_policies.h"
#include "test_iterators.h"
-struct ThrowOnCompare {};
-
-#ifndef TEST_HAS_NO_EXCEPTIONS
-bool operator==(ThrowOnCompare, ThrowOnCompare) { throw int{}; }
-#endif
-
int main(int, char**) {
test_execution_policies([](auto&& policy) {
EXPECT_STD_TERMINATE([&] {
@@ -38,7 +32,6 @@ int main(int, char**) {
} catch (const util::iterator_error&) {
assert(false);
}
- std::terminate(); // make the test pass in case the algorithm didn't move the iterator
});
EXPECT_STD_TERMINATE([&] {
try {
@@ -53,7 +46,6 @@ int main(int, char**) {
} catch (const util::iterator_error&) {
assert(false);
}
- std::terminate(); // make the test pass in case the algorithm didn't move the iterator
});
});
}
>From 7d281e9bd193aa0e9a2a7a1d82ae097ef5ae40a5 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 23 Nov 2023 17:47:58 -0500
Subject: [PATCH 3/6] Fix modules build
---
libcxx/include/__algorithm/pstl_equal.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__algorithm/pstl_equal.h b/libcxx/include/__algorithm/pstl_equal.h
index 3403ee60a344654..8ea9b2c3a8a53b8 100644
--- a/libcxx/include/__algorithm/pstl_equal.h
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -12,6 +12,7 @@
#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>
>From 5dac60cf51167473b816a23f63933294942770c2 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 23 Nov 2023 18:32:37 -0500
Subject: [PATCH 4/6] Add another missing include
---
libcxx/include/__algorithm/pstl_equal.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__algorithm/pstl_equal.h b/libcxx/include/__algorithm/pstl_equal.h
index 8ea9b2c3a8a53b8..b343d2675980c9c 100644
--- a/libcxx/include/__algorithm/pstl_equal.h
+++ b/libcxx/include/__algorithm/pstl_equal.h
@@ -9,6 +9,7 @@
#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>
>From d5cb5b28ac92cfdaf847ad65bda963e4f0b55fa2 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 27 Nov 2023 15:57:46 -0500
Subject: [PATCH 5/6] Fix typo in UNSUPPORTED annotation
---
.../algorithms/alg.nonmodifying/alg.equal/pstl.equal.pass.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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
index 9583b44b17321a9..e2f4dafe1a05d80 100644
--- 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
@@ -8,7 +8,7 @@
// UNSUPPORTED: c++03, c++11, c++14
-// UNSUPPORETD: libcpp-has-no-incomplete-pstl
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
// <algorithm>
>From c7e3b6f0b9d84e589e810859222179e0d82a04c4 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 28 Nov 2023 09:49:32 -0500
Subject: [PATCH 6/6] Require unix headers in exception test
---
.../alg.nonmodifying/alg.equal/pstl.exception_handling.pass.cpp | 1 +
1 file changed, 1 insertion(+)
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
index e3486b50742af14..4c2f9946165e7a9 100644
--- 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
@@ -8,6 +8,7 @@
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: no-exceptions
+// REQUIRES: has-unix-headers
// UNSUPPORTED: libcpp-has-no-incomplete-pstl
More information about the libcxx-commits
mailing list