authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-03 14:42:32+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-03 21:51:03+01:00
logdb3aea3a0bfce6af04d941003c6a63e86cfdee1a
treeecadf721e422cc48bcaf4da578f0e1885efb4527
parentfd8d8afb243d5a6ceadaf38ad08b72be915fce2e

Change API for binarySearch fn


1 files changed, 17 insertions(+), 18 deletions(-)

lib/std/sort.zig+17-18
......@@ -5,7 +5,7 @@ const mem = std.mem;
55const math = std.math;
66const builtin = @import("builtin");
77
8pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T) math.Order) ?usize {
8pub fn binarySearch(comptime T: type, key: T, items: []const T, comptime compareFn: fn (lhs: T, rhs: T) math.Order) ?usize {
99 if (items.len < 1)
1010 return null;
1111
......@@ -15,11 +15,11 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T
1515 while (left <= right) {
1616 // Avoid overflowing in the midpoint calculation
1717 const mid = left + (right - left) / 2;
18 // Compare the midpoint element with the key
19 switch (compareFn(items[mid])) {
18 // Compare the key with the midpoint element
19 switch (compareFn(key, items[mid])) {
2020 .eq => return mid,
21 .lt => left = mid + 1,
22 .gt => right = mid - 1,
21 .gt => left = mid + 1,
22 .lt => right = mid - 1,
2323 }
2424 }
2525
......@@ -28,41 +28,40 @@ pub fn binarySearch(comptime T: type, items: []T, comptime compareFn: fn (val: T
2828
2929test "std.sort.binarySearch" {
3030 const S = struct {
31 fn makeComparisonPred(comptime T: type, value: T) type {
32 return struct {
33 fn pred(v: T) math.Order {
34 return math.order(v, value);
35 }
36 };
31 fn order_u32(lhs: u32, rhs: u32) math.Order {
32 return math.order(lhs, rhs);
33 }
34 fn order_i32(lhs: i32, rhs: i32) math.Order {
35 return math.order(lhs, rhs);
3736 }
3837 };
3938 testing.expectEqual(
4039 @as(?usize, null),
41 binarySearch(u32, &[_]u32{}, S.makeComparisonPred(u32, 1).pred),
40 binarySearch(u32, 1, &[_]u32{}, S.order_u32),
4241 );
4342 testing.expectEqual(
4443 @as(?usize, 0),
45 binarySearch(u32, &[_]u32{1}, S.makeComparisonPred(u32, 1).pred),
44 binarySearch(u32, 1, &[_]u32{1}, S.order_u32),
4645 );
4746 testing.expectEqual(
4847 @as(?usize, null),
49 binarySearch(u32, &[_]u32{0}, S.makeComparisonPred(u32, 1).pred),
48 binarySearch(u32, 1, &[_]u32{0}, S.order_u32),
5049 );
5150 testing.expectEqual(
5251 @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),
5453 );
5554 testing.expectEqual(
5655 @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),
5857 );
5958 testing.expectEqual(
6059 @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),
6261 );
6362 testing.expectEqual(
6463 @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),
6665 );
6766}
6867