-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.mem: add indexOfMin and indexOfMax #9915
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
Conversation
var best = slice[0]; | ||
var index: usize = 0; | ||
for (slice[1..]) |item, i| { | ||
if (item < best) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comparison predicate should be specified by the user to allow for extra flexibility and custom non-scalar types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about that. The precedent from std.math.min
and std.mem.min
is to only support scalar types. A custom predicate would be good to have, but maybe open an issue first?
Thanks for the review, @LemonBoy. I've added the assert and more tests to the new functions, as well as to |
Drone failure unrelated? |
Hold on, there needs to be a |
In principle, yes. But I'm not sure what the function signature would be. There doesn't seem to be an |
How about something explicit like |
Added combined min-max functions in a separate commit. |
There is already an equivalent of the minIndex and maxIndex functions in std.sort: Lines 1413 to 1418 in ee98d87
Lines 1361 to 1366 in ee98d87
|
Interesting. I wouldn't quite call it an equivalent, as these functions take an explicit comparator and corresponding context -- which I think is too verbose for general use. But if it's felt that |
Going to close and reopen to kick off CI |
Drone failure looks unrelated, but I'm not quite sure. Has it been seen before? Something about missing references to "ceill" and "florl". |
For finding the minimum and maximum values (and indices) in a slice in a single pass.
Find the index of the smallest/largest number in a slice, without traversing it twice.
closes #9891