mashmap/
lib.rs

1#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
2
3//! A flat HashMap that supports multiple entries per key.
4//!
5//! This is an adaptation of Rust's standard `HashMap` (using [hashbrown](https://github.com/rust-lang/hashbrown)'s [`RawTable`](https://docs.rs/hashbrown/0.14.5/hashbrown/raw/struct.RawTable.html)) to support multiple entries with the same key.
6//! While a common approach is to use a `HashMap<K, Vec<V>>` to store the entries corresponding to a key in a `Vec`, [`MashMap`] keeps a flat layout and stores all entries in the same table using probing to select the slots.
7//! This approach avoids the memory indirection caused by vector pointers, and reduces memory overhead since it avoids storing the pointer + length + capacity of a vector for each key.
8//!
9//! ```rust
10//! use mashmap::MashMap;
11//!
12//! let mut map = MashMap::<usize, usize>::new();
13//! map.insert(1, 10);
14//! map.insert(1, 11);
15//! map.insert(1, 12);
16//! map.insert(2, 20);
17//! map.insert(2, 21);
18//!
19//! // iterate over the values with key `1` with mutable references and increment them
20//! for val in map.get_mut_iter(&1) {
21//!     *val += 1;
22//! }
23//!
24//! // collect the values with keys `1` and `2`
25//! // note that the order may differ from the insertion order
26//! let mut values_1: Vec<_> = map.get_iter(&1).copied().collect();
27//! let mut values_2: Vec<_> = map.get_iter(&2).copied().collect();
28//! values_1.sort_unstable();
29//! values_2.sort_unstable();
30//!
31//! assert_eq!(values_1, vec![11, 12, 13]);
32//! assert_eq!(values_2, vec![20, 21]);
33//! ```
34
35pub(crate) mod exhaust;
36mod map;
37
38pub use map::*;