First, I am wondering if anyone knows of a hash function for vector representing an n-D vector?
Second, is there a similar hash function where I can specify a resolution such that two "close" vectors hash to the same value?
For example:
given resolution r = 0.01
q1 = {1.01, 2.3}
q2 = {1.01, 2.31}
would hash to the same value.
Thanks for the help!
Perhaps something like this would work for you?
#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;
}