memgraph/utils/hashing/fnv32.hpp
2015-06-22 02:26:41 +02:00

53 lines
1.0 KiB
C++

#ifndef MEMGRAPH_UTILS_HASHING_FNV_FNV32_HPP
#define MEMGRAPH_UTILS_HASHING_FNV_FNV32_HPP
#include <cstdlib>
#include <string>
#define OFFSET_BASIS32 2166136261u
#define FNV_PRIME32 16777619u
uint32_t fnv32(const unsigned char* const data, size_t n)
{
uint32_t hash = OFFSET_BASIS32;
for(size_t i = 0; i < n; ++i)
hash = (hash * FNV_PRIME32) xor (uint32_t)data[i];
return hash;
}
template <class T>
uint32_t fnv32(const T& data)
{
return fnv32(&data, sizeof(data));
}
template<> uint32_t fnv32(const std::string& data)
{
return fnv32((const unsigned char*)data.c_str(), data.size());
}
uint32_t fnv1a32(const unsigned char* const data, size_t n)
{
uint32_t hash = OFFSET_BASIS32;
for(size_t i = 0; i < n; ++i)
hash = (hash xor (uint32_t)data[i]) * FNV_PRIME32;
return hash;
}
template <class T>
uint32_t fnv1a32(const T& data)
{
return fnv1a32(&data, sizeof(data));
}
template<> uint32_t fnv1a32(const std::string& data)
{
return fnv1a32((const unsigned char*)data.c_str(), data.size());
}
#endif