|
| 1 | +/** |
| 2 | + * @file constexpr_fnv1a.hpp |
| 3 | + * @brief Compile-time FNV-1a hash for string interning. |
| 4 | + * |
| 5 | + * Provides constexpr FNV-1a hashing algorithm for compile-time |
| 6 | + * string hash computation. Supports both 32-bit and 64-bit variants. |
| 7 | + * |
| 8 | + * @author Charliechen114514 |
| 9 | + * @date 2026-02-25 |
| 10 | + * @version 0.1 |
| 11 | + * @since 0.1 |
| 12 | + * @ingroup base_hash |
| 13 | + */ |
| 14 | +#pragma once |
| 15 | + |
| 16 | +#include <cstdint> |
| 17 | +#include <string_view> |
| 18 | + |
| 19 | +namespace cf::hash { |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief FNV-1a 64-bit offset basis. |
| 23 | + * |
| 24 | + * @since 0.1 |
| 25 | + * @ingroup base_hash |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +inline constexpr uint64_t fnv1a64_offset_basis = 14695981039346656037ULL; |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief FNV-1a 64-bit prime. |
| 32 | + * |
| 33 | + * @since 0.1 |
| 34 | + * @ingroup base_hash |
| 35 | + * @internal |
| 36 | + */ |
| 37 | +inline constexpr uint64_t fnv1a64_prime = 1099511628211ULL; |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief FNV-1a 32-bit offset basis. |
| 41 | + * |
| 42 | + * @since 0.1 |
| 43 | + * @ingroup base_hash |
| 44 | + * @internal |
| 45 | + */ |
| 46 | +inline constexpr uint32_t fnv1a32_offset_basis = 2166136261U; |
| 47 | + |
| 48 | +/** |
| 49 | + * @brief FNV-1a 32-bit prime. |
| 50 | + * |
| 51 | + * @since 0.1 |
| 52 | + * @ingroup base_hash |
| 53 | + * @internal |
| 54 | + */ |
| 55 | +inline constexpr uint32_t fnv1a32_prime = 16777619U; |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief FNV-1a 64-bit hash constexpr implementation. |
| 59 | + * |
| 60 | + * Computes FNV-1a hash at compile time for string literals. |
| 61 | + * The FNV-1a algorithm provides excellent distribution and |
| 62 | + * minimal collisions for string identifiers. |
| 63 | + * |
| 64 | + * @param str Null-terminated string to hash. |
| 65 | + * @param seed Starting hash value (default: FNV offset basis). |
| 66 | + * |
| 67 | + * @return 64-bit hash value. |
| 68 | + * |
| 69 | + * @throws None. |
| 70 | + * |
| 71 | + * @note The hash is computed at compile time for string literals. |
| 72 | + * |
| 73 | + * @warning Different inputs may produce the same hash (collision). |
| 74 | + * Use string comparison as fallback when hashes match. |
| 75 | + * |
| 76 | + * @since 0.1 |
| 77 | + * @ingroup base_hash |
| 78 | + * |
| 79 | + * @code |
| 80 | + * constexpr uint64_t h1 = fnv1a64("TokenA"); // Compile-time |
| 81 | + * constexpr uint64_t h2 = fnv1a64("TokenB"); // Different value |
| 82 | + * static_assert(h1 != h2, "Hash collision!"); |
| 83 | + * @endcode |
| 84 | + */ |
| 85 | +constexpr uint64_t fnv1a64(const char* str, uint64_t seed = fnv1a64_offset_basis) { |
| 86 | + return (*str == 0) |
| 87 | + ? seed |
| 88 | + : fnv1a64(str + 1, (seed ^ static_cast<uint64_t>(*str)) * fnv1a64_prime); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief FNV-1a 64-bit hash for std::string_view (runtime/constexpr). |
| 93 | + * |
| 94 | + * @param sv String view to hash. |
| 95 | + * @param seed Starting hash value (default: FNV offset basis). |
| 96 | + * |
| 97 | + * @return 64-bit hash value. |
| 98 | + * |
| 99 | + * @since 0.1 |
| 100 | + * @ingroup base_hash |
| 101 | + */ |
| 102 | +constexpr uint64_t fnv1a64(std::string_view sv, uint64_t seed = fnv1a64_offset_basis) { |
| 103 | + uint64_t result = seed; |
| 104 | + for (char c : sv) { |
| 105 | + result = (result ^ static_cast<uint64_t>(c)) * fnv1a64_prime; |
| 106 | + } |
| 107 | + return result; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * @brief FNV-1a 32-bit hash constexpr implementation. |
| 112 | + * |
| 113 | + * @param str Null-terminated string to hash. |
| 114 | + * @param seed Starting hash value (default: FNV offset basis). |
| 115 | + * |
| 116 | + * @return 32-bit hash value. |
| 117 | + * |
| 118 | + * @since 0.1 |
| 119 | + * @ingroup base_hash |
| 120 | + */ |
| 121 | +constexpr uint32_t fnv1a32(const char* str, uint32_t seed = fnv1a32_offset_basis) { |
| 122 | + return (*str == 0) |
| 123 | + ? seed |
| 124 | + : fnv1a32(str + 1, (seed ^ static_cast<uint32_t>(*str)) * fnv1a32_prime); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief FNV-1a 32-bit hash for std::string_view. |
| 129 | + * |
| 130 | + * @param sv String view to hash. |
| 131 | + * @param seed Starting hash value (default: FNV offset basis). |
| 132 | + * |
| 133 | + * @return 32-bit hash value. |
| 134 | + * |
| 135 | + * @since 0.1 |
| 136 | + * @ingroup base_hash |
| 137 | + */ |
| 138 | +constexpr uint32_t fnv1a32(std::string_view sv, uint32_t seed = fnv1a32_offset_basis) { |
| 139 | + uint32_t result = seed; |
| 140 | + for (char c : sv) { |
| 141 | + result = (result ^ static_cast<uint32_t>(c)) * fnv1a32_prime; |
| 142 | + } |
| 143 | + return result; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * @brief User-defined literal for compile-time FNV-1a 64-bit hashing. |
| 148 | + * |
| 149 | + * Enables syntax: `"TokenName"_hash` for compile-time hash computation. |
| 150 | + * |
| 151 | + * @since 0.1 |
| 152 | + * @ingroup base_hash |
| 153 | + * |
| 154 | + * @code |
| 155 | + * constexpr uint64_t h = "MyToken"_hash; // Compile-time hash |
| 156 | + * static_assert(h == fnv1a64("MyToken"), "Hash must match"); |
| 157 | + * @endcode |
| 158 | + */ |
| 159 | +constexpr uint64_t operator""_hash(const char* str, size_t) { |
| 160 | + return fnv1a64(str); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace cf::hash |
0 commit comments