drain_at_sorted_unchecked

Crates.iodrain_at_sorted_unchecked
lib.rsdrain_at_sorted_unchecked
version0.1.0
sourcesrc
created_at2023-07-22 04:56:03.874625
updated_at2023-07-22 04:56:03.874625
descriptionTerribly unsafe but highly efficient function that allows removing items from a vector with few moves.
homepagehttps://github.com/JohnScience/drain_at_sorted_unchecked
repositoryhttps://github.com/JohnScience/drain_at_sorted_unchecked
max_upload_size
id922975
size28,183
Dmitrii - Demenev (JohnScience)

documentation

https://docs.rs/drain_at_sorted_unchecked

README

drain_at_sorted_unchecked

Crates.io Downloads Documentation License Dependency Status

Terribly unsafe but highly efficient function that allows removing items from a vector with few moves.

Example

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]);
}

Safety

  • The indices must be sorted in ascending order.
  • The indices must be within bounds of the collection.
  • The indices must be unique.
  • The items must be trivially movable.

Notes

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.

License

Licensed under either of

at your option.

Contribution

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.

Commit count: 1

cargo fmt