added fnv and fnv1a hashing algorithms
This commit is contained in:
parent
829a0370b4
commit
686275ad30
34
utils/hashing/fnv.hpp
Normal file
34
utils/hashing/fnv.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef MEMGRAPH_UTILS_HASHING_FNV_FNV_HPP
|
||||
#define MEMGRAPH_UTILS_HASHING_FNV_FNV_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include "utils/platform.hpp"
|
||||
|
||||
#include "fnv32.hpp"
|
||||
#include "fnv64.hpp"
|
||||
|
||||
// fnv1a is recommended so use it as a default implementation. also use the
|
||||
// platform specific version of the function
|
||||
|
||||
#ifdef MEMGRAPH64
|
||||
|
||||
template <class T>
|
||||
uint64_t fnv(const T& data)
|
||||
{
|
||||
return fnv1a64<T>(data);
|
||||
}
|
||||
|
||||
#elif
|
||||
|
||||
template <class T>
|
||||
uint32_t fnv(const T& data)
|
||||
{
|
||||
return fnv1a32<T>(data);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
52
utils/hashing/fnv32.hpp
Normal file
52
utils/hashing/fnv32.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
#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
|
52
utils/hashing/fnv64.hpp
Normal file
52
utils/hashing/fnv64.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef MEMGRAPH_UTILS_HASHING_FNV_FNV64_HPP
|
||||
#define MEMGRAPH_UTILS_HASHING_FNV_FNV64_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#define OFFSET_BASIS64 14695981039346656037u
|
||||
#define FNV_PRIME64 1099511628211u
|
||||
|
||||
uint64_t fnv64(const unsigned char* const data, size_t n)
|
||||
{
|
||||
uint64_t hash = OFFSET_BASIS64;
|
||||
|
||||
for(size_t i = 0; i < n; ++i)
|
||||
hash = (hash * FNV_PRIME64) xor (uint64_t)data[i];
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
uint64_t fnv64(const T& data)
|
||||
{
|
||||
return fnv64(&data, sizeof(data));
|
||||
}
|
||||
|
||||
template<> uint64_t fnv64(const std::string& data)
|
||||
{
|
||||
return fnv64((const unsigned char*)data.c_str(), data.size());
|
||||
}
|
||||
|
||||
uint64_t fnv1a64(const unsigned char* const data, size_t n)
|
||||
{
|
||||
uint64_t hash = OFFSET_BASIS64;
|
||||
|
||||
for(size_t i = 0; i < n; ++i)
|
||||
hash = (hash xor (uint64_t)data[i]) * FNV_PRIME64;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
uint64_t fnv1a64(const T& data)
|
||||
{
|
||||
return fnv1a64(&data, sizeof(data));
|
||||
}
|
||||
|
||||
template<> uint64_t fnv1a64(const std::string& data)
|
||||
{
|
||||
return fnv1a64((const unsigned char*)data.c_str(), data.size());
|
||||
}
|
||||
|
||||
#endif
|
16
utils/platform.hpp
Normal file
16
utils/platform.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef MEMGRAPH_UTILS_PLATFORM_HPP
|
||||
#define MEMGRAPH_UTILS_PLATFORM_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
// is there a better way?
|
||||
#if UINTPTR_MAX == 0xffffffff
|
||||
#define MEMGRAPH32
|
||||
#elif UINTPTR_MAX == 0xffffffffffffffff
|
||||
#define MEMGRAPH64
|
||||
#else
|
||||
#error Unrecognized platform (neither 32 or 64)
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user