tkaitchuck / aHash

aHash is a non-cryptographic hashing algorithm that uses the AES hardware instruction
https://crates.io/crates/ahash
Apache License 2.0
986 stars 94 forks source link

Suggestion: Alternative wrapper #233

Closed Jgfrausing closed 3 months ago

Jgfrausing commented 3 months ago

Instead of having to decide between using ahash::HashMap and ahash::AhashMap, I was wondering if they could be merged using a builder. The idea is to export a struct with only the new() method(s) that returns a std::collections::HashMap. I'm not sure which considerations has been put into the current wrappers and aliases, so I might be missing something. The idea looks as follows:

pub use hash::{HashMapBuilder, HashSetBuilder};
pub use std::collections::hash_map::*;
pub use std::collections::hash_set::*;

mod hash {
    use ahash::{HashMap, HashMapExt};

    pub type HashSetBuilder<K> = HashMap<K, ()>;
    pub struct HashMapBuilder;

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

        pub fn with_capacity<K, V>(capacity: usize) -> HashMap<K, V> {
            HashMap::with_capacity(capacity)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{HashMap, HashMapBuilder};

    #[test]
    fn test_hash_map() {
        let map = HashMapBuilder::new();
        let mut map = identity(map);
        map.insert(1, 2);
        assert_eq!(map.get(&1), Some(&2));
    }

    fn identity<K, V, S>(x: HashMap<K, V, S>) -> HashMap<K, V, S> {
        x
    }
}

The only downside, I've been able to find, is that the following function does not work. This is because the two Random state variants are not identical. Maybe someone smarter than me can find a way to solve that.

fn identity<K, V>(
    x: HashMap<K, V>,
) -> HashMap<K, V> {
    x
}

Is it something that makes sense to include?

Jgfrausing commented 3 months ago

Closing again as this did not make usage any simple than use ahash::{HashMap, HashMapExt}