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.
Add partition_point #73577
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
Add partition_point #73577
Changes from 9 commits
4c8ce48
c9b4915
27b06f1
8cc6998
52f9762
9335787
83d5998
d720a19
60f2ba2
6f8ad3b
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
I think we'd better change to "If this slice is not sorted ..." as we actually want to tell the end-user an ordered slice is required for this method.
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.
The comment is right -- the slice has to be partitioned, and not ordered. Raison d'être of
partition_point
is that it works with non-Ord
things. To make this more precise, we might link to https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.is_partitioned.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.
Indeed in 95% cases
partition_point
is used to getlower/upper_bound
. However in 5% cases it looks more partition point than lower bound.For example, we can get all valid values from
[3, 1, 4, 1, 5, -1, -1, -1, -1, -1]
.Actually this array is sorted by
|&x| x != -1
in descending order (true is larger than false), but many user would think that the array is partitioned at index 5 by whether -1 or not.What do you think about adding like this?
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.
Thanks for the kind explanation and sorry for my misunderstanding. Yes, the slice requires partitioned instead of sorted.
However, I still confused by the comment
If this slice is not partitioned, the returned result is unspecified and meaningless
. If we did call this method on a not partitioned slice, the result should be anOption<size>
rather than an unspecified or meaningless value.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.
Binary search runs in O(log n) by assuming ordered. If it checks whether is sorted, it must slower than O(n).
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.
I think
eigher
is a typo? It should beeither
:)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.
oops thank you.
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.
Please add a comment about why this
unsafe
is fine. You can probably take the text frombinary_search
. Also see #66219 (let the comment start with// SAFETY:
).