Fowler–Noll–Vo ハッシュ関数の実装を試みています
擬似コードは次のようになります
hash = FNV_offset_basis
for each byte_of_data to be hashed
hash = hash × FNV_prime
hash = hash XOR byte_of_data
return hash
これはそのための私のコードです
uint8_t byte_of_data;
uint16_t hash;
uint16_t FNV_offset_basis;
uint16_t FNV_prime;
void computeHash(std::string p)
{
FNV_offset_basis = 0xcbf29ce484222325;
FNV_prime = 0x100000001b3;
hash = FNV_offset_basis;
//Iterate through the string
for(int i=0 ; i<p.size();i++)
{
hash = hash * FNV_prime;
hash = hash ^ p.at(i);
}
std::cout << hash; //output 2983
std::cout << std::hex << hash ; //ba7
}
今はこんな感じで使っています
int main()
{
computeHash("Hello");
}
ここで結果をテストして おり、結果は0d47307150c412cfとして得られます
アップデート:
タイプを次のように修正しました
uint8_t byte_of_data;
uint64_t hash;
uint64_t FNV_offset_basis;
uint64_t FNV_prime;
そして、まだ結果0d47307150c412cfと一致しない結果fa365282a44c0ba7を取得します
これを修正する方法についての提案