矢量的散列函数
问题描述:
首先,我想知道是否有人知道表示n维矢量的矢量的散列函数?矢量的散列函数<double>
其次,有没有类似的散列函数,我可以指定一个分辨率,使两个“关闭”向量散列到相同的值?
例如: 定分辨率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;
}
顾名思义,这不是散列函数。我想你可以为每一个做floor(x * 10),然后使用正常的散列函数。 – 2013-03-27 04:52:00