Skip to main content

sparse_hash_map/
sparsity.rs

1//! Sparsity levels controlling per-array capacity slack.
2//!
3//! A sparse array over-allocates its dense storage in fixed steps. A larger
4//! step wastes more memory but reallocates less often on insert. Sparsity does
5//! not change lookup speed or iteration order. It changes only the memory and
6//! insert-speed trade-off.
7
8/// A sparsity level. `STEP` is the amount a full sparse array grows by.
9pub trait Sparsity: Clone {
10    /// Capacity growth step for a sparse array.
11    const STEP: u8;
12}
13
14/// Least memory slack. Grows by 2. Slower inserts.
15#[derive(Clone)]
16pub struct High;
17impl Sparsity for High {
18    const STEP: u8 = 2;
19}
20
21/// Balanced slack. Grows by 4. The default.
22#[derive(Clone)]
23pub struct Medium;
24impl Sparsity for Medium {
25    const STEP: u8 = 4;
26}
27
28/// Most memory slack. Grows by 8. Faster inserts.
29#[derive(Clone)]
30pub struct Low;
31impl Sparsity for Low {
32    const STEP: u8 = 8;
33}