[libc++][PSTL] Implement std::is_partitioned

Reviewed By: #libc, ldionne

Spies: ldionne, libcxx-commits

Differential Revision: https://reviews.llvm.org/D152853
This commit is contained in:
Nikolas Klauser 2023-06-13 10:50:38 -07:00
parent 4b111dd798
commit 3a7876f6e2
8 changed files with 176 additions and 1 deletions

View File

@ -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|

1 Section Description Assignee Complete
27 | `[alg.merge] <https://wg21.link/alg.merge>`_ std::inplace_merge Nikolas Klauser |Not Started|
28 | `[alg.heap.operations] <https://wg21.link/alg.heap.operations>`_ std::is_heap Nikolas Klauser |Not Started|
29 | `[alg.heap.operations] <https://wg21.link/alg.heap.operations>`_ std::is_heap_until Nikolas Klauser |Not Started|
30 | `[alg.partitions] <https://wg21.link/alg.partitions>`_ std::is_partitioned Nikolas Klauser |Not Started| |Complete|
31 | `[alg.sort] <https://wg21.link/alg.sort>`_ std::is_sorted Nikolas Klauser |Not Started|
32 | `[alg.sort] <https://wg21.link/alg.sort>`_ std::is_sorted_until Nikolas Klauser |Not Started|
33 | `[alg.lex.comparison] <https://wg21.link/alg.lex.comparison>`_ std::lexicographical_compare Nikolas Klauser |Not Started|

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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>

View File

@ -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));
}

View File

@ -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);

View File

@ -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;
}