gen_map
A customizable generational map that returns a Key upon inserting a value. The key can be used to later access or remove the value, and removing a value bumps its slot's generation, so the old key no longer matches.
The operations for inserting, removing and accessing a value are all O(1).
The crate is no_std and only needs alloc.
[]
= "0.1"
Example
use GenMap;
let mut map = new;
let a = map.insert;
let b = map.insert;
assert_eq!;
assert_eq!;
assert_eq!;
assert!; // A removed key stays invalid forever.
let c = map.insert; // This takes the slot `a` had, but under a new key.
assert_ne!;
for in &map
Configuring the map
A Config picks the index and generation integers, how a key stores the
two, where the slots live and what happens when a slot's generation
overflows. The default uses a u32 index and a u32 generation stored as
two fields, keeps the slots in a Vec, and retires a slot whose generation
overflows, so no stale key can ever match a new value.
use ;
;
let mut map = new_with_config;
let key = map.insert;
assert_eq!;
Option<Key> is the same size as Key with either layout.
Packing the key into one integer
Split stores the index and the generation as two fields. Packed puts
them in the bits of one integer instead, so the two parts can have any bit
counts that add up to that integer. The low GEN_BITS bits hold the
generation and the bits above them hold the index. A config whose bit counts
do not add up fails to compile.
use ;
/// Four byte keys with 24 bits of index and 8 bits of generation.
;
let mut map = new_with_config;
let key = map.insert;
assert_eq!;
assert_eq!;
Choosing the storage
The slots can live in any collection that implements SlotStorage, even
those with a fixed capacity. Besides Vec, the ArrayVec from arrayvec
and the SmallVec from smallvec work out of the box when the crate's
arrayvec or smallvec feature is turned on. An ArrayVec has a fixed
capacity and never allocates, and a SmallVec keeps a few slots inline
before it allocates.
The smallvec feature uses a beta of smallvec 2.0. Until smallvec 2.0 is
released, a newer smallvec beta or a new release of gen_map may break this
feature, so it is not covered by semver.
reserve, try_reserve and the with_capacity constructors are only there
when the storage also implements ReserveStorage, which Vec and
SmallVec do.
Handling a full map
insert panics when the map is full, which happens when the keys have no
index left for a new slot or the storage can not make room for one.
try_insert lets you handle the failure.
use ;
;
let mut map = new_with_config;
for i in 0..256
assert!;
License
gen_map is released under the MIT license.