Crates.io | allocvec |
lib.rs | allocvec |
version | 0.1.0 |
source | src |
created_at | 2024-07-04 11:59:55.835879 |
updated_at | 2024-07-04 11:59:55.835879 |
description | A vector type that preserves the index of every inserted element |
homepage | |
repository | https://github.com/bohdloss/allocvec |
max_upload_size | |
id | 1291622 |
size | 19,213 |
A vector where each element pushed is guaranteed to keep its index until it is removed.
Internally, a normal Vec
is used along with a linked list
for fast allocation and de-allocation.
Supports reserving space and filling it with an element later.
use allocvec::AllocVec;
let mut vec = AllocVec::new();
let idx1 = vec.allocate(4);
let idx2 = vec.allocate(8);
let idx3 = vec.allocate(15);
vec.deallocate(idx1);
vec.deallocate(idx3);
assert_eq!(Some(8), vec.get(idx2));
Though I haven't benchmarked the efficiency of the implementation, allocation, deallocation and indexing are O(1) operations, while calculating the length of the vector is O(n).
Iterators need to filter unallocated and empty slots in the vector, so that will most likely be bad for branch prediction.