https://github.com/mpdn/scanmut
O(n) multi-insert and -remove for Rust vectors
https://github.com/mpdn/scanmut
Last synced: about 1 year ago
JSON representation
O(n) multi-insert and -remove for Rust vectors
- Host: GitHub
- URL: https://github.com/mpdn/scanmut
- Owner: mpdn
- License: mit
- Created: 2019-12-01T12:49:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-19T18:41:49.000Z (over 5 years ago)
- Last Synced: 2025-05-08T01:44:26.196Z (about 1 year ago)
- Language: Rust
- Size: 15.6 KB
- Stars: 10
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scanmut
Insert or remove multiple elements from a `Vec` in `O(n)` time.
This crate provides two types for versatile and efficient `Vec` mutation; `Inserter` and
`Remover`. These two types can be seen as more generic implementations of `Vec::insert` and
`Vec::drain`, allowing you to for example efficiently insert a slice, conditionally drain
elements, or access elements of a `Vec` while draining from it.
`Inserter` and `Remover` requires an ordering of the insert and removal indices; monotonically
non-increasing for `Inserter` and monotonically increasing for `Remover`.
For convenience, there are also extension traits adding common higher level operations using the
`Inserter` and `Remover`. See `ScanMut`, `InsertSliceClone`, and `InsertSliceCopy`.
## Examples
Inserting a slice into a vec using [ScanMut::insert_all]:
```rust
use scanmut::prelude::*;
let mut v = vec!['a', 'b', 'c'];
v.insert_all(1, ['d', 'e'].iter().cloned());
assert_eq!(v, vec!['a', 'd', 'e', 'b', 'c']);
```
Removing multiple elements in one scan using [ScanMut::multi_remove]:
```rust
use scanmut::prelude::*;
let mut v = vec!['a', 'b', 'c'];
v.multi_remove([0, 2].iter().cloned(), drop);
assert_eq!(v, vec!['b']);
```
License: MIT