authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2019-12-04 18:10:20+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2019-12-04 18:10:20+01:00
log0159fa284a54d25440899b6d8d84ac0a1e424a7f
tree7000b10b779b41d551e387aa56dc7356992694ba
parent65f57e44993c51ae6a582d18ad7c460ac229e68f

Make std.sort.min and std.sort.max return ?T


1 files changed, 24 insertions(+), 14 deletions(-)

lib/std/sort.zig+24-14
......@@ -1189,7 +1189,11 @@ fn fuzzTest(rng: *std.rand.Random) void {
11891189 }
11901190}
11911191
1192pub fn min(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) T {
1192pub fn min(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?T {
1193 if (items.len == 0) {
1194 return null;
1195 }
1196
11931197 var i: usize = 0;
11941198 var smallest = items[0];
11951199 for (items[1..]) |item| {
......@@ -1201,15 +1205,20 @@ pub fn min(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) boo
12011205}
12021206
12031207test "std.sort.min" {
1204 testing.expectEqual(@as(i32, 1), min(i32, &[_]i32{1}, asc(i32)));
1205 testing.expectEqual(@as(i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1206 testing.expectEqual(@as(i32, 2), min(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1207 testing.expectEqual(@as(i32, 1), min(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1208 testing.expectEqual(@as(i32, -10), min(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1209 testing.expectEqual(@as(i32, 7), min(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
1208 testing.expectEqual(@as(?i32, null), min(i32, &[_]i32{}, asc(i32)));
1209 testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{1}, asc(i32)));
1210 testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1211 testing.expectEqual(@as(?i32, 2), min(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1212 testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1213 testing.expectEqual(@as(?i32, -10), min(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1214 testing.expectEqual(@as(?i32, 7), min(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
12101215}
12111216
1212pub fn max(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) T {
1217pub fn max(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?T {
1218 if (items.len == 0) {
1219 return null;
1220 }
1221
12131222 var i: usize = 0;
12141223 var biggest = items[0];
12151224 for (items[1..]) |item| {
......@@ -1221,12 +1230,13 @@ pub fn max(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) boo
12211230}
12221231
12231232test "std.sort.max" {
1224 testing.expectEqual(@as(i32, 1), max(i32, &[_]i32{1}, asc(i32)));
1225 testing.expectEqual(@as(i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1226 testing.expectEqual(@as(i32, 9), max(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1227 testing.expectEqual(@as(i32, 1), max(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1228 testing.expectEqual(@as(i32, 10), max(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1229 testing.expectEqual(@as(i32, 3), max(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
1233 testing.expectEqual(@as(?i32, null), max(i32, &[_]i32{}, asc(i32)));
1234 testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{1}, asc(i32)));
1235 testing.expectEqual(@as(?i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1236 testing.expectEqual(@as(?i32, 9), max(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1237 testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1238 testing.expectEqual(@as(?i32, 10), max(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1239 testing.expectEqual(@as(?i32, 3), max(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
12301240}
12311241
12321242pub fn isSorted(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) bool {