| ... | @@ -5,7 +5,7 @@ const mem = std.mem; | ... | @@ -5,7 +5,7 @@ const mem = std.mem; |
| 5 | const math = std.math; | 5 | const math = std.math; |
| 6 | const builtin = @import("builtin"); | 6 | const builtin = @import("builtin"); |
| 7 | | 7 | |
| 8 | pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T) math.Order) ?usize { | 8 | pub fn binarySearch(comptime T: type, key: T, items: []const T, comptime compareFn: fn (lhs: T, rhs: T) math.Order) ?usize { |
| 9 | if (items.len < 1) | 9 | if (items.len < 1) |
| 10 | return null; | 10 | return null; |
| 11 | | 11 | |
| ... | @@ -15,11 +15,11 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T | ... | @@ -15,11 +15,11 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T |
| 15 | while (left <= right) { | 15 | while (left <= right) { |
| 16 | // Avoid overflowing in the midpoint calculation | 16 | // Avoid overflowing in the midpoint calculation |
| 17 | const mid = left + (right - left) / 2; | 17 | const mid = left + (right - left) / 2; |
| 18 | // Compare the midpoint element with the key | 18 | // Compare the key with the midpoint element |
| 19 | switch (compareFn(items[mid])) { | 19 | switch (compareFn(key, items[mid])) { |
| 20 | .eq => return mid, | 20 | .eq => return mid, |
| 21 | .lt => left = mid + 1, | 21 | .gt => left = mid + 1, |
| 22 | .gt => right = mid - 1, | 22 | .lt => right = mid - 1, |
| 23 | } | 23 | } |
| 24 | } | 24 | } |
| 25 | | 25 | |
| ... | @@ -28,41 +28,40 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T | ... | @@ -28,41 +28,40 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T |
| 28 | | 28 | |
| 29 | test "std.sort.binarySearch" { | 29 | test "std.sort.binarySearch" { |
| 30 | const S = struct { | 30 | const S = struct { |
| 31 | fn makeComparisonPred(comptime T: type, value: T) type { | 31 | fn order_u32(lhs: u32, rhs: u32) math.Order { |
| 32 | return struct { | 32 | return math.order(lhs, rhs); |
| 33 | fn pred(v: T) math.Order { | 33 | } |
| 34 | return math.order(v, value); | 34 | fn order_i32(lhs: i32, rhs: i32) math.Order { |
| 35 | } | 35 | return math.order(lhs, rhs); |
| 36 | }; | | |
| 37 | } | 36 | } |
| 38 | }; | 37 | }; |
| 39 | testing.expectEqual( | 38 | testing.expectEqual( |
| 40 | @as(?usize, null), | 39 | @as(?usize, null), |
| 41 | binarySearch(u32, &[_]u32{}, S.makeComparisonPred(u32, 1).pred), | 40 | binarySearch(u32, 1, &[_]u32{}, S.order_u32), |
| 42 | ); | 41 | ); |
| 43 | testing.expectEqual( | 42 | testing.expectEqual( |
| 44 | @as(?usize, 0), | 43 | @as(?usize, 0), |
| 45 | binarySearch(u32, &[_]u32{1}, S.makeComparisonPred(u32, 1).pred), | 44 | binarySearch(u32, 1, &[_]u32{1}, S.order_u32), |
| 46 | ); | 45 | ); |
| 47 | testing.expectEqual( | 46 | testing.expectEqual( |
| 48 | @as(?usize, null), | 47 | @as(?usize, null), |
| 49 | binarySearch(u32, &[_]u32{0}, S.makeComparisonPred(u32, 1).pred), | 48 | binarySearch(u32, 1, &[_]u32{0}, S.order_u32), |
| 50 | ); | 49 | ); |
| 51 | testing.expectEqual( | 50 | testing.expectEqual( |
| 52 | @as(?usize, 4), | 51 | @as(?usize, 4), |
| 53 | binarySearch(u32, &[_]u32{ 1, 2, 3, 4, 5 }, S.makeComparisonPred(u32, 5).pred), | 52 | binarySearch(u32, 5, &[_]u32{ 1, 2, 3, 4, 5 }, S.order_u32), |
| 54 | ); | 53 | ); |
| 55 | testing.expectEqual( | 54 | testing.expectEqual( |
| 56 | @as(?usize, 0), | 55 | @as(?usize, 0), |
| 57 | binarySearch(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, S.makeComparisonPred(u32, 2).pred), | 56 | binarySearch(u32, 2, &[_]u32{ 2, 4, 8, 16, 32, 64 }, S.order_u32), |
| 58 | ); | 57 | ); |
| 59 | testing.expectEqual( | 58 | testing.expectEqual( |
| 60 | @as(?usize, 1), | 59 | @as(?usize, 1), |
| 61 | binarySearch(i32, &[_]i32{ -7, -4, 0, 9, 10 }, S.makeComparisonPred(i32, -4).pred), | 60 | binarySearch(i32, -4, &[_]i32{ -7, -4, 0, 9, 10 }, S.order_i32), |
| 62 | ); | 61 | ); |
| 63 | testing.expectEqual( | 62 | testing.expectEqual( |
| 64 | @as(?usize, 3), | 63 | @as(?usize, 3), |
| 65 | binarySearch(i32, &[_]i32{ -100, -25, 2, 98, 99, 100 }, S.makeComparisonPred(i32, 98).pred), | 64 | binarySearch(i32, 98, &[_]i32{ -100, -25, 2, 98, 99, 100 }, S.order_i32), |
| 66 | ); | 65 | ); |
| 67 | } | 66 | } |
| 68 | | 67 | |