ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
#[cfg(feature = "std")]
use crate::map::{HashMap, HashSet};
use crate::{
  arch::Active,
  backend::{ActiveBackend, Platform},
  hasher::{IBuildHasher, IHasher},
  stream::{Stream, fill_xof_root},
};

/// Computes a seeded 32-bit hash equal to the low 32 bits of [`hash64`].
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash32(data: &[u8], seed: u64) -> u32 {
  Active::hash32::<false>(data, seed)
}

/// Computes a seeded 64-bit hash equal to the low 64 bits of [`hash128`].
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash64(data: &[u8], seed: u64) -> u64 {
  Active::hash64::<false>(data, seed)
}

/// Computes a seeded 128-bit hash equal to the low 128 bits of [`hash256`].
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash128(data: &[u8], seed: u64) -> u128 {
  Active::hash128::<false>(data, seed)
}

/// Computes a seeded 256-bit hash equal to the first 32 bytes of [`hash_xof`].
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash256(data: &[u8], seed: u64) -> [u8; 32] {
  Active::hash256::<false>(data, seed)
}

/// Fills `out` with a seeded deterministic arbitrary-length extendable-output digest (`XOF`).
#[inline]
pub fn hash_xof(data: &[u8], seed: u64, out: &mut [u8]) {
  if out.is_empty() {
    return;
  }
  fill_xof_root(hash256(data, seed), out, ActiveBackend);
}

/// Creates a seekable deterministic stream seeded by `hash256(data, seed)`.
#[must_use]
#[inline]
pub fn stream(data: &[u8], seed: u64) -> Stream {
  Stream::from_root(hash256(data, seed))
}

/// Creates a keyed seekable cipher stream directly from `seed` (`hash256(&[], seed)`).
#[must_use]
#[inline]
pub fn keystream(seed: u64) -> Stream {
  stream(&[], seed)
}

/// Creates a deterministic `IHasher` initialized with `seed`.
#[must_use]
#[inline]
pub const fn hasher(seed: u64) -> IHasher {
  IHasher::with_seed(seed)
}

/// Creates a deterministic `IBuildHasher` pinned to `seed`.
#[must_use]
#[inline]
pub const fn build_hasher(seed: u64) -> IBuildHasher {
  IBuildHasher::with_seed(seed)
}

/// Creates an empty `HashMap` pinned to a deterministic `seed`.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_map<K, V>(seed: u64) -> HashMap<K, V> {
  HashMap::with_hasher(build_hasher(seed))
}

/// Creates an empty `HashSet` pinned to a deterministic `seed`.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_set<T>(seed: u64) -> HashSet<T> {
  HashSet::with_hasher(build_hasher(seed))
}