From 1ddd0691c65ebbd4888509bb51a7b2c7d32acb8f Mon Sep 17 00:00:00 2001 From: garrisonhh Date: Tue, 5 Mar 2024 09:11:21 -0500 Subject: [PATCH] std.mem: use destructurable tuple for indexOfMinMax return type --- lib/std/mem.zig | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index b3e3cce593..ffb202fc8a 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -3385,9 +3385,9 @@ test "indexOfMax" { } /// Finds the indices of the smallest and largest number in a slice. O(n). -/// Returns an anonymous struct with the fields `index_min` and `index_max`. +/// Returns the indices of the smallest and largest numbers in that order. /// `slice` must not be empty. -pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult { +pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } { assert(slice.len > 0); var minVal = slice[0]; var maxVal = slice[0]; @@ -3403,15 +3403,13 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult { maxIdx = i + 1; } } - return .{ .index_min = minIdx, .index_max = maxIdx }; + return .{ minIdx, maxIdx }; } -pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize }; - test "indexOfMinMax" { - try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg")); - try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef")); - try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 0 }, indexOfMinMax(u8, "a")); + try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg")); + try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef")); + try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a")); } pub fn swap(comptime T: type, a: *T, b: *T) void {