Skip to content
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 examples to document the return type of quickselect functions #121187

Merged

Conversation

Takashiidobe
Copy link
Contributor

Currently, select_nth_unstable, select_nth_unstable_by, and select_nth_unstable_by_key's examples do not show how to use the return values of the functions in an example, so this PR adds that in.

Note: I didn't know what to call the parameters, so I settled on lesser, median, greater because the example is used for median finding so I retained that naming for the pivot, but lesser and greater are poor names for the example that sorts in descending order, because lesser and greater are then flipped.

I think it's common to say "lo" and "hi" for low and high respectively, but that's also not great when the comparator flips the elements. Otherwise, "left" and "right" are also commonly used but I think that's poor naming because some languages read right to left so those names are also unintuitive.

Lesser and greater are also not that great but I found a test that used less, equal, greater so I took that:

v.select_nth_unstable_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());

…elect_nth_unstable_by`, and `select_nth_unstable_by_key`.
@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 16, 2024
@Noratrieb
Copy link
Member

I really like the names for the "correctly-ordered" tests, and agree that they're suboptimal for the reverse test. But I think it's fine to keep them, as I also cannot come up with better names or a better comparison algorithm. Ordering uses Less too, so this test is just intrinsically a bit confusing.
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 16, 2024

📌 Commit b49bd0b has been approved by Nilstrieb

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 16, 2024
Nadrieril added a commit to Nadrieril/rust that referenced this pull request Feb 17, 2024
…quickselect, r=Nilstrieb

Add examples to document the return type of quickselect functions

Currently, `select_nth_unstable`, `select_nth_unstable_by`, and `select_nth_unstable_by_key`'s examples do not show how to use the return values of the functions in an example, so this PR adds that in.

Note: I didn't know what to call the parameters, so I settled on lesser, median, greater because the example is used for median finding so I retained that naming for the pivot, but lesser and greater are poor names for the example that sorts in descending order, because lesser and greater are then flipped.

I think it's common to say "lo" and "hi" for low and high respectively, but that's also not great when the comparator flips the elements. Otherwise, "left" and "right" are also commonly used but I think that's poor naming because some languages read right to left so those names are also unintuitive.

Lesser and greater are also not that great but I found a test that used `less`, `equal`, `greater` so I took that: https://github.com/rust-lang/rust/blob/dfa88b328f969871d12dba3b2c0257ab3ea6703a/library/core/tests/slice.rs#L1962
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#119032 (Use a hardcoded constant instead of calling OpenProcessToken.)
 - rust-lang#120932 (const_mut_refs: allow mutable pointers to statics)
 - rust-lang#121059 (Add and use a simple extension trait derive macro in the compiler)
 - rust-lang#121135 (coverage: Discard spans that fill the entire function body)
 - rust-lang#121187 (Add examples to document the return type of quickselect functions)
 - rust-lang#121191 (Add myself to review rotation (and a rustbot ping))
 - rust-lang#121192 (Give some intrinsics fallback bodies)
 - rust-lang#121197 (Ensure `./configure` works when `configure.py` path contains spaces)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f3d9abc into rust-lang:master Feb 17, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Feb 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
Rollup merge of rust-lang#121187 - Takashiidobe:takashi/examples-for-quickselect, r=Nilstrieb

Add examples to document the return type of quickselect functions

Currently, `select_nth_unstable`, `select_nth_unstable_by`, and `select_nth_unstable_by_key`'s examples do not show how to use the return values of the functions in an example, so this PR adds that in.

Note: I didn't know what to call the parameters, so I settled on lesser, median, greater because the example is used for median finding so I retained that naming for the pivot, but lesser and greater are poor names for the example that sorts in descending order, because lesser and greater are then flipped.

I think it's common to say "lo" and "hi" for low and high respectively, but that's also not great when the comparator flips the elements. Otherwise, "left" and "right" are also commonly used but I think that's poor naming because some languages read right to left so those names are also unintuitive.

Lesser and greater are also not that great but I found a test that used `less`, `equal`, `greater` so I took that: https://github.com/rust-lang/rust/blob/dfa88b328f969871d12dba3b2c0257ab3ea6703a/library/core/tests/slice.rs#L1962
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants