33,007
社区成员
发帖
与我相关
我的任务
分享
cdb_hash(const void *buf, unsigned len)
{
register const unsigned char *p = (const unsigned char *)buf;
register const unsigned char *end = p + len;
register unsigned hash = 5381; /* start value */
while (p < end)
hash = (hash + (hash << 5)) ^ *p++;
return hash;
}
#include <iostream>
#include <map>
#include <vector>
using namespace std;
unsigned
cdb_hash(const void *buf, unsigned len)
{
register const unsigned char *p = (const unsigned char *)buf;
register const unsigned char *end = p + len;
register unsigned hash = 5381; /* start value */
while (p < end)
hash = (hash + (hash << 5)) ^ *p++;
return hash;
}
int main()
{
map<int ,int> cdbmap;
cout << sizeof(int) / sizeof(char) << endl;
unsigned long a = 0 ;
unsigned long b = ~(0L);
cout << b;
for(; a <=b ; a ++ )
{
//cout << a << " "<< cdb_hash(&a,4) << endl;
unsigned t = cdb_hash(&a,4);
if( cdbmap.find(t) == cdbmap.end())
{
cdbmap.insert(map<int,int>::value_type(t,a));
}
else
{
cout<< "有冲突" << a << endl; //这里就可以显示第一次冲突变量a的值
break ;
}
}
return 0;
}
public int hashCode() {
int h = hash;
int len = count;
if (h == 0 && len > 0) {
int off = offset;
char val[] = value;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
unsigned cdb_hash_2nd(const void *buf, unsigned len)
{
unsigned int b = 378551 ;
unsigned int a = 63689 ;
unsigned int hash = 0 ;
unsigned char *str = (unsigned char *)buf;
while (len)
{
hash = hash * a + (*str++ );
a *= b;
--len;
}
return hash;
}