/// A [`Hasher`] instance implementing foldhash, optimized for quality. /// /// While you can create one directly with [`FoldHasher::with_seed`], you /// most likely want to use [`RandomState`], [`SeedableRandomState`] or /// [`FixedState`] to create [`FoldHasher`]s. #[derive(Clone)] pubstruct FoldHasher<'a> { pub(crate) inner: fast::FoldHasher<'a>,
}
impl<'a> FoldHasher<'a> { /// Initializes this [`FoldHasher`] with the given per-hasher seed and /// [`SharedSeed`]. #[inline(always)] pubconstfn with_seed(per_hasher_seed: u64, shared_seed: &'a SharedSeed) -> FoldHasher<'a> {
FoldHasher {
inner: fast::FoldHasher::with_seed(per_hasher_seed, shared_seed),
}
}
}
/// A [`BuildHasher`] for [`quality::FoldHasher`](FoldHasher) that is randomly initialized. #[derive(Clone, Default, Debug)] pubstruct RandomState {
inner: fast::RandomState,
}
impl BuildHasher for RandomState { type Hasher = FoldHasher<'static>;
/// A [`BuildHasher`] for [`quality::FoldHasher`](FoldHasher) that is randomly /// initialized by default, but can also be initialized with a specific seed. /// /// This can be useful for e.g. testing, but the downside is that this type /// has a size of 16 bytes rather than the 8 bytes [`RandomState`] is. #[derive(Clone, Default, Debug)] pubstruct SeedableRandomState {
inner: fast::SeedableRandomState,
}
impl SeedableRandomState { /// Generates a random [`SeedableRandomState`], similar to [`RandomState`]. #[inline(always)] pubfn random() -> Self { Self {
inner: fast::SeedableRandomState::random(),
}
}
/// Generates a fixed [`SeedableRandomState`], similar to [`FixedState`]. #[inline(always)] pubfn fixed() -> Self { Self {
inner: fast::SeedableRandomState::fixed(),
}
}
/// Generates a [`SeedableRandomState`] with the given per-hasher seed /// and [`SharedSeed`]. #[inline(always)] pubfn with_seed(per_hasher_seed: u64, shared_seed: &'static SharedSeed) -> Self { Self { // We do an additional folded multiply with the seed here for // the quality hash to ensure better independence between seed // and hash.
inner: fast::SeedableRandomState::with_seed(
folded_multiply(per_hasher_seed, ARBITRARY4),
shared_seed,
),
}
}
}
impl BuildHasher for SeedableRandomState { type Hasher = FoldHasher<'static>;
/// A [`BuildHasher`] for [`quality::FoldHasher`](FoldHasher) that always has the same fixed seed. /// /// Not recommended unless you absolutely need determinism. #[derive(Clone, Default, Debug)] pubstruct FixedState {
inner: fast::FixedState,
}
impl FixedState { /// Creates a [`FixedState`] with the given per-hasher seed. #[inline(always)] pubconstfn with_seed(per_hasher_seed: u64) -> Self { Self { // We do an additional folded multiply with the seed here for // the quality hash to ensure better independence between seed // and hash. If the seed is zero the folded multiply is zero, // preserving with_seed(0) == default().
inner: fast::FixedState::with_seed(folded_multiply(per_hasher_seed, ARBITRARY4)),
}
}
}
impl BuildHasher for FixedState { type Hasher = FoldHasher<'static>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.