#hash-map #map #slab #generate-keys

blazemap

实现了一个基于向量的类似slab的map,接口与HashMap类似,并提供生成轻量级标识符的工具,这些标识符可以作为此map的键安全地使用

6个版本

0.5.1 2024年5月21日
0.4.0 2024年4月24日
0.0.7 2023年12月23日
0.0.3 2023年11月30日

#201 in 数据结构

Download history 669/week @ 2024-04-19 238/week @ 2024-04-26 8/week @ 2024-05-03 2/week @ 2024-05-10 154/week @ 2024-05-17 15/week @ 2024-05-24 2/week @ 2024-05-31 3/week @ 2024-06-07 2/week @ 2024-06-14

1,205 每月下载次数

MIT 许可证

105KB
2.5K SLoC

blazemap

实现了一个基于向量的类似slab的map,接口与HashMap类似,并提供生成轻量级标识符的工具,这些标识符可以作为此map的键安全地使用。

Crates.io Documentation MIT licensed Build Status

用法

目前,这个crate提供了3种基于 usize 的新类型创建方法,这些类型可以用作BlazeMap的键。它们通过以下宏表示,并提供不同的优化。

1. define_key_wrapper!

创建一个新类型,该类型作为旧类型的 usize 替代品,可用于 blazemap 集合的键。

示例

use blazemap::prelude::{BlazeMap, define_key_wrapper};

define_key_wrapper! {
    pub struct Key(&'static str);
    Derive(as for Original Type): {  // Optional section
        Debug,
        Display,
    };
    Derive(as for usize): {          // Optional section
        Ord,
    }
}

let key_1 = Key::new("first");
let key_2 = Key::new("second");
let key_3 = Key::new("third");

let mut map = BlazeMap::new();
map.insert(key_2, "2");
map.insert(key_1, "1");
map.insert(key_3, "3");

assert_eq!(format!("{map:?}"), r#"{"first": "1", "second": "2", "third": "3"}"#)

2. define_key_wrapper_bounded!

创建一个新类型,该类型作为旧类型的 usize 替代品,可用于 blazemap 集合的键。

是针对用户可以静态保证唯一键数量不超过 MAX_CAP 的情况的 define_key_wrapper! 的类似物,它针对读操作进行了优化,以便它们不会产生任何多线程竞争。

示例

use blazemap::prelude::{BlazeMap, define_key_wrapper_bounded};

define_key_wrapper_bounded! {
    pub struct Key(&'static str);
    MAX_CAP = 40_000;
    Derive(as for Original Type): {  // Optional section
        Debug,
        Display,
    };
    Derive(as for usize): {          // Optional section
        Ord,
    }
}

let key_1 = Key::new("first");
let key_2 = Key::new("second");
let key_3 = Key::new("third");

let mut map = BlazeMap::new();
map.insert(key_2, "2");
map.insert(key_1, "1");
map.insert(key_3, "3");

assert_eq!(format!("{map:?}"), r#"{"first": "1", "second": "2", "third": "3"}"#)

3. define_plain_id!

创建一个基于递增生成的 usize 实例的新类型,这些实例可以用作 blazemap 集合的键。这是为 BlazeMap 生成键的最高效方式。

示例

use blazemap::prelude::{BlazeMap, define_plain_id};

define_plain_id! {
    pub struct Id;
    Derive: {       // Optional section
        Ord
    };
}

let key_1 = Id::new();
let key_2 = Id::new();
let key_3 = Id::new();

let mut map = BlazeMap::new();
map.insert(key_2, "2");
map.insert(key_1, "1");
map.insert(key_3, "3");

assert_eq!(format!("{map:?}"), r#"{0: "1", 1: "2", 2: "3"}"#)

依赖项

~0.4–25MB
~347K SLoC