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 {...@@ -1189,19 +1189,36 @@ fn fuzzTest(rng: *std.rand.Random) void {
1189 }1189 }
1190}1190}
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 {
1193 if (items.len == 0) {1193 if (items.len == 0) {
1194 return null;1194 return null;
1195 }1195 }
11961196
1197 var i: usize = 0;
1198 var smallest = items[0];1197 var smallest = items[0];
1199 for (items[1..]) |item| {1198 var smallest_index: usize = 0;
1199 for (items[1..]) |item, i| {
1200 if (lessThan(item, smallest)) {1200 if (lessThan(item, smallest)) {
1201 smallest = item;1201 smallest = item;
1202 smallest_index = i + 1;
1202 }1203 }
1203 }1204 }
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];
1205}1222}
12061223
1207test "std.sort.min" {1224test "std.sort.min" {
...@@ -1214,19 +1231,36 @@ test "std.sort.min" {...@@ -1214,19 +1231,36 @@ test "std.sort.min" {
1214 testing.expectEqual(@as(?i32, 7), min(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));1231 testing.expectEqual(@as(?i32, 7), min(i32, &[_]i32{ 6, 3, 5, 7, 6 }, desc(i32)));
1215}1232}
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 {
1218 if (items.len == 0) {1235 if (items.len == 0) {
1219 return null;1236 return null;
1220 }1237 }
12211238
1222 var i: usize = 0;
1223 var biggest = items[0];1239 var biggest = items[0];
1224 for (items[1..]) |item| {1240 var biggest_index: usize = 0;
1241 for (items[1..]) |item, i| {
1225 if (lessThan(biggest, item)) {1242 if (lessThan(biggest, item)) {
1226 biggest = item;1243 biggest = item;
1244 biggest_index = i + 1;
1227 }1245 }
1228 }1246 }
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];
1230}1264}
12311265
1232test "std.sort.max" {1266test "std.sort.max" {