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 @@ -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 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -66,6 +66,8 @@ __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_all_of.h + __algorithm/ranges_any_of.h __algorithm/ranges_find.h __algorithm/ranges_find_if.h __algorithm/ranges_find_if_not.h @@ -73,6 +75,7 @@ __algorithm/ranges_min.h __algorithm/ranges_min_element.h __algorithm/ranges_mismatch.h + __algorithm/ranges_none_of.h __algorithm/ranges_swap_ranges.h __algorithm/remove.h __algorithm/remove_copy.h diff --git a/libcxx/include/__algorithm/ranges_all_of.h b/libcxx/include/__algorithm/ranges_all_of.h new file mode 100644 --- /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 --- /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 --- /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 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -96,6 +96,30 @@ requires indirectly_copyable_storable, range_value_t*> constexpr range_value_t min(R&& r, Comp comp = {}, Proj proj = {}); // 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 @@ -813,6 +837,9 @@ #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_none_of.h> #include <__algorithm/ranges_find.h> #include <__algorithm/ranges_find_if.h> #include <__algorithm/ranges_find_if_not.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -294,6 +294,8 @@ 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_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" } @@ -301,6 +303,7 @@ module ranges_min { private header "__algorithm/ranges_min.h" } module ranges_min_element { private header "__algorithm/ranges_min_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } + module ranges_none_of { private header "__algorithm/ranges_none_of.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_all_of.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_all_of.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_all_of.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_all_of.h'}} +#include <__algorithm/ranges_all_of.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_any_of.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_any_of.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_any_of.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_any_of.h'}} +#include <__algorithm/ranges_any_of.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_none_of.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_none_of.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_none_of.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_none_of.h'}} +#include <__algorithm/ranges_none_of.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 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +template > +concept HasAllOfIt = requires(It first, Sent last) { std::ranges::all_of(first, last, std::identity{}); }; + +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, std::identity{}); }; + +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 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +template > +concept HasAnyOfIt = requires(It first, Sent last) { std::ranges::any_of(first, last, std::identity{}); }; + +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, std::identity{}); }; + +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 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +template > +concept HasNoneOfIt = requires(It first, Sent last) { std::ranges::none_of(first, last, std::identity{}); }; + +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, std::identity{}); }; + +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; +}