哈希函数矢量&lt;双&GT; [英] Hash function for vector<double>
本文介绍了哈希函数矢量&lt;双&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我想知道是否有人知道的哈希函数向量重新presenting钕载体?
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?
例如: 定的分辨率R = 0.01 Q1 = {1.01,2.3} Q2 = {1.01,2.31} 将散列到相同的值。
For example: given resolution r = 0.01 q1 = {1.01, 2.3} q2 = {1.01, 2.31} would hash to the same value.
感谢您的帮助!
推荐答案
也许这样的事情会为你工作?
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;
}
这篇关于哈希函数矢量&lt;双&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文