Skip to main content

aoc_core/
hash.rs

1use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
2
3/// Adds `with_capacity` function for [`rustc-hash`](https://docs.rs/rustc-hash) types.
4pub trait FxHashWithCapacity {
5    /// Creates an empty instance with the specified `capacity`.
6    fn with_capacity(capacity: usize) -> Self;
7}
8
9#[allow(clippy::implicit_hasher)]
10impl<K, V> FxHashWithCapacity for FxHashMap<K, V> {
11    #[inline]
12    fn with_capacity(capacity: usize) -> Self {
13        Self::with_capacity_and_hasher(capacity, FxBuildHasher)
14    }
15}
16
17#[allow(clippy::implicit_hasher)]
18impl<V> FxHashWithCapacity for FxHashSet<V> {
19    fn with_capacity(capacity: usize) -> Self {
20        Self::with_capacity_and_hasher(capacity, FxBuildHasher)
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use rustc_hash::{FxHashMap, FxHashSet};
27
28    use super::FxHashWithCapacity;
29
30    #[test]
31    fn fxhashmap_fxhashwithcapacity() {
32        let input = 8;
33        let expected = 8;
34        let output = FxHashMap::<u8, u8>::with_capacity(input).capacity();
35        assert!(expected <= output, "\n input: {input:?}");
36    }
37
38    #[test]
39    fn fxhashset_fxhashwithcapacity() {
40        let input = 8;
41        let expected = 8;
42        let output = FxHashSet::<u8>::with_capacity(input).capacity();
43        assert!(expected <= output, "\n input: {input:?}");
44    }
45}