diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -90,6 +90,7 @@ __algorithm/pstl_find.h __algorithm/pstl_for_each.h __algorithm/pstl_frontend_dispatch.h + __algorithm/pstl_generate.h __algorithm/pstl_merge.h __algorithm/pstl_replace.h __algorithm/pstl_stable_sort.h diff --git a/libcxx/include/__algorithm/pstl_generate.h b/libcxx/include/__algorithm/pstl_generate.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_generate.h @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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_GENERATE_H +#define _LIBCPP___ALGORITHM_PSTL_GENERATE_H + +#include <__algorithm/pstl_backend.h> +#include <__algorithm/pstl_for_each.h> +#include <__algorithm/pstl_frontend_dispatch.h> +#include <__config> +#include <__iterator/cpp17_iterator_concepts.h> +#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 +void __pstl_generate(); + +template , + enable_if_t, int> = 0> +_LIBCPP_HIDE_FROM_ABI void +generate(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Generator __gen) { + _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator); + std::__pstl_frontend_dispatch( + _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate), + [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Generator __g_gen) { + std::for_each( + __policy, std::move(__g_first), std::move(__g_last), [&](__iter_reference<_ForwardIterator> __element) { + __element = __g_gen(); + }); + }, + std::move(__first), + std::move(__last), + std::move(__gen)); +} + +template +void __pstl_generate_n(); + +template , + enable_if_t, int> = 0> +_LIBCPP_HIDE_FROM_ABI void +generate_n(_ExecutionPolicy&& __policy, _ForwardIterator __first, _Size __n, _Generator __gen) { + _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator); + std::__pstl_frontend_dispatch( + _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate_n), + [&__policy](_ForwardIterator __g_first, _Size __g_n, _Generator __g_gen) { + std::for_each_n(__policy, std::move(__g_first), __g_n, [&](__iter_reference<_ForwardIterator> __element) { + __element = __g_gen(); + }); + }, + std::move(__first), + __n, + std::move(__gen)); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_GENERATE_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1806,6 +1806,7 @@ #include <__algorithm/pstl_fill.h> #include <__algorithm/pstl_find.h> #include <__algorithm/pstl_for_each.h> +#include <__algorithm/pstl_generate.h> #include <__algorithm/pstl_merge.h> #include <__algorithm/pstl_replace.h> #include <__algorithm/pstl_stable_sort.h> 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 --- 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 @@ -62,6 +62,22 @@ return 0; } +bool pstl_generate_called = false; + +template +void __pstl_generate(TestBackend, ForwardIterator, ForwardIterator, Gen) { + assert(!pstl_generate_called); + pstl_generate_called = true; +} + +bool pstl_generate_n_called = false; + +template +void __pstl_generate_n(TestBackend, Size, ForwardIterator, Gen) { + assert(!pstl_generate_n_called); + pstl_generate_n_called = true; +} + bool pstl_none_of_called = false; template @@ -267,6 +283,10 @@ assert(std::pstl_for_each_called); (void)std::for_each_n(TestPolicy{}, std::begin(a), std::size(a), pred); assert(std::pstl_for_each_n_called); + (void)std::generate(TestPolicy{}, std::begin(a), std::end(a), pred); + 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::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.modifying.operations/alg.generate/pstl.generate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/pstl.generate.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/pstl.generate.pass.cpp @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template +// void generate(ExecutionPolicy&& exec, +// ForwardIterator first, ForwardIterator last, +// Generator gen); + +#include +#include +#include + +#include "test_iterators.h" +#include "test_execution_policies.h" +#include "type_algorithms.h" + +template +struct Test { + template + void operator()(ExecutionPolicy&& policy) { + { // simple test + int a[10]; + std::generate(policy, Iter(std::begin(a)), Iter(std::end(a)), []() { return 1; }); + assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 1; })); + } + { // empty range works + int a[10] {3}; + std::generate(policy, Iter(std::begin(a)), Iter(std::begin(a)), []() { return 1; }); + assert(a[0] == 3); + } + { // single-element range works + int a[] {3}; + std::generate(policy, Iter(std::begin(a)), Iter(std::end(a)), []() { return 5; }); + assert(a[0] == 5); + } + { // large range works + std::vector vec(150, 4); + std::generate(policy, Iter(std::data(vec)), Iter(std::data(vec) + std::size(vec)), []() { return 5; }); + assert(std::all_of(std::begin(vec), std::end(vec), [](int i) { return i == 5; })); + } + } +}; + +int main(int, char**) { + types::for_each(types::forward_iterator_list{}, TestIteratorWithPolicies{}); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/pstl.generate_n.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/pstl.generate_n.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/pstl.generate_n.pass.cpp @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template +// ForwardIterator generate_n(ExecutionPolicy&& exec, +// ForwardIterator first, Size n, Generator gen); + +#include +#include +#include + +#include "test_iterators.h" +#include "test_execution_policies.h" +#include "type_algorithms.h" + +template +struct Test { + template + void operator()(ExecutionPolicy&& policy) { + { // simple test + int a[10]; + std::generate_n(policy, Iter(std::begin(a)), std::size(a), []() { return 1; }); + assert(std::all_of(std::begin(a), std::end(a), [](int i) { return i == 1; })); + } + { // empty range works + int a[10] {3}; + std::generate_n(policy, Iter(std::begin(a)), 0, []() { return 1; }); + assert(a[0] == 3); + } + { // single-element range works + int a[] {3}; + std::generate_n(policy, Iter(std::begin(a)), std::size(a), []() { return 5; }); + assert(a[0] == 5); + } + { // large range works + std::vector vec(150, 4); + std::generate_n(policy, Iter(std::data(vec)), std::size(vec), []() { return 5; }); + assert(std::all_of(std::begin(vec), std::end(vec), [](int i) { return i == 5; })); + } + } +}; + +int main(int, char**) { + types::for_each(types::forward_iterator_list{}, TestIteratorWithPolicies{}); + + return 0; +}