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
#![feature(doc_cfg)]

pub use gxhash::{self, GxHashMap as HashMap, GxHashSet as HashSet};

pub const SEED: i64 = 212370440130137963;

pub fn hash32(input: &[u8]) -> u32 {
  gxhash::gxhash32(input, SEED)
}

pub fn hash64(input: &[u8]) -> u64 {
  gxhash::gxhash64(input, SEED)
}

pub fn hash128(input: &[u8]) -> u128 {
  gxhash::gxhash128(input, SEED)
}

pub const HASH128_LEN: usize = 16;

macro_rules! xhash {
  ($hash:expr,$len:expr) => {
    [
      &$hash.to_le_bytes()[..],
      &intbin::u64_bin(($len - HASH128_LEN) as _)[..],
    ]
    .concat()
    .into()
  };
}

#[cfg(feature = "xhash")]
#[doc(cfg(feature = "xhash"))]
pub fn xhash(input: impl AsRef<[u8]>) -> Box<[u8]> {
  let input = input.as_ref();

  let len = input.len();

  if len > HASH128_LEN {
    return xhash!(hash128(input), len);
  }

  input.into()
}

#[cfg(feature = "hasher")]
#[doc(cfg(feature = "hasher"))]
mod hasher;

#[cfg(feature = "hasher")]
#[doc(cfg(feature = "hasher"))]
pub use hasher::{li, Hasher};