4 个版本 (2 个破坏性更新)

0.3.0 2023年1月4日
0.2.1 2022年10月30日
0.2.0 2022年4月15日
0.1.0 2022年4月15日

740 in 游戏开发

MIT 许可证

26KB
445

基于代际索引的向量

此包提供了一个使用代际索引来访问其元素的向量类型。向索引添加代际计数器允许使先前删除的向量条目的陈旧引用无效。

该向量本身由一个 空闲 列表支持,以跟踪元素删除后可重用的空隙。

use generational_vector::{GenerationalVector, DeletionResult};

fn example() {
    let mut v = GenerationalVector::default();

    // Adding elements.
    let a = v.push("first");
    let b = v.push("second");
    assert_eq!(v.get(&a).unwrap(), &"first");
    assert_eq!(v.get(&b).unwrap(), &"second");

    // Removing elements.
    v.remove(&b);
    assert!(v.get(&b).is_none());

    // Overwriting a previously freed slot.
    let c = v.push("third");
    assert_eq!(v.get(&c).unwrap(), &"third");

    // The previous index 'b' internally points to the
    // same address as c. It uses an older generation however,
    // so is considered "not found":
    assert_eq!(v.get(&b), None);

    // Values can be enumerated.
    // Note that the ordering depends on insertions and deletions.
    for value in v {
        println!("{}", value);
    }
}

上述脚本来自 examples/example.rs,可以使用以下命令运行:

cargo run --example example

您可以在 tests/tests.rs 中找到更多使用示例。

包功能

  • smallvec:启用在空闲列表中使用 SmallVec<T>
  • tinyvec:启用在空闲列表中使用 TinyVec<T>

基准测试

该项目使用 Criterion 进行基准测试。要执行基准测试,请运行

cargo criterion

材料和来源

依赖关系

~235KB