Skip to content

std: Restore conventional compareFn behavior for binarySearch#21373

Merged
andrewrk merged 1 commit intoziglang:masterfrom
jayschwa:restore-compare-param-order
Sep 16, 2024
Merged

std: Restore conventional compareFn behavior for binarySearch#21373
andrewrk merged 1 commit intoziglang:masterfrom
jayschwa:restore-compare-param-order

Conversation

@jayschwa
Copy link
Contributor

PR #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.

@andrewrk
Copy link
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.
@jayschwa jayschwa force-pushed the restore-compare-param-order branch from 87ce3d8 to b505db9 Compare September 10, 2024 17:13
@andrewrk andrewrk merged commit 812557b into ziglang:master Sep 16, 2024
@jayschwa jayschwa deleted the restore-compare-param-order branch September 16, 2024 21:06
efjimm added a commit to efjimm/zig-wcwidth that referenced this pull request Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants