[libc++] Implement ranges::{all, any, none}_of

Reviewed By: ldionne, var-const, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D123016
This commit is contained in:
Nikolas Klauser
2022-05-26 16:42:46 +02:00
parent 14258d6fb5
commit 0e3dc1a52f
14 changed files with 735 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
Category,Algorithm,Assignee,CL,Complete
Search,any_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_
Search,all_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_
Search,none_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_
Search,any_of,Nikolas Klauser,`D123016 <https://llvm.org/D123016>`_,
Search,all_of,Nikolas Klauser,`D123016 <https://llvm.org/D123016>`_,
Search,none_of,Nikolas Klauser,`D123016 <https://llvm.org/D123016>`_,
Search,find,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,
Search,find_if,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,
Search,find_if_not,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,
1 Category,Algorithm,Assignee,CL,Complete Category Algorithm Assignee CL Complete
2 Search,any_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_ Search any_of Nikolas Klauser `D123016 <https://llvm.org/D123016>`_
3 Search,all_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_ Search all_of Nikolas Klauser `D123016 <https://llvm.org/D123016>`_
4 Search,none_of,Christopher Di Bella,`D105793 <https://llvm.org/D105793>`_ Search none_of Nikolas Klauser `D123016 <https://llvm.org/D123016>`_
5 Search,find,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,✅ Search find Nikolas Klauser `D121248 <https://reviews.llvm.org/D121248>`_
6 Search,find_if,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,✅ Search find_if Nikolas Klauser `D121248 <https://reviews.llvm.org/D121248>`_
7 Search,find_if_not,Nikolas Klauser,`D121248 <https://reviews.llvm.org/D121248>`_,✅ Search find_if_not Nikolas Klauser `D121248 <https://reviews.llvm.org/D121248>`_

View File

@@ -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

View File

@@ -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 <class _Iter, class _Sent, class _Proj, class _Pred>
_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 <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _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 <input_range _Range, class _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _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

View File

@@ -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 <class _Iter, class _Sent, class _Proj, class _Pred>
_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 <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _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 <input_range _Range, class _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _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

View File

@@ -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 <class _Iter, class _Sent, class _Proj, class _Pred>
_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 <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _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 <input_range _Range, class _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _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

View File

@@ -305,6 +305,30 @@ namespace ranges {
constexpr bool ranges::equal(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
template<input_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_unary_predicate<projected<I, Proj>> Pred>
constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {}); // since C++20
template<input_range R, class Proj = identity,
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {}); // since C++20
template<input_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_unary_predicate<projected<I, Proj>> Pred>
constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {}); // since C++20
template<input_range R, class Proj = identity,
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); // since C++20
template<input_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_unary_predicate<projected<I, Proj>> Pred>
constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); // since C++20
template<input_range R, class Proj = identity,
indirect_unary_predicate<projected<iterator_t<R>, 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 <class BidirectionalIterator, class Compare>
#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 <class BidirectionalIterator, class Compare>
#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>

View File

@@ -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" }

View File

@@ -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);

View File

@@ -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);

View File

@@ -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'}}

View File

@@ -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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I, sentinel_for<I> S, class Proj = identity,
// indirect_unary_predicate<projected<I, Proj>> Pred>
// constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {});
// template<input_range R, class Proj = identity,
// indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
// constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
struct UnaryFunctor {
bool operator()(auto&&);
};
template <class It, class Sent = sentinel_wrapper<It>>
concept HasAllOfIt = requires(It first, Sent last) { std::ranges::all_of(first, last, UnaryFunctor{}); };
static_assert(HasAllOfIt<int*>);
static_assert(!HasAllOfIt<InputIteratorNotDerivedFrom>);
static_assert(!HasAllOfIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasAllOfIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasAllOfIt<int*, SentinelForNotSemiregular>);
static_assert(!HasAllOfIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
template <class Func>
concept HasAllOfItFunc = requires(int* ptr) { std::ranges::all_of(ptr, ptr, Func{}); };
static_assert(HasAllOfItFunc<UnaryFunctor>);
static_assert(!HasAllOfItFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasAllOfItFunc<IndirectUnaryPredicateNotPredicate>);
template <class Range>
concept HasAllOfR = requires(Range range) { std::ranges::all_of(range, UnaryFunctor{}); };
static_assert(HasAllOfR<std::array<int, 10>>);
static_assert(!HasAllOfR<InputRangeNotDerivedFrom>);
static_assert(!HasAllOfR<InputRangeNotIndirectlyReadable>);
static_assert(!HasAllOfR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasAllOfR<InputRangeNotSentinelSemiregular>);
static_assert(!HasAllOfR<InputRangeNotSentinelEqualityComparableWith>);
template <class Func>
concept HasAllOfRFunc = requires(std::array<int, 10> range) { std::ranges::all_of(range, Func{}); };
static_assert(HasAllOfRFunc<UnaryFunctor>);
static_assert(!HasAllOfRFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasAllOfRFunc<IndirectUnaryPredicateNotPredicate>);
template <class It, class Sent = It>
constexpr void test_iterators() {
{ // simple test
{
int a[] = {1, 2, 3, 4};
std::same_as<bool> 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<bool> decltype(auto) ret = std::ranges::all_of(range, [](int) { return true; });
assert(ret);
}
}
{ // check that an empty range works
std::array<int, 0> 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<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterators<forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<int*>();
{ // 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;
}

View File

@@ -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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I, sentinel_for<I> S, class Proj = identity,
// indirect_unary_predicate<projected<I, Proj>> Pred>
// constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {});
// template<input_range R, class Proj = identity,
// indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
// constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
struct UnaryFunctor {
bool operator()(auto&&);
};
template <class It, class Sent = sentinel_wrapper<It>>
concept HasAnyOfIt = requires(It first, Sent last) { std::ranges::any_of(first, last, UnaryFunctor{}); };
static_assert(HasAnyOfIt<int*>);
static_assert(!HasAnyOfIt<InputIteratorNotDerivedFrom>);
static_assert(!HasAnyOfIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasAnyOfIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasAnyOfIt<int*, SentinelForNotSemiregular>);
static_assert(!HasAnyOfIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
template <class Func>
concept HasAnyOfItFunc = requires(int* ptr) { std::ranges::any_of(ptr, ptr, Func{}); };
static_assert(HasAnyOfItFunc<UnaryFunctor>);
static_assert(!HasAnyOfItFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasAnyOfItFunc<IndirectUnaryPredicateNotPredicate>);
template <class Range>
concept HasAnyOfR = requires(Range range) { std::ranges::any_of(range, UnaryFunctor{}); };
static_assert(HasAnyOfR<std::array<int, 10>>);
static_assert(!HasAnyOfR<InputRangeNotDerivedFrom>);
static_assert(!HasAnyOfR<InputRangeNotIndirectlyReadable>);
static_assert(!HasAnyOfR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasAnyOfR<InputRangeNotSentinelSemiregular>);
static_assert(!HasAnyOfR<InputRangeNotSentinelEqualityComparableWith>);
template <class Func>
concept HasAnyOfRFunc = requires(std::array<int, 10> range) { std::ranges::any_of(range, Func{}); };
static_assert(HasAnyOfRFunc<UnaryFunctor>);
static_assert(!HasAnyOfRFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasAnyOfRFunc<IndirectUnaryPredicateNotPredicate>);
template <class It, class Sent = It>
constexpr void test_iterators() {
{ // simple test
{
int a[] = {1, 2, 3, 4};
std::same_as<bool> 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<bool> decltype(auto) ret = std::ranges::any_of(range, [](int) { return true; });
assert(ret);
}
}
{ // check that an empty range works
std::array<int, 0> 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<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterators<forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<int*>();
{ // 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;
}

View File

@@ -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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I, sentinel_for<I> S, class Proj = identity,
// indirect_unary_predicate<projected<I, Proj>> Pred>
// constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {});
// template<input_range R, class Proj = identity,
// indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
// constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
struct UnaryFunctor {
bool operator()(auto&&);
};
template <class It, class Sent = sentinel_wrapper<It>>
concept HasNoneOfIt = requires(It first, Sent last) { std::ranges::none_of(first, last, UnaryFunctor{}); };
static_assert(HasNoneOfIt<int*>);
static_assert(!HasNoneOfIt<InputIteratorNotDerivedFrom>);
static_assert(!HasNoneOfIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasNoneOfIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasNoneOfIt<int*, SentinelForNotSemiregular>);
static_assert(!HasNoneOfIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
template <class Func>
concept HasNoneOfItFunc = requires(int* ptr) { std::ranges::none_of(ptr, ptr, Func{}); };
static_assert(HasNoneOfItFunc<UnaryFunctor>);
static_assert(!HasNoneOfItFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasNoneOfItFunc<IndirectUnaryPredicateNotPredicate>);
template <class Range>
concept HasNoneOfR = requires(Range range) { std::ranges::none_of(range, UnaryFunctor{}); };
static_assert(HasNoneOfR<std::array<int, 10>>);
static_assert(!HasNoneOfR<InputRangeNotDerivedFrom>);
static_assert(!HasNoneOfR<InputRangeNotIndirectlyReadable>);
static_assert(!HasNoneOfR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasNoneOfR<InputRangeNotSentinelSemiregular>);
static_assert(!HasNoneOfR<InputRangeNotSentinelEqualityComparableWith>);
template <class Func>
concept HasNoneOfRFunc = requires(std::array<int, 10> range) { std::ranges::none_of(range, Func{}); };
static_assert(HasNoneOfRFunc<UnaryFunctor>);
static_assert(!HasNoneOfRFunc<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasNoneOfRFunc<IndirectUnaryPredicateNotPredicate>);
template <class It, class Sent = It>
constexpr void test_iterators() {
{ // simple test
{
int a[] = {1, 2, 3, 4};
std::same_as<bool> 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<bool> decltype(auto) ret = std::ranges::none_of(range, [](int) { return true; });
assert(!ret);
}
}
{ // check that an empty range works
std::array<int, 0> 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<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterators<forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<int*>();
{ // 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;
}

View File

@@ -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));