use fearless_simd::Level;
#[cfg(feature = "std")]
use super::{HashMap, HashSet};
use super::{
IBuildHasher, IHasher, Stream,
vperm::{CpuBackend, compress_hi, compress_lo, detect_level},
};
use crate::{
arch::soft::{short_128, short_256},
state::SHORT_MAX,
stream::fill_xof_root,
};
pub(super) trait LevelSource: Copy {
fn resolve(self) -> Level;
}
impl LevelSource for Level {
#[inline(always)]
fn resolve(self) -> Level {
self
}
}
#[derive(Clone, Copy)]
pub(super) struct DetectLevel;
impl LevelSource for DetectLevel {
#[inline(always)]
fn resolve(self) -> Level {
detect_level()
}
}
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub(super) fn hash128_with<const SEED0: bool>(
data: &[u8],
seed: u64,
src: impl LevelSource,
) -> u128 {
if data.len() <= SHORT_MAX {
return short_128::<SEED0>(data, seed);
}
compress_lo::<SEED0>(data, seed, src.resolve())
}
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub(super) fn hash256_with<const SEED0: bool>(
data: &[u8],
seed: u64,
src: impl LevelSource,
) -> [u8; 32] {
if data.len() <= SHORT_MAX {
return short_256::<SEED0>(data, seed);
}
compress_hi::<SEED0>(data, seed, src.resolve())
}
#[inline(always)]
pub(super) fn hash_xof_with<const SEED0: bool>(
data: &[u8],
seed: u64,
out: &mut [u8],
src: impl LevelSource,
) {
if out.is_empty() {
return;
}
let lvl = src.resolve();
let root = hash256_with::<SEED0>(data, seed, lvl);
fill_xof_root(root, out, CpuBackend(lvl));
}
#[must_use]
#[inline]
pub fn hash32_with_level(data: &[u8], seed: u64, level: Level) -> u32 {
hash64_with_level(data, seed, level) as u32
}
#[must_use]
#[inline]
pub fn hash64_with_level(data: &[u8], seed: u64, level: Level) -> u64 {
hash128_with_level(data, seed, level) as u64
}
#[must_use]
#[inline]
pub fn hash128_with_level(data: &[u8], seed: u64, level: Level) -> u128 {
hash128_with::<false>(data, seed, level)
}
#[must_use]
#[inline]
pub fn hash256_with_level(data: &[u8], seed: u64, level: Level) -> [u8; 32] {
hash256_with::<false>(data, seed, level)
}
#[inline]
pub fn hash_xof_with_level(data: &[u8], seed: u64, out: &mut [u8], level: Level) {
hash_xof_with::<false>(data, seed, out, level);
}
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash32(data: &[u8], seed: u64) -> u32 {
hash64(data, seed) as u32
}
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash64(data: &[u8], seed: u64) -> u64 {
hash128(data, seed) as u64
}
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash128(data: &[u8], seed: u64) -> u128 {
hash128_with::<false>(data, seed, DetectLevel)
}
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash256(data: &[u8], seed: u64) -> [u8; 32] {
hash256_with::<false>(data, seed, DetectLevel)
}
#[inline]
pub fn hash_xof(data: &[u8], seed: u64, out: &mut [u8]) {
hash_xof_with::<false>(data, seed, out, DetectLevel);
}
#[must_use]
#[inline]
pub fn stream(data: &[u8], seed: u64) -> Stream {
stream_with_level(data, seed, detect_level())
}
#[must_use]
#[inline]
pub fn stream_with_level(data: &[u8], seed: u64, level: Level) -> Stream {
Stream::from_root_with_level(hash256_with_level(data, seed, level), level)
}
#[must_use]
#[inline]
pub fn keystream(seed: u64) -> Stream {
stream(&[], seed)
}
#[must_use]
#[inline]
pub fn keystream_with_level(seed: u64, level: Level) -> Stream {
stream_with_level(&[], seed, level)
}
#[must_use]
#[inline]
pub fn hasher(seed: u64) -> IHasher {
IHasher::with_seed(seed)
}
#[must_use]
#[inline]
pub const fn hasher_with_level(seed: u64, level: Level) -> IHasher {
IHasher::with_seed_and_level(seed, level)
}
#[must_use]
#[inline]
pub fn build_hasher(seed: u64) -> IBuildHasher {
IBuildHasher::with_seed(seed)
}
#[must_use]
#[inline]
pub const fn build_hasher_with_level(seed: u64, level: Level) -> IBuildHasher {
IBuildHasher::with_seed_and_level(seed, level)
}
#[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))
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_map_with_level<K, V>(seed: u64, level: Level) -> HashMap<K, V> {
HashMap::with_hasher(build_hasher_with_level(seed, level))
}
#[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))
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_set_with_level<T>(seed: u64, level: Level) -> HashSet<T> {
HashSet::with_hasher(build_hasher_with_level(seed, level))
}