entropy-map 1.1.0

Ultra-low latency hash map using minimal perfect hash functions and compact encoding of values, minimizing memory footprint and storage size for efficient data retrieval.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! A module providing `MapWithDict`, an immutable hash map implementation.
//!
//! `MapWithDict` is a hash map structure that optimizes for space by utilizing a minimal perfect
//! hash function (MPHF) for indexing the map's keys. This enables efficient storage and retrieval,
//! as it reduces the overall memory footprint by packing unique values into a dictionary. The MPHF
//! provides direct access to the indices of keys, which correspond to their respective values in
//! the values dictionary. Keys are stored to ensure that `get` operation will return `None` if key
//! wasn't present in original set.

use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::mem::size_of_val;

use num::{PrimInt, Unsigned};
use wyhash::WyHash;

use crate::mphf::{Mphf, MphfError, DEFAULT_GAMMA};

/// An efficient, immutable hash map with values dictionary-packed for optimized space usage.
#[derive(Default)]
#[cfg_attr(feature = "rkyv_derive", derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize))]
#[cfg_attr(feature = "rkyv_derive", archive_attr(derive(rkyv::CheckBytes)))]
pub struct MapWithDict<K, V, const B: usize = 32, const S: usize = 8, ST = u8, H = WyHash>
where
    ST: PrimInt + Unsigned,
    H: Hasher + Default,
{
    /// Minimally Perfect Hash Function for keys indices retrieval
    mphf: Mphf<B, S, ST, H>,
    /// Map keys
    keys: Box<[K]>,
    /// Points to the value index in the dictionary
    values_index: Box<[usize]>,
    /// Map unique values
    values_dict: Box<[V]>,
}

impl<K, V, const B: usize, const S: usize, ST, H> MapWithDict<K, V, B, S, ST, H>
where
    K: Eq + Hash + Clone,
    V: Eq + Clone + Hash,
    ST: PrimInt + Unsigned,
    H: Hasher + Default,
{
    /// Constructs a `MapWithDict` from an iterator of key-value pairs and MPHF function params.
    pub fn from_iter_with_params<I>(iter: I, gamma: f32) -> Result<Self, MphfError>
    where
        I: IntoIterator<Item = (K, V)>,
    {
        let mut keys = vec![];
        let mut values_index = vec![];
        let mut values_dict = vec![];
        let mut offsets_cache = HashMap::new();

        for (k, v) in iter {
            keys.push(k.clone());

            if let Some(&offset) = offsets_cache.get(&v) {
                // re-use dictionary offset if found in cache
                values_index.push(offset);
            } else {
                // store current dictionary length as an offset in both index and cache
                let offset = values_dict.len();
                offsets_cache.insert(v.clone(), offset);
                values_index.push(offset);
                values_dict.push(v.clone());
            }
        }

        let mphf = Mphf::from_slice(&keys, gamma)?;

        // Re-order `keys` and `values_index` according to `mphf`
        for i in 0..keys.len() {
            loop {
                let idx = mphf.get(&keys[i]).unwrap();
                if idx == i {
                    break;
                }
                keys.swap(i, idx);
                values_index.swap(i, idx);
            }
        }

        Ok(MapWithDict {
            mphf,
            keys: keys.into_boxed_slice(),
            values_index: values_index.into_boxed_slice(),
            values_dict: values_dict.into_boxed_slice(),
        })
    }

    /// Returns a reference to the value corresponding to the key. Returns `None` if the key is
    /// not present in the map.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// assert_eq!(map.get(&1), Some(&2));
    /// assert_eq!(map.get(&5), None);
    /// ```
    #[inline]
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q> + PartialEq<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let idx = self.mphf.get(key)?;

        // SAFETY: `idx` is always within bounds (ensured during construction)
        unsafe {
            if self.keys.get_unchecked(idx) == key {
                // SAFETY: `idx` and `value_idx` are always within bounds (ensure during construction)
                let value_idx = *self.values_index.get_unchecked(idx);
                Some(self.values_dict.get_unchecked(value_idx))
            } else {
                None
            }
        }
    }

    /// Returns the number of key-value pairs in the map.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// assert_eq!(map.len(), 2);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Returns `true` if the map contains no elements.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(0, 0); 0])).unwrap();
    /// assert_eq!(map.is_empty(), true);
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// assert_eq!(map.is_empty(), false);
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }

    /// Checks if the map contains the specified key.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// assert_eq!(map.contains_key(&1), true);
    /// assert_eq!(map.contains_key(&2), false);
    /// ```
    #[inline]
    pub fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: Borrow<Q> + PartialEq<Q>,
        Q: Hash + Eq + ?Sized,
    {
        if let Some(idx) = self.mphf.get(key) {
            // SAFETY: `idx` is always within bounds (ensured during construction)
            unsafe { self.keys.get_unchecked(idx) == key }
        } else {
            false
        }
    }

    /// Returns an iterator over the map, yielding key-value pairs.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// for (key, val) in map.iter() {
    ///     println!("key: {key} val: {val}");
    /// }
    /// ```
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
        self.keys
            .iter()
            .zip(self.values_index.iter())
            .map(move |(key, &value_idx)| {
                // SAFETY: `value_idx` is always within bounds (ensured during construction)
                let value = unsafe { self.values_dict.get_unchecked(value_idx) };
                (key, value)
            })
    }

    /// Returns an iterator over the keys of the map.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// for key in map.keys() {
    ///     println!("{key}");
    /// }
    /// ```
    #[inline]
    pub fn keys(&self) -> impl Iterator<Item = &K> {
        self.keys.iter()
    }

    /// Returns an iterator over the values of the map.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// for val in map.values() {
    ///     println!("{val}");
    /// }
    /// ```
    #[inline]
    pub fn values(&self) -> impl Iterator<Item = &V> {
        self.values_index.iter().map(move |&value_idx| {
            // SAFETY: `value_idx` is always within bounds (ensured during construction)
            unsafe { self.values_dict.get_unchecked(value_idx) }
        })
    }

    /// Returns the total number of bytes occupied by the structure.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// assert_eq!(map.size(), 270);
    /// ```
    #[inline]
    pub fn size(&self) -> usize {
        size_of_val(self)
            + self.mphf.size()
            + size_of_val(self.keys.as_ref())
            + size_of_val(self.values_index.as_ref())
            + size_of_val(self.values_dict.as_ref())
    }
}

/// Creates a `MapWithDict` from a `HashMap`.
impl<K, V> TryFrom<HashMap<K, V>> for MapWithDict<K, V>
where
    K: Eq + Hash + Clone,
    V: Eq + Clone + Hash,
{
    type Error = MphfError;

    #[inline]
    fn try_from(value: HashMap<K, V>) -> Result<Self, Self::Error> {
        MapWithDict::<K, V>::from_iter_with_params(value, DEFAULT_GAMMA)
    }
}

/// Implement `get` for `Archived` version of `MapWithDict` if feature is enabled
#[cfg(feature = "rkyv_derive")]
impl<K, V, const B: usize, const S: usize, ST, H> ArchivedMapWithDict<K, V, B, S, ST, H>
where
    K: PartialEq + Hash + rkyv::Archive,
    K::Archived: PartialEq<K>,
    V: rkyv::Archive,
    ST: PrimInt + Unsigned + rkyv::Archive<Archived = ST>,
    H: Hasher + Default,
{
    /// Checks if the map contains the specified key.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// let archived_map = rkyv::from_bytes::<MapWithDict<u32, u32>>(
    ///     &rkyv::to_bytes::<_, 1024>(&map).unwrap()
    /// ).unwrap();
    /// assert_eq!(archived_map.contains_key(&1), true);
    /// assert_eq!(archived_map.contains_key(&2), false);
    /// ```
    #[inline]
    pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        <K as rkyv::Archive>::Archived: PartialEq<Q>,
        Q: Hash + Eq,
    {
        if let Some(idx) = self.mphf.get(key) {
            // SAFETY: `idx` is always within bounds (ensured during construction)
            unsafe { self.keys.get_unchecked(idx) == key }
        } else {
            false
        }
    }

    /// Returns a reference to the value corresponding to the key. Returns `None` if the key is
    /// not present in the map.
    ///
    /// # Examples
    /// ```
    /// # use std::collections::HashMap;
    /// # use entropy_map::MapWithDict;
    /// let map = MapWithDict::try_from(HashMap::from([(1, 2), (3, 4)])).unwrap();
    /// let archived_map = rkyv::from_bytes::<MapWithDict<u32, u32>>(
    ///     &rkyv::to_bytes::<_, 1024>(&map).unwrap()
    /// ).unwrap();
    /// assert_eq!(archived_map.get(&1), Some(&2));
    /// assert_eq!(archived_map.get(&5), None);
    /// ```
    #[inline]
    pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V::Archived>
    where
        K: Borrow<Q>,
        <K as rkyv::Archive>::Archived: PartialEq<Q>,
        Q: Hash + Eq,
    {
        let idx = self.mphf.get(key)?;

        // SAFETY: `idx` is always within bounds (ensured during construction)
        unsafe {
            if self.keys.get_unchecked(idx) == key {
                // SAFETY: `idx` and `value_idx` are always within bounds (ensure during construction)
                let value_idx = *self.values_index.get_unchecked(idx) as usize;
                Some(self.values_dict.get_unchecked(value_idx))
            } else {
                None
            }
        }
    }

    /// Returns an iterator over the archived map, yielding archived key-value pairs.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&K::Archived, &V::Archived)> {
        self.keys
            .iter()
            .zip(self.values_index.iter())
            .map(move |(key, &value_idx)| {
                // SAFETY: `value_idx` is always within bounds (ensured during construction)
                let value = unsafe { self.values_dict.get_unchecked(value_idx as usize) };
                (key, value)
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use paste::paste;
    use proptest::prelude::*;
    use rand::{Rng, SeedableRng};
    use rand_chacha::ChaCha8Rng;
    use std::collections::{hash_map::RandomState, HashSet};

    fn gen_map(items_num: usize) -> HashMap<u64, u32> {
        let mut rng = ChaCha8Rng::seed_from_u64(123);

        (0..items_num)
            .map(|_| {
                let key = rng.gen::<u64>();
                let value = rng.gen_range(1..=10);
                (key, value)
            })
            .collect()
    }

    #[test]
    fn test_map_with_dict() {
        // Collect original key-value pairs directly into a HashMap
        let original_map = gen_map(1000);

        // Create the map from the iterator
        let map = MapWithDict::try_from(original_map.clone()).unwrap();

        // Test len
        assert_eq!(map.len(), original_map.len());

        // Test is_empty
        assert_eq!(map.is_empty(), original_map.is_empty());

        // Test get, contains_key
        for (key, value) in &original_map {
            assert_eq!(map.get(key), Some(value));
            assert!(map.contains_key(key));
        }

        // Test iter
        for (&k, &v) in map.iter() {
            assert_eq!(original_map.get(&k), Some(&v));
        }

        // Test keys
        for k in map.keys() {
            assert!(original_map.contains_key(k));
        }

        // Test values
        for &v in map.values() {
            assert!(original_map.values().any(|&val| val == v));
        }

        // Test size
        assert_eq!(map.size(), 16626);
    }

    /// Assert that we can call `.get()` with `K::borrow()`.
    #[test]
    fn test_get_borrow() {
        let original_map = HashMap::from_iter([("a".to_string(), ()), ("b".to_string(), ())]);
        let map = MapWithDict::try_from(original_map).unwrap();

        assert_eq!(map.get("a"), Some(&()));
        assert!(map.contains_key("a"));
        assert_eq!(map.get("b"), Some(&()));
        assert!(map.contains_key("b"));
        assert_eq!(map.get("c"), None);
        assert!(!map.contains_key("c"));
    }

    #[cfg(feature = "rkyv_derive")]
    #[test]
    fn test_rkyv() {
        // create regular `HashMap`, then `MapWithDict`, then serialize to `rkyv` bytes.
        let original_map = gen_map(1000);
        let map = MapWithDict::try_from(original_map.clone()).unwrap();
        let rkyv_bytes = rkyv::to_bytes::<_, 1024>(&map).unwrap();

        assert_eq!(rkyv_bytes.len(), 12464);

        let rkyv_map = rkyv::check_archived_root::<MapWithDict<u64, u32>>(&rkyv_bytes).unwrap();

        // Test get on `Archived` version
        for (k, v) in original_map.iter() {
            assert_eq!(v, rkyv_map.get(k).unwrap());
        }

        // Test iter on `Archived` version
        for (&k, &v) in rkyv_map.iter() {
            assert_eq!(original_map.get(&k), Some(&v));
        }
    }

    #[cfg(feature = "rkyv_derive")]
    #[test]
    fn test_rkyv_get_borrow() {
        let original_map = HashMap::from_iter([("a".to_string(), ()), ("b".to_string(), ())]);
        let map = MapWithDict::try_from(original_map).unwrap();
        let rkyv_bytes = rkyv::to_bytes::<_, 1024>(&map).unwrap();
        let rkyv_map = rkyv::check_archived_root::<MapWithDict<String, ()>>(&rkyv_bytes).unwrap();

        assert_eq!(map.get("a"), Some(&()));
        assert!(rkyv_map.contains_key("a"));
        assert_eq!(map.get("b"), Some(&()));
        assert!(rkyv_map.contains_key("b"));
        assert_eq!(map.get("c"), None);
        assert!(!rkyv_map.contains_key("c"));
    }

    macro_rules! proptest_map_with_dict_model {
        ($(($b:expr, $s:expr, $gamma:expr)),* $(,)?) => {
            $(
                paste! {
                    proptest! {
                        #[test]
                        fn [<proptest_map_with_dict_model_ $b _ $s _ $gamma>](model: HashMap<u64, u64>, arbitrary: HashSet<u64>) {
                            let entropy_map: MapWithDict<u64, u64, $b, $s> = MapWithDict::from_iter_with_params(
                                model.clone(),
                                $gamma as f32 / 100.0
                            ).unwrap();

                            // Assert that length matches model.
                            assert_eq!(entropy_map.len(), model.len());
                            assert_eq!(entropy_map.is_empty(), model.is_empty());

                            // Assert that keys and values match model.
                            assert_eq!(
                                HashSet::<_, RandomState>::from_iter(entropy_map.keys()),
                                HashSet::from_iter(model.keys())
                            );
                            assert_eq!(
                                HashSet::<_, RandomState>::from_iter(entropy_map.values()),
                                HashSet::from_iter(model.values())
                            );

                            // Assert that contains and get operations match model for contained elements.
                            for (k, v) in &model {
                                assert!(entropy_map.contains_key(&k));
                                assert_eq!(entropy_map.get(&k), Some(v));
                            }

                            // Assert that contains and get operations match model for random elements.
                            for k in arbitrary {
                                assert_eq!(
                                    model.contains_key(&k),
                                    entropy_map.contains_key(&k),
                                );
                                assert_eq!(entropy_map.get(&k), model.get(&k));
                            }
                        }
                    }
                }
            )*
        };
    }

    proptest_map_with_dict_model!(
        // (1, 8, 100),
        (2, 8, 100),
        (4, 8, 100),
        (7, 8, 100),
        (8, 8, 100),
        (15, 8, 100),
        (16, 8, 100),
        (23, 8, 100),
        (24, 8, 100),
        (31, 8, 100),
        (32, 8, 100),
        (33, 8, 100),
        (48, 8, 100),
        (53, 8, 100),
        (61, 8, 100),
        (63, 8, 100),
        (64, 8, 100),
        (32, 7, 100),
        (32, 5, 100),
        (32, 4, 100),
        (32, 3, 100),
        (32, 1, 100),
        (32, 0, 100),
        (32, 8, 200),
        (32, 6, 200),
    );
}