diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -26,7 +26,7 @@ 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 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -75,6 +75,7 @@ __algorithm/ranges_find.h __algorithm/ranges_find_if.h __algorithm/ranges_find_if_not.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 --- /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 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -238,6 +238,15 @@ requires indirectly_copyable, I> constexpr ranges::copy_backward_result, I> ranges::copy_backward(R&& r, I result); // 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 @@ -964,6 +973,7 @@ #include <__algorithm/ranges_find.h> #include <__algorithm/ranges_find_if.h> #include <__algorithm/ranges_find_if_not.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 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -303,6 +303,7 @@ module ranges_find { private header "__algorithm/ranges_find.h" } module ranges_find_if { private header "__algorithm/ranges_find_if.h" } module ranges_find_if_not { private header "__algorithm/ranges_find_if_not.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 --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -112,6 +112,7 @@ #include <__algorithm/ranges_find.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_find.h'}} #include <__algorithm/ranges_find_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_find_if.h'}} #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_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 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp @@ -0,0 +1,225 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +template > +concept HasIsPartitionedIt = requires(Iter iter, Sent sent) { + std::ranges::is_partitioned(iter, sent, std::identity{}); +}; + +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, std::identity{}); +}; + +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; +}