unordered_hash/
lib.rs

1use std::collections::hash_map::DefaultHasher;
2use std::hash::{Hash, Hasher};
3use std::marker::PhantomData;
4
5#[cfg(test)]
6mod tests;
7
8// An alternative API was considered for an UnorderedHasher which implements Hasher by treating each call to write(&mut self, bytes: &[u8])
9// as being atomic. This quickly runs into issues, as eg: &[u64] makes only 2 calls to write, first with the length of the slice,
10// and again with the binary representation of all it's items. So, it's impossible for the UnorderedHasher to know
11// what the boundaries between atomic hashables are.
12pub struct UnorderedHasher<T = DefaultHasher> {
13    hash: u64,
14    _marker: PhantomData<*const T>,
15}
16
17impl UnorderedHasher {
18    pub fn finish(&self) -> u64 {
19        self.hash
20    }
21}
22
23impl<T: Hasher + Default> UnorderedHasher<T> {
24    pub fn new() -> Self {
25        Self {
26            hash: 0,
27            _marker: PhantomData,
28        }
29    }
30
31    pub fn write<H: Hash>(&mut self, value: &H) {
32        // Hashing each independently than adding the results is simple, but correct.
33        // If the original hash function is well distributed, then the addition
34        // will also result in a well distributed value.
35        let mut hasher = T::default();
36        value.hash(&mut hasher);
37        self.hash = self.hash.wrapping_add(hasher.finish());
38    }
39}
40
41impl Default for UnorderedHasher {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47pub fn unordered_hash<T: Hash>(values: impl Iterator<Item = T>) -> u64 {
48    let mut hash = 0;
49    for value in values {
50        let mut hasher = DefaultHasher::default();
51        value.hash(&mut hasher);
52        hash = hasher.finish().wrapping_add(hash);
53    }
54    hash
55}