[libcxx-commits] [libcxx] 3a7876f - [libc++][PSTL] Implement std::is_partitioned
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jun 15 11:23:13 PDT 2023
Author: Nikolas Klauser
Date: 2023-06-15T11:23:07-07:00
New Revision: 3a7876f6e2b01550c2178a7d295842b637d183c7
URL: https://github.com/llvm/llvm-project/commit/3a7876f6e2b01550c2178a7d295842b637d183c7
DIFF: https://github.com/llvm/llvm-project/commit/3a7876f6e2b01550c2178a7d295842b637d183c7.diff
LOG: [libc++][PSTL] Implement std::is_partitioned
Reviewed By: #libc, ldionne
Spies: ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D152853
Added:
libcxx/include/__algorithm/pstl_is_partitioned.h
libcxx/test/libcxx/algorithms/alg.sorting/pstl.is_partitioned.pass.cpp
libcxx/test/std/algorithms/alg.sorting/alg.partitions/pstl.is_partitioned.pass.cpp
Modified:
libcxx/docs/Status/PSTLPaper.csv
libcxx/include/CMakeLists.txt
libcxx/include/__algorithm/pstl_backend.h
libcxx/include/algorithm
libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/Status/PSTLPaper.csv b/libcxx/docs/Status/PSTLPaper.csv
index e70c44ca82f8c..d2a51a33b75fd 100644
--- a/libcxx/docs/Status/PSTLPaper.csv
+++ b/libcxx/docs/Status/PSTLPaper.csv
@@ -27,7 +27,7 @@ Section,Description,Assignee,Complete
| `[alg.merge] <https://wg21.link/alg.merge>`_,std::inplace_merge,Nikolas Klauser,|Not Started|
| `[alg.heap.operations] <https://wg21.link/alg.heap.operations>`_,std::is_heap,Nikolas Klauser,|Not Started|
| `[alg.heap.operations] <https://wg21.link/alg.heap.operations>`_,std::is_heap_until,Nikolas Klauser,|Not Started|
-| `[alg.partitions] <https://wg21.link/alg.partitions>`_,std::is_partitioned,Nikolas Klauser,|Not Started|
+| `[alg.partitions] <https://wg21.link/alg.partitions>`_,std::is_partitioned,Nikolas Klauser,|Complete|
| `[alg.sort] <https://wg21.link/alg.sort>`_,std::is_sorted,Nikolas Klauser,|Not Started|
| `[alg.sort] <https://wg21.link/alg.sort>`_,std::is_sorted_until,Nikolas Klauser,|Not Started|
| `[alg.lex.comparison] <https://wg21.link/alg.lex.comparison>`_,std::lexicographical_compare,Nikolas Klauser,|Not Started|
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 7088a3ecec603..a2cc9e2cac3b3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -91,6 +91,7 @@ set(files
__algorithm/pstl_for_each.h
__algorithm/pstl_frontend_dispatch.h
__algorithm/pstl_generate.h
+ __algorithm/pstl_is_partitioned.h
__algorithm/pstl_merge.h
__algorithm/pstl_replace.h
__algorithm/pstl_stable_sort.h
diff --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h
index 55003a8c93523..368235931fcb8 100644
--- a/libcxx/include/__algorithm/pstl_backend.h
+++ b/libcxx/include/__algorithm/pstl_backend.h
@@ -101,6 +101,9 @@ implemented, all the algorithms will eventually forward to the basis algorithms
template <class _ExecutionPolicy, class _Iterator, class _Generator>
void __pstl_generate(_Backend, _Iterator __first, _Iterator __last, _Generator __gen);
+ template <class _ExecutionPolicy, class _Iterator, class _Predicate>
+ void __pstl_is_partitioned(_Backend, _Iterator __first, _Iterator __last, _Predicate __pred);
+
template <class _ExecutionPolicy, class _Iterator, class _Size, class _Generator>
void __pstl_generator_n(_Backend, _Iterator __first, _Size __n, _Generator __gen);
diff --git a/libcxx/include/__algorithm/pstl_is_partitioned.h b/libcxx/include/__algorithm/pstl_is_partitioned.h
new file mode 100644
index 0000000000000..70f0df162bd47
--- /dev/null
+++ b/libcxx/include/__algorithm/pstl_is_partitioned.h
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_IS_PARITTIONED
+#define _LIBCPP___ALGORITHM_PSTL_IS_PARITTIONED
+
+#include <__algorithm/pstl_any_all_none_of.h>
+#include <__algorithm/pstl_backend.h>
+#include <__algorithm/pstl_find.h>
+#include <__algorithm/pstl_frontend_dispatch.h>
+#include <__config>
+#include <__type_traits/is_execution_policy.h>
+#include <__type_traits/remove_cvref.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_is_partitioned();
+
+template <class _ExecutionPolicy,
+ class _ForwardIterator,
+ class _Predicate,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI bool
+is_partitioned(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
+ return std::__pstl_frontend_dispatch(
+ _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_is_partitioned),
+ [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) {
+ __g_first = std::find_if_not(__policy, __g_first, __g_last, __g_pred);
+ if (__g_first == __g_last)
+ return true;
+ ++__g_first;
+ return std::none_of(__policy, __g_first, __g_last, __g_pred);
+ },
+ std::move(__first),
+ std::move(__last),
+ std::move(__pred));
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
+
+#endif // _LIBCPP___ALGORITHM_PSTL_IS_PARITTIONED
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index fefa98a67384c..29e7871b6fc05 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -1816,6 +1816,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/pstl_find.h>
#include <__algorithm/pstl_for_each.h>
#include <__algorithm/pstl_generate.h>
+#include <__algorithm/pstl_is_partitioned.h>
#include <__algorithm/pstl_merge.h>
#include <__algorithm/pstl_replace.h>
#include <__algorithm/pstl_stable_sort.h>
diff --git a/libcxx/test/libcxx/algorithms/alg.sorting/pstl.is_partitioned.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/pstl.is_partitioned.pass.cpp
new file mode 100644
index 0000000000000..8560e7c252a75
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/alg.sorting/pstl.is_partitioned.pass.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// Make sure that the predicate is called exactly N times in is_partitioned
+
+#include <algorithm>
+#include <cassert>
+#include <execution>
+#include <iterator>
+
+int main(int, char**) {
+ int call_count = 0;
+ int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ assert(std::is_partitioned(std::execution::seq, std::begin(a), std::end(a), [&](int i) {
+ ++call_count;
+ return i < 5;
+ }));
+ assert(call_count == std::size(a));
+}
diff --git a/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp b/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp
index 914159dc214ce..4abd0eda43949 100644
--- a/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp
@@ -146,6 +146,15 @@ void __pstl_fill_n(TestBackend, ForwardIterator, Size, Func) {
pstl_fill_n_called = true;
}
+bool pstl_is_partitioned_called = false;
+
+template <class, class ForwardIterator, class Func>
+bool __pstl_is_partitioned(TestBackend, ForwardIterator, ForwardIterator, Func) {
+ assert(!pstl_is_partitioned_called);
+ pstl_is_partitioned_called = true;
+ return {};
+}
+
bool pstl_replace_called = false;
template <class, class ForwardIterator, class T>
@@ -287,6 +296,8 @@ int main(int, char**) {
assert(std::pstl_generate_called);
(void)std::generate_n(TestPolicy{}, std::begin(a), std::size(a), pred);
assert(std::pstl_generate_n_called);
+ (void)std::is_partitioned(TestPolicy{}, std::begin(a), std::end(a), pred);
+ assert(std::pstl_generate_n_called);
(void)std::replace(TestPolicy{}, std::begin(a), std::end(a), 0, 0);
assert(std::pstl_replace_called);
(void)std::replace_if(TestPolicy{}, std::begin(a), std::end(a), pred, 0);
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.partitions/pstl.is_partitioned.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/pstl.is_partitioned.pass.cpp
new file mode 100644
index 0000000000000..a80e2f6ddc637
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/pstl.is_partitioned.pass.cpp
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// template<class ExecutionPolicy, class ForwardIterator, class Predicate>
+// bool is_partitioned(ExecutionPolicy&& exec,
+// ForwardIterator first, ForwardIterator last, Predicate pred);
+
+#include <algorithm>
+#include <cassert>
+#include <vector>
+
+#include "test_iterators.h"
+#include "test_execution_policies.h"
+
+template <class Iter>
+struct Test {
+ template <class Policy>
+ void operator()(Policy&& policy) {
+ { // simple test
+ int a[] = {1, 2, 3, 4, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
+ }
+ { // check that the range is partitioned if the predicate returns true for all elements
+ int a[] = {1, 2, 3, 4, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int) { return true; }));
+ }
+ { // check that the range is partitioned if the predicate returns false for all elements
+ int a[] = {1, 2, 3, 4, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int) { return false; }));
+ }
+ { // check that false is returned if the range is not partitioned
+ int a[] = {1, 2, 3, 2, 5};
+ assert(!std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
+ }
+ { // check that an empty range is partitioned
+ int a[] = {1, 2, 3, 2, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::begin(a)), [](int i) { return i < 3; }));
+ }
+ { // check that a single element is partitioned
+ int a[] = {1};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
+ }
+ { // check that a range is partitioned when the partition point is the first element
+ int a[] = {1, 2, 2, 4, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 2; }));
+ }
+ { // check that a range is partitioned when the partition point is the last element
+ int a[] = {1, 2, 2, 4, 5};
+ assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 2; }));
+ }
+ { // check that a large range works
+ std::vector<int> vec(150, 4);
+ vec[0] = 2;
+ vec[1] = 1;
+ assert(std::is_partitioned(policy, Iter(std::data(vec)), Iter(std::data(vec) + std::size(vec)), [](int i) {
+ return i < 3;
+ }));
+ }
+ }
+};
+
+int main(int, char**) {
+ types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
+
+ return 0;
+}
More information about the libcxx-commits
mailing list