Skip to content

[libc++] Implement ranges::contains #65148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 92 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
02e9afd
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
55a6817
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 1, 2023
80c08f6
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
34d8e29
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
9db49dd
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
e0ea68c
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 7, 2023
54bccdd
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
826bd98
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
32cf818
[libc++] Implement ranges::contains
ZijunZhaoCCK Aug 31, 2023
0174984
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 12, 2023
9fd0ede
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 12, 2023
a19c951
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 12, 2023
56e0a7c
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 12, 2023
4420189
update the tests and make it more elegant
ZijunZhaoCCK Sep 12, 2023
e00ebb2
[NFC] test commit
Keenuts Sep 11, 2023
7481a66
update test types
ZijunZhaoCCK Sep 12, 2023
90a64b6
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 12, 2023
f37aa1d
simplify code
ZijunZhaoCCK Sep 13, 2023
441a2cb
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Sep 20, 2023
e21911a
nits fixed
ZijunZhaoCCK Oct 4, 2023
de8820d
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 13, 2023
7f452e3
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 13, 2023
fdff1a4
1. remove Comparable and TriviallyComparable
ZijunZhaoCCK Oct 13, 2023
89153ef
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 13, 2023
c49ea46
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 13, 2023
1a2b342
[NFC] test commit
Keenuts Sep 11, 2023
51672ee
reformat
ZijunZhaoCCK Oct 13, 2023
c7a85d7
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 17, 2023
302b936
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 17, 2023
adc8c39
1. add robust tests
ZijunZhaoCCK Oct 17, 2023
717fdde
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 17, 2023
4700079
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 19, 2023
321a010
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 19, 2023
3e16dde
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 21, 2023
3ae6559
Merge branch 'main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 25, 2023
f8113e7
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 30, 2023
7406611
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 30, 2023
2df36fe
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 30, 2023
e88ff21
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 31, 2023
eabbd2c
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Oct 31, 2023
decac9b
Add benchmark tests
ZijunZhaoCCK Oct 31, 2023
295757b
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Nov 2, 2023
1dc09ff
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Nov 29, 2023
061b132
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Nov 29, 2023
19ce63f
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Nov 30, 2023
d779a80
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Nov 30, 2023
3555d17
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 6, 2023
ff58d89
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 6, 2023
81aab9c
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 6, 2023
eb4ca79
Improve benchmark tests.
ZijunZhaoCCK Dec 6, 2023
5354f00
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 7, 2023
16a3c3d
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 12, 2023
221cd0a
Update benchmark tests and identity.h
ZijunZhaoCCK Dec 12, 2023
898a8c7
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 13, 2023
8530245
reformat
ZijunZhaoCCK Dec 13, 2023
5299856
reformat test
ZijunZhaoCCK Dec 13, 2023
bea58e6
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 13, 2023
2262fca
reformat
ZijunZhaoCCK Dec 13, 2023
68d5519
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 13, 2023
1ebcc0c
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 13, 2023
2f8b619
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
23a9e61
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
9658671
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
bc510f8
fix build failure
ZijunZhaoCCK Dec 14, 2023
04cfbed
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
bf1c492
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
42d7a3c
reformat
ZijunZhaoCCK Dec 14, 2023
6f0a529
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 14, 2023
64ed92f
reformat
ZijunZhaoCCK Dec 14, 2023
d68f8c7
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 15, 2023
f6a7b11
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 15, 2023
1ef7586
reformat
ZijunZhaoCCK Dec 14, 2023
a8d2fd5
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 15, 2023
ab88ae1
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 15, 2023
01e3830
Update files
ZijunZhaoCCK Dec 14, 2023
3d26bac
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 17, 2023
a3ac4bc
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 18, 2023
3e207a4
1. remove move() for end sentinel 2. add more tests
ZijunZhaoCCK Dec 18, 2023
11ead5f
reformat
ZijunZhaoCCK Dec 18, 2023
215cfef
reformat
ZijunZhaoCCK Dec 18, 2023
c5aacec
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 18, 2023
1c06faf
fix CI build error
ZijunZhaoCCK Dec 18, 2023
7bc83d3
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 18, 2023
9019dbf
update tests
ZijunZhaoCCK Dec 18, 2023
76e9682
reformat
ZijunZhaoCCK Dec 18, 2023
89a2f6e
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 18, 2023
f724c88
Update header files and tests
ZijunZhaoCCK Dec 18, 2023
7fff049
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 18, 2023
65cec29
Update header files and tests
ZijunZhaoCCK Dec 18, 2023
65e5d8a
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 19, 2023
7b24f0d
Merge branch 'llvm:main' into libcxx-ranges-contains
ZijunZhaoCCK Dec 19, 2023
256ab44
Update header files and tests
ZijunZhaoCCK Dec 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcxx/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ set(BENCHMARK_TESTS
algorithms/pop_heap.bench.cpp
algorithms/pstl.stable_sort.bench.cpp
algorithms/push_heap.bench.cpp
algorithms/ranges_contains.bench.cpp
algorithms/ranges_ends_with.bench.cpp
algorithms/ranges_make_heap.bench.cpp
algorithms/ranges_make_heap_then_sort_heap.bench.cpp
Expand Down
49 changes: 49 additions & 0 deletions libcxx/benchmarks/algorithms/ranges_contains.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include <algorithm>
#include <benchmark/benchmark.h>
#include <iterator>
#include <vector>

#include "test_iterators.h"

static void bm_contains_char(benchmark::State& state) {
std::vector<char> a(state.range(), 'a');

for (auto _ : state) {
benchmark::DoNotOptimize(a);

benchmark::DoNotOptimize(std::ranges::contains(a.begin(), a.end(), 'B'));
}
}
BENCHMARK(bm_contains_char)->RangeMultiplier(16)->Range(16, 16 << 20);

static void bm_contains_int(benchmark::State& state) {
std::vector<int> a(state.range(), 1);

for (auto _ : state) {
benchmark::DoNotOptimize(a);

benchmark::DoNotOptimize(std::ranges::contains(a.begin(), a.end(), 2));
}
}
BENCHMARK(bm_contains_int)->RangeMultiplier(16)->Range(16, 16 << 20);

static void bm_contains_bool(benchmark::State& state) {
std::vector<bool> a(state.range(), true);

for (auto _ : state) {
benchmark::DoNotOptimize(a);

benchmark::DoNotOptimize(std::ranges::contains(a.begin(), a.end(), false));
}
}
BENCHMARK(bm_contains_bool)->RangeMultiplier(16)->Range(16, 16 << 20);

BENCHMARK_MAIN();
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ set(files
__algorithm/ranges_any_of.h
__algorithm/ranges_binary_search.h
__algorithm/ranges_clamp.h
__algorithm/ranges_contains.h
__algorithm/ranges_copy.h
__algorithm/ranges_copy_backward.h
__algorithm/ranges_copy_if.h
Expand Down
61 changes: 61 additions & 0 deletions libcxx/include/__algorithm/ranges_contains.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// 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_CONTAINS_H
#define _LIBCPP___ALGORITHM_RANGES_CONTAINS_H

#include <__algorithm/ranges_find.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/ranges_operations.h>
#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
#include <__iterator/indirectly_comparable.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 >= 23

_LIBCPP_BEGIN_NAMESPACE_STD

namespace ranges {
namespace __contains {
struct __fn {
template <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static
operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) {
return ranges::find(std::move(__first), __last, __value, std::ref(__proj)) != __last;
}

template <input_range _Range, class _Type, class _Proj = identity>
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static
operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) {
return ranges::find(ranges::begin(__range), ranges::end(__range), __value, std::ref(__proj)) !=
ranges::end(__range);
}
};
} // namespace __contains

inline namespace __cpo {
inline constexpr auto contains = __contains::__fn{};
} // namespace __cpo
} // namespace ranges

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_STD_VER >= 23

#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_H
9 changes: 9 additions & 0 deletions libcxx/include/__functional/identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define _LIBCPP___FUNCTIONAL_IDENTITY_H

#include <__config>
#include <__functional/reference_wrapper.h>
#include <__type_traits/integral_constant.h>
#include <__utility/forward.h>

Expand All @@ -34,6 +35,10 @@ struct __identity {

template <>
struct __is_identity<__identity> : true_type {};
template <>
struct __is_identity<reference_wrapper<__identity> > : true_type {};
template <>
struct __is_identity<reference_wrapper<const __identity> > : true_type {};

#if _LIBCPP_STD_VER >= 20

Expand All @@ -48,6 +53,10 @@ struct identity {

template <>
struct __is_identity<identity> : true_type {};
template <>
struct __is_identity<reference_wrapper<identity> > : true_type {};
template <>
struct __is_identity<reference_wrapper<const identity> > : true_type {};

#endif // _LIBCPP_STD_VER >= 20

Expand Down
9 changes: 9 additions & 0 deletions libcxx/include/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ namespace ranges {
template<class I1, class I2>
using copy_backward_result = in_out_result<I1, I2>; // 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 bool ranges::contains(I first, S last, const T& value, Proj proj = {}); // since C++23

template<input_range R, class T, class Proj = identity>
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {}); // since C++23

template<input_iterator I, sentinel_for<I> S, weakly_incrementable O>
requires indirectly_copyable<I, O>
constexpr ranges::copy_result<I, O> ranges::copy(I first, S last, O result); // since C++20
Expand Down Expand Up @@ -1845,6 +1853,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_any_of.h>
#include <__algorithm/ranges_binary_search.h>
#include <__algorithm/ranges_clamp.h>
#include <__algorithm/ranges_contains.h>
#include <__algorithm/ranges_copy.h>
#include <__algorithm/ranges_copy_backward.h>
#include <__algorithm/ranges_copy_if.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ module std_private_algorithm_ranges_clamp [system
header "__algorithm/ranges_clamp.h"
export std_private_functional_ranges_operations
}
module std_private_algorithm_ranges_contains [system] { header "__algorithm/ranges_contains.h" }
module std_private_algorithm_ranges_copy [system] {
header "__algorithm/ranges_copy.h"
export std_private_algorithm_in_out_result
Expand Down
4 changes: 2 additions & 2 deletions libcxx/modules/std/algorithm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export namespace std {
}

// [alg.contains], contains
#if 0
namespace ranges {
using std::ranges::contains;
#if 0
using std::ranges::contains_subrange;
} // namespace ranges
#endif
} // namespace ranges

// [alg.foreach], for each
using std::for_each;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ constexpr bool all_the_algorithms()
(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);
#if TEST_STD_VER >= 23
(void)std::ranges::contains(first, last, value, Proj(&copies));
assert(copies == 0);
(void)std::ranges::contains(a, value, Proj(&copies));
assert(copies == 0);
#endif
(void)std::ranges::count(first, last, value, Proj(&copies)); assert(copies == 0);
(void)std::ranges::count(a, value, Proj(&copies)); assert(copies == 0);
(void)std::ranges::count_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void test_algorithms() {
#if TEST_STD_VER >= 17
std::clamp(2, 1, 3);
std::clamp(2, 3, 1, std::greater<int>());
#endif
#if TEST_STD_VER >= 23
std::ranges::contains(arr, arr + 1, 1);
#endif
std::count_if(std::begin(arr), std::end(arr), P());
std::count(std::begin(arr), std::end(arr), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void test_algorithms() {
std::clamp(2, 1, 3, std::greater<int>());
#endif

#if TEST_STD_VER >= 23
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::contains(arr, arr + 1, 1);
#endif

// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::count_if(std::begin(arr), std::end(arr), P());

Expand Down
Loading