diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -67,6 +67,8 @@ __algorithm/prev_permutation.h __algorithm/push_heap.h __algorithm/ranges_min_element.h + __algorithm/ranges_minmax.h + __algorithm/ranges_minmax_element.h __algorithm/ranges_swap_ranges.h __algorithm/remove.h __algorithm/remove_copy.h diff --git a/libcxx/include/__algorithm/ranges_minmax.h b/libcxx/include/__algorithm/ranges_minmax.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_minmax.h @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// 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_RANGES_MINMAX_H +#define _LIBCPP___ALGORITHM_RANGES_MINMAX_H + +#include <__algorithm/ranges_minmax_element.h> +#include <__concepts/copyable.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __minmax { +struct __fn { + template > _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result + operator()(const _Tp& __a, const _Tp& __b, _Comp __comp = {}, _Proj __proj = {}) const { + if (std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b))) + return ranges::minmax_result {__a, __b}; + return ranges::minmax_result {__b, __a}; + } + + template > _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + ranges::minmax_result<_Tp> operator()(initializer_list<_Tp> __r, _Comp __comp = {}, _Proj __proj = {}) const { + auto __iters = __minmax_element::__fn::__go(ranges::begin(__r), ranges::end(__r), __comp, __proj); + return ranges::minmax_result<_Tp> { *__iters.min, *__iters.max }; + } + + template , _Proj>> _Comp = ranges::less> + requires indirectly_copyable_storable, range_value_t<_Rp>*> + _LIBCPP_HIDE_FROM_ABI constexpr + ranges::minmax_result> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const { + auto __iters = __minmax_element::__fn::__go(ranges::begin(__r), ranges::end(__r), __comp, __proj); + return ranges::minmax_result> { *__iters.min, *__iters.max }; + } +}; +} // namespace __minmax + +inline namespace __cpo { +inline constexpr auto minmax = __minmax::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H diff --git a/libcxx/include/__algorithm/ranges_minmax_element.h b/libcxx/include/__algorithm/ranges_minmax_element.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_minmax_element.h @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// 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_RANGES_MINMAX_ELEMENT_H +#define _LIBCPP___ALGORITHM_RANGES_MINMAX_ELEMENT_H + +#include <__algorithm/min_max_result.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +template +using minmax_result = min_max_result<_Tp>; + +namespace __minmax_element { +struct __fn { + template + static _LIBCPP_HIDE_FROM_ABI constexpr + minmax_result<_Ip> __go(_Ip __first, _Sp __last, _Comp __comp, _Proj __proj) { + auto __project = [&](_Ip& __p) -> decltype(auto) { return std::invoke(__proj, *__p); }; + + ranges::minmax_result<_Ip> __result {__first, __first}; + if (__first == __last || ++__first == __last) + return __result; + + if (std::invoke(__comp, __project(__first), __project(__result.min))) + __result.min = __first; + else + __result.max = __first; + + while (++__first != __last) { + _Ip __i = __first; + if (++__first == __last) { + if (std::invoke(__comp, __project(__i), __project(__result.min))) + __result.min = __i; + else if (!std::invoke(__comp, __project(__i), __project(__result.max))) + __result.max = __i; + return __result; + } + + if (std::invoke(__comp, __project(__first), __project(__i))) { + if (std::invoke(__comp, __project(__first), __project(__result.min))) + __result.min = __first; + if (!std::invoke(__comp, __project(__i), __project(__result.max))) + __result.max = __i; + } else { + if (std::invoke(__comp, __project(__i), __project(__result.min))) + __result.min = __i; + if (!std::invoke(__comp, __project(__first), __project(__result.max))) + __result.max = __first; + } + } + + return __result; + } + + template _Sp, class _Proj = identity, + indirect_strict_weak_order> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + ranges::minmax_result<_Ip> operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const { + return __go(__first, __last, __comp, __proj); + } + + template , _Proj>> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + ranges::minmax_result> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const { + return __go(ranges::begin(__r), ranges::end(__r), __comp, __proj); + } +}; +} // namespace __minmax_element + +inline namespace __cpo { +inline constexpr auto minmax_element = __minmax_element::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -763,6 +763,8 @@ #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> #include <__algorithm/ranges_min_element.h> +#include <__algorithm/ranges_minmax.h> +#include <__algorithm/ranges_minmax_element.h> #include <__algorithm/ranges_swap_ranges.h> #include <__algorithm/remove.h> #include <__algorithm/remove_copy.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -289,6 +289,8 @@ module prev_permutation { private header "__algorithm/prev_permutation.h" } module push_heap { private header "__algorithm/push_heap.h" } module ranges_min_element { private header "__algorithm/ranges_min_element.h" } + module ranges_minmax { private header "__algorithm/ranges_minmax.h" } + module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } module remove { private header "__algorithm/remove.h" } module remove_copy { private header "__algorithm/remove_copy.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax.module.verify.cpp @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// 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: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}} +#include <__algorithm/ranges_minmax.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax_element.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax_element.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_minmax_element.module.verify.cpp @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// 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: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}} +#include <__algorithm/ranges_minmax_element.h> diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax.pass.cpp @@ -0,0 +1,193 @@ +//===----------------------------------------------------------------------===// +// +// 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, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// XFAIL: gcc + +// template S, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr I ranges::minmax(I first, S last, Comp comp = {}, Proj proj = {}); +// +// template, Proj>> Comp = ranges::less> +// constexpr borrowed_iterator_t ranges::minmax(R&& r, Comp comp = {}, Proj proj = {}); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +concept HasMinMax = requires { std::ranges::minmax(std::declval()); }; + +struct NoLessThanOp {}; +struct NotTotallyOrdered { + int i; + bool operator<(const NotTotallyOrdered& o) const { return i < o.i; } +}; + +struct TotallyOrderedRVal { + int i; + bool operator<(const NotTotallyOrdered& o) const&& { return i < o.i; } +}; + +static_assert(HasMinMax); +static_assert(HasMinMax); +static_assert(!HasMinMax); +static_assert(!HasMinMax); + +static_assert(HasMinMax>); +static_assert(!HasMinMax>); +static_assert(!HasMinMax>); + +static_assert(std::is_same_v>); + +constexpr void test_2_arguments() { + const int one = 1; + const int two = 2; + { + auto result = std::ranges::minmax(one, two); + assert(result.min == 1); + assert(result.max == 2); + } + { + auto result = std::ranges::minmax(two, one); + assert(result.min == 1); + assert(result.max == 2); + } + { // test comparator + auto result = std::ranges::minmax(one, two, std::ranges::greater{}); + assert(result.min == 2); + assert(result.max == 1); + } + { // test projection + auto result = std::ranges::minmax(one, two, std::ranges::less{}, [](int i) { return i == 1 ? 10 : i; }); + assert(result.min == 2); + assert(result.max == 1); + } + { // test if std::invoke is used + struct S { + int i; + }; + S a[3] = {S{2}, S{1}, S{3}}; + decltype(auto) ret = std::ranges::minmax(a[0], a[1], {}, &S::i); + ASSERT_SAME_TYPE(decltype(ret), std::ranges::minmax_result); + assert(&ret.min == &a[1]); + assert(&ret.max == &a[0]); + assert(ret.min.i == 1); + assert(ret.max.i == 2); + } +} + +constexpr void test_initializer_list() { + { // test projection + auto proj = [](int i) { return i == 5 ? -100 : i; }; + auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::less{}, proj); + assert(ret.min == 5); + assert(ret.max == 9); + } + { // test comparator + auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater{}); + assert(ret.min == 9); + assert(ret.max == 1); + } + { + int compares = 0; + int projections = 0; + auto comparator = [&](int a, int b) { + ++compares; + return a < b; + }; + auto projection = [&](int a) { + ++projections; + return a; + }; + std::same_as> decltype(auto) ret = + std::ranges::minmax({1, 2, 3}, comparator, projection); + assert(ret.min == 1); + assert(ret.max == 3); + assert(compares == 3); + assert(projections == 6); + } + { + struct S { + int i; + }; + decltype(auto) ret = std::ranges::minmax({S{2}, S{1}, S{3}}, {}, &S::i); + ASSERT_SAME_TYPE(decltype(ret), std::ranges::minmax_result); + assert(ret.min.i == 1); + assert(ret.max.i == 3); + } +} + +constexpr void test_range() { + int a[] = {7, 6, 9, 3, 5, 1, 2, 4}; + { // test projection + auto proj = [](int& i) { return i == 5 ? -100 : i; }; + auto ret = std::ranges::minmax(a, std::ranges::less{}, proj); + assert(ret.min == 5); + assert(ret.max == 9); + } + { // test comparator + auto ret = std::ranges::minmax(a, std::ranges::greater{}); + assert(ret.min == 9); + assert(ret.max == 1); + } + { + int compares = 0; + int projections = 0; + auto comparator = [&](int x, int y) { + ++compares; + return x < y; + }; + auto projection = [&](int x) { + ++projections; + return x; + }; + std::same_as> decltype(auto) ret = + std::ranges::minmax(std::array{1, 2, 3}, comparator, projection); + assert(ret.min == 1); + assert(ret.max == 3); + assert(compares == 3); + assert(projections == 6); + } + { + struct S { + int i; + }; + S b[3] = {S{2}, S{1}, S{3}}; + decltype(auto) ret = std::ranges::minmax(b, {}, &S::i); + ASSERT_SAME_TYPE(decltype(ret), std::ranges::minmax_result); + assert(ret.min.i == 1); + assert(ret.max.i == 3); + } +} + +constexpr bool test() { + test_2_arguments(); + test_initializer_list(); + test_range(); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax_element.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax_element.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.minmax_element.pass.cpp @@ -0,0 +1,210 @@ +//===----------------------------------------------------------------------===// +// +// 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, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// template S, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr I ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {}); +// +// template, Proj>> Comp = ranges::less> +// constexpr borrowed_iterator_t ranges::minmax_element(R&& r, Comp comp = {}, Proj proj = {}); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +concept HasMinMaxElement = requires(T t) { std::ranges::minmax_element(t); }; + +struct NoLessThanOp {}; +struct NotTotallyOrdered { + int i; + bool operator<(const NotTotallyOrdered& o) const { return i < o.i; } +}; + +static_assert(HasMinMaxElement>); +static_assert(!HasMinMaxElement); +static_assert(!HasMinMaxElement); +static_assert(!HasMinMaxElement); + +template +constexpr void test_iterators(Iter first, Iter last) { + std::same_as> auto it = std::ranges::minmax_element(first, last); + auto expected = std::minmax_element(first, last); + assert(expected.first == it.min); + assert(expected.second == it.max); +} + +template +constexpr void test_range(Range&& rng, Iter begin, Iter end) { + std::same_as> auto it = std::ranges::minmax_element(std::forward(rng)); + auto expected = std::minmax_element(begin, end); + assert(expected.first == it.min); + assert(expected.second == it.max); +} + +template +constexpr void test(std::initializer_list a, int expectedMin, int expectedMax) { + using Expected = std::ranges::minmax_result; + const int* first = a.begin(); + const int* last = a.end(); + { + std::same_as auto it = std::ranges::minmax_element(It(first), It(last)); + assert(base(it.min) == first + expectedMin); + assert(base(it.max) == first + expectedMax); + } + { + using Sent = sentinel_wrapper; + std::same_as auto it = std::ranges::minmax_element(It(first), Sent(It(last))); + assert(base(it.min) == first + expectedMin); + assert(base(it.max) == first + expectedMax); + } + { + auto range = std::ranges::subrange(It(first), It(last)); + std::same_as auto it = std::ranges::minmax_element(range); + assert(base(it.min) == first + expectedMin); + assert(base(it.max) == first + expectedMax); + } + { + using Sent = sentinel_wrapper; + auto range = std::ranges::subrange(It(first), Sent(It(last))); + std::same_as auto it = std::ranges::minmax_element(range); + assert(base(it.min) == first + expectedMin); + assert(base(it.max) == first + expectedMax); + } +} + +template +constexpr bool test() { + test({}, 0, 0); + test({1}, 0, 0); + test({1, 2}, 0, 1); + test({2, 1}, 1, 0); + test({2, 1, 2}, 1, 2); + test({2, 1, 1}, 1, 0); + + return true; +} + +constexpr void test_borrowed_range_and_sentinel() { + int a[] = {7, 6, 1, 3, 5, 1, 2, 4}; + + std::ranges::minmax_result ret = std::ranges::minmax_element(std::views::all(a)); + assert(ret.min == a + 2); + assert(ret.max == a + 0); + assert(*ret.min == 1); + assert(*ret.max == 7); +} + +constexpr void test_comparator() { + int a[] = {7, 6, 9, 3, 5, 1, 2, 4}; + std::ranges::minmax_result ret = std::ranges::minmax_element(a, std::ranges::greater{}); + assert(ret.min == a + 2); + assert(ret.max == a + 5); + assert(*ret.min == 9); + assert(*ret.max == 1); +} + +constexpr void test_projection() { + int a[] = {7, 6, 9, 3, 5, 1, 2, 4}; + { + std::ranges::minmax_result ret = + std::ranges::minmax_element(a, std::ranges::less{}, [](int i) { return i == 5 ? -100 : i; }); + assert(ret.min == a + 4); + assert(ret.max == a + 2); + assert(*ret.min == 5); + assert(*ret.max == 9); + } + { + std::ranges::minmax_result ret = std::ranges::minmax_element(a, std::less{}, [](int& i) { return &i; }); + assert(ret.min == a + 0); + assert(ret.max == a + 7); + assert(*ret.min == 7); + assert(*ret.max == 4); + } +} + +struct Immobile { + int i; + + constexpr Immobile(int i_) : i(i_) {} + Immobile(const Immobile&) = delete; + Immobile(Immobile&&) = delete; + + auto operator<=>(const Immobile&) const = default; +}; + +constexpr void test_immobile() { + + Immobile arr[]{1, 2, 3}; + { + auto ret = std::ranges::minmax_element(arr); + assert(ret.min == arr + 0); + assert(ret.max == arr + 2); + } + { + auto ret = std::ranges::minmax_element(arr, arr + 3); + assert(ret.min == arr + 0); + assert(ret.max == arr + 2); + } +} + +constexpr void test_dangling() { + int compares = 0; + int projections = 0; + auto comparator = [&](int a, int b) { + ++compares; + return a < b; + }; + auto projection = [&](int a) { + ++projections; + return a; + }; + [[maybe_unused]] std::same_as> auto ret = + std::ranges::minmax_element(std::array{1, 2, 3}, comparator, projection); + assert(compares == 3); + assert(projections == 6); +} + +constexpr bool test() { + + test>(); + test>(); + test>(); + test(); + + int a[] = {7, 6, 5, 3, 4, 2, 1, 8}; + test_iterators(a, a + 8); + int a2[] = {7, 6, 5, 3, 4, 2, 1, 8}; + test_range(a2, a2, a2 + 8); + + test_borrowed_range_and_sentinel(); + test_comparator(); + test_projection(); + test_dangling(); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -102,8 +102,8 @@ //static_assert(test(std::ranges::merge, a, a, a)); //static_assert(test(std::ranges::min, a)); static_assert(test(std::ranges::min_element, a)); -//static_assert(test(std::ranges::minmax, a)); -//static_assert(test(std::ranges::minmax_element, a)); +static_assert(test(std::ranges::minmax, a)); +static_assert(test(std::ranges::minmax_element, a)); //static_assert(test(std::ranges::mismatch, a, a)); //static_assert(test(std::ranges::move, a, a)); //static_assert(test(std::ranges::move_backward, a, a));