| ... | @@ -3385,9 +3385,9 @@ test "indexOfMax" { | ... | @@ -3385,9 +3385,9 @@ test "indexOfMax" { |
| 3385 | } | 3385 | } |
| 3386 | | 3386 | |
| 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. |
| 3390 | pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult { | 3390 | pub 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 | } |
| 3408 | | 3408 | |
| 3409 | pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize }; | | |
| 3410 | | | |
| 3411 | test "indexOfMinMax" { | 3409 | test "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 | } |
| 3416 | | 3414 | |
| 3417 | pub fn swap(comptime T: type, a: *T, b: *T) void { | 3415 | pub fn swap(comptime T: type, a: *T, b: *T) void { |