8#ifndef SEN_CORE_BASE_HASH32_H
9#define SEN_CORE_BASE_HASH32_H
43template <
typename InputIterator>
44[[nodiscard]]
u32 crc32(InputIterator first, InputIterator last)
noexcept;
47[[nodiscard]]
inline u32 crc32(std::string_view str)
noexcept {
return crc32(str.begin(), str.end()); }
50template <
typename... Types>
56template <
typename... Types>
67 std::string_view symbolName,
68 const std::filesystem::path& outputFile);
80constexpr u32 fnv1aOffsetBasis = 0x811c9dc5;
81constexpr u32 fnv1aPrime = 0x01000193;
85inline constexpr std::enable_if_t<std::is_integral_v<T>,
u32> hashIntegral(T value)
noexcept
87 using UT = std::make_unsigned_t<T>;
88 auto u =
static_cast<UT
>(value);
90 auto hash = fnv1aOffsetBasis;
93 for (
int i = (
sizeof(UT) - 1) * 8; i >= 0; i -= 8)
95 hash ^=
static_cast<u8>((u >> i) & 0xFF);
104inline constexpr std::enable_if_t<std::is_floating_point_v<T>,
u32> hashFloat(T value)
noexcept
106 using IntType = std::conditional_t<
sizeof(T) == 4,
u32,
u64>;
109 std::memcpy(&bits, &value,
sizeof(T));
110 return hashIntegral(bits);
113template <
typename T,
typename Enable =
void>
117struct hash<T, typename std::enable_if_t<std::is_integral_v<T>>>
119 inline u32 operator()(T value)
const noexcept {
return hashIntegral(value); }
123struct hash<T, typename std::enable_if_t<std::is_floating_point_v<T>>>
125 inline u32 operator()(T value)
const noexcept {
return hashFloat(value); }
131 inline u32 operator()(
bool value)
const noexcept {
return hashIntegral(
static_cast<u8>(value)); }
137 inline u32 operator()(
i8 value)
const noexcept {
return hashIntegral(
static_cast<u8>(value)); }
141struct hash<std::basic_string_view<char>>
143 inline u32 operator()(
const std::basic_string_view<char>& value)
const noexcept
145 auto hash = fnv1aOffsetBasis;
147 for (
unsigned char c: value)
158struct hash<std::basic_string<char>>
160 inline u32 operator()(
const std::basic_string<char>& value)
const noexcept
162 return hash<std::basic_string_view<char>>()(value);
167struct hash<T, std::enable_if_t<std::is_enum_v<T>>>
169 inline u32 operator()(T value)
const noexcept {
return hashIntegral(
static_cast<std::underlying_type_t<T>
>(value)); }
175 u32 operator()(T* value)
const noexcept
177 return hashIntegral(
reinterpret_cast<std::uintptr_t
>(value));
181[[nodiscard]] std::array<u32, 256> generateCrc32LookupTable() noexcept;
184inline
void hashCombineImpl(
u32& seed, const T& val) noexcept
186 seed ^= hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2);
190inline void platformDependentHashCombineImpl(std::uint_fast32_t& seed,
const T& val)
noexcept
192 seed ^= std::hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2);
197template <
typename InputIterator>
198inline u32 crc32(InputIterator first, InputIterator last)
noexcept
201 static auto const table = impl::generateCrc32LookupTable();
205 return u32 {0xffffffffUL} &
206 ~std::accumulate(first,
208 ~
u32 {0} &
u32 {0xffffffffUL},
209 [](
u32 checksum,
u32 value)
210 {
return table[(checksum ^ value) & 0xffU] ^ (checksum >> 8U); });
213template <
typename... Types>
216 (impl::hashCombineImpl(seed, args), ...);
220template <
typename... Types>
223 (impl::platformDependentHashCombineImpl(seed, args), ...);
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:33
u32 crc32(InputIterator first, InputIterator last) noexcept
Calculates the CRC32 for any sequence of values. (You could use type traits and a static assert to en...
Definition hash32.h:198
std::string decompressSymbolToString(const void *compressedData, unsigned int originalSize)
Decompresses a blob into a string.
constexpr u32 propertyHashSeed
Definition hash32.h:34
constexpr u32 methodHashSeed
Definition hash32.h:35
uint_fast32_t platformDependentHashCombine(uint_fast32_t seed, const Types &... args) noexcept
Old version of the hashCombine method. The current one replaced this implementation to allow sen proc...
constexpr u32 eventHashSeed
Definition hash32.h:36
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:214
unsigned char * decompressSymbol(const void *compressedData)
Decompresses a blob into its original shape.
constexpr u32 nonPresentTypeHash
This hash is combined when no Type is found in a certain spec.
Definition hash32.h:39
bool fileToCompressedArrayFile(const std::filesystem::path &inputFile, std::string_view symbolName, const std::filesystem::path &outputFile)
Creates a C++ source file that contains an array representing the contents of another file.
int8_t i8
Definition numbers.h:20
uint64_t u64
Definition numbers.h:27
uint32_t u32
Definition numbers.h:25
uint8_t u8
Definition numbers.h:21