stable-vec

Build Status crates.io version docs

A Vec<T>-like collection which guarantees stable indices and features O(1) element removal at the cost of wasting some memory. It is semantically very similar to Vec<Option<T>>, but with a more optimized memory layout and a more convenient API. This data structure is very useful as a foundation to implement other data structures like graphs and polygon meshes. In those situations, stable-vec functions a bit like an arena memory allocator. This crate works in #![no_std] context (it needs the alloc crate, though).

This crate implements different strategies to store the information. As these strategies have slightly different performance characteristics, the user can choose which to use. The two main strategies are: - something similar to Vec<T> with a BitVec (used by default), and - something similar to Vec<Option<T>>.

Please refer to the documentation for more information. Example:

```rust let mut sv = StableVec::new(); let staridx = sv.push('★'); let heartidx = sv.push('♥'); let lamda_idx = sv.push('λ');

// Deleting an element does not invalidate any other indices. sv.remove(staridx); asserteq!(sv[heartidx], '♥'); asserteq!(sv[lamda_idx], 'λ');

// You can insert into empty slots (again, without invalidating any indices) sv.insert(star_idx, '☺');

// We can also reserve memory (create new empty slots) and insert into // these new slots. All slots up to sv.capacity() can be accessed. sv.reservefor(15); asserteq!(sv.get(15), None); sv.insert(15, '☮');

// The final state of the stable vec asserteq!(sv.get(0), Some(&'☺')); asserteq!(sv.get(1), Some(&'♥')); asserteq!(sv.get(2), Some(&'λ')); asserteq!(sv.get(3), None); asserteq!(sv.get(14), None); asserteq!(sv.get(15), Some(&'☮')); ```

Alternatives? What about slab?

The crate slab works very similar to stable-vec, but has way more downloads. Despite being very similar, there are a few differences which might be important for you:


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.