1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
use crate::{city_hash_128_to_64, city_hash_128, city_hash_128_with_seed, city_hash_32, city_hash_64, city_hash_64_with_seed};
use core::hash::{BuildHasher, BuildHasherDefault, Hasher};
/// 64-bits CityHash hasher
#[derive(Debug, Default)]
pub struct CityHash64Hasher {
key: Option<u64>,
}
impl CityHash64Hasher {
/// Create a new [CityHash64Hasher] initiated with a hash key
pub fn with_seed(seed: u64) -> CityHash64Hasher {
CityHash64Hasher { key: Some(seed) }
}
}
impl Hasher for CityHash64Hasher {
/// Returns the hash value for the values written so far.
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash64Hasher;
/// use core::hash::Hasher;
///
/// let hasher = CityHash64Hasher::with_seed(0xB4BFA9E87732C149);
/// assert_eq!(hasher.finish(), 0xB4BFA9E87732C149);
/// ```
fn finish(&self) -> u64 {
if let Some(hash) = self.key {
hash
} else {
0
}
}
/// Writes some data into the [CityHash64Hasher].
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash64Hasher;
/// use core::hash::Hasher;
///
/// let mut hasher = CityHash64Hasher::default();
/// hasher.write(b"hash me!");
/// ```
fn write(&mut self, bytes: &[u8]) {
self.key = Some(match self.key {
None => city_hash_64(bytes),
Some(seed) => city_hash_64_with_seed(bytes, seed),
})
}
}
impl BuildHasher for CityHash64Hasher {
type Hasher = CityHash64Hasher;
/// Creates a new [CityHash64Hasher].
fn build_hasher(&self) -> Self::Hasher {
CityHash64Hasher::default()
}
}
/// 32-bits CityHash hasher
#[derive(Debug, Default)]
pub struct CityHash32Hasher {
key: Option<u32>,
}
impl CityHash32Hasher {
/// Create a new [CityHash32Hasher] initiated with a hash key
pub fn with_seed(seed: u32) -> CityHash32Hasher {
CityHash32Hasher { key: Some(seed) }
}
}
impl Hasher for CityHash32Hasher {
/// Returns the hash value for the values written so far.
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash32Hasher;
/// use core::hash::Hasher;
///
/// let hasher = CityHash32Hasher::with_seed(0xB4BFA9E);
/// assert_eq!(hasher.finish(), 0xB4BFA9E);
/// ```
fn finish(&self) -> u64 {
if let Some(hash) = self.key {
hash as u64
} else {
0
}
}
/// Writes some data into the [CityHash32Hasher].
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash32Hasher;
/// use core::hash::Hasher;
///
/// let mut hasher = CityHash32Hasher::default();
/// hasher.write(b"hash me!");
/// ```
fn write(&mut self, bytes: &[u8]) {
self.key = Some(match self.key {
None => city_hash_32(bytes),
Some(mut seed) => {
let mut key = city_hash_32(bytes);
// Mix of 2 u32 based on Murmur3.
// Magic numbers for 32-bit hashing.
const C1: u32 = 0xcc9e2d51;
const C2: u32 = 0x1b873593;
// Combine 2 32-bits values from Murmur3
key = key.wrapping_mul(C1);
key = key.rotate_right(17);
key = key.wrapping_mul(C2);
seed ^= key;
seed = seed.rotate_right(19);
seed.wrapping_mul(5).wrapping_add(0xe6546b64)
}
})
}
}
impl BuildHasher for CityHash32Hasher {
type Hasher = CityHash32Hasher;
/// Creates a new [CityHash32Hasher].
fn build_hasher(&self) -> Self::Hasher {
CityHash32Hasher::default()
}
}
/// 128-bits CityHash hasher
#[derive(Debug, Default)]
pub struct CityHash128Hasher {
key: Option<u128>,
}
impl CityHash128Hasher {
/// Create a new [CityHash128Hasher] initiated with a hash key
pub fn with_seed(seed: u128) -> CityHash128Hasher {
CityHash128Hasher { key: Some(seed) }
}
}
impl Hasher for CityHash128Hasher {
/// Returns the hash value for the values written so far.
/// The hash is compress to a 64-bits with [city_hash_128_to_64]
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash128Hasher;
/// use core::hash::Hasher;
///
/// let hasher = CityHash128Hasher::with_seed(0xAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB);
/// assert_eq!(hasher.finish(), 0x88FC029FFEBB98B4);
/// ```
fn finish(&self) -> u64 {
if let Some(hash) = self.key {
city_hash_128_to_64(hash)
} else {
0
}
}
/// Writes some data into the [CityHash128Hasher].
///
/// # Example
///
/// ```
/// use cityhash_sys::CityHash128Hasher;
/// use core::hash::Hasher;
///
/// let mut hasher = CityHash128Hasher::default();
/// hasher.write(b"hash me!");
/// ```
fn write(&mut self, bytes: &[u8]) {
self.key = Some(match self.key {
None => city_hash_128(bytes),
Some(seed) => city_hash_128_with_seed(bytes, seed),
})
}
}
impl BuildHasher for CityHash128Hasher {
type Hasher = CityHash128Hasher;
/// Creates a new [CityHash128Hasher].
fn build_hasher(&self) -> Self::Hasher {
CityHash128Hasher::default()
}
}
/// A builder for default 32-bits CityHash hashers.
pub type CityHash32BuildHasher = BuildHasherDefault<CityHash32Hasher>;
/// A builder for default 64-bits CityHash hashers.
pub type CityHash64BuildHasher = BuildHasherDefault<CityHash64Hasher>;
/// A builder for default 128-bits CityHash hashers.
pub type CityHash128BuildHasher = BuildHasherDefault<CityHash128Hasher>;
/// A builder for default CityHash hashers.
pub type CityHashBuildHasher = CityHash64BuildHasher;