Skip to content

[ADT] Relax iterator constraints on all_equal #106400

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 5 commits into from
Aug 28, 2024
Merged

Conversation

aws-taylor
Copy link
Contributor

The previous all_equal implementation contained Begin + 1, which implicitly requires Begin to model the random_access_iterator concept due to the usage of the + operator. By swapping this out with std::next, this method can be used with weaker iterator concepts, such as forward_iterator.

The previous `all_equal` implementation contained `Begin + 1`, which implicitly requires `Begin` to model the random_access_iterator concept due to the usage of the `+` operator. By swapping this out with `std::next`, this method can be used with weaker iterator concepts, such as forward_iterator.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2024

@llvm/pr-subscribers-llvm-adt

Author: None (aws-taylor)

Changes

The previous all_equal implementation contained Begin + 1, which implicitly requires Begin to model the random_access_iterator concept due to the usage of the + operator. By swapping this out with std::next, this method can be used with weaker iterator concepts, such as forward_iterator.


Full diff: https://github.com/llvm/llvm-project/pull/106400.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/STLExtras.h (+1-1)
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index b2df0cd650ad7b..e11d6cac7685e4 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2062,7 +2062,7 @@ bool equal(L &&LRange, R &&RRange, BinaryPredicate P) {
 template <typename R> bool all_equal(R &&Range) {
   auto Begin = adl_begin(Range);
   auto End = adl_end(Range);
-  return Begin == End || std::equal(Begin + 1, End, Begin);
+  return Begin == End || std::equal(std::next(Begin), End, Begin);
 }
 
 /// Returns true if all Values in the initializer lists are equal or the list

kuhar
kuhar previously approved these changes Aug 28, 2024
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aws-taylor. Could you also add a test?

@kuhar kuhar dismissed their stale review August 28, 2024 15:10

Misclick

@kuhar kuhar changed the title Relax iterator constraints on all_equal [ADT] Relax iterator constraints on all_equal Aug 28, 2024
@aws-taylor
Copy link
Contributor Author

aws-taylor commented Aug 28, 2024

@kuhar,

I don't see any examples of using type-parameterized tests in the codebase. How do y'all feel about those / would you prefer I just copy/paste the existing tests with a new type such as std:: forward_list ?

@kuhar
Copy link
Member

kuhar commented Aug 28, 2024

Yeah a simple test function with a list will do

Copy link

github-actions bot commented Aug 28, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for improving this

@kuhar
Copy link
Member

kuhar commented Aug 28, 2024

@aws-taylor please ping me when the CI passes and I will merge

@aws-taylor
Copy link
Contributor Author

@kuhar

Looks like the CI passed. Thanks for your help!

@kuhar kuhar merged commit 6b4b8dc into llvm:main Aug 28, 2024
8 checks passed
Copy link

@aws-taylor Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants