usecrate::convert::*; usecrate::operations::*; usecrate::random_state::PI; usecrate::RandomState; use core::hash::Hasher;
/// A `Hasher` for hashing an arbitrary stream of bytes. /// /// Instances of [`AHasher`] represent state that is updated while hashing data. /// /// Each method updates the internal state based on the new data provided. Once /// all of the data has been provided, the resulting hash can be obtained by calling /// `finish()` /// /// [Clone] is also provided in case you wish to calculate hashes for two different items that /// start with the same data. /// #[derive(Debug, Clone)] pubstruct AHasher {
enc: u128,
sum: u128,
key: u128,
}
impl AHasher { /// Creates a new hasher keyed to the provided keys. /// /// Normally hashers are created via `AHasher::default()` for fixed keys or `RandomState::new()` for randomly /// generated keys and `RandomState::with_seeds(a,b)` for seeds that are set and can be reused. All of these work at /// map creation time (and hence don't have any overhead on a per-item bais). /// /// This method directly creates the hasher instance and performs no transformation on the provided seeds. This may /// be useful where a HashBuilder is not desired, such as for testing purposes. /// /// # Example /// /// ``` /// use std::hash::Hasher; /// use ahash::AHasher; /// /// let mut hasher = AHasher::new_with_keys(1234, 5678); /// /// hasher.write_u32(1989); /// hasher.write_u8(11); /// hasher.write_u8(9); /// hasher.write(b"Huh?"); /// /// println!("Hash is {:x}!", hasher.finish()); /// ``` #[inline] pub(crate) fn new_with_keys(key1: u128, key2: u128) -> Self { let pi: [u128; 2] = PI.convert(); let key1 = key1 ^ pi[0]; let key2 = key2 ^ pi[1]; Self {
enc: key1,
sum: key2,
key: key1 ^ key2,
}
}
/// Provides [Hasher] methods to hash all of the primitive types. /// /// [Hasher]: core::hash::Hasher impl Hasher for AHasher { #[inline] fn write_u8(&mutself, i: u8) { self.write_u64(i as u64);
}
#[inline] fn write_u16(&mutself, i: u16) { self.write_u64(i as u64);
}
#[inline] fn write_u32(&mutself, i: u32) { self.write_u64(i as u64);
}
/// A specialized hasher for strings /// Note that the other types don't panic because the hash impl for String tacks on an unneeded call. (As does vec) #[cfg(feature = "specialize")] impl Hasher for AHasherStr { #[inline] fn finish(&self) -> u64 { let result: [u64; 2] = self.0.enc.convert();
result[0]
}
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.