Files
Library/include/Void/Core/Hash.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

171 lines
4.9 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <span>
#include <string_view>
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<const std::byte> data) noexcept
{
std::uint32_t h = 2166136261u;
for (auto b : data) {
h ^= static_cast<unsigned char>(b);
h *= 16777619u;
}
return h;
}
// ── FNV-1a (64-bit) ──────────────────────────────────────────────────────────
[[nodiscard]] constexpr std::uint64_t fnv1a64(std::string_view s) noexcept
{
std::uint64_t h = 14695981039346656037ull;
for (unsigned char c : s) {
h ^= c;
h *= 1099511628211ull;
}
return h;
}
[[nodiscard]] constexpr std::uint64_t
fnv1a64(std::span<const std::byte> data) noexcept
{
std::uint64_t h = 14695981039346656037ull;
for (auto b : data) {
h ^= static_cast<unsigned char>(b);
h *= 1099511628211ull;
}
return h;
}
// ── CRC-32 (ISO 3309 / Ethernet polynomial) ───────────────────────────────────
namespace Detail
{
[[nodiscard]] constexpr std::array<std::uint32_t, 256> make_crc32_table() noexcept
{
std::array<std::uint32_t, 256> 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;
}
} // namespace Detail
[[nodiscard]] inline std::uint32_t crc32(std::string_view s) noexcept
{
static constexpr auto table = Detail::make_crc32_table();
std::uint32_t c = 0xFFFFFFFFu;
for (unsigned char b : s) {
c = table[(c ^ b) & 0xFF] ^ (c >> 8);
}
return c ^ 0xFFFFFFFFu;
}
[[nodiscard]] inline std::uint32_t
crc32(std::span<const std::byte> data) noexcept
{
static constexpr auto table = Detail::make_crc32_table();
std::uint32_t c = 0xFFFFFFFFu;
for (auto b : data) {
c = table[(c ^ static_cast<unsigned char>(b)) & 0xFF] ^ (c >> 8);
}
return c ^ 0xFFFFFFFFu;
}
// ── 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) ─────────────────────────────────────────────────
[[nodiscard]] inline std::uint32_t murmur3_32(std::string_view key,
std::uint32_t seed = 0) noexcept
{
const auto* data = reinterpret_cast<const std::uint8_t*>(key.data());
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) {
std::uint32_t k;
__builtin_memcpy(&k, data + i * 4, 4);
k *= c1;
k = (k << 15) | (k >> 17);
k *= c2;
h ^= k;
h = (h << 13) | (h >> 19);
h = h * 5 + 0xe6546b64u;
}
const auto* tail = data + nblocks * 4;
std::uint32_t k = 0;
switch (len & 3) {
case 3: k ^= static_cast<std::uint32_t>(tail[2]) << 16; [[fallthrough]];
case 2: k ^= static_cast<std::uint32_t>(tail[1]) << 8; [[fallthrough]];
case 1:
k ^= static_cast<std::uint32_t>(tail[0]);
k *= c1;
k = (k << 15) | (k >> 17);
k *= c2;
h ^= k;
}
h ^= static_cast<std::uint32_t>(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