Skip to main content

farmhash/
lib.rs

1mod platform;
2mod farmhashna;
3mod farmhashmk;
4mod farmhashuo;
5mod farmhashxo;
6mod farmhashna_shared;
7mod farmhashcc_shared;
8mod farmhashmk_shared;
9
10use farmhashna::na_hash64;
11use farmhashmk::mk_hash32;
12use farmhashmk::mk_hash32_with_seed;
13use farmhashxo::xo_hash64;
14use farmhashxo::xo_hash64_with_seed;
15use farmhashxo::xo_hash64_with_seeds;
16use std::hash::Hasher;
17
18/// Create a new farmhash based u32 for an array of bytes.  Hash value may vary
19/// with library version and platform.
20///
21/// # Examples
22///
23/// ```
24/// let value: &str = "hello world";
25/// let res32 = farmhash::hash32(&value.as_bytes());
26/// //res32 ==> 430397466
27/// ```
28pub fn hash32(s: &[u8]) -> u32 {
29    mk_hash32(s)
30}
31
32/// Create a new farmhash based u32 for an array of bytes with a given seed.
33/// Hash value may vary with library version and platform.
34pub fn hash32_with_seed(s: &[u8], seed: u32) -> u32 {
35    mk_hash32_with_seed(s, seed)
36}
37
38/// Create a new farmhash based u64 for an array of bytes.  Hash value may
39/// vary with library version and platform.
40///
41/// # Examples
42///
43/// ```
44/// let value: &str = "hello world";
45/// let res64 = farmhash::hash64(&value.as_bytes());
46/// //res64 ==> 6381520714923946011
47/// ```
48pub fn hash64(s: &[u8]) -> u64 {
49    xo_hash64(s)
50}
51
52/// Create a new farmhash based u64 for an array of bytes with a given seed.
53/// Hash value may vary with library version and platform.
54pub fn hash64_with_seed(s: &[u8], seed: u64) -> u64 {
55    xo_hash64_with_seed(s, seed)
56}
57
58/// Create a new farmhash based u64 for an array of bytes with 2 given seeds.
59/// Hash value may vary with library version and platform.
60pub fn hash64_with_seeds(s: &[u8], seed0: u64, seed1: u64) -> u64 {
61    xo_hash64_with_seeds(s, seed0, seed1)
62}
63
64/// Create a new farmhash based u32 for an array of bytes.  Fingerprint value
65/// should be portable and stable across library versions and platforms.
66///
67/// # Examples
68///
69/// ```
70/// let value: &str = "hello world";
71/// let res32 = farmhash::fingerprint32(&value.as_bytes());
72/// //res32 ==> 430397466
73/// ```
74pub fn fingerprint32(s: &[u8]) -> u32 {
75    mk_hash32(s)
76}
77
78/// Create a stable farmhash based u64 for an array of bytes.  Fingerprint value
79/// should be portable and stable across library versions and platforms.
80///
81/// # Examples
82///
83/// ```
84/// let value: &str = "hello world";
85/// let res64 = farmhash::fingerprint64(&value.as_bytes());
86/// //res64 ==> 6381520714923946011
87/// ```
88pub fn fingerprint64(s: &[u8]) -> u64 {
89    na_hash64(s)
90}
91
92pub struct FarmHasher {
93    bytes: Vec<u8>
94}
95
96impl Default for FarmHasher {
97    #[inline]
98    fn default() -> FarmHasher { FarmHasher{bytes: Vec::with_capacity(20)} }
99}
100
101impl Hasher for FarmHasher {
102    #[inline]
103    fn finish(&self) -> u64 {
104        hash64(&self.bytes[..])
105    }
106    #[inline]
107    fn write(&mut self, bytes: &[u8]) {
108        self.bytes.extend(bytes.iter().cloned());
109    }
110}