-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Implement partial_sort_unstable for slice #149318
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
Implement partial_sort_unstable for slice #149318
Conversation
This comment has been minimized.
This comment has been minimized.
7af04ad to
115ac5c
Compare
This comment has been minimized.
This comment has been minimized.
115ac5c to
0e87d5d
Compare
|
cc @orlp |
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.
Left some remarks, some on style, but also some with substance.
Besides comments on the code that's written, I do note a lack of tests?
Doc tests cover most branches. I don't find a dedicated file to cover its cousin |
|
The examples can change at any time. And you didn't test, for example, the post-condition that all elements |
Thanks and yes. Do you know where the unit tests of |
|
I believe the bulk is found in https://github.com/rust-lang/rust/blob/main/library/alloctests/tests/sort/tests.rs. |
|
What I suggested in the ACP was a sketch implementation, I did some more thinking and I think the following handles all corner cases nicely: pub fn partial_sort<T, F, R>(mut v: &mut [T], range: R, is_less: &mut F)
where
F: FnMut(&T, &T) -> bool,
R: RangeBounds<usize>,
{
let len = v.len();
let Range { start, end } = slice::range(range, ..len);
if end - start <= 1 {
// Can be resolved in at most a single partition_at_index call, without
// further sorting. Do nothing if it is an empty range at start or end.
if start != len && end != 0 {
sort::select::partition_at_index(v, start, is_less);
}
return;
}
// Don't bother reducing the slice to sort if it eliminates fewer than 8 elements.
if end + 8 <= len {
v = sort::select::partition_at_index(v, end - 1, is_less).0;
}
if start >= 8 {
v = sort::select::partition_at_index(v, start, is_less).2;
}
sort::unstable::sort(v, is_less);
}And to formalize the post-conditions, I think the following should hold after a call to for i in 0..b {
for j in b..n {
assert!(v[i] <= v[j]);
}
}
for i in 0..e {
for j in e..n {
assert!(v[i] <= v[j]);
}
}
for i in b..e {
for j in i..e {
assert!(v[i] <= v[j]);
}
} |
A lot of those individual comparisons are implied by transitivity of the ordering, so it can be reduced to choosing the maximum of the prefix (if any), the minimum of the suffix (if any), and then asserting that the concatenation is sorted. Informally, let max_before = v[..b].iter().max().into_iter();
let sorted_range = v[b..e].iter();
let min_after = v[e..].iter().min().into_iter();
let seq = max_before.chain(sorted_range).chain(min_after);
assert!(seq.is_sorted());That's pretty much what you said in rust-lang/libs-team#685 (comment) , just using transitivity of the comparison. Without assuming that, the implementation couldn't guarantee the universally quantified property anyway. |
f9a09e0 to
372589e
Compare
This comment has been minimized.
This comment has been minimized.
|
Pushed a new implementation. I'm writing tests but perhaps we'd have a new mod under |
cc @Amanieu for early review for the direction and advice on where to organize the tests. |
This comment has been minimized.
This comment has been minimized.
6ef6ab4 to
10d053f
Compare
This comment has been minimized.
This comment has been minimized.
10d053f to
43fc006
Compare
…oss35 Implement partial_sort_unstable for slice This refers to rust-lang#149046.
|
@bors r- |
|
Commit 45e0fbf has been unapproved. |
Do you have a more concrete analysis? This PR adds new code that should not be used anywhere else so far. |
|
not really, besides "the test no longer failed when I excluded it from the rollup" unfortunately |
|
@bors try jobs=aarch64-gnu-llvm-20-1 |
This comment has been minimized.
This comment has been minimized.
Implement partial_sort_unstable for slice try-job: aarch64-gnu-llvm-20-1
|
I don't think there's any reason this should have caused a failure there, it's completely unrelated. What a bizarre issue though, which test actually failed? It didn't print that info so it's like the test runner itself crashed. |
|
...well, one of the PRs in that rollup actually did change the test runner #150131. I'll start a job there to check |
|
Huh, neither this one nor that one then. That's a scary issue to have if it's spurious... @bors r+ |
…oss35 Implement partial_sort_unstable for slice This refers to rust-lang#149046.
Rollup of 9 pull requests Successful merges: - #149318 (Implement partial_sort_unstable for slice) - #150805 (Fix ICE in inline always warning emission.) - #150822 (Fix for ICE: eii: fn / macro rules None in find_attr()) - #150853 (std: sys: fs: uefi: Implement File::read) - #150855 (std: sys: fs: uefi: Implement File::tell) - #150881 (Fix std::fs::copy on WASI by setting proper OpenOptions flags) - #150891 (Fix a trivial typo in def_id.rs) - #150892 (Don't check `[mentions]` paths in submodules from tidy) - #150894 (cg_llvm: add a pause to make comment less confusing) r? @ghost
|
It looks like the newly-added tests in alloctests are taking an additional 1.5 hours to run under Miri (!), which has exploded CI times to 4 hours. I have a proposed fix at #150947. |
This refers to #149046.