[libc++][ranges] Implement ranges::find{, _if, _if_not}
Reviewed By: var-const, #libc, ldionne Spies: ldionne, tcanens, libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D121248
This commit is contained in:
parent
55a970fbd4
commit
ee0f8c4010
@ -2,9 +2,9 @@ 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,find,Christopher Di Bella,`D105456 <https://llvm.org/D105456>`_
|
||||
Search,find_if,Christopher Di Bella,`D105792 <https://llvm.org/D105792>`_
|
||||
Search,find_if_not,Christopher Di Bella,`D105792 <https://llvm.org/D105792>`_
|
||||
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>`_,✅
|
||||
Search,find_first_of,Not assigned,n/a,Not started
|
||||
Search,adjacent_find,Not assigned,n/a,Not started
|
||||
Search,mismatch,Nikolas Klauser,`D117817 <https://llvm.org/D117817>`,Complete
|
||||
|
|
@ -66,6 +66,9 @@ set(files
|
||||
__algorithm/pop_heap.h
|
||||
__algorithm/prev_permutation.h
|
||||
__algorithm/push_heap.h
|
||||
__algorithm/ranges_find.h
|
||||
__algorithm/ranges_find_if.h
|
||||
__algorithm/ranges_find_if_not.h
|
||||
__algorithm/ranges_max_element.h
|
||||
__algorithm/ranges_min_element.h
|
||||
__algorithm/ranges_mismatch.h
|
||||
|
63
libcxx/include/__algorithm/ranges_find.h
Normal file
63
libcxx/include/__algorithm/ranges_find.h
Normal file
@ -0,0 +1,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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_FIND_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_FIND_H
|
||||
|
||||
#include <__algorithm/ranges_find_if.h>
|
||||
#include <__config>
|
||||
#include <__functional/identity.h>
|
||||
#include <__functional/invoke.h>
|
||||
#include <__functional/ranges_operations.h>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/projected.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.h>
|
||||
#include <__utility/forward.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 __find {
|
||||
struct __fn {
|
||||
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, class _Proj = identity>
|
||||
requires indirect_binary_predicate<ranges::equal_to, projected<_Ip, _Proj>, const _Tp*>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
_Ip operator()(_Ip __first, _Sp __last, const _Tp& __value, _Proj __proj = {}) const {
|
||||
auto __pred = [&](auto&& __e) { return std::forward<decltype(__e)>(__e) == __value; };
|
||||
return ranges::__find_if_impl(std::move(__first), std::move(__last), __pred, __proj);
|
||||
}
|
||||
|
||||
template <input_range _Rp, class _Tp, class _Proj = identity>
|
||||
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Rp>, _Proj>, const _Tp*>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
borrowed_iterator_t<_Rp> operator()(_Rp&& __r, const _Tp& __value, _Proj __proj = {}) const {
|
||||
auto __pred = [&](auto&& __e) { return std::forward<decltype(__e)>(__e) == __value; };
|
||||
return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj);
|
||||
}
|
||||
};
|
||||
} // namespace __find
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto find = __find::__fn{};
|
||||
} // namespace __cpo
|
||||
} // namespace ranges
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
#endif // _LIBCPP___ALGORITHM_RANGES_FIND_H
|
71
libcxx/include/__algorithm/ranges_find_if.h
Normal file
71
libcxx/include/__algorithm/ranges_find_if.h
Normal file
@ -0,0 +1,71 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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_FIND_IF_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_FIND_IF_H
|
||||
|
||||
#include <__config>
|
||||
#include <__functional/identity.h>
|
||||
#include <__functional/invoke.h>
|
||||
#include <__functional/ranges_operations.h>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/projected.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.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 {
|
||||
|
||||
template <class _Ip, class _Sp, class _Pred, class _Proj>
|
||||
_LIBCPP_HIDE_FROM_ABI static constexpr
|
||||
_Ip __find_if_impl(_Ip __first, _Sp __last, _Pred& __pred, _Proj& __proj) {
|
||||
for (; __first != __last; ++__first) {
|
||||
if (std::invoke(__pred, std::invoke(__proj, *__first)))
|
||||
break;
|
||||
}
|
||||
return __first;
|
||||
}
|
||||
|
||||
namespace __find_if {
|
||||
struct __fn {
|
||||
|
||||
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Proj = identity,
|
||||
indirect_unary_predicate<projected<_Ip, _Proj>> _Pred>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
_Ip operator()(_Ip __first, _Sp __last, _Pred __pred, _Proj __proj = {}) const {
|
||||
return ranges::__find_if_impl(std::move(__first), std::move(__last), __pred, __proj);
|
||||
}
|
||||
|
||||
template <input_range _Rp, class _Proj = identity,
|
||||
indirect_unary_predicate<projected<iterator_t<_Rp>, _Proj>> _Pred>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
borrowed_iterator_t<_Rp> operator()(_Rp&& __r, _Pred __pred, _Proj __proj = {}) const {
|
||||
return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj);
|
||||
}
|
||||
};
|
||||
} // namespace __find_if
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto find_if = __find_if::__fn{};
|
||||
} // namespace __cpo
|
||||
} // namespace ranges
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
#endif // _LIBCPP___ALGORITHM_RANGES_FIND_IF_H
|
63
libcxx/include/__algorithm/ranges_find_if_not.h
Normal file
63
libcxx/include/__algorithm/ranges_find_if_not.h
Normal file
@ -0,0 +1,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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_FIND_IF_NOT_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_FIND_IF_NOT_H
|
||||
|
||||
#include <__algorithm/ranges_find_if.h>
|
||||
#include <__config>
|
||||
#include <__functional/identity.h>
|
||||
#include <__functional/invoke.h>
|
||||
#include <__functional/ranges_operations.h>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/projected.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.h>
|
||||
#include <__utility/forward.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 __find_if_not {
|
||||
struct __fn {
|
||||
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Proj = identity,
|
||||
indirect_unary_predicate<projected<_Ip, _Proj>> _Pred>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
_Ip operator()(_Ip __first, _Sp __last, _Pred __pred, _Proj __proj = {}) const {
|
||||
auto __pred2 = [&](auto&& __e) { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
|
||||
return ranges::__find_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
|
||||
}
|
||||
|
||||
template <input_range _Rp, class _Proj = identity,
|
||||
indirect_unary_predicate<projected<iterator_t<_Rp>, _Proj>> _Pred>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
borrowed_iterator_t<_Rp> operator()(_Rp&& __r, _Pred __pred, _Proj __proj = {}) const {
|
||||
auto __pred2 = [&](auto&& __e) { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
|
||||
return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred2, __proj);
|
||||
}
|
||||
};
|
||||
} // namespace __find_if_not
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto find_if_not = __find_if_not::__fn{};
|
||||
} // namespace __cpo
|
||||
} // namespace ranges
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
#endif // _LIBCPP___ALGORITHM_RANGES_FIND_IF_NOT_H
|
@ -56,6 +56,33 @@ namespace ranges {
|
||||
requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
|
||||
constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
|
||||
mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) // since C++20
|
||||
|
||||
template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
|
||||
requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
|
||||
constexpr I find(I first, S last, const T& value, Proj proj = {}); // since C++20
|
||||
|
||||
template<input_range R, class T, class Proj = identity>
|
||||
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
|
||||
constexpr borrowed_iterator_t<R>
|
||||
find(R&& r, const T& value, Proj proj = {}); // since C++20
|
||||
|
||||
template<input_iterator I, sentinel_for<I> S, class Proj = identity,
|
||||
indirect_unary_predicate<projected<I, Proj>> Pred>
|
||||
constexpr I find_if(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 borrowed_iterator_t<R>
|
||||
find_if(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 I find_if_not(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 borrowed_iterator_t<R>
|
||||
find_if_not(R&& r, Pred pred, Proj proj = {}); // since C++20
|
||||
}
|
||||
|
||||
template <class InputIterator, class Predicate>
|
||||
@ -775,6 +802,9 @@ template <class BidirectionalIterator, class Compare>
|
||||
#include <__algorithm/pop_heap.h>
|
||||
#include <__algorithm/prev_permutation.h>
|
||||
#include <__algorithm/push_heap.h>
|
||||
#include <__algorithm/ranges_find.h>
|
||||
#include <__algorithm/ranges_find_if.h>
|
||||
#include <__algorithm/ranges_find_if_not.h>
|
||||
#include <__algorithm/ranges_max_element.h>
|
||||
#include <__algorithm/ranges_min_element.h>
|
||||
#include <__algorithm/ranges_mismatch.h>
|
||||
|
@ -294,6 +294,9 @@ 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_find { private header "__algorithm/ranges_find.h" }
|
||||
module ranges_find_if { private header "__algorithm/ranges_find_if.h" }
|
||||
module ranges_find_if_not { private header "__algorithm/ranges_find_if_not.h" }
|
||||
module ranges_max_element { private header "__algorithm/ranges_max_element.h" }
|
||||
module ranges_min_element { private header "__algorithm/ranges_min_element.h" }
|
||||
module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" }
|
||||
|
@ -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_find.h'}}
|
||||
#include <__algorithm/ranges_find.h>
|
@ -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_find_if.h'}}
|
||||
#include <__algorithm/ranges_find_if.h>
|
@ -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_find_if_not.h'}}
|
||||
#include <__algorithm/ranges_find_if_not.h>
|
@ -0,0 +1,258 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 T, class Proj = identity>
|
||||
// requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
|
||||
// constexpr I ranges::find(I first, S last, const T& value, Proj proj = {});
|
||||
// template<input_range R, class T, class Proj = identity>
|
||||
// requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
|
||||
// constexpr borrowed_iterator_t<R>
|
||||
// ranges::find(R&& r, const T& value, Proj proj = {});
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
struct NotEqualityComparable {};
|
||||
|
||||
template <class It, class Sent = It>
|
||||
concept HasFindIt = requires(It it, Sent sent) { std::ranges::find(it, sent, *it); };
|
||||
static_assert(HasFindIt<int*>);
|
||||
static_assert(!HasFindIt<NotEqualityComparable*>);
|
||||
static_assert(!HasFindIt<InputIteratorNotDerivedFrom>);
|
||||
static_assert(!HasFindIt<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!HasFindIt<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(!HasFindIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
|
||||
static_assert(!HasFindIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
|
||||
|
||||
static_assert(!HasFindIt<int*, int>);
|
||||
static_assert(!HasFindIt<int, int*>);
|
||||
|
||||
template <class Range, class ValT>
|
||||
concept HasFindR = requires(Range r) { std::ranges::find(r, ValT{}); };
|
||||
static_assert(HasFindR<std::array<int, 1>, int>);
|
||||
static_assert(!HasFindR<int, int>);
|
||||
static_assert(!HasFindR<std::array<NotEqualityComparable, 1>, NotEqualityComparable>);
|
||||
static_assert(!HasFindR<InputRangeNotDerivedFrom, int>);
|
||||
static_assert(!HasFindR<InputRangeNotIndirectlyReadable, int>);
|
||||
static_assert(!HasFindR<InputRangeNotInputOrOutputIterator, int>);
|
||||
static_assert(!HasFindR<InputRangeNotSentinelSemiregular, int>);
|
||||
static_assert(!HasFindR<InputRangeNotSentinelEqualityComparableWith, int>);
|
||||
|
||||
template <class It, class Sent = It>
|
||||
constexpr void test_iterators() {
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<It> auto ret = std::ranges::find(It(a), Sent(It(a + 4)), 4);
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
|
||||
std::same_as<It> auto ret = std::ranges::find(range, 4);
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
}
|
||||
|
||||
struct OneWayComparable {
|
||||
bool isLeft;
|
||||
friend constexpr bool operator==(OneWayComparable l, OneWayComparable) { return l.isLeft; }
|
||||
};
|
||||
|
||||
struct NonConstComparableLValue {
|
||||
friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
|
||||
};
|
||||
|
||||
struct NonConstComparableRValue {
|
||||
friend constexpr bool operator==(const NonConstComparableRValue&, const NonConstComparableRValue&) { return false; }
|
||||
friend constexpr bool operator==(const NonConstComparableRValue&&, const NonConstComparableRValue&&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableRValue&&, NonConstComparableRValue&&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableRValue&&, const NonConstComparableRValue&) { return true; }
|
||||
};
|
||||
|
||||
constexpr bool test() {
|
||||
test_iterators<int*>();
|
||||
test_iterators<const int*>();
|
||||
test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
|
||||
test_iterators<bidirectional_iterator<int*>>();
|
||||
test_iterators<forward_iterator<int*>>();
|
||||
test_iterators<random_access_iterator<int*>>();
|
||||
test_iterators<contiguous_iterator<int*>>();
|
||||
|
||||
{
|
||||
// check that projections are used properly and that they are called with the iterator directly
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find(a, a + 4, a + 3, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find(a, a + 3, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that the first element is returned
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find(a, 0, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find(a, a + 3, 0, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that end + 1 iterator is returned with no match
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find(a, a + 3, 0);
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find(a, 0);
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that ranges::dangling is returned
|
||||
[[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
|
||||
std::ranges::find(std::array{1, 2}, 3);
|
||||
}
|
||||
|
||||
{
|
||||
// check that an iterator is returned with a borrowing range
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<int*> auto ret = std::ranges::find(std::views::all(a), 1);
|
||||
assert(ret == a);
|
||||
assert(*ret == 1);
|
||||
}
|
||||
|
||||
{
|
||||
// check that std::invoke is used
|
||||
struct S { int i; };
|
||||
S a[] = { S{1}, S{3}, S{2} };
|
||||
std::same_as<S*> auto ret = std::ranges::find(a, 4, &S::i);
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
|
||||
{
|
||||
// count invocations of the projection
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find(a, a + 4, 2, [&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find(a, 2, [&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check comparison order
|
||||
{
|
||||
OneWayComparable a[] = { OneWayComparable{true} };
|
||||
auto ret = std::ranges::find(a, a + 1, OneWayComparable{false});
|
||||
assert(ret == a);
|
||||
}
|
||||
{
|
||||
OneWayComparable a[] = { OneWayComparable{true} };
|
||||
auto ret = std::ranges::find(a, OneWayComparable{false});
|
||||
assert(ret == a);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that the return type of `iter::operator*` doesn't change
|
||||
{
|
||||
NonConstComparableLValue a[] = { NonConstComparableLValue{} };
|
||||
auto ret = std::ranges::find(a, a + 1, NonConstComparableLValue{});
|
||||
assert(ret == a);
|
||||
}
|
||||
{
|
||||
using It = std::move_iterator<NonConstComparableRValue*>;
|
||||
NonConstComparableRValue a[] = { NonConstComparableRValue{} };
|
||||
auto ret = std::ranges::find(It(a), It(a + 1), NonConstComparableRValue{});
|
||||
assert(ret.base() == a);
|
||||
}
|
||||
{
|
||||
NonConstComparableLValue a[] = { NonConstComparableLValue{} };
|
||||
auto ret = std::ranges::find(a, NonConstComparableLValue{});
|
||||
assert(ret == a);
|
||||
}
|
||||
{
|
||||
using It = std::move_iterator<NonConstComparableRValue*>;
|
||||
NonConstComparableRValue a[] = { NonConstComparableRValue{} };
|
||||
auto range = std::ranges::subrange(It(a), It(a + 1));
|
||||
auto ret = std::ranges::find(range, NonConstComparableRValue{});
|
||||
assert(ret.base() == a);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that an empty range works
|
||||
{
|
||||
std::array<int ,0> a = {};
|
||||
auto ret = std::ranges::find(a.begin(), a.end(), 1);
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
{
|
||||
std::array<int, 0> a = {};
|
||||
auto ret = std::ranges::find(a, 1);
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,235 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 I ranges::find_if(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 borrowed_iterator_t<R>
|
||||
// ranges::find_if(R&& r, Pred pred, Proj proj = {});
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
struct Predicate {
|
||||
bool operator()(int);
|
||||
};
|
||||
|
||||
template <class It, class Sent = It>
|
||||
concept HasFindIfIt = requires(It it, Sent sent) { std::ranges::find_if(it, sent, Predicate{}); };
|
||||
static_assert(HasFindIfIt<int*>);
|
||||
static_assert(!HasFindIfIt<InputIteratorNotDerivedFrom>);
|
||||
static_assert(!HasFindIfIt<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!HasFindIfIt<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(!HasFindIfIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
|
||||
static_assert(!HasFindIfIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
|
||||
|
||||
static_assert(!HasFindIfIt<int*, int>);
|
||||
static_assert(!HasFindIfIt<int, int*>);
|
||||
|
||||
template <class Pred>
|
||||
concept HasFindIfPred = requires(int* it, Pred pred) {std::ranges::find_if(it, it, pred); };
|
||||
|
||||
static_assert(!HasFindIfPred<IndirectUnaryPredicateNotCopyConstructible>);
|
||||
static_assert(!HasFindIfPred<IndirectUnaryPredicateNotPredicate>);
|
||||
|
||||
template <class R>
|
||||
concept HasFindIfR = requires(R r) { std::ranges::find_if(r, Predicate{}); };
|
||||
static_assert(HasFindIfR<std::array<int, 0>>);
|
||||
static_assert(!HasFindIfR<int>);
|
||||
static_assert(!HasFindIfR<InputRangeNotDerivedFrom>);
|
||||
static_assert(!HasFindIfR<InputRangeNotIndirectlyReadable>);
|
||||
static_assert(!HasFindIfR<InputRangeNotInputOrOutputIterator>);
|
||||
static_assert(!HasFindIfR<InputRangeNotSentinelSemiregular>);
|
||||
static_assert(!HasFindIfR<InputRangeNotSentinelEqualityComparableWith>);
|
||||
|
||||
template <class It, class Sent = It>
|
||||
constexpr void test_iterators() {
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<It> auto ret = std::ranges::find_if(It(a), Sent(It(a + 4)), [](int x) mutable { return x == 4; });
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
|
||||
std::same_as<It> auto ret = std::ranges::find_if(range, [](int x) mutable { return x == 4; });
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
}
|
||||
|
||||
struct NonConstComparable {
|
||||
friend constexpr bool operator==(const NonConstComparable&, const NonConstComparable&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparable&, NonConstComparable&) { return false; }
|
||||
friend constexpr bool operator==(const NonConstComparable&, NonConstComparable&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparable&, const NonConstComparable&) { return true; }
|
||||
};
|
||||
|
||||
constexpr bool test() {
|
||||
test_iterators<int*>();
|
||||
test_iterators<const int*>();
|
||||
test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
|
||||
test_iterators<bidirectional_iterator<int*>>();
|
||||
test_iterators<forward_iterator<int*>>();
|
||||
test_iterators<random_access_iterator<int*>>();
|
||||
test_iterators<contiguous_iterator<int*>>();
|
||||
|
||||
{
|
||||
// check that projections are used properly and that they are called with the iterator directly
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find_if(a, a + 4, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find_if(a, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that the first element is returned
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find_if(a, [](int i){ return i == 0; }, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find_if(a, a + 3, [](int i) { return i == 0; }, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that end + 1 iterator is returned with no match
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find_if(a, a + 3, [](int) { return false; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find_if(a, [](int){ return false; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that ranges::dangling is returned
|
||||
[[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
|
||||
std::ranges::find_if(std::array{1, 2}, [](int){ return false; });
|
||||
}
|
||||
|
||||
{
|
||||
// check that an iterator is returned with a borrowing range
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<int*> auto ret = std::ranges::find_if(std::views::all(a), [](int){ return true; });
|
||||
assert(ret == a);
|
||||
assert(*ret == 1);
|
||||
}
|
||||
|
||||
{
|
||||
// check that std::invoke is used
|
||||
struct S { int i; };
|
||||
S a[] = { S{1}, S{3}, S{2} };
|
||||
std::same_as<S*> auto ret = std::ranges::find_if(a, [](int) { return false; }, &S::i);
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
|
||||
{
|
||||
// count projection and predicate invocation count
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int predicate_count = 0;
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find_if(a, a + 4,
|
||||
[&](int i) { ++predicate_count; return i == 2; },
|
||||
[&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(predicate_count == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int predicate_count = 0;
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find_if(a,
|
||||
[&](int i) { ++predicate_count; return i == 2; },
|
||||
[&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(predicate_count == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that the return type of `iter::operator*` doesn't change
|
||||
{
|
||||
NonConstComparable a[] = { NonConstComparable{} };
|
||||
auto ret = std::ranges::find_if(a, a + 1, [](auto&& e) { return e == NonConstComparable{}; });
|
||||
assert(ret == a);
|
||||
}
|
||||
{
|
||||
NonConstComparable a[] = { NonConstComparable{} };
|
||||
auto ret = std::ranges::find_if(a, [](auto&& e) { return e == NonConstComparable{}; });
|
||||
assert(ret == a);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that an empty range works
|
||||
{
|
||||
std::array<int ,0> a = {};
|
||||
auto ret = std::ranges::find_if(a.begin(), a.end(), [](int) { return true; });
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
{
|
||||
std::array<int, 0> a = {};
|
||||
auto ret = std::ranges::find_if(a, [](int) { return true; });
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,229 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 I ranges::find_if_not(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 borrowed_iterator_t<R>
|
||||
// ranges::find_if_not(R&& r, Pred pred, Proj proj = {});
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
struct Predicate {
|
||||
bool operator()(int);
|
||||
};
|
||||
|
||||
template <class It, class Sent = It>
|
||||
concept HasFindIfNotIt = requires(It it, Sent sent) { std::ranges::find_if_not(it, sent, Predicate{}); };
|
||||
static_assert(HasFindIfNotIt<int*>);
|
||||
static_assert(!HasFindIfNotIt<InputIteratorNotDerivedFrom>);
|
||||
static_assert(!HasFindIfNotIt<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!HasFindIfNotIt<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
|
||||
static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
|
||||
|
||||
static_assert(!HasFindIfNotIt<int*, int>);
|
||||
static_assert(!HasFindIfNotIt<int, int*>);
|
||||
|
||||
template <class Pred>
|
||||
concept HasFindIfNotPred = requires(int* it, Pred pred) {std::ranges::find_if_not(it, it, pred); };
|
||||
|
||||
static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotCopyConstructible>);
|
||||
static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotPredicate>);
|
||||
|
||||
template <class R>
|
||||
concept HasFindIfNotR = requires(R r) { std::ranges::find_if_not(r, Predicate{}); };
|
||||
static_assert(HasFindIfNotR<std::array<int, 0>>);
|
||||
static_assert(!HasFindIfNotR<int>);
|
||||
static_assert(!HasFindIfNotR<InputRangeNotDerivedFrom>);
|
||||
static_assert(!HasFindIfNotR<InputRangeNotIndirectlyReadable>);
|
||||
static_assert(!HasFindIfNotR<InputRangeNotInputOrOutputIterator>);
|
||||
static_assert(!HasFindIfNotR<InputRangeNotSentinelSemiregular>);
|
||||
static_assert(!HasFindIfNotR<InputRangeNotSentinelEqualityComparableWith>);
|
||||
|
||||
template <class It, class Sent = It>
|
||||
constexpr void test_iterators() {
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<It> auto ret = std::ranges::find_if_not(It(a), Sent(It(a + 4)), [c = 0](int) mutable { return c++ <= 2; });
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
|
||||
std::same_as<It> auto ret = std::ranges::find_if_not(range, [c = 0](int) mutable { return c++ <= 2; });
|
||||
assert(base(ret) == a + 3);
|
||||
assert(*ret == 4);
|
||||
}
|
||||
}
|
||||
|
||||
struct NonConstComparableLValue {
|
||||
friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
|
||||
friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
|
||||
};
|
||||
|
||||
constexpr bool test() {
|
||||
test_iterators<int*>();
|
||||
test_iterators<const int*>();
|
||||
test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
|
||||
test_iterators<bidirectional_iterator<int*>>();
|
||||
test_iterators<forward_iterator<int*>>();
|
||||
test_iterators<random_access_iterator<int*>>();
|
||||
test_iterators<contiguous_iterator<int*>>();
|
||||
|
||||
{ // check that projections are used properly and that they are called with the iterator directly
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find_if_not(a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
auto ret = std::ranges::find_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that the first element is returned
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find_if_not(a, [](int i){ return i != 0; }, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
{
|
||||
struct S {
|
||||
int comp;
|
||||
int other;
|
||||
};
|
||||
S a[] = { {0, 0}, {0, 2}, {0, 1} };
|
||||
auto ret = std::ranges::find_if_not(a, a + 3, [](int i) { return i != 0; }, &S::comp);
|
||||
assert(ret == a);
|
||||
assert(ret->comp == 0);
|
||||
assert(ret->other == 0);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that end + 1 iterator is returned with no match
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find_if(a, a + 3, [](int) { return false; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 1, 1};
|
||||
auto ret = std::ranges::find_if(a, [](int){ return false; });
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that ranges::dangling is returned
|
||||
[[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
|
||||
std::ranges::find_if_not(std::array{1, 2}, [](int){ return true; });
|
||||
}
|
||||
|
||||
{ // check that an iterator is returned with a borrowing range
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::same_as<int*> auto ret = std::ranges::find_if_not(std::views::all(a), [](int){ return false; });
|
||||
assert(ret == a);
|
||||
assert(*ret == 1);
|
||||
}
|
||||
|
||||
{ // check that std::invoke is used
|
||||
struct S { int i; };
|
||||
S a[] = { S{1}, S{3}, S{2} };
|
||||
std::same_as<S*> auto ret = std::ranges::find_if_not(a, [](int) { return true; }, &S::i);
|
||||
assert(ret == a + 3);
|
||||
}
|
||||
|
||||
{ // count projection and predicate invocation count
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int predicate_count = 0;
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find_if_not(a, a + 4,
|
||||
[&](int i) { ++predicate_count; return i != 2; },
|
||||
[&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(predicate_count == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int predicate_count = 0;
|
||||
int projection_count = 0;
|
||||
auto ret = std::ranges::find_if_not(a,
|
||||
[&](int i) { ++predicate_count; return i != 2; },
|
||||
[&](int i) { ++projection_count; return i; });
|
||||
assert(ret == a + 1);
|
||||
assert(*ret == 2);
|
||||
assert(predicate_count == 2);
|
||||
assert(projection_count == 2);
|
||||
}
|
||||
}
|
||||
|
||||
{ // check that the return type of `iter::operator*` doesn't change
|
||||
{
|
||||
NonConstComparableLValue a[] = { NonConstComparableLValue{} };
|
||||
auto ret = std::ranges::find_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparableLValue{}; });
|
||||
assert(ret == a);
|
||||
}
|
||||
{
|
||||
NonConstComparableLValue a[] = { NonConstComparableLValue{} };
|
||||
auto ret = std::ranges::find_if_not(a, [](auto&& e) { return e != NonConstComparableLValue{}; });
|
||||
assert(ret == a);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// check that an empty range works
|
||||
{
|
||||
std::array<int ,0> a = {};
|
||||
auto ret = std::ranges::find_if_not(a.begin(), a.end(), [](int) { return true; });
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
{
|
||||
std::array<int, 0> a = {};
|
||||
auto ret = std::ranges::find_if_not(a, [](int) { return true; });
|
||||
assert(ret == a.begin());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
@ -54,7 +54,7 @@ constexpr bool test(CPO& o, Args&&...) {
|
||||
|
||||
int *p;
|
||||
int a[10];
|
||||
//auto odd = [](int x) { return x % 2 != 0; };
|
||||
auto odd = [](int x) { return x % 2 != 0; };
|
||||
//auto triple = [](int x) { return 3*x; };
|
||||
//auto plus = [](int x, int y) { return x == y; };
|
||||
//std::mt19937 g;
|
||||
@ -77,11 +77,11 @@ int a[10];
|
||||
//static_assert(test(std::ranges::equal_range, a, 42));
|
||||
//static_assert(test(std::ranges::fill, a, 42));
|
||||
//static_assert(test(std::ranges::fill_n, a, 10, 42));
|
||||
//static_assert(test(std::ranges::find, a, 42));
|
||||
static_assert(test(std::ranges::find, a, 42));
|
||||
//static_assert(test(std::ranges::find_end, a, a));
|
||||
//static_assert(test(std::ranges::find_first_of, a, a));
|
||||
//static_assert(test(std::ranges::find_if, a, odd));
|
||||
//static_assert(test(std::ranges::find_if_not, a, odd));
|
||||
static_assert(test(std::ranges::find_if, a, odd));
|
||||
static_assert(test(std::ranges::find_if_not, a, odd));
|
||||
//static_assert(test(std::ranges::for_each, a, odd));
|
||||
//static_assert(test(std::ranges::for_each_n, a, 10, odd));
|
||||
//static_assert(test(std::ranges::generate, a, 42));
|
||||
|
131
libcxx/test/support/almost_satisfies_types.h
Normal file
131
libcxx/test/support/almost_satisfies_types.h
Normal file
@ -0,0 +1,131 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 ALMOST_SATISFIES_TYPES_H
|
||||
#define ALMOST_SATISFIES_TYPES_H
|
||||
|
||||
#include <iterator>
|
||||
#include <ranges>
|
||||
|
||||
#include "test_iterators.h"
|
||||
|
||||
template <class T, class U = sentinel_wrapper<T>>
|
||||
class UncheckedRange {
|
||||
public:
|
||||
T begin();
|
||||
U end();
|
||||
};
|
||||
|
||||
static_assert(std::ranges::contiguous_range<UncheckedRange<int*, int*>>);
|
||||
|
||||
// almost an input_iterator
|
||||
class InputIteratorNotDerivedFrom {
|
||||
public:
|
||||
using difference_type = long;
|
||||
using value_type = int;
|
||||
using iterator_category = void;
|
||||
|
||||
InputIteratorNotDerivedFrom& operator++();
|
||||
void operator++(int);
|
||||
const int& operator*() const;
|
||||
};
|
||||
|
||||
using InputRangeNotDerivedFrom = UncheckedRange<InputIteratorNotDerivedFrom>;
|
||||
|
||||
static_assert(std::input_or_output_iterator<InputIteratorNotDerivedFrom>);
|
||||
static_assert(std::indirectly_readable<InputIteratorNotDerivedFrom>);
|
||||
static_assert(!std::input_iterator<InputIteratorNotDerivedFrom>);
|
||||
static_assert(!std::ranges::input_range<InputRangeNotDerivedFrom>);
|
||||
|
||||
class InputIteratorNotIndirectlyReadable {
|
||||
public:
|
||||
using difference_type = long;
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
|
||||
InputIteratorNotIndirectlyReadable& operator++();
|
||||
void operator++(int);
|
||||
const int& operator*() const;
|
||||
};
|
||||
|
||||
using InputRangeNotIndirectlyReadable = UncheckedRange<InputIteratorNotIndirectlyReadable>;
|
||||
|
||||
static_assert(std::input_or_output_iterator<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!std::indirectly_readable<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!std::input_iterator<InputIteratorNotIndirectlyReadable>);
|
||||
static_assert(!std::ranges::input_range<InputIteratorNotIndirectlyReadable>);
|
||||
|
||||
class InputIteratorNotInputOrOutputIterator {
|
||||
public:
|
||||
using difference_type = long;
|
||||
using value_type = int;
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
|
||||
int& operator++();
|
||||
void operator++(int);
|
||||
const int& operator*() const;
|
||||
};
|
||||
|
||||
using InputRangeNotInputOrOutputIterator = UncheckedRange<InputIteratorNotInputOrOutputIterator>;
|
||||
|
||||
static_assert(!std::input_or_output_iterator<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(std::indirectly_readable<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(!std::input_iterator<InputIteratorNotInputOrOutputIterator>);
|
||||
static_assert(!std::ranges::input_range<InputRangeNotInputOrOutputIterator>);
|
||||
|
||||
// almost an indirect_unary_predicate
|
||||
class IndirectUnaryPredicateNotCopyConstructible {
|
||||
public:
|
||||
IndirectUnaryPredicateNotCopyConstructible(const IndirectUnaryPredicateNotCopyConstructible&) = delete;
|
||||
bool operator()(int) const;
|
||||
};
|
||||
|
||||
static_assert(std::predicate<IndirectUnaryPredicateNotCopyConstructible, int&>);
|
||||
static_assert(!std::indirect_unary_predicate<IndirectUnaryPredicateNotCopyConstructible, int*>);
|
||||
|
||||
class IndirectUnaryPredicateNotPredicate {
|
||||
public:
|
||||
bool operator()(int&&) const;
|
||||
};
|
||||
|
||||
static_assert(!std::predicate<IndirectUnaryPredicateNotPredicate, int&>);
|
||||
static_assert(!std::indirect_unary_predicate<IndirectUnaryPredicateNotPredicate, int*>);
|
||||
|
||||
// almost a sentinel_for cpp20_input_iterator
|
||||
class SentinelForNotSemiregular {
|
||||
public:
|
||||
SentinelForNotSemiregular() = delete;
|
||||
using difference_type = long;
|
||||
SentinelForNotSemiregular& operator++();
|
||||
void operator++(int);
|
||||
const int& operator*() const;
|
||||
friend bool operator==(const SentinelForNotSemiregular&, const cpp20_input_iterator<int*>&);
|
||||
};
|
||||
|
||||
using InputRangeNotSentinelSemiregular = UncheckedRange<cpp20_input_iterator<int*>, SentinelForNotSemiregular>;
|
||||
|
||||
static_assert(std::input_or_output_iterator<SentinelForNotSemiregular>);
|
||||
static_assert(!std::semiregular<SentinelForNotSemiregular>);
|
||||
static_assert(!std::sentinel_for<SentinelForNotSemiregular, cpp20_input_iterator<int*>>);
|
||||
|
||||
// almost a sentinel_for cpp20_input_iterator
|
||||
class SentinelForNotWeaklyEqualityComparableWith {
|
||||
public:
|
||||
using difference_type = long;
|
||||
SentinelForNotWeaklyEqualityComparableWith& operator++();
|
||||
void operator++(int);
|
||||
const int& operator*() const;
|
||||
};
|
||||
|
||||
using InputRangeNotSentinelEqualityComparableWith =
|
||||
UncheckedRange<cpp20_input_iterator<int*>, SentinelForNotWeaklyEqualityComparableWith>;
|
||||
|
||||
static_assert(std::input_or_output_iterator<SentinelForNotWeaklyEqualityComparableWith>);
|
||||
static_assert(std::semiregular<SentinelForNotWeaklyEqualityComparableWith>);
|
||||
static_assert(!std::sentinel_for<SentinelForNotWeaklyEqualityComparableWith, cpp20_input_iterator<int*>>);
|
||||
|
||||
#endif // ALMOST_SATISFIES_TYPES_H
|
Loading…
x
Reference in New Issue
Block a user