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
use std::{collections::HashSet, hash::{BuildHasher, Hash, Hasher, RandomState}, ops::{Deref, DerefMut}};

#[derive(Clone, Debug, Default)]
pub struct HashableSet<T, S = RandomState>(HashSet<T, S>);

impl<T> HashableSet<T> {
    pub fn new() -> Self {
        Self(HashSet::new())
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self(HashSet::with_capacity(capacity))
    }
}

impl<T, S> HashableSet<T, S> {
    pub fn with_hasher(hash_builder: S) -> Self {
        Self(HashSet::with_hasher(hash_builder))
    }

    pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
        Self(HashSet::with_capacity_and_hasher(capacity, hash_builder))
    }
}

impl<T, S> Deref for HashableSet<T, S> {
    type Target = HashSet<T, S>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, S> DerefMut for HashableSet<T, S> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T, S> From<HashSet<T, S>> for HashableSet<T, S> {
    fn from(value: HashSet<T, S>) -> Self {
        Self(value)
    }
}

impl<T, S> Into<HashSet<T, S>> for HashableSet<T, S> {
    fn into(self) -> HashSet<T, S> {
        self.0
    }
}

impl<T, S, D> Hash for HashableSet<T, S>
where
    T: Hash,
    S: BuildHasher<Hasher = D>,
    D: Hasher + Default
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        let hash = self.iter().map(|t| {
            let mut hasher = D::default();
            t.hash(&mut hasher);
            hasher.finish()
        }).fold(0, u64::wrapping_add);

        state.write_u64(hash);
    }
}

#[cfg(test)]
mod tests {
    use std::hash::DefaultHasher;
    use fxhash::FxBuildHasher;

    use super::*;

    #[test]
    fn random_state() {
        let mut map_a = HashableSet::<_, RandomState>::default();
        map_a.insert(1);
        map_a.insert(2);
        let mut map_b = HashableSet::<_, RandomState>::default();
        map_b.insert(1);
        map_b.insert(2);
        let mut map_c = HashableSet::<_, RandomState>::default();
        map_c.insert(1);
        map_c.insert(3);

        let mut hasher_a = DefaultHasher::new();
        map_a.hash(&mut hasher_a);
        let mut hasher_b = DefaultHasher::new();
        map_b.hash(&mut hasher_b);
        let mut hasher_c = DefaultHasher::new();
        map_c.hash(&mut hasher_c);

        assert_eq!(hasher_a.finish(), hasher_b.finish());
        assert_ne!(hasher_a.finish(), hasher_c.finish());
    }

    #[test]
    fn fx_build_hasher() {
        let mut map_a = HashableSet::<_, FxBuildHasher>::default();
        map_a.insert(1);
        map_a.insert(2);
        let mut map_b = HashableSet::<_, FxBuildHasher>::default();
        map_b.insert(1);
        map_b.insert(2);
        let mut map_c = HashableSet::<_, FxBuildHasher>::default();
        map_c.insert(1);
        map_c.insert(3);

        let mut hasher_a = DefaultHasher::new();
        map_a.hash(&mut hasher_a);
        let mut hasher_b = DefaultHasher::new();
        map_b.hash(&mut hasher_b);
        let mut hasher_c = DefaultHasher::new();
        map_c.hash(&mut hasher_c);

        assert_eq!(hasher_a.finish(), hasher_b.finish());
        assert_ne!(hasher_a.finish(), hasher_c.finish());
    }
}