diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv index 0023d2b26748e4..3664106093c41e 100644 --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -26,7 +26,7 @@ Search,count_if,Nikolas Klauser, `D121523 `_,✅ Search,search,Not assigned,n/a,Not started Search,search_n,Not assigned,n/a,Not started Search,find_end,Not assigned,n/a,Not started -Read-only,is_partitioned,Christopher Di Bella,`D105794 `_,Under review +Read-only,is_partitioned,Nikolas Klauser,`D124440 `_,✅ Read-only,is_sorted,Not assigned,n/a,Not started Read-only,is_sorted_unitl,Not assigned,n/a,Not started Read-only,includes,Not assigned,n/a,Not started diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 257e9d59f59d16..b79f286a719945 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -77,6 +77,7 @@ set(files __algorithm/ranges_find_if_not.h __algorithm/ranges_for_each.h __algorithm/ranges_for_each_n.h + __algorithm/ranges_is_partitioned.h __algorithm/ranges_max.h __algorithm/ranges_max_element.h __algorithm/ranges_min.h diff --git a/libcxx/include/__algorithm/ranges_is_partitioned.h b/libcxx/include/__algorithm/ranges_is_partitioned.h new file mode 100644 index 00000000000000..3d572895ebe15f --- /dev/null +++ b/libcxx/include/__algorithm/ranges_is_partitioned.h @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// 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_IS_PARTITIONED_H +#define _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.h> +#include <__iterator/indirectly_comparable.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __is_partitioned { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __is_parititioned_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + for (; __first != __last; ++__first) { + if (!std::invoke(__pred, std::invoke(__proj, *__first))) + break; + } + + if (__first == __last) + return true; + ++__first; + + for (; __first != __last; ++__first) { + if (std::invoke(__pred, std::invoke(__proj, *__first))) + return false; + } + + return true; + } + + template _Sent, + class _Proj = identity, + indirect_unary_predicate> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const { + return __is_parititioned_impl(std::move(__first), std::move(__last), __pred, __proj); + } + + template , _Proj>> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { + return __is_parititioned_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __is_partitioned + +inline namespace __cpo { + inline constexpr auto is_partitioned = __is_partitioned::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 13b306429eaec3..1a75dc0dacb22b 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -256,6 +256,15 @@ namespace ranges { indirectly_unary_invocable> Fun> constexpr ranges::for_each_n_result ranges::for_each_n(I first, iter_difference_t n, Fun f, Proj proj = {}); // since C++20 + + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {}); // since C++20 + } constexpr bool // constexpr in C++20 @@ -983,6 +992,7 @@ template #include <__algorithm/ranges_find_if_not.h> #include <__algorithm/ranges_for_each.h> #include <__algorithm/ranges_for_each_n.h> +#include <__algorithm/ranges_is_partitioned.h> #include <__algorithm/ranges_max.h> #include <__algorithm/ranges_max_element.h> #include <__algorithm/ranges_min.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index c027bdcb522e3e..c291901c6bba05 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -305,6 +305,7 @@ module std [system] { module ranges_find_if_not { private header "__algorithm/ranges_find_if_not.h" } module ranges_for_each { private header "__algorithm/ranges_for_each.h" } module ranges_for_each_n { private header "__algorithm/ranges_for_each_n.h" } + module ranges_is_partitioned { private header "__algorithm/ranges_is_partitioned.h" } module ranges_max { private header "__algorithm/ranges_max.h" } module ranges_max_element { private header "__algorithm/ranges_max_element.h" } module ranges_min { private header "__algorithm/ranges_min.h" } diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp index f8b7ea5239acb3..c0228a94590f62 100644 --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -114,6 +114,7 @@ END-SCRIPT #include <__algorithm/ranges_find_if_not.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_find_if_not.h'}} #include <__algorithm/ranges_for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each.h'}} #include <__algorithm/ranges_for_each_n.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each_n.h'}} +#include <__algorithm/ranges_is_partitioned.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_is_partitioned.h'}} #include <__algorithm/ranges_max.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max.h'}} #include <__algorithm/ranges_max_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max_element.h'}} #include <__algorithm/ranges_min.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min.h'}} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp new file mode 100644 index 00000000000000..264a707dce619c --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp @@ -0,0 +1,229 @@ +//===----------------------------------------------------------------------===// +// +// 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-has-no-incomplete-ranges + +// + +// template S, class Proj = identity, +// indirect_unary_predicate> Pred> +// constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {}); + + +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct Functor { + bool operator()(auto&&); +}; + +template > +concept HasIsPartitionedIt = requires(Iter iter, Sent sent) { + std::ranges::is_partitioned(iter, sent, Functor{}); +}; + +static_assert(HasIsPartitionedIt); +static_assert(!HasIsPartitionedIt); +static_assert(!HasIsPartitionedIt); +static_assert(!HasIsPartitionedIt); +static_assert(!HasIsPartitionedIt); +static_assert(!HasIsPartitionedIt); + +template +concept HasIsPartitionedItPred = requires(int* first, int* last, Pred pred) { + std::ranges::is_partitioned(first, last, pred); +}; + +static_assert(HasIsPartitionedItPred); +static_assert(!HasIsPartitionedItPred); +static_assert(!HasIsPartitionedItPred); + +template +concept HasIsPartitionedR = requires (Range range) { + std::ranges::is_partitioned(range, Functor{}); +}; + +static_assert(HasIsPartitionedR>); +static_assert(!HasIsPartitionedR); +static_assert(!HasIsPartitionedR); +static_assert(!HasIsPartitionedR); +static_assert(!HasIsPartitionedR); +static_assert(!HasIsPartitionedR); + +template +concept HasIsPartitionedRPred = requires(Pred pred) { + std::ranges::is_partitioned(UncheckedRange{}, pred); +}; + +static_assert(HasIsPartitionedRPred); +static_assert(!HasIsPartitionedRPred); +static_assert(!HasIsPartitionedRPred); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4, 5}; + std::same_as decltype(auto) ret = + std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 5)), [](int i) { return i < 3; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 5))); + std::same_as decltype(auto) ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; }); + assert(ret); + } + } + + { // check that it's partitoned if the predicate is true for all elements + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4))); + auto ret = std::ranges::is_partitioned(range, [](int) { return true; }); + assert(ret); + } + } + + { // check that it's partitioned if the predicate is false for all elements + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int) { return false; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4))); + auto ret = std::ranges::is_partitioned(range, [](int) { return false; }); + assert(ret); + } + } + + { // check that false is returned if the range isn't partitioned + { + int a[] = {1, 3, 2, 4}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int i) { return i < 3; }); + assert(!ret); + } + { + int a[] = {1, 3, 2, 4}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4))); + auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; }); + assert(!ret); + } + } + + { // check that an empty range is partitioned + { + int a[] = {}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a)), [](int i) { return i < 3; }); + assert(ret); + } + { + int a[] = {}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a))); + auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; }); + assert(ret); + } + } + + { // check that a single element is partitioned + { + int a[] = {1}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 1)), [](int i) { return i < 3; }); + assert(ret); + } + { + int a[] = {1}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 1))); + auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; }); + assert(ret); + } + } + + { // check that it is partitioned when the first element is the partition point + { + int a[] = {0, 1, 1}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 3)), [](int i) { return i < 1; }); + assert(ret); + } + { + int a[] = {0, 1, 1}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3))); + auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 1; }); + assert(ret); + } + } + + { // check that it is partitioned when the last element is the partition point + { + int a[] = {0, 0, 1}; + auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 3)), [](int i) { return i < 1; }); + assert(ret); + } + { + int a[] = {0, 0, 1}; + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3))); + auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 1; }); + assert(ret); + } + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + test_iterators(); + + { // check that std:invoke is used + struct S { + int check; + int other; + + constexpr S& identity() { + return *this; + } + }; + { + S a[] = {{1, 2}, {3, 4}, {5, 6}}; + auto ret = std::ranges::is_partitioned(a, a + 3, &S::check, &S::identity); + assert(ret); + } + { + S a[] = {{1, 2}, {3, 4}, {5, 6}}; + auto ret = std::ranges::is_partitioned(a, &S::check, &S::identity); + assert(ret); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}