Skip to content

Commit

Permalink
[libc++] Implement ranges::{all, any, none}_of
Browse files Browse the repository at this point in the history
Reviewed By: ldionne, var-const, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D123016
  • Loading branch information
philnik777 committed May 26, 2022
1 parent 14258d6 commit 0e3dc1a
Show file tree
Hide file tree
Showing 14 changed files with 735 additions and 18 deletions.
6 changes: 3 additions & 3 deletions libcxx/docs/Status/RangesAlgorithms.csv
@@ -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>`_,✅
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down
68 changes: 68 additions & 0 deletions 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 <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
68 changes: 68 additions & 0 deletions 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 <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
68 changes: 68 additions & 0 deletions 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 <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
27 changes: 27 additions & 0 deletions libcxx/include/algorithm
Expand Up @@ -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
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/module.modulemap
Expand Up @@ -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" }
Expand All @@ -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" }
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions libcxx/test/libcxx/private_headers.verify.cpp
Expand Up @@ -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'}}
Expand All @@ -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'}}
Expand Down

0 comments on commit 0e3dc1a

Please sign in to comment.