authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2019-12-04 18:20:55+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2019-12-04 18:20:55+01:00
log841a37ab597d3075c1cb15dc5280e04b48eafa27
tree7a7f80244150391fbcc6deb45eed3af3fd6b07d4
parent0159fa284a54d25440899b6d8d84ac0a1e424a7f

Add std.sort.argMax and std.sort.argMin


1 files changed, 42 insertions(+), 8 deletions(-)

lib/std/sort.zig+42-8
......@@ -1189,19 +1189,36 @@ 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 argMin(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?usize {
11931193 if (items.len == 0) {
11941194 return null;
11951195 }
11961196
1197 var i: usize = 0;
11981197 var smallest = items[0];
1199 for (items[1..]) |item| {
1198 var smallest_index: usize = 0;
1199 for (items[1..]) |item, i| {
12001200 if (lessThan(item, smallest)) {
12011201 smallest = item;
1202 smallest_index = i + 1;
12021203 }
12031204 }
1204 return smallest;
1205
1206 return smallest_index;
1207}
1208
1209test "std.sort.argMin" {
1210 testing.expectEqual(@as(?usize, null), argMin(i32, &[_]i32{}, asc(i32)));
1211 testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{1}, asc(i32)));
1212 testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1213 testing.expectEqual(@as(?usize, 3), argMin(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1214 testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1215 testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1216 testing.expectEqual(@as(?usize, 3), argMin(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
1217}
1218
1219pub fn min(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?T {
1220 const i = argMin(T, items, lessThan) orelse return null;
1221 return items[i];
12051222}
12061223
12071224test "std.sort.min" {
......@@ -1214,19 +1231,36 @@ test "std.sort.min" {
12141231 testing.expectEqual(@as(?i32, 7), min(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
12151232}
12161233
1217pub fn max(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?T {
1234pub fn argMax(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?usize {
12181235 if (items.len == 0) {
12191236 return null;
12201237 }
12211238
1222 var i: usize = 0;
12231239 var biggest = items[0];
1224 for (items[1..]) |item| {
1240 var biggest_index: usize = 0;
1241 for (items[1..]) |item, i| {
12251242 if (lessThan(biggest, item)) {
12261243 biggest = item;
1244 biggest_index = i + 1;
12271245 }
12281246 }
1229 return biggest;
1247
1248 return biggest_index;
1249}
1250
1251test "std.sort.argMax" {
1252 testing.expectEqual(@as(?usize, null), argMax(i32, &[_]i32{}, asc(i32)));
1253 testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{1}, asc(i32)));
1254 testing.expectEqual(@as(?usize, 4), argMax(i32, &[_]i32{ 1, 2, 3, 4, 5 }, asc(i32)));
1255 testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{ 9, 3, 8, 2, 5 }, asc(i32)));
1256 testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{ 1, 1, 1, 1, 1 }, asc(i32)));
1257 testing.expectEqual(@as(?usize, 2), argMax(i32, &[_]i32{ -10, 1, 10 }, asc(i32)));
1258 testing.expectEqual(@as(?usize, 1), argMax(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
1259}
1260
1261pub fn max(comptime T: type, items: []const T, lessThan: fn (lhs: T, rhs: T) bool) ?T {
1262 const i = argMax(T, items, lessThan) orelse return null;
1263 return items[i];
12301264}
12311265
12321266test "std.sort.max" {