From 0e3dc1a52ffe8a2c7464d6acaeaec23eca21f4f4 Mon Sep 17 00:00:00 2001 From: Nikolas Klauser Date: Thu, 26 May 2022 16:42:46 +0200 Subject: [PATCH] [libc++] Implement ranges::{all, any, none}_of Reviewed By: ldionne, var-const, #libc Spies: libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D123016 --- libcxx/docs/Status/RangesAlgorithms.csv | 6 +- libcxx/include/CMakeLists.txt | 3 + libcxx/include/__algorithm/ranges_all_of.h | 68 ++++++++ libcxx/include/__algorithm/ranges_any_of.h | 68 ++++++++ libcxx/include/__algorithm/ranges_none_of.h | 68 ++++++++ libcxx/include/algorithm | 27 +++ libcxx/include/module.modulemap | 3 + ...obust_against_copying_comparators.pass.cpp | 12 +- ...obust_against_copying_projections.pass.cpp | 12 +- libcxx/test/libcxx/private_headers.verify.cpp | 3 + .../alg.all_of/ranges.all_of.pass.cpp | 159 ++++++++++++++++++ .../alg.any_of/ranges.any_of.pass.cpp | 159 ++++++++++++++++++ .../alg.none_of/ranges.none_of.pass.cpp | 159 ++++++++++++++++++ .../niebloid.compile.pass.cpp | 6 +- 14 files changed, 735 insertions(+), 18 deletions(-) create mode 100644 libcxx/include/__algorithm/ranges_all_of.h create mode 100644 libcxx/include/__algorithm/ranges_any_of.h create mode 100644 libcxx/include/__algorithm/ranges_none_of.h create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv index c757abd2201d..283470af0f11 100644 --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -1,7 +1,7 @@ Category,Algorithm,Assignee,CL,Complete -Search,any_of,Christopher Di Bella,`D105793 `_ -Search,all_of,Christopher Di Bella,`D105793 `_ -Search,none_of,Christopher Di Bella,`D105793 `_ +Search,any_of,Nikolas Klauser,`D123016 `_,✅ +Search,all_of,Nikolas Klauser,`D123016 `_,✅ +Search,none_of,Nikolas Klauser,`D123016 `_,✅ Search,find,Nikolas Klauser,`D121248 `_,✅ Search,find_if,Nikolas Klauser,`D121248 `_,✅ Search,find_if_not,Nikolas Klauser,`D121248 `_,✅ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 65261da12586..80261bf2a453 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -66,6 +66,8 @@ set(files __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_all_of.h + __algorithm/ranges_any_of.h __algorithm/ranges_copy.h __algorithm/ranges_copy_backward.h __algorithm/ranges_copy_if.h @@ -88,6 +90,7 @@ set(files __algorithm/ranges_minmax.h __algorithm/ranges_minmax_element.h __algorithm/ranges_mismatch.h + __algorithm/ranges_none_of.h __algorithm/ranges_reverse.h __algorithm/ranges_swap_ranges.h __algorithm/ranges_transform.h diff --git a/libcxx/include/__algorithm/ranges_all_of.h b/libcxx/include/__algorithm/ranges_all_of.h new file mode 100644 index 000000000000..a146865652a7 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_all_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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_ALL_OF_H +#define _LIBCPP___ALGORITHM_RANGES_ALL_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.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 __all_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __all_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + 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 __all_of_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 __all_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __all_of + +inline namespace __cpo { + inline constexpr auto all_of = __all_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_ALL_OF_H diff --git a/libcxx/include/__algorithm/ranges_any_of.h b/libcxx/include/__algorithm/ranges_any_of.h new file mode 100644 index 000000000000..11c52cbe21ea --- /dev/null +++ b/libcxx/include/__algorithm/ranges_any_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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_ANY_OF_H +#define _LIBCPP___ALGORITHM_RANGES_ANY_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.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 __any_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __any_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + for (; __first != __last; ++__first) { + if (std::invoke(__pred, std::invoke(__proj, *__first))) + return true; + } + return false; + } + + 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 __any_of_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 __any_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __any_of + +inline namespace __cpo { + inline constexpr auto any_of = __any_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_ANY_OF_H diff --git a/libcxx/include/__algorithm/ranges_none_of.h b/libcxx/include/__algorithm/ranges_none_of.h new file mode 100644 index 000000000000..706ff5cd741d --- /dev/null +++ b/libcxx/include/__algorithm/ranges_none_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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_NONE_OF_H +#define _LIBCPP___ALGORITHM_RANGES_NONE_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.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 __none_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __none_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + 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 __none_of_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 __none_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __none_of + +inline namespace __cpo { + inline constexpr auto none_of = __none_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_NONE_OF_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index aa2191464a83..30f647c416f8 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -305,6 +305,30 @@ namespace ranges { constexpr bool ranges::equal(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + } constexpr bool // constexpr in C++20 @@ -1021,6 +1045,8 @@ template #include <__algorithm/pop_heap.h> #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> +#include <__algorithm/ranges_all_of.h> +#include <__algorithm/ranges_any_of.h> #include <__algorithm/ranges_copy.h> #include <__algorithm/ranges_copy_backward.h> #include <__algorithm/ranges_copy_if.h> @@ -1043,6 +1069,7 @@ template #include <__algorithm/ranges_minmax.h> #include <__algorithm/ranges_minmax_element.h> #include <__algorithm/ranges_mismatch.h> +#include <__algorithm/ranges_none_of.h> #include <__algorithm/ranges_reverse.h> #include <__algorithm/ranges_swap_ranges.h> #include <__algorithm/ranges_transform.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index cf394de39a2d..0e43ca4e44b1 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -298,6 +298,8 @@ module std [system] { module pop_heap { private header "__algorithm/pop_heap.h" } module prev_permutation { private header "__algorithm/prev_permutation.h" } module push_heap { private header "__algorithm/push_heap.h" } + module ranges_all_of { private header "__algorithm/ranges_all_of.h" } + module ranges_any_of { private header "__algorithm/ranges_any_of.h" } module ranges_copy { private header "__algorithm/ranges_copy.h" } module ranges_copy_backward { private header "__algorithm/ranges_copy_backward.h" } module ranges_copy_if { private header "__algorithm/ranges_copy_if.h" } @@ -320,6 +322,7 @@ module std [system] { module ranges_minmax { private header "__algorithm/ranges_minmax.h" } module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } + module ranges_none_of { private header "__algorithm/ranges_none_of.h" } module ranges_reverse { private header "__algorithm/ranges_reverse.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } module ranges_transform { private header "__algorithm/ranges_transform.h" } diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp index d5b7dd11f710..95f1ed7248df 100644 --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -92,10 +92,10 @@ constexpr bool all_the_algorithms() int copies = 0; //(void)std::ranges::adjacent_find(first, last, Equal(&copies)); assert(copies == 0); //(void)std::ranges::adjacent_find(a, Equal(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::all_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::any_of(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(first, last, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(a, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::clamp(value, value, value, Less(&copies)); assert(copies == 0); @@ -167,8 +167,8 @@ constexpr bool all_the_algorithms() (void)std::ranges::mismatch(a, b, Equal(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(first, last, Less(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(a, Less(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::none_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::none_of(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(first, mid, last, Less(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(a, mid, Less(&copies)); assert(copies == 0); //(void)std::ranges::partial_sort(first, mid, last, Less(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp index 8d6753d56208..9efb19d6e3ee 100644 --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp @@ -74,10 +74,10 @@ constexpr bool all_the_algorithms() int copies = 0; //(void)std::ranges::adjacent_find(first, last, Equal(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::adjacent_find(a, Equal(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::all_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::all_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::any_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::any_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(first, last, value, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(a, value, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::clamp(T(), T(), T(), Less(), Proj(&copies)); assert(copies == 0); @@ -150,8 +150,8 @@ constexpr bool all_the_algorithms() (void)std::ranges::mismatch(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(first, last, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(a, Less(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::none_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::none_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(first, mid, last, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(a, mid, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::partial_sort(first, mid, last, Less(), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp index b83308c7b8ea..d7889f7bb2b8 100644 --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -103,6 +103,8 @@ END-SCRIPT #include <__algorithm/pop_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pop_heap.h'}} #include <__algorithm/prev_permutation.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/prev_permutation.h'}} #include <__algorithm/push_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/push_heap.h'}} +#include <__algorithm/ranges_all_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_all_of.h'}} +#include <__algorithm/ranges_any_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_any_of.h'}} #include <__algorithm/ranges_copy.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy.h'}} #include <__algorithm/ranges_copy_backward.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy_backward.h'}} #include <__algorithm/ranges_copy_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy_if.h'}} @@ -125,6 +127,7 @@ END-SCRIPT #include <__algorithm/ranges_minmax.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}} #include <__algorithm/ranges_minmax_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}} #include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}} +#include <__algorithm/ranges_none_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_none_of.h'}} #include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}} #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}} #include <__algorithm/ranges_transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp new file mode 100644 index 000000000000..343d60ae9ef6 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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::all_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasAllOfIt = requires(It first, Sent last) { std::ranges::all_of(first, last, UnaryFunctor{}); }; + +static_assert(HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); + +template +concept HasAllOfItFunc = requires(int* ptr) { std::ranges::all_of(ptr, ptr, Func{}); }; + +static_assert(HasAllOfItFunc); +static_assert(!HasAllOfItFunc); +static_assert(!HasAllOfItFunc); + +template +concept HasAllOfR = requires(Range range) { std::ranges::all_of(range, UnaryFunctor{}); }; + +static_assert(HasAllOfR>); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); + +template +concept HasAllOfRFunc = requires(std::array range) { std::ranges::all_of(range, Func{}); }; + +static_assert(HasAllOfRFunc); +static_assert(!HasAllOfRFunc); +static_assert(!HasAllOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::all_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::all_of(range, [](int) { return true; }); + assert(ret); + } + } + + { // check that an empty range works + std::array a; + assert(std::ranges::all_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(std::ranges::all_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return true; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(std::ranges::all_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return true; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::all_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that false is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::all_of(range, [](int i) { return i > 5; })); + } + + { // check that true is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::all_of(range, [](int i) { return i < 5; })); + } + + { // check that false is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(!std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::all_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::all_of(a, a + 3, [](int i) { return i == 1; }, &S::check)); + assert(std::ranges::all_of(a, [](int i) { return i == 1; }, &S::check)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp new file mode 100644 index 000000000000..28045e6b2860 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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::any_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasAnyOfIt = requires(It first, Sent last) { std::ranges::any_of(first, last, UnaryFunctor{}); }; + +static_assert(HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); + +template +concept HasAnyOfItFunc = requires(int* ptr) { std::ranges::any_of(ptr, ptr, Func{}); }; + +static_assert(HasAnyOfItFunc); +static_assert(!HasAnyOfItFunc); +static_assert(!HasAnyOfItFunc); + +template +concept HasAnyOfR = requires(Range range) { std::ranges::any_of(range, UnaryFunctor{}); }; + +static_assert(HasAnyOfR>); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); + +template +concept HasAnyOfRFunc = requires(std::array range) { std::ranges::any_of(range, Func{}); }; + +static_assert(HasAnyOfRFunc); +static_assert(!HasAnyOfRFunc); +static_assert(!HasAnyOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::any_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::any_of(range, [](int) { return true; }); + assert(ret); + } + } + + { // check that an empty range works + std::array a; + assert(!std::ranges::any_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(!std::ranges::any_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(!std::ranges::any_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::any_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that false is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::any_of(range, [](int i) { return i > 5; })); + } + + { // check that true is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::any_of(range, [](int i) { return i < 5; })); + } + + { // check that true is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::any_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::any_of(a, a + 3, [](int i) { return i == 1; }, &S::check)); + assert(std::ranges::any_of(a, [](int i) { return i == 1; }, &S::check)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp new file mode 100644 index 000000000000..1975dfff6b3b --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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::none_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasNoneOfIt = requires(It first, Sent last) { std::ranges::none_of(first, last, UnaryFunctor{}); }; + +static_assert(HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); + +template +concept HasNoneOfItFunc = requires(int* ptr) { std::ranges::none_of(ptr, ptr, Func{}); }; + +static_assert(HasNoneOfItFunc); +static_assert(!HasNoneOfItFunc); +static_assert(!HasNoneOfItFunc); + +template +concept HasNoneOfR = requires(Range range) { std::ranges::none_of(range, UnaryFunctor{}); }; + +static_assert(HasNoneOfR>); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); + +template +concept HasNoneOfRFunc = requires(std::array range) { std::ranges::none_of(range, Func{}); }; + +static_assert(HasNoneOfRFunc); +static_assert(!HasNoneOfRFunc); +static_assert(!HasNoneOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::none_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(!ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::none_of(range, [](int) { return true; }); + assert(!ret); + } + } + + { // check that an empty range works + std::array a; + assert(std::ranges::none_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(std::ranges::none_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(std::ranges::none_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::none_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that true is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::none_of(range, [](int i) { return i > 5; })); + } + + { // check that false is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::none_of(range, [](int i) { return i < 5; })); + } + + { // check that false is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::none_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::none_of(a, a + 3, [](int i) { return i != 1; }, &S::check)); + assert(std::ranges::none_of(a, [](int i) { return i != 1; }, &S::check)); + } + + 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 index 03c0563597b9..47707c091c47 100644 --- 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 @@ -61,8 +61,8 @@ auto odd = [](int x) { return x % 2 != 0; }; // [algorithm.syn] //static_assert(test(std::ranges::adjacent_find, a)); -//static_assert(test(std::ranges::all_of, a, odd)); -//static_assert(test(std::ranges::any_of, a, odd)); +static_assert(test(std::ranges::all_of, a, odd)); +static_assert(test(std::ranges::any_of, a, odd)); //static_assert(test(std::ranges::binary_search, a, 42)); //static_assert(test(std::ranges::clamp, 42, 42, 42)); //static_assert(test(std::ranges::copy, a, a)); @@ -107,7 +107,7 @@ 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)); //static_assert(test(std::ranges::next_permutation, a)); -//static_assert(test(std::ranges::none_of, a, odd)); +static_assert(test(std::ranges::none_of, a, odd)); //static_assert(test(std::ranges::nth_element, a, a+5)); //static_assert(test(std::ranges::partial_sort, a, a+5)); //static_assert(test(std::ranges::partial_sort_copy, a, a));