summaryrefslogtreecommitdiff
path: root/util/mem.zig
blob: 728db44c3d3d2115e7b2a62358130d9e3d795ebf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const std = @import("std");

const math = std.math;
const testing_allocator = std.testing.allocator;

/// Returns the position of the smallest number in a slice.
pub fn min_idx(comptime T: type, slice: []const T) usize {
    var best = slice[0];
    var idx: usize = 0;

    for (slice[1..]) |item, i| {
        const possible_best = math.min(best, item);
        if (best > possible_best) {
            best = possible_best;
            idx = i + 1;
        }
    }
    return idx;
}