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,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_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> + +#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(__first, __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 @@ -964,6 +964,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,135 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +#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 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 +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4, 5}; + std::same_as decltype(auto) ret = std::ranges::is_partitioned(a, a + 5, [](int i) { return i < 3; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5}; + std::same_as decltype(auto) ret = std::ranges::is_partitioned(a, [](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(a, a + 4, [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::is_partitioned(a, a + 4, [](int) { return true; }); + assert(ret); + } + } + + { // check that it's partitoned if the predicate is false for all elements + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::is_partitioned(a, a + 4, [](int) { return false; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::is_partitioned(a, a + 4, [](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(a, a + 4, [](int i) { return i < 3; }); + assert(!ret); + } + { + int a[] = {1, 3, 2, 4}; + auto ret = std::ranges::is_partitioned(a, a + 4, [](int i) { return i < 3; }); + 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; +}