Crates.io | drain_at_sorted_unchecked |
lib.rs | drain_at_sorted_unchecked |
version | 0.1.0 |
source | src |
created_at | 2023-07-22 04:56:03.874625 |
updated_at | 2023-07-22 04:56:03.874625 |
description | Terribly unsafe but highly efficient function that allows removing items from a vector with few moves. |
homepage | https://github.com/JohnScience/drain_at_sorted_unchecked |
repository | https://github.com/JohnScience/drain_at_sorted_unchecked |
max_upload_size | |
id | 922975 |
size | 28,183 |
Terribly unsafe but highly efficient function that allows removing items from a vector with few moves.
use drain_at_sorted_unchecked::drain_at_sorted_unchecked;
fn main() {
let mut v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
// Safety:
//
// [x] The indices are sorted in ascending order.
// [x] The indices are within bounds of the vector.
// [x] The indices are unique.
// [x] Items of type i32 are trivially movable.
unsafe { drain_at_sorted_unchecked(&mut v, [2,4,6]); }
assert_eq!(v, [0, 1, 3, 5, 7, 8]);
}
At the moment of writing the algorithm is implemented only for a vector because that's what the author needed. Extending the algorithm to other contiguous collections (e.g. heapless::Vec
or arrayvec::ArrayVec
) should be straightforward.
The library is quite heavily tested but there's still a slim chance that there are some bugs. Please report them if you find any.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.