Added FNV1a hash
This commit is contained in:
parent
a1cd09b5d4
commit
79bb27d9cb
@ -26,8 +26,12 @@ namespace hgl
|
||||
XXH3_64,
|
||||
XXH3_128,
|
||||
|
||||
FNV1a,
|
||||
// Murmur3,
|
||||
// City64,
|
||||
// City128,
|
||||
|
||||
ENUM_CLASS_RANGE(Adler32,XXH3_128)
|
||||
ENUM_CLASS_RANGE(Adler32,FNV1a)
|
||||
};//enum HASH
|
||||
|
||||
/**
|
||||
@ -110,8 +114,9 @@ namespace hgl
|
||||
using HashCodexxH64 =HashCode<8> ;
|
||||
using HashCodeXXH3_64 =HashCode<8> ;
|
||||
using HashCodeXXH3_128 =HashCode<16> ;
|
||||
using HashCodeFNV1a =HashCode<4>;
|
||||
|
||||
const int hash_code_bytes[]={4,4,16,16,20,20,32,64,4,8,16}; //hash码长度
|
||||
const int hash_code_bytes[]={4,4,16,16,20,20,32,64,4,8,16,4}; //hash码长度
|
||||
|
||||
/**
|
||||
* 散列值计算功能基类
|
||||
@ -160,8 +165,9 @@ namespace hgl
|
||||
HGL_CREATE_HASH_FUNC(xxH64)
|
||||
HGL_CREATE_HASH_FUNC(XXH3_64)
|
||||
HGL_CREATE_HASH_FUNC(XXH3_128)
|
||||
HGL_CREATE_HASH_FUNC(FNV1a)
|
||||
|
||||
#undef HGL_CREATE_HASH_FUNC
|
||||
//#undef HGL_CREATE_HASH_FUNC
|
||||
|
||||
inline Hash *CreateHash(HASH ha)
|
||||
{
|
||||
@ -183,6 +189,7 @@ namespace hgl
|
||||
CreatexxH64Hash,
|
||||
CreateXXH3_64Hash,
|
||||
CreateXXH3_128Hash,
|
||||
CreateFNV1aHash
|
||||
};
|
||||
|
||||
return func[(size_t)ha]();
|
||||
@ -240,7 +247,8 @@ namespace hgl
|
||||
CountHash<HASH::xxH32 >,
|
||||
CountHash<HASH::xxH64 >,
|
||||
CountHash<HASH::XXH3_64 >,
|
||||
CountHash<HASH::XXH3_128>
|
||||
CountHash<HASH::XXH3_128>,
|
||||
CountHash<HASH::FNV1a >
|
||||
};
|
||||
|
||||
return func[(size_t)ha](data,size,hash_code);
|
||||
@ -311,7 +319,8 @@ namespace hgl
|
||||
CountHashStr<HASH::xxH32 >,
|
||||
CountHashStr<HASH::xxH64 >,
|
||||
CountHashStr<HASH::XXH3_64 >,
|
||||
CountHashStr<HASH::XXH3_128 >
|
||||
CountHashStr<HASH::XXH3_128 >,
|
||||
CountHashStr<HASH::FNV1a >
|
||||
};
|
||||
|
||||
return func[(size_t)ha](data,size,hash_str,litter);
|
||||
@ -333,6 +342,7 @@ namespace hgl
|
||||
HGL_COUNT_HASH_FUNC(xxH64)
|
||||
HGL_COUNT_HASH_FUNC(XXH3_64)
|
||||
HGL_COUNT_HASH_FUNC(XXH3_128)
|
||||
HGL_COUNT_HASH_FUNC(FNV1a)
|
||||
|
||||
#undef HGL_COUNT_HASH_FUNC
|
||||
|
||||
|
@ -69,7 +69,11 @@ IF(CM_UTIL_SUPPORT_HASH)
|
||||
hash/sha1le.cpp
|
||||
hash/sha256.cpp
|
||||
hash/sha512.cpp
|
||||
hash/xxHash3.cpp)
|
||||
hash/xxHash3.cpp
|
||||
hash/FNV1a.cpp
|
||||
hash/MurmurHash3.cpp
|
||||
hash/GoogleCityHash.cpp
|
||||
)
|
||||
|
||||
SOURCE_GROUP("HASH" FILES ${HASH_HEADER_FILES} ${HASH_SOURCE_FILES})
|
||||
ENDIF(CM_UTIL_SUPPORT_HASH)
|
||||
|
@ -6,14 +6,13 @@ namespace hgl
|
||||
{
|
||||
namespace
|
||||
{
|
||||
uint32_t FNV1aHash(const void *key, int len)
|
||||
uint32_t CountFNV1a(uint32_t hash,const void *key, int len)
|
||||
{
|
||||
//本代码来自Github Copilot
|
||||
|
||||
//FNV-1a 是一种简单且高效的哈希算法,适用于大多数场景。它具有良好的分布性和较快的计算速度。
|
||||
|
||||
const uint8_t *data = (const uint8_t *)key;
|
||||
uint32_t hash = 2166136261u;
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
hash ^= data[i];
|
||||
@ -23,5 +22,33 @@ namespace hgl
|
||||
return hash;
|
||||
}
|
||||
}//namespace
|
||||
|
||||
class FNV1a:public Hash
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
public:
|
||||
|
||||
void GetName(UTF8String &str)const override{str=U8_TEXT("FNV1a");}
|
||||
void GetName(UTF16String &str)const override{str=U16_TEXT("FNV1a");}
|
||||
const int GetHashBytes()const override{return 4;}
|
||||
void Init()override
|
||||
{
|
||||
result=2166136261u;
|
||||
}
|
||||
void Update(const void *input,uint inputLen)override
|
||||
{
|
||||
result=CountFNV1a(result,input,inputLen);
|
||||
}
|
||||
void Final(void *digest)override
|
||||
{
|
||||
*(uint32_t *)digest=result;
|
||||
}
|
||||
};//class FNV1a
|
||||
|
||||
Hash *CreateFNV1aHash()
|
||||
{
|
||||
return(new FNV1a);
|
||||
}
|
||||
}//namespace util
|
||||
}//namespace hgl
|
||||
|
Loading…
x
Reference in New Issue
Block a user