-
Notifications
You must be signed in to change notification settings - Fork 117
Add ranges::{starts,ends}_with #2618
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
Open
akukanov
wants to merge
3
commits into
main
Choose a base branch
from
dev/ranges-starts-ends-with-akukanov
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // -*- C++ -*- | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Copyright (C) 2026 UXL Foundation Contributors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "std_ranges_test.h" | ||
|
|
||
| #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_starts_ends_with >= 202106L | ||
| auto checker = TEST_PREPARE_CALLABLE(std::ranges::ends_with); | ||
| #else | ||
| struct { | ||
| template<std::ranges::input_range R1, std::ranges::input_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 = {}) | ||
| { | ||
| std::ranges::reverse_view r2_reverse{r2}; | ||
| auto last = r2_reverse.end(); | ||
| return std::ranges::mismatch(std::ranges::reverse_view(r1), r2_reverse, pred, proj1, proj2).in2 == last; | ||
| } | ||
| } checker; | ||
| #endif | ||
| #endif | ||
|
|
||
| std::int32_t | ||
| main() | ||
| { | ||
| #if _ENABLE_STD_RANGES_TESTING | ||
| using namespace test_std_ranges; | ||
| namespace dpl_ranges = oneapi::dpl::ranges; | ||
|
|
||
| using data_gen_shift_med = decltype([](auto i){ return i + medium_size/2; }); | ||
| using data_gen_shift_big = decltype([](auto i){ return i + big_size/2; }); | ||
|
|
||
| launcher<0, int>{big_sz}(dpl_ranges::ends_with, checker, binary_pred_const); | ||
| launcher<1, int>{}(dpl_ranges::ends_with, checker, binary_pred, proj); | ||
| launcher<2, int, decltype(proj)>{}(dpl_ranges::ends_with, checker, binary_pred, proj); | ||
| launcher<3, P2>{}(dpl_ranges::ends_with, checker, binary_pred_const, &P2::x, &P2::proj); | ||
| launcher<4, P2>{}(dpl_ranges::ends_with, checker, binary_pred, &P2::proj, &P2::x); | ||
| launcher<5, int, data_gen_shift_med>{}(dpl_ranges::ends_with, checker); | ||
| launcher<6, int, data_gen_shift_big>{big_sz}(dpl_ranges::ends_with, checker); | ||
| #endif //_ENABLE_STD_RANGES_TESTING | ||
|
|
||
| return TestUtils::done(_ENABLE_STD_RANGES_TESTING); | ||
| } |
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,52 @@ | ||
| // -*- C++ -*- | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Copyright (C) 2026 UXL Foundation Contributors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "std_ranges_test.h" | ||
|
|
||
| #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_starts_ends_with >= 202106L | ||
| auto checker = TEST_PREPARE_CALLABLE(std::ranges::starts_with); | ||
| #else | ||
| struct { | ||
| template<std::ranges::input_range R1, std::ranges::input_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 = {}) | ||
| { | ||
| auto last = std::ranges::end(r2); | ||
| return std::ranges::mismatch(r1, r2, pred, proj1, proj2).in2 == last; | ||
| } | ||
| } checker; | ||
| #endif | ||
| #endif | ||
|
|
||
| std::int32_t | ||
| main() | ||
| { | ||
| #if _ENABLE_STD_RANGES_TESTING | ||
| using namespace test_std_ranges; | ||
| namespace dpl_ranges = oneapi::dpl::ranges; | ||
|
|
||
| auto almost_always_i = [](auto i){ return (i == medium_size/2 + 19)? 0 : i; }; | ||
| using data_gen_needle = decltype(almost_always_i); | ||
|
|
||
| launcher<0, int>{big_sz}(dpl_ranges::starts_with, checker, binary_pred_const); | ||
| launcher<1, int>{}(dpl_ranges::starts_with, checker, binary_pred, proj); | ||
| launcher<2, int, decltype(proj)>{}(dpl_ranges::starts_with, checker, binary_pred, proj); | ||
| launcher<3, P2>{}(dpl_ranges::starts_with, checker, binary_pred_const, &P2::x, &P2::proj); | ||
| launcher<4, P2>{}(dpl_ranges::starts_with, checker, binary_pred, &P2::proj, &P2::x); | ||
| launcher<5, int, data_gen_needle>{}(dpl_ranges::starts_with, checker); | ||
| #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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
views::alldeliberately used instead ofranges::ref_view, which is shown in the standard? If yes, what is the motivation?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the standard,
In our case,
ref_view{__r1}is valid (it can be formally proven based on the definition of theref_viewconstructor), so the 3rd option is never considered. And that I think makes the use ofviews::allboth appropriate and beneficial, as it avoids creating an extra indirection in case__r1is already a view, and it "simplifies" the expression syntax via "pipelining".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
views::allis almost always good enough, in my opinion unless you explicitly want a view with reference-like semantics or specifically want owning semantics.Also, Alexey is right in making a point that
all_tdoes not create any indirection, when possible. This technique is used instd::rangesdeduction guides all over the place.