fix static hash function

This commit is contained in:
Andrei Yankovich 2020-05-23 02:12:35 +03:00
parent c65aba6329
commit 88a3632807

View File

@ -65,68 +65,42 @@ constexpr inline T operator ^ (T lhs, T rhs)
return static_cast<T>(static_cast<int>(lhs) ^ static_cast<int>(rhs)); return static_cast<T>(static_cast<int>(lhs) ^ static_cast<int>(rhs));
} }
template <class IntegerType>
constexpr uint32_t static_hash(const char* str) { constexpr IntegerType static_mul(const char* str, unsigned char depth, unsigned char index = 0) noexcept {
uint32_t hash = 0; return (str[index] && (depth > index))? str[index] * static_mul<IntegerType>(str, depth, index + 1) : 1;
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 <class IntegerType>
template<class T, class Hash> constexpr IntegerType static_hash(const char* str, unsigned char index = 0) noexcept {
/** return (index && index % sizeof(IntegerType) == 0)?
* @brief static_type_hash_base static_hash<IntegerType>(str, index + 1) % static_mul<IntegerType>(str, index , index - sizeof(IntegerType)):
* @return hash of type (on compiller time) (str[index])?
*/ static_hash<IntegerType>(str, index + 1):
constexpr Hash static_type_hash_base() { static_mul<IntegerType>(str, index, index - (index % sizeof(IntegerType)));
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<class T> template<class T>
constexpr uint64_t static_type_hash_64() { u_int64_t static_type_hash_64() noexcept {
return static_type_hash_base<T, uint64_t>(); return static_hash<u_int64_t>(typeid (T).name());
}; };
template<class T> template<class T>
constexpr uint32_t static_type_hash_32() { u_int32_t static_type_hash_32() noexcept {
return static_type_hash_base<T, uint32_t>(); return typeid (T).hash_code();
}; };
template<class T> template<class T>
constexpr uint16_t static_type_hash_16() { u_int16_t static_type_hash_16() noexcept {
return static_type_hash_base<T, uint16_t>(); return typeid (T).hash_code() % 0xFFFF;
}; };
template<class T> template<class T>
constexpr unsigned char static_type_hash_8() { u_int8_t static_type_hash_8() noexcept {
return static_type_hash_base<T, unsigned char>(); return typeid (T).hash_code() % 0xFF;
}; };
#define H_8 static_type_hash_8 #define H_8 static_type_hash_8
#define H_16 static_type_hash_16 #define H_16 static_type_hash_16
#define H_32 static_type_hash_32 #define H_32 static_type_hash_32
#define H_64 static_type_hash_64
#endif // GLOBAL_H #endif // GLOBAL_H