Skip to content

std.mem: add indexOfMin/Max #9891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Oct 4, 2021 · 4 comments · Fixed by #9915
Closed

std.mem: add indexOfMin/Max #9891

ghost opened this issue Oct 4, 2021 · 4 comments · Fixed by #9915
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@ghost
Copy link

ghost commented Oct 4, 2021

I think the usefulness of the index is obvious.

@nektro
Copy link
Contributor

nektro commented Oct 5, 2021

can you clarify what the goal of this issue is?

@ghost
Copy link
Author

ghost commented Oct 5, 2021

std.mem has min which gives you the min val. Getting the min val is a common task but sometimes we also need the index and I didn't find an fn that does that.

@andrewrk
Copy link
Member

andrewrk commented Oct 7, 2021

This issue is missing components that would make it viable:

  • A better description of the motivation. Don't assume it is obvious. You should be able to link to some code in a zig project that had to do something worse, due to not having this feature.
  • The proposed function prototype and doc comments

@andrewrk andrewrk closed this as completed Oct 7, 2021
@ghost
Copy link

ghost commented Oct 7, 2021

The presumed purpose of this function is to find the index of the smallest/largest value in a slice. This functionality is good to have, because otherwise it is necessary to first call mem.min to get the value and then mem.indexOfScalar to get the location, which traverses the array twice. I have used these functions several times in a numerical computing context (in Julia).

In case someone wants to make a PR: #9915

const testing = @import("std").testing;

/// Returns the index of the smallest number in a slice. O(n).
/// `slice` must not be empty.
pub fn minIndex(comptime T: type, slice: []const T) usize {
    var best = slice[0];
    var index: usize = 0;
    for (slice[1..]) |item, i| {
        if (item < best) {
            best = item;
            index = i + 1;
        }
    }
    return index;
}

test "mem.minIndex" {
    try testing.expect(minIndex(u8, "abcdefg") == 0);
}

/// Returns the index of the largest number in a slice. O(n).
/// `slice` must not be empty.
pub fn maxIndex(comptime T: type, slice: []const T) usize {
    var best = slice[0];
    var index: usize = 0;
    for (slice[1..]) |item, i| {
        if (item > best) {
            best = item;
            index = i + 1;
        }
    }
    return index;
}

test "mem.maxIndex" {
    try testing.expect(maxIndex(u8, "abcdefg") == 6);
}

@andrewrk andrewrk reopened this Oct 7, 2021
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library. labels Oct 7, 2021
@andrewrk andrewrk added this to the 0.10.0 milestone Oct 7, 2021
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Oct 7, 2021
@andrewrk andrewrk modified the milestones: 0.11.0, 0.10.0 Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants