[libcxx-commits] [libcxx] 672843a - [libc++][pstl] Implementation of parallel std::minmax_element() based on parallel reduce (#221572)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Sep 10 01:03:39 PDT 2026
Author: Michael G. Kazakov
Date: 2026-09-10T09:03:34+01:00
New Revision: 672843a3f2ee2aaaed0463ec9064b6ccea2a5d4d
URL: https://github.com/llvm/llvm-project/commit/672843a3f2ee2aaaed0463ec9064b6ccea2a5d4d
DIFF: https://github.com/llvm/llvm-project/commit/672843a3f2ee2aaaed0463ec9064b6ccea2a5d4d.diff
LOG: [libc++][pstl] Implementation of parallel std::minmax_element() based on parallel reduce (#221572)
This PR adds an implementation of parallel `std::minmax_element()` based
on parallel reduce.
The implementation formulates `minmax_element` as a reduction of
iterators over the input range.
The iterators are first transformed into a min-max iterator pair.
The reduction is provided in two forms: 2-element reduction that makes
an iterator pair pointing to a smaller and a greater value, and a
range-based reduction with init value.
The latter delegates the heavy-lifting to the serial implementation of
`std::minmax_element()`.
Part of #99938.
Added:
libcxx/include/__pstl/cpu_algos/minmax_element.h
libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp
Modified:
libcxx/include/CMakeLists.txt
libcxx/include/__algorithm/pstl.h
libcxx/include/__pstl/backend_fwd.h
libcxx/include/__pstl/backends/default.h
libcxx/include/__pstl/backends/libdispatch.h
libcxx/include/__pstl/backends/serial.h
libcxx/include/__pstl/backends/std_thread.h
libcxx/include/module.modulemap.in
libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d8fbc3462b02b..113d4fde7ca59 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -703,6 +703,7 @@ set(files
__pstl/cpu_algos/is_heap_until.h
__pstl/cpu_algos/merge.h
__pstl/cpu_algos/min_element.h
+ __pstl/cpu_algos/minmax_element.h
__pstl/cpu_algos/mismatch.h
__pstl/cpu_algos/reverse.h
__pstl/cpu_algos/search.h
diff --git a/libcxx/include/__algorithm/pstl.h b/libcxx/include/__algorithm/pstl.h
index 9b4de5de5d36f..eefc90da27409 100644
--- a/libcxx/include/__algorithm/pstl.h
+++ b/libcxx/include/__algorithm/pstl.h
@@ -189,6 +189,31 @@ min_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardItera
std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), std::move(__comp));
}
+template <class _ExecutionPolicy,
+ class _ForwardIterator,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>
+minmax_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
+ _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "minmax_element requires ForwardIterators");
+ using _Implementation = __pstl::__dispatch<__pstl::__minmax_element, __pstl::__current_configuration, _RawPolicy>;
+ return __pstl::__handle_exception<_Implementation>(
+ std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), less<>{});
+}
+
+template <class _ExecutionPolicy,
+ class _ForwardIterator,
+ class _Compare,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>
+minmax_element(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
+ _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "minmax_element requires ForwardIterators");
+ using _Implementation = __pstl::__dispatch<__pstl::__minmax_element, __pstl::__current_configuration, _RawPolicy>;
+ return __pstl::__handle_exception<_Implementation>(
+ std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), std::move(__comp));
+}
+
template <class _ExecutionPolicy,
class _ForwardIterator1,
class _ForwardIterator2,
diff --git a/libcxx/include/__pstl/backend_fwd.h b/libcxx/include/__pstl/backend_fwd.h
index bd8ea25f4f70b..40a335ae1f2ad 100644
--- a/libcxx/include/__pstl/backend_fwd.h
+++ b/libcxx/include/__pstl/backend_fwd.h
@@ -213,6 +213,12 @@ struct __min_element;
// optional<_ForwardIterator>
// operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept;
+template <class _Backend, class _ExecutionPolicy>
+struct __minmax_element;
+// template <class _Policy, class _ForwardIterator, class _Compare>
+// optional<pair<_ForwardIterator, _ForwardIterator>>
+// operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept;
+
template <class _Backend, class _ExecutionPolicy>
struct __search_n;
// template <class _Policy, class _ForwardIterator, class _Size, class _T, class _Predicate>
diff --git a/libcxx/include/__pstl/backends/default.h b/libcxx/include/__pstl/backends/default.h
index a26d29b5d81db..b5d7045bbb9b8 100644
--- a/libcxx/include/__pstl/backends/default.h
+++ b/libcxx/include/__pstl/backends/default.h
@@ -82,6 +82,10 @@ namespace __pstl {
// ---------------
// - max_element
//
+// minmax_element family
+// -------------------
+// No other algorithms based on minmax_element
+//
// mismatch family
// ---------------
// - adjacent_find
diff --git a/libcxx/include/__pstl/backends/libdispatch.h b/libcxx/include/__pstl/backends/libdispatch.h
index 5d3fef8a4a176..dbd9fa34e1f87 100644
--- a/libcxx/include/__pstl/backends/libdispatch.h
+++ b/libcxx/include/__pstl/backends/libdispatch.h
@@ -43,6 +43,7 @@
#include <__pstl/cpu_algos/is_heap_until.h>
#include <__pstl/cpu_algos/merge.h>
#include <__pstl/cpu_algos/min_element.h>
+#include <__pstl/cpu_algos/minmax_element.h>
#include <__pstl/cpu_algos/mismatch.h>
#include <__pstl/cpu_algos/reverse.h>
#include <__pstl/cpu_algos/search.h>
@@ -389,6 +390,10 @@ template <class _ExecutionPolicy>
struct __min_element<__libdispatch_backend_tag, _ExecutionPolicy>
: __cpu_parallel_min_element<__libdispatch_backend_tag, _ExecutionPolicy> {};
+template <class _ExecutionPolicy>
+struct __minmax_element<__libdispatch_backend_tag, _ExecutionPolicy>
+ : __cpu_parallel_minmax_element<__libdispatch_backend_tag, _ExecutionPolicy> {};
+
template <class _ExecutionPolicy>
struct __mismatch<__libdispatch_backend_tag, _ExecutionPolicy>
: __cpu_parallel_mismatch<__libdispatch_backend_tag, _ExecutionPolicy> {};
diff --git a/libcxx/include/__pstl/backends/serial.h b/libcxx/include/__pstl/backends/serial.h
index ae4a21315ea31..85c73b0f15db5 100644
--- a/libcxx/include/__pstl/backends/serial.h
+++ b/libcxx/include/__pstl/backends/serial.h
@@ -16,6 +16,7 @@
#include <__algorithm/is_heap_until.h>
#include <__algorithm/merge.h>
#include <__algorithm/min_element.h>
+#include <__algorithm/minmax_element.h>
#include <__algorithm/mismatch.h>
#include <__algorithm/reverse.h>
#include <__algorithm/search.h>
@@ -29,6 +30,7 @@
#include <__utility/empty.h>
#include <__utility/forward.h>
#include <__utility/move.h>
+#include <__utility/pair.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -145,6 +147,15 @@ struct __min_element<__serial_backend_tag, _ExecutionPolicy> {
}
};
+template <class _ExecutionPolicy>
+struct __minmax_element<__serial_backend_tag, _ExecutionPolicy> {
+ template <class _Policy, class _ForwardIterator, class _Compare>
+ _LIBCPP_HIDE_FROM_ABI optional<pair<_ForwardIterator, _ForwardIterator>>
+ operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept {
+ return std::minmax_element(std::move(__first), std::move(__last), std::move(__comp));
+ }
+};
+
template <class _ExecutionPolicy>
struct __reverse<__serial_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _BidirectionalIterator>
diff --git a/libcxx/include/__pstl/backends/std_thread.h b/libcxx/include/__pstl/backends/std_thread.h
index 25742c01eb298..bc7fc020867f8 100644
--- a/libcxx/include/__pstl/backends/std_thread.h
+++ b/libcxx/include/__pstl/backends/std_thread.h
@@ -22,6 +22,7 @@
#include <__pstl/cpu_algos/is_heap_until.h>
#include <__pstl/cpu_algos/merge.h>
#include <__pstl/cpu_algos/min_element.h>
+#include <__pstl/cpu_algos/minmax_element.h>
#include <__pstl/cpu_algos/mismatch.h>
#include <__pstl/cpu_algos/reverse.h>
#include <__pstl/cpu_algos/search.h>
@@ -120,6 +121,10 @@ template <class _ExecutionPolicy>
struct __min_element<__std_thread_backend_tag, _ExecutionPolicy>
: __cpu_parallel_min_element<__std_thread_backend_tag, _ExecutionPolicy> {};
+template <class _ExecutionPolicy>
+struct __minmax_element<__std_thread_backend_tag, _ExecutionPolicy>
+ : __cpu_parallel_minmax_element<__std_thread_backend_tag, _ExecutionPolicy> {};
+
template <class _ExecutionPolicy>
struct __mismatch<__std_thread_backend_tag, _ExecutionPolicy>
: __cpu_parallel_mismatch<__std_thread_backend_tag, _ExecutionPolicy> {};
diff --git a/libcxx/include/__pstl/cpu_algos/minmax_element.h b/libcxx/include/__pstl/cpu_algos/minmax_element.h
new file mode 100644
index 0000000000000..de737ed9f3317
--- /dev/null
+++ b/libcxx/include/__pstl/cpu_algos/minmax_element.h
@@ -0,0 +1,93 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
+#define _LIBCPP___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
+
+#include <__algorithm/minmax_element.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/iterator_traits.h>
+#include <__optional/optional.h>
+#include <__pstl/backend_fwd.h>
+#include <__pstl/cpu_algos/cpu_traits.h>
+#include <__type_traits/is_execution_policy.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 17
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __pstl {
+
+template <class _Backend, class _RawExecutionPolicy>
+struct __cpu_parallel_minmax_element {
+ template <class _Policy, class _ForwardIterator, class _Compare>
+ _LIBCPP_HIDE_FROM_ABI optional<pair<_ForwardIterator, _ForwardIterator>>
+ operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) const noexcept {
+ if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
+ __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
+ using _IterPair = pair<_ForwardIterator, _ForwardIterator>;
+
+ if (__first == __last) {
+ return _IterPair{__last, __last}; // Nothing to do
+ }
+
+ _IterPair __init = {__first, __first};
+ ++__first;
+ if (__first == __last) {
+ return __init; // The only element is both the minimum and the maximum
+ }
+
+ // A reduction that returns a pair of iterators pointing to the minimum and maximum elements.
+ // In a case of a tie the minimum iterators are biased left and the maximum iterators are biased right.
+ auto __iter_reduce = [&__comp](_IterPair __lhs, _IterPair __rhs) {
+ return _IterPair{__comp(*__rhs.first, *__lhs.first) ? __rhs.first : __lhs.first,
+ __comp(*__rhs.second, *__lhs.second) ? __lhs.second : __rhs.second};
+ };
+
+ // Perform a parallel reduction of iterators [first+1, last) with {first, first} as init.
+ return __cpu_traits<_Backend>::__transform_reduce(
+ std::move(__first),
+ std::move(__last),
+ [](auto __it) { return _IterPair{__it, __it}; }, // Transform an iterator into an iterator pair
+ std::move(__init), // Use the pair of first iterators as the init element
+ __iter_reduce, // Reduction of 2 minmax pairs
+ [&__iter_reduce, &__comp](auto __brick_first, auto __brick_last, auto __brick_init) {
+ // Reduction of an iterator range + init element: use the serial version to find the minmax among
+ // the iterators and then reduce it with the init element.
+ // Atm __transform_reduce can give empty bricks in edge cases, handle them explicitly until the contract is
+ // tightened.
+ return __brick_first == __brick_last
+ ? __brick_init
+ : __iter_reduce(__brick_init, std::minmax_element(__brick_first, __brick_last, __comp));
+ });
+ } else {
+ // Non-random access iterators cannot be processed in parallel, fall back to the sequential implementation.
+ return std::minmax_element(std::move(__first), std::move(__last), std::move(__comp));
+ }
+ }
+};
+
+} // namespace __pstl
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 17
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___PSTL_CPU_ALGOS_MINMAX_ELEMENT_H
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 45f98a7b6b9e5..d22a9ce36d02c 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -2449,6 +2449,9 @@ module std {
module min_element {
header "__pstl/cpu_algos/min_element.h"
}
+ module minmax_element {
+ header "__pstl/cpu_algos/minmax_element.h"
+ }
module mismatch {
header "__pstl/cpu_algos/mismatch.h"
}
diff --git a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
index 5bc435dfb9103..f12d5bdcb3121 100644
--- a/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp
@@ -196,6 +196,9 @@ void f(non_forward_iterator non_fwd,
(void)std::max_element(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: max_element}}
(void)std::min_element(pol, non_fwd, non_fwd); // expected-error@*:* {{static assertion failed: min_element}}
(void)std::min_element(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: min_element}}
+ (void)std::minmax_element(pol, non_fwd, non_fwd); // expected-error@*:* {{static assertion failed: minmax_element}}
+ (void)std::minmax_element(
+ pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: minmax_element}}
}
{
diff --git a/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp b/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
index 55f2a1f93376e..f36f12a135fba 100644
--- a/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/pstl.nodiscard.verify.cpp
@@ -88,6 +88,10 @@ void test() {
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::min_element(std::execution::par, std::begin(a), std::end(a), pred2);
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::minmax_element(std::execution::par, std::begin(a), std::end(a));
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::minmax_element(std::execution::par, std::begin(a), std::end(a), pred2);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::mismatch(std::execution::par, std::begin(a), std::end(a), std::begin(b));
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::mismatch(std::execution::par, std::begin(a), std::end(a), std::begin(b), std::end(b));
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
new file mode 100644
index 0000000000000..d4092a90a2919
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element.pass.cpp
@@ -0,0 +1,209 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator>
+// pair<ForwardIterator, ForwardIterator>
+// minmax_element(ExecutionPolicy&& exec,
+// ForwardIterator first, ForwardIterator last);
+
+#include <cstddef>
+#include <algorithm>
+#include <cassert>
+#include <iterator>
+#include <numeric>
+#include <random>
+#include <vector>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(minmax_element);
+
+static_assert(sfinae_test_minmax_element<int, int*, int*>);
+static_assert(!sfinae_test_minmax_element<std::execution::parallel_policy, int*, int*>);
+
+template <class Iter>
+struct Test {
+ template <class ExecutionPolicy>
+ void operator()(ExecutionPolicy&& policy) {
+ using IterPair = std::pair<Iter, Iter>;
+ { // Check the return type
+ int a[] = {0};
+ auto res = std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)));
+ static_assert(std::is_same_v<decltype(res), IterPair>);
+ }
+ { // Empty
+ int a[] = {0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::begin(a))) ==
+ IterPair(std::begin(a), std::begin(a)));
+ }
+ { // Single
+ int a[] = {0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a)));
+ }
+ { // Two elements, first min, second max
+ int a[] = {0, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 1));
+ }
+ { // Two elements, second min, first max
+ int a[] = {1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a)));
+ }
+ { // Three elements, first min, third max
+ int a[] = {0, 1, 2};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 2));
+ }
+ { // Three elements, middle min, third max
+ int a[] = {1, 0, 2};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a) + 2));
+ }
+ { // Three elements, middle min and equal to last element, first max
+ int a[] = {1, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a)));
+ }
+ { // Three elements, third min, second max
+ int a[] = {1, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 2, std::begin(a) + 1));
+ }
+ { // Three elements, last min, first max
+ int a[] = {2, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 2, std::begin(a)));
+ }
+ { // Four elements, first min, fourth max
+ int a[] = {0, 1, 2, 3};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Four elements, all equal
+ int a[] = {1, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Four elements, middle min, first max
+ int a[] = {3, 0, 2, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a)));
+ }
+ { // Four elements, middle min and equal to consequent elements, first max
+ int a[] = {3, 0, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a)));
+ }
+ { // Four elements, middle min and equal to consequent elements, second max
+ int a[] = {3, 3, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 2, std::begin(a) + 1));
+ }
+ { // Four elements, last min, first max
+ int a[] = {3, 2, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 3, std::begin(a)));
+ }
+ { // Five elements, all equal
+ int a[] = {1, 1, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, ascending
+ int a[] = {1, 2, 3, 4, 5};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, descending
+ int a[] = {5, 4, 3, 2, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 4, std::begin(a)));
+ }
+ { // Five elements, 2 flat regions, descending
+ int a[] = {1, 1, 1, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 3, std::begin(a) + 2));
+ }
+ { // Five elements, 2 flat regions, ascending
+ int a[] = {0, 0, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, alternating 1/0 pattern
+ int a[] = {1, 0, 1, 0, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 1, std::begin(a) + 4));
+ }
+ { // Check for minimum and maximum among iotaed and shuffled elements
+ std::mt19937 randomness;
+ auto verify_min = [&](Iter first, Iter last) {
+ IterPair i = std::minmax_element(policy, first, last);
+ if (first != last) {
+ for (Iter j = first; j != last; ++j) {
+ assert(!(*j < *i.first));
+ assert(!(*i.second < *j));
+ }
+ } else {
+ assert(i == IterPair(last, last));
+ }
+ };
+ auto test_n_elem = [&](int N) {
+ std::vector<int> a(N);
+ std::iota(a.begin(), a.end(), 0);
+ std::shuffle(a.begin(), a.end(), randomness);
+ verify_min(Iter(a.data()), Iter(a.data() + N));
+ };
+ test_n_elem(3);
+ test_n_elem(4);
+ test_n_elem(5);
+ test_n_elem(10);
+ test_n_elem(100);
+ test_n_elem(1000);
+ }
+ { // Check that the first lowest is returned as the minimum and the last greatest is returned as the maximum
+ int a[1073];
+ // Fill with 2 2 2 2 .... 0 1 0 1 ...
+ for (std::size_t i = 0; i < std::size(a); ++i)
+ a[i] = (i < 473) ? 2 : (i % 2 ? 0 : 1);
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + 473, std::begin(a) + 472));
+ }
+ { // Check that the minimum/maximum at sampled locations is found
+ int a[1073];
+ std::fill(std::begin(a), std::end(a), 1);
+ runway_sample(std::size(a) - 1, [&](size_t i) {
+ if (i == 0)
+ return;
+ a[i] = 0;
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a) + i, std::begin(a) + std::size(a) - 1));
+ a[i] = 2;
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a))) ==
+ IterPair(std::begin(a), std::begin(a) + i));
+ a[i] = 1;
+ });
+ }
+ }
+};
+
+int main(int, char**) {
+ types::for_each(types::forward_iterator_list<const int*>{}, TestIteratorWithPolicies<Test>{});
+ return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp
new file mode 100644
index 0000000000000..b84906c0a9b5f
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/pstl.minmax_element_comp.pass.cpp
@@ -0,0 +1,212 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+// UNSUPPORTED: libcpp-has-no-incomplete-pstl
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator, class Compare>
+// pair<ForwardIterator, ForwardIterator>
+// minmax_element(ExecutionPolicy&& exec,
+// ForwardIterator first, ForwardIterator last,
+// Compare comp);
+
+#include <cstddef>
+#include <algorithm>
+#include <cassert>
+#include <functional>
+#include <iterator>
+#include <numeric>
+#include <random>
+#include <vector>
+
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "type_algorithms.h"
+#include "runway_sample.h"
+
+EXECUTION_POLICY_SFINAE_TEST(minmax_element);
+
+static_assert(sfinae_test_minmax_element<int, int*, int*, bool (*)(int, int)>);
+static_assert(!sfinae_test_minmax_element<std::execution::parallel_policy, int*, int*, bool (*)(int, int)>);
+
+template <class Iter>
+struct Test {
+ template <class ExecutionPolicy>
+ void operator()(ExecutionPolicy&& policy) {
+ using IterPair = std::pair<Iter, Iter>;
+ std::greater<int> comp;
+ { // Check the return type
+ int a[] = {0};
+ auto res = std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp);
+ static_assert(std::is_same_v<decltype(res), IterPair>);
+ }
+ { // Empty
+ int a[] = {0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::begin(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a)));
+ }
+ { // Single
+ int a[] = {0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a)));
+ }
+ { // Two elements, second min, first max
+ int a[] = {0, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 1, std::begin(a)));
+ }
+ { // Two elements, first min, second max
+ int a[] = {1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 1));
+ }
+ { // Three elements, third min, first max
+ int a[] = {0, 1, 2};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 2, std::begin(a)));
+ }
+ { // Three elements, third min, middle max
+ int a[] = {1, 0, 2};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 2, std::begin(a) + 1));
+ }
+ { // Three elements, first min, third max
+ int a[] = {1, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 2));
+ }
+ { // Three elements, first min, third max
+ int a[] = {1, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 2));
+ }
+ { // Three elements, first min, third max
+ int a[] = {2, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 2));
+ }
+ { // Four elements, fourth min, first max
+ int a[] = {0, 1, 2, 3};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 3, std::begin(a)));
+ }
+ { // Four elements, all equal
+ int a[] = {1, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Four elements, first min, middle max
+ int a[] = {3, 0, 2, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 1));
+ }
+ { // Four elements, first min, fourth max
+ int a[] = {3, 0, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Four elements, first min, fourth max
+ int a[] = {3, 3, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Four elements, first min, fourth max
+ int a[] = {3, 2, 1, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Five elements, all equal
+ int a[] = {1, 1, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, ascending
+ int a[] = {1, 2, 3, 4, 5};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 4, std::begin(a)));
+ }
+ { // Five elements, descending
+ int a[] = {5, 4, 3, 2, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, 2 flat regions, descending
+ int a[] = {1, 1, 1, 0, 0};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 4));
+ }
+ { // Five elements, 2 flat regions, ascending
+ int a[] = {0, 0, 1, 1, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + 2, std::begin(a) + 1));
+ }
+ { // Five elements, alternating 1/0 pattern
+ int a[] = {1, 0, 1, 0, 1};
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 3));
+ }
+ { // Check for minimum and maximum among iotaed and shuffled elements
+ std::mt19937 randomness;
+ auto verify_min = [&](Iter first, Iter last) {
+ IterPair i = std::minmax_element(policy, first, last, comp);
+ if (first != last) {
+ for (Iter j = first; j != last; ++j) {
+ assert(!(*i.first < *j));
+ assert(!(*j < *i.second));
+ }
+ } else {
+ assert(i == IterPair(last, last));
+ }
+ };
+ auto test_n_elem = [&](int N) {
+ std::vector<int> a(N);
+ std::iota(a.begin(), a.end(), 0);
+ std::shuffle(a.begin(), a.end(), randomness);
+ verify_min(Iter(a.data()), Iter(a.data() + N));
+ };
+ test_n_elem(3);
+ test_n_elem(4);
+ test_n_elem(5);
+ test_n_elem(10);
+ test_n_elem(100);
+ test_n_elem(1000);
+ }
+ { // Check that the first greatest is returned as the minimum and the last smallest is returned as the maximum
+ int a[1073];
+ // Fill with 2 2 2 2 .... 0 1 0 1 ...
+ for (std::size_t i = 0; i < std::size(a); ++i)
+ a[i] = (i < 473) ? 2 : (i % 2 ? 0 : 1);
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + 1071));
+ }
+ { // Check that the minimum/maximum at sampled locations is found
+ int a[1073];
+ std::fill(std::begin(a), std::end(a), 1);
+ runway_sample(std::size(a) - 1, [&](size_t i) {
+ if (i == 0)
+ return;
+ a[i] = 0;
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a), std::begin(a) + i));
+ a[i] = 2;
+ assert(std::minmax_element(policy, Iter(std::begin(a)), Iter(std::end(a)), comp) ==
+ IterPair(std::begin(a) + i, std::begin(a) + std::size(a) - 1));
+ a[i] = 1;
+ });
+ }
+ }
+};
+
+int main(int, char**) {
+ types::for_each(types::forward_iterator_list<const int*>{}, TestIteratorWithPolicies<Test>{});
+ return 0;
+}
diff --git a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
index 0c6da3b9a9a09..b85cc984d969d 100644
--- a/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
+++ b/libcxx/test/std/algorithms/pstl.exception_handling.pass.cpp
@@ -306,6 +306,14 @@ int main(int, char**) {
assert_non_throwing([=, &policy] {
(void)std::min_element(policy, std::move(first1), std::move(last1), compare);
});
+
+ // minmax_element(first, last)
+ assert_non_throwing([=, &policy] { (void)std::minmax_element(policy, std::move(first1), std::move(last1)); });
+
+ // minmax_element(first, last, comp)
+ assert_non_throwing([=, &policy] {
+ (void)std::minmax_element(policy, std::move(first1), std::move(last1), compare);
+ });
}
{
More information about the libcxx-commits
mailing list