authorgravatar for mpopov@fastmail.fmMikhail Popov <mpopov@fastmail.fm> 2021-10-08 00:37:50+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-27 19:58:27+02:00
logdddbd2f511f505ad4018ba007f065ea2b4ec8f79
tree9de4573808d5f509afd541dc4c7919af3b7fe8e4
parent6d1b1374f722f2b6f6f91e181e831238e3c8ce00

std.mem: Add functions to find the index of the mimimum/maximum value in a slice.


1 files changed, 50 insertions(+), 2 deletions(-)

lib/std/mem.zig+50-2
...@@ -2143,6 +2143,7 @@ fn testWriteIntImpl() !void {...@@ -2143,6 +2143,7 @@ fn testWriteIntImpl() !void {
2143/// Returns the smallest number in a slice. O(n).2143/// Returns the smallest number in a slice. O(n).
2144/// `slice` must not be empty.2144/// `slice` must not be empty.
2145pub fn min(comptime T: type, slice: []const T) T {2145pub fn min(comptime T: type, slice: []const T) T {
2146 assert(slice.len > 0);
2146 var best = slice[0];2147 var best = slice[0];
2147 for (slice[1..]) |item| {2148 for (slice[1..]) |item| {
2148 best = math.min(best, item);2149 best = math.min(best, item);
...@@ -2151,12 +2152,15 @@ pub fn min(comptime T: type, slice: []const T) T {...@@ -2151,12 +2152,15 @@ pub fn min(comptime T: type, slice: []const T) T {
2151}2152}
21522153
2153test "mem.min" {2154test "mem.min" {
2154 try testing.expect(min(u8, "abcdefg") == 'a');2155 try testing.expectEqual(min(u8, "abcdefg"), 'a');
2156 try testing.expectEqual(min(u8, "bcdefga"), 'a');
2157 try testing.expectEqual(min(u8, "a"), 'a');
2155}2158}
21562159
2157/// Returns the largest number in a slice. O(n).2160/// Returns the largest number in a slice. O(n).
2158/// `slice` must not be empty.2161/// `slice` must not be empty.
2159pub fn max(comptime T: type, slice: []const T) T {2162pub fn max(comptime T: type, slice: []const T) T {
2163 assert(slice.len > 0);
2160 var best = slice[0];2164 var best = slice[0];
2161 for (slice[1..]) |item| {2165 for (slice[1..]) |item| {
2162 best = math.max(best, item);2166 best = math.max(best, item);
...@@ -2165,7 +2169,51 @@ pub fn max(comptime T: type, slice: []const T) T {...@@ -2165,7 +2169,51 @@ pub fn max(comptime T: type, slice: []const T) T {
2165}2169}
21662170
2167test "mem.max" {2171test "mem.max" {
2168 try testing.expect(max(u8, "abcdefg") == 'g');2172 try testing.expectEqual(max(u8, "abcdefg"), 'g');
2173 try testing.expectEqual(max(u8, "gabcdef"), 'g');
2174 try testing.expectEqual(max(u8, "g"), 'g');
2175}
2176
2177/// Returns the index of the smallest number in a slice. O(n).
2178/// `slice` must not be empty.
2179pub fn indexOfMin(comptime T: type, slice: []const T) usize {
2180 assert(slice.len > 0);
2181 var best = slice[0];
2182 var index: usize = 0;
2183 for (slice[1..]) |item, i| {
2184 if (item < best) {
2185 best = item;
2186 index = i + 1;
2187 }
2188 }
2189 return index;
2190}
2191
2192test "mem.indexOfMin" {
2193 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);
2194 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);
2195 try testing.expectEqual(indexOfMin(u8, "a"), 0);
2196}
2197
2198/// Returns the index of the largest number in a slice. O(n).
2199/// `slice` must not be empty.
2200pub fn indexOfMax(comptime T: type, slice: []const T) usize {
2201 assert(slice.len > 0);
2202 var best = slice[0];
2203 var index: usize = 0;
2204 for (slice[1..]) |item, i| {
2205 if (item > best) {
2206 best = item;
2207 index = i + 1;
2208 }
2209 }
2210 return index;
2211}
2212
2213test "mem.indexOfMax" {
2214 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);
2215 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);
2216 try testing.expectEqual(indexOfMax(u8, "a"), 0);
2169}2217}
21702218
2171pub fn swap(comptime T: type, a: *T, b: *T) void {2219pub fn swap(comptime T: type, a: *T, b: *T) void {