#pragma once #include #include #include #include namespace Void::Hash { // ── FNV-1a (32-bit) ────────────────────────────────────────────────────────── [[nodiscard]] constexpr std::uint32_t fnv1a32(std::string_view s) noexcept { std::uint32_t h = 2166136261u; for (unsigned char c : s) { h ^= c; h *= 16777619u; } return h; } [[nodiscard]] constexpr std::uint32_t fnv1a32(std::span data) noexcept { std::uint32_t h = 2166136261u; for (auto b : data) { h ^= static_cast(b); h *= 16777619u; } return h; } // ── FNV-1a (64-bit) ────────────────────────────────────────────────────────── inline constexpr std::uint64_t fnv1a64_offset_basis = 14695981039346656037ull; [[nodiscard]] constexpr std::uint64_t fnv1a64_update(std::uint64_t state, std::span data) noexcept { for (auto b : data) { state ^= static_cast(b); state *= 1099511628211ull; } return state; } [[nodiscard]] constexpr std::uint64_t fnv1a64_update(std::uint64_t state, std::string_view s) noexcept { for (unsigned char c : s) { state ^= c; state *= 1099511628211ull; } return state; } [[nodiscard]] constexpr std::uint64_t fnv1a64(std::string_view s) noexcept { return fnv1a64_update(fnv1a64_offset_basis, s); } [[nodiscard]] constexpr std::uint64_t fnv1a64(std::span data) noexcept { return fnv1a64_update(fnv1a64_offset_basis, data); } // ── CRC-32 (ISO 3309 / Ethernet polynomial) ─────────────────────────────────── namespace Detail { [[nodiscard]] constexpr std::array make_crc32_table() noexcept { std::array t{}; for (std::uint32_t i = 0; i < 256; ++i) { std::uint32_t c = i; for (int k = 0; k < 8; ++k) { c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); } t[i] = c; } return t; } inline constexpr auto kCrc32Table = make_crc32_table(); } // namespace Detail inline constexpr std::uint32_t crc32_initial = 0xFFFFFFFFu; [[nodiscard]] constexpr std::uint32_t crc32_update(std::uint32_t state, std::span data) noexcept { for (auto b : data) { state = Detail::kCrc32Table[(state ^ static_cast(b)) & 0xFF] ^ (state >> 8); } return state; } [[nodiscard]] constexpr std::uint32_t crc32_update(std::uint32_t state, std::string_view s) noexcept { for (unsigned char b : s) { state = Detail::kCrc32Table[(state ^ b) & 0xFF] ^ (state >> 8); } return state; } [[nodiscard]] constexpr std::uint32_t crc32_finalize(std::uint32_t state) noexcept { return state ^ 0xFFFFFFFFu; } [[nodiscard]] constexpr std::uint32_t crc32(std::string_view s) noexcept { return crc32_finalize(crc32_update(crc32_initial, s)); } [[nodiscard]] constexpr std::uint32_t crc32(std::span data) noexcept { return crc32_finalize(crc32_update(crc32_initial, data)); } // ── DJB2 ────────────────────────────────────────────────────────────────────── [[nodiscard]] constexpr std::uint64_t djb2(std::string_view s) noexcept { std::uint64_t h = 5381; for (unsigned char c : s) { h = ((h << 5) + h) ^ c; } return h; } // ── MurmurHash3 (32-bit, x86, endian-portable) ──────────────────────────────── [[nodiscard]] constexpr std::uint32_t murmur3_32(std::string_view key, std::uint32_t seed = 0) noexcept { const std::size_t len = key.size(); const std::size_t nblocks = len / 4; std::uint32_t h = seed; constexpr std::uint32_t c1 = 0xcc9e2d51u; constexpr std::uint32_t c2 = 0x1b873593u; for (std::size_t i = 0; i < nblocks; ++i) { const std::size_t off = i * 4; std::uint32_t k = static_cast(static_cast(key[off])) | (static_cast(static_cast(key[off + 1])) << 8) | (static_cast(static_cast(key[off + 2])) << 16) | (static_cast(static_cast(key[off + 3])) << 24); k *= c1; k = (k << 15) | (k >> 17); k *= c2; h ^= k; h = (h << 13) | (h >> 19); h = h * 5 + 0xe6546b64u; } const std::size_t tail_off = nblocks * 4; std::uint32_t k = 0; switch (len & 3) { case 3: k ^= static_cast( static_cast(key[tail_off + 2])) << 16; [[fallthrough]]; case 2: k ^= static_cast( static_cast(key[tail_off + 1])) << 8; [[fallthrough]]; case 1: k ^= static_cast( static_cast(key[tail_off])); k *= c1; k = (k << 15) | (k >> 17); k *= c2; h ^= k; } h ^= static_cast(len); h ^= h >> 16; h *= 0x85ebca6bu; h ^= h >> 13; h *= 0xc2b2ae35u; h ^= h >> 16; return h; } // ── Convenience: string literal compile-time hash ──────────────────────────── inline namespace Literals { [[nodiscard]] consteval std::uint32_t operator""_fnv32(const char* s, std::size_t n) noexcept { return fnv1a32(std::string_view{s, n}); } [[nodiscard]] consteval std::uint64_t operator""_fnv64(const char* s, std::size_t n) noexcept { return fnv1a64(std::string_view{s, n}); } } // namespace Literals } // namespace Void::Hash