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

#[derive(Clone, Debug, Default)]
pub struct HashableMap<K, V, S = RandomState>(HashMap<K, V, S>);

impl<K, V> HashableMap<K, V> {
    pub fn new() -> Self {
        Self(HashMap::new())
    }

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

impl<K, V, S> HashableMap<K, V, S> {
    pub fn with_hasher(hash_builder: S) -> Self {
        Self(HashMap::with_hasher(hash_builder))
    }

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

impl<K, V, S> Deref for HashableMap<K, V, S> {
    type Target = HashMap<K, V, S>;

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

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

impl<K, V, S> Hash for HashableMap<K, V, S>
where
    K: Hash,
    V: Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        let hash = self.iter().map(|(k, v)| {
            let mut hasher = std::hash::DefaultHasher::new();
            k.hash(&mut hasher);
            v.hash(&mut hasher);
            hasher.finish()
        }).fold(0, u64::wrapping_add);

        state.write_u64(hash);
    }
}

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

    use super::*;

    #[test]
    fn hash() {
        let mut map_a = HashableMap::<_, _, RandomState>::default();
        map_a.insert(1, "1");
        map_a.insert(2, "2");
        let mut map_b = HashableMap::<_, _, RandomState>::default();
        map_b.insert(1, "1");
        map_b.insert(2, "2");
        let mut map_c = HashableMap::<_, _, RandomState>::default();
        map_c.insert(1, "1");
        map_c.insert(3, "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());
    }
}