Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 15.5k
Add slice::sort_by_cached_key as a memoised sort_by_key#48639
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ea6a1bd670e69eb8452cc9fbee3521fde097dcfc07bdcc6f9f41a26fb430cbaca3bed09896b3881edd17785e3c3eca1e189c7b69eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -425,6 +425,14 @@ fn test_sort() { | ||
| v.sort_by(|a, b| b.cmp(a)); | ||
| assert!(v.windows(2).all(|w| w[0] >= w[1])); | ||
| // Sort in lexicographic order. | ||
| let mut v1 = orig.clone(); | ||
| let mut v2 = orig.clone(); | ||
| v1.sort_by_key(|x| x.to_string()); | ||
| v2.sort_by_cached_key(|x| x.to_string()); | ||
| assert!(v1.windows(2).all(|w| w[0].to_string() <= w[1].to_string())); | ||
| assert!(v1 == v2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! | ||
| // Sort with many pre-sorted runs. | ||
| let mut v = orig.clone(); | ||
| v.sort(); | ||
| @@ -477,24 +485,29 @@ fn test_sort_stability() { | ||
| // the second item represents which occurrence of that | ||
| // number this element is, i.e. the second elements | ||
| // will occur in sorted order. | ||
| let mut v: Vec<_> = (0..len) | ||
| let mut orig: Vec<_> = (0..len) | ||
| .map(|_| { | ||
| let n = thread_rng().gen::<usize>() % 10; | ||
| counts[n] += 1; | ||
| (n, counts[n]) | ||
| }) | ||
| .collect(); | ||
| // only sort on the first element, so an unstable sort | ||
| let mut v = orig.clone(); | ||
| // Only sort on the first element, so an unstable sort | ||
| // may mix up the counts. | ||
| v.sort_by(|&(a, _), &(b, _)| a.cmp(&b)); | ||
| // this comparison includes the count (the second item | ||
| // This comparison includes the count (the second item | ||
| // of the tuple), so elements with equal first items | ||
| // will need to be ordered with increasing | ||
| // counts... i.e. exactly asserting that this sort is | ||
| // stable. | ||
| assert!(v.windows(2).all(|w| w[0] <= w[1])); | ||
| let mut v = orig.clone(); | ||
| v.sort_by_cached_key(|&(x, _)| x); | ||
| assert!(v.windows(2).all(|w| w[0] <= w[1])); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Would be nice to be able to use
SmallVechere and save an allocation when sorting short slices.Just a suggestion, though. Maybe leave that for a future PR?
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.
That's a good idea; I might leave that for a future PR though, yeah.
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.
Rather than pulling in a full crate (which is mostly about encapsulating stuff in a single type that can be moved as a unit), you can reproduce that functionality with a couple of stack variables. Something like:
Uh oh!
There was an error while loading. Please reload this page.
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.
(Just a caveat, that's the gist of it and the array code needs to take more precautions to be safe.)
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.
@bluss Oh, do you mean panic-safe? Yes, I forgot about that, the array should be inside a
ManuallyDrop.Uh oh!
There was an error while loading. Please reload this page.
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.
And use
ptr::writeor the equivalent with a ManuallyDrop around each element, that's the two things I could spot.