#drain #remove #vec #retain #vector

drain_at_sorted_unchecked

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

1 unstable release

0.1.0 Jul 22, 2023

#2109 in Algorithms

23 downloads per month

MIT/Apache

16KB
173 lines

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.

No runtime deps