std: Restore conventional compareFn behavior for binarySearch#21373
Merged
andrewrk merged 1 commit intoziglang:masterfrom Sep 16, 2024
Merged
std: Restore conventional compareFn behavior for binarySearch#21373andrewrk merged 1 commit intoziglang:masterfrom
compareFn behavior for binarySearch#21373andrewrk merged 1 commit intoziglang:masterfrom
Conversation
Member
|
Thank you. This really tripped me up when I rebased my branch on #20927. |
PR ziglang#20927 made some improvements to the `binarySearch` API, but one change I found surprising was the relationship between the left-hand and right-hand parameters of `compareFn` was inverted. This is different from how comparison functions typically behave, both in other parts of Zig (e.g. `std.math.order`) and in other languages (e.g. C's `bsearch`). Unless a strong reason can be identified and documented for doing otherwise, I think it'll be better to stick with convention. While writing this patch and changing things back to the way they were, the predicates of `lowerBound` and `upperBound` seemed to be the only areas that benefited from the inversion. I don't think that benefit is worth the cost, personally. Calling `Order.invert()` in the predicates accomplishes the same goal.
87ce3d8 to
b505db9
Compare
efjimm
added a commit
to efjimm/zig-wcwidth
that referenced
this pull request
Dec 20, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR #20927 made some improvements to the
binarySearchAPI, but one change I found surprising was the relationship between the left-hand and right-hand parameters ofcompareFnwas inverted. This is different from how comparison functions typically behave, both in other parts of Zig (e.g.std.math.order) and in other languages (e.g. C'sbsearch). Unless a strong reason can be identified and documented for doing otherwise, I think it'll be better to stick with convention.While writing this patch and changing things back to the way they were, the predicates of
lowerBoundandupperBoundseemed to be the only areas that benefited from the inversion. I don't think that benefit is worth the cost, personally. CallingOrder.invert()in the predicates accomplishes the same goal.