首先,我想知道是否有人知道表示 nD 向量的向量的散列函数?
其次,是否有类似的散列函数,我可以指定一个分辨率,使两个“接近”的向量散列到相同的值?
例如:给定分辨率 r = 0.01 q1 = {1.01, 2.3} q2 = {1.01, 2.31} 将散列到相同的值。
谢谢您的帮助!
也许这样的东西对你有用?
#include <stdint.h>
#include <iostream>
#include <vector>
using namespace std;
// simple variant of ELF hash ... but you could use any general-purpose hashing algorithm here instead
static int GetHashCodeForBytes(const char * bytes, int numBytes)
{
unsigned long h = 0, g;
for (int i=0; i<numBytes; i++)
{
h = ( h << 4 ) + bytes[i];
if (g = h & 0xF0000000L) {h ^= g >> 24;}
h &= ~g;
}
return h;
}
static int GetHashForDouble(double v)
{
return GetHashCodeForBytes((const char *)&v, sizeof(v));
}
static int GetHashForDoubleVector(const vector<double> & v)
{
int ret = 0;
for (int i=0; i<v.size(); i++) ret += ((i+1)*(GetHashForDouble(v[i])));
return ret;
}
int main()
{
vector<double> vec;
vec.push_back(3.14159);
vec.push_back(2.34567);
cout << " Hash code for test vec is: " << GetHashForDoubleVector(vec) << endl;
return 0;
}