authorgravatar for garrisonhh@pm.mearrisonhh <garrisonhh@pm.me> 2024-03-05 09:11:21-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-15 16:40:51+02:00
log1ddd0691c65ebbd4888509bb51a7b2c7d32acb8f
treee96545d0b5624a0e3c838e51bcdfc30f84fcfd10
parentdf174648e4e0178d1eaea3736cd34b1928e0b05d

std.mem: use destructurable tuple for indexOfMinMax return type


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

lib/std/mem.zig+6-8
......@@ -3385,9 +3385,9 @@ test "indexOfMax" {
33853385}
33863386
33873387/// Finds the indices of the smallest and largest number in a slice. O(n).
3388/// Returns an anonymous struct with the fields `index_min` and `index_max`.
3388/// Returns the indices of the smallest and largest numbers in that order.
33893389/// `slice` must not be empty.
3390pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {
3390pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } {
33913391 assert(slice.len > 0);
33923392 var minVal = slice[0];
33933393 var maxVal = slice[0];
......@@ -3403,15 +3403,13 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {
34033403 maxIdx = i + 1;
34043404 }
34053405 }
3406 return .{ .index_min = minIdx, .index_max = maxIdx };
3406 return .{ minIdx, maxIdx };
34073407}
34083408
3409pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize };
3410
34113409test "indexOfMinMax" {
3412 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg"));
3413 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef"));
3414 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 0 }, indexOfMinMax(u8, "a"));
3410 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
3411 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
3412 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
34153413}
34163414
34173415pub fn swap(comptime T: type, a: *T, b: *T) void {