| ... | ... | @@ -2143,6 +2143,7 @@ fn testWriteIntImpl() !void { |
| 2143 | 2143 | /// Returns the smallest number in a slice. O(n). |
| 2144 | 2144 | /// `slice` must not be empty. |
| 2145 | 2145 | pub fn min(comptime T: type, slice: []const T) T { |
| 2146 | assert(slice.len > 0); |
| 2146 | 2147 | var best = slice[0]; |
| 2147 | 2148 | for (slice[1..]) |item| { |
| 2148 | 2149 | best = math.min(best, item); |
| ... | ... | @@ -2151,12 +2152,15 @@ pub fn min(comptime T: type, slice: []const T) T { |
| 2151 | 2152 | } |
| 2152 | 2153 | |
| 2153 | 2154 | test "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 | } |
| 2156 | 2159 | |
| 2157 | 2160 | /// Returns the largest number in a slice. O(n). |
| 2158 | 2161 | /// `slice` must not be empty. |
| 2159 | 2162 | pub fn max(comptime T: type, slice: []const T) T { |
| 2163 | assert(slice.len > 0); |
| 2160 | 2164 | var best = slice[0]; |
| 2161 | 2165 | for (slice[1..]) |item| { |
| 2162 | 2166 | best = math.max(best, item); |
| ... | ... | @@ -2165,7 +2169,51 @@ pub fn max(comptime T: type, slice: []const T) T { |
| 2165 | 2169 | } |
| 2166 | 2170 | |
| 2167 | 2171 | test "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. |
| 2179 | pub 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 | |
| 2192 | test "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. |
| 2200 | pub 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 | |
| 2213 | test "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 | } |
| 2170 | 2218 | |
| 2171 | 2219 | pub fn swap(comptime T: type, a: *T, b: *T) void { |