int32_t BKDRHash(const std::string &name){
int32_t hash = 0, seed = 131;
for (uint32_t i = 0; i < name.length(); i++){
hash = hash * seed + name[i];
//transform hash into 131 system
}
//to get positive number by using hash & 0111 111 111 1111b
}
//hash function 2
int32_t APHash(const std::string &name){
int32_t hash = 0;
for (uint32_t i = 0; i < name.length(); i++){
if (i % 2){
hash ^= (~((hash << 11) ^ (name[i]) ^ (hash >> 5)));
}
else {
hash ^= ((hash << 7) ^ (name[i]) ^ (hash >> 3));
}
}
return (hash & 0x7fffffff);
}
常用哈希函数
最新推荐文章于 2024-05-25 10:00:54 发布