#no-std #vec #vector #drain #remove

no-std vecrem

Cursor-like structure for fast iterative removing of elements from a vec

1 unstable release

0.1.0 Jan 7, 2021

#4 in #drain

Download history 65/week @ 2022-08-16 38/week @ 2022-08-23 61/week @ 2022-08-30 104/week @ 2022-09-06 51/week @ 2022-09-13 79/week @ 2022-09-20 134/week @ 2022-09-27 227/week @ 2022-10-04 157/week @ 2022-10-11 59/week @ 2022-10-18 133/week @ 2022-10-25 55/week @ 2022-11-01 123/week @ 2022-11-08 130/week @ 2022-11-15 112/week @ 2022-11-22 96/week @ 2022-11-29

484 downloads per month
Used in 2 crates (via teloxide-core)

Custom license

19KB
270 lines

vecrem

CI status documentation (master) documentation (docs.rs) crates.io LICENSE

Cursor-like helper which allows removing elements from vector without moving the tail every time.

[dependencies]
vecrem = "0.1"

Compiler support: requires rustc 1.36+


lib.rs:

Cursor-like helper which allows removing elements from vector without moving the tail every time.

Vec::remove comparison

If you'll use common Vec::remove to remove only some elements starting from the head, you'll have bad times because it will copy a lot of elements:

vec's memory: [0, 1, 2, 3, 4]

> vec.remove(0);
vec's memory: [-, 1, 2, 3, 4]
vec's memory: [1, 2, 3, 4, -] // copy of 4 elements (the whole tail)

> vec.remove(1);
vec's memory: [1, -, 3, 4, -]
vec's memory: [1, 3, 4, -, -] // copy of 2 elements

> vec.remove(2);
vec's memory: [1, 3, -, -, -]

Whereas [Removing] uses swaps:

vec's memory: [0, 1, 2, 3, 4]
rem's ptr:     ^

> let rem = vec.removing();
> rem.next().unwrap().remove();
vec's memory: [-, 1, 2, 3, 4]
rem's ptr:        ^

> rem.next().unwrap();
vec's memory: [1, -, 2, 3, 4] // one copy of 1
rem's ptr:           ^

> rem.next().unwrap().remove();
vec's memory: [1, -, -, 3, 4]
rem's ptr:              ^

> rem.next().unwrap();
vec's memory: [1, 3, -, -, 4] // one copy of 3
rem's ptr:                 ^

> rem.next().unwrap().remove();
vec's memory: [1, 3, -, -, -]

no_std support

This crate supports #![no_std] but requires alloc (we are working with vec after all)

No runtime deps