-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add 'partition_at_index/_by/_by_key' for slices. #55448
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @bluss (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Ping from triage @bluss / @rust-lang/libs: This PR requires your review. |
@Mokosha, can you say more about why this is useful? What’s an example of a situation where you’d use this? @rust-lang/libs, thoughts on having this feature in the standard library? It’s apparently already available on crates.io rust-lang/rfcs#1470 (comment) |
@SimonSapin I have two separate use-cases in my own projects, both involving selecting the median of a dataset.
In the former I implemented quickselect myself because I couldn't find any crate implementing quickselect or any other kth-element algorithm when I wrote the code back in 2016. I didn't discover That's a major issue with saying "just use something from crates.io", sometimes people don't know what they're actually looking for or what keywords to search to find it. This is a fundamental enough operation that it should reside in the stdlib, where people are most likely to look first for solutions. In the latter because the dataset is a small, constant size and I needed to copy it to preserve the original ordering (potentially not necessary, I already see a possible optimization), I decided to just use the stdlib's sort and index the set directly. In both cases I would prefer a stdlib-provided implementation if it was available. In both cases it would probably be more optimal than the solutions I ended up going with, and importing an external crate just for one function at one use site in each case seemed like overkill (though in |
This seems reasonable to me to add, pending bikeshedding the name/returns/etc |
@Mokosha It looks like there's been a few review comments, can you take a look at those? |
Yes. My apologies. I will take a look at these over the weekend. |
I haven't tested the changes, as I'm running into an issue building |
All tests pass. :) Waiting for additional feedback. |
@Mokosha they'll want the commits to be squashed before they merge. I don't have review permissions. |
Is this something I'm in charge of? (Do I create a new PR that squashes the commits?) I thought that was an option when doing the merge... |
Merges aren't done directly through Github, I don't think bors will squash. You can do an interactive rebase against the parent commit of your first one, mark all subsequent commits as "fixup" and then force-push to your branch when the rebase is complete. The change in commits will be immediately reflected on the PR. |
This seems good! The name doesn't feel intuitive to me (but not the operation either, so maybe I haven't used it anywhere). Name was discussed in rust-lang/rfcs/issues/1470. I think I understand this operation better as |
I don't know which name is the most intuitive, honestly. The reason I didn't choose anything with |
Perhaps |
@Mark-Simulacrum It doesn't quite sort the first N elements. Rather, it chooses an index
|
Ah, okay. Then I think the partition naming is probably the best that I can think of -- and seems to jive okay with the other partition method on slices (https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.partition_dedup). If we do rename to the |
OK -- good point about consistency. How about |
Would there be anything lost by using a fixed seed instead? IMO non-deterministic tests are an antipattern anyway. If you use something like |
@RalfJung Probably not, but I'm matching the code already written in the test for |
IMO they should, but that's up to @rust-lang/libs |
Ping from triage, @scottmcm |
@bors r=bluss |
📌 Commit 3f306db has been approved by |
⌛ Testing commit 3f306db with merge 8be24b37d498dbf7b85aef82b1007522a4b73cbc... |
💥 Test timed out |
@bors retry Double scheduling in two Travis builds. |
Add 'partition_at_index/_by/_by_key' for slices. This is an analog to C++'s std::nth_element (a.k.a. quickselect). Corresponds to tracking bug rust-lang#55300.
Add 'partition_at_index/_by/_by_key' for slices. This is an analog to C++'s std::nth_element (a.k.a. quickselect). Corresponds to tracking bug rust-lang#55300.
Thank you to everyone who helped review this change and get it merged! |
This is an analog to C++'s std::nth_element (a.k.a. quickselect).
Corresponds to tracking bug #55300.