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" {...@@ -3385,9 +3385,9 @@ test "indexOfMax" {
3385}3385}
33863386
3387/// Finds the indices of the smallest and largest number in a slice. O(n).3387/// 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.
3389/// `slice` must not be empty.3389/// `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 } {
3391 assert(slice.len > 0);3391 assert(slice.len > 0);
3392 var minVal = slice[0];3392 var minVal = slice[0];
3393 var maxVal = slice[0];3393 var maxVal = slice[0];
...@@ -3403,15 +3403,13 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {...@@ -3403,15 +3403,13 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {
3403 maxIdx = i + 1;3403 maxIdx = i + 1;
3404 }3404 }
3405 }3405 }
3406 return .{ .index_min = minIdx, .index_max = maxIdx };3406 return .{ minIdx, maxIdx };
3407}3407}
34083408
3409pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize };
3410
3411test "indexOfMinMax" {3409test "indexOfMinMax" {
3412 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg"));3410 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
3413 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef"));3411 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
3414 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 0 }, indexOfMinMax(u8, "a"));3412 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
3415}3413}
34163414
3417pub fn swap(comptime T: type, a: *T, b: *T) void {3415pub fn swap(comptime T: type, a: *T, b: *T) void {