Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/llogiq/partition
partition slices in-place by a predicate
https://github.com/llogiq/partition
Last synced: about 2 months ago
JSON representation
partition slices in-place by a predicate
- Host: GitHub
- URL: https://github.com/llogiq/partition
- Owner: llogiq
- License: apache-2.0
- Created: 2017-02-03T18:37:09.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-09-17T22:21:12.000Z (over 3 years ago)
- Last Synced: 2024-10-13T13:44:27.958Z (2 months ago)
- Language: Rust
- Size: 13.7 KB
- Stars: 17
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.Apache2
Awesome Lists containing this project
README
# partition
partition slices in-place by a predicate
[![Build Status](https://travis-ci.org/llogiq/partition.svg)](https://travis-ci.org/llogiq/partition)
[![Current Version](https://img.shields.io/crates/v/partition.svg)](https://crates.io/crates/partition)
[![License: Apache 2.0/MIT](https://img.shields.io/crates/l/partition.svg)](#license)Rust has the `Iterator::partition(_)` method, but this requires allocating two
`Vec`s to hold the values. This has the benefit of preserving order, at the
cost of spending time and RAM to do the allocation, which also makes this API
unavailable on systems without an allocator.This crate has a `partition(..)` function to partition a mutable slice of
values by a predicate. It will swap around values to separate those the
predicate holds for and those it doesn't and return the two mutable sub-slices.This crate also provides `partition_index(..)` which operates similarly, but
instead returns the index of the first element to evaluate to false. All
elements before this index evaluate to true, and all elements after evaluate
to false.### Warning
Note that since partition works by swapping values, the order of elements within
the slice will not be preserved.## Example
```Rust
let mut even_odd = [0u8, 1, 2, 3, 4, 5, 6];
let (even, odd) = partition(&mut even_odd, |x| x & 1 == 0);
``````Rust
let mut less_than_4 = [0u8, 3, 6, 2, 1, 5, 4];
let idx = partition_index(&mut less_than_4, |x| x < 4);
```## Performance
On a Core m3-6y30 with 4GB of RAM, I get the following benchmark results
(in ns/iter):|Number of Elements|`partition(&[T], _)`|`Iter::partition(_)`|
|------------------|--------------------|--------------------|
|10000 | 8,738 ± 665 | 101,625 ± 10,930 |
|1000 | 896 ± 75 | 6,826 ± 760 |
|100 | 118 ± 11 | 1,013 ± 116 |
|10 | 14 ± 2 | 295 ± 93 |
|1 | 4 ± 1 | 51 ± 7 |So it's safe to say performance compares very favorably.
## License
Licensed under either of at your discretion:
- [Apache 2.0](LICENSE.Apache2)
- [MIT](LICENSE.MIT)