use ahash::{AHasher, RandomState};
use core::hash::{BuildHasher, Hasher};
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[must_use]
pub fn create_hash_map<K, V>() -> HashMap<K, V, RandomState> {
HashMap::with_hasher(RandomState::new())
}
#[must_use]
pub fn create_hash_map_with_capacity<K, V>(capacity: usize) -> HashMap<K, V, RandomState> {
HashMap::with_capacity_and_hasher(capacity, RandomState::new())
}
#[must_use]
pub fn create_hasher() -> AHasher {
RandomState::new().build_hasher()
}
#[cfg(test)]
#[must_use]
pub fn create_deterministic_hasher() -> AHasher {
use ahash::RandomState;
RandomState::with_seeds(0x1234_5678_9abc_def0, 0xfedc_ba98_7654_3210, 0, 0).build_hasher()
}
pub fn hash_value<T: core::hash::Hash>(value: &T) -> u64 {
let mut hasher = create_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[derive(Debug, Clone)]
pub struct HashConfig {
pub deterministic: bool,
pub default_capacity: usize,
pub load_factor: f32,
}
impl Default for HashConfig {
fn default() -> Self {
Self {
deterministic: false,
default_capacity: 16,
load_factor: 0.75,
}
}
}
impl HashConfig {
#[cfg(test)]
#[must_use]
pub const fn for_testing() -> Self {
Self {
deterministic: true,
default_capacity: 8,
load_factor: 0.75,
}
}
#[must_use]
pub fn create_map<K, V>(&self) -> HashMap<K, V, RandomState> {
if self.deterministic {
HashMap::with_capacity_and_hasher(
self.default_capacity,
RandomState::with_seeds(0x1234_5678_9abc_def0, 0xfedc_ba98_7654_3210, 0, 0),
)
} else {
create_hash_map_with_capacity(self.default_capacity)
}
}
}
#[cfg(test)]
mod tests {}