axhash-map 0.1.0

High-performance HashMap and HashSet backed by hashbrown (SwissTable) and fueled by axhash.
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
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::ops::{Deref, DerefMut, Index};

pub use axhash_core::AxBuildHasher;
pub use hashbrown::{HashMap as RawHashMap, HashSet as RawHashSet};

// ── AxHashMap ────────────────────────────────────────────────────────────────
pub struct AxHashMap<K, V, S = AxBuildHasher>(RawHashMap<K, V, S>);

impl<K, V> AxHashMap<K, V, AxBuildHasher> {
    /// Creates an empty map with the default [`AxBuildHasher`].
    ///
    /// The map is initially created with a capacity of 0 and will reallocate
    /// as elements are inserted.
    #[inline]
    pub fn new() -> Self {
        Self(RawHashMap::with_hasher(AxBuildHasher::new()))
    }

    /// Creates an empty map with at least the given capacity and the default
    /// [`AxBuildHasher`].
    ///
    /// The map will be able to hold at least `capacity` elements without
    /// reallocating.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self(RawHashMap::with_capacity_and_hasher(
            capacity,
            AxBuildHasher::new(),
        ))
    }
}

impl<K, V, S: BuildHasher> AxHashMap<K, V, S> {
    /// Creates an empty map that uses the supplied `hasher`.
    ///
    /// Use this when you need a custom seed or a completely different
    /// [`BuildHasher`].
    #[inline]
    pub fn with_hasher(hasher: S) -> Self {
        Self(RawHashMap::with_hasher(hasher))
    }

    /// Creates an empty map with at least the given capacity that uses the
    /// supplied `hasher`.
    #[inline]
    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
        Self(RawHashMap::with_capacity_and_hasher(capacity, hasher))
    }

    /// Consumes the wrapper and returns the underlying [`RawHashMap`].
    #[inline]
    pub fn into_inner(self) -> RawHashMap<K, V, S> {
        self.0
    }
}

// ── Deref / DerefMut ─────────────────────────────────────────────────────────

impl<K, V, S> Deref for AxHashMap<K, V, S> {
    type Target = RawHashMap<K, V, S>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<K, V, S> DerefMut for AxHashMap<K, V, S> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

// ── Standard traits ───────────────────────────────────────────────────────────

impl<K, V> Default for AxHashMap<K, V, AxBuildHasher> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<K: fmt::Debug, V: fmt::Debug, S> fmt::Debug for AxHashMap<K, V, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<K: Clone, V: Clone, S: Clone> Clone for AxHashMap<K, V, S> {
    #[inline]
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<K: Hash + Eq, V: PartialEq, S: BuildHasher> PartialEq for AxHashMap<K, V, S> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<K: Hash + Eq, V: Eq, S: BuildHasher> Eq for AxHashMap<K, V, S> {}

impl<K, Q, V, S> Index<&Q> for AxHashMap<K, V, S>
where
    K: Hash + Eq + core::borrow::Borrow<Q>,
    Q: Hash + Eq + ?Sized,
    S: BuildHasher,
{
    type Output = V;

    #[inline]
    fn index(&self, key: &Q) -> &Self::Output {
        self.0.index(key)
    }
}

// ── FromIterator / Extend ─────────────────────────────────────────────────────

impl<K: Hash + Eq, V> FromIterator<(K, V)> for AxHashMap<K, V, AxBuildHasher> {
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let (lower, _) = iter.size_hint();
        let mut map = Self::with_capacity(lower);
        map.extend(iter);
        map
    }
}

impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for AxHashMap<K, V, S> {
    #[inline]
    fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
        self.0.extend(iter);
    }
}

impl<'a, K: Hash + Eq + Copy, V: Copy, S: BuildHasher> Extend<(&'a K, &'a V)>
    for AxHashMap<K, V, S>
{
    #[inline]
    fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
        self.0.extend(iter);
    }
}

// ── IntoIterator ──────────────────────────────────────────────────────────────

impl<K, V, S> IntoIterator for AxHashMap<K, V, S> {
    type Item = (K, V);
    type IntoIter = hashbrown::hash_map::IntoIter<K, V>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a, K, V, S> IntoIterator for &'a AxHashMap<K, V, S> {
    type Item = (&'a K, &'a V);
    type IntoIter = hashbrown::hash_map::Iter<'a, K, V>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<'a, K, V, S> IntoIterator for &'a mut AxHashMap<K, V, S> {
    type Item = (&'a K, &'a mut V);
    type IntoIter = hashbrown::hash_map::IterMut<'a, K, V>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}

// ── From conversions ──────────────────────────────────────────────────────────

impl<K, V, S> From<RawHashMap<K, V, S>> for AxHashMap<K, V, S> {
    #[inline]
    fn from(inner: RawHashMap<K, V, S>) -> Self {
        Self(inner)
    }
}

impl<K, V, S> From<AxHashMap<K, V, S>> for RawHashMap<K, V, S> {
    #[inline]
    fn from(wrapper: AxHashMap<K, V, S>) -> Self {
        wrapper.0
    }
}

// ── AxHashSet ────────────────────────────────────────────────────────────────
pub struct AxHashSet<T, S = AxBuildHasher>(RawHashSet<T, S>);

impl<T> AxHashSet<T, AxBuildHasher> {
    /// Creates an empty set with the default [`AxBuildHasher`].
    #[inline]
    pub fn new() -> Self {
        Self(RawHashSet::with_hasher(AxBuildHasher::new()))
    }

    /// Creates an empty set with at least the given capacity and the default
    /// [`AxBuildHasher`].
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self(RawHashSet::with_capacity_and_hasher(
            capacity,
            AxBuildHasher::new(),
        ))
    }
}

impl<T, S: BuildHasher> AxHashSet<T, S> {
    /// Creates an empty set that uses the supplied `hasher`.
    #[inline]
    pub fn with_hasher(hasher: S) -> Self {
        Self(RawHashSet::with_hasher(hasher))
    }

    /// Creates an empty set with at least the given capacity that uses the
    /// supplied `hasher`.
    #[inline]
    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
        Self(RawHashSet::with_capacity_and_hasher(capacity, hasher))
    }

    /// Consumes the wrapper and returns the underlying [`RawHashSet`].
    #[inline]
    pub fn into_inner(self) -> RawHashSet<T, S> {
        self.0
    }
}

// ── Deref / DerefMut ─────────────────────────────────────────────────────────

impl<T, S> Deref for AxHashSet<T, S> {
    type Target = RawHashSet<T, S>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, S> DerefMut for AxHashSet<T, S> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

// ── Standard traits ───────────────────────────────────────────────────────────

impl<T> Default for AxHashSet<T, AxBuildHasher> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T: fmt::Debug, S> fmt::Debug for AxHashSet<T, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T: Clone, S: Clone> Clone for AxHashSet<T, S> {
    #[inline]
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T: Hash + Eq, S: BuildHasher> PartialEq for AxHashSet<T, S> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T: Hash + Eq, S: BuildHasher> Eq for AxHashSet<T, S> {}

// ── FromIterator / Extend ─────────────────────────────────────────────────────

impl<T: Hash + Eq> FromIterator<T> for AxHashSet<T, AxBuildHasher> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let (lower, _) = iter.size_hint();
        let mut set = Self::with_capacity(lower);
        set.extend(iter);
        set
    }
}

impl<T: Hash + Eq, S: BuildHasher> Extend<T> for AxHashSet<T, S> {
    #[inline]
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        self.0.extend(iter);
    }
}

impl<'a, T: Hash + Eq + Copy, S: BuildHasher> Extend<&'a T> for AxHashSet<T, S> {
    #[inline]
    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
        self.0.extend(iter);
    }
}

// ── IntoIterator ──────────────────────────────────────────────────────────────

impl<T, S> IntoIterator for AxHashSet<T, S> {
    type Item = T;
    type IntoIter = hashbrown::hash_set::IntoIter<T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a, T, S> IntoIterator for &'a AxHashSet<T, S> {
    type Item = &'a T;
    type IntoIter = hashbrown::hash_set::Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

// ── From conversions ──────────────────────────────────────────────────────────

impl<T, S> From<RawHashSet<T, S>> for AxHashSet<T, S> {
    #[inline]
    fn from(inner: RawHashSet<T, S>) -> Self {
        Self(inner)
    }
}

impl<T, S> From<AxHashSet<T, S>> for RawHashSet<T, S> {
    #[inline]
    fn from(wrapper: AxHashSet<T, S>) -> Self {
        wrapper.0
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn map_basic_operations() {
        let mut map: AxHashMap<&str, u32> = AxHashMap::new();
        assert!(map.is_empty());

        map.insert("one", 1);
        map.insert("two", 2);
        map.insert("three", 3);

        assert_eq!(map.len(), 3);
        assert_eq!(map["one"], 1);
        assert_eq!(map.get("two"), Some(&2));
        assert_eq!(map.get("missing"), None);

        map.remove("two");
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn map_with_capacity() {
        let map: AxHashMap<u32, u32> = AxHashMap::with_capacity(128);
        assert!(map.capacity() >= 128);
    }

    #[test]
    fn map_default() {
        let map: AxHashMap<u64, u64> = AxHashMap::default();
        assert!(map.is_empty());
    }

    #[test]
    fn map_from_iterator() {
        let pairs = vec![("a", 1u32), ("b", 2), ("c", 3)];
        let map: AxHashMap<&str, u32> = pairs.into_iter().collect();
        assert_eq!(map.len(), 3);
        assert_eq!(map["b"], 2);
    }

    #[test]
    fn map_extend() {
        let mut map: AxHashMap<u32, u32> = AxHashMap::new();
        map.extend([(1, 10), (2, 20)]);
        map.extend([(3, 30)]);
        assert_eq!(map.len(), 3);
        assert_eq!(map[&2], 20);
    }

    #[test]
    fn map_iter() {
        let map: AxHashMap<u32, u32> = [(1, 10), (2, 20)].into_iter().collect();
        let mut sum = 0u32;
        for (_, v) in &map {
            sum += v;
        }
        assert_eq!(sum, 30);
    }

    #[test]
    fn map_into_inner_roundtrip() {
        let mut map: AxHashMap<&str, i32> = AxHashMap::new();
        map.insert("x", 99);
        let raw: RawHashMap<&str, i32, AxBuildHasher> = map.into_inner();
        assert_eq!(raw["x"], 99);
        let wrapped: AxHashMap<&str, i32> = raw.into();
        assert_eq!(wrapped["x"], 99);
    }

    #[test]
    fn map_seeded_hasher() {
        let hasher = AxBuildHasher::with_seed(0x1234_5678_9abc_def0);
        let mut map: AxHashMap<&str, u32> = AxHashMap::with_hasher(hasher);
        map.insert("seeded", 7);
        assert_eq!(map["seeded"], 7);
    }

    #[test]
    fn set_basic_operations() {
        let mut set: AxHashSet<u32> = AxHashSet::new();
        assert!(set.is_empty());

        set.insert(1);
        set.insert(2);
        set.insert(2); // duplicate
        set.insert(3);

        assert_eq!(set.len(), 3);
        assert!(set.contains(&1));
        assert!(!set.contains(&99));

        set.remove(&2);
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn set_with_capacity() {
        let set: AxHashSet<u64> = AxHashSet::with_capacity(64);
        assert!(set.capacity() >= 64);
    }

    #[test]
    fn set_default() {
        let set: AxHashSet<u64> = AxHashSet::default();
        assert!(set.is_empty());
    }

    #[test]
    fn set_from_iterator() {
        let set: AxHashSet<u32> = [1u32, 2, 3, 2, 1].into_iter().collect();
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn set_extend() {
        let mut set: AxHashSet<u32> = AxHashSet::new();
        set.extend([1u32, 2, 3]);
        set.extend([3u32, 4, 5]);
        assert_eq!(set.len(), 5);
    }

    #[test]
    fn set_set_operations() {
        let a: AxHashSet<u32> = [1, 2, 3].into_iter().collect();
        let b: AxHashSet<u32> = [2, 3, 4].into_iter().collect();

        // union, intersection, difference via deref
        let union: AxHashSet<u32> = a.union(&b).copied().collect();
        assert_eq!(union.len(), 4);

        let inter: AxHashSet<u32> = a.intersection(&b).copied().collect();
        assert_eq!(inter.len(), 2);
    }

    #[test]
    fn set_into_inner_roundtrip() {
        let mut set: AxHashSet<i32> = AxHashSet::new();
        set.insert(42);
        let raw: RawHashSet<i32, AxBuildHasher> = set.into_inner();
        assert!(raw.contains(&42));
        let wrapped: AxHashSet<i32> = raw.into();
        assert!(wrapped.contains(&42));
    }
}