std: Restore conventional compareFn behavior for binarySearch
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.
This commit is contained in:
committed by
Andrew Kelley
parent
7caa3d9da7
commit
812557bfde
@@ -1624,12 +1624,12 @@ pub fn unwindFrameDwarf(
|
||||
} else {
|
||||
const index = std.sort.binarySearch(Dwarf.FrameDescriptionEntry, di.fde_list.items, context.pc, struct {
|
||||
pub fn compareFn(pc: usize, item: Dwarf.FrameDescriptionEntry) std.math.Order {
|
||||
if (pc < item.pc_begin) return .gt;
|
||||
if (pc < item.pc_begin) return .lt;
|
||||
|
||||
const range_end = item.pc_begin + item.pc_range;
|
||||
if (pc < range_end) return .eq;
|
||||
|
||||
return .lt;
|
||||
return .gt;
|
||||
}
|
||||
}.compareFn);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user