mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.mem: use destructurable tuple for indexOfMinMax return type
This commit is contained in:
parent
df174648e4
commit
1ddd0691c6
1 changed files with 6 additions and 8 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue