| ... | @@ -6,20 +6,17 @@ const math = std.math; | ... | @@ -6,20 +6,17 @@ const math = std.math; |
| 6 | const builtin = @import("builtin"); | 6 | const builtin = @import("builtin"); |
| 7 | | 7 | |
| 8 | pub fn binarySearch(comptime T: type, key: T, items: []const T, comptime compareFn: fn (lhs: T, rhs: 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) | | |
| 10 | return null; | | |
| 11 | | | |
| 12 | var left: usize = 0; | 9 | var left: usize = 0; |
| 13 | var right: usize = items.len - 1; | 10 | var right: usize = items.len; |
| 14 | | 11 | |
| 15 | while (left <= right) { | 12 | while (left < right) { |
| 16 | // Avoid overflowing in the midpoint calculation | 13 | // Avoid overflowing in the midpoint calculation |
| 17 | const mid = left + (right - left) / 2; | 14 | const mid = left + (right - left) / 2; |
| 18 | // Compare the key with the midpoint element | 15 | // Compare the key with the midpoint element |
| 19 | switch (compareFn(key, items[mid])) { | 16 | switch (compareFn(key, items[mid])) { |
| 20 | .eq => return mid, | 17 | .eq => return mid, |
| 21 | .gt => left = mid + 1, | 18 | .gt => left = mid + 1, |
| 22 | .lt => right = mid - 1, | 19 | .lt => right = mid, |
| 23 | } | 20 | } |
| 24 | } | 21 | } |
| 25 | | 22 | |
| ... | @@ -47,6 +44,10 @@ test "std.sort.binarySearch" { | ... | @@ -47,6 +44,10 @@ test "std.sort.binarySearch" { |
| 47 | @as(?usize, null), | 44 | @as(?usize, null), |
| 48 | binarySearch(u32, 1, &[_]u32{0}, S.order_u32), | 45 | binarySearch(u32, 1, &[_]u32{0}, S.order_u32), |
| 49 | ); | 46 | ); |
| | 47 | testing.expectEqual( |
| | 48 | @as(?usize, null), |
| | 49 | binarySearch(u32, 0, &[_]u32{1}, S.order_u32), |
| | 50 | ); |
| 50 | testing.expectEqual( | 51 | testing.expectEqual( |
| 51 | @as(?usize, 4), | 52 | @as(?usize, 4), |
| 52 | binarySearch(u32, 5, &[_]u32{ 1, 2, 3, 4, 5 }, S.order_u32), | 53 | binarySearch(u32, 5, &[_]u32{ 1, 2, 3, 4, 5 }, S.order_u32), |