8#ifndef SEN_CORE_BASE_HASH32_H
9#define SEN_CORE_BASE_HASH32_H
44template <
typename InputIterator>
45[[nodiscard]]
u32 crc32(InputIterator first, InputIterator last)
noexcept;
48[[nodiscard]]
inline u32 crc32(std::string_view str)
noexcept {
return crc32(str.begin(), str.end()); }
51template <
typename... Types>
57template <
typename... Types>
61[[nodiscard]] std::unique_ptr<unsigned char[]>
decompressSymbol(
const void* compressedData);
68 std::string_view symbolName,
69 const std::filesystem::path& outputFile);
81constexpr u32 fnv1aOffsetBasis = 0x811c9dc5;
82constexpr u32 fnv1aPrime = 0x01000193;
86inline constexpr std::enable_if_t<std::is_integral_v<T>,
u32> hashIntegral(T value)
noexcept
88 using UT = std::make_unsigned_t<T>;
89 auto u =
static_cast<UT
>(value);
91 auto hash = fnv1aOffsetBasis;
94 for (
int i = (
sizeof(UT) - 1) * 8; i >= 0; i -= 8)
96 hash ^=
static_cast<u8>((u >> i) & 0xFF);
105inline constexpr std::enable_if_t<std::is_floating_point_v<T>,
u32> hashFloat(T value)
noexcept
107 using IntType = std::conditional_t<
sizeof(T) == 4,
u32,
u64>;
110 std::memcpy(&bits, &value,
sizeof(T));
111 return hashIntegral(bits);
114template <
typename T,
typename Enable =
void>
118struct hash<T, typename std::enable_if_t<std::is_integral_v<T>>>
120 inline u32 operator()(T value)
const noexcept {
return hashIntegral(value); }
124struct hash<T, typename std::enable_if_t<std::is_floating_point_v<T>>>
126 inline u32 operator()(T value)
const noexcept {
return hashFloat(value); }
132 inline u32 operator()(
bool value)
const noexcept {
return hashIntegral(
static_cast<u8>(value)); }
138 inline u32 operator()(
i8 value)
const noexcept {
return hashIntegral(
static_cast<u8>(value)); }
142struct hash<std::basic_string_view<char>>
144 inline u32 operator()(
const std::basic_string_view<char>& value)
const noexcept
146 auto hash = fnv1aOffsetBasis;
148 for (
unsigned char c: value)
159struct hash<std::basic_string<char>>
161 inline u32 operator()(
const std::basic_string<char>& value)
const noexcept
163 return hash<std::basic_string_view<char>>()(value);
168struct hash<T, std::enable_if_t<std::is_enum_v<T>>>
170 inline u32 operator()(T value)
const noexcept {
return hashIntegral(
static_cast<std::underlying_type_t<T>
>(value)); }
176 u32 operator()(T* value)
const noexcept
178 return hashIntegral(
reinterpret_cast<std::uintptr_t
>(value));
182[[nodiscard]] std::array<u32, 256> generateCrc32LookupTable() noexcept;
185inline
void hashCombineImpl(
u32& seed, const T& val) noexcept
187 seed ^= hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2);
191inline void platformDependentHashCombineImpl(std::uint_fast32_t& seed,
const T& val)
noexcept
193 seed ^= std::hash<T>()(val) + 0x9e3779b9U + (seed << 6) + (seed >> 2);
198template <
typename InputIterator>
199inline u32 crc32(InputIterator first, InputIterator last)
noexcept
202 static auto const table = impl::generateCrc32LookupTable();
206 return u32 {0xffffffffUL} &
207 ~std::accumulate(first,
209 ~
u32 {0} &
u32 {0xffffffffUL},
210 [](
u32 checksum,
u32 value)
211 {
return table[(checksum ^ value) & 0xffU] ^ (checksum >> 8U); });
214template <
typename... Types>
217 (impl::hashCombineImpl(seed, args), ...);
221template <
typename... Types>
224 (impl::platformDependentHashCombineImpl(seed, args), ...);
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:34
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:199
std::string decompressSymbolToString(const void *compressedData, unsigned int originalSize)
Decompresses a blob into a string.
constexpr u32 propertyHashSeed
Definition hash32.h:35
constexpr u32 methodHashSeed
Definition hash32.h:36
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:37
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:215
std::unique_ptr< 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:40
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