Commit 1ddd0691c6

garrisonhh <garrisonhh@pm.me>
2024-03-05 15:11:21
std.mem: use destructurable tuple for indexOfMinMax return type
1 parent df17464
Changed files (1)
lib
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 {