From 88a3632807a68c52a8d8f50b423a41c07fd0d81f Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 23 May 2020 02:12:35 +0300 Subject: [PATCH] fix static hash function --- global.h | 62 ++++++++++++++++---------------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/global.h b/global.h index 873befb..f0f543d 100644 --- a/global.h +++ b/global.h @@ -65,68 +65,42 @@ constexpr inline T operator ^ (T lhs, T rhs) return static_cast(static_cast(lhs) ^ static_cast(rhs)); } - -constexpr uint32_t static_hash(const char* str) { - uint32_t hash = 0; - unsigned char index = 0; - uint32_t tmp = 1; - while (str[index] && index < 0xff) { - tmp *= str[index]; - if (index % 4 == 0) { - hash = hash % str[index]; - tmp = 1; - } - } - return hash; +template +constexpr IntegerType static_mul(const char* str, unsigned char depth, unsigned char index = 0) noexcept { + return (str[index] && (depth > index))? str[index] * static_mul(str, depth, index + 1) : 1; } - -template -/** - * @brief static_type_hash_base - * @return hash of type (on compiller time) - */ -constexpr Hash static_type_hash_base() { - - auto name = typeid(T).name(); - - Hash hash = 0; - unsigned char index = 0; - Hash tmp = 1; - while (name[index] && index < 0xff) { - tmp *= name[index]; - if (index % sizeof(Hash) == 0) { - hash = hash % name[index]; - tmp = 1; - } - } - return hash; +template +constexpr IntegerType static_hash(const char* str, unsigned char index = 0) noexcept { + return (index && index % sizeof(IntegerType) == 0)? + static_hash(str, index + 1) % static_mul(str, index , index - sizeof(IntegerType)): + (str[index])? + static_hash(str, index + 1): + static_mul(str, index, index - (index % sizeof(IntegerType))); } template -constexpr uint64_t static_type_hash_64() { - return static_type_hash_base(); +u_int64_t static_type_hash_64() noexcept { + return static_hash(typeid (T).name()); }; template -constexpr uint32_t static_type_hash_32() { - return static_type_hash_base(); +u_int32_t static_type_hash_32() noexcept { + return typeid (T).hash_code(); }; template -constexpr uint16_t static_type_hash_16() { - return static_type_hash_base(); +u_int16_t static_type_hash_16() noexcept { + return typeid (T).hash_code() % 0xFFFF; }; template -constexpr unsigned char static_type_hash_8() { - return static_type_hash_base(); +u_int8_t static_type_hash_8() noexcept { + return typeid (T).hash_code() % 0xFF; }; - #define H_8 static_type_hash_8 #define H_16 static_type_hash_16 #define H_32 static_type_hash_32 -#define H_64 static_type_hash_64 #endif // GLOBAL_H