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

blazemap

Implements a vector-based slab-like map with an interface similar to that of HashMap, and also provides tools for generating lightweight identifiers that can be type-safely used as keys for this map

6 releases

0.5.1 May 21, 2024
0.4.0 Apr 24, 2024
0.0.7 Dec 23, 2023
0.0.3 Nov 30, 2023

#214 in Concurrency

Download history 3/week @ 2024-08-17 29/week @ 2024-08-24 61/week @ 2024-08-31 41/week @ 2024-09-07 19/week @ 2024-09-14 73/week @ 2024-09-21 49/week @ 2024-09-28 35/week @ 2024-10-05 117/week @ 2024-10-12 38/week @ 2024-10-19 177/week @ 2024-10-26 25/week @ 2024-11-02

358 downloads per month

MIT license

105KB
2.5K SLoC

blazemap

Implements a vector-based slab-like map with an interface similar to that of HashMap, and also provides tools for generating lightweight identifiers that can be type-safely used as keys for this map.

Crates.io Documentation MIT licensed Build Status

Usage

Currently, this crate provides 3 ways to create new types based on usize that can be used as keys to BlazeMap. They are represented by the following macros and provide different optimizations.

1. define_key_wrapper!

Creates a new type that acts as an usize-based replacement for the old type that can be used as a key for blazemap collections.

Example

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!

Creates a new type that acts as an usize-based replacement for the old type that can be used as a key for blazemap collections.

Being an analogue of define_key_wrapper! for the case when the user could statically guarantee that the number of unique keys doesn't exceed MAX_CAP, it's optimized for read operations so that they don't create any multi-thread contention.

Example

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!

Creates a new type based on incrementally generated usize instances that can be used as a key for blazemap collections. This is the most performant way to generate keys for BlazeMap.

Example

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"}"#)

Dependencies

~0.4–25MB
~345K SLoC