diff options
| author | Pietro Albini <pietro@pietroalbini.org> | 2018-09-25 22:34:38 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-09-25 22:34:38 +0200 |
| commit | b940e1d961b274eb036e3155c6152705ac0d2227 (patch) | |
| tree | ed095f9d1178b1903dce354094cfd3174edaa553 /src/rustllvm/RustWrapper.cpp | |
| parent | 707c9795ac00bdc487f79f048618b4119f70ba0d (diff) | |
| parent | d560292a87a89587e0345e13b9714c90495ea50f (diff) | |
| download | rust-b940e1d961b274eb036e3155c6152705ac0d2227.tar.gz rust-b940e1d961b274eb036e3155c6152705ac0d2227.zip | |
Rollup merge of #54058 - Kerollmops:slice-dedup, r=shepmaster
Introduce the partition_dedup/by/by_key methods for slices
This PR propose to add three methods to the slice type, the `partition_dedup`, `partition_dedup_by` and `partition_dedup_by_key`. The two other methods are based on `slice::partition_dedup_by`.
These methods take a mutable slice, deduplicates it and moves all duplicates to the end of it, returning two mutable slices, the first containing the deduplicated elements and the second all the duplicates unordered.
```rust
let mut slice = [1, 2, 2, 3, 3, 2];
let (dedup, duplicates) = slice.partition_dedup();
assert_eq!(dedup, [1, 2, 3, 2]);
assert_eq!(duplicates, [3, 2]);
```
The benefits of adding these methods is that it is now possible to:
- deduplicate a slice without having to allocate and possibly clone elements on the heap, really useful for embedded stuff that can't allocate for example.
- not loose duplicate elements, because, when using `Vec::dedup`, duplicates elements are dropped. These methods add more flexibillity to the user.
Note that this is near a copy/paste of the `Vec::dedup_by` function, once this method is stable the goal is to replace the algorithm in `Vec` by the following.
```rust
pub fn Vec::dedup_by<F>(&mut self, same_bucket: F)
where F: FnMut(&mut T, &mut T) -> bool
{
let (dedup, _) = self.as_mut_slice().partition_dedup_by(same_bucket);
let len = dedup.len();
self.truncate(len);
}
```
Diffstat (limited to 'src/rustllvm/RustWrapper.cpp')
0 files changed, 0 insertions, 0 deletions
