-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Unexpected/undocumented binary_search
behavior with duplicates
#51817
Comments
The exact behavior is deliberately unspecified. In fact it is "documented" that the position is unspecified:
See #45333 for a discussion. (As discussed in #45333, we may want a |
Thanks for your response. I completely missed that detail when reading the documentation. Perhaps an extra phrase/sentence could be added to the documentation summary to make it more explicit? I'm probably not alone in skipping reading the
|
@scottjmaddox That wording looks good to me! Do you want to open a pull request with that change? If not, I can 💁♂️ |
Also we may want to add the same addition to |
Hey, sorry, just now seeing your response (it was buried in gmail). If you would be willing to submit the PR, that would be awesome! |
Opened a PR: #54700 |
Clarify docs for when binary_search has many matches. Fixes #51817.
slice::binary_search
(and perhaps its cousins?) behave in a fairly unexpected (and undocumented) way in the presence of duplicates:While not strictly a bug (since the documentation does not state a particular behavior in this case) it would be fairly simple to tweak the implementation to provide the more expected behavior of returning the first match. Either way, it would be preferable if the behavior were documented.
Making the change at this point might result in some unexpected code breakage, even though the behavior is not documented. In order to ease the transition, it may be best to rename the current implementation to
reverse_binary_search
or similar, and provide the expected implementation forbinary_search
.The text was updated successfully, but these errors were encountered: