-
Notifications
You must be signed in to change notification settings - Fork 118
Add ranges::contains[_subrange] #2607
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
+139
−0
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
847a8e7
Add ranges::contains and ranges::contains_range
akukanov 6b16318
Use input ranges as lvalues to avoid dangling pointers
akukanov 7b93499
Add a test for ranges::contains
akukanov 63f5fa1
Add a test for ranges::contains_subrange
akukanov 38fe99e
Improve formatting and comments
akukanov 078df62
Add forward declarations
akukanov 9fa865c
Fix a range size check
akukanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // -*- C++ -*- | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Copyright (C) 2026 UXL Foundation Contributors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "std_ranges_test.h" | ||
|
|
||
| #if _ENABLE_STD_RANGES_TESTING | ||
| #if __cpp_lib_ranges_contains >= 202207L | ||
| auto contains_checker = TEST_PREPARE_CALLABLE(std::ranges::contains); | ||
| #else | ||
| struct { | ||
| template<std::ranges::input_range R, typename V, typename Proj = std::identity> | ||
| bool operator()(R&& r, V value, Proj proj = {}) | ||
| { | ||
| auto last = std::ranges::end(r); | ||
| return std::ranges::find(std::ranges::begin(r), last, value, proj) != last; | ||
| } | ||
| } contains_checker; | ||
| #endif | ||
| #endif | ||
|
|
||
| std::int32_t | ||
| main() | ||
| { | ||
| #if _ENABLE_STD_RANGES_TESTING | ||
| using namespace test_std_ranges; | ||
| namespace dpl_ranges = oneapi::dpl::ranges; | ||
|
|
||
| // expected to be found | ||
| test_range_algo<0>{big_sz}(dpl_ranges::contains, contains_checker, small_size - 19); | ||
| test_range_algo<1>{}(dpl_ranges::contains, contains_checker, proj(small_size/2 + 28), proj); | ||
| test_range_algo<2, P2>{}(dpl_ranges::contains, contains_checker, 137, &P2::x); | ||
|
|
||
| // expected to be absent | ||
| test_range_algo<3, P2>{}(dpl_ranges::contains, contains_checker, -27, &P2::proj); | ||
| test_range_algo<4>{big_sz}(dpl_ranges::contains, contains_checker, -43); | ||
| #endif //_ENABLE_STD_RANGES_TESTING | ||
|
|
||
| return TestUtils::done(_ENABLE_STD_RANGES_TESTING); | ||
| } |
53 changes: 53 additions & 0 deletions
53
test/parallel_api/ranges/std_ranges_contains_subrange.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // -*- C++ -*- | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Copyright (C) 2026 UXL Foundation Contributors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "std_ranges_test.h" | ||
|
|
||
| #if _ENABLE_STD_RANGES_TESTING | ||
| template<int call_id, typename T, typename DataGen2 = std::identity> | ||
| using launcher = test_std_ranges::test_range_algo<call_id, T, test_std_ranges::data_in_in, | ||
| /*DataGen1*/ std::identity, DataGen2>; | ||
| #if __cpp_lib_ranges_contains >= 202207L | ||
| auto checker = TEST_PREPARE_CALLABLE(std::ranges::contains_subrange); | ||
| #else | ||
| struct { | ||
| template<std::ranges::forward_range R1, std::ranges::forward_range R2, typename Pred = std::ranges::equal_to, | ||
| typename Proj1 = std::identity, typename Proj2 = std::identity> | ||
| bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) | ||
| { | ||
| if (std::ranges::begin(r2) == std::ranges::end(r2)) | ||
| return true; | ||
| auto&& result = std::ranges::search(r1, r2, pred, proj1, proj2); | ||
| return !result.empty(); | ||
| } | ||
| } checker; | ||
| #endif | ||
| #endif | ||
|
|
||
| std::int32_t | ||
| main() | ||
| { | ||
| #if _ENABLE_STD_RANGES_TESTING | ||
| using namespace test_std_ranges; | ||
| namespace dpl_ranges = oneapi::dpl::ranges; | ||
|
|
||
| auto shift = [](auto i) { return i + 371; }; | ||
| using data_gen_shifted = decltype(shift); | ||
|
|
||
| // Sizes of both sequences vary in the test, so each call might test both successful and unsuccessful searches | ||
| launcher<0, int>{big_sz}(dpl_ranges::contains_subrange, checker, binary_pred_const); | ||
| launcher<1, int>{}(dpl_ranges::contains_subrange, checker); | ||
| launcher<2, int>{}(dpl_ranges::contains_subrange, checker, binary_pred, dpl::identity{}); | ||
| launcher<3, int, data_gen_shifted>{big_sz}(dpl_ranges::contains_subrange, checker, binary_pred, proj, proj); | ||
| launcher<4, P3, data_gen_shifted>{}(dpl_ranges::contains_subrange, checker, binary_pred_const, &P3::x, &P3::proj); | ||
| launcher<5, P3>{}(dpl_ranges::contains_subrange, checker, std::equal_to<>{}, &P3::proj, &P3::y); | ||
| #endif //_ENABLE_STD_RANGES_TESTING | ||
|
|
||
| return TestUtils::done(_ENABLE_STD_RANGES_TESTING); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.