Crates.io | pi_slotmap |
lib.rs | pi_slotmap |
version | 0.1.17 |
source | src |
created_at | 2022-03-04 10:23:31.436563 |
updated_at | 2024-05-29 03:11:49.164492 |
description | Slotmap data structure |
homepage | |
repository | https://github.com/GaiaWorld/pi_slotmap.git |
max_upload_size | |
id | 543453 |
size | 341,529 |
该库slotmap的一份拷贝,另外添加了一些新的功能
A Rust library providing three containers with persistent unique keys to access
stored values, SlotMap
, HopSlotMap
and DenseSlotMap
. Upon insertion a key
is returned that can be used to later access or remove the values. Insertion,
deletion and access all take O(1) time with low overhead. Great for storing
collections of objects that need stable, safe references but have no clear
ownership otherwise, such as game entities or graph nodes. Two secondary maps,
SecondaryMap
and SparseSecondaryMap
are also provided that allow you to map
further objects to the keys created by one of the slot maps. Please refer to the
the documentation for more information.
The minimum required stable Rust version for slotmap
is 1.49. To start using
slotmap
add the following to your Cargo.toml
:
[dependencies]
slotmap = "1.0"
A short example:
use slotmap::{SlotMap, SecondaryMap};
let mut sm = SlotMap::new();
let foo = sm.insert("foo"); // Key generated on insert.
let bar = sm.insert("bar");
assert_eq!(sm[foo], "foo");
assert_eq!(sm[bar], "bar");
sm.remove(bar);
let reuse = sm.insert("reuse"); // Space from bar reused.
assert_eq!(sm.contains_key(bar), false); // After deletion a key stays invalid.
let mut sec = SecondaryMap::new();
sec.insert(foo, "noun"); // We provide the key for secondary maps.
sec.insert(reuse, "verb");
for (key, val) in sm {
println!("{} is a {}", val, sec[key]);
}
slotmap
is released under the Zlib license, a permissive license. It is
OSI and FSF approved and GPL compatible.